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

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