1- import { afterAll , afterEach , expect , test } from "bun:test"
1+ import { afterAll , afterEach , expect , spyOn , test } from "bun:test"
22import { GlobalRegistrator } from "@happy-dom/global-registrator"
33import { act , createElement } from "react"
44import { createRoot , type Root } from "react-dom/client"
@@ -51,13 +51,8 @@ const messages: UIMessage[] = [{
5151 metadata : { opencode : { created : 2 } } ,
5252} ]
5353
54- async function renderLiveTurn ( steady : boolean , status : "streaming" | "ready" = "streaming" ) {
55- useSteadyLiveTurnStore . getState ( ) . setEnabled ( steady )
56- const container = document . createElement ( "div" )
57- document . body . append ( container )
58- const root = createRoot ( container )
59- mounted = { root, container }
60- await act ( async ( ) => root . render ( createElement ( QueryClientProvider , { client : queryClient ,
54+ function liveTurnView ( status : "streaming" | "ready" , content : UIMessage [ ] = messages ) {
55+ return createElement ( QueryClientProvider , { client : queryClient ,
6156 children : createElement ( DenAuthProvider , { children : createElement ( PlatformProvider , {
6257 value : createDefaultPlatform ( ) ,
6358 children : createElement ( MessageListProvider , {
@@ -67,16 +62,70 @@ async function renderLiveTurn(steady: boolean, status: "streaming" | "ready" = "
6762 onForkAtMessage : ( ) => { } , onEditUserMessage : ( ) => { } ,
6863 onMcpReconnect : async ( ) => { throw new Error ( "Unexpected reconnect" ) } ,
6964 onMcpReopenAuthorization : async ( ) => { } ,
70- children : createElement ( MessageList , { messages, status, activityStatus : status === "streaming" ? "responding" : "idle" } ) ,
65+ children : createElement ( MessageList , { messages : content , status, activityStatus : status === "streaming" ? "responding" : "idle" } ) ,
7166 } ) ,
72- } ) } ) } ) ) )
67+ } ) } ) } )
68+ }
69+
70+ async function renderLiveTurn ( steady : boolean , status : "streaming" | "ready" = "streaming" ) {
71+ useSteadyLiveTurnStore . getState ( ) . setEnabled ( steady )
72+ const container = document . createElement ( "div" )
73+ document . body . append ( container )
74+ const root = createRoot ( container )
75+ mounted = { root, container }
76+ await act ( async ( ) => root . render ( liveTurnView ( status ) ) )
7377 return container
7478}
7579
76- test ( "off: a running turn is split at its latest text and its steps area holds its height" , async ( ) => {
77- const container = await renderLiveTurn ( false )
78- expect ( container . querySelector ( "[data-live-steps]" ) ) . not . toBeNull ( )
79- expect ( container . querySelector ( "[data-live-turn-reserve]" ) ) . toBeNull ( )
80+ test ( "off: a running turn splits at its latest text without holding a blank steps panel open" , async ( ) => {
81+ // A tall live tool detail can disappear when the tool settles or the latest
82+ // answer moves. Simulate real layout: happy-dom does not measure content.
83+ const measure = spyOn ( HTMLElement . prototype , "getBoundingClientRect" ) . mockImplementation ( function ( this : HTMLElement ) {
84+ return new DOMRect ( 0 , 0 , 600 , this . hasAttribute ( "data-live-steps" ) ? 900 : 0 )
85+ } )
86+ try {
87+ const container = await renderLiveTurn ( false )
88+ const steps = container . querySelector < HTMLElement > ( "[data-live-steps]" )
89+ expect ( steps ) . not . toBeNull ( )
90+ expect ( steps ?. style . minHeight ) . toBe ( "" )
91+ expect ( container . querySelector ( "[data-live-turn-reserve]" ) ) . toBeNull ( )
92+ // Toggling the rollout must not leave a reserved height on the off path.
93+ await act ( async ( ) => useSteadyLiveTurnStore . getState ( ) . setEnabled ( true ) )
94+ await act ( async ( ) => useSteadyLiveTurnStore . getState ( ) . setEnabled ( false ) )
95+ expect ( container . querySelector < HTMLElement > ( "[data-live-steps]" ) ?. style . minHeight ) . toBe ( "" )
96+ } finally {
97+ measure . mockRestore ( )
98+ }
99+ } )
100+
101+ test ( "off: settling a tool and advancing the answer releases its old height before the turn ends" , async ( ) => {
102+ let height = 900
103+ const measure = spyOn ( HTMLElement . prototype , "getBoundingClientRect" ) . mockImplementation ( function ( this : HTMLElement ) {
104+ return new DOMRect ( 0 , 0 , 600 , this . hasAttribute ( "data-live-steps" ) ? height : 0 )
105+ } )
106+ try {
107+ const container = await renderLiveTurn ( false )
108+ const root = mounted ?. root
109+ if ( ! root ) throw new Error ( "Missing live turn root" )
110+ // The same call settles, then the next text changes the steps/answer split.
111+ // Neither transition is a pointer/key event that could release a height hold.
112+ const next : UIMessage [ ] = messages . map ( message => message . role !== "assistant" ? message : {
113+ ...message ,
114+ parts : message . parts . map ( part => part . type === "dynamic-tool" && part . toolCallId === "call_after"
115+ ? { ...part , state : "output-available" , output : "No conflicts." } : part ) . concat ( { type : "text" , text : "Available times are ready." } ) ,
116+ } )
117+ height = 40
118+ await act ( async ( ) => root . render ( liveTurnView ( "streaming" , next ) ) )
119+ expect ( container . textContent ) . toContain ( "Available times are ready." )
120+ expect ( container . querySelector < HTMLElement > ( "[data-live-steps]" ) ?. style . minHeight ) . toBe ( "" )
121+ expect ( container . querySelector ( "[data-live-turn-reserve]" ) ) . toBeNull ( )
122+ await act ( async ( ) => root . render ( liveTurnView ( "ready" , next ) ) )
123+ expect ( container . textContent ) . toContain ( "Available times are ready." )
124+ expect ( container . querySelector ( "[data-live-steps]" ) ) . toBeNull ( )
125+ expect ( container . querySelector ( "[data-live-turn-reserve]" ) ) . toBeNull ( )
126+ } finally {
127+ measure . mockRestore ( )
128+ }
80129} )
81130
82131test ( "on: a running turn keeps its parts in arrival order, with no steps area to hold open" , async ( ) => {
0 commit comments