d5fbada13d
Move test coercion out of a hidden global and require each test to import the helper so review tools and runtime scripts can see the unsafe boundary. Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-openagent) Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
79 lines
2.5 KiB
TypeScript
79 lines
2.5 KiB
TypeScript
import { describe, test, expect } from "bun:test"
|
|
import { unsafeTestValue } from "../../../test-support/unsafe-test-value"
|
|
|
|
describe("pending-calls cleanup interval", () => {
|
|
test("starts cleanup once and unrefs timer", async () => {
|
|
//#given
|
|
const originalSetInterval = globalThis.setInterval
|
|
const setIntervalCalls: number[] = []
|
|
let unrefCalled = 0
|
|
|
|
globalThis.setInterval = unsafeTestValue<typeof setInterval>(((
|
|
_handler: TimerHandler,
|
|
timeout?: number,
|
|
..._args: unknown[]
|
|
) => {
|
|
setIntervalCalls.push(timeout as number)
|
|
return unsafeTestValue<ReturnType<typeof setInterval>>({
|
|
unref: () => {
|
|
unrefCalled += 1
|
|
},
|
|
})
|
|
}))
|
|
|
|
try {
|
|
const modulePath = new URL("./pending-calls.ts", import.meta.url).pathname
|
|
const pendingCallsModule = await import(`${modulePath}?pending-calls-test-once`)
|
|
|
|
//#when
|
|
pendingCallsModule.startPendingCallCleanup()
|
|
pendingCallsModule.startPendingCallCleanup()
|
|
|
|
//#then
|
|
expect(setIntervalCalls).toEqual([10_000])
|
|
expect(unrefCalled).toBe(1)
|
|
} finally {
|
|
globalThis.setInterval = originalSetInterval
|
|
}
|
|
})
|
|
|
|
test("#given cleanup timer already started #when stop cleanup runs #then interval state resets for future reuse", async () => {
|
|
//#given
|
|
const originalSetInterval = globalThis.setInterval
|
|
const originalClearInterval = globalThis.clearInterval
|
|
let intervalHandle: ReturnType<typeof setInterval> | undefined
|
|
let clearCalls = 0
|
|
|
|
globalThis.setInterval = unsafeTestValue<typeof setInterval>(((
|
|
_handler: TimerHandler,
|
|
_timeout?: number,
|
|
..._args: unknown[]
|
|
) => {
|
|
intervalHandle = unsafeTestValue<ReturnType<typeof setInterval>>({ unref: () => {} })
|
|
return intervalHandle
|
|
}))
|
|
|
|
globalThis.clearInterval = unsafeTestValue<typeof clearInterval>(((handle?: ReturnType<typeof setInterval>) => {
|
|
if (handle === intervalHandle) {
|
|
clearCalls += 1
|
|
}
|
|
}))
|
|
|
|
try {
|
|
const modulePath = new URL("./pending-calls.ts", import.meta.url).pathname
|
|
const pendingCallsModule = await import(`${modulePath}?pending-calls-test-stop`)
|
|
pendingCallsModule.startPendingCallCleanup()
|
|
|
|
//#when
|
|
pendingCallsModule.stopPendingCallCleanup()
|
|
pendingCallsModule.startPendingCallCleanup()
|
|
|
|
//#then
|
|
expect(clearCalls).toBe(1)
|
|
} finally {
|
|
globalThis.setInterval = originalSetInterval
|
|
globalThis.clearInterval = originalClearInterval
|
|
}
|
|
})
|
|
})
|