|
| 1 | +import { describe, test, expect } from "bun:test" |
| 2 | +import { createNonInteractiveEnvHook, NON_INTERACTIVE_ENV } from "./index" |
| 3 | + |
| 4 | +describe("non-interactive-env hook", () => { |
| 5 | + const mockCtx = {} as Parameters<typeof createNonInteractiveEnvHook>[0] |
| 6 | + |
| 7 | + describe("git command modification", () => { |
| 8 | + test("#given git command #when hook executes #then prepends env vars", async () => { |
| 9 | + const hook = createNonInteractiveEnvHook(mockCtx) |
| 10 | + const output: { args: Record<string, unknown>; message?: string } = { |
| 11 | + args: { command: "git commit -m 'test'" }, |
| 12 | + } |
| 13 | + |
| 14 | + await hook["tool.execute.before"]( |
| 15 | + { tool: "bash", sessionID: "test", callID: "1" }, |
| 16 | + output |
| 17 | + ) |
| 18 | + |
| 19 | + const cmd = output.args.command as string |
| 20 | + expect(cmd).toContain("GIT_EDITOR=:") |
| 21 | + expect(cmd).toContain("EDITOR=:") |
| 22 | + expect(cmd).toContain("PAGER=cat") |
| 23 | + expect(cmd).toEndWith(" git commit -m 'test'") |
| 24 | + }) |
| 25 | + |
| 26 | + test("#given non-git bash command #when hook executes #then command unchanged", async () => { |
| 27 | + const hook = createNonInteractiveEnvHook(mockCtx) |
| 28 | + const output: { args: Record<string, unknown>; message?: string } = { |
| 29 | + args: { command: "ls -la" }, |
| 30 | + } |
| 31 | + |
| 32 | + await hook["tool.execute.before"]( |
| 33 | + { tool: "bash", sessionID: "test", callID: "1" }, |
| 34 | + output |
| 35 | + ) |
| 36 | + |
| 37 | + expect(output.args.command).toBe("ls -la") |
| 38 | + }) |
| 39 | + |
| 40 | + test("#given non-bash tool #when hook executes #then command unchanged", async () => { |
| 41 | + const hook = createNonInteractiveEnvHook(mockCtx) |
| 42 | + const output: { args: Record<string, unknown>; message?: string } = { |
| 43 | + args: { command: "git status" }, |
| 44 | + } |
| 45 | + |
| 46 | + await hook["tool.execute.before"]( |
| 47 | + { tool: "Read", sessionID: "test", callID: "1" }, |
| 48 | + output |
| 49 | + ) |
| 50 | + |
| 51 | + expect(output.args.command).toBe("git status") |
| 52 | + }) |
| 53 | + |
| 54 | + test("#given empty command #when hook executes #then no error", async () => { |
| 55 | + const hook = createNonInteractiveEnvHook(mockCtx) |
| 56 | + const output: { args: Record<string, unknown>; message?: string } = { |
| 57 | + args: {}, |
| 58 | + } |
| 59 | + |
| 60 | + await hook["tool.execute.before"]( |
| 61 | + { tool: "bash", sessionID: "test", callID: "1" }, |
| 62 | + output |
| 63 | + ) |
| 64 | + |
| 65 | + expect(output.args.command).toBeUndefined() |
| 66 | + }) |
| 67 | + }) |
| 68 | + |
| 69 | + describe("shell escaping", () => { |
| 70 | + test("#given git command #when building prefix #then VISUAL properly escaped", async () => { |
| 71 | + const hook = createNonInteractiveEnvHook(mockCtx) |
| 72 | + const output: { args: Record<string, unknown>; message?: string } = { |
| 73 | + args: { command: "git status" }, |
| 74 | + } |
| 75 | + |
| 76 | + await hook["tool.execute.before"]( |
| 77 | + { tool: "bash", sessionID: "test", callID: "1" }, |
| 78 | + output |
| 79 | + ) |
| 80 | + |
| 81 | + const cmd = output.args.command as string |
| 82 | + expect(cmd).toContain("VISUAL=''") |
| 83 | + }) |
| 84 | + |
| 85 | + test("#given git command #when building prefix #then all NON_INTERACTIVE_ENV vars included", async () => { |
| 86 | + const hook = createNonInteractiveEnvHook(mockCtx) |
| 87 | + const output: { args: Record<string, unknown>; message?: string } = { |
| 88 | + args: { command: "git log" }, |
| 89 | + } |
| 90 | + |
| 91 | + await hook["tool.execute.before"]( |
| 92 | + { tool: "bash", sessionID: "test", callID: "1" }, |
| 93 | + output |
| 94 | + ) |
| 95 | + |
| 96 | + const cmd = output.args.command as string |
| 97 | + for (const key of Object.keys(NON_INTERACTIVE_ENV)) { |
| 98 | + expect(cmd).toContain(`${key}=`) |
| 99 | + } |
| 100 | + }) |
| 101 | + }) |
| 102 | + |
| 103 | + describe("banned command detection", () => { |
| 104 | + test("#given vim command #when hook executes #then warning message set", async () => { |
| 105 | + const hook = createNonInteractiveEnvHook(mockCtx) |
| 106 | + const output: { args: Record<string, unknown>; message?: string } = { |
| 107 | + args: { command: "vim file.txt" }, |
| 108 | + } |
| 109 | + |
| 110 | + await hook["tool.execute.before"]( |
| 111 | + { tool: "bash", sessionID: "test", callID: "1" }, |
| 112 | + output |
| 113 | + ) |
| 114 | + |
| 115 | + expect(output.message).toContain("vim") |
| 116 | + expect(output.message).toContain("interactive") |
| 117 | + }) |
| 118 | + |
| 119 | + test("#given safe command #when hook executes #then no warning", async () => { |
| 120 | + const hook = createNonInteractiveEnvHook(mockCtx) |
| 121 | + const output: { args: Record<string, unknown>; message?: string } = { |
| 122 | + args: { command: "ls -la" }, |
| 123 | + } |
| 124 | + |
| 125 | + await hook["tool.execute.before"]( |
| 126 | + { tool: "bash", sessionID: "test", callID: "1" }, |
| 127 | + output |
| 128 | + ) |
| 129 | + |
| 130 | + expect(output.message).toBeUndefined() |
| 131 | + }) |
| 132 | + }) |
| 133 | +}) |
0 commit comments