workspace-hook-trust-identity.test.ts 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. // Hook Trust identity 的 CLI adapter(docs/specs/workspace-hook-trust.md D23)。
  2. // 用真实 git 临时仓库 + 受管 worktree 布局 <dataBaseDir>/.zcode/worktrees/<4hex>/<repo> 验证:
  3. // - adapter 与共享 resolver 得到同一个 Trust identity;
  4. // - Runtime snapshot(createConfig)在 worktree 中归一为源仓库路径,bundleDigest 与源仓库一致;
  5. // - 伪造反向链接不继承;远程 identity 原样返回。
  6. import { execFile } from "node:child_process";
  7. import { mkdir, mkdtemp, realpath, rm, writeFile } from "node:fs/promises";
  8. import { tmpdir } from "node:os";
  9. import { join } from "node:path";
  10. import { promisify } from "node:util";
  11. import { afterEach, describe, expect, it } from "vitest";
  12. import {
  13. createConfig,
  14. resolveWorkspaceHookTrustIdentityForWorkspace,
  15. } from "../src/config/index.js";
  16. const execFileAsync = promisify(execFile);
  17. const roots: string[] = [];
  18. const BUCKET = "3f2a";
  19. const REPO = "repo";
  20. afterEach(async () => {
  21. await Promise.all(roots.splice(0).map((path) => rm(path, { recursive: true, force: true })));
  22. });
  23. async function git(cwd: string, ...args: string[]): Promise<void> {
  24. await execFileAsync(
  25. "git",
  26. ["-c", "user.name=ZCode Test", "-c", "user.email=test@example.com", ...args],
  27. { cwd },
  28. );
  29. }
  30. /** 源仓库提交一条 workspace Hook,再按受管布局创建 detached worktree。 */
  31. async function managedWorktreeFixture() {
  32. // macOS 临时目录 /var 是 /private/var 的软链;resolver 按 realpath 判定,fixture 也用 realpath。
  33. const root = await realpath(await mkdtemp(join(tmpdir(), "zcode-hook-trust-identity-")));
  34. roots.push(root);
  35. const source = join(root, "projects", REPO);
  36. const dataBaseDir = join(root, "data");
  37. const worktree = join(dataBaseDir, ".zcode", "worktrees", BUCKET, REPO);
  38. await mkdir(join(source, ".zcode"), { recursive: true });
  39. await mkdir(join(source, "packages", "app"), { recursive: true });
  40. await writeFile(join(source, "packages", "app", "README.md"), "app\n");
  41. await writeFile(
  42. join(source, ".zcode", "config.json"),
  43. JSON.stringify({
  44. hooks: {
  45. enabled: true,
  46. events: {
  47. SessionStart: [
  48. { matcher: "startup", hooks: [{ type: "command", command: "./start.sh" }] },
  49. ],
  50. },
  51. },
  52. }),
  53. );
  54. await git(source, "init", "-q");
  55. await git(source, "add", ".");
  56. await git(source, "commit", "-q", "-m", "init");
  57. await mkdir(join(dataBaseDir, ".zcode", "worktrees", BUCKET), { recursive: true });
  58. await git(source, "worktree", "add", "-q", "--detach", worktree);
  59. return { dataBaseDir, env: { ZCODE_DATA_BASE_DIR: dataBaseDir }, root, source, worktree };
  60. }
  61. describe("resolveWorkspaceHookTrustIdentityForWorkspace", () => {
  62. it("受管 worktree 及其子目录归一为源仓库中对应路径;源仓库与非受管目录保持自身路径", async () => {
  63. const value = await managedWorktreeFixture();
  64. await expect(
  65. resolveWorkspaceHookTrustIdentityForWorkspace({
  66. workspacePath: value.worktree,
  67. env: value.env,
  68. }),
  69. ).resolves.toBe(value.source);
  70. await expect(
  71. resolveWorkspaceHookTrustIdentityForWorkspace({
  72. workspacePath: join(value.worktree, "packages", "app"),
  73. env: value.env,
  74. }),
  75. ).resolves.toBe(join(value.source, "packages", "app"));
  76. await expect(
  77. resolveWorkspaceHookTrustIdentityForWorkspace({
  78. workspacePath: value.source,
  79. env: value.env,
  80. }),
  81. ).resolves.toBe(value.source);
  82. // ZCODE_DATA_BASE_DIR 指向别处时,同一路径不在受管根目录下,不继承。
  83. await expect(
  84. resolveWorkspaceHookTrustIdentityForWorkspace({
  85. workspacePath: value.worktree,
  86. env: { ZCODE_DATA_BASE_DIR: join(value.root, "other-data") },
  87. }),
  88. ).resolves.toBe(value.worktree);
  89. });
  90. it("伪造 gitdir 反向链接时不继承,退回 worktree 自身路径", async () => {
  91. const value = await managedWorktreeFixture();
  92. const adminDir = join(value.source, ".git", "worktrees", REPO);
  93. await writeFile(join(adminDir, "gitdir"), `${join(value.root, "elsewhere", ".git")}\n`);
  94. await expect(
  95. resolveWorkspaceHookTrustIdentityForWorkspace({
  96. workspacePath: value.worktree,
  97. env: value.env,
  98. }),
  99. ).resolves.toBe(value.worktree);
  100. });
  101. it("远程 identity 原样返回,不做本地解析", async () => {
  102. const value = await managedWorktreeFixture();
  103. const identity = "remote:ssh:example.test:22:coder:/workspace/repo";
  104. await expect(
  105. resolveWorkspaceHookTrustIdentityForWorkspace({
  106. workspacePath: value.worktree,
  107. workspaceIdentity: identity,
  108. env: value.env,
  109. }),
  110. ).resolves.toBe(identity);
  111. });
  112. });
  113. describe("Runtime snapshot 的 Trust identity", () => {
  114. it("worktree 中 snapshot identity 是源仓库路径,bundleDigest 与源仓库相同", async () => {
  115. const value = await managedWorktreeFixture();
  116. const build = async (workingDirectory: string) =>
  117. createConfig({
  118. env: value.env,
  119. skipUserConfig: true,
  120. workingDirectory,
  121. workspaceHookTrustIdentity: await resolveWorkspaceHookTrustIdentityForWorkspace({
  122. workspacePath: workingDirectory,
  123. env: value.env,
  124. }),
  125. }).sources.project.workspaceHookSnapshot;
  126. const inWorktree = await build(value.worktree);
  127. const inSource = await build(value.source);
  128. expect(inWorktree?.workspaceIdentity).toBe(value.source);
  129. expect(inSource?.workspaceIdentity).toBe(value.source);
  130. expect(inWorktree?.bundleDigest).toBe(inSource?.bundleDigest);
  131. expect(inWorktree?.hooks.map((hook) => hook.hookDeclarationDigest)).toEqual(
  132. inSource?.hooks.map((hook) => hook.hookDeclarationDigest),
  133. );
  134. });
  135. it("未提供 Trust identity 时保持原规则:远程 identity 优先,否则为工作目录", async () => {
  136. const value = await managedWorktreeFixture();
  137. expect(
  138. createConfig({ env: value.env, skipUserConfig: true, workingDirectory: value.worktree })
  139. .sources.project.workspaceHookSnapshot?.workspaceIdentity,
  140. ).toBe(value.worktree);
  141. });
  142. });