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,7 +223,16 @@ 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 () => {
vi.useFakeTimers();
try {
const onAddServer = vi.fn(); const onAddServer = vi.fn();
renderWithMantine( renderWithMantine(
<ServerImportJsonModal <ServerImportJsonModal
@ -232,17 +243,21 @@ describe("ServerImportJsonModal", () => {
/>, />,
); );
await pasteJson(npmJson); await pasteJson(npmJson);
await waitFor(() => // Let the first validation land, so the button is enabled to click.
expect(screen.getByRole("button", { name: "Add Server" })).toBeEnabled(), await act(async () => {
); await vi.advanceTimersByTimeAsync(VALIDATE_DEBOUNCE_MS);
// Replace with invalid content; the button hasn't re-disabled yet (the });
// debounce is still pending), so clicking exercises the submit-time guard. expect(screen.getByRole("button", { name: "Add Server" })).toBeEnabled();
// Replace with invalid content and do *not* advance: the debounce stays
// pending, the button stays enabled, and clicking exercises the
// submit-time guard that re-parses the live text.
await pasteJson("{not json"); await pasteJson("{not json");
fireEvent.click(screen.getByRole("button", { name: "Add Server" })); fireEvent.click(screen.getByRole("button", { name: "Add Server" }));
expect(onAddServer).not.toHaveBeenCalled(); expect(onAddServer).not.toHaveBeenCalled();
expect( expect(screen.getByText(/Fix the validation errors/)).toBeInTheDocument();
await screen.findByText(/Fix the validation errors/), } finally {
).toBeInTheDocument(); vi.useRealTimers();
}
}); });
it("loads server.json from a chosen file", async () => { it("loads server.json from a chosen file", async () => {