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

12345678910111213141516171819202122232425262728293031323334
  1. import { workspaceHookMatcherConfigSchema } from "@zcode/shared/workspace-hook-discovery";
  2. import { describe, expect, it } from "vitest";
  3. import { WORKSPACE_HOOK_SCHEMA_FIELDS } from "../../contracts/src/hooks/index.js";
  4. import {
  5. hookCommandSchema,
  6. hookMatcherSchema,
  7. hookProcessSchema,
  8. hooksSchema,
  9. } from "../src/config/schema.js";
  10. function schemaKeys(schema: { shape: Record<string, unknown> }): string[] {
  11. return Object.keys(schema.shape);
  12. }
  13. describe("workspace hook trust phase 0", () => {
  14. it("按相同规则读取用户和工作区的 Hook matcher", () => {
  15. const hooks = [{ type: "command", command: "echo test" }];
  16. for (const schema of [hookMatcherSchema, workspaceHookMatcherConfigSchema]) {
  17. expect(schema.parse({ hooks, matcher: "" }).matcher).toBeUndefined();
  18. expect(schema.parse({ hooks, matcher: " Bash " }).matcher).toBe(" Bash ");
  19. expect(schema.safeParse({ hooks, matcher: null }).success).toBe(false);
  20. }
  21. });
  22. it("真实 Hook file schema 与 canonical exhaustive lock 保持一致", () => {
  23. expect(schemaKeys(hooksSchema)).toEqual(WORKSPACE_HOOK_SCHEMA_FIELDS.root);
  24. expect(schemaKeys(hookMatcherSchema)).toEqual(WORKSPACE_HOOK_SCHEMA_FIELDS.matcher);
  25. expect(schemaKeys(hooksSchema.shape.events.unwrap())).toEqual(
  26. WORKSPACE_HOOK_SCHEMA_FIELDS.events,
  27. );
  28. expect(schemaKeys(hookProcessSchema)).toEqual(WORKSPACE_HOOK_SCHEMA_FIELDS.process);
  29. expect(schemaKeys(hookCommandSchema)).toEqual(WORKSPACE_HOOK_SCHEMA_FIELDS.command);
  30. });
  31. });