import { Either, Signals } from '../lib/index'; import { CollectingTrace, TestTraceable } from './test_utils'; describe('process/signals (Signals.awaitClose)', () => { const originalOn = process.on; afterEach(() => { // restore in case a test fails mid-way (process.on as any) = originalOn; jest.restoreAllMocks(); }); test('resolves right on SIGINT with no error', async () => { const handlers: Record void> = {}; jest.spyOn(process, 'on').mockImplementation(((evt: string, cb: () => void) => { handlers[evt] = cb; return process; }) as any); const close = jest.fn((cb: (err: Error | undefined) => void) => cb(undefined)); const trace = new CollectingTrace(); const t = TestTraceable.of({ close }, trace); const p = Signals.awaitClose(t); handlers.SIGINT(); const res = await p; expect(res.left().present()).toBe(false); expect(close).toHaveBeenCalledTimes(1); const flattened = trace.events.flatMap((e) => e); expect(flattened).toEqual(expect.arrayContaining(['closing', 'finished'])); }); test('resolves left on SIGTERM with error', async () => { const handlers: Record void> = {}; jest.spyOn(process, 'on').mockImplementation(((evt: string, cb: () => void) => { handlers[evt] = cb; return process; }) as any); const close = jest.fn((cb: (err: Error | undefined) => void) => cb(new Error('boom'))); const trace = new CollectingTrace(); const t = TestTraceable.of({ close }, trace); const p = Signals.awaitClose(t); handlers.SIGTERM(); const res = await p; expect(res.left().get().message).toBe('boom'); expect(close).toHaveBeenCalledTimes(1); }); test('close callback is optional error', async () => { const handlers: Record void> = {}; jest.spyOn(process, 'on').mockImplementation(((evt: string, cb: () => void) => { handlers[evt] = cb; return process; }) as any); const close = jest.fn((cb: (err: Error | undefined) => void) => cb(undefined)); const t = TestTraceable.of({ close }, new CollectingTrace()); const p = Signals.awaitClose(t); handlers.SIGINT(); const res = await p; expect(res).toEqual(Either.right(undefined)); }); });