hook-trust-worktree-fixture.ts 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import { execFile } from "node:child_process";
  2. import { mkdir, writeFile } from "node:fs/promises";
  3. import { join } from "node:path";
  4. import { promisify } from "node:util";
  5. import { DEFAULT_WORKSPACE } from "./desktop-app.js";
  6. /**
  7. * 受管 worktree 的 Workspace Hook Trust 审核入口 fixture(HK20,ZP-20261110-06)。
  8. *
  9. * 源仓库提交 v1 入口脚本,再在工作区改成未提交的 v2:源仓库发现 v2,worktree 检出 v1,
  10. * 两边的 declaration digest 不同(D23、D24)。与 hook-trust-fixture 相同,必须在首个 agent
  11. * 进程冷启动前落盘(wdio.conf.ts resetE2EHome),否则源仓库任务的 SessionStart 软门禁不生效。
  12. */
  13. export const HOOK_TRUST_WORKTREE_ENTRY_LABEL = "hook-trust-worktree-entry";
  14. const execGit = promisify(execFile);
  15. const ZCODE_DIR = join(DEFAULT_WORKSPACE, ".zcode");
  16. const CONFIG_RELATIVE_PATH = join(".zcode", "config.json");
  17. // 相对路径的 node 入口会被识别为直接入口脚本,内容参与 digest(D24)。
  18. const SCRIPT_RELATIVE_PATH = ".zcode/hook-trust-worktree.mjs";
  19. async function git(...args: string[]): Promise<void> {
  20. await execGit("git", args, { cwd: DEFAULT_WORKSPACE });
  21. }
  22. export async function installHookTrustWorktreeFixture(): Promise<void> {
  23. await mkdir(ZCODE_DIR, { recursive: true });
  24. await writeFile(
  25. join(DEFAULT_WORKSPACE, CONFIG_RELATIVE_PATH),
  26. `${JSON.stringify(
  27. {
  28. hooks: {
  29. enabled: true,
  30. events: {
  31. SessionStart: [
  32. {
  33. matcher: "startup",
  34. hooks: [
  35. {
  36. type: "process",
  37. command: "node",
  38. args: [SCRIPT_RELATIVE_PATH, HOOK_TRUST_WORKTREE_ENTRY_LABEL],
  39. enabled: true,
  40. timeoutMs: 10000,
  41. },
  42. ],
  43. },
  44. ],
  45. },
  46. },
  47. },
  48. null,
  49. 2,
  50. )}\n`,
  51. "utf8",
  52. );
  53. const scriptPath = join(DEFAULT_WORKSPACE, SCRIPT_RELATIVE_PATH);
  54. await writeFile(scriptPath, "// hook-trust-worktree v1\n", "utf8");
  55. await git("init", "-q", "-b", "main");
  56. await git("config", "user.email", "zcode-e2e@example.invalid");
  57. await git("config", "user.name", "ZCode E2E");
  58. await git("add", CONFIG_RELATIVE_PATH, SCRIPT_RELATIVE_PATH);
  59. await git("commit", "-qm", "test: workspace hook v1");
  60. // 未提交的 v2 只存在于源仓库;新建的 worktree 检出已提交的 v1。
  61. await writeFile(scriptPath, "// hook-trust-worktree v2\n", "utf8");
  62. }