Skip to content

Linger is An Open-Source Personal Agent That Runs Fully on Your PC. We aim to break Big Tech’s grip on AI by bringing powerful local AI to everyday computers through efficient model–harness co-design, so everyone can run their own.

test(web): restore timer spies before real timers and fail a test that leaks fake timers

shoudi zhengadaab1ea8f27
diff --git a/apps/web/src/features/auth/auth-illustration.test.tsx b/apps/web/src/features/auth/auth-illustration.test.tsx
index 402eb41b..24faa086 100644
--- a/apps/web/src/features/auth/auth-illustration.test.tsx
+++ b/apps/web/src/features/auth/auth-illustration.test.tsx
@@ -21,9 +21,11 @@ beforeEach(() => {
);
});
afterEach(() => {
+ // Restore timer spies first: restoring one after the fake clock is gone
+ // would put the fake function back on the shared window.
+ vi.restoreAllMocks();
vi.useRealTimers();
vi.unstubAllGlobals();
- vi.restoreAllMocks();
});
function shownNudge() {
diff --git a/apps/web/src/features/local-computer/computer-connection-status.test.tsx b/apps/web/src/features/local-computer/computer-connection-status.test.tsx
index caaf03ee..b8ad9f46 100644
--- a/apps/web/src/features/local-computer/computer-connection-status.test.tsx
+++ b/apps/web/src/features/local-computer/computer-connection-status.test.tsx
@@ -16,8 +16,10 @@ function ComputerConnectionStatus({
afterEach(() => {
cleanup();
- vi.useRealTimers();
+ // Restore timer spies first: restoring one after the fake clock is gone
+ // would put the fake function back on the shared window.
vi.restoreAllMocks();
+ vi.useRealTimers();
});
it("keeps reconnect churn stable and only settles after sustained availability", () => {
diff --git a/apps/web/src/features/rooms/chat/agent-status-line.test.tsx b/apps/web/src/features/rooms/chat/agent-status-line.test.tsx
index 98dca25f..8c264339 100644
--- a/apps/web/src/features/rooms/chat/agent-status-line.test.tsx
+++ b/apps/web/src/features/rooms/chat/agent-status-line.test.tsx
@@ -413,7 +413,12 @@ describe("advanceAgentStatusLine", () => {
});
describe("useAgentStatusLine", () => {
- afterEach(() => vi.useRealTimers());
+ afterEach(() => {
+ // Restore timer spies first: restoring one after the fake clock is gone
+ // would put the fake function back on the shared window.
+ vi.restoreAllMocks();
+ vi.useRealTimers();
+ });
it("keeps one timer across rerenders and clears it once on unmount", () => {
vi.useFakeTimers();
diff --git a/apps/web/src/test/setup.ts b/apps/web/src/test/setup.ts
index 7f9224e4..b5cd191d 100644
--- a/apps/web/src/test/setup.ts
+++ b/apps/web/src/test/setup.ts
@@ -111,6 +111,46 @@ function resetDocumentGlobals() {
}
}
+// The worker's real timer functions, captured by the first file to load this
+// setup: a later file may already run with a leaked replacement.
+type TimerGlobal =
+ | "setTimeout"
+ | "clearTimeout"
+ | "setInterval"
+ | "clearInterval"
+ | "Date";
+const TIMER_GLOBALS: TimerGlobal[] = [
+ "setTimeout",
+ "clearTimeout",
+ "setInterval",
+ "clearInterval",
+ "Date",
+];
+const realTimers = ((
+ globalThis as { __lingerRealTimers?: Record<TimerGlobal, unknown> }
+).__lingerRealTimers ??= Object.fromEntries(
+ TIMER_GLOBALS.map((name) => [name, globalThis[name]]),
+) as Record<TimerGlobal, unknown>);
+
+// A spy created on a timer while fake timers are on records the fake clock's
+// function as the original; restoring it after useRealTimers writes that dead
+// function back, and every later timer in the worker never fires (waitFor
+// stops re-checking). Restore the real ones and fail the test that leaked.
+function assertRealTimerGlobals() {
+ vi.restoreAllMocks();
+ const leaked = TIMER_GLOBALS.filter(
+ (name) => globalThis[name] !== realTimers[name],
+ );
+ if (leaked.length === 0) return;
+ for (const name of leaked) {
+ Object.assign(globalThis, { [name]: realTimers[name] });
+ }
+ throw new Error(
+ `This test left fake ${leaked.join(", ")} on the global object. ` +
+ "Restore timer spies before vi.useRealTimers(), or spy on timers before vi.useFakeTimers().",
+ );
+}
+
afterEach(() => {
try {
cleanupRenderedComponents?.();
@@ -131,5 +171,6 @@ afterEach(() => {
resetComposerAttachmentDraftsForTest();
resetRoomTimelineReadingPositions?.();
assertNoUnhandledTestRoutes();
+ assertRealTimerGlobals();
}
});