conversation-telemetry-identity.test.ts 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. import { describe, expect, it } from "vitest";
  2. import { SessionEventType, type SessionEvent } from "@zcode/contracts";
  3. import { uuidv7 } from "@zcode/shared";
  4. import { ConversationTelemetryFactNormalizer } from "../src/zcode-protocol-v4/conversation-telemetry-facts.js";
  5. function event(type: SessionEvent["type"], payload: unknown, seq: number): SessionEvent {
  6. return {
  7. id: `event-${seq}`,
  8. sessionId: "parent",
  9. turnId: "turn",
  10. traceId: "trace",
  11. sequenceNumber: seq,
  12. timestamp: new Date(),
  13. type,
  14. payload,
  15. } as SessionEvent;
  16. }
  17. describe("main turn telemetry identity", () => {
  18. it.each([undefined, "background_task"])("keeps inputId through %s turn facts", (inputSource) => {
  19. const normalizer = new ConversationTelemetryFactNormalizer();
  20. const inputId = uuidv7();
  21. expect(
  22. normalizer.normalize(
  23. "parent",
  24. event(
  25. SessionEventType.TurnStarted,
  26. {
  27. turnNumber: 1,
  28. input: "prompt",
  29. inputId,
  30. inputSource,
  31. messageId: "msg_persisted",
  32. },
  33. 1,
  34. ),
  35. ),
  36. ).toMatchObject({ sourceCommandId: inputId, kind: "turn.started" });
  37. expect(
  38. normalizer.normalize(
  39. "parent",
  40. event(
  41. SessionEventType.TurnComplete,
  42. {
  43. turnNumber: 1,
  44. result: { type: "complete" },
  45. duration: 10,
  46. },
  47. 2,
  48. ),
  49. ),
  50. ).toMatchObject({ sourceCommandId: inputId, kind: "turn.terminal" });
  51. });
  52. it("does not substitute a persisted message ID for a missing input ID", () => {
  53. const normalizer = new ConversationTelemetryFactNormalizer();
  54. const fact = normalizer.normalize(
  55. "parent",
  56. event(
  57. SessionEventType.TurnStarted,
  58. {
  59. turnNumber: 1,
  60. input: "notice",
  61. inputSource: "background_task",
  62. messageId: "msg_persisted",
  63. },
  64. 1,
  65. ),
  66. );
  67. expect(fact).not.toHaveProperty("sourceCommandId");
  68. });
  69. });