workspaceHookTrustStoreFile.test.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. import { describe, expect, it } from "vitest";
  2. import {
  3. WORKSPACE_HOOK_TRUST_STORE_SCHEMA_VERSION,
  4. parseWorkspaceHookTrustStoreContent,
  5. workspaceHookTrustStoreFileSchema,
  6. } from "../src/workspace-hook-trust-store-file.js";
  7. const VALID_DIGEST = "a".repeat(64);
  8. const OTHER_DIGEST = "b".repeat(64);
  9. function validRecord(overrides: Record<string, unknown> = {}): Record<string, unknown> {
  10. return {
  11. workspaceIdentity: "/repo/workspace-a",
  12. hookDeclarationDigest: VALID_DIGEST,
  13. digestAlgorithm: "sha256",
  14. decision: "trusted",
  15. grantedAt: "2026-08-17T09:00:00.000Z",
  16. eventAtGrant: "SessionStart",
  17. displayCommandAtGrant: "node recorder.mjs",
  18. sourcePathAtGrant: ".zcode/config.json",
  19. ...overrides,
  20. };
  21. }
  22. function validFile(records: unknown[]): Record<string, unknown> {
  23. return { schemaVersion: WORKSPACE_HOOK_TRUST_STORE_SCHEMA_VERSION, records };
  24. }
  25. describe("workspaceHookTrustStoreFileSchema", () => {
  26. it("接受合法完整记录", () => {
  27. const result = parseWorkspaceHookTrustStoreContent(
  28. JSON.stringify(validFile([validRecord(), validRecord({ workspaceIdentity: "/repo/other", hookDeclarationDigest: OTHER_DIGEST })])),
  29. );
  30. expect(result.status).toBe("ok");
  31. if (result.status !== "ok") return;
  32. expect(result.file.records).toHaveLength(2);
  33. expect(result.file.records[0]?.hookDeclarationDigest).toBe(VALID_DIGEST);
  34. });
  35. it("拒绝非法 JSON", () => {
  36. expect(parseWorkspaceHookTrustStoreContent("{ not json").status).toBe("invalid");
  37. });
  38. it("拒绝缺少 schemaVersion", () => {
  39. const { schemaVersion: _schemaVersion, ...rest } = validFile([validRecord()]);
  40. expect(workspaceHookTrustStoreFileSchema.safeParse(rest).success).toBe(false);
  41. });
  42. it("拒绝 records 非数组", () => {
  43. expect(workspaceHookTrustStoreFileSchema.safeParse({ ...validFile([]), records: "nope" }).success).toBe(false);
  44. });
  45. it("拒绝未知顶层字段", () => {
  46. expect(
  47. workspaceHookTrustStoreFileSchema.safeParse({ ...validFile([validRecord()]), extra: 1 }).success,
  48. ).toBe(false);
  49. });
  50. it("拒绝缺少必需字段的 record(grantedAt / digestAlgorithm / eventAtGrant / displayCommandAtGrant / sourcePathAtGrant)", () => {
  51. for (const key of [
  52. "grantedAt",
  53. "digestAlgorithm",
  54. "eventAtGrant",
  55. "displayCommandAtGrant",
  56. "sourcePathAtGrant",
  57. "decision",
  58. "workspaceIdentity",
  59. "hookDeclarationDigest",
  60. ]) {
  61. const { [key]: _removed, ...record } = validRecord();
  62. expect(workspaceHookTrustStoreFileSchema.safeParse(validFile([record])).success, key).toBe(false);
  63. }
  64. });
  65. it("拒绝非法 decision / digestAlgorithm", () => {
  66. expect(
  67. workspaceHookTrustStoreFileSchema.safeParse(validFile([validRecord({ decision: "maybe" })])).success,
  68. ).toBe(false);
  69. expect(
  70. workspaceHookTrustStoreFileSchema.safeParse(validFile([validRecord({ digestAlgorithm: "md5" })])).success,
  71. ).toBe(false);
  72. });
  73. it("拒绝非法 grantedAt 时间戳", () => {
  74. expect(
  75. workspaceHookTrustStoreFileSchema.safeParse(validFile([validRecord({ grantedAt: "yesterday" })])).success,
  76. ).toBe(false);
  77. });
  78. it("拒绝非 64 位 hex digest", () => {
  79. expect(
  80. workspaceHookTrustStoreFileSchema.safeParse(validFile([validRecord({ hookDeclarationDigest: "xyz" })])).success,
  81. ).toBe(false);
  82. });
  83. it("拒绝未知 record 字段", () => {
  84. expect(
  85. workspaceHookTrustStoreFileSchema.safeParse(validFile([validRecord({ surprise: true })])).success,
  86. ).toBe(false);
  87. });
  88. it("拒绝重复的 workspaceIdentity + digest key", () => {
  89. expect(
  90. workspaceHookTrustStoreFileSchema.safeParse(
  91. validFile([validRecord(), validRecord()]),
  92. ).success,
  93. ).toBe(false);
  94. });
  95. it("拒绝非法 eventAtGrant 事件名", () => {
  96. expect(
  97. workspaceHookTrustStoreFileSchema.safeParse(
  98. validFile([validRecord({ eventAtGrant: "OnShutdown" })]),
  99. ).success,
  100. ).toBe(false);
  101. });
  102. });