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

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