session-mapper-turn-payload.test.ts 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. // Legacy v3 边界:TurnStarted / TurnSteerQueued 的 payload 原样透传,strict schema 漏声明任一
  2. // CLI 字段都会让 Host 丢弃整条事件(Bot 收不到 task_run_started,上一轮正文串进下一轮)。
  3. import { describe, expect, it } from "vitest";
  4. import {
  5. SessionEventType,
  6. type EventId,
  7. type SessionEvent,
  8. type SessionId,
  9. type TraceId,
  10. type TurnId,
  11. } from "@zcode/contracts";
  12. import { zcodeSessionEventSchema } from "@zcode/shared";
  13. import { mapSessionEvent } from "../src/zcode-protocol/session-mapper.js";
  14. function turnEvent(type: SessionEventType, payload: Record<string, unknown>): SessionEvent {
  15. return {
  16. id: `legacy-${type}` as EventId,
  17. sessionId: "legacy-session" as SessionId,
  18. turnId: "turn-1" as TurnId,
  19. type,
  20. timestamp: new Date(1_700_000_000_000),
  21. traceId: "legacy-trace" as TraceId,
  22. sequenceNumber: 1,
  23. payload,
  24. };
  25. }
  26. describe("legacy turn payload 映射", () => {
  27. it("turn.started 保留 inputOrigin、workflowLaunch 与 epilogueStart", () => {
  28. const mapped = mapSessionEvent(
  29. turnEvent(SessionEventType.TurnStarted, {
  30. turnNumber: 1,
  31. input: "hello",
  32. inputId: "input-1",
  33. inputOrigin: "mobile",
  34. epilogueStart: 3,
  35. workflowLaunch: { runId: "run-1", toolCallId: "launch-1", name: "Daily" },
  36. }),
  37. );
  38. expect(zcodeSessionEventSchema.safeParse(mapped).error?.issues ?? []).toEqual([]);
  39. });
  40. it("turn.steerQueued 保留 inputPresentation 与 automationId", () => {
  41. const mapped = mapSessionEvent(
  42. turnEvent(SessionEventType.TurnSteerQueued, {
  43. pendingInputId: "pending-1",
  44. input: "next",
  45. inputPreview: "next",
  46. inputSize: 4,
  47. inputPresentation: "user_steer",
  48. automationId: "automation-1",
  49. targetTurnId: "turn-1",
  50. queueLength: 1,
  51. }),
  52. );
  53. expect(zcodeSessionEventSchema.safeParse(mapped).error?.issues ?? []).toEqual([]);
  54. });
  55. });