fix(test): treat timer handles as opaque; the numeric guard never matched

Review was right, and I had assumed the handle type rather than checking it.
Probed both under happy-dom:

  setTimeout            -> typeof "object", constructor Timeout
  requestAnimationFrame -> typeof "object", constructor Immediate

So `typeof id === "number"` in the clearTimeout wrapper never matched, and an
explicitly-cleared timer was never untracked. Both `Set<number>` annotations and
`const id: number` were fictions TypeScript could not catch, since the DOM lib
DECLARES number while happy-dom RETURNS an object.

The effect was benign — the teardown sweep re-clears them and clearTimeout is
idempotent — but the sharper problem is that the test asserting this passed for
the wrong reason. "clearTimeout doesn't throw" holds whether or not tracking
works, so it proved nothing and hid the broken guard.

Hold handles as `unknown`, route cancellation through two small helpers that
carry the one justified cast each, and drop the numeric guards. The test now
asserts the net's own bookkeeping through an exported pendingTimerCount(), and
is mutation-checked: restoring the numeric guard fails it (1 failed | 6 passed).

That is the second test in this PR that passed without proving its claim. Both
had the same tell — the assertion would have held with the feature removed.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01HDdo1rNRVQnVRRdsqSrvG3
Signed-off-by: cliffhall <cliff@futurescale.com>
This commit is contained in:
cliffhall 2026-08-12 01:23:21 -04:00
commit c0e4b89c8d

View file

@ -16,6 +16,7 @@
import { describe, it, expect, vi } from "vitest";
import { Modal } from "@mantine/core";
import { renderWithMantine } from "./renderWithMantine";
import { pendingTimerCount } from "./setup";
/** Schedule through the wrapper the net installed, keeping its inferred handle
* type so it stays directly acceptable to `window.clearTimeout`. */
@ -46,11 +47,17 @@ describe("leaked-timer safety net", () => {
expect(leaked.callback).not.toHaveBeenCalled();
});
it("clearTimeout stops tracking, so an explicitly-cleared timer is not double-cleared", () => {
it("clearTimeout untracks the timer", () => {
// Asserted against the net's own bookkeeping, not against "clearTimeout
// doesn't throw" — the latter holds whether or not tracking works, since
// clearTimeout is idempotent, so it proved nothing. That vacuity hid a real
// bug: happy-dom's handle is a `Timeout` object, so the original
// `typeof id === "number"` guard never matched and nothing was untracked.
const before = pendingTimerCount();
const id = scheduleTracked(1000);
expect(() => window.clearTimeout(id)).not.toThrow();
// Clearing twice must stay a no-op — the net also clears at teardown.
expect(() => window.clearTimeout(id)).not.toThrow();
expect(pendingTimerCount()).toBe(before + 1);
window.clearTimeout(id);
expect(pendingTimerCount()).toBe(before);
});
it("queues a frame that would schedule a timer after the sweep", () => {