@@ -109,6 +109,44 @@ void appendUserAndAssistantSeparatelyMatchAppendRound() {
109109 assertThat ("atomic-round and split-commit must converge" , b .snapshot (), is (a .snapshot ()));
110110 }
111111
112+ @ Test
113+ @ DisplayName ("removePendingUserTurn drops a trailing user turn and reports true" )
114+ void removePendingUserTurnDropsTrailingUserTurn () {
115+ ChatTranscript t = new ChatTranscript (null );
116+ t .appendUserTurn ("pending question" );
117+ assertThat ("precondition: one dangling user turn" , t .size (), is (1 ));
118+
119+ boolean removed = t .removePendingUserTurn ();
120+
121+ assertThat ("a dangling user turn must be reported as removed" , removed , is (true ));
122+ assertThat ("transcript is rolled back to its pre-stream (empty) shape" , t .size (), is (0 ));
123+ }
124+
125+ @ Test
126+ @ DisplayName ("removePendingUserTurn is a no-op when the last turn is an assistant turn" )
127+ void removePendingUserTurnKeepsCommittedRound () {
128+ ChatTranscript t = new ChatTranscript (null );
129+ t .appendRound ("q" , "a" ); // last turn is "assistant", not a dangling user turn
130+
131+ boolean removed = t .removePendingUserTurn ();
132+
133+ assertThat ("a committed round has no dangling user turn to remove" , removed , is (false ));
134+ assertThat ("the committed round must be left intact" , t .size (), is (2 ));
135+ assertThat (t .snapshot ().get (0 ).getRole (), is ("user" ));
136+ assertThat (t .snapshot ().get (1 ).getRole (), is ("assistant" ));
137+ }
138+
139+ @ Test
140+ @ DisplayName ("removePendingUserTurn is a safe no-op on an empty transcript" )
141+ void removePendingUserTurnNoOpOnEmptyTranscript () {
142+ ChatTranscript t = new ChatTranscript ("system" );
143+
144+ boolean removed = t .removePendingUserTurn ();
145+
146+ assertThat ("an empty transcript has nothing to remove" , removed , is (false ));
147+ assertThat (t .size (), is (0 ));
148+ }
149+
112150 @ Test
113151 @ DisplayName ("messagesWithPendingUserTurn does NOT mutate the transcript" )
114152 void messagesWithPendingUserTurnDoesNotMutate () {
0 commit comments