@@ -160,36 +160,157 @@ test("rapid Debug flag/reset keeps controls busy and applies only the latest mut
160160 ( btn . textContent ?? "" ) . includes ( "Clear runtime overrides" ) ,
161161 ) ;
162162
163- // Fire flag + reset before the first busy paint can serialize them.
163+ // Fire flag + reset before the first busy paint can serialize them via disabled controls .
164164 await act ( async ( ) => {
165165 usageSwitch ( ) ?. click ( ) ;
166166 resetBtn ( ) ?. click ( ) ;
167167 } ) ;
168- await waitFor ( ( ) => putGates . length >= 2 ) ;
169-
168+ // Serialized: only the first PUT is in flight until it settles.
169+ await waitFor ( ( ) => putGates . length === 1 ) ;
170+ expect ( putBodies ) . toEqual ( [ { usage : true } ] ) ;
170171 expect ( usageSwitch ( ) ?. disabled ) . toBe ( true ) ;
171172 expect ( resetBtn ( ) ?. disabled ) . toBe ( true ) ;
172- expect ( putBodies [ 0 ] ) . toEqual ( { usage : true } ) ;
173- expect ( putBodies [ 1 ] ) . toEqual ( { reset : true } ) ;
174173
175- // Older usage response lands first — must not stick after reset settles.
176174 await act ( async ( ) => {
177175 putGates [ 0 ] ! . resolve ( ) ;
178176 await Promise . resolve ( ) ;
179177 } ) ;
178+ await waitFor ( ( ) => putGates . length === 2 ) ;
179+ expect ( putBodies [ 1 ] ) . toEqual ( { reset : true } ) ;
180+ expect ( usageSwitch ( ) ?. disabled ) . toBe ( true ) ;
181+
180182 await act ( async ( ) => {
181- await new Promise < void > ( resolve => testWindow . setTimeout ( resolve , 0 ) ) ;
183+ putGates [ 1 ] ! . resolve ( ) ;
184+ await Promise . resolve ( ) ;
182185 } ) ;
186+ await waitFor ( ( ) => usageSwitch ( ) ?. disabled === false ) ;
183187 expect ( usageSwitch ( ) ?. getAttribute ( "aria-pressed" ) ) . toBe ( "false" ) ;
184- expect ( usageSwitch ( ) ?. disabled ) . toBe ( true ) ;
188+ expect ( resetBtn ( ) ?. disabled ) . toBe ( false ) ;
189+
190+ await act ( async ( ) => {
191+ root . unmount ( ) ;
192+ } ) ;
193+ container . remove ( ) ;
194+ } ) ;
195+
196+ test ( "Debug PUTs are serialized so reverse-order response settlement cannot leave server ahead of UI" , async ( ) => {
197+ const { createRoot } = await import ( "react-dom/client" ) ;
198+ const container = document . createElement ( "div" ) ;
199+ document . body . append ( container ) ;
200+
201+ type Gate = { resolve : ( ) => void } ;
202+ const putGates : Gate [ ] = [ ] ;
203+ const putStartOrder : string [ ] = [ ] ;
204+ let inFlight = 0 ;
205+ let maxInFlight = 0 ;
206+ let serverSettings : DebugSettings = { ...BASE_SETTINGS , runtimeOverride : { } , env : { ...BASE_SETTINGS . env } } ;
207+ const serverSnapshots : DebugSettings [ ] = [ ] ;
208+
209+ globalThis . fetch = ( async ( input : RequestInfo | URL , init ?: RequestInit ) => {
210+ const url = String ( input ) ;
211+ const method = ( init ?. method ?? "GET" ) . toUpperCase ( ) ;
212+ if ( url . includes ( "/api/debug/logs" ) || url . includes ( "/api/debug/usage-logs" ) || url . includes ( "/api/debug/injection-logs" ) ) {
213+ return Response . json ( [ ] ) ;
214+ }
215+ if ( url . endsWith ( "/api/debug" ) && method === "GET" ) {
216+ return Response . json ( serverSettings ) ;
217+ }
218+ if ( url . endsWith ( "/api/debug" ) && method === "PUT" ) {
219+ const body = JSON . parse ( String ( init ?. body ?? "{}" ) ) as Record < string , unknown > ;
220+ putStartOrder . push ( body . reset === true ? "reset" : Object . keys ( body ) . sort ( ) . join ( "," ) ) ;
221+ inFlight += 1 ;
222+ maxInFlight = Math . max ( maxInFlight , inFlight ) ;
223+ try {
224+ await new Promise < void > ( resolve => {
225+ putGates . push ( { resolve } ) ;
226+ } ) ;
227+ // Apply when the response settles. Concurrent PUTs + reverse resolve
228+ // order (reset, then usage) would leave usage=true on the server while
229+ // browser generation checks show the reset UI — serialization prevents that.
230+ if ( body . reset === true ) {
231+ serverSettings = {
232+ ...BASE_SETTINGS ,
233+ enabled : false ,
234+ usage : false ,
235+ injection : false ,
236+ claude : false ,
237+ runtimeOverride : { } ,
238+ env : { ...BASE_SETTINGS . env } ,
239+ } ;
240+ } else {
241+ serverSettings = {
242+ ...serverSettings ,
243+ enabled : body . debug === undefined ? serverSettings . enabled : body . debug === true ,
244+ usage : body . usage === undefined ? serverSettings . usage : body . usage === true ,
245+ injection : body . injection === undefined ? serverSettings . injection : body . injection === true ,
246+ claude : body . claude === undefined ? serverSettings . claude : body . claude === true ,
247+ runtimeOverride : {
248+ ...serverSettings . runtimeOverride ,
249+ ...( body . debug === undefined ? { } : { debug : body . debug === true } ) ,
250+ ...( body . usage === undefined ? { } : { usage : body . usage === true } ) ,
251+ ...( body . injection === undefined ? { } : { injection : body . injection === true } ) ,
252+ ...( body . claude === undefined ? { } : { claude : body . claude === true } ) ,
253+ } ,
254+ } ;
255+ }
256+ serverSnapshots . push ( {
257+ ...serverSettings ,
258+ runtimeOverride : { ...serverSettings . runtimeOverride } ,
259+ env : { ...serverSettings . env } ,
260+ } ) ;
261+ return Response . json ( serverSettings ) ;
262+ } finally {
263+ inFlight -= 1 ;
264+ }
265+ }
266+ return new Response ( null , { status : 404 } ) ;
267+ } ) as typeof fetch ;
268+
269+ let root ! : Root ;
270+ await act ( async ( ) => {
271+ root = createRoot ( container ) ;
272+ root . render (
273+ < LanguageProvider >
274+ < Debug apiBase = "http://localhost" />
275+ </ LanguageProvider > ,
276+ ) ;
277+ } ) ;
278+ await waitFor ( ( ) => container . querySelector ( 'button.switch[aria-label="Usage extraction"]' ) != null ) ;
279+
280+ const usageSwitch = ( ) => container . querySelector < HTMLButtonElement > ( 'button.switch[aria-label="Usage extraction"]' ) ;
281+ const resetBtn = ( ) => Array . from ( container . querySelectorAll < HTMLButtonElement > ( "button" ) ) . find ( btn =>
282+ ( btn . textContent ?? "" ) . includes ( "Clear runtime overrides" ) ,
283+ ) ;
284+
285+ await act ( async ( ) => {
286+ usageSwitch ( ) ?. click ( ) ;
287+ resetBtn ( ) ?. click ( ) ;
288+ } ) ;
289+
290+ await waitFor ( ( ) => putGates . length === 1 ) ;
291+ expect ( putStartOrder ) . toEqual ( [ "usage" ] ) ;
292+ // Attempt reverse-order settlement: only the in-flight PUT can resolve.
293+ // A concurrent implementation would have putGates[1] available here.
294+ expect ( putGates [ 1 ] ) . toBeUndefined ( ) ;
295+ await act ( async ( ) => {
296+ putGates [ 0 ] ! . resolve ( ) ;
297+ await Promise . resolve ( ) ;
298+ } ) ;
299+ await waitFor ( ( ) => putGates . length === 2 ) ;
300+ expect ( putStartOrder ) . toEqual ( [ "usage" , "reset" ] ) ;
301+ expect ( serverSnapshots [ 0 ] ?. usage ) . toBe ( true ) ;
185302
186303 await act ( async ( ) => {
187304 putGates [ 1 ] ! . resolve ( ) ;
188305 await Promise . resolve ( ) ;
189306 } ) ;
190307 await waitFor ( ( ) => usageSwitch ( ) ?. disabled === false ) ;
308+
309+ expect ( maxInFlight ) . toBe ( 1 ) ;
310+ expect ( serverSettings . usage ) . toBe ( false ) ;
311+ expect ( serverSettings . runtimeOverride ) . toEqual ( { } ) ;
191312 expect ( usageSwitch ( ) ?. getAttribute ( "aria-pressed" ) ) . toBe ( "false" ) ;
192- expect ( resetBtn ( ) ?. disabled ) . toBe ( false ) ;
313+ expect ( serverSnapshots . at ( - 1 ) ?. usage ) . toBe ( false ) ;
193314
194315 await act ( async ( ) => {
195316 root . unmount ( ) ;
0 commit comments