@@ -3748,4 +3748,87 @@ describe("loadChatHistory inFlightRun adoption (#90755)", () => {
37483748 expect ( state . chatRunId ) . toBe ( "run-fresh" ) ;
37493749 expect ( state . chatStream ) . toBe ( "fresh live text" ) ;
37503750 } ) ;
3751+
3752+ it ( "reconciles overlap when the buffer has leading whitespace not present in the committed message" , async ( ) => {
3753+ // Normalize whitespace before comparison: if the buffer starts with \n but
3754+ // the committed assistant message does not, the dedup should still work.
3755+ const messages = [
3756+ { role : "user" , content : [ { type : "text" , text : "go ahead" } ] } ,
3757+ { role : "assistant" , content : [ { type : "text" , text : "Starting analysis..." } ] } ,
3758+ ] ;
3759+ const request = vi . fn ( ) . mockResolvedValue ( {
3760+ messages,
3761+ inFlightRun : { runId : "run-ws" , text : "\nStarting analysis...\nPhase 1 complete.\nPhase 2 in progress..." } ,
3762+ } ) ;
3763+ const state = createState ( {
3764+ client : { request } as unknown as ChatState [ "client" ] ,
3765+ connected : true ,
3766+ chatRunId : null ,
3767+ chatStream : null ,
3768+ chatStreamStartedAt : null ,
3769+ chatError : "previous stale error" ,
3770+ lastError : "another stale error" ,
3771+ } ) ;
3772+
3773+ await loadChatHistory ( state ) ;
3774+
3775+ expect ( state . chatRunId ) . toBe ( "run-ws" ) ;
3776+ // The leading \n from the buffer is preserved; only the overlapping text is deduped
3777+ expect ( state . chatStream ) . toContain ( "Phase 1 complete" ) ;
3778+ expect ( state . chatStream ) . toContain ( "Phase 2 in progress" ) ;
3779+ expect ( state . chatStream ) . not . toContain ( "Starting analysis..." ) ;
3780+ expect ( state . chatStreamStartedAt ) . toEqual ( expect . any ( Number ) ) ;
3781+ // Stale errors cleared on adoption
3782+ expect ( state . chatError ) . toBeNull ( ) ;
3783+ expect ( state . lastError ) . toBeNull ( ) ;
3784+ } ) ;
3785+
3786+ it ( "preserves full buffer when last visible message is not an assistant reply" , async ( ) => {
3787+ // Only assistant messages participate in reconciliation; user messages
3788+ // should not cause the buffer to be trimmed.
3789+ const messages = [
3790+ { role : "user" , content : [ { type : "text" , text : "run lint" } ] } ,
3791+ ] ;
3792+ const request = vi . fn ( ) . mockResolvedValue ( {
3793+ messages,
3794+ inFlightRun : { runId : "run-nonassist" , text : "Lint output:\n35 errors found" } ,
3795+ } ) ;
3796+ const state = createState ( {
3797+ client : { request } as unknown as ChatState [ "client" ] ,
3798+ connected : true ,
3799+ chatRunId : null ,
3800+ chatStream : null ,
3801+ chatStreamStartedAt : null ,
3802+ } ) ;
3803+
3804+ await loadChatHistory ( state ) ;
3805+
3806+ expect ( state . chatRunId ) . toBe ( "run-nonassist" ) ;
3807+ // Full buffer adopted since last message is user, not assistant
3808+ expect ( state . chatStream ) . toBe ( "Lint output:\n35 errors found" ) ;
3809+ } ) ;
3810+
3811+ it ( "clears stale errors when adopting an in-flight run" , async ( ) => {
3812+ const messages = [ { role : "user" , content : [ { type : "text" , text : "Hello" } ] } ] ;
3813+ const request = vi . fn ( ) . mockResolvedValue ( {
3814+ messages,
3815+ inFlightRun : { runId : "run-err" , text : "fresh stream" } ,
3816+ } ) ;
3817+ const state = createState ( {
3818+ client : { request } as unknown as ChatState [ "client" ] ,
3819+ connected : true ,
3820+ chatRunId : null ,
3821+ chatStream : null ,
3822+ chatStreamStartedAt : null ,
3823+ chatError : "gateway timeout" ,
3824+ lastError : "request failed" ,
3825+ } ) ;
3826+
3827+ await loadChatHistory ( state ) ;
3828+
3829+ expect ( state . chatRunId ) . toBe ( "run-err" ) ;
3830+ expect ( state . chatStream ) . toBe ( "fresh stream" ) ;
3831+ expect ( state . chatError ) . toBeNull ( ) ;
3832+ expect ( state . lastError ) . toBeNull ( ) ;
3833+ } ) ;
37513834} ) ;
0 commit comments