@@ -11,11 +11,11 @@ const mocks = vi.hoisted(() => ({
1111 markdownRenderer : vi . fn ( ) ,
1212} ) ) ;
1313
14- vi . mock ( '@/infrastructure/markdown' , ( ) => ( {
15- MarkdownRenderer : ( props : { content : string ; isStreaming ?: boolean } ) => {
14+ vi . mock ( '@/infrastructure/markdown' , async ( ) => ( {
15+ MarkdownRenderer : ( await import ( 'react' ) ) . memo ( ( props : { content : string ; isStreaming ?: boolean } ) => {
1616 mocks . markdownRenderer ( props ) ;
1717 return < div data-testid = "markdown-renderer" > { props . content } </ div > ;
18- } ,
18+ } ) ,
1919} ) ) ;
2020
2121vi . mock ( './modern/FlowChatContext' , ( ) => ( {
@@ -51,6 +51,25 @@ describe('FlowTextBlock', () => {
5151 vi . clearAllMocks ( ) ;
5252 } ) ;
5353
54+ it ( 'reparses only the live semantic suffix and retains the source owner and completed prefix nodes' , async ( ) => {
55+ const content = 'A paragraph with **bold** text and Unicode 中文.\n\n' . repeat ( 800 ) ;
56+ const item : FlowTextItem = { id : 'long-stream' , type : 'text' , timestamp : 1 , status : 'streaming' , isStreaming : true , isMarkdown : true , content } ;
57+ act ( ( ) => root . render ( < FlowTextBlock textItem = { item } /> ) ) ;
58+ const owner = container . querySelector ( '[data-flow-item-id]' ) ;
59+ const prefix = container . querySelector ( '[data-testid="markdown-renderer"]' ) ;
60+ expect ( mocks . markdownRenderer . mock . calls . length ) . toBeGreaterThan ( 5 ) ;
61+ expect ( mocks . markdownRenderer . mock . calls . filter ( ( [ props ] ) => props . isStreaming ) ) . toHaveLength ( 1 ) ;
62+ mocks . markdownRenderer . mockClear ( ) ;
63+ const appended = `${ content } New streaming suffix.` ;
64+ act ( ( ) => root . render ( < FlowTextBlock textItem = { { ...item , content : appended } } /> ) ) ;
65+ await act ( async ( ) => { await vi . advanceTimersByTimeAsync ( 1000 ) ; } ) ;
66+ expect ( container . textContent ) . toBe ( appended ) ;
67+ expect ( container . querySelector ( '[data-flow-item-id]' ) ) . toBe ( owner ) ;
68+ expect ( container . querySelector ( '[data-testid="markdown-renderer"]' ) ) . toBe ( prefix ) ;
69+ expect ( mocks . markdownRenderer . mock . calls . every ( ( [ props ] ) => props . content . length < 4000 ) ) . toBe ( true ) ;
70+ expect ( mocks . markdownRenderer . mock . calls . some ( ( [ props ] ) => props . content . includes ( 'New streaming suffix.' ) ) ) . toBe ( true ) ;
71+ } ) ;
72+
5473 it ( 'does not re-render completed markdown just to settle streaming growth state' , async ( ) => {
5574 const textItem : FlowTextItem = {
5675 id : 'text-1' ,
@@ -70,6 +89,23 @@ describe('FlowTextBlock', () => {
7089 expect ( mocks . markdownRenderer ) . toHaveBeenCalledTimes ( 1 ) ;
7190 } ) ;
7291
92+ it ( 'keeps a selected sealed live section mounted without reparsing when a later section grows' , async ( ) => {
93+ const prefix = 'A stable paragraph.\n\n' . repeat ( 150 ) ;
94+ const item : FlowTextItem = { id : 'sectioned' , type : 'text' , timestamp : 1 , status : 'streaming' , isStreaming : true , isMarkdown : true , content : prefix + 'Changing suffix.' } ;
95+ act ( ( ) => root . render ( < FlowTextBlock textItem = { item } markdownSection = { { start : 0 , end : prefix . length } } /> ) ) ;
96+ const node = container . querySelector ( '[data-testid="markdown-renderer"]' ) ! . firstChild ! ;
97+ const range = document . createRange ( ) ; range . setStart ( node , 2 ) ; range . setEnd ( node , 16 ) ;
98+ const selection = window . getSelection ( ) ! ; selection . removeAllRanges ( ) ; selection . addRange ( range ) ;
99+ mocks . markdownRenderer . mockClear ( ) ;
100+ act ( ( ) => root . render ( < FlowTextBlock textItem = { { ...item , content : item . content + ' More output.' . repeat ( 600 ) } } markdownSection = { { start : 0 , end : prefix . length } } /> ) ) ;
101+ await act ( async ( ) => { await vi . advanceTimersByTimeAsync ( 1000 ) ; } ) ;
102+ expect ( container . querySelector ( '[data-testid="markdown-renderer"]' ) ! . firstChild ) . toBe ( node ) ;
103+ expect ( selection . toString ( ) ) . toBe ( prefix . slice ( 2 , 16 ) ) ;
104+ expect ( mocks . markdownRenderer ) . not . toHaveBeenCalled ( ) ;
105+ expect ( container . querySelector ( '[data-streaming]' ) ?. getAttribute ( 'data-streaming' ) ) . toBe ( 'false' ) ;
106+ selection . removeAllRanges ( ) ;
107+ } ) ;
108+
73109 it . each ( [ '' , ' \n\t\u3000' ] ) ( 'renders no wrapper for blank text %j' , content => {
74110 const textItem : FlowTextItem = {
75111 id : 'blank' , type : 'text' , timestamp : 1 , status : 'completed' ,
0 commit comments