Files
kestrelos/test/integration/shutdown.spec.js
Madison Grubb 82bd51c3a4
Some checks failed
ci/woodpecker/pr/pr Pipeline failed
more functional design principles for tests
2026-02-17 11:21:02 -05:00

84 lines
2.4 KiB
JavaScript

import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest'
import { registerCleanup, graceful, initShutdownHandlers, clearCleanup } from '../../server/utils/shutdown.js'
describe('shutdown integration', () => {
const testState = {
originalExit: null,
exitCalls: [],
originalOn: null,
}
beforeEach(() => {
clearCleanup()
testState.exitCalls = []
testState.originalExit = process.exit
process.exit = vi.fn((code) => {
testState.exitCalls.push(code)
})
testState.originalOn = process.on
})
afterEach(() => {
process.exit = testState.originalExit
process.on = testState.originalOn
clearCleanup()
})
it('initializes signal handlers', () => {
const handlers = {}
process.on = vi.fn((signal, handler) => {
handlers[signal] = handler
})
initShutdownHandlers()
expect(process.on).toHaveBeenCalledWith('SIGTERM', expect.any(Function))
expect(process.on).toHaveBeenCalledWith('SIGINT', expect.any(Function))
process.on = testState.originalOn
})
it('signal handler calls graceful', async () => {
const handlers = {}
process.on = vi.fn((signal, handler) => {
handlers[signal] = handler
})
initShutdownHandlers()
const sigtermHandler = handlers.SIGTERM
expect(sigtermHandler).toBeDefined()
await sigtermHandler()
expect(testState.exitCalls.length).toBeGreaterThan(0)
process.on = testState.originalOn
})
it('signal handler handles graceful error', async () => {
const handlers = {}
process.on = vi.fn((signal, handler) => {
handlers[signal] = handler
})
initShutdownHandlers()
const sigintHandler = handlers.SIGINT
vi.spyOn(console, 'error').mockImplementation(() => {})
registerCleanup(async () => {
throw new Error('Force error')
})
await sigintHandler()
expect(testState.exitCalls.length).toBeGreaterThan(0)
process.on = testState.originalOn
})
it('covers timeout path in graceful', async () => {
registerCleanup(async () => {
await new Promise(resolve => setTimeout(resolve, 40000))
})
graceful()
await new Promise(resolve => setTimeout(resolve, 100))
expect(testState.exitCalls.length).toBeGreaterThan(0)
})
it('covers graceful catch block', async () => {
registerCleanup(async () => {
throw new Error('Test error')
})
await graceful()
expect(testState.exitCalls.length).toBeGreaterThan(0)
})
})