/** * 「会话 cwd 在 linked worktree 里时,主仓的 `.git` 也进可删边界」的**接线**测试。 * * 为什么断言 profile 文本而不是真跑一次 `git commit`:seatbelt **不允许嵌套** —— 在 pi 会话 * 里跑 `node --test` 时内层的 `sandbox_apply` 会直接失败(`Operation not permitted`, exit 71), * 这与「沙箱配置对不对」无关。所以这里把要验证的东西前移到可读的证据上: * profile 的 allow 行里有没有那个目录。真沙箱用例在 render.test.ts 里,嵌套不可用时 skip。 * * node --test clients/pi/extensions/bash-command-collapse/git-meta-wiring.test.ts */ import assert from "node:assert/strict"; import { spawnSync } from "node:child_process"; import fs from "node:fs"; import os from "node:os"; import path from "node:path"; import test from "node:test"; import { gitMetaWriteRoots } from "./git-meta.ts"; import { boundaryFromEnv, buildSeatbeltProfile, withExtraRoots } from "./sandbox.ts"; const cleanup: Array<() => void> = []; test.after(() => { for (const fn of cleanup) fn(); }); function makeRepo(): { repo: string; worktree: string } { const root = fs.realpathSync(fs.mkdtempSync(path.join(os.tmpdir(), "pi-gitmeta-wire-"))); const repo = path.join(root, "repo"); fs.mkdirSync(repo); const git = (cwd: string, ...args: string[]) => { const r = spawnSync("git", args, { cwd, encoding: "utf8" }); assert.equal(r.status, 0, `git ${args.join(" ")}: ${r.stderr}`); }; git(repo, "init", "-q"); git(repo, "config", "user.email", "t@example.com"); git(repo, "config", "user.name", "t"); git(repo, "config", "commit.gpgsign", "false"); fs.writeFileSync(path.join(repo, "a.txt"), "a\n"); git(repo, "add", "."); git(repo, "commit", "-qm", "init"); const worktree = path.join(repo, ".worktrees", "feat"); git(repo, "worktree", "add", "-q", worktree, "-b", "feat"); cleanup.push(() => fs.rmSync(root, { recursive: true, force: true })); return { repo, worktree }; } /** 从 profile 的 allow 行里取出所有 subpath。 */ function allowSubpaths(profile: string): string[] { const line = profile.split("\n").find((l) => l.startsWith("(allow file-write-unlink")); assert.ok(line, "profile 必须有 allow 行"); return [...line!.matchAll(/\(subpath "([^"]+)"\)/g)].map((m) => m[1]); } test("composition:worktree 会话的 profile 里包含主仓 .git,主仓根会话里没有", () => { const { repo, worktree } = makeRepo(); const mainBoundary = withExtraRoots(boundaryFromEnv(repo), gitMetaWriteRoots(repo)); const mainSubpaths = allowSubpaths(buildSeatbeltProfile(mainBoundary)); // 主仓根会话:.git 本来就在项目目录之下,不该作为**单独**条目出现 assert.equal( mainSubpaths.filter((p) => p === fs.realpathSync(path.join(repo, ".git"))).length, 0, "主仓根会话不该额外列出 .git", ); const wtBoundary = withExtraRoots(boundaryFromEnv(worktree), gitMetaWriteRoots(worktree)); const wtSubpaths = allowSubpaths(buildSeatbeltProfile(wtBoundary)); const gitDir = fs.realpathSync(path.join(repo, ".git")); assert.ok(wtSubpaths.includes(gitDir), `worktree 会话的可删边界必须含 ${gitDir}(实际:${wtSubpaths.join(", ")})`); assert.ok( wtSubpaths.some((p) => p === fs.realpathSync(worktree)), "worktree 自己的目录当然也要在边界里", ); }); test("主仓根会话的 profile 逐字节等价于不加 git 根时的那份", () => { const { repo } = makeRepo(); const plain = buildSeatbeltProfile(boundaryFromEnv(repo)); const withGit = buildSeatbeltProfile(withExtraRoots(boundaryFromEnv(repo), gitMetaWriteRoots(repo))); assert.equal(withGit, plain, "主仓根会话不能因为这条改动产生任何 profile 差异"); });