@@ -121,14 +121,126 @@ function fixture() {
121121 } ,
122122 } ;
123123}
124+ async function loadedInstructions ( ) {
125+ const field = screen . getByRole ( "textbox" , { name : "Instructions" } ) ;
126+ await waitFor ( ( ) => expect ( field ) . toBeEnabled ( ) ) ;
127+ return field ;
128+ }
129+ it . each ( [ false , true ] ) (
130+ "keeps the full form mounted across settings and catalog loading (catalog fails: %s)" ,
131+ async ( fails ) => {
132+ const h = fixture ( ) ;
133+ const user = userEvent . setup ( ) ;
134+ const settings = deferred < HostResponse > ( ) ;
135+ const catalog = deferred < HostResponse > ( ) ;
136+ const data = JSON . parse ( response ( ) . body ) ;
137+ Object . assign ( data . agents [ 0 ] , {
138+ model_id : "one" ,
139+ reasoning_effort : "high" ,
140+ model_selection_status : 1 ,
141+ } ) ;
142+ const settingsResponse = { ...response ( ) , body : JSON . stringify ( data ) } ;
143+ const request = h . request . getMockImplementation ( ) ;
144+ if ( ! request ) throw new Error ( "Missing request fixture" ) ;
145+ h . request . mockImplementation ( ( input ) => {
146+ if ( input . url . endsWith ( "/list-agents" ) ) return settings . promise ;
147+ if ( input . url . endsWith ( "/get-model-catalog" ) ) return catalog . promise ;
148+ return request ( input ) ;
149+ } ) ;
150+ render ( < h . Tab agent = { agent ( ) } save = { vi . fn ( ) } /> , { wrapper : StrictMode } ) ;
151+ const field = screen . getByRole ( "textbox" , { name : "Instructions" } ) ;
152+ const harness = screen . getByRole ( "button" , { name : "Harness: Builderlab" } ) ;
153+ const model = screen . getByRole ( "button" , { name : "Model: …" } ) ;
154+ const thinking = screen . getByRole ( "combobox" , { name : "Thinking" } ) ;
155+ const region = field . closest ( "[aria-busy]" ) ;
156+ try {
157+ await waitFor ( ( ) =>
158+ expect ( h . request ) . toHaveBeenCalledWith (
159+ expect . objectContaining ( {
160+ url : expect . stringContaining ( "/list-agents" ) ,
161+ } ) ,
162+ ) ,
163+ ) ;
164+ expect ( region ) . toHaveAttribute ( "aria-busy" , "true" ) ;
165+ expect ( region ?. querySelector ( ".sr-only" ) ) . toBeInTheDocument ( ) ;
166+ expect ( region ?. querySelector ( ".buzz-loading-region-feedback" ) ) . toBeNull ( ) ;
167+ expect ( model ) . toBeDisabled ( ) ;
168+ expect ( model . querySelector ( ".buzz-select-leading" ) ) . toBeNull ( ) ;
169+ expect ( thinking ) . toBeDisabled ( ) ;
170+ expect ( field ) . toBeDisabled ( ) ;
171+ expect ( field ) . toHaveValue ( "" ) ;
172+ await user . type ( field , "Must not become a draft." ) ;
173+ expect ( field ) . toHaveValue ( "" ) ;
174+ expect ( screen . queryByRole ( "button" , { name : "Save" } ) ) . toBeNull ( ) ;
175+ act ( ( ) => harness . focus ( ) ) ;
176+ await act ( async ( ) => settings . resolve ( settingsResponse ) ) ;
177+ await waitFor ( ( ) => expect ( field ) . toBeEnabled ( ) ) ;
178+ await waitFor ( ( ) =>
179+ expect ( h . request ) . toHaveBeenCalledWith (
180+ expect . objectContaining ( {
181+ url : expect . stringContaining ( "/get-model-catalog" ) ,
182+ } ) ,
183+ ) ,
184+ ) ;
185+ expect ( harness ) . toHaveFocus ( ) ;
186+ expect ( screen . getByRole ( "textbox" , { name : "Instructions" } ) ) . toBe ( field ) ;
187+ expect ( field ) . toHaveValue ( "First instructions." ) ;
188+ expect ( region ) . toHaveAttribute ( "aria-busy" , "true" ) ;
189+ expect ( region ?. querySelector ( ".sr-only" ) ) . toBeInTheDocument ( ) ;
190+ expect ( model ) . toBeDisabled ( ) ;
191+ expect ( thinking ) . toBeDisabled ( ) ;
192+ await user . type ( field , " Draft." ) ;
193+ if ( fails ) {
194+ await act ( async ( ) =>
195+ catalog . resolve ( { status : 400 , headers : { } , body : "{}" } ) ,
196+ ) ;
197+ expect ( await screen . findByRole ( "alert" ) ) . toHaveTextContent ( "HTTP 400" ) ;
198+ expect ( model ) . toHaveTextContent ( "Unavailable" ) ;
199+ expect ( model . querySelector ( ".buzz-select-leading" ) ) . toBeNull ( ) ;
200+ expect ( model ) . toBeDisabled ( ) ;
201+ expect ( thinking ) . toBeDisabled ( ) ;
202+ h . request . mockImplementation ( request ) ;
203+ await user . click ( screen . getByRole ( "button" , { name : "Retry models" } ) ) ;
204+ } else {
205+ const result = await request ( {
206+ url : "https://builderlab.example/get-model-catalog" ,
207+ method : "POST" ,
208+ } ) ;
209+ await act ( async ( ) => catalog . resolve ( result ) ) ;
210+ }
211+ await waitFor ( ( ) => expect ( model ) . toBeEnabled ( ) ) ;
212+ expect ( model ) . toHaveTextContent ( "Friendly Model" ) ;
213+ expect ( thinking ) . toHaveTextContent ( "High" ) ;
214+ expect ( region ) . toHaveAttribute ( "aria-busy" , "false" ) ;
215+ expect ( field ) . toHaveValue ( "First instructions. Draft." ) ;
216+ expect ( screen . getByRole ( "button" , { name : / ^ M o d e l : / } ) ) . toBe ( model ) ;
217+ expect ( screen . getByRole ( "combobox" , { name : "Thinking" } ) ) . toBe ( thinking ) ;
218+ expect ( screen . getByRole ( "button" , { name : "Save" } ) ) . toBeEnabled ( ) ;
219+ } finally {
220+ await act ( async ( ) => {
221+ settings . resolve ( response ( ) ) ;
222+ catalog . resolve ( { status : 200 , headers : { } , body : '{"models":[]}' } ) ;
223+ } ) ;
224+ }
225+ } ,
226+ ) ;
124227it ( "retries a failed load and refuses to save a draft through another account" , async ( ) => {
125228 const h = fixture ( ) ;
126229 const user = userEvent . setup ( ) ;
127230 h . request . mockResolvedValueOnce ( { ...response ( ) , status : 400 } ) ;
128231 render ( < h . Tab agent = { agent ( ) } save = { vi . fn ( ) } /> , { wrapper : StrictMode } ) ;
129232 expect ( await screen . findByRole ( "alert" ) ) . toHaveTextContent ( "HTTP 400" ) ;
233+ expect (
234+ screen . getByRole ( "button" , { name : "Harness: Builderlab" } ) ,
235+ ) . toBeVisible ( ) ;
236+ expect (
237+ screen . getByRole ( "button" , { name : "Model: Unavailable" } ) ,
238+ ) . toBeDisabled ( ) ;
239+ expect ( screen . getByRole ( "combobox" , { name : "Thinking" } ) ) . toBeDisabled ( ) ;
240+ expect ( screen . getByRole ( "textbox" , { name : "Instructions" } ) ) . toBeDisabled ( ) ;
241+ expect ( screen . queryByRole ( "button" , { name : "Save" } ) ) . toBeNull ( ) ;
130242 await user . click ( screen . getByRole ( "button" , { name : "Retry" } ) ) ;
131- const field = await screen . findByRole ( "textbox" , { name : "Instructions" } ) ;
243+ const field = await loadedInstructions ( ) ;
132244 expect ( field ) . toHaveValue ( "First instructions." ) ;
133245 await user . type ( field , "Changed." ) ;
134246 await act ( async ( ) => h . changeAccount ( ) ) ;
@@ -146,7 +258,7 @@ it("switching agents hides the old draft and ignores the switched-away read", as
146258 const mounted = render ( < h . Tab agent = { agent ( ) } save = { vi . fn ( ) } /> , {
147259 wrapper : StrictMode ,
148260 } ) ;
149- const field = await screen . findByRole ( "textbox" , { name : "Instructions" } ) ;
261+ const field = await loadedInstructions ( ) ;
150262 await user . type ( field , "Unsaved." ) ;
151263 const delayed = deferred < HostResponse > ( ) ;
152264 h . request . mockReturnValueOnce ( delayed . promise ) ;
@@ -159,14 +271,14 @@ it("switching agents hides the old draft and ignores the switched-away read", as
159271 ) ,
160272 ) . toHaveLength ( 2 ) ,
161273 ) ;
162- expect ( screen . getByRole ( "status" ) ) . toHaveTextContent ( "Loading settings" ) ;
163274 expect (
164- screen . queryByRole ( "textbox" , { name : "Instructions" } ) ,
165- ) . not . toBeInTheDocument ( ) ;
275+ screen . getByRole ( "textbox" , { name : "Instructions" } ) ,
276+ ) . toBeDisabled ( ) ;
277+ expect ( screen . getByRole ( "textbox" , { name : "Instructions" } ) ) . toHaveValue (
278+ "" ,
279+ ) ;
166280 mounted . rerender ( < h . Tab agent = { agent ( ) } save = { vi . fn ( ) } /> ) ;
167- expect (
168- await screen . findByRole ( "textbox" , { name : "Instructions" } ) ,
169- ) . toHaveValue ( "First instructions." ) ;
281+ expect ( await loadedInstructions ( ) ) . toHaveValue ( "First instructions." ) ;
170282 } finally {
171283 await act ( async ( ) => {
172284 delayed . resolve ( response ( ) ) ;
@@ -252,7 +364,7 @@ it("keeps hosted instructions on cancellation and resets a confirmed discard", a
252364 "aria-selected" ,
253365 "true" ,
254366 ) ;
255- const field = await screen . findByRole ( "textbox" , { name : "Instructions" } ) ;
367+ const field = await loadedInstructions ( ) ;
256368 expect ( await screen . findByRole ( "button" , { name : / ^ M o d e l : / } ) ) . toBeVisible ( ) ;
257369 expect ( screen . getByRole ( "combobox" , { name : "Thinking" } ) ) . toBeVisible ( ) ;
258370 expect ( screen . queryByText ( "Workspace" ) ) . toBeNull ( ) ;
@@ -308,7 +420,7 @@ it("keeps hosted instructions on cancellation and resets a confirmed discard", a
308420 expect ( screen . queryByRole ( "button" , { name : / ^ M o d e l : / } ) ) . toBeNull ( ) ;
309421 expect ( screen . queryByRole ( "combobox" , { name : "Thinking" } ) ) . toBeNull ( ) ;
310422 await user . click ( screen . getByRole ( "tab" , { name : "Identity" } ) ) ;
311- const fresh = await screen . findByRole ( "textbox" , { name : "Instructions" } ) ;
423+ const fresh = await loadedInstructions ( ) ;
312424 expect ( fresh ) . not . toBe ( field ) ;
313425 expect ( fresh ) . toHaveValue ( "First instructions." ) ;
314426 expect ( screen . queryByRole ( "alert" ) ) . toBeNull ( ) ;
@@ -320,7 +432,7 @@ it("guards hosted model drafts and a pending model save", async () => {
320432 const h = fixture ( ) ;
321433 const user = userEvent . setup ( ) ;
322434 renderTabs ( h ) ;
323- await screen . findByRole ( "textbox" , { name : "Instructions" } ) ;
435+ await loadedInstructions ( ) ;
324436 const modelTrigger = await screen . findByRole ( "button" , { name : / ^ M o d e l : / } ) ;
325437 await waitFor ( ( ) => expect ( modelTrigger ) . toBeEnabled ( ) ) ;
326438 await user . click ( modelTrigger ) ;
@@ -361,9 +473,7 @@ it("guards hosted model drafts and a pending model save", async () => {
361473 expect ( screen . getByText ( "Agent settings" ) ) . toBeVisible ( ) ;
362474 expect ( screen . queryByRole ( "button" , { name : / ^ M o d e l : / } ) ) . toBeNull ( ) ;
363475 await user . click ( screen . getByRole ( "tab" , { name : "Identity" } ) ) ;
364- expect (
365- await screen . findByRole ( "textbox" , { name : "Instructions" } ) ,
366- ) . toHaveValue ( "First instructions." ) ;
476+ expect ( await loadedInstructions ( ) ) . toHaveValue ( "First instructions." ) ;
367477 expect (
368478 await screen . findByRole ( "button" , { name : / ^ M o d e l : / } ) ,
369479 ) . toHaveTextContent ( "Choose a model" ) ;
@@ -377,7 +487,7 @@ it("saves model and instructions together through one footer action", async () =
377487 const h = fixture ( ) ;
378488 const user = userEvent . setup ( ) ;
379489 renderTabs ( h ) ;
380- const field = await screen . findByRole ( "textbox" , { name : "Instructions" } ) ;
490+ const field = await loadedInstructions ( ) ;
381491 expect ( screen . queryByRole ( "button" , { name : "Save" } ) ) . toBeNull ( ) ;
382492 const modelTrigger = await screen . findByRole ( "button" , { name : / ^ M o d e l : / } ) ;
383493 await waitFor ( ( ) => expect ( modelTrigger ) . toBeEnabled ( ) ) ;
0 commit comments