test(web): make the debounce-cancellation test load-bearing (review round 1)

The "cancels a pending debounce timer when the input is re-focused" test
passed trivially after the delay:null change: synchronous typing meant the
assertion ran at t≈0, before the 300ms debounce could ever fire, so it
passed whether or not handleFocus's clearTimeout worked.

Rewrote it with fake timers so the debounce window elapses deterministically
(advance the clock by 400ms directly). The interaction is driven with
fireEvent rather than userEvent because userEvent's async internals deadlock
under vitest fake timers with the Mantine/happy-dom stack. Verified
load-bearing: removing the clearTimeout in handleFocus makes it fail
(expected 1 to be 0), and it passes once restored.

Also tightened the sibling-context comment (with delay:null the sibling is
reliably "es"); the tolerant /^es?$/ regex is left as-is.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01S3fTN8H3R8YV4yUGvZjYnX
This commit is contained in:
cliffhall 2026-07-01 21:22:04 -04:00
commit dd7812063c

View file

@ -6,6 +6,8 @@ import {
renderWithMantine, renderWithMantine,
screen, screen,
waitFor, waitFor,
fireEvent,
act,
} from "../../../test/renderWithMantine"; } from "../../../test/renderWithMantine";
import { PromptArgumentsForm } from "./PromptArgumentsForm"; import { PromptArgumentsForm } from "./PromptArgumentsForm";
@ -363,8 +365,9 @@ describe("PromptArgumentsForm", () => {
); );
// Poll until the debounced "text" call fires, then assert it captured a // Poll until the debounced "text" call fires, then assert it captured a
// non-empty sibling (read at fire time). The exact string may be "e" or // non-empty sibling (read at fire time, not the empty value present when
// "es" depending on where the fire lands relative to the keystrokes. // the call was scheduled). With delay:null the sibling is fully typed
// ("es") before the debounce fires; the /^es?$/ regex stays tolerant.
await waitFor(() => { await waitFor(() => {
const textCalls = onCompleteArgument.mock.calls.filter( const textCalls = onCompleteArgument.mock.calls.filter(
([n]) => n === "text", ([n]) => n === "text",
@ -504,45 +507,67 @@ describe("PromptArgumentsForm", () => {
}); });
it("cancels a pending debounce timer when the input is re-focused", async () => { it("cancels a pending debounce timer when the input is re-focused", async () => {
const user = userEvent.setup({ delay: null }); // This is the one completion test that must let the 300ms debounce
const onCompleteArgument = vi // window fully elapse to be meaningful — asserting at t≈0 (as the
.fn< // sibling negative tests do) would pass whether or not handleFocus's
( // clearTimeout actually cancels the pending timer. Fake timers make that
argName: string, // window deterministic: we advance the clock directly. The interaction
value: string, // is driven with fireEvent rather than userEvent, because userEvent's
context: Record<string, string>, // async internals deadlock under vitest fake timers with this
) => Promise<string[]> // Mantine/happy-dom stack. This test is load-bearing: if the
>() // clearTimeout in handleFocus were removed, the debounce would fire
.mockResolvedValue([]); // during the advance and the final assertion would fail (verified).
vi.useFakeTimers();
try {
const onCompleteArgument = vi
.fn<
(
argName: string,
value: string,
context: Record<string, string>,
) => Promise<string[]>
>()
.mockResolvedValue([]);
renderWithMantine( renderWithMantine(
<StatefulForm <StatefulForm
prompt={promptWithArgs} prompt={promptWithArgs}
onGetPrompt={vi.fn()} onGetPrompt={vi.fn()}
completionsSupported completionsSupported
onCompleteArgument={onCompleteArgument} onCompleteArgument={onCompleteArgument}
/>, />,
); );
const textInput = screen.getByRole("textbox", { name: /^text/ }); const textInput = screen.getByRole("textbox", { name: /^text/ });
const siblingInput = screen.getByRole("textbox", { const siblingInput = screen.getByRole("textbox", {
name: /targetLanguage/, name: /targetLanguage/,
}); });
// Type into text → schedules a debounce timer. Immediately move focus // Type into text → schedules the 300ms debounce timer for "text".
// away and back (synchronous under delay: null, so well within the 300ms fireEvent.focus(textInput);
// window), so handleFocus sees the pending timer for "text" and clears fireEvent.change(textInput, { target: { value: "h" } });
// it. Clearing is synchronous, so the keystroke completion can never // Move focus to the sibling and back — the re-focus makes
// fire afterward regardless of machine speed. // handleFocus("text") see the pending debounce timer and clear it.
await user.type(textInput, "h"); fireEvent.focus(siblingInput);
await user.click(siblingInput); fireEvent.focus(textInput);
await user.click(textInput);
onCompleteArgument.mockClear(); // Drop the focus-fire calls made above; only a *debounce* fire after
const textCalls = onCompleteArgument.mock.calls.filter( // this point would indicate the cancellation failed.
([n]) => n === "text", onCompleteArgument.mockClear();
);
// The cancelled debounce produced no post-clear "text" completion. // Elapse well past the debounce window. The cancelled timer must not
expect(textCalls.length).toBe(0); // fire a stale keystroke completion.
await act(async () => {
await vi.advanceTimersByTimeAsync(400);
});
const textCalls = onCompleteArgument.mock.calls.filter(
([n]) => n === "text",
);
expect(textCalls.length).toBe(0);
} finally {
vi.useRealTimers();
}
}); });
it("does not call onCompleteArgument when completions are unsupported", async () => { it("does not call onCompleteArgument when completions are unsupported", async () => {