Skip to content

Commit efb3b4f

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 f160a99 commit efb3b4f

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
@@ -289,16 +289,29 @@ describe("abort-signal utilities", () => {
289289

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

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

0 commit comments

Comments
 (0)