diff options
| author | Elizabeth Hunt <me@liz.coffee> | 2025-12-14 22:39:18 -0800 |
|---|---|---|
| committer | Elizabeth Hunt <me@liz.coffee> | 2025-12-14 22:39:18 -0800 |
| commit | 666674327f009e9b1013218fc384f193b64c6997 (patch) | |
| tree | acebae7b425b469584eb0a5bec396899c2739501 /tst/signals.test.ts | |
| parent | 594ce452693a71b501d3aff3f35ef3732c06c341 (diff) | |
| download | pengueno-666674327f009e9b1013218fc384f193b64c6997.tar.gz pengueno-666674327f009e9b1013218fc384f193b64c6997.zip | |
Adds unit tests
Diffstat (limited to 'tst/signals.test.ts')
| -rw-r--r-- | tst/signals.test.ts | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/tst/signals.test.ts b/tst/signals.test.ts new file mode 100644 index 0000000..14b4da6 --- /dev/null +++ b/tst/signals.test.ts @@ -0,0 +1,70 @@ +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<string, () => 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<any>(); + 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<string, () => 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<any>(); + 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<string, () => 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<any>()); + + const p = Signals.awaitClose(t); + handlers.SIGINT(); + + const res = await p; + expect(res).toEqual(Either.right(undefined)); + }); +}); |
