conversation-session-v4-queue.test.ts 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. import { mkdir, writeFile } from "node:fs/promises";
  2. import { resolve } from "node:path";
  3. import { chromium } from "playwright-core";
  4. import { openMobilePairingPage, seedMobileFixtureLogin } from "#e2e/helpers/mobile-pairing";
  5. import { obtainUnreadPairingUrl, showMobileTaskHome } from "#e2e/helpers/web-remote-unread";
  6. import {
  7. expectQueueFreezeEditWithoutFlash,
  8. finishQueueEditVisualProbe,
  9. startQueueEditVisualProbe,
  10. } from "#e2e/helpers/queue-edit-visual";
  11. // M4 门禁:queue 单项管理 + B09 冻结编辑(docs/ui/composer-edit-mode.md)。
  12. // 证据层 L3:running turn 期间 sendText 入队 → QueuePanel → 编辑时 lockQueueItemEdit 原位冻结 →
  13. // composer 暂存原草稿并载入 text + AttachmentRef[] → 发送走 editQueueItem 原地替换(ID 不变)
  14. // 或取消解锁;两条路径都恢复原草稿。删除与重排仍回到 CLI 权威投影。
  15. import { clearAppData } from "#e2e/helpers/desktop-app";
  16. import {
  17. clickV4ComposerEditCancel,
  18. getV4QueueItemEditLock,
  19. waitForV4ComposerEditClosed,
  20. waitForV4ComposerEditState,
  21. waitForV4QueueItemEditLock,
  22. } from "#e2e/helpers/v4-composer-edit";
  23. import {
  24. clickV4Stop,
  25. clickV4QueueItemDelete,
  26. dragV4QueueItemOnto,
  27. clickV4QueueItemEdit,
  28. getV4ComposerAttachments,
  29. getV4ComposerText,
  30. getV4PaneSnapshot,
  31. getV4QueueItems,
  32. isV4ComposerFocused,
  33. prepareV4ConversationE2E,
  34. pasteV4ComposerImageAttachment,
  35. sendV4Prompt,
  36. setV4ComposerText,
  37. waitForV4ComposerText,
  38. waitForV4Pane,
  39. waitForV4QueueCount,
  40. waitForV4TimelineContaining,
  41. } from "#e2e/helpers/v4-conversation";
  42. describe("v4 M4/B09 门禁:queue 单项管理与冻结编辑", () => {
  43. afterEach(async () => {
  44. const snapshot = await getV4PaneSnapshot();
  45. if (!snapshot.canStop) return;
  46. // 修复原因:controlled stream 会跨 case 存活 45 秒;不主动 stop 会让上一条
  47. // active runtime 与下一条 draft 并行,并在结尾 drain 残留 queue,污染模型/fixture。
  48. await clickV4Stop();
  49. await waitForV4Pane((next) => !next.canStop, "queue operation case 收尾 stop 没有完成", 30000);
  50. });
  51. after(async () => {
  52. await browser.electron.restoreAllMocks();
  53. await clearAppData();
  54. });
  55. it("running 中入队一条 → QueuePanel 显示 → 删除 → 队列清空", async () => {
  56. await prepareV4ConversationE2E();
  57. // 首轮:可控慢流,保持 running
  58. await sendV4Prompt("E2E_V4_QUEUE_SLOW 慢慢回答");
  59. await waitForV4TimelineContaining("V4_QUEUE_STREAMING", 45000);
  60. await waitForV4Pane(
  61. (s) => s.canStop && s.sessionId !== "draft" && s.sessionId !== null,
  62. "首轮没有进入 running(canStop)",
  63. 45000,
  64. );
  65. // running 中发第二条 → 入队
  66. await sendV4Prompt("E2E_V4_QUEUE_ITEM 排队的问题");
  67. await waitForV4QueueCount(1, 30000);
  68. const before = await getV4QueueItems();
  69. expect(before).toHaveLength(1);
  70. expect(before[0]!.text).toContain("E2E_V4_QUEUE_ITEM");
  71. // 删除该队列项 → 队列清空
  72. const clicked = await clickV4QueueItemDelete(before[0]!.queueItemId);
  73. expect(clicked).toBe(true);
  74. await waitForV4QueueCount(0, 30000);
  75. expect(await getV4QueueItems()).toHaveLength(0);
  76. });
  77. it("B09/B09b/B09e:带图片入队 → 有草稿时编辑 → 原位冻结 → 修改后发送原地替换并恢复草稿", async () => {
  78. await prepareV4ConversationE2E();
  79. await sendV4Prompt("E2E_V4_QUEUE_SLOW 慢慢回答");
  80. await waitForV4TimelineContaining("V4_QUEUE_STREAMING", 45000);
  81. await waitForV4Pane(
  82. (s) => s.canStop && s.sessionId !== "draft" && s.sessionId !== null,
  83. "首轮没有进入 running(canStop)",
  84. 45000,
  85. );
  86. await pasteV4ComposerImageAttachment("e2e-queue-edit.png");
  87. await sendV4Prompt("E2E_V4_QUEUE_ITEM 排队原文");
  88. await waitForV4QueueCount(1, 30000);
  89. const before = await getV4QueueItems();
  90. expect(before[0]!.text).toContain("排队原文");
  91. const queueItemId = before[0]!.queueItemId;
  92. // B09:composer 已有草稿也可以编辑;草稿由编辑态暂存,不再提示冲突。
  93. await setV4ComposerText("保留现有草稿");
  94. // 同一事件循环连点两次,验证 lock 请求期间的 operation lock 在 React 重渲染前生效,
  95. // 只进入一次编辑态。
  96. // B09g:单次点击前启动逐帧采样,验证加锁中 → 本端占位之间不回到普通行、不重挂队列。
  97. await browser.execute(startQueueEditVisualProbe, queueItemId);
  98. const clicked = await clickV4QueueItemEdit(queueItemId, 2);
  99. expect(clicked).toBe(true);
  100. await waitForV4ComposerEditState(
  101. (state) =>
  102. state?.kind === "queueItem" &&
  103. state.targetId === queueItemId &&
  104. state.state === "editing" &&
  105. state.hasCancel,
  106. "queue 项没有进入 composer 编辑态",
  107. 30000,
  108. );
  109. // 队列项留在原位,显示「正在下方编辑」,并隐藏本行操作。
  110. const locked = await waitForV4QueueItemEditLock(
  111. queueItemId,
  112. (state) => state.locked && state.owner === "self",
  113. "queue 项没有原位冻结",
  114. );
  115. expect(locked.lockText.length).toBeGreaterThan(0);
  116. expect(locked.editAction).toBe(false);
  117. expect(locked.deleteAction).toBe(false);
  118. expect(locked.sendNowAction).toBe(false);
  119. expect(await getV4QueueItems()).toHaveLength(1);
  120. await browser.waitUntil(
  121. async () => {
  122. const text = await getV4ComposerText();
  123. const attachments = await getV4ComposerAttachments();
  124. return (
  125. text === "E2E_V4_QUEUE_ITEM 排队原文" &&
  126. attachments.length === 1 &&
  127. // Bug 原因:从 queue 载入的媒体 ref 不重建本地 object URL,缩略图可能只显示
  128. // image fallback icon;用 innerText 验证 filename 会误判已载入且 ready 的附件。
  129. attachments[0]?.uploadStatus === "ready"
  130. );
  131. },
  132. { timeout: 30000, timeoutMsg: "编辑态没有完整载入队列项文本与附件" },
  133. );
  134. const visual = await browser.execute(finishQueueEditVisualProbe);
  135. await saveVisualEvidence("attachment", visual);
  136. expectQueueFreezeEditWithoutFlash(visual, {
  137. initialComposerText: "保留现有草稿",
  138. loadedText: "E2E_V4_QUEUE_ITEM 排队原文",
  139. });
  140. expect(await isV4ComposerFocused()).toBe(true);
  141. await sendV4Prompt("E2E_V4_QUEUE_ITEM 编辑后重新提交");
  142. await waitForV4ComposerEditClosed("editQueueItem 成功后没有退出编辑态", 30000);
  143. await browser.waitUntil(
  144. async () => {
  145. const items = await getV4QueueItems();
  146. return items.length === 1 && items[0]!.text.includes("编辑后重新提交");
  147. },
  148. { timeout: 30000, timeoutMsg: "editQueueItem 没有原地替换队列项内容" },
  149. );
  150. const after = await getV4QueueItems();
  151. // B09e:原地替换,queueItemId 与位置不变。
  152. expect(after[0]?.queueItemId).toBe(queueItemId);
  153. expect((await getV4QueueItemEditLock(queueItemId)).locked).toBe(false);
  154. // 退出编辑态后恢复进入前的原草稿。
  155. await waitForV4ComposerText("保留现有草稿", "editQueueItem 成功后没有恢复原草稿", 15000);
  156. expect(await getV4ComposerAttachments()).toHaveLength(0);
  157. });
  158. for (const [label, text, theme, held] of [
  159. ["short", "E2E_V4_QUEUE_ITEM 排队原文", "zai-light", false],
  160. ["multiline", "E2E_V4_QUEUE_ITEM 第一行\n第二行\n第三行\n第四行\n第五行", "zai-dark", true],
  161. ] as const) {
  162. it(`B09g:单条队列冻结编辑不闪动:${label} / ${theme} / ${held ? "held" : "running"}`, async () => {
  163. await prepareV4ConversationE2E();
  164. await browser.execute(setQueueTestTheme, theme);
  165. await sendV4Prompt("E2E_V4_QUEUE_SLOW 慢慢回答");
  166. await waitForV4TimelineContaining("V4_QUEUE_STREAMING", 45000);
  167. await sendV4Prompt(text);
  168. await waitForV4QueueCount(1, 30000);
  169. if (held) {
  170. await clickV4Stop();
  171. await waitForV4Pane((snapshot) => !snapshot.canStop, "held queue stop", 30000);
  172. }
  173. const [item] = await getV4QueueItems();
  174. await browser.execute(startQueueEditVisualProbe, item!.queueItemId);
  175. expect(await clickV4QueueItemEdit(item!.queueItemId)).toBe(true);
  176. await waitForV4QueueItemEditLock(
  177. item!.queueItemId,
  178. (state) => state.locked && state.owner === "self",
  179. "单条队列项没有原位冻结",
  180. );
  181. await waitForV4ComposerText(text, "单条队列编辑态没有载入原文");
  182. const visual = await browser.execute(finishQueueEditVisualProbe);
  183. await saveVisualEvidence(label, visual);
  184. expectQueueFreezeEditWithoutFlash(visual, { initialComposerText: "", loadedText: text });
  185. expect(await isV4ComposerFocused()).toBe(true);
  186. expect(await getV4QueueItems()).toHaveLength(1);
  187. // 取消后解锁并恢复空草稿,afterEach 才能停止慢流。
  188. await clickV4ComposerEditCancel();
  189. await waitForV4ComposerEditClosed("单条队列取消编辑后没有退出编辑态");
  190. });
  191. }
  192. it("B09g:手机单条队列冻结编辑不闪动,深浅主题及桌面草稿隔离", async function () {
  193. this.timeout(180000);
  194. await prepareV4ConversationE2E();
  195. await sendV4Prompt("E2E_V4_QUEUE_SLOW 慢慢回答");
  196. await waitForV4TimelineContaining("V4_QUEUE_STREAMING", 45000);
  197. const taskId = (await getV4PaneSnapshot()).sessionId;
  198. await clickV4Stop();
  199. await waitForV4Pane((snapshot) => !snapshot.canStop, "手机配对前停止首轮", 30000);
  200. const endpoint = process.env.ZCODE_WEB_REMOTE_CONTROL_URL;
  201. if (!endpoint || !taskId) throw new Error("缺少手机 fixture / task");
  202. const pairingUrl = await obtainUnreadPairingUrl(new URL(endpoint).origin);
  203. const executablePath = process.env.ZCODE_E2E_MOBILE_BROWSER_EXECUTABLE;
  204. const chrome = await chromium.launch({
  205. ...(executablePath ? { executablePath } : { channel: "chrome" }),
  206. headless: true,
  207. });
  208. try {
  209. const context = await chrome.newContext({
  210. viewport: { width: 390, height: 844 },
  211. isMobile: true,
  212. hasTouch: true,
  213. locale: "zh-CN",
  214. });
  215. // WDIO 自动注入此构建辅助函数,Playwright 没有;让共享采样函数中的嵌套回调可序列化运行。
  216. await context.addInitScript({
  217. content:
  218. 'globalThis.__name = (target, value) => Object.defineProperty(target, "name", { value, configurable: true });',
  219. });
  220. await seedMobileFixtureLogin(context);
  221. const page = await context.newPage();
  222. await openMobilePairingPage(page, pairingUrl, {
  223. artifactDir,
  224. caseMarker: "B09g",
  225. phase: "pairing",
  226. readySelector: '[data-testid="sidebar"], [data-mobile-page="chat"]',
  227. });
  228. await showMobileTaskHome(page);
  229. const toggles = page.getByTestId("virtual-project-toggle");
  230. for (let i = 0; i < (await toggles.count()); i++) {
  231. const toggle = toggles.nth(i);
  232. if ((await toggle.getAttribute("aria-expanded")) !== "true") await toggle.click();
  233. }
  234. await page.getByTestId(`task-item-${taskId}`).first().click();
  235. // 配对耗时不应消耗 controlled stream 的运行窗口。
  236. await sendV4Prompt("E2E_V4_QUEUE_SLOW 慢慢回答");
  237. await waitForV4Pane((snapshot) => snapshot.canStop, "手机编辑前启动慢流", 30000);
  238. for (const theme of ["zai-light", "zai-dark"] as const) {
  239. await page.evaluate(setQueueTestTheme, theme);
  240. const text = `E2E_V4_QUEUE_ITEM ${theme}\n第二行\n第三行`;
  241. await sendV4Prompt(text);
  242. await waitForV4QueueCount(1, 30000);
  243. const [item] = await getV4QueueItems();
  244. const edit = page.getByTestId(`v4-queue-item-edit-${item!.queueItemId}`);
  245. await edit.waitFor();
  246. // 手机发起的冻结编辑只进入手机 composer;桌面原草稿不得被覆盖。
  247. await setV4ComposerText("desktop draft stays local");
  248. await page.evaluate(startQueueEditVisualProbe, item!.queueItemId);
  249. await edit.click();
  250. await page.waitForFunction((expected) => {
  251. const element = document.querySelector<HTMLElement>(
  252. '[data-testid="v4-composer-input"]',
  253. ) as (HTMLElement & { __zcodeLexicalInputE2E?: { getText(): string } }) | null;
  254. return element?.__zcodeLexicalInputE2E?.getText() === expected;
  255. }, text);
  256. await page
  257. .locator(`[data-queue-item-id="${item!.queueItemId}"][data-edit-locked="true"]`)
  258. .waitFor();
  259. const visual = await page.evaluate(finishQueueEditVisualProbe);
  260. await saveVisualEvidence(`mobile-${theme}`, visual);
  261. expectQueueFreezeEditWithoutFlash(visual, { initialComposerText: "", loadedText: text });
  262. // 桌面看到其他设备持有的锁,自己的草稿不变。
  263. await waitForV4QueueItemEditLock(
  264. item!.queueItemId,
  265. (state) => state.locked && state.owner === "other",
  266. "桌面没有显示手机持有的编辑锁",
  267. );
  268. expect(await getV4ComposerText()).toBe("desktop draft stays local");
  269. // 手机取消编辑释放锁,再删除本轮队列项,下一主题从单条队列重新开始。
  270. await page.getByTestId("v4-composer-edit-cancel").click();
  271. await waitForV4QueueItemEditLock(
  272. item!.queueItemId,
  273. (state) => state.exists && !state.locked,
  274. "手机取消后锁没有释放",
  275. );
  276. expect(await clickV4QueueItemDelete(item!.queueItemId)).toBe(true);
  277. await waitForV4QueueCount(0, 30000);
  278. await setV4ComposerText("");
  279. }
  280. } finally {
  281. await chrome.close();
  282. }
  283. });
  284. it("B09n:编辑中点取消 → 解锁并恢复原草稿,队列内容与位置不变", async () => {
  285. await prepareV4ConversationE2E();
  286. await sendV4Prompt("E2E_V4_QUEUE_SLOW 慢慢回答");
  287. await waitForV4TimelineContaining("V4_QUEUE_STREAMING", 45000);
  288. await waitForV4Pane(
  289. (s) => s.canStop && s.sessionId !== "draft" && s.sessionId !== null,
  290. "首轮没有进入 running(canStop)",
  291. 45000,
  292. );
  293. await sendV4Prompt("E2E_V4_QUEUE_ITEM 第一条问题");
  294. await waitForV4QueueCount(1, 30000);
  295. await sendV4Prompt("E2E_V4_QUEUE_ITEM 取消编辑的问题");
  296. await waitForV4QueueCount(2, 30000);
  297. const before = await getV4QueueItems();
  298. const target = before[1]!;
  299. await setV4ComposerText("保留现有草稿");
  300. expect(await clickV4QueueItemEdit(target.queueItemId)).toBe(true);
  301. await waitForV4ComposerEditState(
  302. (state) => state?.kind === "queueItem" && state.targetId === target.queueItemId,
  303. "queue 项没有进入 composer 编辑态",
  304. 30000,
  305. );
  306. await waitForV4ComposerText(
  307. "E2E_V4_QUEUE_ITEM 取消编辑的问题",
  308. "编辑态没有载入队列项原文",
  309. 15000,
  310. );
  311. await setV4ComposerText("E2E_V4_QUEUE_ITEM 改了但取消");
  312. await clickV4ComposerEditCancel();
  313. await waitForV4ComposerEditClosed("取消后没有退出编辑态");
  314. await waitForV4ComposerText("保留现有草稿", "取消后没有恢复原草稿", 15000);
  315. await waitForV4QueueItemEditLock(
  316. target.queueItemId,
  317. (state) => !state.locked && state.editAction && state.deleteAction,
  318. "取消后队列项没有解锁",
  319. );
  320. const after = await getV4QueueItems();
  321. expect(after.map((item) => item.queueItemId)).toEqual(before.map((item) => item.queueItemId));
  322. expect(after[1]!.text).toContain("取消编辑的问题");
  323. expect(after[1]!.text).not.toContain("改了但取消");
  324. });
  325. it("running 中入队两条 → 拖拽第二条到第一条 → 顺序互换(reorderQueueItem)", async () => {
  326. await prepareV4ConversationE2E();
  327. await sendV4Prompt("E2E_V4_QUEUE_SLOW 慢慢回答");
  328. await waitForV4TimelineContaining("V4_QUEUE_STREAMING", 45000);
  329. await waitForV4Pane(
  330. (s) => s.canStop && s.sessionId !== "draft" && s.sessionId !== null,
  331. "首轮没有进入 running(canStop)",
  332. 45000,
  333. );
  334. // running 中依次入队两条
  335. await sendV4Prompt("E2E_V4_QUEUE_ITEM 第一条问题");
  336. await waitForV4QueueCount(1, 30000);
  337. await sendV4Prompt("E2E_V4_QUEUE_ITEM 第二条问题");
  338. await waitForV4QueueCount(2, 30000);
  339. const before = await getV4QueueItems();
  340. expect(before).toHaveLength(2);
  341. expect(before[0]!.text).toContain("第一条问题");
  342. expect(before[1]!.text).toContain("第二条问题");
  343. const secondId = before[1]!.queueItemId;
  344. // 拖拽只发 reorderQueueItem;顺序更新必须来自 CLI projection 回流。
  345. await dragV4QueueItemOnto(secondId, before[0]!.queueItemId);
  346. await browser.waitUntil(
  347. async () => {
  348. const items = await getV4QueueItems();
  349. return items.length === 2 && items[0]!.queueItemId === secondId;
  350. },
  351. { timeout: 30000, timeoutMsg: "第二条没有拖拽到队首" },
  352. );
  353. const after = await getV4QueueItems();
  354. expect(after).toHaveLength(2);
  355. expect(after[0]!.queueItemId).toBe(secondId);
  356. expect(after[0]!.text).toContain("第二条问题");
  357. expect(after[1]!.text).toContain("第一条问题");
  358. });
  359. });
  360. const artifactDir = resolve(process.env.ZCODE_E2E_ARTIFACT_DIR ?? ".e2e-artifacts/queue-edit");
  361. async function saveVisualEvidence(label: string, samples: unknown) {
  362. await mkdir(artifactDir, { recursive: true });
  363. await writeFile(
  364. resolve(artifactDir, `queue-edit-${label}.json`),
  365. JSON.stringify(samples, null, 2),
  366. );
  367. }
  368. function setQueueTestTheme(theme: string) {
  369. const actions = (window as Window & { __testActions?: { setTheme(theme: string): void } })
  370. .__testActions;
  371. if (!actions) throw new Error("缺少主题测试入口");
  372. actions.setTheme(theme);
  373. }