Skip to content

Commit 3933b64

Browse files
committed
test(api): ban setTimeout in provider specs, use waitFor and shared helpers
1 parent 356d6d9 commit 3933b64

16 files changed

Lines changed: 91 additions & 113 deletions

‎packages/config-eslint/api-provider-hygiene.js‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,3 +81,20 @@ export const apiSpecTsIgnoreConfig = {
8181
"@typescript-eslint/ban-ts-comment": ["error", { "ts-ignore": true }],
8282
},
8383
}
84+
85+
// A fixed sleep in a provider spec either flakes or hides a missing await. Poll with vi.waitFor on the
86+
// observable condition. This uses no-restricted-globals, not no-restricted-syntax, because later config
87+
// blocks replace no-restricted-syntax for the same files.
88+
export const apiSpecSleepConfig = {
89+
files: ["api/**/__tests__/**/*.spec.ts"],
90+
rules: {
91+
"no-restricted-globals": [
92+
"error",
93+
{
94+
name: "setTimeout",
95+
message:
96+
"Poll with vi.waitFor on the observable condition. A fixed sleep either flakes or hides a missing await.",
97+
},
98+
],
99+
},
100+
}

‎src/api/providers/__tests__/base-provider.spec.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import type { ModelInfo } from "@roo-code/types"
55
import { BaseProvider } from "../base-provider"
66
import type { ApiStream } from "../../transform/stream"
77
import { expectNoLoneSurrogates } from "../../../test-utils/surrogates"
8+
import { flushMacrotasks } from "../../../test-utils/timers"
89

