|
| 1 | +import { describe, it, expect, vi, afterEach } from 'vitest'; |
| 2 | +import { StdioTransport } from './transport'; |
| 3 | + |
| 4 | +// StdioTransport is the ACP wire layer: newline-delimited JSON-RPC over stdio. |
| 5 | +// We exercise its framing/routing by driving the private onData() directly (so |
| 6 | +// we never touch the real process.stdin) and spying on process.stdout for the |
| 7 | +// outbound side. |
| 8 | + |
| 9 | +function makeTransport(handler = vi.fn()) { |
| 10 | + const t = new StdioTransport(); |
| 11 | + (t as unknown as { handler: unknown }).handler = handler; |
| 12 | + return { t, handler }; |
| 13 | +} |
| 14 | +function feed(t: StdioTransport, chunk: string) { |
| 15 | + (t as unknown as { onData(c: string): void }).onData(chunk); |
| 16 | +} |
| 17 | + |
| 18 | +afterEach(() => vi.restoreAllMocks()); |
| 19 | + |
| 20 | +describe('StdioTransport — inbound framing', () => { |
| 21 | + it('parses a complete line and forwards it to the handler', () => { |
| 22 | + const { t, handler } = makeTransport(); |
| 23 | + feed(t, '{"jsonrpc":"2.0","id":1,"method":"initialize","params":{}}\n'); |
| 24 | + expect(handler).toHaveBeenCalledTimes(1); |
| 25 | + expect(handler.mock.calls[0][0]).toMatchObject({ id: 1, method: 'initialize' }); |
| 26 | + }); |
| 27 | + |
| 28 | + it('buffers a partial message until the newline arrives', () => { |
| 29 | + const { t, handler } = makeTransport(); |
| 30 | + feed(t, '{"jsonrpc":"2.0",'); |
| 31 | + expect(handler).not.toHaveBeenCalled(); |
| 32 | + feed(t, '"method":"x"}\n'); |
| 33 | + expect(handler).toHaveBeenCalledTimes(1); |
| 34 | + expect(handler.mock.calls[0][0]).toMatchObject({ method: 'x' }); |
| 35 | + }); |
| 36 | + |
| 37 | + it('splits multiple messages in one chunk, in order', () => { |
| 38 | + const { t, handler } = makeTransport(); |
| 39 | + feed(t, '{"jsonrpc":"2.0","method":"a"}\n{"jsonrpc":"2.0","method":"b"}\n'); |
| 40 | + expect(handler.mock.calls.map((c) => (c[0] as { method: string }).method)).toEqual(['a', 'b']); |
| 41 | + }); |
| 42 | + |
| 43 | + it('ignores malformed JSON and blank lines without throwing', () => { |
| 44 | + const { t, handler } = makeTransport(); |
| 45 | + expect(() => feed(t, 'not json\n\n \n')).not.toThrow(); |
| 46 | + expect(handler).not.toHaveBeenCalled(); |
| 47 | + }); |
| 48 | + |
| 49 | + it('routes a response to the matching pending request, not the handler', () => { |
| 50 | + const { t, handler } = makeTransport(); |
| 51 | + const resolve = vi.fn(); |
| 52 | + (t as unknown as { pendingRequests: Map<number, unknown> }).pendingRequests.set(5, resolve); |
| 53 | + feed(t, '{"jsonrpc":"2.0","id":5,"result":{"ok":true}}\n'); |
| 54 | + expect(resolve).toHaveBeenCalledWith({ ok: true }); |
| 55 | + expect(handler).not.toHaveBeenCalled(); |
| 56 | + expect((t as unknown as { pendingRequests: Map<number, unknown> }).pendingRequests.has(5)).toBe(false); |
| 57 | + }); |
| 58 | + |
| 59 | + it('resets the buffer instead of growing past the 10MB cap', () => { |
| 60 | + const { t, handler } = makeTransport(); |
| 61 | + feed(t, 'x'.repeat(10 * 1024 * 1024 + 1)); // no newline — would otherwise buffer forever |
| 62 | + expect(handler).not.toHaveBeenCalled(); |
| 63 | + expect((t as unknown as { buffer: string }).buffer).toBe(''); |
| 64 | + }); |
| 65 | +}); |
| 66 | + |
| 67 | +describe('StdioTransport — outbound frames', () => { |
| 68 | + it('respond() writes a JSON-RPC result line', () => { |
| 69 | + const write = vi.spyOn(process.stdout, 'write').mockImplementation(() => true); |
| 70 | + new StdioTransport().respond(1, { x: 1 }); |
| 71 | + expect(write).toHaveBeenCalledWith('{"jsonrpc":"2.0","id":1,"result":{"x":1}}\n'); |
| 72 | + }); |
| 73 | + |
| 74 | + it('error() writes a JSON-RPC error line', () => { |
| 75 | + const write = vi.spyOn(process.stdout, 'write').mockImplementation(() => true); |
| 76 | + new StdioTransport().error(2, -32601, 'Method not found'); |
| 77 | + const sent = JSON.parse((write.mock.calls[0][0] as string).trim()); |
| 78 | + expect(sent).toEqual({ jsonrpc: '2.0', id: 2, error: { code: -32601, message: 'Method not found' } }); |
| 79 | + }); |
| 80 | + |
| 81 | + it('notify() writes a JSON-RPC notification (no id)', () => { |
| 82 | + const write = vi.spyOn(process.stdout, 'write').mockImplementation(() => true); |
| 83 | + new StdioTransport().notify('session/update', { a: 1 }); |
| 84 | + const sent = JSON.parse((write.mock.calls[0][0] as string).trim()); |
| 85 | + expect(sent).toEqual({ jsonrpc: '2.0', method: 'session/update', params: { a: 1 } }); |
| 86 | + expect('id' in sent).toBe(false); |
| 87 | + }); |
| 88 | +}); |
| 89 | + |
| 90 | +describe('StdioTransport — outbound request round-trip', () => { |
| 91 | + it('sends a request with an incrementing id and resolves on the matching response', async () => { |
| 92 | + const write = vi.spyOn(process.stdout, 'write').mockImplementation(() => true); |
| 93 | + const t = new StdioTransport(); |
| 94 | + const p = t.request('session/request_permission', { foo: 1 }); |
| 95 | + |
| 96 | + const sent = JSON.parse((write.mock.calls[0][0] as string).trim()); |
| 97 | + expect(sent).toMatchObject({ jsonrpc: '2.0', method: 'session/request_permission', params: { foo: 1 } }); |
| 98 | + expect(typeof sent.id).toBe('number'); |
| 99 | + |
| 100 | + feed(t, JSON.stringify({ jsonrpc: '2.0', id: sent.id, result: { ok: true } }) + '\n'); |
| 101 | + await expect(p).resolves.toEqual({ ok: true }); |
| 102 | + }); |
| 103 | + |
| 104 | + it('resolves to null when the request times out', async () => { |
| 105 | + vi.useFakeTimers(); |
| 106 | + try { |
| 107 | + vi.spyOn(process.stdout, 'write').mockImplementation(() => true); |
| 108 | + const p = new StdioTransport().request('m', {}); |
| 109 | + vi.advanceTimersByTime(30_000); |
| 110 | + await expect(p).resolves.toBeNull(); |
| 111 | + } finally { |
| 112 | + vi.useRealTimers(); |
| 113 | + } |
| 114 | + }); |
| 115 | +}); |
0 commit comments