@@ -138,8 +138,9 @@ describe("MCP Server Restriction UI", () => {
138138 expect ( screen . getByTestId ( "restrict-mcp-servers-toggle" ) ) . toBeInTheDocument ( )
139139 } )
140140
141- const toggleLabel = screen . getByTestId ( "restrict-mcp-servers-toggle" )
142- const checkbox = toggleLabel . querySelector ( "input[type='checkbox']" ) as HTMLInputElement
141+ // The toolkit mock forwards `data-testid` to the inner
142+ // <input type="checkbox">, so getByTestId resolves to the checkbox input directly.
143+ const checkbox = screen . getByTestId ( "restrict-mcp-servers-toggle" ) as HTMLInputElement
143144 fireEvent . click ( checkbox )
144145
145146 await waitFor ( ( ) => {
@@ -165,8 +166,8 @@ describe("MCP Server Restriction UI", () => {
165166 expect ( screen . getByTestId ( "restrict-mcp-servers-toggle" ) ) . toBeInTheDocument ( )
166167 } )
167168
168- const toggleLabel = screen . getByTestId ( "restrict-mcp-servers-toggle" )
169- const checkbox = toggleLabel . querySelector ( "input[type='checkbox'] ") as HTMLInputElement
169+ // data-testid is forwarded to the inner checkbox input by the toolkit mock.
170+ const checkbox = screen . getByTestId ( "restrict-mcp-servers-toggle ") as HTMLInputElement
170171 fireEvent . click ( checkbox )
171172
172173 await waitFor ( ( ) => {
@@ -222,17 +223,16 @@ describe("McpServerRestriction subcomponent — flicker regressions", () => {
222223 const onCommit = vitest . fn ( )
223224 render ( < McpServerRestriction customMode = { baseCustomMode } mcpServers = { mcpServers } onCommit = { onCommit } /> )
224225
225- const toggleLabel = screen . getByTestId ( "restrict-mcp-servers-toggle" )
226- const checkbox = toggleLabel . querySelector ( "input[type='checkbox']" ) as HTMLInputElement
226+ // The toolkit mock forwards `data-testid` to the inner checkbox input,
227+ // so getByTestId resolves to the <input type="checkbox"> directly.
228+ const checkbox = screen . getByTestId ( "restrict-mcp-servers-toggle" ) as HTMLInputElement
227229 expect ( checkbox . checked ) . toBe ( false )
228230 expect ( screen . queryByTestId ( "mcp-server-list" ) ) . not . toBeInTheDocument ( )
229231
230232 fireEvent . click ( checkbox )
231233
232234 // Synchronous post-click assertions — no advanceTimers, no host echo.
233- const checkboxAfter = screen
234- . getByTestId ( "restrict-mcp-servers-toggle" )
235- . querySelector ( "input[type='checkbox']" ) as HTMLInputElement
235+ const checkboxAfter = screen . getByTestId ( "restrict-mcp-servers-toggle" ) as HTMLInputElement
236236 expect ( checkboxAfter . checked ) . toBe ( true )
237237 expect ( screen . getByTestId ( "mcp-server-list" ) ) . toBeInTheDocument ( )
238238 // Debounced flush has not fired yet.
@@ -314,10 +314,10 @@ describe("McpServerRestriction subcomponent — flicker regressions", () => {
314314 const initialCommits = onRender . mock . calls . length
315315 expect ( initialCommits ) . toBeGreaterThan ( 0 )
316316
317- // (a) Click — exactly 1 additional commit.
318- const serverCheckbox = screen
319- . getByTestId ( "mcp-server- checkbox-server-a" )
320- . querySelector ( "input[type=' checkbox'] ") as HTMLInputElement
317+ // (a) Click — exactly 1 additional commit. The toolkit mock forwards
318+ // `data-testid` to the inner checkbox input, so getByTestId resolves
319+ // to the <input type=" checkbox"> directly.
320+ const serverCheckbox = screen . getByTestId ( "mcp-server- checkbox-server-a ") as HTMLInputElement
321321 fireEvent . click ( serverCheckbox )
322322 const afterClickCommits = onRender . mock . calls . length
323323 expect ( afterClickCommits - initialCommits ) . toBe ( 1 )
@@ -340,4 +340,57 @@ describe("McpServerRestriction subcomponent — flicker regressions", () => {
340340 vitest . useRealTimers ( )
341341 }
342342 } )
343+
344+ /**
345+ * Test 4 — Concurrent-edit safety: the debounced flush must not clobber a
346+ * newer edit to another field of the same mode made within the 150 ms
347+ * window.
348+ *
349+ * Repro: the user toggles a per-server checkbox (schedules the debounced
350+ * flush), then within the debounce window another field of the same mode
351+ * (e.g. `name`) changes — the parent re-renders the component with an
352+ * updated `customMode`. Before the fix, the flush spread the STALE
353+ * `customMode` captured when the timeout was scheduled, sending the old
354+ * `name` back to the host and clobbering the newer value.
355+ *
356+ * After the fix, the flush merges `allowedMcpServers` into the freshest
357+ * `customMode` (via a ref), so the committed snapshot carries the updated
358+ * `name`.
359+ */
360+ it ( "Test 4: debounced flush merges into the latest customMode, not the stale snapshot" , ( ) => {
361+ vitest . useFakeTimers ( )
362+ try {
363+ const onCommit = vitest . fn ( )
364+ const customMode = { ...baseCustomMode , name : "Old Name" , allowedMcpServers : [ ] as string [ ] }
365+
366+ const { rerender } = render (
367+ < McpServerRestriction customMode = { customMode } mcpServers = { mcpServers } onCommit = { onCommit } /> ,
368+ )
369+
370+ // User edits the allowlist (schedules the debounced flush). The toolkit
371+ // mock forwards `data-testid` to the inner checkbox input, so getByTestId
372+ // resolves to the <input type="checkbox"> directly.
373+ const serverCheckbox = screen . getByTestId ( "mcp-server-checkbox-server-a" ) as HTMLInputElement
374+ fireEvent . click ( serverCheckbox )
375+ expect ( onCommit ) . not . toHaveBeenCalled ( ) // debounce hasn't fired yet
376+
377+ // Within the debounce window, another field of the same mode changes.
378+ const updatedCustomMode = { ...customMode , name : "New Name" }
379+ rerender (
380+ < McpServerRestriction customMode = { updatedCustomMode } mcpServers = { mcpServers } onCommit = { onCommit } /> ,
381+ )
382+
383+ // Let the 150 ms debounce fire.
384+ vitest . advanceTimersByTime ( 200 )
385+
386+ expect ( onCommit ) . toHaveBeenCalledTimes ( 1 )
387+ const [ , committedConfig ] = onCommit . mock . calls [ 0 ]
388+ // The newer field value must be preserved (not clobbered by the stale snapshot).
389+ expect ( committedConfig . name ) . toBe ( "New Name" )
390+ // And the user's allowlist edit must still be present.
391+ expect ( committedConfig . allowedMcpServers ) . toEqual ( [ "server-a" ] )
392+ } finally {
393+ vitest . useRealTimers ( )
394+ }
395+ } )
343396} )
0 commit comments