Merge pull request #2255 from modelcontextprotocol/v2/fix/2250-import-json-debounce-flake

fix(web): remove the wall-clock race in the import-JSON debounce guard test
This commit is contained in:
Cliff Hall 2026-09-05 12:39:05 -04:00 • committed by GitHub
commit abd28bc55b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -1,12 +1,14 @@
import { describe, it, expect, vi } from "vitest"; import { describe, it, expect, vi } from "vitest";
import userEvent from "@testing-library/user-event"; import userEvent from "@testing-library/user-event";
import { import {
act,
renderWithMantine, renderWithMantine,
screen, screen,
fireEvent, fireEvent,
waitFor, waitFor,
} from "../../../test/renderWithMantine"; } from "../../../test/renderWithMantine";
import { setAceText } from "../../../test/aceEditor"; import { setAceText } from "../../../test/aceEditor";
import { VALIDATE_DEBOUNCE_MS } from "../../../hooks/useServerJsonImport";
import { ServerImportJsonModal } from "./ServerImportJsonModal"; import { ServerImportJsonModal } from "./ServerImportJsonModal";
const npmJson = JSON.stringify({ const npmJson = JSON.stringify({
@ -221,28 +223,41 @@ describe("ServerImportJsonModal", () => {
expect(screen.getByRole("button", { name: "Add Server" })).toBeDisabled(); expect(screen.getByRole("button", { name: "Add Server" })).toBeDisabled();
}); });
// The window this exercises is the one between an edit and the debounce that
// re-disables the button, so the whole test runs on fake timers: the pending
// re-validation then cannot land unless this test advances it, and the window
// stops depending on how long the machine takes to get from the paste to the
// click. On real timers a loaded box could spend more than
// VALIDATE_DEBOUNCE_MS there, re-disable the button, and turn the click into a
// no-op that sets no submit error at all (#2250).
it("guards against a live edit made before the debounce re-validates", async () => { it("guards against a live edit made before the debounce re-validates", async () => {
const onAddServer = vi.fn(); vi.useFakeTimers();
renderWithMantine( try {
<ServerImportJsonModal const onAddServer = vi.fn();
opened renderWithMantine(
existingIds={[]} <ServerImportJsonModal
onClose={vi.fn()} opened
onAddServer={onAddServer} existingIds={[]}
/>, onClose={vi.fn()}
); onAddServer={onAddServer}
await pasteJson(npmJson); />,
await waitFor(() => );
expect(screen.getByRole("button", { name: "Add Server" })).toBeEnabled(), await pasteJson(npmJson);
); // Let the first validation land, so the button is enabled to click.
// Replace with invalid content; the button hasn't re-disabled yet (the await act(async () => {
// debounce is still pending), so clicking exercises the submit-time guard. await vi.advanceTimersByTimeAsync(VALIDATE_DEBOUNCE_MS);
await pasteJson("{not json"); });
fireEvent.click(screen.getByRole("button", { name: "Add Server" })); expect(screen.getByRole("button", { name: "Add Server" })).toBeEnabled();
expect(onAddServer).not.toHaveBeenCalled(); // Replace with invalid content and do *not* advance: the debounce stays
expect( // pending, the button stays enabled, and clicking exercises the
await screen.findByText(/Fix the validation errors/), // submit-time guard that re-parses the live text.
).toBeInTheDocument(); await pasteJson("{not json");
fireEvent.click(screen.getByRole("button", { name: "Add Server" }));
expect(onAddServer).not.toHaveBeenCalled();
expect(screen.getByText(/Fix the validation errors/)).toBeInTheDocument();
} finally {
vi.useRealTimers();
}
}); });
it("loads server.json from a chosen file", async () => { it("loads server.json from a chosen file", async () => {