910
// Create a concrete implementation for testing
1011
class TestProvider extends BaseProvider {
@@ -595,7 +596,7 @@ describe("BaseProvider", () => {
595596
await expect(wait).rejects.toThrow("caller gave up")
596597

597598
resolveLoad()
598-
await new Promise((resolve) => setTimeout(resolve, 0))
599+
await flushMacrotasks()
599600

600601
// The shared load completed, so a later caller is a memoized no-op.
601602
await expect(provider.testEnsureCatalogLoaded(load)).resolves.toBeUndefined()

‎src/api/providers/__tests__/gemini.spec.ts‎

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ import type { ApiHandlerCreateMessageMetadata } from "../../index"
2121
import { GeminiHandler } from "../gemini"
2222
import { asyncStreamFrom, collectStream } from "../../../test-utils/stream"
2323
import { makeCreateMessageMetadata } from "../../../test-utils/api"
24+
import { flushMacrotasks } from "../../../test-utils/timers"
25+
import { withSettleGuard } from "../../../test-utils/settle-guard"
2426

2527
const GEMINI_MODEL_NAME = geminiDefaultModelId
2628

@@ -1540,18 +1542,12 @@ describe("GeminiHandler", () => {
15401542
)
15411543

15421544
const collector = collectStream(stream).catch((e: unknown) => e)
1543-
await new Promise((resolve) => setTimeout(resolve, 10))
1545+
await flushMacrotasks()
15441546
controller.abort()
15451547

15461548
// Bound the wait so a broken abort bridge fails this test fast (and fails
15471549
// the Stryker mutant) instead of hanging until the runner timeout.
1548-
const error = await new Promise<unknown>((resolve) => {
1549-
const deadline = setTimeout(() => resolve(new Error("abort propagation deadline exceeded")), 3000)
1550-
collector.then((result) => {
1551-
clearTimeout(deadline)
1552-
resolve(result)
1553-
})
1554-
})
1550+
const error = await withSettleGuard(collector, 3000)
15551551
expect(error).toBeInstanceOf(Error)
15561552
expect((error as Error).name).toBe("AbortError")
15571553
expect((error as Error).message).toBe("The Gemini request was aborted")

‎src/api/providers/__tests__/github-copilot.spec.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ import type { ApiHandlerOptions } from "../../../shared/api"
102102
import { clearAllMocks } from "../../../test-utils/reset"
103103
import { checkContextWindowExceededError } from "../../../core/context/context-management/context-error-handling"
104104
import { collectStream } from "../../../test-utils/stream"
105+
import { flushMacrotasks } from "../../../test-utils/timers"
105106

106107
/** The stable typings this extension builds against predate image parts, so reach them by name. */
107108
const dataPart = () =>
@@ -753,7 +754,7 @@ describe("GitHubCopilotHandler", () => {
753754
() => (settled = true),
754755
() => (settled = true),
755756
)
756-
await new Promise((resolve) => setTimeout(resolve, 0))
757+
await flushMacrotasks()
757758
expect(settled).toBe(false)
758759
expect(source.cancel).not.toHaveBeenCalled()
759760

‎src/api/providers/__tests__/lite-llm.spec.ts‎

Lines changed: 15 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import { type ModelRecord, litellmDefaultModelId, litellmDefaultModelInfo } from
88
import { asyncStreamFrom, collectStream } from "../../../test-utils/stream"
99
import { clearAllMocks } from "../../../test-utils/reset"
1010
import { makeCreateMessageMetadata } from "../../../test-utils/api"
11+
import { withSettleGuard } from "../../../test-utils/settle-guard"
12+
import { flushMacrotasks } from "../../../test-utils/timers"
1113

1214
// Mock vscode first to avoid import errors
1315
vi.mock("vscode", () => ({
@@ -1570,7 +1572,7 @@ describe("LiteLLMHandler", () => {
15701572
const completion = handler
15711573
.completePrompt("test prompt", { abortSignal: controller.signal })
15721574
.catch((e: unknown) => e)
1573-
await new Promise((resolve) => setTimeout(resolve, 10))
1575+
await flushMacrotasks()
15741576
controller.abort()
15751577

15761578
const error = await completion
@@ -1788,13 +1790,7 @@ describe("LiteLLMHandler", () => {
17881790

17891791
// Bound the wait so a broken abort bridge fails this test fast (and fails
17901792
// the Stryker mutant) instead of hanging until the runner timeout.
1791-
const error = await new Promise<unknown>((resolve) => {
1792-
const deadline = setTimeout(() => resolve(new Error("abort propagation deadline exceeded")), 3000)
1793-
collector.then((result) => {
1794-
clearTimeout(deadline)
1795-
resolve(result)
1796-
})
1797-
})
1793+
const error = await withSettleGuard(collector, 3000)
17981794
expect(error).toBeInstanceOf(Error)
17991795
expect((error as Error).name).toBe("AbortError")
18001796
expect((error as Error).message).toBe("The LiteLLM request was aborted")
@@ -1868,13 +1864,7 @@ describe("LiteLLMHandler", () => {
18681864

18691865
// Bound the wait so a broken discovery race fails this test fast
18701866
// (and fails the Stryker mutant) instead of hanging.
1871-
const error = await new Promise<unknown>((resolve) => {
1872-
const deadline = setTimeout(() => resolve(new Error("discovery abort deadline exceeded")), 3000)
1873-
collector.then((result) => {
1874-
clearTimeout(deadline)
1875-
resolve(result)
1876-
})
1877-
})
1867+
const error = await withSettleGuard(collector, 3000)
18781868
expect(error).toBeInstanceOf(Error)
18791869
expect((error as Error).name).toBe("AbortError")
18801870
expect((error as Error).message).toBe("The LiteLLM request was aborted")
@@ -1960,13 +1950,7 @@ describe("LiteLLMHandler", () => {
19601950
await discoveryStarted
19611951
controller.abort()
19621952

1963-
const error = await new Promise<unknown>((resolve) => {
1964-
const deadline = setTimeout(() => resolve(new Error("discovery abort deadline exceeded")), 3000)
1965-
collector.then((result) => {
1966-
clearTimeout(deadline)
1967-
resolve(result)
1968-
})
1969-
})
1953+
const error = await withSettleGuard(collector, 3000)
19701954
expect(error).toBeInstanceOf(Error)
19711955
expect((error as Error).name).toBe("AbortError")
19721956
expect((error as Error).message).toBe("The LiteLLM request was aborted")
@@ -2026,7 +2010,7 @@ describe("LiteLLMHandler", () => {
20262010
// an erroneous early settlement (e.g. the abort leaking into the
20272011
// sibling) settles `sibling` in a microtask, so an assertion
20282012
// right after attaching the callback could never observe it.
2029-
await new Promise<void>((resolve) => setTimeout(resolve, 0))
2013+
await flushMacrotasks()
20302014
expect(siblingSettled).toBe(false)
20312015

20322016
// ...and settles normally once the shared discovery resolves.
@@ -2056,13 +2040,7 @@ describe("LiteLLMHandler", () => {
20562040
await discoveryStarted
20572041
controller.abort()
20582042

2059-
const error = await new Promise<unknown>((resolve) => {
2060-
const deadline = setTimeout(() => resolve(new Error("discovery abort deadline exceeded")), 3000)
2061-
collector.then((result) => {
2062-
clearTimeout(deadline)
2063-
resolve(result)
2064-
})
2065-
})
2043+
const error = await withSettleGuard(collector, 3000)
20662044
expect(error).toBeInstanceOf(Error)
20672045
expect((error as Error).name).toBe("AbortError")
20682046
expect((error as Error).message).toBe("The LiteLLM request was aborted")
@@ -2075,19 +2053,13 @@ describe("LiteLLMHandler", () => {
20752053
// instead of the abort contract error.
20762054
vi.mocked(getModels).mockImplementationOnce(() => new Promise<ModelRecord>(() => {}))
20772055

2078-
const error = await new Promise<unknown>((resolve) => {
2079-
const deadline = setTimeout(() => resolve(new Error("timeout deadline exceeded")), 3000)
2056+
const error = await withSettleGuard(
20802057
handler.completePrompt("test prompt", { timeoutMs: 50 }).then(
2081-
() => {
2082-
clearTimeout(deadline)
2083-
resolve(new Error("completePrompt unexpectedly resolved"))
2084-
},
2085-
(e) => {
2086-
clearTimeout(deadline)
2087-
resolve(e)
2088-
},
2089-
)
2090-
})
2058+
() => new Error("completePrompt unexpectedly resolved"),
2059+
(e: unknown) => e,
2060+
),
2061+
3000,
2062+
)
20912063
expect(error).toBeInstanceOf(Error)
20922064
expect((error as Error).name).toBe("TimeoutError")
20932065
expect((error as Error).message).toBe("The LiteLLM model discovery timed out")
@@ -2102,6 +2074,7 @@ describe("LiteLLMHandler", () => {
21022074
vi.mocked(getModels).mockImplementationOnce(
21032075
() =>
21042076
new Promise<ModelRecord>((resolve) => {
2077+
// eslint-disable-next-line no-restricted-globals -- vi.useFakeTimers drives this timer
21052078
setTimeout(() => resolve({ [litellmDefaultModelId]: litellmDefaultModelInfo }), 50)
21062079
}),
21072080
)

‎src/api/providers/__tests__/lm-studio-timeout.spec.ts‎

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -116,15 +116,8 @@ describe("LmStudioHandler abort signal wiring", () => {
116116
return err
117117
}
118118

119-
const waitForCreateCall = async (create: { mock: { calls: unknown[][] } }, timeoutMs = 5000): Promise<void> => {
120-
const start = Date.now()
121-
while (create.mock.calls.length === 0) {
122-
if (Date.now() - start > timeoutMs) {
123-
throw new Error("timed out waiting for the SDK create call")
124-
}
125-
await new Promise((resolve) => setTimeout(resolve, 5))
126-
}
127-
}
119+
const waitForCreateCall = (create: { mock: { calls: unknown[][] } }): Promise<void> =>
120+
vitest.waitFor(() => expect(create.mock.calls.length).toBeGreaterThan(0))
128121

129122
const waitForSignalAbort = (signal: AbortSignal | undefined): Promise<void> => {
130123
return new Promise((resolve, reject) => {

‎src/api/providers/__tests__/lm-studio.spec.ts‎

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -54,15 +54,8 @@ describe("LmStudioHandler abort wiring", () => {
5454
return err
5555
}
5656

57-
const waitForCreateCall = async (create: { mock: { calls: unknown[][] } }, timeoutMs = 5000): Promise<void> => {
58-
const start = Date.now()
59-
while (create.mock.calls.length === 0) {
60-
if (Date.now() - start > timeoutMs) {
61-
throw new Error("timed out waiting for the SDK create call")
62-
}
63-
await new Promise((resolve) => setTimeout(resolve, 5))
64-
}
65-
}
57+
const waitForCreateCall = (create: { mock: { calls: unknown[][] } }): Promise<void> =>
58+
vitest.waitFor(() => expect(create.mock.calls.length).toBeGreaterThan(0))
6659

6760
const waitForSignalAbort = (signal: AbortSignal | undefined): Promise<void> => {
6861
return new Promise((resolve, reject) => {
@@ -184,7 +177,6 @@ describe("LmStudioHandler abort wiring", () => {
184177
return
185178
}
186179
opts.signal.addEventListener("abort", () => reject(sdkAbortError()), { once: true })
187-
setTimeout(() => resolve(asyncStreamFrom([])), 300)
188180
})
189181
})
190182

@@ -250,7 +242,7 @@ describe("LmStudioHandler abort wiring", () => {
250242
const external = new AbortController()
251243
const stream = handler.createMessage("system", [], { taskId: "t1", abortSignal: external.signal })
252244
const pending = stream.next()
253-
await new Promise((resolve) => setTimeout(resolve, 10)) // let the generator reach the pending count
245+
await vitest.waitFor(() => expect(handler.countTokens).toHaveBeenCalledTimes(1)) // let the generator reach the pending count
254246
external.abort()
255247
resolveCountTokens(1)
256248

@@ -278,7 +270,7 @@ describe("LmStudioHandler abort wiring", () => {
278270
const external = new AbortController()
279271
const stream = handler.createMessage("system", [], { taskId: "t1", abortSignal: external.signal })
280272
const pending = stream.next()
281-
await new Promise((resolve) => setTimeout(resolve, 10)) // let the generator reach the pending count
273+
await vitest.waitFor(() => expect(handler.countTokens).toHaveBeenCalledTimes(1)) // let the generator reach the pending count
282274
external.abort()
283275

284276
let caught: unknown
@@ -309,7 +301,7 @@ describe("LmStudioHandler abort wiring", () => {
309301
const first = await stream.next()
310302
expect(first.value?.type).toBe("text")
311303
const pending = stream.next() // runs past the stream into the pending output count
312-
await new Promise((resolve) => setTimeout(resolve, 10))
304+
await vitest.waitFor(() => expect(handler.countTokens).toHaveBeenCalledTimes(2))
313305
external.abort()
314306

315307
let caught: unknown
@@ -666,7 +658,7 @@ describe("LmStudioHandler abort wiring", () => {
666658
const external = new AbortController()
667659
const stream = handler.createMessage("system", [], { taskId: "t1", abortSignal: external.signal })
668660
const pending = stream.next()
669-
await new Promise((resolve) => setTimeout(resolve, 10)) // let the generator reach the pending count
661+
await vitest.waitFor(() => expect(handler.countTokens).toHaveBeenCalledTimes(1)) // let the generator reach the pending count
670662
resolveInputCount(1)
671663
await Promise.resolve() // let the race settle and detach its listener
672664
external.abort()

‎src/api/providers/__tests__/lmstudio.spec.ts‎

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ import { providerIdentifiers } from "@roo-code/types"
6666

6767
import { LmStudioHandler } from "../lm-studio"
6868
import type { ApiHandlerOptions } from "../../../shared/api"
69+
import { withSettleGuard } from "../../../test-utils/settle-guard"
6970

7071
const { mockGetModels, mockGetModelsFromCache } = vi.hoisted(() => ({
7172
mockGetModels: vi.fn(),
@@ -274,23 +275,14 @@ describe("LmStudioHandler", () => {
274275
const pending = collectStream(stream).catch((error: unknown) => error)
275276

276277
// Let the generator reach the token count, then abort while it is pending.
277-
const start = Date.now()
278-
while (countSpy.mock.calls.length === 0) {
279-
if (Date.now() - start > 5000) {
280-
throw new Error("timed out waiting for the token count")
281-
}
282-
await new Promise((resolve) => setTimeout(resolve, 5))
283-
}
278+
await vitest.waitFor(() => expect(countSpy).toHaveBeenCalled())
284279
controller.abort()
285280

286281
try {
287282
// The count gate is still closed, so cancellation itself has to settle the
288283
// pending count. Releasing the gate first would let counting finish normally
289284
// and the later pre-request abort check would make this pass anyway.
290-
const raced = await Promise.race([
291-
pending,
292-
new Promise((resolve) => setTimeout(() => resolve(undefined), 300)),
293-
])
285+
const raced = await withSettleGuard(pending, 300)
294286
expect(raced).toBeInstanceOf(Error)
295287
expect((raced as Error).name).toBe("AbortError")
296288
expect((raced as Error).message).toBe("The LM Studio request was aborted")

‎src/api/providers/__tests__/mistral.spec.ts‎

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ import type {
6262
import { makeCreateMessageMetadata } from "../../../test-utils/api"
6363
import { mistralModels } from "@roo-code/types"
6464
import { calculateApiCostOpenAI } from "../../../shared/cost"
65+
import { flushMacrotasks } from "../../../test-utils/timers"
66+
import { withSettleGuard } from "../../../test-utils/settle-guard"
6567

6668
describe("MistralHandler", () => {
6769
let handler: MistralHandler
@@ -1085,6 +1087,7 @@ describe("MistralHandler", () => {
10851087
// timers so the test is deterministic and does not wait in real time.
10861088
const timeoutSpy = vi.spyOn(AbortSignal, "timeout").mockImplementation((ms = 0) => {
10871089
const controller = new AbortController()
1090+
// eslint-disable-next-line no-restricted-globals -- vi.useFakeTimers drives this timer
10881091
setTimeout(() => controller.abort(new DOMException("timed out", "TimeoutError")), ms)
10891092
return controller.signal
10901093
})
@@ -1124,6 +1127,7 @@ describe("MistralHandler", () => {
11241127
vi.useFakeTimers()
11251128
const timeoutSpy = vi.spyOn(AbortSignal, "timeout").mockImplementation((ms = 0) => {
11261129
const controller = new AbortController()
1130+
// eslint-disable-next-line no-restricted-globals -- vi.useFakeTimers drives this timer
11271131
setTimeout(() => controller.abort(new DOMException("timed out", "TimeoutError")), ms)
11281132
return controller.signal
11291133
})
@@ -1162,6 +1166,7 @@ describe("MistralHandler", () => {
11621166
vi.useFakeTimers()
11631167
const timeoutSpy = vi.spyOn(AbortSignal, "timeout").mockImplementation((ms = 0) => {
11641168
const controller = new AbortController()
1169+
// eslint-disable-next-line no-restricted-globals -- vi.useFakeTimers drives this timer
11651170
setTimeout(() => controller.abort(new DOMException("timed out", "TimeoutError")), ms)
11661171
return controller.signal
11671172
})
@@ -1297,18 +1302,12 @@ describe("MistralHandler", () => {
12971302
)
12981303

12991304
const collector = collectStream(stream).catch((e: unknown) => e)
1300-
await new Promise((resolve) => setTimeout(resolve, 10))
1305+
await flushMacrotasks()
13011306
controller.abort()
13021307

13031308
// Bound the wait so a broken abort bridge fails this test fast (and fails
13041309
// the Stryker mutant) instead of hanging until the runner timeout.
1305-
const error = await new Promise<unknown>((resolve) => {
1306-
const deadline = setTimeout(() => resolve(new Error("abort propagation deadline exceeded")), 3000)
1307-
collector.then((result) => {
1308-
clearTimeout(deadline)
1309-
resolve(result)
1310-
})
1311-
})
1310+
const error = await withSettleGuard(collector, 3000)
13121311
expect(error).toBeInstanceOf(Error)
13131312
expect((error as Error).name).toBe("AbortError")
13141313
expect((error as Error).message).toBe("The Mistral request was aborted")

‎src/api/providers/__tests__/model-catalog-adapters.spec.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ import { RequestyHandler } from "../requesty"
124124
import { UnboundHandler } from "../unbound"
125125
import { VercelAiGatewayHandler } from "../vercel-ai-gateway"
126126
import { ZooGatewayHandler } from "../zoo-gateway"
127+
import { flushMacrotasks } from "../../../test-utils/timers"
127128

128129
const BASE_URL = "https://catalog.example.com/v1"
129130
const API_KEY = "catalog-key"
@@ -421,7 +422,7 @@ describe("model catalog adapters", () => {
421422

422423
settle(CATALOG)
423424
// Let the shared load's promise chain settle (load -> .then -> .finally).
424-
await new Promise((resolve) => setTimeout(resolve, 0))
425+
await flushMacrotasks()
425426

426427
// The load completed despite the detached waiter, so the next call
427428
// is a memoized no-op rather than a second fetch.

0 commit comments

Comments
 (0)