zcodeProtocolV4HookSummary.test.ts 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. expect(JSON.stringify(parsed)).not.toMatch(
  54. /commandDisplay|sourcePath|stdin|stdout|stderr|toolInput/u,
  55. );
  56. });
  57. it("rejects Desktop-only command and path details at the strict wire boundary", () => {
  58. const row = hookRow();
  59. expect(() =>
  60. hookInvocationRowSchema.parse({
  61. ...row,
  62. executions: [
  63. {
  64. ...row.executions[0],
  65. commandDisplay: "node hooks/check.mjs --token secret",
  66. sourcePath: "/workspace/.zcode/config.json",
  67. },
  68. ],
  69. }),
  70. ).toThrow();
  71. });
  72. it.each([DELIVERY_PROFILES.continuous, DELIVERY_PROFILES.replayable])(
  73. "keeps the same safe Hook row for $flushWindowMs ms profile",
  74. (profile) => {
  75. const row = hookRow();
  76. expect(filterConversationRowsForProfile([row], profile)).toEqual([row]);
  77. expect(
  78. filterConversationDeltasForProfile([{ op: "row.appended", row }], profile),
  79. ).toEqual([{ op: "row.appended", row }]);
  80. },
  81. );
  82. });