conversation-session-queue-operations.test.ts 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. import { setRendererViewport, clearRendererViewport } from "#e2e/helpers/renderer-viewport";
  2. import { join } from "node:path";
  3. import { releaseControlledReplay, waitForControlledReplay } from "#e2e/helpers/controlled-replay";
  4. import {
  5. approveV4Permission,
  6. waitForV4PermissionDialogInComposerDock,
  7. waitForV4AssistantMessageContaining,
  8. } from "#e2e/helpers/v4-conversation";
  9. import { clearAppData } from "#e2e/helpers/desktop-app";
  10. import {
  11. E2E_REPLY_TOKEN,
  12. clickV4Stop,
  13. getV4ComposerText,
  14. setV4ComposerText,
  15. dragV4QueueItemOnto,
  16. prepareV4ConversationE2E,
  17. sendV4Prompt,
  18. waitForV4ConversationState,
  19. waitForV4ComposerText,
  20. } from "#e2e/helpers/v4-conversation";
  21. import {
  22. clickQueueRemove,
  23. getQueueItems,
  24. waitForQueueCount,
  25. waitForQueueOrderContaining,
  26. } from "#e2e/helpers/conversation-session-queue";
  27. import { countUpstreamRequestsContaining } from "#e2e/helpers/conversation-session-network";
  28. import {
  29. getV4QueueItemEditLock,
  30. openV4QueueItemEditInComposer,
  31. waitForV4ComposerEditClosed,
  32. waitForV4QueueItemEditLock,
  33. } from "#e2e/helpers/v4-composer-edit";
  34. describe("会话区 Held Queue 操作 E2E", () => {
  35. after(async () => {
  36. await browser.electron.restoreAllMocks();
  37. await clearAppData();
  38. });
  39. it("held queue 编辑原位冻结,重排和删除其他项保留编辑内容,提交原地替换且不触发模型请求", async function () {
  40. this.timeout(180000);
  41. await prepareV4ConversationE2E();
  42. const runId = Date.now();
  43. const runningPrompt = `E2E_SLOW_STREAM E2E_QUEUE_OPS_RUNNING_${runId}: Reply with exactly "${E2E_REPLY_TOKEN}" and no other text.`;
  44. await sendV4Prompt(runningPrompt);
  45. await waitForV4ComposerText("", "held queue 操作首轮发送后输入框没有清空");
  46. await waitForV4ConversationState(
  47. (snapshot) => snapshot.state === "streaming",
  48. "held queue 操作首轮没有进入 streaming",
  49. 30000,
  50. );
  51. const firstQueuedPrompt = `E2E_QUEUE_OPS_FIRST_${runId}: Reply with exactly "${E2E_REPLY_TOKEN}" and no other text.`;
  52. const secondQueuedPrompt = `E2E_QUEUE_OPS_SECOND_${runId}: Reply with exactly "${E2E_REPLY_TOKEN}" and no other text.`;
  53. const thirdQueuedPrompt = `E2E_QUEUE_OPS_THIRD_${runId}: Reply with exactly "${E2E_REPLY_TOKEN}" and no other text.`;
  54. await sendV4Prompt(firstQueuedPrompt);
  55. await waitForQueueCount(1);
  56. await sendV4Prompt(secondQueuedPrompt);
  57. await waitForQueueCount(2);
  58. await sendV4Prompt(thirdQueuedPrompt);
  59. let queue = await waitForQueueCount(3);
  60. expect(queue.map((item) => item.content)).toEqual([
  61. firstQueuedPrompt,
  62. secondQueuedPrompt,
  63. thirdQueuedPrompt,
  64. ]);
  65. await clickV4Stop();
  66. await waitForV4ConversationState(
  67. (snapshot) => snapshot.state !== "streaming" && snapshot.queueCount === 3,
  68. "held queue 操作 stop 后没有保留 3 条队列",
  69. 30000,
  70. );
  71. queue = await getQueueItems();
  72. const firstId = queue[0]?.id;
  73. const secondId = queue[1]?.id;
  74. const thirdId = queue[2]?.id;
  75. if (!firstId || !secondId || !thirdId) {
  76. throw new Error(`队列项 id 缺失: ${JSON.stringify(queue)}`);
  77. }
  78. const editedSecondPrompt = `E2E_QUEUE_OPS_SECOND_EDITED_${runId}: Reply with exactly "${E2E_REPLY_TOKEN}" and no other text.`;
  79. const editedRequestsBefore = await countUpstreamRequestsContaining(
  80. `E2E_QUEUE_OPS_SECOND_EDITED_${runId}`,
  81. );
  82. // 编辑时队列项原位冻结(docs/ui/composer-edit-mode.md),composer 载入原文。
  83. await openV4QueueItemEditInComposer(secondId);
  84. await waitForV4ComposerText(secondQueuedPrompt, "编辑态没有载入原文");
  85. queue = await waitForQueueCount(3);
  86. expect(queue.map((item) => item.id)).toEqual([firstId, secondId, thirdId]);
  87. // 编辑态在 lock ACK 后进入;editLock 随后经投影到达,需等待而不是读一次。
  88. await waitForV4QueueItemEditLock(secondId, (state) => state.locked, "编辑项没有显示编辑锁");
  89. await setV4ComposerText(editedSecondPrompt);
  90. expect(await countUpstreamRequestsContaining(`E2E_QUEUE_OPS_SECOND_EDITED_${runId}`)).toBe(
  91. editedRequestsBefore,
  92. );
  93. // 冻结项之外的项仍可重排;编辑内容不受影响。
  94. await dragV4QueueItemOnto(thirdId, firstId);
  95. queue = await waitForQueueOrderContaining(
  96. [`E2E_QUEUE_OPS_THIRD_${runId}`, `E2E_QUEUE_OPS_FIRST_${runId}`],
  97. "重排队列项后没有只改变 queue 顺序",
  98. );
  99. expect(queue.map((item) => item.id)).toEqual([thirdId, firstId, secondId]);
  100. expect(await getV4ComposerText()).toBe(editedSecondPrompt);
  101. const removedRequestsBefore = await countUpstreamRequestsContaining(
  102. `E2E_QUEUE_OPS_FIRST_${runId}`,
  103. );
  104. await clickQueueRemove(firstId);
  105. queue = await waitForQueueCount(2);
  106. expect(queue.map((item) => item.id)).toEqual([thirdId, secondId]);
  107. expect(queue.map((item) => item.content).join("\n")).not.toContain(
  108. `E2E_QUEUE_OPS_FIRST_${runId}`,
  109. );
  110. expect(await getV4ComposerText()).toBe(editedSecondPrompt);
  111. expect(await countUpstreamRequestsContaining(`E2E_QUEUE_OPS_FIRST_${runId}`)).toBe(
  112. removedRequestsBefore,
  113. );
  114. // 提交编辑:原地替换 second,ID 与位置不变;held queue 下不触发消费。
  115. await sendV4Prompt(editedSecondPrompt);
  116. await waitForV4ComposerEditClosed("held queue 编辑提交后没有退出编辑态", 30000);
  117. queue = await waitForQueueOrderContaining(
  118. [`E2E_QUEUE_OPS_THIRD_${runId}`, `E2E_QUEUE_OPS_SECOND_EDITED_${runId}`],
  119. "编辑提交后队列项没有原地替换",
  120. );
  121. expect(queue.map((item) => item.id)).toEqual([thirdId, secondId]);
  122. expect((await getV4QueueItemEditLock(secondId)).locked).toBe(false);
  123. await waitForV4ComposerText("", "编辑提交后没有恢复原草稿(空)");
  124. expect(await countUpstreamRequestsContaining(`E2E_QUEUE_OPS_SECOND_EDITED_${runId}`)).toBe(
  125. editedRequestsBefore,
  126. );
  127. });
  128. for (const kind of ["permission", "question"] as const) {
  129. it(`已有队列在 ${kind} 阻塞态仍可删除,宽窄视口均可见`, async function () {
  130. this.timeout(180000);
  131. await prepareV4ConversationE2E();
  132. const marker = `E2E_QUEUE_OPS_BLOCK_${kind.toUpperCase()}`;
  133. const fixtureId = `queue-block-${kind}`;
  134. await sendV4Prompt(marker);
  135. await waitForControlledReplay(fixtureId);
  136. await sendV4Prompt(`E2E_QUEUE_OPS_KEEP_${kind}`);
  137. await waitForQueueCount(1);
  138. await sendV4Prompt(`E2E_QUEUE_OPS_DELETE_${kind}`);
  139. const queue = await waitForQueueCount(2);
  140. await releaseControlledReplay(fixtureId);
  141. if (kind === "permission") {
  142. expect((await waitForV4PermissionDialogInComposerDock()).composerHidden).toBe(true);
  143. } else {
  144. await $('[data-elicitation-dialog-body="true"]').waitForDisplayed({ timeout: 30000 });
  145. }
  146. const queueElement = $('[data-testid="v4-queue"]');
  147. await queueElement.waitForDisplayed({ timeout: 10000 });
  148. try {
  149. await setRendererViewport(900, 900);
  150. await queueElement.waitForDisplayed();
  151. await browser.saveScreenshot(
  152. join(process.env.ZCODE_E2E_ARTIFACT_DIR!, `${fixtureId}-narrow.png`),
  153. );
  154. const remove = $(`[data-testid="v4-queue-item-delete-${queue[1]!.id}"]`);
  155. await remove.waitForDisplayed();
  156. expect(await remove.isClickable()).toBe(true);
  157. expect(
  158. await browser.execute((id) => {
  159. const rect = document
  160. .querySelector(`[data-testid="v4-queue-item-delete-${id}"]`)
  161. ?.getBoundingClientRect();
  162. return Boolean(
  163. rect &&
  164. rect.left >= 0 &&
  165. rect.right <= innerWidth &&
  166. rect.top >= 0 &&
  167. rect.bottom <= innerHeight,
  168. );
  169. }, queue[1]!.id),
  170. ).toBe(true);
  171. await remove.click();
  172. expect((await waitForQueueCount(1))[0]!.id).toBe(queue[0]!.id);
  173. } finally {
  174. await clearRendererViewport();
  175. }
  176. // React 队列节点即使渲染 null 仍为真;记录真实父节点,检查最后一项删除后连同留白退出。
  177. await browser.execute(() => {
  178. document
  179. .querySelector('[data-testid="v4-queue"]')
  180. ?.parentElement?.setAttribute("data-e2e-queue-wrapper", "true");
  181. });
  182. await $(`[data-testid="v4-queue-item-delete-${queue[0]!.id}"]`).click();
  183. await waitForQueueCount(0);
  184. expect(await $('[data-e2e-queue-wrapper="true"]').isExisting()).toBe(false);
  185. if (kind === "permission") {
  186. expect(await approveV4Permission()).toBe(true);
  187. } else {
  188. await $('[data-elicitation-dialog-body="true"] textarea').setValue("Continue verification");
  189. const buttons = await $$('[data-elicitation-dialog-footer="true"] button');
  190. let submitted = false;
  191. for (const button of buttons) {
  192. if (/^(Submit|提交)$/.test((await button.getText()).trim())) {
  193. await button.click();
  194. submitted = true;
  195. break;
  196. }
  197. }
  198. expect(submitted).toBe(true);
  199. }
  200. await waitForQueueCount(0);
  201. await waitForV4AssistantMessageContaining(E2E_REPLY_TOKEN, 60000);
  202. expect(await countUpstreamRequestsContaining(`E2E_QUEUE_OPS_DELETE_${kind}`)).toBe(0);
  203. });
  204. }
  205. });