|
| 1 | +import { spawnSync } from "node:child_process"; |
| 2 | +import { mkdirSync, realpathSync, rmSync, symlinkSync, writeFileSync } from "node:fs"; |
| 3 | +import { join } from "node:path"; |
| 4 | +import { afterEach, describe, expect, it } from "vitest"; |
| 5 | +import { useAutoCleanupTempDirTracker } from "../helpers/temp-dir.js"; |
| 6 | + |
| 7 | +const tempDirs = useAutoCleanupTempDirTracker(afterEach); |
| 8 | +const repoRoot = process.cwd(); |
| 9 | +const commonScript = join(repoRoot, "scripts/pr-lib/common.sh"); |
| 10 | +const worktreeScript = join(repoRoot, "scripts/pr-lib/worktree.sh"); |
| 11 | +const reviewScript = join(repoRoot, "scripts/pr-lib/review.sh"); |
| 12 | +const describePosix = process.platform === "win32" ? describe.skip : describe; |
| 13 | + |
| 14 | +type Fixture = { |
| 15 | + root: string; |
| 16 | + mainSha: string; |
| 17 | + siblingBranch: string; |
| 18 | + siblingSha: string; |
| 19 | +}; |
| 20 | + |
| 21 | +function git(root: string, ...args: string[]) { |
| 22 | + const result = spawnSync("git", args, { cwd: root, encoding: "utf8" }); |
| 23 | + expect(result.status, `${result.stdout}\n${result.stderr}`).toBe(0); |
| 24 | + return result.stdout.trim(); |
| 25 | +} |
| 26 | + |
| 27 | +function createFixture(): Fixture { |
| 28 | + const root = realpathSync(tempDirs.make("openclaw-pr-worktree-containment-")); |
| 29 | + git(root, "init", "--initial-branch=main"); |
| 30 | + git(root, "config", "user.name", "OpenClaw Test"); |
| 31 | + git(root, "config", "user.email", "[email protected]"); |
| 32 | + writeFileSync(join(root, "fixture.txt"), "main\n"); |
| 33 | + git(root, "add", "fixture.txt"); |
| 34 | + git(root, "commit", "-m", "main fixture"); |
| 35 | + const mainSha = git(root, "rev-parse", "HEAD"); |
| 36 | + git(root, "remote", "add", "origin", root); |
| 37 | + git(root, "fetch", "origin"); |
| 38 | + git(root, "checkout", "-b", "sibling/work"); |
| 39 | + writeFileSync(join(root, "fixture.txt"), "sibling\n"); |
| 40 | + git(root, "commit", "-am", "sibling fixture"); |
| 41 | + return { |
| 42 | + root, |
| 43 | + mainSha, |
| 44 | + siblingBranch: git(root, "branch", "--show-current"), |
| 45 | + siblingSha: git(root, "rev-parse", "HEAD"), |
| 46 | + }; |
| 47 | +} |
| 48 | + |
| 49 | +function makeStaleWorktreeDir(fixture: Fixture) { |
| 50 | + mkdirSync(join(fixture.root, ".worktrees", "pr-42"), { recursive: true }); |
| 51 | +} |
| 52 | + |
| 53 | +function runShell(fixture: Fixture, commands: string[]) { |
| 54 | + return spawnSync( |
| 55 | + "bash", |
| 56 | + [ |
| 57 | + "-c", |
| 58 | + [ |
| 59 | + "set -euo pipefail", |
| 60 | + 'source "$1"', |
| 61 | + 'source "$2"', |
| 62 | + 'source "$3"', |
| 63 | + 'fixture_root="$4"', |
| 64 | + 'repo_root() { printf "%s\\n" "$fixture_root"; }', |
| 65 | + "ensure_gh_api_auth() { :; }", |
| 66 | + "mark_pr_operation_side_effects_started() { :; }", |
| 67 | + "set_review_mode() { :; }", |
| 68 | + ...commands, |
| 69 | + ].join("\n"), |
| 70 | + "pr-worktree-containment", |
| 71 | + commonScript, |
| 72 | + worktreeScript, |
| 73 | + reviewScript, |
| 74 | + fixture.root, |
| 75 | + ], |
| 76 | + { cwd: fixture.root, encoding: "utf8" }, |
| 77 | + ); |
| 78 | +} |
| 79 | + |
| 80 | +function expectCanonicalCheckoutUnchanged(fixture: Fixture) { |
| 81 | + expect(git(fixture.root, "branch", "--show-current")).toBe(fixture.siblingBranch); |
| 82 | + expect(git(fixture.root, "rev-parse", "HEAD")).toBe(fixture.siblingSha); |
| 83 | +} |
| 84 | + |
| 85 | +describePosix("scripts/pr worktree containment", () => { |
| 86 | + it("stale .worktrees/pr-<N> directory does not clobber the canonical checkout", () => { |
| 87 | + const fixture = createFixture(); |
| 88 | + makeStaleWorktreeDir(fixture); |
| 89 | + |
| 90 | + runShell(fixture, ["enter_worktree 42 true"]); |
| 91 | + |
| 92 | + expectCanonicalCheckoutUnchanged(fixture); |
| 93 | + }); |
| 94 | + |
| 95 | + it("review_checkout_main cannot detach the canonical checkout", () => { |
| 96 | + const fixture = createFixture(); |
| 97 | + makeStaleWorktreeDir(fixture); |
| 98 | + |
| 99 | + const result = runShell(fixture, ["review_checkout_main 42"]); |
| 100 | + |
| 101 | + expectCanonicalCheckoutUnchanged(fixture); |
| 102 | + if (result.status !== 0) { |
| 103 | + expect(result.stderr).toContain("scripts/pr refuses to mutate the shared canonical checkout"); |
| 104 | + } |
| 105 | + }); |
| 106 | + |
| 107 | + it("failure midway leaves the canonical checkout untouched", () => { |
| 108 | + const fixture = createFixture(); |
| 109 | + const brokenWorktree = join(fixture.root, ".worktrees", "pr-42"); |
| 110 | + git(fixture.root, "worktree", "add", brokenWorktree, "-b", "temp/pr-42", "origin/main"); |
| 111 | + rmSync(join(brokenWorktree, ".git")); |
| 112 | + |
| 113 | + const result = runShell(fixture, [ |
| 114 | + "enter_worktree 42 false", |
| 115 | + "git checkout --detach origin/main", |
| 116 | + "exit 1", |
| 117 | + ]); |
| 118 | + |
| 119 | + expect(result.status).not.toBe(0); |
| 120 | + expectCanonicalCheckoutUnchanged(fixture); |
| 121 | + }); |
| 122 | + |
| 123 | + it("refuses a symlink alias pointing at another PR's worktree", () => { |
| 124 | + const fixture = createFixture(); |
| 125 | + const worktrees = join(fixture.root, ".worktrees"); |
| 126 | + mkdirSync(worktrees, { recursive: true }); |
| 127 | + git( |
| 128 | + fixture.root, |
| 129 | + "worktree", |
| 130 | + "add", |
| 131 | + join(worktrees, "pr-99"), |
| 132 | + "-b", |
| 133 | + "temp/pr-99", |
| 134 | + "origin/main", |
| 135 | + ); |
| 136 | + symlinkSync("pr-99", join(worktrees, "pr-42"), "dir"); |
| 137 | + |
| 138 | + const result = runShell(fixture, ["enter_worktree 42 true"]); |
| 139 | + |
| 140 | + expect(result.status).not.toBe(0); |
| 141 | + expect(result.stderr).toContain("refuses to mutate the shared canonical checkout"); |
| 142 | + expect(git(join(worktrees, "pr-99"), "branch", "--show-current")).toBe("temp/pr-99"); |
| 143 | + expectCanonicalCheckoutUnchanged(fixture); |
| 144 | + }); |
| 145 | + |
| 146 | + it("reuses a properly registered PR worktree", () => { |
| 147 | + const fixture = createFixture(); |
| 148 | + const expectedWorktree = join(fixture.root, ".worktrees", "pr-42"); |
| 149 | + git(fixture.root, "worktree", "add", expectedWorktree, "-b", "temp/pr-42", "origin/main"); |
| 150 | + |
| 151 | + const result = runShell(fixture, [ |
| 152 | + "enter_worktree 42 false", |
| 153 | + 'printf "cwd=%s\\n" "$PWD"', |
| 154 | + 'printf "branch=%s\\n" "$(git branch --show-current)"', |
| 155 | + 'printf "head=%s\\n" "$(git rev-parse HEAD)"', |
| 156 | + ]); |
| 157 | + |
| 158 | + expect(result.status, `${result.stdout}\n${result.stderr}`).toBe(0); |
| 159 | + expect(result.stdout).toContain(`cwd=${realpathSync(expectedWorktree)}`); |
| 160 | + expect(result.stdout).toContain("branch=temp/pr-42"); |
| 161 | + expect(result.stdout).toContain(`head=${fixture.mainSha}`); |
| 162 | + expectCanonicalCheckoutUnchanged(fixture); |
| 163 | + }); |
| 164 | +}); |
0 commit comments