zcodeProtocolWorkspaceHookTrust.test.ts 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import { describe, expect, it } from "vitest";
  2. import {
  3. zcodeProtocolMethods,
  4. zcodeWorkspaceHookTrustGrantParamsSchema,
  5. zcodeWorkspaceHookTrustGrantResultSchema,
  6. } from "../src/zcode-protocol/index.js";
  7. describe("workspace Hook Trust workspace protocol", () => {
  8. it("declares an exact workspace/bundle/declaration grant contract", () => {
  9. expect(zcodeProtocolMethods.workspaceHookTrustGrant).toBe("workspace/hooks/trustGrant");
  10. expect(
  11. zcodeWorkspaceHookTrustGrantParamsSchema.parse({
  12. workspace: {
  13. workspacePath: "/repo",
  14. workspaceIdentity: "remote:ssh:host:/repo",
  15. workspaceKey: "remote:ssh:host:/repo",
  16. },
  17. bundleDigest: "b".repeat(64),
  18. hookDeclarationDigest: "a".repeat(64),
  19. }),
  20. ).toEqual(
  21. expect.objectContaining({
  22. bundleDigest: "b".repeat(64),
  23. hookDeclarationDigest: "a".repeat(64),
  24. }),
  25. );
  26. expect(
  27. zcodeWorkspaceHookTrustGrantResultSchema.parse({
  28. accepted: false,
  29. reasonCode: "workspace_hooks_config_unreadable",
  30. }),
  31. ).toEqual({ accepted: false, reasonCode: "workspace_hooks_config_unreadable" });
  32. });
  33. it("rejects malformed digests and undeclared fields", () => {
  34. expect(() =>
  35. zcodeWorkspaceHookTrustGrantParamsSchema.parse({
  36. workspace: { workspacePath: "/repo", workspaceKey: "/repo" },
  37. bundleDigest: "not-a-digest",
  38. hookDeclarationDigest: "a".repeat(64),
  39. }),
  40. ).toThrow();
  41. expect(() =>
  42. zcodeWorkspaceHookTrustGrantResultSchema.parse({ accepted: true, bypassed: true }),
  43. ).toThrow();
  44. expect(() =>
  45. zcodeWorkspaceHookTrustGrantResultSchema.parse({
  46. accepted: false,
  47. reasonCode: "/Users/alice/private/config.json",
  48. }),
  49. ).toThrow();
  50. });
  51. });