|
| 1 | +/** |
| 2 | + * Coverage tests targeting lines 62-63 of acp-adapter.ts: |
| 3 | + * the branch where spawn returns a child process with missing stdin/stdout pipes. |
| 4 | + * |
| 5 | + * Branch: `if (!child.stdin || !child.stdout) { child.kill(); throw ... }` |
| 6 | + */ |
| 7 | + |
| 8 | +import type { ChildProcess } from "node:child_process"; |
| 9 | +import { EventEmitter } from "node:events"; |
| 10 | +import { describe, expect, it, vi } from "vitest"; |
| 11 | +import type { SpawnFn } from "./acp-adapter.js"; |
| 12 | +import { AcpAdapter } from "./acp-adapter.js"; |
| 13 | + |
| 14 | +function makeMinimalChild(overrides: Partial<{ stdin: unknown; stdout: unknown }>): ChildProcess { |
| 15 | + const child = new EventEmitter() as ChildProcess; |
| 16 | + const kill = vi.fn((_signal?: string) => true); |
| 17 | + Object.assign(child, { |
| 18 | + stdin: overrides.stdin ?? null, |
| 19 | + stdout: overrides.stdout ?? null, |
| 20 | + stderr: new EventEmitter(), |
| 21 | + pid: 99999, |
| 22 | + killed: false, |
| 23 | + kill, |
| 24 | + }); |
| 25 | + return child; |
| 26 | +} |
| 27 | + |
| 28 | +describe("AcpAdapter — missing stdio pipes (lines 62-63)", () => { |
| 29 | + it("kills child and throws when stdin is null", async () => { |
| 30 | + // Spawn returns a child whose stdin is null (e.g. stdio: 'ignore') |
| 31 | + const child = makeMinimalChild({ stdin: null, stdout: new EventEmitter() }); |
| 32 | + const spawnFn: SpawnFn = vi.fn(() => child); |
| 33 | + |
| 34 | + const adapter = new AcpAdapter(spawnFn); |
| 35 | + await expect(adapter.connect({ sessionId: "sess-no-stdin" })).rejects.toThrow( |
| 36 | + "Failed to open stdio pipes for ACP subprocess", |
| 37 | + ); |
| 38 | + |
| 39 | + expect(child.kill).toHaveBeenCalled(); |
| 40 | + }); |
| 41 | + |
| 42 | + it("kills child and throws when stdout is null", async () => { |
| 43 | + // Spawn returns a child whose stdout is null |
| 44 | + const child = makeMinimalChild({ stdin: new EventEmitter(), stdout: null }); |
| 45 | + const spawnFn: SpawnFn = vi.fn(() => child); |
| 46 | + |
| 47 | + const adapter = new AcpAdapter(spawnFn); |
| 48 | + await expect(adapter.connect({ sessionId: "sess-no-stdout" })).rejects.toThrow( |
| 49 | + "Failed to open stdio pipes for ACP subprocess", |
| 50 | + ); |
| 51 | + |
| 52 | + expect(child.kill).toHaveBeenCalled(); |
| 53 | + }); |
| 54 | + |
| 55 | + it("kills child and throws when both stdin and stdout are null", async () => { |
| 56 | + const child = makeMinimalChild({ stdin: null, stdout: null }); |
| 57 | + const spawnFn: SpawnFn = vi.fn(() => child); |
| 58 | + |
| 59 | + const adapter = new AcpAdapter(spawnFn); |
| 60 | + await expect(adapter.connect({ sessionId: "sess-no-pipes" })).rejects.toThrow( |
| 61 | + "Failed to open stdio pipes for ACP subprocess", |
| 62 | + ); |
| 63 | + |
| 64 | + expect(child.kill).toHaveBeenCalled(); |
| 65 | + }); |
| 66 | +}); |
0 commit comments