hooks-trust-command.test.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. });