Skip to content

Commit 441ccf8

Browse files
committed
test(api): assert the rounded delay reaching AbortSignal.timeout
CodeRabbit: the fractional-timeout test only proved the call did not throw and eventually aborted - a round-down mutation (1.5 -> 1) would still pass. The test now spies AbortSignal.timeout and pins the received delay: 2 for 1.5 and 1 for 0.5 (never 0). Negative control: mutating Math.ceil to Math.floor fails exactly this test.
1 parent 6096675 commit 441ccf8

1 file changed

Lines changed: 19 additions & 6 deletions

File tree

‎src/api/providers/utils/__tests__/abort-signal.spec.ts‎

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -288,16 +288,29 @@ describe("abort-signal utilities", () => {
288288

289289
it("rounds a fractional timeoutMs up before scheduling", async () => {
290290
// Node validates AbortSignal.timeout delays as uint32: 1.5 or 0.5
291-
// would throw ERR_OUT_OF_RANGE. The helper rounds up, so a positive
292-
// fraction never becomes 0 ms either.
291+
// would throw ERR_OUT_OF_RANGE. Pin the rounded delay that actually
292+
// reaches AbortSignal.timeout (1.5 -> 2, 0.5 -> 1, never 0) so a
293+
// round-down mutation reddens this test instead of still aborting
294+
// eventually.
295+
const timeoutSpy = vi.spyOn(AbortSignal, "timeout")
293296
let result: AbortSignal | undefined
294-
expect(() => {
295-
result = mergeAbortSignalAndTimeout(undefined, 1.5)
296-
}).not.toThrow()
297+
let tiny: AbortSignal | undefined
298+
try {
299+
expect(() => {
300+
result = mergeAbortSignalAndTimeout(undefined, 1.5)
301+
}).not.toThrow()
302+
expect(timeoutSpy).toHaveBeenCalledWith(2)
303+
expect(() => {
304+
tiny = mergeAbortSignalAndTimeout(undefined, 0.5)
305+
}).not.toThrow()
306+
expect(timeoutSpy).toHaveBeenCalledWith(1)
307+
} finally {
308+
timeoutSpy.mockRestore()
309+
}
297310
expect(result).toBeInstanceOf(AbortSignal)
298311
expect(result?.aborted).toBe(false)
312+
expect(tiny).toBeInstanceOf(AbortSignal)
299313
await vi.waitFor(() => expect(result?.aborted).toBe(true))
300-
expect(() => mergeAbortSignalAndTimeout(undefined, 0.5)).not.toThrow()
301314
})
302315

303316
it("caps an overflowing timeoutMs instead of aborting immediately", async () => {

0 commit comments

Comments
 (0)