conversation-session-queue-edit-freeze-mobile.test.ts 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. // B09j/B09l/B09m 排队消息冻结编辑的多端边界(docs/ui/composer-edit-mode.md)。
  2. // 证据层 L3:真实 Desktop + 真实手机 Web 配对,同一 CLI 队列投影。
  3. // 1) 手机端编辑:手机 composer 进入编辑态,桌面同一项显示「正在其他窗口或设备编辑」与「解除」;
  4. // 2) 队首冻结且空闲时,桌面新输入直接入队尾,不弹暂停队列确认(B09m);
  5. // 3) 桌面解除后,在线的手机端进入失效态(editLockLost),续跑从冻结项继续(B09j 在线);
  6. // 4) 暂停队列「清空队列并发送」删除冻结项,手机端进入失效态(queueItemRemoved)(B09l);
  7. // 5) 手机断开后锁保留,桌面手动解除,不依赖超时(B09j 离线)。
  8. // 6) 断开前刷新手机页面:沿用同一 editorId 并自动释放遗留锁;复制手机标签页:复制页经 Web Lock
  9. // 换新 editorId,不释放原页面的锁(B09k)。
  10. import { join } from "node:path";
  11. import { chromium, type Browser, type BrowserContext, type Page } from "playwright-core";
  12. import {
  13. TID_TASK_ITEM,
  14. TID_V4_COMPOSER_EDIT_BAR,
  15. TID_V4_COMPOSER_EDIT_CANCEL,
  16. TID_V4_QUEUE_ITEM_EDIT,
  17. TID_V4_QUEUE_ITEM_EDIT_LOCK,
  18. testId,
  19. } from "@zcode/shared/test-ids";
  20. import { clearAppData } from "#e2e/helpers/desktop-app";
  21. import { releaseControlledReplay } from "#e2e/helpers/controlled-replay";
  22. import { openMobilePairingPage, seedMobileFixtureLogin } from "#e2e/helpers/mobile-pairing";
  23. import { obtainUnreadPairingUrl } from "#e2e/helpers/web-remote-unread";
  24. import { expectNoUpstreamRequestForTextWithin } from "#e2e/helpers/conversation-session-network";
  25. import {
  26. clickV4QueueItemReleaseEdit,
  27. waitForV4QueueItemEditLock,
  28. } from "#e2e/helpers/v4-composer-edit";
  29. import {
  30. clickV4PausedQueueResume,
  31. clickV4Stop,
  32. getV4PaneSnapshot,
  33. getV4QueueItems,
  34. hasV4PausedQueueSendDialog,
  35. prepareV4ConversationE2E,
  36. sendV4PausedQueueClear,
  37. sendV4Prompt,
  38. startNewV4Draft,
  39. waitForV4Pane,
  40. waitForV4PausedQueueBanner,
  41. waitForV4QueueCount,
  42. waitForV4TimelineContaining,
  43. } from "#e2e/helpers/v4-conversation";
  44. const CASE_MARKER = "B09-MOBILE";
  45. interface MobileEditBar {
  46. kind: string | null;
  47. targetId: string | null;
  48. state: string | null;
  49. invalidReason: string | null;
  50. }
  51. describe("B09 排队消息冻结编辑:桌面与手机", () => {
  52. let webBrowser: Browser | undefined;
  53. let mobileContext: BrowserContext | undefined;
  54. let mobile: Page | undefined;
  55. after(async () => {
  56. if (mobile && process.env.ZCODE_E2E_ARTIFACT_DIR)
  57. await mobile
  58. .screenshot({ path: join(process.env.ZCODE_E2E_ARTIFACT_DIR, "B09-mobile.png") })
  59. .catch(() => undefined);
  60. await webBrowser?.close();
  61. const pane = await getV4PaneSnapshot().catch(() => null);
  62. if (pane?.canStop) await clickV4Stop().catch(() => undefined);
  63. await browser.electron.restoreAllMocks();
  64. await clearAppData();
  65. });
  66. it("手机编辑冻结 → 桌面入队尾 → 桌面解除 → 清空删除冻结项 → 手机断开后手动解除", async function () {
  67. this.timeout(360_000);
  68. await prepareV4ConversationE2E();
  69. await startNewV4Draft();
  70. await sendV4Prompt("E2E_QEFM_SLOW_1 慢慢回答");
  71. await waitForV4TimelineContaining("QEFM_1_STREAMING", 45_000);
  72. const running = await waitForV4Pane(
  73. (s) => s.canStop && s.sessionId !== null && s.sessionId !== "draft",
  74. "首轮没有进入 running",
  75. 45_000,
  76. );
  77. const taskId = running.sessionId!;
  78. await sendV4Prompt("E2E_QEFM_C 手机编辑的一条");
  79. await waitForV4QueueCount(1, 30_000);
  80. await sendV4Prompt("E2E_QEFM_D 之后的一条");
  81. await waitForV4QueueCount(2, 30_000);
  82. const [itemC, itemD] = await getV4QueueItems();
  83. if (!itemC || !itemD) throw new Error("队列项不完整");
  84. mobile = await openMobileTask(taskId);
  85. // 手机端编辑:手机进入编辑态,桌面显示其他设备持有的锁与「解除」。
  86. await mobileClick(mobile, testId(TID_V4_QUEUE_ITEM_EDIT, itemC.queueItemId));
  87. await waitForMobileEditBar(
  88. mobile,
  89. (bar) =>
  90. bar?.kind === "queueItem" && bar.targetId === itemC.queueItemId && bar.state === "editing",
  91. "手机端没有进入排队消息编辑态",
  92. );
  93. const remoteLock = await waitForV4QueueItemEditLock(
  94. itemC.queueItemId,
  95. (state) => state.locked && state.owner === "other",
  96. "桌面没有显示其他设备持有的编辑锁",
  97. );
  98. expect(remoteLock).toMatchObject({ releasable: true, editAction: false, deleteAction: false });
  99. // B09m:当前轮结束后队首冻结且空闲;桌面新输入直接入队尾,不弹暂停队列确认。
  100. await releaseControlledReplay("qefm-slow-1");
  101. await waitForV4Pane((s) => !s.canStop, "首轮结束后没有停在冻结队首前", 45_000);
  102. await expectNoUpstreamRequestForTextWithin("E2E_QEFM_C", 1_500);
  103. await sendV4Prompt("E2E_QEFM_E 冻结时的新输入");
  104. await waitForV4QueueCount(3, 30_000);
  105. expect(await hasV4PausedQueueSendDialog()).toBe(false);
  106. expect((await getV4QueueItems()).map((item) => item.queueItemId).slice(0, 2)).toEqual([
  107. itemC.queueItemId,
  108. itemD.queueItemId,
  109. ]);
  110. // B09j 在线:桌面解除后手机进入失效态,续跑从 C 继续,然后 D、E。
  111. await clickV4QueueItemReleaseEdit(itemC.queueItemId);
  112. // 解除后队首 C 立即被自动续跑提升并离开队列。手机只按当前投影派生失效原因:
  113. // 先收到解锁投影时为 editLockLost,解锁与提升合并到达时直接为 queueItemRemoved。
  114. // 两者都是正确终态(编辑失效、发送作为新消息),不断言中间态。
  115. await waitForMobileEditBar(
  116. mobile,
  117. (bar) =>
  118. bar?.state === "invalid" &&
  119. (bar.invalidReason === "editLockLost" || bar.invalidReason === "queueItemRemoved"),
  120. "桌面解除后手机端没有进入失效态",
  121. );
  122. await waitForV4TimelineContaining("QEFM_C_REPLY", 45_000);
  123. await waitForV4TimelineContaining("QEFM_D_REPLY", 45_000);
  124. await waitForV4TimelineContaining("QEFM_E_REPLY", 45_000);
  125. await waitForV4QueueCount(0, 30_000);
  126. await mobileClick(mobile, TID_V4_COMPOSER_EDIT_CANCEL);
  127. await waitForMobileEditBar(mobile, (bar) => bar === null, "手机端取消后没有退出编辑态");
  128. // B09l:Stop 形成暂停队列;手机编辑 F,桌面「清空队列并发送」删除 F,手机进入失效态。
  129. await sendV4Prompt("E2E_QEFM_SLOW_2 慢慢回答");
  130. await waitForV4TimelineContaining("QEFM_2_STREAMING", 45_000);
  131. await waitForV4Pane((s) => s.canStop, "第二轮没有进入 running", 45_000);
  132. await sendV4Prompt("E2E_QEFM_F 将被清空的一条");
  133. await waitForV4QueueCount(1, 30_000);
  134. const [itemF] = await getV4QueueItems();
  135. if (!itemF) throw new Error("F 队列项不存在");
  136. await clickV4Stop();
  137. await waitForV4Pane((s) => !s.canStop, "Stop 后没有收口", 30_000);
  138. await mobileClick(mobile, testId(TID_V4_QUEUE_ITEM_EDIT, itemF.queueItemId));
  139. await waitForMobileEditBar(
  140. mobile,
  141. (bar) => bar?.targetId === itemF.queueItemId && bar.state === "editing",
  142. "手机端没有进入 F 的编辑态",
  143. );
  144. await waitForV4QueueItemEditLock(
  145. itemF.queueItemId,
  146. (state) => state.locked && state.owner === "other",
  147. "桌面没有显示 F 的远端编辑锁",
  148. );
  149. await sendV4PausedQueueClear("E2E_QEFM_H 清空后发送");
  150. await waitForV4TimelineContaining("QEFM_H_REPLY", 45_000);
  151. await waitForV4QueueCount(0, 30_000);
  152. await waitForMobileEditBar(
  153. mobile,
  154. (bar) => bar?.state === "invalid" && bar.invalidReason === "queueItemRemoved",
  155. "冻结项被清空后手机端没有进入失效态",
  156. );
  157. await mobileClick(mobile, TID_V4_COMPOSER_EDIT_CANCEL);
  158. await waitForMobileEditBar(mobile, (bar) => bar === null, "手机端取消后没有退出编辑态");
  159. // B09j 离线:手机编辑 J 后断开,锁保留;桌面手动解除后续跑继续。
  160. await sendV4Prompt("E2E_QEFM_SLOW_3 慢慢回答");
  161. await waitForV4TimelineContaining("QEFM_3_STREAMING", 45_000);
  162. await waitForV4Pane((s) => s.canStop, "第三轮没有进入 running", 45_000);
  163. await sendV4Prompt("E2E_QEFM_J 断开时冻结的一条");
  164. await waitForV4QueueCount(1, 30_000);
  165. const [itemJ] = await getV4QueueItems();
  166. if (!itemJ) throw new Error("J 队列项不存在");
  167. await mobileClick(mobile, testId(TID_V4_QUEUE_ITEM_EDIT, itemJ.queueItemId));
  168. await waitForV4QueueItemEditLock(
  169. itemJ.queueItemId,
  170. (state) => state.locked && state.owner === "other",
  171. "桌面没有显示 J 的远端编辑锁",
  172. );
  173. // B09k(刷新):手机页面刷新后沿用同一 editorId(Web Lock 随旧文档卸载释放,新文档能重新取得),
  174. // 发现自己的遗留锁并自动释放;随后重新进入编辑,供下面的复制标签页与断开步骤使用。
  175. const editorIdKey = "zcode-v4-editor-id:v1";
  176. const editorIdBeforeReload = await mobile!.evaluate(
  177. (key) => sessionStorage.getItem(key),
  178. editorIdKey,
  179. );
  180. await mobile!.reload();
  181. // 刷新可能直接恢复到原任务,也可能回到任务首页;回到首页时点进同一任务。
  182. const reloadedSession = mobile!.locator(`[data-session-id="${taskId}"]`);
  183. const reloadedTaskItem = mobile!.getByTestId(testId(TID_TASK_ITEM, taskId)).first();
  184. await reloadedSession.or(reloadedTaskItem).first().waitFor({ timeout: 45_000 });
  185. if (!(await reloadedSession.isVisible())) {
  186. await reloadedTaskItem.tap();
  187. await reloadedSession.waitFor();
  188. }
  189. await waitForV4QueueItemEditLock(
  190. itemJ.queueItemId,
  191. (state) => state.exists && !state.locked,
  192. "手机刷新后没有自动释放自己的遗留锁",
  193. );
  194. expect(await mobile!.evaluate((key) => sessionStorage.getItem(key), editorIdKey)).toBe(
  195. editorIdBeforeReload,
  196. );
  197. await mobileClick(mobile!, testId(TID_V4_QUEUE_ITEM_EDIT, itemJ.queueItemId));
  198. await waitForV4QueueItemEditLock(
  199. itemJ.queueItemId,
  200. (state) => state.locked && state.owner === "other",
  201. "手机刷新后重新编辑 J 没有加锁",
  202. );
  203. // B09k(复制标签页):浏览器复制标签页会连同 sessionStorage 复制 editorId。复制页不能把原页面
  204. // 正在编辑的锁当成自己的遗留锁释放;它经 Web Lock 查重后换新 id,并把该锁显示为可解除的其他端锁。
  205. // 远控同一设备只保留一个在线控制端,复制页会接管连接;原页面随后在本步骤断开,不再对它断言。
  206. const sharedEditorId = await mobile!.evaluate(
  207. (key) => sessionStorage.getItem(key),
  208. editorIdKey,
  209. );
  210. expect(sharedEditorId).toBeTruthy();
  211. const duplicate = await mobileContext!.newPage();
  212. await duplicate.addInitScript(
  213. ([key, value]) => {
  214. if (!sessionStorage.getItem(key)) sessionStorage.setItem(key, value);
  215. },
  216. [editorIdKey, sharedEditorId!] as const,
  217. );
  218. // 手机 URL 不含任务路由(任务由页内点击打开),复制页与原页一样从任务首页点进同一任务。
  219. await duplicate.goto(mobile!.url());
  220. await duplicate.getByTestId(testId(TID_TASK_ITEM, taskId)).first().tap();
  221. const duplicateLock = duplicate.getByTestId(
  222. testId(TID_V4_QUEUE_ITEM_EDIT_LOCK, itemJ.queueItemId),
  223. );
  224. await duplicateLock.waitFor({ timeout: 45_000 });
  225. expect(await duplicateLock.getAttribute("data-edit-lock-owner")).toBe("other");
  226. await duplicate.waitForFunction(
  227. ([key, shared]) => {
  228. const current = sessionStorage.getItem(key);
  229. return current !== null && current !== shared;
  230. },
  231. [editorIdKey, sharedEditorId!] as const,
  232. );
  233. await mobileContext?.close();
  234. mobileContext = undefined;
  235. mobile = undefined;
  236. // B09l 的 Stop 让 autoDrain=false,「清空队列并发送」不改变它;第三轮结束后队列暂停。
  237. await releaseControlledReplay("qefm-slow-3");
  238. await waitForV4Pane((s) => !s.canStop, "第三轮没有收口", 45_000);
  239. await waitForV4PausedQueueBanner(30_000);
  240. await expectNoUpstreamRequestForTextWithin("E2E_QEFM_J", 1_500);
  241. // 不使用超时释放:手机断开后锁仍在,必须由用户手动解除。
  242. const stillLocked = await waitForV4QueueItemEditLock(
  243. itemJ.queueItemId,
  244. (state) => state.locked && state.owner === "other" && state.releasable,
  245. "手机断开后锁没有保留",
  246. );
  247. expect(stillLocked.lockText.length).toBeGreaterThan(0);
  248. await clickV4QueueItemReleaseEdit(itemJ.queueItemId);
  249. await waitForV4QueueItemEditLock(
  250. itemJ.queueItemId,
  251. (state) => state.exists && !state.locked && state.editAction,
  252. "桌面手动解除后锁没有消失",
  253. );
  254. await clickV4PausedQueueResume();
  255. await waitForV4TimelineContaining("QEFM_J_REPLY", 45_000);
  256. await waitForV4QueueCount(0, 30_000);
  257. });
  258. async function openMobileTask(taskId: string): Promise<Page> {
  259. const pairingUrl = await obtainUnreadPairingUrl(
  260. new URL(process.env.ZCODE_WEB_REMOTE_CONTROL_URL!).origin,
  261. );
  262. webBrowser = await chromium.launch({
  263. ...(process.env.ZCODE_E2E_MOBILE_BROWSER_EXECUTABLE
  264. ? { executablePath: process.env.ZCODE_E2E_MOBILE_BROWSER_EXECUTABLE }
  265. : { channel: "chrome" }),
  266. headless: true,
  267. args: process.env.ZCODE_E2E_BROWSER_NO_PROXY === "1" ? ["--no-proxy-server"] : [],
  268. });
  269. mobileContext = await webBrowser.newContext({
  270. viewport: { width: 390, height: 844 },
  271. isMobile: true,
  272. hasTouch: true,
  273. locale: "zh-CN",
  274. });
  275. await seedMobileFixtureLogin(mobileContext);
  276. const page = await mobileContext.newPage();
  277. await openMobilePairingPage(page, pairingUrl, {
  278. artifactDir: process.env.ZCODE_E2E_ARTIFACT_DIR!,
  279. caseMarker: CASE_MARKER,
  280. phase: "pair",
  281. readySelector: `[data-testid="${testId(TID_TASK_ITEM, taskId)}"]`,
  282. });
  283. await page.getByTestId(testId(TID_TASK_ITEM, taskId)).first().tap();
  284. await page.locator(`[data-session-id="${taskId}"]`).waitFor();
  285. return page;
  286. }
  287. });
  288. /** 队列行操作在触屏上常显;DOM click 与桌面 clickTestIdByDom 一样走 React onClick。 */
  289. async function mobileClick(page: Page, testIdValue: string) {
  290. await page.waitForFunction(
  291. (value) => {
  292. const button = document.querySelector<HTMLButtonElement>(`[data-testid="${value}"]`);
  293. if (!button || button.disabled) return false;
  294. button.click();
  295. return true;
  296. },
  297. testIdValue,
  298. { timeout: 30_000 },
  299. );
  300. }
  301. function readMobileEditBar(page: Page): Promise<MobileEditBar | null> {
  302. return page.evaluate((barTestId) => {
  303. const bar = document.querySelector<HTMLElement>(`[data-testid="${barTestId}"]`);
  304. if (!bar) return null;
  305. return {
  306. kind: bar.getAttribute("data-edit-target-kind"),
  307. targetId: bar.getAttribute("data-edit-target-id"),
  308. state: bar.getAttribute("data-edit-state"),
  309. invalidReason: bar.getAttribute("data-edit-invalid-reason"),
  310. };
  311. }, TID_V4_COMPOSER_EDIT_BAR);
  312. }
  313. async function waitForMobileEditBar(
  314. page: Page,
  315. predicate: (bar: MobileEditBar | null) => boolean,
  316. timeoutMsg: string,
  317. ) {
  318. let latest: MobileEditBar | null = null;
  319. try {
  320. await browser.waitUntil(
  321. async () => {
  322. latest = await readMobileEditBar(page);
  323. return predicate(latest);
  324. },
  325. { timeout: 30_000, timeoutMsg },
  326. );
  327. } catch (error) {
  328. throw new Error(`${timeoutMsg}: ${JSON.stringify(latest)}`, { cause: error });
  329. }
  330. }