|
| 1 | +import path from "node:path" |
| 2 | +import { describe, expect, test } from "bun:test" |
| 3 | +import { runHook, type HookEnv } from "../../src/hook" |
| 4 | + |
| 5 | +const SCRIPT_PATH = path.resolve( |
| 6 | + import.meta.dir, |
| 7 | + "../../../../.opencode/hooks/enforce-factcheck-before-edit.sh", |
| 8 | +) |
| 9 | + |
| 10 | +function makeEnv(toolName: string, toolInput: string): HookEnv { |
| 11 | + return { |
| 12 | + OPENCODE_HOOK_EVENT: "PreToolUse", |
| 13 | + OPENCODE_TOOL_NAME: toolName, |
| 14 | + OPENCODE_TOOL_INPUT: toolInput, |
| 15 | + OPENCODE_PROJECT_DIR: "/tmp/test", |
| 16 | + OPENCODE_SESSION_ID: "test-session", |
| 17 | + } |
| 18 | +} |
| 19 | + |
| 20 | +describe("enforce-factcheck-before-edit", () => { |
| 21 | + test("passes non-edit tools without message", async () => { |
| 22 | + const result = await runHook({ command: SCRIPT_PATH }, makeEnv("bash", "{}")) |
| 23 | + expect(result.action).toBe("pass") |
| 24 | + expect(result.message).toBeUndefined() |
| 25 | + }) |
| 26 | + |
| 27 | + test("passes non-edit tool 'read'", async () => { |
| 28 | + const result = await runHook({ command: SCRIPT_PATH }, makeEnv("read", '{"file":"/tmp/x"}')) |
| 29 | + expect(result.action).toBe("pass") |
| 30 | + expect(result.message).toBeUndefined() |
| 31 | + }) |
| 32 | + |
| 33 | + test("warns on Write without evidence", async () => { |
| 34 | + const result = await runHook( |
| 35 | + { command: SCRIPT_PATH }, |
| 36 | + makeEnv("Write", '{"file_path":"/tmp/test.ts","content":"hello"}'), |
| 37 | + ) |
| 38 | + expect(result.action).toBe("pass") |
| 39 | + expect(result.message).toContain("FACT-CHECK WARNING") |
| 40 | + }) |
| 41 | + |
| 42 | + test("warns on edit without evidence", async () => { |
| 43 | + const result = await runHook( |
| 44 | + { command: SCRIPT_PATH }, |
| 45 | + makeEnv("edit", '{"old_string":"foo","new_string":"bar"}'), |
| 46 | + ) |
| 47 | + expect(result.action).toBe("pass") |
| 48 | + expect(result.message).toContain("FACT-CHECK WARNING") |
| 49 | + }) |
| 50 | + |
| 51 | + test("passes Edit with evidence phrase 'based on reading'", async () => { |
| 52 | + const result = await runHook( |
| 53 | + { command: SCRIPT_PATH }, |
| 54 | + makeEnv("Edit", '{"old_string":"based on reading the config"}'), |
| 55 | + ) |
| 56 | + expect(result.action).toBe("pass") |
| 57 | + expect(result.message).toContain("Evidence of prior research detected") |
| 58 | + }) |
| 59 | + |
| 60 | + test("passes Write with evidence phrase 'line 42'", async () => { |
| 61 | + const result = await runHook( |
| 62 | + { command: SCRIPT_PATH }, |
| 63 | + makeEnv("Write", '{"content":"fix at line 42"}'), |
| 64 | + ) |
| 65 | + expect(result.action).toBe("pass") |
| 66 | + expect(result.message).toContain("Evidence of prior research detected") |
| 67 | + }) |
| 68 | + |
| 69 | + test("passes write with evidence phrase 'grep result'", async () => { |
| 70 | + const result = await runHook( |
| 71 | + { command: SCRIPT_PATH }, |
| 72 | + makeEnv("write", '{"content":"grep result shows usage"}'), |
| 73 | + ) |
| 74 | + expect(result.action).toBe("pass") |
| 75 | + expect(result.message).toContain("Evidence of prior research detected") |
| 76 | + }) |
| 77 | + |
| 78 | + test("passes Edit with evidence phrase 'read tool output'", async () => { |
| 79 | + const result = await runHook( |
| 80 | + { command: SCRIPT_PATH }, |
| 81 | + makeEnv("Edit", '{"old_string":"per read tool output"}'), |
| 82 | + ) |
| 83 | + expect(result.action).toBe("pass") |
| 84 | + expect(result.message).toContain("Evidence of prior research detected") |
| 85 | + }) |
| 86 | + |
| 87 | + test("never blocks (advisory only)", async () => { |
| 88 | + const result = await runHook( |
| 89 | + { command: SCRIPT_PATH }, |
| 90 | + makeEnv("Write", '{"file_path":"/tmp/x","content":"no evidence here"}'), |
| 91 | + ) |
| 92 | + expect(result.action).toBe("pass") |
| 93 | + }) |
| 94 | +}) |
0 commit comments