zcodeProtocolV4HookSummary.test.ts 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import { describe, expect, it } from "vitest";
  2. import {
  3. DELIVERY_PROFILES,
  4. filterConversationDeltasForProfile,
  5. filterConversationRowsForProfile,
  6. hookInvocationRowSchema,
  7. type HookInvocationRow,
  8. } from "../src/zcode-protocol-v4/index.js";
  9. function hookRow(): HookInvocationRow {
  10. return {
  11. rowId: 3,
  12. turnId: "turn-1",
  13. entityId: "hook-invocation-1",
  14. productTurnId: "turn-1",
  15. visibility: "visible",
  16. createdAt: 1_700_000_000_000,
  17. createdAtSeq: 3,
  18. kind: "hookInvocation",
  19. hookInvocationId: "hook-invocation-1",
  20. hookEventName: "PreToolUse",
  21. hookCount: 1,
  22. state: "completed",
  23. startedAt: 1_700_000_000_000,
  24. endedAt: 1_700_000_000_025,
  25. durationMs: 25,
  26. lane: "toolBefore",
  27. anchorToolCallId: "tool-1",
  28. executions: [
  29. {
  30. hookRunId: "hook-run-1",
  31. hookIndex: 0,
  32. didExecute: true,
  33. state: "completed",
  34. outcome: "success",
  35. startedAt: 1_700_000_000_000,
  36. endedAt: 1_700_000_000_025,
  37. durationMs: 25,
  38. displayName: "validate-write",
  39. sourceKind: "project",
  40. toolName: "Write",
  41. },
  42. ],
  43. };
  44. }
  45. describe("V4 Hook turn summary schema", () => {
  46. it("accepts only the client-safe execution summary", () => {
  47. const parsed = hookInvocationRowSchema.parse(hookRow());
  48. expect(parsed.executions[0]).toMatchObject({
  49. didExecute: true,
  50. displayName: "validate-write",
  51. sourceKind: "project",
  52. });
  53. });
  54. it("rejects Desktop-only command and path details at the strict wire boundary", () => {
  55. const row = hookRow();
  56. expect(() =>
  57. hookInvocationRowSchema.parse({
  58. ...row,
  59. executions: [
  60. {
  61. ...row.executions[0],
  62. commandDisplay: "node hooks/check.mjs --token secret",
  63. sourcePath: "/workspace/.zcode/config.json",
  64. },
  65. ],
  66. }),
  67. ).toThrow();
  68. });
  69. it.each([DELIVERY_PROFILES.continuous, DELIVERY_PROFILES.replayable])(
  70. "keeps the same safe Hook row for $flushWindowMs ms profile",
  71. (profile) => {
  72. const row = hookRow();
  73. expect(filterConversationRowsForProfile([row], profile)).toEqual([row]);
  74. expect(filterConversationDeltasForProfile([{ op: "row.appended", row }], profile)).toEqual([
  75. { op: "row.appended", row },
  76. ]);
  77. },
  78. );
  79. });