Skip to content

Commit b13d8c9

Browse files
test(openai-native): drop the four fixed sleeps that trip the lint idiom rule
The required compile arm runs `pnpm run lint` in src, and at 2a9c3d0 it exits 1 with 4 `no-restricted-globals` errors in src/api/providers/__tests__/openai-native.spec.ts (601:63, 609:20, 1263:36, 2037:35): "Unexpected use of 'setTimeout'. Poll with vi.waitFor on the observable condition. A fixed sleep either flakes or hides a missing await." The rule is apiSpecSleepConfig in packages/config-eslint/api-provider-hygiene.js, scoped to api/**/__tests__/**/*.spec.ts, which is why this file carries it and no other provider spec does; src/eslint-suppressions.json holds no entry for this rule, so these 4 errors were never suppressed. Site by site: - tick() has nothing observable to poll, so it is flushMacrotasks() from src/test-utils/timers, whose own header says to use it when there is no observable signal and to poll with vi.waitFor when there is one. - untilSignalAborted() is waiting for a deadline, so its fallback is now AbortSignal.timeout(timeoutMs) raced against the same abort event: same window, no timer to clear, nothing to forget on early abort. - The 429-races-abort row polls pullStarted (set when the reader enters pull and parks on the gate) and then flushes one macrotask, so the abort lands on a registered pending read instead of after 10 wall-clock ms. - The completePrompt abort row polls sdkRequestStarted, set inside the SDK double, so the abort arrives mid-request instead of after one timeout(0). A first cut polled untilSignalAborted with vi.waitFor(signal.aborted) at a 10 ms interval. That variant turned two abort-bridging rows red - "abort it mid-flight" (head :740) and "stop consuming the SDK stream" (head :788) - because the mocked stream's blocking window is load-bearing for those assertions: resolving it in ~10 ms lets the SDK iterator finish before the provider's between-pulls check runs. The deadline form is what is committed. Gates, run by hand from src with node v22.23.4: pnpm run lint exit 0 (was exit 1 with those 4 rows); eslint on this file exit 0, and with head content put back at the same path the same command exits 1 with exactly those 4 rows, so the change is what the control removes; vitest api shard 114 files, 4115 passed, 2 skipped (4117) exit 0; this file alone 128 passed; pnpm run check-types exit 0; prettier --check exit 0. eslint --prune-suppressions exits 0 and rewrites src/eslint-suppressions.json from 30,910 to 33,294 bytes with identical parsed content and 265 records both ways - the reformat 6e43f34 had just landed - so that file is reverted and is not in this commit. Test-only diff: __tests__ is in EXCLUDED_PATH_SEGMENTS of scripts/stryker-diff.mjs, so this delta adds no mutants and no production line moves.
1 parent 2a9c3d0 commit b13d8c9

1 file changed

Lines changed: 25 additions & 12 deletions

File tree

‎src/api/providers/__tests__/openai-native.spec.ts‎

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import {
2626
} from "../../../test-utils/api"
2727
import { asyncStreamFrom, collectStream } from "../../../test-utils/stream"
2828
import { deleteGlobalFetch } from "../../../test-utils/reset"
29+
import { flushMacrotasks } from "../../../test-utils/timers"
2930

3031
// Mock OpenAI client - now everything uses Responses API
3132
const mockResponsesCreate = vitest.fn()
@@ -598,23 +599,26 @@ describe("OpenAiNativeHandler", () => {
598599
return error
599600
}
600601

601-
const tick = (): Promise<void> => new Promise((resolve) => setTimeout(resolve, 0))
602+
// A tick has no observable condition to poll here, so it uses the shared macrotask flush
603+
// (test-utils/timers, setImmediate-based) rather than a wall-clock timer.
604+
const tick = flushMacrotasks
602605

606+
// The mocked stream must stay open until the abort arrives, with a bounded fallback so a
607+
// missing propagation cannot hang the test. AbortSignal.timeout is the platform's own
608+
// deadline primitive: no wall-clock sleep to clear and no listener to forget.
603609
function untilSignalAborted(signal: AbortSignal, timeoutMs = 200): Promise<void> {
604610
return new Promise<void>((resolve) => {
605611
if (signal.aborted) {
606612
resolve()
607613
return
608614
}
609-
const timer = setTimeout(() => resolve(), timeoutMs)
610-
signal.addEventListener(
611-
"abort",
612-
() => {
613-
clearTimeout(timer)
614-
resolve()
615-
},
616-
{ once: true },
617-
)
615+
const deadline = AbortSignal.timeout(timeoutMs)
616+
const settle = () => {
617+
deadline.removeEventListener("abort", settle)
618+
resolve()
619+
}
620+
signal.addEventListener("abort", settle, { once: true })
621+
deadline.addEventListener("abort", settle, { once: true })
618622
})
619623
}
620624

@@ -1240,9 +1244,11 @@ describe("OpenAiNativeHandler", () => {
12401244
const streamGate = new Promise<void>((resolve) => {
12411245
releaseStream = resolve
12421246
})
1247+
let pullStarted = false
12431248
const mockFetch = vitest.fn().mockImplementation(() => {
12441249
const body = new ReadableStream<Uint8Array>({
12451250
pull: async () => {
1251+
pullStarted = true
12461252
await streamGate
12471253
throw rateLimited
12481254
},
@@ -1260,7 +1266,10 @@ describe("OpenAiNativeHandler", () => {
12601266
),
12611267
)
12621268
// Abort first, then let the stream fail: the failure is the real outcome.
1263-
await new Promise((resolve) => setTimeout(resolve, 10))
1269+
// Observable: the reader has entered pull and is parked on the gate. Polling it, then
1270+
// flushing one macrotask so the pending read is registered, replaces the fixed 10ms sleep.
1271+
await vi.waitFor(() => pullStarted, { timeout: 2000, interval: 10 })
1272+
await flushMacrotasks()
12641273
controller.abort()
12651274
releaseStream()
12661275

@@ -2020,8 +2029,10 @@ describe("OpenAiNativeHandler", () => {
20202029
// "Request was aborted.") instead of a native AbortError when the
20212030
// request signal aborts. Cancellations must surface as the contract
20222031
// AbortError and must never be captured as provider telemetry.
2032+
let sdkRequestStarted = false
20232033
mockResponsesCreate.mockImplementation((_body: unknown, options: { signal?: AbortSignal }) => {
20242034
return new Promise((_resolve, reject) => {
2035+
sdkRequestStarted = true
20252036
const error = new Error("Request was aborted.")
20262037
error.name = "APIUserAbortError"
20272038
if (options?.signal?.aborted) {
@@ -2034,7 +2045,9 @@ describe("OpenAiNativeHandler", () => {
20342045

20352046
const controller = new AbortController()
20362047
const pending = handler.completePrompt("Test prompt", { abortSignal: controller.signal })
2037-
await new Promise((resolve) => setTimeout(resolve, 0))
2048+
// Observable: the SDK call has been issued with the request signal, so the abort now
2049+
// arrives mid-request instead of after a wall-clock delay.
2050+
await vi.waitFor(() => sdkRequestStarted, { timeout: 2000, interval: 25 })
20382051
controller.abort()
20392052

20402053
await expect(pending).rejects.toMatchObject({

0 commit comments

Comments
 (0)