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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. pendingInputReservations: new Map<string, string>(),
  62. persistUserPrompt: vi.fn(async () => undefined),
  63. sessionId: "session-current",
  64. workingDirectory: "/workspace",
  65. };
  66. await drainPendingInput.call(runtime as never, {
  67. activeTurn: activeTurn as never,
  68. events: [],
  69. traceContext: { traceId: "trace-current" } as never,
  70. });
  71. expect(activeTurn.pendingInputs).toEqual([futureQueue]);
  72. expect(runtime.messageHistory.addEntries).toHaveBeenCalledOnce();
  73. expect(JSON.stringify(runtime.messageHistory.addEntries.mock.calls[0])).toContain(
  74. "guide current work",
  75. );
  76. expect(JSON.stringify(runtime.messageHistory.addEntries.mock.calls[0])).not.toContain(
  77. "future work",
  78. );
  79. // 页面状态挂在 guide 自己的 entry 上,不写单独的状态条目。
  80. expect(runtime.messageHistory.addEntries.mock.calls[0]?.[0]).toEqual([
  81. expect.objectContaining({
  82. message: { role: "user", content: "guide current work" },
  83. metadata: expect.objectContaining({
  84. genUiState: expect.stringContaining(
  85. '[{"path":"/project/example.html","state":{"stage":"guide"}}]',
  86. ),
  87. }),
  88. }),
  89. ]);
  90. expect(appendedEvents).toEqual([
  91. expect.objectContaining({
  92. payload: expect.objectContaining({ pendingInputIds: ["current-guide"] }),
  93. }),
  94. ]);
  95. });
  96. });