row-identity-materialization.test.ts 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import { describe, expect, it } from "vitest";
  2. import {
  3. SessionEventType,
  4. type EventId,
  5. type SessionEvent,
  6. type SessionId,
  7. type TraceId,
  8. type TurnId,
  9. } from "@zcode/contracts";
  10. import { conversationSnapshotSchema } from "@zcode/shared/zcode-protocol-v4";
  11. import { ProductProjection } from "../src/zcode-protocol-v4/product-projection.js";
  12. function event(
  13. sequenceNumber: number,
  14. type: SessionEvent["type"],
  15. payload: unknown,
  16. turnId = "runtime-turn-1",
  17. ): SessionEvent {
  18. return {
  19. id: `identity-event-${sequenceNumber}` as EventId,
  20. sessionId: "identity-session" as SessionId,
  21. turnId: turnId as TurnId,
  22. type,
  23. timestamp: new Date(1_700_000_000_000 + sequenceNumber),
  24. traceId: "identity-trace" as TraceId,
  25. sequenceNumber,
  26. payload,
  27. };
  28. }
  29. describe("row identity materialization", () => {
  30. it("row 与 command target 使用同一 canonical entity,并显式下发 productTurn/visibility", () => {
  31. const projection = new ProductProjection("identity-session", "identity-epoch");
  32. const events = [
  33. event(1, SessionEventType.SessionCreated, { mode: "default", contextWindow: 200_000 }),
  34. event(2, SessionEventType.TurnStarted, {
  35. turnNumber: 1,
  36. input: "identity query",
  37. messageId: "user-message-1",
  38. }),
  39. event(3, SessionEventType.ModelStreaming, {
  40. kind: "text_start",
  41. delta: "",
  42. done: false,
  43. assistantMessageId: "assistant-message-1",
  44. partId: "assistant-part-1",
  45. }),
  46. event(4, SessionEventType.ModelStreaming, {
  47. kind: "text_delta",
  48. delta: "identity answer",
  49. done: false,
  50. partId: "assistant-part-1",
  51. }),
  52. event(5, SessionEventType.ModelStreaming, {
  53. kind: "text_end",
  54. delta: "",
  55. done: false,
  56. partId: "assistant-part-1",
  57. }),
  58. event(6, SessionEventType.TurnComplete, {
  59. response: "identity answer",
  60. tokenCount: 1,
  61. toolCallCount: 0,
  62. duration: 1,
  63. resultType: "success",
  64. }),
  65. ];
  66. for (const item of events) projection.applyEvent(item);
  67. const snapshot = projection.getSnapshot();
  68. expect(() => conversationSnapshotSchema.parse(snapshot)).not.toThrow();
  69. for (const row of snapshot.rows.window) {
  70. expect(row).toMatchObject({
  71. entityId: expect.any(String),
  72. productTurnId: row.turnId,
  73. visibility: "visible",
  74. });
  75. expect(projection.getEntityIdForRow(row.rowId)).toBe(
  76. (row as unknown as { entityId: string }).entityId,
  77. );
  78. }
  79. const user = snapshot.rows.window.find((row) => row.kind === "userInput");
  80. const assistant = snapshot.rows.window.find((row) => row.kind === "assistantText");
  81. expect(user).toMatchObject({
  82. entityId: "user-message-1",
  83. productTurnId: "user-message-1",
  84. visibility: "visible",
  85. });
  86. expect(assistant).toMatchObject({
  87. entityId: "assistant-message-1",
  88. productTurnId: "user-message-1",
  89. visibility: "visible",
  90. });
  91. expect(projection.getMessageIdForRow(user!.rowId)).toBe(
  92. (user as unknown as { entityId: string }).entityId,
  93. );
  94. });
  95. });