|
| 1 | +import { describe, expect, test } from "bun:test" |
| 2 | +import { runHooks, type HookEnv } from "../../src/hook" |
| 3 | +import type { HookEntry } from "../../src/hook" |
| 4 | + |
| 5 | +function makeEnv(overrides?: Partial<HookEnv>): HookEnv { |
| 6 | + return { |
| 7 | + OPENCODE_HOOK_EVENT: "PreToolUse", |
| 8 | + OPENCODE_TOOL_NAME: "bash", |
| 9 | + OPENCODE_TOOL_INPUT: "{}", |
| 10 | + OPENCODE_PROJECT_DIR: "/tmp/test", |
| 11 | + OPENCODE_SESSION_ID: "test-session-id", |
| 12 | + ...overrides, |
| 13 | + } |
| 14 | +} |
| 15 | + |
| 16 | +/** |
| 17 | + * Applies PreToolUse hook context injection to built-in tool output. |
| 18 | + * Mirrors the logic in prompt.ts Site 1. |
| 19 | + */ |
| 20 | +function injectPreToolUseContext( |
| 21 | + hookMessage: string | undefined, |
| 22 | + originalOutput: string, |
| 23 | +): string { |
| 24 | + if (hookMessage) { |
| 25 | + return `<hook-context>\n${hookMessage}\n</hook-context>\n\n${originalOutput}` |
| 26 | + } |
| 27 | + return originalOutput |
| 28 | +} |
| 29 | + |
| 30 | +/** |
| 31 | + * Applies PostToolUse hook context injection to built-in tool output. |
| 32 | + * Mirrors the logic in prompt.ts Site 2. |
| 33 | + */ |
| 34 | +function injectPostToolUseContext( |
| 35 | + hookMessage: string | undefined, |
| 36 | + currentOutput: string, |
| 37 | +): string { |
| 38 | + if (hookMessage) { |
| 39 | + return `${currentOutput}\n\n<hook-context>\n${hookMessage}\n</hook-context>` |
| 40 | + } |
| 41 | + return currentOutput |
| 42 | +} |
| 43 | + |
| 44 | +describe("hook context injection", () => { |
| 45 | + describe("PreToolUse pass-with-message injects <hook-context>", () => { |
| 46 | + test("stderr message from exit 0 produces hook-context prefix", async () => { |
| 47 | + const entries: HookEntry[] = [ |
| 48 | + { command: 'echo "security warning: path is sensitive" >&2; exit 0' }, |
| 49 | + ] |
| 50 | + const result = await runHooks(entries, "bash", makeEnv()) |
| 51 | + |
| 52 | + expect(result.action).toBe("pass") |
| 53 | + expect(result.message).toBe("security warning: path is sensitive") |
| 54 | + |
| 55 | + const toolOutput = "file contents here" |
| 56 | + const injected = injectPreToolUseContext(result.message, toolOutput) |
| 57 | + expect(injected).toBe( |
| 58 | + `<hook-context>\nsecurity warning: path is sensitive\n</hook-context>\n\n${toolOutput}`, |
| 59 | + ) |
| 60 | + }) |
| 61 | + |
| 62 | + test("multiple hook messages are joined and wrapped", async () => { |
| 63 | + const entries: HookEntry[] = [ |
| 64 | + { command: 'echo "check-1: ok" >&2; exit 0' }, |
| 65 | + { command: 'echo "check-2: warning" >&2; exit 0' }, |
| 66 | + ] |
| 67 | + const result = await runHooks(entries, "bash", makeEnv()) |
| 68 | + |
| 69 | + expect(result.action).toBe("pass") |
| 70 | + expect(result.message).toBe("check-1: ok\ncheck-2: warning") |
| 71 | + |
| 72 | + const toolOutput = "tool result" |
| 73 | + const injected = injectPreToolUseContext(result.message, toolOutput) |
| 74 | + expect(injected).toContain("<hook-context>") |
| 75 | + expect(injected).toContain("check-1: ok\ncheck-2: warning") |
| 76 | + expect(injected).toEndWith(toolOutput) |
| 77 | + }) |
| 78 | + }) |
| 79 | + |
| 80 | + describe("pass-without-message leaves output unchanged", () => { |
| 81 | + test("exit 0 without stderr produces no injection", async () => { |
| 82 | + const entries: HookEntry[] = [{ command: "exit 0" }] |
| 83 | + const result = await runHooks(entries, "bash", makeEnv()) |
| 84 | + |
| 85 | + expect(result.action).toBe("pass") |
| 86 | + expect(result.message).toBeUndefined() |
| 87 | + |
| 88 | + const toolOutput = "original output" |
| 89 | + const injected = injectPreToolUseContext(result.message, toolOutput) |
| 90 | + expect(injected).toBe(toolOutput) |
| 91 | + }) |
| 92 | + |
| 93 | + test("empty entries produce no injection", async () => { |
| 94 | + const result = await runHooks([], "bash", makeEnv()) |
| 95 | + |
| 96 | + expect(result.action).toBe("pass") |
| 97 | + expect(result.message).toBeUndefined() |
| 98 | + |
| 99 | + const toolOutput = "original output" |
| 100 | + const injected = injectPreToolUseContext(result.message, toolOutput) |
| 101 | + expect(injected).toBe(toolOutput) |
| 102 | + }) |
| 103 | + }) |
| 104 | + |
| 105 | + describe("block still works as before", () => { |
| 106 | + test("exit 2 returns block and does not reach injection", async () => { |
| 107 | + const entries: HookEntry[] = [ |
| 108 | + { command: 'echo "forbidden" >&2; exit 2' }, |
| 109 | + ] |
| 110 | + const result = await runHooks(entries, "bash", makeEnv()) |
| 111 | + |
| 112 | + expect(result.action).toBe("block") |
| 113 | + expect(result.message).toBe("forbidden") |
| 114 | + |
| 115 | + // In prompt.ts, block returns early before injection code runs. |
| 116 | + // Verify the block message is usable as-is for the error output. |
| 117 | + expect(result.message).not.toContain("<hook-context>") |
| 118 | + }) |
| 119 | + }) |
| 120 | + |
| 121 | + describe("PostToolUse context injection", () => { |
| 122 | + test("post-hook message is appended as suffix", async () => { |
| 123 | + const entries: HookEntry[] = [ |
| 124 | + { command: 'echo "audit: file was read" >&2; exit 0' }, |
| 125 | + ] |
| 126 | + const result = await runHooks( |
| 127 | + entries, |
| 128 | + "bash", |
| 129 | + makeEnv({ OPENCODE_HOOK_EVENT: "PostToolUse" }), |
| 130 | + ) |
| 131 | + |
| 132 | + expect(result.action).toBe("pass") |
| 133 | + expect(result.message).toBe("audit: file was read") |
| 134 | + |
| 135 | + const toolOutput = "file contents here" |
| 136 | + const injected = injectPostToolUseContext(result.message, toolOutput) |
| 137 | + expect(injected).toBe( |
| 138 | + `${toolOutput}\n\n<hook-context>\naudit: file was read\n</hook-context>`, |
| 139 | + ) |
| 140 | + }) |
| 141 | + |
| 142 | + test("post-hook without message leaves output unchanged", async () => { |
| 143 | + const entries: HookEntry[] = [{ command: "exit 0" }] |
| 144 | + const result = await runHooks( |
| 145 | + entries, |
| 146 | + "bash", |
| 147 | + makeEnv({ OPENCODE_HOOK_EVENT: "PostToolUse" }), |
| 148 | + ) |
| 149 | + |
| 150 | + expect(result.action).toBe("pass") |
| 151 | + expect(result.message).toBeUndefined() |
| 152 | + |
| 153 | + const toolOutput = "original output" |
| 154 | + const injected = injectPostToolUseContext(result.message, toolOutput) |
| 155 | + expect(injected).toBe(toolOutput) |
| 156 | + }) |
| 157 | + }) |
| 158 | + |
| 159 | + describe("combined Pre + Post injection", () => { |
| 160 | + test("both hooks inject context around tool output", async () => { |
| 161 | + const preEntries: HookEntry[] = [ |
| 162 | + { command: 'echo "pre-context: checking" >&2; exit 0' }, |
| 163 | + ] |
| 164 | + const postEntries: HookEntry[] = [ |
| 165 | + { command: 'echo "post-context: logged" >&2; exit 0' }, |
| 166 | + ] |
| 167 | + |
| 168 | + const preResult = await runHooks(preEntries, "bash", makeEnv()) |
| 169 | + const postResult = await runHooks( |
| 170 | + postEntries, |
| 171 | + "bash", |
| 172 | + makeEnv({ OPENCODE_HOOK_EVENT: "PostToolUse" }), |
| 173 | + ) |
| 174 | + |
| 175 | + let output = "tool execution result" |
| 176 | + output = injectPreToolUseContext(preResult.message, output) |
| 177 | + output = injectPostToolUseContext(postResult.message, output) |
| 178 | + |
| 179 | + expect(output).toStartWith("<hook-context>\npre-context: checking\n</hook-context>") |
| 180 | + expect(output).toContain("tool execution result") |
| 181 | + expect(output).toEndWith("<hook-context>\npost-context: logged\n</hook-context>") |
| 182 | + }) |
| 183 | + }) |
| 184 | +}) |
0 commit comments