queue-input-fidelity.test.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. provenance: {
  27. sourceCommandId: "original-command",
  28. queueItemId: "original-queue",
  29. clientId: "mobile-client",
  30. },
  31. };
  32. function queuedEvent(sequence: number, metadata?: TurnInputIntentMetadata): SessionEvent {
  33. return {
  34. id: `event-${sequence}`,
  35. sessionId: "session",
  36. turnId: "turn",
  37. type: SessionEventType.TurnSteerQueued,
  38. timestamp: new Date(1234 + sequence),
  39. traceId: "trace",
  40. sequenceNumber: sequence,
  41. payload: {
  42. pendingInputId: "queued-input",
  43. input: sequence === 1 ? intent.text : "修改正文",
  44. queueLength: 1,
  45. delivery: "queue",
  46. ...(metadata ? { intent: metadata } : {}),
  47. },
  48. } as SessionEvent;
  49. }
  50. describe("Todo151 Queue 提交事实往返", () => {
  51. it.each(["queue", "guide"] as const)(
  52. "%s 投递经真实事件投影再提升不丢字段",
  53. (requestedDelivery) => {
  54. const projection = new ProductProjection("session", "epoch");
  55. const submitted = {
  56. ...intent,
  57. requestedDelivery,
  58. ...(requestedDelivery === "guide"
  59. ? { fallbackReasonCode: "guide.attachmentsUnsupported" }
  60. : {}),
  61. };
  62. projection.applyEvent(queuedEvent(1, submitted));
  63. const item = queueItemSchema.parse(projection.getSnapshot().queue.items[0]);
  64. expect(inputIntentMetadataFromQueueItem(item, item.text)).toEqual(submitted);
  65. },
  66. );
  67. it("只带正文的旧编辑事件保留同项原选择、上下文和来源", () => {
  68. const projection = new ProductProjection("session", "epoch");
  69. projection.applyEvent(queuedEvent(1, intent));
  70. projection.applyEvent(queuedEvent(2));
  71. const item = projection.getSnapshot().queue.items[0]!;
  72. expect(inputIntentMetadataFromQueueItem(item, item.text)).toEqual({
  73. ...intent,
  74. text: "修改正文",
  75. });
  76. });
  77. });