runtime-queue-auto-drain.test.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. import { describe, expect, it, vi } from "vitest";
  2. import {
  3. completeExternalQueueDrain,
  4. drainPendingInput,
  5. hasInlineGuidePendingInput,
  6. } from "../src/runtime/methods/steering.js";
  7. describe("guide 行内 drain 的外层 FIFO 门", () => {
  8. it("外层普通队列恢复期间阻止 guide 越过旧项,消费到空后恢复 tool-batch drain", () => {
  9. const activeTurn = {
  10. pendingInputs: [{ id: "new-running-input", commandKind: undefined, delivery: "guide" }],
  11. };
  12. const runtime = {
  13. activeTurn,
  14. pendingInputReservations: new Map<string, string>(),
  15. queueExternalDrainActive: true,
  16. };
  17. expect(hasInlineGuidePendingInput.call(runtime as never, activeTurn as never)).toBe(false);
  18. completeExternalQueueDrain.call(runtime as never);
  19. expect(hasInlineGuidePendingInput.call(runtime as never, activeTurn as never)).toBe(true);
  20. });
  21. it("普通 future queue 在前时仍允许后续 guide 走当前轮", () => {
  22. const activeTurn = {
  23. pendingInputs: [
  24. { id: "future-queue", commandKind: undefined, delivery: "queue" },
  25. { id: "current-guide", commandKind: undefined, delivery: "guide" },
  26. ],
  27. };
  28. const runtime = {
  29. activeTurn,
  30. pendingInputReservations: new Map<string, string>(),
  31. queueExternalDrainActive: false,
  32. };
  33. expect(hasInlineGuidePendingInput.call(runtime as never, activeTurn as never)).toBe(true);
  34. });
  35. it("行内 drain 只取最早 guide,并把普通 future queue 留在原位", async () => {
  36. const futureQueue = {
  37. id: "future-queue",
  38. input: "future work",
  39. queuedAt: new Date(0),
  40. delivery: "queue",
  41. };
  42. const currentGuide = {
  43. id: "current-guide",
  44. input: "guide current work",
  45. queuedAt: new Date(0),
  46. delivery: "guide",
  47. intent: {
  48. genUiContext: [{ path: "/project/example.html", state: { stage: "guide" } }],
  49. },
  50. };
  51. const activeTurn = {
  52. pendingInputs: [futureQueue, currentGuide],
  53. turnId: "turn-current",
  54. };
  55. const appendedEvents: Array<{ payload: { pendingInputIds?: string[] } }> = [];
  56. const runtime = {
  57. activeTurn,
  58. appendEvent: vi.fn(async (event) => appendedEvents.push(event)),
  59. createEvent: vi.fn((_type, payload) => ({ payload })),
  60. messageHistory: { addEntries: vi.fn() },
  61. now: () => new Date(0),
  62. pendingInputReservations: new Map<string, string>(),
  63. persistUserPrompt: vi.fn(async () => undefined),
  64. sessionId: "session-current",
  65. workingDirectory: "/workspace",
  66. };
  67. await drainPendingInput.call(runtime as never, {
  68. activeTurn: activeTurn as never,
  69. events: [],
  70. traceContext: { traceId: "trace-current" } as never,
  71. });
  72. expect(activeTurn.pendingInputs).toEqual([futureQueue]);
  73. expect(runtime.messageHistory.addEntries).toHaveBeenCalledOnce();
  74. expect(JSON.stringify(runtime.messageHistory.addEntries.mock.calls[0])).toContain(
  75. "guide current work",
  76. );
  77. expect(JSON.stringify(runtime.messageHistory.addEntries.mock.calls[0])).not.toContain(
  78. "future work",
  79. );
  80. // 页面状态挂在 guide 自己的 entry 上,不写单独的状态条目。
  81. expect(runtime.messageHistory.addEntries.mock.calls[0]?.[0]).toEqual([
  82. expect.objectContaining({
  83. message: { role: "user", content: "guide current work" },
  84. metadata: expect.objectContaining({
  85. genUiState: expect.stringContaining(
  86. '[{"path":"/project/example.html","state":{"stage":"guide"}}]',
  87. ),
  88. }),
  89. }),
  90. ]);
  91. expect(appendedEvents).toEqual([
  92. expect.objectContaining({
  93. payload: expect.objectContaining({ pendingInputIds: ["current-guide"] }),
  94. }),
  95. ]);
  96. });
  97. });