conversation-session-goal-queue.test.ts 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. import { clearAppData } from "../../../helpers/desktop-app.js";
  2. import {
  3. E2E_REPLY_TOKEN,
  4. assertVisibleUserMessagesNotContaining,
  5. clickChatStop,
  6. clickQueueEdit,
  7. clickQueueEditSave,
  8. clickQueueRemove,
  9. countUpstreamRequestsContaining,
  10. dragQueueItemOnto,
  11. expectNoUpstreamRequestForTextWithin,
  12. getQueueItems,
  13. prepareConversationE2E,
  14. sendPrompt,
  15. setQueueEditInputText,
  16. startNewTask,
  17. waitForChatState,
  18. waitForComposerText,
  19. waitForQueueCount,
  20. waitForQueueOrderContaining,
  21. } from "../../../helpers/conversation-session.js";
  22. const COMPACT_REQUEST_SENTINEL = "CRITICAL: Respond with TEXT ONLY";
  23. describe("会话区 Goal Queue E2E", () => {
  24. afterEach(async () => {
  25. await stopIfStreaming();
  26. });
  27. after(async () => {
  28. await browser.electron.restoreAllMocks();
  29. await clearAppData();
  30. });
  31. it("goal 队列项可编辑、删除、重排;running 中 /compact 不进入混合队列", async function () {
  32. this.timeout(170000);
  33. await prepareConversationE2E();
  34. await startNewTask();
  35. const runId = Date.now();
  36. await sendPrompt(
  37. `E2E_SLOW_STREAM E2E_GOAL_QUEUE_BASE_${runId}: Reply with exactly "${E2E_REPLY_TOKEN}" and no other text.`,
  38. );
  39. await waitForComposerText("", "goal queue 基础消息发送后输入框没有清空");
  40. await waitForChatState(
  41. (snapshot) => snapshot.state === "streaming",
  42. "goal queue 基础消息没有进入 streaming",
  43. 30000,
  44. );
  45. const firstGoal = goalPrompt(`E2E_GOAL_QUEUE_FIRST_${runId}`);
  46. const queuedText = `E2E_GOAL_QUEUE_TEXT_${runId}: Reply with exactly "${E2E_REPLY_TOKEN}" and no other text.`;
  47. const secondGoal = goalPrompt(`E2E_GOAL_QUEUE_SECOND_${runId}`);
  48. await sendPrompt(firstGoal);
  49. await sendPrompt(queuedText);
  50. await sendPrompt(secondGoal);
  51. await waitForQueueOrderContaining([
  52. `E2E_GOAL_QUEUE_FIRST_${runId}`,
  53. `E2E_GOAL_QUEUE_TEXT_${runId}`,
  54. `E2E_GOAL_QUEUE_SECOND_${runId}`,
  55. ]);
  56. let queue = await waitForQueueCount(3);
  57. expect(queue.map((item) => item.kind)).toEqual(["goal", "text", "goal"]);
  58. const compactRequestsBefore = await countUpstreamRequestsContaining(
  59. COMPACT_REQUEST_SENTINEL,
  60. );
  61. await sendPrompt("/compact");
  62. await waitForQueueCount(3);
  63. await assertVisibleUserMessagesNotContaining("/compact");
  64. expect(
  65. await countUpstreamRequestsContaining(COMPACT_REQUEST_SENTINEL),
  66. ).toBe(compactRequestsBefore);
  67. await clickChatStop();
  68. await waitForChatState(
  69. (snapshot) =>
  70. snapshot.state !== "streaming" &&
  71. snapshot.runtimeStatus === "completed" &&
  72. snapshot.activeInputId === null &&
  73. snapshot.queueCount === 3,
  74. // 修复原因:stop ACK 可能早于 runtime terminal 投影;后续 queue 编辑
  75. // 虽不发 provider,也需要等旧 activeInput 清空,避免拖拽/删除读到过渡态。
  76. "goal queue stop 后没有保留 3 条队列",
  77. 30000,
  78. );
  79. queue = await getQueueItems();
  80. const firstId = queue[0]?.id;
  81. const textId = queue[1]?.id;
  82. const secondId = queue[2]?.id;
  83. if (!firstId || !textId || !secondId) {
  84. throw new Error(`goal queue 队列项 id 缺失: ${JSON.stringify(queue)}`);
  85. }
  86. const editedGoalMarker = `E2E_GOAL_QUEUE_SECOND_EDITED_${runId}`;
  87. const editedGoalRequestsBefore =
  88. await countUpstreamRequestsContaining(editedGoalMarker);
  89. await clickQueueEdit(secondId);
  90. await setQueueEditInputText(secondId, goalObjective(editedGoalMarker));
  91. await clickQueueEditSave(secondId);
  92. queue = await waitForQueueOrderContaining([
  93. `E2E_GOAL_QUEUE_FIRST_${runId}`,
  94. `E2E_GOAL_QUEUE_TEXT_${runId}`,
  95. editedGoalMarker,
  96. ]);
  97. expect(queue.map((item) => item.kind)).toEqual(["goal", "text", "goal"]);
  98. expect(await countUpstreamRequestsContaining(editedGoalMarker)).toBe(
  99. editedGoalRequestsBefore,
  100. );
  101. await dragQueueItemOnto(secondId, firstId);
  102. queue = await waitForQueueOrderContaining([
  103. editedGoalMarker,
  104. `E2E_GOAL_QUEUE_FIRST_${runId}`,
  105. `E2E_GOAL_QUEUE_TEXT_${runId}`,
  106. ]);
  107. expect(queue.map((item) => item.id)).toEqual([secondId, firstId, textId]);
  108. expect(queue.map((item) => item.kind)).toEqual(["goal", "goal", "text"]);
  109. await clickQueueRemove(firstId);
  110. queue = await waitForQueueOrderContaining([
  111. editedGoalMarker,
  112. `E2E_GOAL_QUEUE_TEXT_${runId}`,
  113. ]);
  114. expect(queue.map((item) => item.id)).toEqual([secondId, textId]);
  115. expect(queue.map((item) => item.kind)).toEqual(["goal", "text"]);
  116. expect(queue.map((item) => item.content).join("\n")).not.toContain(
  117. `E2E_GOAL_QUEUE_FIRST_${runId}`,
  118. );
  119. await expectNoUpstreamRequestForTextWithin(editedGoalMarker, 800);
  120. });
  121. it("多个 queued goal 在 running 中保持 FIFO,停止后不自动消费", async function () {
  122. this.timeout(190000);
  123. await prepareConversationE2E();
  124. await startNewTask();
  125. const runId = Date.now();
  126. await sendPrompt(
  127. `E2E_SLOW_STREAM E2E_GOAL_QUEUE_ORDER_BASE_${runId}: Reply with exactly "${E2E_REPLY_TOKEN}" and no other text.`,
  128. );
  129. await waitForChatState(
  130. (snapshot) => snapshot.state === "streaming",
  131. "goal queue order 基础消息没有进入 streaming",
  132. 30000,
  133. );
  134. const firstMarker = `E2E_GOAL_QUEUE_ORDER_FIRST_${runId}`;
  135. const secondMarker = `E2E_GOAL_QUEUE_ORDER_SECOND_${runId}`;
  136. await sendPrompt(goalPrompt(firstMarker));
  137. await sendPrompt(goalPrompt(secondMarker));
  138. let queue = await waitForQueueOrderContaining([firstMarker, secondMarker]);
  139. expect(queue.map((item) => item.kind)).toEqual(["goal", "goal"]);
  140. await expectNoUpstreamRequestForTextWithin(firstMarker, 800);
  141. await expectNoUpstreamRequestForTextWithin(secondMarker, 800);
  142. await clickChatStop();
  143. await waitForChatState(
  144. (snapshot) =>
  145. snapshot.state !== "streaming" &&
  146. snapshot.runtimeStatus === "completed" &&
  147. snapshot.activeInputId === null &&
  148. snapshot.queueCount === 2,
  149. // 修复原因:连续 queued goal 的 send-now 覆盖 active target 属于 running-send-now 专项边界;
  150. // 本正式 GQ case 只锁定多条 /goal 在 running 中入队、FIFO 和 stop 后 held queue 不自动消费。
  151. "goal queue order stop 后没有保留 2 条 goal",
  152. 30000,
  153. );
  154. queue = await waitForQueueOrderContaining([firstMarker, secondMarker]);
  155. expect(queue.map((item) => item.kind)).toEqual(["goal", "goal"]);
  156. await expectNoUpstreamRequestForTextWithin(firstMarker, 800);
  157. await expectNoUpstreamRequestForTextWithin(secondMarker, 800);
  158. });
  159. });
  160. function goalPrompt(marker: string) {
  161. return `/goal ${goalObjective(marker)}`;
  162. }
  163. function goalObjective(marker: string) {
  164. return `${marker}: Reply with exactly "${E2E_REPLY_TOKEN}" and no other text.`;
  165. }
  166. async function stopIfStreaming() {
  167. const snapshot = await waitForChatState(
  168. () => true,
  169. "读取 chat 状态失败",
  170. 5000,
  171. );
  172. if (snapshot.state !== "streaming") {
  173. return;
  174. }
  175. await clickChatStop().catch(() => undefined);
  176. await waitForChatState(
  177. (candidate) => candidate.state !== "streaming",
  178. "afterEach stop 后没有退出 streaming",
  179. 30000,
  180. ).catch(() => undefined);
  181. }