browser-design-mode-cross-session.test.ts 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. import type { Server } from "node:http";
  2. import { TID_BROWSER_DESIGN_TOOLBAR, TID_TASK_ITEM, testId } from "@zcode/shared/test-ids";
  3. import { clearAppData, clickTestIdByDom } from "#e2e/helpers/desktop-app";
  4. import { openFirstBrowserTab } from "#e2e/helpers/browser-side-pane";
  5. import {
  6. enableDesignPlugin,
  7. closeFixtureServer,
  8. countAttachments,
  9. navigateBrowser,
  10. countElementTokens,
  11. deleteLastElementToken,
  12. selectGuestElement,
  13. waitForElementTokens,
  14. startFixtureServer,
  15. waitForReadyAttachment,
  16. } from "#e2e/helpers/browser-design-mode";
  17. import { waitForUpstreamRequest } from "#e2e/helpers/conversation-session-network";
  18. import {
  19. getV4PaneSnapshot,
  20. prepareV4ConversationE2E,
  21. sendV4Prompt,
  22. startNewV4Draft,
  23. waitForV4Pane,
  24. } from "#e2e/helpers/v4-conversation";
  25. // BU-E2E-022(docs/browser-use/2026-11-03-browser-design-mode-spec.md 第四节):
  26. // 点选元素的引用截图跟着引用走:同一个输入框在会话间切换时不重复、移除引用后不残留。
  27. // 会话 A 点选 → 新建会话 B(B 里没有)→ 切回 A:仍是 1 个元素引用 + 1 张 element-1.png;
  28. // 再来回切一次结果相同;在 A 移除引用后截图也没有。A 再发一轮时 ambient 块声明设计模式可用(桌面输入)。
  29. // 分屏时两个输入框各自挂着图片附件 hook,不走这里的切换路径(见覆盖矩阵 BU-E2E-022 说明)。
  30. const PIN_SHOT = "element-1.png";
  31. const FIXTURE_HTML = `<!doctype html>
  32. <html data-zcode-design><head><title>E2E design cross session</title></head>
  33. <body><button id="cta">E2E_CROSS_SESSION_CTA</button></body></html>`;
  34. let fixtureServer: Server | null = null;
  35. let fixtureOrigin = "";
  36. describe("BU-E2E-022:设计工具点选引用的截图跨会话切换不重复、不残留", () => {
  37. before(async () => {
  38. ({ server: fixtureServer, origin: fixtureOrigin } = await startFixtureServer(
  39. (_request, response) => {
  40. response.setHeader("content-type", "text/html; charset=utf-8");
  41. response.end(FIXTURE_HTML);
  42. },
  43. ));
  44. });
  45. after(async () => {
  46. await closeFixtureServer(fixtureServer);
  47. fixtureServer = null;
  48. await clearAppData();
  49. });
  50. it("A 点选,切到 B 再切回 A 截图只有一张;移除引用后截图全部撤掉", async function () {
  51. this.timeout(300_000);
  52. await prepareV4ConversationE2E();
  53. await enableDesignPlugin();
  54. // 会话 A:先发一轮,得到侧栏里能切回的正式会话。
  55. const sessionA = await startSessionWithOneTurn("E2E_DESIGN_CROSS_SESSION_A");
  56. await openFirstBrowserTab();
  57. await navigateBrowser(`${fixtureOrigin}/design`, "E2E design cross session", fixtureOrigin);
  58. await waitForDesignToolbar();
  59. // A 点选:1 个元素引用 + element-1.png。
  60. await selectGuestElement(fixtureOrigin, "#cta");
  61. await waitForElementTokens(1, "会话 A 点选后输入框里没有元素 token");
  62. await waitForReadyAttachment(PIN_SHOT);
  63. // 新建会话 B:B 里没有这个引用。
  64. await startNewV4Draft();
  65. const sessionB = await startSessionWithOneTurn("E2E_DESIGN_CROSS_SESSION_B");
  66. await expectComposer({ contexts: null, shots: 0 }, "新建会话 B 后");
  67. // 切回 A:仍是 1 条上下文 + 1 张截图,不重复;来回再切一次也一样。
  68. await openSession(sessionA);
  69. await expectComposer({ contexts: "1", shots: 1 }, "切回会话 A 后(不应重复)");
  70. await openSession(sessionB);
  71. await expectComposer({ contexts: null, shots: 0 }, "再切到会话 B");
  72. await openSession(sessionA);
  73. await expectComposer({ contexts: "1", shots: 1 }, "第二次切回会话 A(不应重复)");
  74. // 在 A 的输入框里移除引用:引用和截图一起撤掉,不留任何一张(含切换中多出来的)。
  75. await deleteLastElementToken();
  76. await expectComposer({ contexts: null, shots: 0 }, "在 A 移除引用后(不应留孤儿截图)");
  77. // 桌面输入时 ambient 块声明设计模式可用(Host 按 clientMode 注入 inputOrigin desktop,
  78. // 手机远控同一会话不带这一行)。A 的浏览器 tab 归属 A,A 的下一轮 sendText 会带 ambient 块。
  79. await sendV4Prompt('E2E_DESIGN_AMBIENT_DESKTOP: Reply with exactly "upstream-e2e-ok".');
  80. await waitForUpstreamRequest(
  81. {
  82. includes: [
  83. "E2E_DESIGN_AMBIENT_DESKTOP",
  84. "- The user has the in-app browser open with",
  85. "- Design mode: available.",
  86. ],
  87. },
  88. "桌面输入的上游请求里没有内置浏览器 ambient 块或「Design mode: available」",
  89. 45_000,
  90. );
  91. });
  92. });
  93. async function startSessionWithOneTurn(marker: string): Promise<string> {
  94. await sendV4Prompt(`${marker}: Reply with exactly "upstream-e2e-ok".`);
  95. await waitForV4Pane(
  96. (snapshot) =>
  97. snapshot.sessionId !== "draft" &&
  98. !snapshot.canStop &&
  99. snapshot.timelineText.includes(marker) &&
  100. snapshot.timelineText.includes("upstream-e2e-ok"),
  101. `${marker} 的第一轮没有完成`,
  102. 60_000,
  103. );
  104. const sessionId = (await getV4PaneSnapshot()).sessionId;
  105. if (!sessionId || sessionId === "draft") throw new Error(`${marker} 没有拿到 sessionId`);
  106. return sessionId;
  107. }
  108. async function waitForDesignToolbar(): Promise<void> {
  109. await $(`[data-testid="${TID_BROWSER_DESIGN_TOOLBAR}"]`).waitForDisplayed({
  110. timeout: 15_000,
  111. timeoutMsg: "设计稿页面没有出现设计工具行",
  112. });
  113. }
  114. async function openSession(sessionId: string): Promise<void> {
  115. await clickTestIdByDom(testId(TID_TASK_ITEM, sessionId), {
  116. timeout: 15_000,
  117. timeoutMsg: `侧栏没有会话 ${sessionId}`,
  118. });
  119. await waitForV4Pane((snapshot) => snapshot.sessionId === sessionId, `没有切回会话 ${sessionId}`);
  120. }
  121. /**
  122. * 稳定 3 秒后读输入框:上下文条数(data-count,没有时 null)与 design-pin-1.png 附件张数。
  123. * 先等到期望值出现,再停 3 秒复读,确认不是切换过程中的瞬时值(重复附件是异步加上的)。
  124. */
  125. async function expectComposer(
  126. expected: { contexts: string | null; shots: number },
  127. label: string,
  128. ): Promise<void> {
  129. const read = async () => ({
  130. contexts: (await countElementTokens()) === 0 ? null : String(await countElementTokens()),
  131. shots: await countAttachments(PIN_SHOT),
  132. });
  133. let last = await read();
  134. try {
  135. await browser.waitUntil(
  136. async () => {
  137. last = await read();
  138. return last.contexts === expected.contexts && last.shots === expected.shots;
  139. },
  140. { timeout: 15_000 },
  141. );
  142. await browser.pause(3_000);
  143. last = await read();
  144. } catch {
  145. // 落到下面统一报错
  146. }
  147. if (last.contexts !== expected.contexts || last.shots !== expected.shots) {
  148. throw new Error(
  149. `${label}:期望上下文 ${expected.contexts ?? "无"} 条、${PIN_SHOT} ${expected.shots} 张;实际上下文 ${last.contexts ?? "无"}、截图 ${last.shots} 张`,
  150. );
  151. }
  152. }