workspace-hook-trust-phase0.test.ts 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. import { mkdir, mkdtemp, rm, writeFile } from "node:fs/promises";
  2. import { tmpdir } from "node:os";
  3. import { join } from "node:path";
  4. import { describe, expect, it } from "vitest";
  5. import { WORKSPACE_HOOK_SCHEMA_FIELDS } from "../../contracts/src/hooks/index.js";
  6. import { createConfig } from "../src/config/index.js";
  7. import {
  8. hookCommandSchema,
  9. hookMatcherSchema,
  10. hookProcessSchema,
  11. hooksSchema,
  12. } from "../src/config/schema.js";
  13. function schemaKeys(schema: { shape: Record<string, unknown> }): string[] {
  14. return Object.keys(schema.shape);
  15. }
  16. describe("workspace hook trust phase 0", () => {
  17. it("真实 Hook file schema 与 canonical exhaustive lock 保持一致", () => {
  18. expect(schemaKeys(hooksSchema)).toEqual(WORKSPACE_HOOK_SCHEMA_FIELDS.root);
  19. expect(schemaKeys(hookMatcherSchema)).toEqual(WORKSPACE_HOOK_SCHEMA_FIELDS.matcher);
  20. expect(schemaKeys(hooksSchema.shape.events.unwrap())).toEqual(
  21. WORKSPACE_HOOK_SCHEMA_FIELDS.events,
  22. );
  23. expect(schemaKeys(hookProcessSchema)).toEqual(WORKSPACE_HOOK_SCHEMA_FIELDS.process);
  24. expect(schemaKeys(hookCommandSchema)).toEqual(WORKSPACE_HOOK_SCHEMA_FIELDS.command);
  25. });
  26. it("startup/resume/async project hooks 在 admission 实现前继续 hard block", async () => {
  27. const root = await mkdtemp(join(tmpdir(), "zcode-workspace-hook-hard-block-"));
  28. try {
  29. await mkdir(join(root, ".git"));
  30. await mkdir(join(root, ".zcode"), { recursive: true });
  31. await writeFile(
  32. join(root, ".zcode", "config.json"),
  33. JSON.stringify({
  34. hooks: {
  35. enabled: true,
  36. events: {
  37. SessionStart: [
  38. {
  39. matcher: "startup",
  40. hooks: [{ type: "command", command: "echo startup-project-hook" }],
  41. },
  42. {
  43. matcher: "resume",
  44. hooks: [
  45. {
  46. type: "command",
  47. command: "echo resume-project-hook",
  48. async: true,
  49. },
  50. ],
  51. },
  52. ],
  53. },
  54. },
  55. }),
  56. );
  57. const result = createConfig({ env: {}, skipUserConfig: true, workingDirectory: root });
  58. expect(result.sources.project.diagnostics).toContainEqual(
  59. expect.objectContaining({ code: "config_project_hooks_pending_trust", path: "hooks" }),
  60. );
  61. expect(result.config.hooks.enabled).toBe(false);
  62. expect(result.config.hooks.events.SessionStart).toBeUndefined();
  63. } finally {
  64. await rm(root, { force: true, recursive: true });
  65. }
  66. });
  67. });