queue-input-fidelity.test.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import { describe, expect, it } from "vitest";
  2. import {
  3. SessionEventType,
  4. type SessionEvent,
  5. type TurnInputIntentMetadata,
  6. } from "@zcode/contracts";
  7. import { queueItemSchema } from "@zcode/shared/zcode-protocol-v4";
  8. import { ProductProjection } from "../src/zcode-protocol-v4/index.js";
  9. import { inputIntentMetadataFromQueueItem } from "../src/zcode-protocol-v4/commands/input-intent.js";
  10. const intent: TurnInputIntentMetadata = {
  11. sourceCommandId: "source-command",
  12. queueItemId: "queued-input",
  13. clientId: "desktop-client",
  14. kind: "sendText",
  15. text: "继续执行",
  16. modelSelection: { providerId: "provider", modelId: "model", options: { reasoningLevel: "high" } },
  17. mode: "yolo",
  18. planEnabled: false,
  19. admissionSeq: 1,
  20. admittedAt: 1234,
  21. requestedDelivery: "queue",
  22. admittedDelivery: "queue",
  23. queuePosition: 0,
  24. attachmentRefs: [{ ref: "artifact://note", fileName: "note.txt", mime: "text/plain", bytes: 3 }],
  25. sharedContextRefs: [{ kind: "shared_context_import", context_id: "shared-context" }],
  26. genUiContext: [{ path: "/project/example.html", state: { stage: "entry" } }],
  27. provenance: {
  28. sourceCommandId: "original-command",
  29. queueItemId: "original-queue",
  30. clientId: "mobile-client",
  31. },
  32. };
  33. function queuedEvent(sequence: number, metadata?: TurnInputIntentMetadata): SessionEvent {
  34. return {
  35. id: `event-${sequence}`,
  36. sessionId: "session",
  37. turnId: "turn",
  38. type: SessionEventType.TurnSteerQueued,
  39. timestamp: new Date(1234 + sequence),
  40. traceId: "trace",
  41. sequenceNumber: sequence,
  42. payload: {
  43. pendingInputId: "queued-input",
  44. input: sequence === 1 ? intent.text : "修改正文",
  45. queueLength: 1,
  46. delivery: "queue",
  47. ...(metadata ? { intent: metadata } : {}),
  48. },
  49. } as SessionEvent;
  50. }
  51. describe("Todo151 Queue 提交事实往返", () => {
  52. it("旧尾块在 CLI 队列入口解码,模型快照和干净正文一起提升", () => {
  53. const projection = new ProductProjection("session", "epoch");
  54. const event = queuedEvent(1);
  55. const text = `继续执行\n\n<untrusted_gen_ui_state>\n${JSON.stringify(intent.genUiContext)}\n</untrusted_gen_ui_state>`;
  56. projection.applyEvent({ ...event, payload: { ...event.payload, input: text } } as SessionEvent);
  57. const item = queueItemSchema.parse(projection.getSnapshot().queue.items[0]);
  58. expect(item.text).toBe("继续执行");
  59. expect(inputIntentMetadataFromQueueItem(item, item.text).genUiContext).toEqual(
  60. intent.genUiContext,
  61. );
  62. });
  63. it.each(["queue", "guide"] as const)(
  64. "%s 投递经真实事件投影再提升不丢字段",
  65. (requestedDelivery) => {
  66. const projection = new ProductProjection("session", "epoch");
  67. const submitted = {
  68. ...intent,
  69. requestedDelivery,
  70. ...(requestedDelivery === "guide"
  71. ? { fallbackReasonCode: "guide.attachmentsUnsupported" }
  72. : {}),
  73. };
  74. projection.applyEvent(queuedEvent(1, submitted));
  75. const item = queueItemSchema.parse(projection.getSnapshot().queue.items[0]);
  76. expect(inputIntentMetadataFromQueueItem(item, item.text)).toEqual(submitted);
  77. },
  78. );
  79. it("只带正文的旧编辑事件保留同项原选择、上下文和来源", () => {
  80. const projection = new ProductProjection("session", "epoch");
  81. projection.applyEvent(queuedEvent(1, intent));
  82. projection.applyEvent(queuedEvent(2));
  83. const item = projection.getSnapshot().queue.items[0]!;
  84. expect(inputIntentMetadataFromQueueItem(item, item.text)).toEqual({
  85. ...intent,
  86. text: "修改正文",
  87. });
  88. });
  89. });