|
| 1 | +import { describe, it, expect } from "vitest"; |
| 2 | +import { spawnSync } from "child_process"; |
| 3 | +import path from "path"; |
| 4 | + |
| 5 | +// Regression: the host harness may append extra flags/args to the hook command. |
| 6 | +// Claude Code appends `--hook-json <data>`; hook input comes from stdin, so the |
| 7 | +// CLI must tolerate (discard) the extra flag instead of erroring (#180). |
| 8 | + |
| 9 | +const CLI_ENTRY = path.join(path.resolve(__dirname, ".."), "dist", "index.js"); |
| 10 | +const PRETOOL_IN = JSON.stringify({ tool_name: "Bash", tool_input: { command: "ls" } }); |
| 11 | +const POSTTOOL_IN = JSON.stringify({ tool_name: "Bash", tool_response: { output: "ok" } }); |
| 12 | + |
| 13 | +function runHook(args: string): { code: number; out: any } { |
| 14 | + const r = spawnSync(`node ${CLI_ENTRY} ${args}`, { |
| 15 | + input: args.includes("posttool") ? POSTTOOL_IN : PRETOOL_IN, |
| 16 | + encoding: "utf-8", |
| 17 | + shell: true, |
| 18 | + timeout: 30_000, |
| 19 | + }); |
| 20 | + return { code: r.status ?? -1, out: JSON.parse(r.stdout || "{}") }; |
| 21 | +} |
| 22 | + |
| 23 | +describe("hook commands tolerate harness-appended flags (#180)", () => { |
| 24 | + it("pretool accepts --hook-json and still returns a decision", () => { |
| 25 | + const { code, out } = runHook("hook pretool --hook-json '{}'"); |
| 26 | + expect(code).toBe(0); |
| 27 | + expect(out.hookSpecificOutput?.permissionDecision).toBe("allow"); |
| 28 | + }); |
| 29 | + |
| 30 | + it("posttool accepts --hook-json", () => { |
| 31 | + const { code, out } = runHook("hook posttool --hook-json '{\"x\":1}'"); |
| 32 | + expect(code).toBe(0); |
| 33 | + expect(out.hookSpecificOutput?.hookEventName).toBe("PostToolUse"); |
| 34 | + }); |
| 35 | + |
| 36 | + it("a real option (--format) is still honored, not swallowed", () => { |
| 37 | + // gemini "allow" emits an empty object, distinct from the claude shape. |
| 38 | + const r = spawnSync(`node ${CLI_ENTRY} hook pretool --format gemini --hook-json '{}'`, { |
| 39 | + input: PRETOOL_IN, encoding: "utf-8", shell: true, timeout: 30_000, |
| 40 | + }); |
| 41 | + expect(r.status).toBe(0); |
| 42 | + expect(r.stdout.trim()).toBe("{}"); |
| 43 | + }); |
| 44 | + |
| 45 | + it("still works with no extra flag (unchanged behavior)", () => { |
| 46 | + const { code, out } = runHook("hook pretool"); |
| 47 | + expect(code).toBe(0); |
| 48 | + expect(out.hookSpecificOutput?.permissionDecision).toBe("allow"); |
| 49 | + }); |
| 50 | +}); |
0 commit comments