|
| 1 | +import { describe, expect, it, jest } from 'bun:test'; |
| 2 | +import { installShutdownHandlers } from './shutdown.js'; |
| 3 | +import type { ShutdownProcess } from './shutdown.js'; |
| 4 | + |
| 5 | +/** A stand-in for `process` that records handlers so tests can fire them. */ |
| 6 | +function createFakeProcess() { |
| 7 | + const signalHandlers = new Map<string, Array<() => void>>(); |
| 8 | + const stdinHandlers = new Map<string, Array<() => void>>(); |
| 9 | + const exit = jest.fn(); |
| 10 | + |
| 11 | + const record = (map: Map<string, Array<() => void>>, event: string, listener: () => void) => { |
| 12 | + const existing = map.get(event) ?? []; |
| 13 | + existing.push(listener); |
| 14 | + map.set(event, existing); |
| 15 | + }; |
| 16 | + |
| 17 | + const proc: ShutdownProcess = { |
| 18 | + on(event: string, listener: () => void) { |
| 19 | + record(signalHandlers, event, listener); |
| 20 | + return proc; |
| 21 | + }, |
| 22 | + stdin: { |
| 23 | + on(event: string, listener: () => void) { |
| 24 | + record(stdinHandlers, event, listener); |
| 25 | + return proc.stdin; |
| 26 | + }, |
| 27 | + }, |
| 28 | + exit, |
| 29 | + }; |
| 30 | + |
| 31 | + return { |
| 32 | + proc, |
| 33 | + exit, |
| 34 | + fireSignal: (event: string) => { |
| 35 | + for (const handler of signalHandlers.get(event) ?? []) handler(); |
| 36 | + }, |
| 37 | + fireStdin: (event: string) => { |
| 38 | + for (const handler of stdinHandlers.get(event) ?? []) handler(); |
| 39 | + }, |
| 40 | + hasSignal: (event: string) => signalHandlers.has(event), |
| 41 | + hasStdin: (event: string) => stdinHandlers.has(event), |
| 42 | + }; |
| 43 | +} |
| 44 | + |
| 45 | +describe('installShutdownHandlers', () => { |
| 46 | + it('subscribes to the termination signals', () => { |
| 47 | + const fake = createFakeProcess(); |
| 48 | + installShutdownHandlers({ dispose: jest.fn() }, fake.proc); |
| 49 | + |
| 50 | + expect(fake.hasSignal('SIGINT')).toBe(true); |
| 51 | + expect(fake.hasSignal('SIGTERM')).toBe(true); |
| 52 | + expect(fake.hasSignal('SIGHUP')).toBe(true); |
| 53 | + }); |
| 54 | + |
| 55 | + // The regression: an MCP client that exits closes the pipe without |
| 56 | + // signalling, and StdioServerTransport never surfaces EOF. |
| 57 | + it('disposes and exits when stdin ends', () => { |
| 58 | + const fake = createFakeProcess(); |
| 59 | + const dispose = jest.fn(); |
| 60 | + installShutdownHandlers({ dispose }, fake.proc); |
| 61 | + |
| 62 | + expect(fake.hasStdin('end')).toBe(true); |
| 63 | + fake.fireStdin('end'); |
| 64 | + |
| 65 | + expect(dispose).toHaveBeenCalledTimes(1); |
| 66 | + expect(fake.exit).toHaveBeenCalledWith(0); |
| 67 | + }); |
| 68 | + |
| 69 | + it('disposes and exits when stdin closes', () => { |
| 70 | + const fake = createFakeProcess(); |
| 71 | + const dispose = jest.fn(); |
| 72 | + installShutdownHandlers({ dispose }, fake.proc); |
| 73 | + |
| 74 | + fake.fireStdin('close'); |
| 75 | + |
| 76 | + expect(dispose).toHaveBeenCalledTimes(1); |
| 77 | + expect(fake.exit).toHaveBeenCalledWith(0); |
| 78 | + }); |
| 79 | + |
| 80 | + it.each(['SIGINT', 'SIGTERM', 'SIGHUP'])('disposes and exits on %s', (signal) => { |
| 81 | + const fake = createFakeProcess(); |
| 82 | + const dispose = jest.fn(); |
| 83 | + installShutdownHandlers({ dispose }, fake.proc); |
| 84 | + |
| 85 | + fake.fireSignal(signal); |
| 86 | + |
| 87 | + expect(dispose).toHaveBeenCalledTimes(1); |
| 88 | + expect(fake.exit).toHaveBeenCalledWith(0); |
| 89 | + }); |
| 90 | + |
| 91 | + // stdin emits both 'end' and 'close', and signals can race each other. |
| 92 | + it('disposes once even when several shutdown triggers fire', () => { |
| 93 | + const fake = createFakeProcess(); |
| 94 | + const dispose = jest.fn(); |
| 95 | + installShutdownHandlers({ dispose }, fake.proc); |
| 96 | + |
| 97 | + fake.fireStdin('end'); |
| 98 | + fake.fireStdin('close'); |
| 99 | + fake.fireSignal('SIGTERM'); |
| 100 | + |
| 101 | + expect(dispose).toHaveBeenCalledTimes(1); |
| 102 | + expect(fake.exit).toHaveBeenCalledTimes(1); |
| 103 | + }); |
| 104 | + |
| 105 | + it('returns a shutdown function that follows the same path', () => { |
| 106 | + const fake = createFakeProcess(); |
| 107 | + const dispose = jest.fn(); |
| 108 | + const shutdown = installShutdownHandlers({ dispose }, fake.proc); |
| 109 | + |
| 110 | + shutdown(1); |
| 111 | + |
| 112 | + expect(dispose).toHaveBeenCalledTimes(1); |
| 113 | + expect(fake.exit).toHaveBeenCalledWith(1); |
| 114 | + }); |
| 115 | +}); |
0 commit comments