guarded-projection.test.ts 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. import { describe, expect, it } from "vitest";
  2. import {
  3. createSessionId,
  4. createRootTraceContext,
  5. SessionEventType,
  6. type SessionEvent,
  7. } from "@zcode/contracts";
  8. import {
  9. DELIVERY_PROFILES,
  10. filterConversationDeltasForProfile,
  11. conversationSnapshotSchema,
  12. } from "@zcode/shared/zcode-protocol-v4";
  13. import { ProductProjection } from "../src/zcode-protocol-v4/index.js";
  14. describe("guarded continuous/replayable share authoritative pending approval", () => {
  15. it("keeps once-only options and feedback through both delivery profiles and replay", () => {
  16. const sessionId = createSessionId("guarded-projection");
  17. const trace = createRootTraceContext({ sessionId });
  18. const event = {
  19. id: "guarded-permission-event",
  20. sessionId,
  21. traceId: trace.traceId,
  22. sequenceNumber: 1,
  23. timestamp: new Date(),
  24. type: SessionEventType.PermissionRequested,
  25. payload: {
  26. approvalMode: "user-once",
  27. fullAccessSupported: true,
  28. requestId: "guarded-request",
  29. toolCallId: "bash",
  30. toolName: "Bash",
  31. riskLevel: "high",
  32. reason: "Dangerous command",
  33. input: { command: "rm -rf fixture" },
  34. },
  35. } as SessionEvent;
  36. const live = new ProductProjection(sessionId, "epoch");
  37. const deltas = live.applyEvent(event);
  38. for (const profile of [DELIVERY_PROFILES.continuous, DELIVERY_PROFILES.replayable]) {
  39. expect(filterConversationDeltasForProfile(deltas, profile)).toEqual(deltas);
  40. }
  41. const restored = new ProductProjection(sessionId, "epoch");
  42. restored.applyEvent(event);
  43. const current = conversationSnapshotSchema.parse(live.getSnapshot());
  44. const replayed = conversationSnapshotSchema.parse(restored.getSnapshot());
  45. expect(replayed.pendingInteractions).toEqual(current.pendingInteractions);
  46. expect(current.pendingInteractions[0]?.payload).toMatchObject({
  47. kind: "permission",
  48. freeText: true,
  49. });
  50. const payload = current.pendingInteractions[0]!.payload;
  51. if (payload.kind !== "permission") throw new Error("Expected permission");
  52. expect(payload.options.map((option) => option.kind)).toEqual(["allowOnce", "deny"]);
  53. expect(payload.fullAccessOption).toBeUndefined();
  54. expect(payload.modeSwitch).toBe("fullAccessOnly");
  55. live.applyEvent({
  56. ...event,
  57. id: "resolved" as SessionEvent["id"],
  58. sequenceNumber: 2,
  59. type: SessionEventType.PermissionResolved,
  60. payload: { requestId: "guarded-request", toolCallId: "bash", decision: "deny" },
  61. });
  62. expect(live.getSnapshot().pendingInteractions).toEqual([]);
  63. });
  64. });