|
| 1 | +import { describe, it, expect, afterAll } from "vitest"; |
| 2 | +import { e2eEnv } from "../env.js"; |
| 3 | + |
| 4 | +/** |
| 5 | + * Verifies the Stop hook forces Claude Code to commit uncommitted changes |
| 6 | + * before exiting in --print mode. |
| 7 | + * |
| 8 | + * Flow: |
| 9 | + * 1. Create a sandbox from the e2e repo |
| 10 | + * 2. Install Claude Code + configure the Stop hook |
| 11 | + * 3. Create an uncommitted file |
| 12 | + * 4. Run Claude Code with a simple prompt (--print mode) |
| 13 | + * 5. Assert git status is clean (hook forced a commit) |
| 14 | + */ |
| 15 | +describe("Stop hook commit guard", () => { |
| 16 | + let sandbox: any; |
| 17 | + |
| 18 | + afterAll(async () => { |
| 19 | + if (sandbox) await sandbox.stop().catch(() => {}); |
| 20 | + }); |
| 21 | + |
| 22 | + it("forces Claude Code to commit uncommitted changes before stopping", async () => { |
| 23 | + const { Sandbox } = await import("@vercel/sandbox"); |
| 24 | + |
| 25 | + sandbox = await Sandbox.create({ |
| 26 | + source: { |
| 27 | + type: "git", |
| 28 | + url: `https://github.com/${e2eEnv.E2E_GITHUB_OWNER}/${e2eEnv.E2E_GITHUB_REPO}.git`, |
| 29 | + username: "x-access-token", |
| 30 | + password: e2eEnv.E2E_GITHUB_TOKEN, |
| 31 | + revision: "main", |
| 32 | + depth: 1, |
| 33 | + }, |
| 34 | + runtime: "node24", |
| 35 | + timeout: 300_000, |
| 36 | + env: { |
| 37 | + ANTHROPIC_API_KEY: process.env.ANTHROPIC_API_KEY ?? "", |
| 38 | + ...(process.env.CLAUDE_CODE_OAUTH_TOKEN |
| 39 | + ? { CLAUDE_CODE_OAUTH_TOKEN: process.env.CLAUDE_CODE_OAUTH_TOKEN } |
| 40 | + : {}), |
| 41 | + }, |
| 42 | + }); |
| 43 | + |
| 44 | + // 1. Configure git identity |
| 45 | + await sandbox.runCommand("bash", [ |
| 46 | + "-c", |
| 47 | + 'git config user.name "test-bot" && git config user.email "test@test.com"', |
| 48 | + ]); |
| 49 | + |
| 50 | + // 2. Install Claude Code |
| 51 | + await sandbox.runCommand("npm", [ |
| 52 | + "install", |
| 53 | + "-g", |
| 54 | + "@anthropic-ai/claude-code", |
| 55 | + ]); |
| 56 | + |
| 57 | + // 3. Set up the Stop hook (same as SandboxManager.provision) |
| 58 | + await sandbox.runCommand("bash", [ |
| 59 | + "-c", |
| 60 | + [ |
| 61 | + `mkdir -p ~/.claude`, |
| 62 | + `cat > ~/.claude/commit-guard.sh << 'SCRIPT'`, |
| 63 | + `#!/bin/bash`, |
| 64 | + `input=$(cat)`, |
| 65 | + `if echo "$input" | grep -q '"stop_hook_active":true'; then exit 0; fi`, |
| 66 | + `changes=$(git status --porcelain | grep -v '^.. \\.claude/' | grep -v '^?? \\.claude/' | grep -v 'requirements\\.md')`, |
| 67 | + `if [ -n "$changes" ]; then`, |
| 68 | + ` echo '{"decision":"block","reason":"You have uncommitted changes. You MUST either commit all changes with a descriptive message or revert them before stopping."}' >&2`, |
| 69 | + ` exit 2`, |
| 70 | + `fi`, |
| 71 | + `SCRIPT`, |
| 72 | + `chmod +x ~/.claude/commit-guard.sh`, |
| 73 | + `cat > ~/.claude/settings.json << 'JSON'`, |
| 74 | + `{"hooks":{"Stop":[{"matcher":"","hooks":[{"type":"command","command":"bash ~/.claude/commit-guard.sh"}]}]}}`, |
| 75 | + `JSON`, |
| 76 | + ].join("\n"), |
| 77 | + ]); |
| 78 | + |
| 79 | + // 4. Skip onboarding (if using OAuth token) |
| 80 | + if (process.env.CLAUDE_CODE_OAUTH_TOKEN) { |
| 81 | + await sandbox.runCommand("bash", [ |
| 82 | + "-c", |
| 83 | + `echo '{"hasCompletedOnboarding":true}' > ~/.claude.json`, |
| 84 | + ]); |
| 85 | + } |
| 86 | + |
| 87 | + // 5. Create an uncommitted file (simulates agent work without committing) |
| 88 | + await sandbox.runCommand("bash", [ |
| 89 | + "-c", |
| 90 | + 'echo "test content" > test-uncommitted.txt', |
| 91 | + ]); |
| 92 | + |
| 93 | + // Verify the file is uncommitted |
| 94 | + const beforeStatus = await sandbox.runCommand("git", [ |
| 95 | + "status", |
| 96 | + "--porcelain", |
| 97 | + ]); |
| 98 | + const beforeOutput = (await beforeStatus.stdout()).trim(); |
| 99 | + expect(beforeOutput).toContain("test-uncommitted.txt"); |
| 100 | + |
| 101 | + // 6. Run Claude Code — the prompt must instruct it to commit any uncommitted changes |
| 102 | + const result = await sandbox.runCommand("bash", [ |
| 103 | + "-c", |
| 104 | + 'echo "Commit all uncommitted changes with a descriptive commit message, then exit." | claude --print --dangerously-skip-permissions', |
| 105 | + ]); |
| 106 | + const stdout = (await result.stdout()).trim(); |
| 107 | + const stderr = (await result.stderr()).trim(); |
| 108 | + |
| 109 | + console.log("Claude stdout:", stdout); |
| 110 | + console.log("Claude stderr:", stderr); |
| 111 | + |
| 112 | + // 7. Check git status — should be clean if the hook worked |
| 113 | + const afterStatus = await sandbox.runCommand("git", [ |
| 114 | + "status", |
| 115 | + "--porcelain", |
| 116 | + ]); |
| 117 | + const afterOutput = (await afterStatus.stdout()).trim(); |
| 118 | + |
| 119 | + expect(afterOutput).toBe(""); |
| 120 | + }); |
| 121 | +}); |
0 commit comments