hooks-trust-command.test.ts 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. import assert from "node:assert/strict";
  2. import test from "node:test";
  3. import type { WorkspaceHookTrustCliStatus } from "@zcode/bootstrap";
  4. import type { RunContext } from "@zcode/shared-types";
  5. import { run } from "../src/run.js";
  6. type CapturedWriteStream = NodeJS.WriteStream & { output(): string };
  7. function stream(): CapturedWriteStream {
  8. let output = "";
  9. return {
  10. output: () => output,
  11. write(chunk: string | Uint8Array): boolean {
  12. output += typeof chunk === "string" ? chunk : chunk.toString();
  13. return true;
  14. },
  15. } as CapturedWriteStream;
  16. }
  17. function context(argv: string[]): RunContext & {
  18. stdout: CapturedWriteStream;
  19. stderr: CapturedWriteStream;
  20. } {
  21. return {
  22. argv,
  23. stdin: { isTTY: false } as NodeJS.ReadStream,
  24. stdout: stream(),
  25. stderr: stream(),
  26. };
  27. }
  28. function status(overrides: Partial<WorkspaceHookTrustCliStatus> = {}): WorkspaceHookTrustCliStatus {
  29. return {
  30. workspacePath: "/repo",
  31. workspaceIdentity: "/repo",
  32. bundleDigest: "b".repeat(64),
  33. reasonCode: "workspace_hooks_pending_trust",
  34. items: [
  35. {
  36. reviewItemId: "item-1",
  37. event: "SessionStart",
  38. matcher: "startup",
  39. displayCommand: "./start.sh",
  40. sourcePath: ".zcode/config.json",
  41. configuredEnabled: true,
  42. hookDeclarationDigest: "a".repeat(64),
  43. trustState: "pending_trust",
  44. },
  45. ],
  46. ...overrides,
  47. };
  48. }
  49. test("hooks trust status emits stable JSON and resolves a local workspace path", async () => {
  50. const ctx = context(["hooks", "trust", "status", "--workspace", "./repo", "--json"]);
  51. const code = await run(ctx, {
  52. cwd: () => "/work",
  53. inspectWorkspaceHookTrust: async (target) => {
  54. assert.equal(target.workspacePath, "/work/repo");
  55. assert.equal(target.workspaceIdentity, undefined);
  56. return status({ workspacePath: "/work/repo", workspaceIdentity: "/work/repo" });
  57. },
  58. });
  59. assert.equal(code, 0);
  60. assert.equal(JSON.parse(ctx.stdout.output()).reasonCode, "workspace_hooks_pending_trust");
  61. assert.equal(ctx.stderr.output(), "");
  62. });
  63. test("hooks trust grant binds all-current to the exact bundle digest", async () => {
  64. const bundleDigest = "b".repeat(64);
  65. const ctx = context([
  66. "hooks",
  67. "trust",
  68. "grant",
  69. "--workspace",
  70. "/repo",
  71. "--all-current",
  72. "--bundle-digest",
  73. bundleDigest,
  74. ]);
  75. const code = await run(ctx, {
  76. grantWorkspaceHookTrust: async (input) => {
  77. assert.equal(input.workspacePath, "/repo");
  78. assert.equal(input.allCurrent, true);
  79. assert.equal(input.bundleDigest, bundleDigest);
  80. return status({ reasonCode: "workspace_hooks_trusted_persistent" });
  81. },
  82. });
  83. assert.equal(code, 0);
  84. assert.match(ctx.stdout.output(), /workspace_hooks_trusted_persistent/);
  85. });
  86. test("hooks trust status treats remote identity as identity and inspects the current host path", async () => {
  87. const ctx = context([
  88. "hooks",
  89. "trust",
  90. "status",
  91. "--workspace",
  92. "remote:ssh:host:/repo",
  93. "--json",
  94. ]);
  95. const code = await run(ctx, {
  96. cwd: () => "/mounted/repo",
  97. inspectWorkspaceHookTrust: async (target) => {
  98. assert.equal(target.workspacePath, "/mounted/repo");
  99. assert.equal(target.workspaceIdentity, "remote:ssh:host:/repo");
  100. return status({
  101. workspacePath: "/mounted/repo",
  102. workspaceIdentity: "remote:ssh:host:/repo",
  103. });
  104. },
  105. });
  106. assert.equal(code, 0);
  107. });
  108. test("hooks trust failures use structured reasonCode in JSON mode", async () => {
  109. const ctx = context([
  110. "hooks",
  111. "trust",
  112. "grant",
  113. "--workspace",
  114. "/repo",
  115. "--all-current",
  116. "--bundle-digest",
  117. "c".repeat(64),
  118. "--json",
  119. ]);
  120. const code = await run(ctx, {
  121. grantWorkspaceHookTrust: async () => {
  122. throw new Error("workspace_hooks_bundle_changed");
  123. },
  124. });
  125. assert.equal(code, 1);
  126. assert.deepEqual(JSON.parse(ctx.stdout.output()), {
  127. accepted: false,
  128. reasonCode: "workspace_hooks_bundle_changed",
  129. });
  130. assert.equal(ctx.stderr.output(), "");
  131. });
  132. test("hooks trust status 在受管 worktree 中提示命令回填当前路径,而不是源仓库 Trust identity", async () => {
  133. const ctx = context([
  134. "hooks",
  135. "trust",
  136. "status",
  137. "--workspace",
  138. "/data/.zcode/worktrees/3f2a/repo",
  139. ]);
  140. const code = await run(ctx, {
  141. inspectWorkspaceHookTrust: async () =>
  142. status({
  143. workspacePath: "/data/.zcode/worktrees/3f2a/repo",
  144. workspaceIdentity: "/projects/repo",
  145. }),
  146. });
  147. assert.equal(code, 0);
  148. const output = ctx.stdout.output();
  149. assert.match(output, /workspace: \/projects\/repo/);
  150. assert.match(output, /grant --workspace "?\/data\/\.zcode\/worktrees\/3f2a\/repo"?/);
  151. assert.doesNotMatch(output, /grant --workspace "?\/projects\/repo/);
  152. });
  153. // 第二轮 CR SG-02:--all-current 因入口脚本不可用被拒时须列出条目;含此类条目时 status 不再推荐 --all-current。
  154. test("hooks trust grant 被入口脚本不可用拒绝时列出被拒条目", async () => {
  155. const ctx = context(["hooks", "trust", "grant", "--workspace", "/repo", "--all-current"]);
  156. const code = await run(ctx, {
  157. cwd: () => "/work",
  158. grantWorkspaceHookTrust: async () => {
  159. throw Object.assign(new Error("workspace_hook_script_unavailable"), {
  160. unavailableItems: ["Stop ./missing.sh (.zcode/config.json)"],
  161. });
  162. },
  163. });
  164. assert.equal(code, 1);
  165. assert.match(ctx.stderr.output(), /Error: workspace_hook_script_unavailable/);
  166. assert.match(ctx.stderr.output(), /Stop \.\/missing\.sh \(\.zcode\/config\.json\)/);
  167. });
  168. test("hooks trust status 含入口不可用的启用条目时不推荐 --all-current", async () => {
  169. const ctx = context(["hooks", "trust", "status", "--workspace", "/repo"]);
  170. const base = status();
  171. const code = await run(ctx, {
  172. cwd: () => "/work",
  173. inspectWorkspaceHookTrust: async () =>
  174. status({
  175. items: [
  176. ...base.items,
  177. { ...base.items[0]!, reviewItemId: "item-2", trustState: "blocked_untrusted" },
  178. ],
  179. }),
  180. });
  181. assert.equal(code, 0);
  182. assert.match(ctx.stdout.output(), /--hook-digest <sha256>/);
  183. assert.doesNotMatch(ctx.stdout.output(), /--all-current/);
  184. });
  185. test("hooks trust grant --json 被入口脚本不可用拒绝时输出被拒条目", async () => {
  186. const ctx = context(["hooks", "trust", "grant", "--workspace", "/repo", "--all-current", "--json"]);
  187. const code = await run(ctx, {
  188. cwd: () => "/work",
  189. grantWorkspaceHookTrust: async () => {
  190. throw Object.assign(new Error("workspace_hook_script_unavailable"), {
  191. unavailableItems: ["Stop ./missing.sh (.zcode/config.json)"],
  192. });
  193. },
  194. });
  195. assert.equal(code, 1);
  196. assert.deepEqual(JSON.parse(ctx.stdout.output()), {
  197. accepted: false,
  198. reasonCode: "workspace_hook_script_unavailable",
  199. unavailableItems: ["Stop ./missing.sh (.zcode/config.json)"],
  200. });
  201. });