v4PendingGuideProjection.test.ts 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. import { describe, expect, it } from "vitest";
  2. import type { QueueItem, QueueState } from "@zcode/shared/zcode-protocol-v4";
  3. import { projectPendingGuideQueue } from "@/v4/pendingGuideProjection.js";
  4. function queueItem(
  5. queueItemId: string,
  6. admitted: "queue" | "guide",
  7. steerState: QueueItem["steer"]["state"] = admitted === "guide"
  8. ? "steering"
  9. : "notRequested",
  10. ): QueueItem {
  11. return {
  12. sourceCommandId: `command-${queueItemId}`,
  13. queueItemId,
  14. clientId: "desktop",
  15. kind: "sendText",
  16. text: `text-${queueItemId}`,
  17. attachments: [],
  18. delivery: { requested: admitted, admitted },
  19. order: {
  20. admissionSeq: Number(queueItemId.replace(/\D/gu, "")) || 0,
  21. queuePosition: 0,
  22. },
  23. steer: { state: steerState },
  24. dispatch: { state: "queued" },
  25. admittedAt: 100,
  26. };
  27. }
  28. describe("projectPendingGuideQueue", () => {
  29. it("把等待注入的 guide 投影到对话流,并从普通队列中排除", () => {
  30. const queue: QueueState = {
  31. autoDrain: true,
  32. items: [queueItem("1", "queue"), queueItem("2", "guide")],
  33. };
  34. const projection = projectPendingGuideQueue(queue);
  35. expect(projection.pendingGuides.map((item) => item.queueItemId)).toEqual([
  36. "2",
  37. ]);
  38. expect(
  39. projection.visibleQueue.items.map((item) => item.queueItemId),
  40. ).toEqual(["1"]);
  41. });
  42. it("guide 降级为 queue 后只回到普通队列", () => {
  43. const fallback = queueItem("2", "queue", "fellBack");
  44. fallback.delivery = {
  45. requested: "guide",
  46. admitted: "queue",
  47. fallbackReasonCode: "guide.queueNotEmpty",
  48. };
  49. const projection = projectPendingGuideQueue({
  50. autoDrain: true,
  51. items: [fallback],
  52. });
  53. expect(projection.pendingGuides).toEqual([]);
  54. expect(projection.visibleQueue.items).toEqual([fallback]);
  55. });
  56. it("保持 guide 的 admission FIFO 顺序", () => {
  57. const projection = projectPendingGuideQueue({
  58. autoDrain: true,
  59. items: [queueItem("3", "guide"), queueItem("4", "guide")],
  60. });
  61. expect(
  62. projection.pendingGuides.map((item) => item.sourceCommandId),
  63. ).toEqual(["command-3", "command-4"]);
  64. });
  65. });