From 4fca2ebc321dacb588e69867f333f931b101801a Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Tue, 17 Nov 2026 19:22:04 -0500 Subject: [PATCH] chore: Replace fixed sleeps and raised timeouts with signals (#14624) --- app/models/base/CoalescedFetch.test.ts | 11 ++++++++-- app/stores/base/StorePersistence.test.ts | 15 ++++++++++++-- app/utils/ModelContext.test.ts | 8 ++++++-- plugins/storage/server/api/files.test.ts | 3 --- server/models/View.test.ts | 6 +++--- .../tasks/RollupDocumentInsightsTask.test.ts | 2 -- .../RollupWeeklyDocumentInsightsTask.test.ts | 2 -- server/utils/fetch.test.ts | 20 ++++++++++++++----- 8 files changed, 46 insertions(+), 21 deletions(-) diff --git a/app/models/base/CoalescedFetch.test.ts b/app/models/base/CoalescedFetch.test.ts index 8773c82219..ce87de11a7 100644 --- a/app/models/base/CoalescedFetch.test.ts +++ b/app/models/base/CoalescedFetch.test.ts @@ -27,8 +27,14 @@ describe("CoalescedFetch", () => { it("should run one more request after an in-flight request when forced", async () => { const queue = new CoalescedFetch(); const requests = [deferred(), deferred()]; + const secondCall = deferred(); let calls = 0; - const fetch = vi.fn(() => requests[calls++].promise); + const fetch = vi.fn(() => { + if (calls === 1) { + secondCall.resolve(); + } + return requests[calls++].promise; + }); const first = queue.run(fetch); const forced = queue.run(fetch, true); @@ -37,7 +43,8 @@ describe("CoalescedFetch", () => { requests[0].resolve(); await first; - await vi.waitFor(() => expect(fetch).toHaveBeenCalledTimes(2)); + await secondCall.promise; + expect(fetch).toHaveBeenCalledTimes(2); let settled = false; void forced.then(() => { diff --git a/app/stores/base/StorePersistence.test.ts b/app/stores/base/StorePersistence.test.ts index 95294abb3d..e5a351c777 100644 --- a/app/stores/base/StorePersistence.test.ts +++ b/app/stores/base/StorePersistence.test.ts @@ -8,6 +8,11 @@ import RootStore from "~/stores/RootStore"; import StorePersistence from "./StorePersistence"; describe("StorePersistence", () => { + afterEach(() => { + vi.useRealTimers(); + vi.restoreAllMocks(); + }); + it("round-trips models through IndexedDB into a fresh store", async () => { const teamId = "team-1"; const source = new RootStore(); @@ -125,13 +130,14 @@ describe("StorePersistence", () => { persistence.persist("doc-1"); await persistence.flush(); + vi.useFakeTimers({ toFake: ["setTimeout", "clearTimeout"] }); const target = new RootStore(); await target.policies.enablePersistence(teamId); // Drop the model without notifying persistence, so that any flush scheduled // during hydration would delete the persisted record when it fires. target.policies.data.delete("doc-1"); - await new Promise((resolve) => setTimeout(resolve, 1100)); + await vi.runAllTimersAsync(); const other = new RootStore(); const otherPersistence = new StorePersistence(other.policies, teamId); @@ -173,6 +179,7 @@ describe("StorePersistence", () => { }); it("does not recreate the database from a pending flush once disabled", async () => { + vi.useFakeTimers({ toFake: ["setTimeout", "clearTimeout"] }); const teamId = "team-10"; const source = new RootStore(); const persistence = new StorePersistence(source.policies, teamId); @@ -181,12 +188,16 @@ describe("StorePersistence", () => { teamId ); + const open = vi.spyOn(indexedDB, "open"); + source.policies.add({ id: "doc-1", abilities: { read: true } }); persistence.persist("doc-1"); // Disabling while a debounced write is still scheduled, as logout does. persistence.disable(); - await new Promise((resolve) => setTimeout(resolve, 1100)); + await vi.runAllTimersAsync(); + + expect(open).not.toHaveBeenCalled(); const databases = await indexedDB.databases(); expect(databases.map((database) => database.name)).not.toContain( diff --git a/app/utils/ModelContext.test.ts b/app/utils/ModelContext.test.ts index 016706f6bc..dc12e0fd22 100644 --- a/app/utils/ModelContext.test.ts +++ b/app/utils/ModelContext.test.ts @@ -79,7 +79,9 @@ describe("ModelContext", () => { expect(registerModelContextTool(makeTool("five"), first.signal)).toBe( true ); - await new Promise((resolve) => setTimeout(resolve, 0)); + await expect(registerTool.mock.results[0].value).rejects.toThrow( + "rejected" + ); const second = new AbortController(); expect(registerModelContextTool(makeTool("five"), second.signal)).toBe( @@ -103,7 +105,9 @@ describe("ModelContext", () => { true ); controller.abort(); - await new Promise((resolve) => setTimeout(resolve, 0)); + await expect(registerTool.mock.results[0].value).rejects.toThrow( + "signal is aborted" + ); expect(warn).not.toHaveBeenCalled(); warn.mockRestore(); diff --git a/plugins/storage/server/api/files.test.ts b/plugins/storage/server/api/files.test.ts index 389df4fa13..33c59a41f3 100644 --- a/plugins/storage/server/api/files.test.ts +++ b/plugins/storage/server/api/files.test.ts @@ -21,9 +21,6 @@ import { randomUUID } from "node:crypto"; const server = getTestServer(); -// Increase timeout for all tests in this file -vi.setConfig({ testTimeout: 10000 }); - describe("#files.create", () => { it("should fail with status 400 bad request if key is invalid", async () => { const user = await buildUser(); diff --git a/server/models/View.test.ts b/server/models/View.test.ts index e2db228b6f..64e20cfccc 100644 --- a/server/models/View.test.ts +++ b/server/models/View.test.ts @@ -171,13 +171,13 @@ describe("View", () => { }); const firstViewedAt = view1.updatedAt; - // Wait a bit to ensure time difference - await new Promise((resolve) => setTimeout(resolve, 10)); + vi.useFakeTimers({ toFake: ["Date"] }); + vi.setSystemTime(firstViewedAt.getTime() + 1000); const view2 = await View.incrementOrCreate(createContext({ user }), { documentId: document.id, userId: user.id, - }); + }).finally(() => vi.useRealTimers()); expect(view2.updatedAt.getTime()).toBeGreaterThan( firstViewedAt.getTime() diff --git a/server/queues/tasks/RollupDocumentInsightsTask.test.ts b/server/queues/tasks/RollupDocumentInsightsTask.test.ts index 1a5a45cc06..e3959ac961 100644 --- a/server/queues/tasks/RollupDocumentInsightsTask.test.ts +++ b/server/queues/tasks/RollupDocumentInsightsTask.test.ts @@ -16,8 +16,6 @@ const props = { }, }; -vi.setConfig({ testTimeout: 30000 }); - const daysAgo = (n: number) => subDays(new Date(), n); const dayStr = (d: Date) => format(d, "yyyy-MM-dd"); diff --git a/server/queues/tasks/RollupWeeklyDocumentInsightsTask.test.ts b/server/queues/tasks/RollupWeeklyDocumentInsightsTask.test.ts index 3e364cd62a..064e7f9014 100644 --- a/server/queues/tasks/RollupWeeklyDocumentInsightsTask.test.ts +++ b/server/queues/tasks/RollupWeeklyDocumentInsightsTask.test.ts @@ -15,8 +15,6 @@ const props = { const daysAgo = (n: number) => subDays(new Date(), n); const dayStr = (d: Date) => format(d, "yyyy-MM-dd"); -vi.setConfig({ testTimeout: 30000 }); - describe("RollupWeeklyDocumentInsightsTask", () => { let task: RollupWeeklyDocumentInsightsTask; diff --git a/server/utils/fetch.test.ts b/server/utils/fetch.test.ts index 4970344d77..6385c9dcc8 100644 --- a/server/utils/fetch.test.ts +++ b/server/utils/fetch.test.ts @@ -1,5 +1,6 @@ import dns from "node:dns"; import http from "node:http"; +import { HttpProxyAgent } from "http-proxy-agent"; import type { MockInstance } from "vitest"; import env from "@server/env"; import { server as msw } from "@server/test/msw"; @@ -159,16 +160,25 @@ describe("fetch", () => { }); it("should not connect to the proxy after a timeout", async () => { + let resolveLookup: (addresses: dns.LookupAddress[]) => void = () => {}; const lookupSpy: MockInstance = vi.spyOn(dns.promises, "lookup"); - lookupSpy.mockImplementation(async () => { - await new Promise((resolve) => setTimeout(resolve, 200)); - return [{ address: "192.0.2.1", family: 4 }]; - }); + lookupSpy.mockReturnValue( + new Promise((resolve) => { + resolveLookup = resolve; + }) + ); + const connectSpy = vi.spyOn(HttpProxyAgent.prototype, "connect"); await expect( fetch("http://slow.example/", { timeout: 50 }) ).rejects.toThrow("Request timeout after 50ms"); - await new Promise((resolve) => setTimeout(resolve, 300)); + expect(lookupSpy).toHaveBeenCalledTimes(1); + + resolveLookup([{ address: "192.0.2.1", family: 4 }]); + + // The connect path after the lookup only runs microtasks. + await new Promise((resolve) => setImmediate(resolve)); + expect(connectSpy).not.toHaveBeenCalled(); expect(proxy.connections).toEqual(0); });