|
| 1 | +import { EventEmitter } from 'events'; |
| 2 | + |
| 3 | +import { describe, expect, it, vi } from 'vitest'; |
| 4 | + |
| 5 | +import { DISPOSE_EXIT_DEADLINE_MS, handleConnection } from '../src/cli/session/connection'; |
| 6 | + |
| 7 | +import type { Socket } from 'net'; |
| 8 | + |
| 9 | +/** Minimal socket double: enough surface for readMessage/sendResponse and the disconnect hooks. */ |
| 10 | +class FakeSocket extends EventEmitter { |
| 11 | + destroyed = false; |
| 12 | + written: string[] = []; |
| 13 | + timeouts: number[] = []; |
| 14 | + /** Set to false to simulate a client that never drains the response. */ |
| 15 | + flushes = true; |
| 16 | + |
| 17 | + setTimeout(ms: number): this { |
| 18 | + this.timeouts.push(ms); |
| 19 | + return this; |
| 20 | + } |
| 21 | + |
| 22 | + setEncoding(): this { |
| 23 | + return this; |
| 24 | + } |
| 25 | + |
| 26 | + destroy(): this { |
| 27 | + if (this.destroyed) return this; |
| 28 | + this.destroyed = true; |
| 29 | + this.emit('close'); |
| 30 | + return this; |
| 31 | + } |
| 32 | + |
| 33 | + end(chunk?: string): this { |
| 34 | + if (chunk) this.written.push(chunk); |
| 35 | + if (this.flushes) this.emit('finish'); |
| 36 | + return this; |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +function connect(): { socket: FakeSocket; net: Socket } { |
| 41 | + const socket = new FakeSocket(); |
| 42 | + return { socket, net: socket as unknown as Socket }; |
| 43 | +} |
| 44 | + |
| 45 | +describe('session connection', () => { |
| 46 | + it('aborts the running command as soon as the client disconnects', async () => { |
| 47 | + const { socket, net } = connect(); |
| 48 | + let signal: AbortSignal | undefined; |
| 49 | + const dispatch = vi.fn((_message: unknown, received: AbortSignal) => { |
| 50 | + signal = received; |
| 51 | + return new Promise(() => {}); |
| 52 | + }); |
| 53 | + |
| 54 | + handleConnection(net, { dispatch }); |
| 55 | + socket.emit('data', '{"command":"analyze","query":"Which region?"}\n'); |
| 56 | + |
| 57 | + await vi.waitFor(() => expect(dispatch).toHaveBeenCalledOnce()); |
| 58 | + expect(signal?.aborted).toBe(false); |
| 59 | + |
| 60 | + socket.emit('end'); |
| 61 | + |
| 62 | + expect(signal?.aborted).toBe(true); |
| 63 | + expect(socket.destroyed).toBe(true); |
| 64 | + }); |
| 65 | + |
| 66 | + it('drops the response for a command whose client already left', async () => { |
| 67 | + const { socket, net } = connect(); |
| 68 | + let release: ((value: unknown) => void) | undefined; |
| 69 | + const dispatch = vi.fn(() => new Promise((resolve) => (release = resolve))); |
| 70 | + |
| 71 | + handleConnection(net, { dispatch }); |
| 72 | + socket.emit('data', '{"command":"schema"}\n'); |
| 73 | + await vi.waitFor(() => expect(dispatch).toHaveBeenCalledOnce()); |
| 74 | + |
| 75 | + socket.emit('end'); |
| 76 | + release!({ models: [] }); |
| 77 | + await new Promise((resolve) => setTimeout(resolve, 20)); |
| 78 | + |
| 79 | + expect(socket.written).toEqual([]); |
| 80 | + }); |
| 81 | + |
| 82 | + it('tracks busy state for the idle timer', async () => { |
| 83 | + const { socket, net } = connect(); |
| 84 | + const onBusy = vi.fn(); |
| 85 | + |
| 86 | + handleConnection(net, { dispatch: vi.fn().mockResolvedValue({ ok: 1 }), onBusy }); |
| 87 | + socket.emit('data', '{"command":"profile"}\n'); |
| 88 | + |
| 89 | + await vi.waitFor(() => expect(onBusy).toHaveBeenCalledTimes(2)); |
| 90 | + expect(onBusy.mock.calls).toEqual([[true], [false]]); |
| 91 | + }); |
| 92 | + |
| 93 | + it('exits once the dispose response is flushed', async () => { |
| 94 | + const { socket, net } = connect(); |
| 95 | + const exit = vi.fn(); |
| 96 | + |
| 97 | + handleConnection(net, { dispatch: vi.fn().mockResolvedValue({ disabled: true, disposed: true }), exit }); |
| 98 | + socket.emit('data', '{"command":"dispose"}\n'); |
| 99 | + |
| 100 | + await vi.waitFor(() => expect(exit).toHaveBeenCalledWith(0)); |
| 101 | + expect(socket.written).toHaveLength(1); |
| 102 | + expect(JSON.parse(socket.written[0].trim())).toEqual({ ok: true, result: { disabled: true, disposed: true } }); |
| 103 | + }); |
| 104 | + |
| 105 | + it('bounds the exit of an unflushed dispose response', async () => { |
| 106 | + vi.useFakeTimers(); |
| 107 | + try { |
| 108 | + const { socket, net } = connect(); |
| 109 | + const exit = vi.fn(); |
| 110 | + socket.flushes = false; |
| 111 | + |
| 112 | + handleConnection(net, { dispatch: vi.fn().mockResolvedValue({ disposed: true }), exit }); |
| 113 | + socket.emit('data', '{"command":"dispose"}\n'); |
| 114 | + await vi.advanceTimersByTimeAsync(0); |
| 115 | + |
| 116 | + expect(exit).not.toHaveBeenCalled(); |
| 117 | + await vi.advanceTimersByTimeAsync(DISPOSE_EXIT_DEADLINE_MS); |
| 118 | + expect(exit).toHaveBeenCalledWith(0); |
| 119 | + } finally { |
| 120 | + vi.useRealTimers(); |
| 121 | + } |
| 122 | + }); |
| 123 | + |
| 124 | + it('answers an invalid request with an error instead of running it', async () => { |
| 125 | + const { socket, net } = connect(); |
| 126 | + const dispatch = vi.fn(); |
| 127 | + |
| 128 | + handleConnection(net, { dispatch }); |
| 129 | + socket.emit('data', '{"command":"nope"}\n'); |
| 130 | + |
| 131 | + await vi.waitFor(() => expect(socket.written).toHaveLength(1)); |
| 132 | + expect(dispatch).not.toHaveBeenCalled(); |
| 133 | + expect(JSON.parse(socket.written[0].trim())).toMatchObject({ ok: false }); |
| 134 | + }); |
| 135 | +}); |
0 commit comments