side-pane-full-view.test.ts 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763
  1. import { join } from "node:path";
  2. import {
  3. TID_SIDE_PANE_FULL_VIEW_BUBBLE,
  4. TID_SIDE_PANE_FULL_VIEW_CONVERSATION_TAB,
  5. TID_SIDE_PANE_FULL_VIEW_DRAWER,
  6. TID_SIDE_PANE_FULL_VIEW_EXPAND,
  7. TID_SIDE_PANE_FULL_VIEW_HANDLE,
  8. TID_SIDE_PANE_FULL_VIEW_HEADER,
  9. TID_SIDE_PANE_FULL_VIEW_HOST,
  10. TID_SIDE_PANE_FULL_VIEW_MENU_OPEN_CONVERSATION,
  11. TID_SIDE_PANE_FULL_VIEW_MINIMIZE,
  12. TID_SIDE_PANE_FULL_VIEW_RUNNING,
  13. TID_SIDE_PANE_FULL_VIEW_TOGGLE,
  14. TID_SIDE_PANE_TOGGLE,
  15. TID_COMPOSER_WORKSPACE_TRIGGER,
  16. TID_V4_COMPOSER_INPUT,
  17. TID_V4_COMPOSER_SEND,
  18. TID_V4_SESSION_PANE,
  19. TID_V4_TIMELINE,
  20. TID_WORKSPACE_HEADER,
  21. testId,
  22. } from "@zcode/shared/test-ids";
  23. import { clearAppData } from "#e2e/helpers/desktop-app";
  24. import { prepareV4ConversationE2E } from "#e2e/helpers/v4-conversation";
  25. // spec:docs/ui/side-pane-full-view.md;用例:docs/testing/side-pane-tabs-e2e-case-catalog.md(SPFV)
  26. const SIDE_FRAME = '[data-workspace-side-frame="true"]';
  27. const SLOT = `${SIDE_FRAME} [data-side-pane-content-slot]`;
  28. const HOST = `[data-testid="${TID_SIDE_PANE_FULL_VIEW_HOST}"]`;
  29. const MAIN_PANE = `[data-testid="${testId(TID_V4_SESSION_PANE, "workspace-main")}"]`;
  30. const COMPOSER_INPUT = `${HOST} [data-testid="${TID_V4_COMPOSER_INPUT}"]`;
  31. const COMPOSER_DOCK = `${HOST} [data-v4-composer-dock="true"]`;
  32. const COMPOSER_SHELL = `${HOST} [data-composer-layout]`;
  33. const COMPOSER_ACTION_MENU = `${HOST} [data-composer-action-menu] button`;
  34. const COMPOSER_SEND = `${HOST} [data-testid="${TID_V4_COMPOSER_SEND}"]`;
  35. const COMPOSER_PROJECT_TRIGGER = `${HOST} [data-testid="${TID_COMPOSER_WORKSPACE_TRIGGER}"]`;
  36. const EDITOR_MARKER = "spfv-editor-instance";
  37. const DRAWER = `[data-testid="${TID_SIDE_PANE_FULL_VIEW_DRAWER}"]`;
  38. const EXPAND = `[data-testid="${TID_SIDE_PANE_FULL_VIEW_EXPAND}"]`;
  39. const TAB = "[data-side-pane-tab-id]";
  40. const PANE_MARKER = "spfv-single-instance";
  41. const DRAFT_TEXT = "E2E_SPFV_FLOATING_DRAFT";
  42. const MARGIN = 16;
  43. interface Rect {
  44. left: number;
  45. top: number;
  46. right: number;
  47. bottom: number;
  48. width: number;
  49. height: number;
  50. }
  51. interface HostMotionProbe {
  52. transitions: string[];
  53. firstFloatingFrame: Rect | null;
  54. stop: () => void;
  55. }
  56. describe("Side Pane 完整视图(SPFV)", () => {
  57. before(async () => {
  58. await prepareV4ConversationE2E({ skipProvider: true });
  59. // 运行日志确认冷启预热回填会重新同步面板 scope;完整视图用例先等待投影就绪。
  60. await browser.waitUntil(
  61. () =>
  62. browser.execute(() => {
  63. const pane = document.querySelector('[data-session-id="draft"]');
  64. return (
  65. pane?.getAttribute("data-session-lifecycle") === "ready" &&
  66. Boolean(pane.getAttribute("data-projection-seq"))
  67. );
  68. }),
  69. { timeout: 30_000, timeoutMsg: "完整视图用例的草稿投影尚未就绪" },
  70. );
  71. await browser.execute(() =>
  72. window.localStorage.removeItem("zcode:side-pane-full-view:floating-anchor"),
  73. );
  74. });
  75. after(async () => {
  76. await browser.electron.restoreAllMocks();
  77. await clearAppData();
  78. });
  79. it("SPFV-E2E-001 进入、退出且不重新挂载对话", async () => {
  80. await ensureSidePaneExpanded();
  81. await ensureTerminalTab();
  82. const before = await readLayoutWidths();
  83. await markMainPane();
  84. await clickFullViewToggle();
  85. await waitForHostMode("floating");
  86. const entered = await readLayoutWidths();
  87. // 完整视图保留左侧任务列表与左上角按钮组。
  88. expect(Math.abs(entered.sidebar - before.sidebar)).toBeLessThanOrEqual(2);
  89. expect(await $('[data-testid="desktop-top-nav-back"]').isExisting()).toBe(true);
  90. // tab 栏是窗口拖拽区;对话 tab 不是 no-drag 时真实鼠标点击会被当成拖动窗口(CDP 点击测不出来)。
  91. expect(
  92. await browser.execute(
  93. (tabId) =>
  94. getComputedStyle(document.querySelector(`[data-testid="${tabId}"]`)!).getPropertyValue(
  95. "-webkit-app-region",
  96. ),
  97. TID_SIDE_PANE_FULL_VIEW_CONVERSATION_TAB,
  98. ),
  99. ).toBe("no-drag");
  100. expect(await $(`[data-testid="${TID_WORKSPACE_HEADER}"]`).isExisting()).toBe(false);
  101. expect(entered.content - entered.sideFrame).toBeLessThanOrEqual(16);
  102. expect(
  103. await browser.execute(
  104. (conversationTabId, tabSelector) => {
  105. const conversationTab = document.querySelector(`[data-testid="${conversationTabId}"]`);
  106. const firstPaneTab = document.querySelector(tabSelector);
  107. return Boolean(
  108. conversationTab &&
  109. firstPaneTab &&
  110. conversationTab.compareDocumentPosition(firstPaneTab) &
  111. Node.DOCUMENT_POSITION_FOLLOWING,
  112. );
  113. },
  114. TID_SIDE_PANE_FULL_VIEW_CONVERSATION_TAB,
  115. TAB,
  116. ),
  117. ).toBe(true);
  118. // 侧栏收起时,tab 栏按 WorkspaceHeader 的规则给左上角按钮组让位,对话 tab 不被按钮盖住。
  119. await realKeys(["Control", "b"]);
  120. await browser.waitUntil(async () => (await readLayoutWidths()).sidebar <= 8, {
  121. timeout: 5_000,
  122. timeoutMsg: "Cmd/Ctrl+B 后侧栏没有收起",
  123. });
  124. await browser.waitUntil(
  125. async () =>
  126. (await readRect(`[data-testid="${TID_SIDE_PANE_FULL_VIEW_CONVERSATION_TAB}"]`)).left >
  127. (await readRect('[data-testid="desktop-top-nav-back"]')).right + 24,
  128. { timeout: 5_000, timeoutMsg: "侧栏收起后 tab 栏没有给左上角按钮组让位" },
  129. );
  130. await realKeys(["Control", "b"]);
  131. await browser.waitUntil(
  132. async () => Math.abs((await readLayoutWidths()).sidebar - before.sidebar) <= 2,
  133. { timeout: 5_000, timeoutMsg: "Cmd/Ctrl+B 后侧栏没有恢复" },
  134. );
  135. await realKeys(["Control", "Shift", "f"]);
  136. await waitForHostMode("normal");
  137. await browser.waitUntil(
  138. async () => Math.abs((await readLayoutWidths()).sidebar - before.sidebar) <= 2,
  139. { timeout: 5_000, timeoutMsg: "退出完整视图后左侧任务列表没有恢复原宽度" },
  140. );
  141. const exited = await readLayoutWidths();
  142. expect(Math.abs(exited.sidebar - before.sidebar)).toBeLessThanOrEqual(2);
  143. expect(Math.abs(exited.sideFrame - before.sideFrame)).toBeLessThanOrEqual(2);
  144. expect(await $(`[data-testid="${TID_WORKSPACE_HEADER}"]`).isExisting()).toBe(true);
  145. await clickFullViewToggle();
  146. await waitForHostMode("floating");
  147. expect(await readMainPaneMarker()).toBe(PANE_MARKER);
  148. });
  149. it("SPFV-E2E-002 对话 tab 全宽,面板 tab 显示浮窗", async () => {
  150. await $(`[data-testid="${TID_SIDE_PANE_FULL_VIEW_CONVERSATION_TAB}"]`).click();
  151. await waitForHostMode("conversation");
  152. const slot = await readRect(SLOT);
  153. await browser.waitUntil(async () => rectsClose(await readRect(HOST), slot, 2), {
  154. timeout: 5_000,
  155. timeoutMsg: "对话 tab 激活后宿主没有铺满面板内容区",
  156. });
  157. expect(await $(`[data-testid="${TID_SIDE_PANE_FULL_VIEW_HEADER}"]`).isExisting()).toBe(false);
  158. // 对话 tab 保持普通两行布局,草稿顶部仍有「选择项目」栏。
  159. expect(await $(COMPOSER_SHELL).getAttribute("data-composer-layout")).toBe("stacked");
  160. expect(await $(COMPOSER_PROJECT_TRIGGER).isExisting()).toBe(true);
  161. await markComposerEditor();
  162. await startHostMotionProbe();
  163. await $(TAB).click();
  164. await waitForHostMode("floating");
  165. const motion = await finishHostMotionProbe();
  166. expect(motion.transitions).toEqual([]);
  167. if (!motion.firstFloatingFrame) throw new Error("未采到切换 tab 后的首个浮窗绘制帧");
  168. expect(rectsClose(motion.firstFloatingFrame, await readRect(HOST), 1)).toBe(true);
  169. await browser.waitUntil(
  170. async () => {
  171. const host = await readRect(HOST);
  172. const currentSlot = await readRect(SLOT);
  173. return (
  174. Math.abs(currentSlot.right - MARGIN - host.right) <= 2 &&
  175. Math.abs(currentSlot.bottom - MARGIN - host.bottom) <= 2 &&
  176. host.height < 320
  177. );
  178. },
  179. { timeout: 5_000, timeoutMsg: "浮窗没有收起在右下角" },
  180. );
  181. // 收起态没有外层:输入区与浮窗同宽、底边对齐,抽屉藏在输入框后面。
  182. const host = await readRect(HOST);
  183. const dock = await readRect(COMPOSER_DOCK);
  184. expect(Math.abs(dock.width - host.width)).toBeLessThanOrEqual(1);
  185. expect(Math.abs(dock.bottom - host.bottom)).toBeLessThanOrEqual(1);
  186. expect(await $(DRAWER).getAttribute("data-open")).toBe("false");
  187. // 浮窗默认是一条:[+] 输入区 [控件][发送] 在同一行,没有草稿项目栏。
  188. expect(await $(COMPOSER_SHELL).getAttribute("data-composer-layout")).toBe("inline");
  189. expect(await $(COMPOSER_PROJECT_TRIGGER).isExisting()).toBe(false);
  190. await browser.waitUntil(async () => (await readRect(HOST)).height <= 64, {
  191. timeout: 5_000,
  192. timeoutMsg: "单行浮窗高度没有收到一行",
  193. });
  194. const plus = await readRect(COMPOSER_ACTION_MENU);
  195. const input = await readRect(COMPOSER_INPUT);
  196. const send = await readRect(COMPOSER_SEND);
  197. expect(plus.right).toBeLessThanOrEqual(input.left);
  198. expect(input.right).toBeLessThanOrEqual(send.left);
  199. expect(Math.abs(centerY(plus) - centerY(send))).toBeLessThanOrEqual(2);
  200. expect(Math.abs(centerY(input) - centerY(send))).toBeLessThanOrEqual(2);
  201. expect(input.width).toBeGreaterThanOrEqual((await readRect(COMPOSER_SHELL)).width / 2 - 16);
  202. // 布局切换只换样式,编辑器节点不重新挂载。
  203. expect(await readComposerEditorMarker()).toBe(EDITOR_MARKER);
  204. });
  205. it("SPFV-E2E-003 操作栏、最小化与恢复共享同一份草稿", async () => {
  206. await focusFloatingComposer();
  207. await waitForHeader(true);
  208. // 抽屉从输入框后面滑出:操作栏只有按钮,抽屉一直延伸到输入框底边,两侧没有凹口。
  209. expect(await $(DRAWER).getAttribute("data-open")).toBe("true");
  210. expect(await $(`[data-testid="${TID_SIDE_PANE_FULL_VIEW_HEADER}"] span`).isExisting()).toBe(
  211. false,
  212. );
  213. // 草稿空闲:操作栏不显示运行中的 loading。
  214. expect(await $(`[data-testid="${TID_SIDE_PANE_FULL_VIEW_RUNNING}"]`).isExisting()).toBe(false);
  215. await browser.waitUntil(
  216. async () =>
  217. Math.abs((await readRect(DRAWER)).bottom - (await readRect(COMPOSER_DOCK)).bottom) <= 1.5,
  218. { timeout: 5_000, timeoutMsg: "抽屉没有延伸到输入框底边" },
  219. );
  220. await browser.keys(DRAFT_TEXT);
  221. await waitForComposerText(COMPOSER_INPUT, DRAFT_TEXT);
  222. // 面板 tab trigger 不抢焦点;点进面板内容区才会让焦点离开浮窗。
  223. await clickSlotTopLeft();
  224. await waitForHeader(false);
  225. await focusFloatingComposer();
  226. await waitForHeader(true);
  227. await $(`[data-testid="${TID_SIDE_PANE_FULL_VIEW_MINIMIZE}"]`).click();
  228. const bubble = await $(`[data-testid="${TID_SIDE_PANE_FULL_VIEW_BUBBLE}"]`);
  229. await bubble.waitForDisplayed({ timeout: 5_000 });
  230. await browser.waitUntil(async () => (await readRect(HOST)).width <= 52, {
  231. timeout: 5_000,
  232. timeoutMsg: "最小化后浮窗没有收成圆形按钮",
  233. });
  234. // 恢复动画逐帧采样:浮窗与输入区高度不能先变高再缩回(回归:宽度过渡时单行输入框折行)。
  235. await browser.execute(
  236. (hostSelector, dockSelector) => {
  237. const samples: Array<{ host: number; dock: number }> = [];
  238. (window as unknown as { __spfvRestoreSamples: typeof samples }).__spfvRestoreSamples =
  239. samples;
  240. const probe = window as unknown as { __spfvRestoreComplete: boolean };
  241. probe.__spfvRestoreComplete = false;
  242. let start = 0;
  243. const tick = () => {
  244. const host = document.querySelector(hostSelector)?.getBoundingClientRect();
  245. const dock = document.querySelector(dockSelector)?.getBoundingClientRect();
  246. samples.push({ host: host?.height ?? 0, dock: dock?.height ?? 0 });
  247. if (performance.now() - start < 500) requestAnimationFrame(tick);
  248. else probe.__spfvRestoreComplete = true;
  249. };
  250. document.addEventListener(
  251. "pointerdown",
  252. () => {
  253. // 采样窗口从真实按下开始,排除 WebDriver 命令往返时间。
  254. start = performance.now();
  255. requestAnimationFrame(tick);
  256. },
  257. { capture: true, once: true },
  258. );
  259. },
  260. HOST,
  261. COMPOSER_DOCK,
  262. );
  263. await bubble.click();
  264. await bubble.waitForExist({ reverse: true, timeout: 5_000 });
  265. await waitForHeader(true);
  266. await browser.waitUntil(
  267. () =>
  268. browser.execute(
  269. (selector) => Boolean(document.activeElement?.closest(selector)),
  270. COMPOSER_INPUT,
  271. ),
  272. { timeout: 5_000, timeoutMsg: "恢复后输入框没有获得焦点" },
  273. );
  274. await waitForComposerText(COMPOSER_INPUT, DRAFT_TEXT);
  275. // 日志中 500ms 只获得 8 帧;几何回归应等待采样完成,不用宿主刷新率作为前置条件。
  276. await browser.waitUntil(
  277. async () =>
  278. await browser.execute(
  279. () => (window as unknown as { __spfvRestoreComplete: boolean }).__spfvRestoreComplete,
  280. ),
  281. { timeout: 5_000, timeoutMsg: "恢复动画采样未完成" },
  282. );
  283. await waitForHostTransitionsSettled();
  284. const finalHost = await readRect(HOST);
  285. const finalDock = await readRect(COMPOSER_DOCK);
  286. const restoreSamples = await browser.execute(
  287. () =>
  288. (window as unknown as { __spfvRestoreSamples: Array<{ host: number; dock: number }> })
  289. .__spfvRestoreSamples,
  290. );
  291. expect(restoreSamples.length).toBeGreaterThan(1);
  292. expect(Math.max(...restoreSamples.map((sample) => sample.host))).toBeLessThanOrEqual(
  293. finalHost.height + 1,
  294. );
  295. expect(Math.max(...restoreSamples.map((sample) => sample.dock))).toBeLessThanOrEqual(
  296. finalDock.height + 1,
  297. );
  298. await $(`[data-testid="${TID_SIDE_PANE_FULL_VIEW_CONVERSATION_TAB}"]`).click();
  299. await waitForHostMode("conversation");
  300. await waitForComposerText(COMPOSER_INPUT, DRAFT_TEXT);
  301. expect(await readMainPaneMarker()).toBe(PANE_MARKER);
  302. await $(TAB).click();
  303. await waitForHostMode("floating");
  304. });
  305. it("SPFV-E2E-004 拖动吸附到左上并通过菜单打开对话", async () => {
  306. await focusFloatingComposer();
  307. await waitForHeader(true);
  308. const handle = await $(`[data-testid="${TID_SIDE_PANE_FULL_VIEW_HANDLE}"]`);
  309. // 聚焦仍会播放操作栏展开动画,等结束后读取拖动柄坐标。
  310. await waitForHostTransitionsSettled();
  311. const handleRect = await readRect(`[data-testid="${TID_SIDE_PANE_FULL_VIEW_HANDLE}"]`);
  312. const slot = await readRect(SLOT);
  313. await startHostMotionProbe();
  314. await browser
  315. .action("pointer", { parameters: { pointerType: "mouse" } })
  316. .move({ x: Math.round(handleRect.left + 8), y: Math.round(handleRect.top + 8) })
  317. .down({ button: 0 })
  318. .move({
  319. x: Math.round(handleRect.left + 20),
  320. y: Math.round(handleRect.top + 8),
  321. duration: 50,
  322. })
  323. .move({ x: Math.round(slot.left + 300), y: Math.round(slot.top + 40), duration: 200 })
  324. .up({ button: 0 })
  325. .perform();
  326. const snapMotion = await finishHostMotionProbe();
  327. expect(snapMotion.transitions).toContain("left");
  328. expect(snapMotion.transitions).toContain("top");
  329. await browser.waitUntil(
  330. async () => {
  331. const host = await readRect(HOST);
  332. const currentSlot = await readRect(SLOT);
  333. return (
  334. Math.abs(host.left - (currentSlot.left + MARGIN)) <= 2 &&
  335. Math.abs(host.top - (currentSlot.top + MARGIN)) <= 2
  336. );
  337. },
  338. { timeout: 5_000, timeoutMsg: "松手后浮窗没有吸附到左上角" },
  339. );
  340. expect(
  341. await browser.execute(() =>
  342. window.localStorage.getItem("zcode:side-pane-full-view:floating-anchor"),
  343. ),
  344. ).toBe("top-left");
  345. await focusFloatingComposer();
  346. await waitForHeader(true);
  347. await handle.click();
  348. const openConversation = await $(
  349. `[data-testid="${TID_SIDE_PANE_FULL_VIEW_MENU_OPEN_CONVERSATION}"]`,
  350. );
  351. await openConversation.waitForDisplayed({ timeout: 5_000 });
  352. await openConversation.click();
  353. await waitForHostMode("conversation");
  354. await $(TAB).click();
  355. await waitForHostMode("floating");
  356. // 回归:菜单 portal 的点击沿 React 树冒泡,曾顺带把浮窗切成展开态。
  357. expect(await $(HOST).getAttribute("data-floating")).toBe("collapsed");
  358. });
  359. it("SPFV-E2E-005 用上 / 下按钮展开与收起消息流", async () => {
  360. await focusFloatingComposer();
  361. await waitForHeader(true);
  362. await waitForHostTransitionsSettled();
  363. expect(await $(EXPAND).getAttribute("aria-expanded")).toBe("false");
  364. await startHostMotionProbe();
  365. await $(EXPAND).click();
  366. expect((await finishHostMotionProbe()).transitions).toContain("height");
  367. await browser.waitUntil(
  368. async () =>
  369. (await $(HOST).getAttribute("data-floating")) === "expanded" &&
  370. (await readRect(HOST)).height > 400,
  371. { timeout: 5_000, timeoutMsg: "点上按钮后浮窗没有展开" },
  372. );
  373. expect(await $(EXPAND).getAttribute("aria-expanded")).toBe("true");
  374. const timeline = await readRect(`${HOST} [data-testid="${TID_V4_TIMELINE}"]`);
  375. expect(timeline.height).toBeGreaterThan(100);
  376. // 滚动容器(含滚动条)止于输入框上方,不再延伸到输入框后面。
  377. expect(timeline.bottom).toBeLessThanOrEqual((await readRect(COMPOSER_DOCK)).top - 8);
  378. await saveThemeScreenshots("spfv-expanded");
  379. await startHostMotionProbe();
  380. await $(EXPAND).click();
  381. expect((await finishHostMotionProbe()).transitions).toContain("height");
  382. await browser.waitUntil(
  383. async () =>
  384. (await $(HOST).getAttribute("data-floating")) === "collapsed" &&
  385. (await readRect(HOST)).height < 360,
  386. { timeout: 5_000, timeoutMsg: "点下按钮后浮窗没有收起" },
  387. );
  388. await saveThemeScreenshots("spfv-collapsed-with-header");
  389. await clickSlotTopLeft();
  390. await waitForHeader(false);
  391. await saveThemeScreenshots("spfv-collapsed");
  392. await focusFloatingComposer();
  393. await waitForHeader(true);
  394. await $(`[data-testid="${TID_SIDE_PANE_FULL_VIEW_MINIMIZE}"]`).click();
  395. await $(`[data-testid="${TID_SIDE_PANE_FULL_VIEW_BUBBLE}"]`).waitForDisplayed({
  396. timeout: 5_000,
  397. });
  398. await saveThemeScreenshots("spfv-minimized");
  399. await $(`[data-testid="${TID_SIDE_PANE_FULL_VIEW_BUBBLE}"]`).click();
  400. await waitForHeader(true);
  401. });
  402. // 插件 UI 页面不在面板 DOM 里:它挂在 body 末尾的停靠层(position:fixed,无 z-index),
  403. // 按 tab 占位元素的坐标摆放。这里按 pluginUiPagePlane 的同一套样式复刻停靠层。
  404. it("SPFV-E2E-009 body 级插件页面层不被完整视图的面板外框遮挡", async () => {
  405. await waitForHostMode("floating");
  406. const slot = await readRect(SLOT);
  407. const hit = await browser.execute((rect) => {
  408. let parking = document.querySelector<HTMLElement>("[data-sandbox-page-parking]");
  409. if (!parking) {
  410. parking = document.createElement("div");
  411. parking.dataset.sandboxPageParking = "true";
  412. parking.style.cssText = "position:fixed;inset:0;pointer-events:none;overflow:visible";
  413. document.body.append(parking);
  414. }
  415. const page = document.createElement("div");
  416. page.dataset.e2eSpfvPluginPage = "true";
  417. Object.assign(page.style, {
  418. position: "fixed",
  419. inset: "auto",
  420. zIndex: "10",
  421. left: `${rect.left}px`,
  422. top: `${rect.top}px`,
  423. width: `${rect.width}px`,
  424. height: `${rect.height}px`,
  425. pointerEvents: "auto",
  426. });
  427. parking.append(page);
  428. // 取 slot 右下区域,避开吸附在左上的浮窗。
  429. const element = document.elementFromPoint(
  430. rect.left + rect.width * 0.85,
  431. rect.top + rect.height * 0.7,
  432. );
  433. const result = element?.closest("[data-e2e-spfv-plugin-page]")
  434. ? "plugin-page"
  435. : (element?.closest("[data-testid]")?.getAttribute("data-testid") ??
  436. element?.tagName ??
  437. null);
  438. page.remove();
  439. return result;
  440. }, slot);
  441. expect(hit).toBe("plugin-page");
  442. });
  443. it("SPFV-E2E-006 面板开关退出完整视图,重新打开不回到完整视图", async () => {
  444. await $(`${SIDE_FRAME} [data-testid="${TID_SIDE_PANE_TOGGLE}"]`).click();
  445. await waitForHostMode("normal");
  446. // 收起有过渡动画,动画结束前再次切换会被吞掉;等面板真正收起再重新打开。
  447. await browser.waitUntil(
  448. () =>
  449. browser.execute((frameSelector) => {
  450. const panel = document.querySelector(frameSelector)?.parentElement;
  451. return !panel || panel.getBoundingClientRect().width < 10;
  452. }, SIDE_FRAME),
  453. { timeout: 5_000, timeoutMsg: "面板开关点击后面板没有收起" },
  454. );
  455. await ensureSidePaneExpanded();
  456. await browser.pause(300);
  457. expect(await $(HOST).getAttribute("data-side-pane-full-view-host")).toBe("normal");
  458. expect(await $(`[data-testid="${TID_WORKSPACE_HEADER}"]`).isExisting()).toBe(true);
  459. expect(await readMainPaneMarker()).toBe(PANE_MARKER);
  460. });
  461. });
  462. /** 视觉验收:深浅两套主题各存一张整窗截图到 artifact 目录。 */
  463. async function saveThemeScreenshots(name: string) {
  464. const artifactDir = process.env.ZCODE_E2E_ARTIFACT_DIR;
  465. if (!artifactDir) return;
  466. for (const theme of ["zai-light", "zai-dark"]) {
  467. await browser.execute((value) => {
  468. const actions = (
  469. window as typeof window & { __testActions?: { setTheme(theme: string): void } }
  470. ).__testActions;
  471. actions?.setTheme(value);
  472. }, theme);
  473. await browser.pause(300);
  474. await browser.saveScreenshot(join(artifactDir, `${name}-${theme}.png`));
  475. }
  476. }
  477. async function realKeys(keys: string[]) {
  478. const isMac = await browser.execute(() => /Mac|iPhone|iPad/.test(navigator.userAgent));
  479. const mapped = keys.map((key) => (key === "Control" && isMac ? "Command" : key));
  480. await browser.keys(mapped as never);
  481. }
  482. async function clickSlotTopLeft() {
  483. await waitForHostTransitionsSettled();
  484. // 浮窗已吸附左上时,固定 slot + 60 会点进浮窗,不能验证外部点击失焦。
  485. // 用真实命中结果选择 slot 内、浮窗外的位置,保留原生指针与 blur 链路。
  486. const point = await browser.execute(
  487. (slotSelector, hostSelector) => {
  488. const slot = document.querySelector(slotSelector)!.getBoundingClientRect();
  489. const candidates = [
  490. { x: slot.left + 60, y: slot.top + 60 },
  491. { x: slot.right - 60, y: slot.bottom - 60 },
  492. { x: slot.right - 60, y: slot.top + 60 },
  493. ];
  494. const outside = candidates.find((point) => {
  495. const hit = document.elementFromPoint(point.x, point.y);
  496. return hit && !hit.closest(hostSelector);
  497. });
  498. if (!outside) throw new Error("找不到浮窗外的 slot 点击位置");
  499. return {
  500. x: Math.round(outside.x),
  501. y: Math.round(outside.y),
  502. originalInsideFloating: Boolean(
  503. document.elementFromPoint(candidates[0]!.x, candidates[0]!.y)?.closest(hostSelector),
  504. ),
  505. };
  506. },
  507. SLOT,
  508. HOST,
  509. );
  510. await browser
  511. .action("pointer", { parameters: { pointerType: "mouse" } })
  512. .move({ x: point.x, y: point.y })
  513. .down({ button: 0 })
  514. .up({ button: 0 })
  515. .perform();
  516. }
  517. async function clickFullViewToggle() {
  518. const toggle = await $(`${SIDE_FRAME} [data-testid="${TID_SIDE_PANE_FULL_VIEW_TOGGLE}"]`);
  519. await toggle.waitForClickable({ timeout: 10_000 });
  520. await toggle.click();
  521. }
  522. async function waitForHostMode(mode: "normal" | "conversation" | "floating") {
  523. await browser.waitUntil(
  524. async () => (await $(HOST).getAttribute("data-side-pane-full-view-host")) === mode,
  525. { timeout: 5_000, timeoutMsg: `完整视图宿主没有进入 ${mode}` },
  526. );
  527. }
  528. async function waitForHeader(visible: boolean) {
  529. await browser.waitUntil(
  530. async () =>
  531. (await $(`[data-testid="${TID_SIDE_PANE_FULL_VIEW_HEADER}"]`).isExisting()) === visible,
  532. { timeout: 5_000, timeoutMsg: `浮窗操作栏可见性应为 ${visible}` },
  533. );
  534. }
  535. async function focusFloatingComposer() {
  536. const input = await $(COMPOSER_INPUT);
  537. await input.waitForDisplayed({ timeout: 5_000 });
  538. await input.click();
  539. }
  540. async function waitForComposerText(selector: string, expected: string) {
  541. await browser.waitUntil(
  542. async () =>
  543. (await browser.execute(
  544. (inputSelector) => document.querySelector(inputSelector)?.textContent ?? "",
  545. selector,
  546. )) === expected,
  547. { timeout: 5_000, timeoutMsg: `输入框文字应为 ${expected}` },
  548. );
  549. }
  550. async function markMainPane() {
  551. await browser.execute(
  552. (selector, marker) => {
  553. const pane = document.querySelector<HTMLElement>(selector);
  554. if (!pane) throw new Error("缺少主 SessionPane");
  555. pane.dataset.e2eSpfvMarker = marker;
  556. },
  557. MAIN_PANE,
  558. PANE_MARKER,
  559. );
  560. }
  561. function readMainPaneMarker() {
  562. return browser.execute(
  563. (selector) => document.querySelector<HTMLElement>(selector)?.dataset.e2eSpfvMarker ?? null,
  564. MAIN_PANE,
  565. );
  566. }
  567. async function markComposerEditor() {
  568. await browser.execute(
  569. (selector, marker) => {
  570. const editor = document.querySelector<HTMLElement>(selector);
  571. if (!editor) throw new Error("缺少输入框");
  572. editor.dataset.e2eSpfvMarker = marker;
  573. },
  574. COMPOSER_INPUT,
  575. EDITOR_MARKER,
  576. );
  577. }
  578. function readComposerEditorMarker() {
  579. return browser.execute(
  580. (selector) => document.querySelector<HTMLElement>(selector)?.dataset.e2eSpfvMarker ?? null,
  581. COMPOSER_INPUT,
  582. );
  583. }
  584. async function waitForHostTransitionsSettled() {
  585. await browser.waitUntil(
  586. () =>
  587. browser.execute(
  588. (selector) =>
  589. (document.querySelector(selector)?.getAnimations() ?? []).every(
  590. (animation) => animation.playState !== "running",
  591. ),
  592. HOST,
  593. ),
  594. { timeout: 5_000, timeoutMsg: "完整视图宿主的几何过渡没有结束" },
  595. );
  596. }
  597. async function startHostMotionProbe() {
  598. await browser.execute((selector) => {
  599. const host = document.querySelector<HTMLElement>(selector);
  600. if (!host) throw new Error("缺少完整视图宿主");
  601. const probeWindow = window as typeof window & { __spfvMotionProbe?: HostMotionProbe };
  602. probeWindow.__spfvMotionProbe?.stop();
  603. let frame: number | undefined;
  604. const probe: HostMotionProbe = {
  605. transitions: [],
  606. firstFloatingFrame: null,
  607. stop: () => {
  608. host.removeEventListener("transitionrun", onTransition);
  609. observer.disconnect();
  610. if (frame !== undefined) cancelAnimationFrame(frame);
  611. },
  612. };
  613. const onTransition = (event: TransitionEvent) => {
  614. if (event.target === host) probe.transitions.push(event.propertyName);
  615. };
  616. // 在真实点击前监听,避免 WebDriver 往返已跨过 150ms 动画而只看到终态。
  617. const observer = new MutationObserver(() => {
  618. if (host.dataset.sidePaneFullViewHost !== "floating" || frame !== undefined) return;
  619. frame = requestAnimationFrame(() => {
  620. const rect = host.getBoundingClientRect();
  621. probe.firstFloatingFrame = {
  622. left: rect.left,
  623. top: rect.top,
  624. right: rect.right,
  625. bottom: rect.bottom,
  626. width: rect.width,
  627. height: rect.height,
  628. };
  629. });
  630. });
  631. observer.observe(host, {
  632. attributes: true,
  633. attributeFilter: ["data-side-pane-full-view-host", "data-floating", "style"],
  634. });
  635. host.addEventListener("transitionrun", onTransition);
  636. probeWindow.__spfvMotionProbe = probe;
  637. }, HOST);
  638. }
  639. async function finishHostMotionProbe() {
  640. await waitForHostTransitionsSettled();
  641. return browser.execute(
  642. () =>
  643. new Promise<Omit<HostMotionProbe, "stop">>((resolve) => {
  644. requestAnimationFrame(() => {
  645. const probeWindow = window as typeof window & { __spfvMotionProbe?: HostMotionProbe };
  646. const probe = probeWindow.__spfvMotionProbe;
  647. if (!probe) throw new Error("缺少浮窗动画采样");
  648. probe.stop();
  649. delete probeWindow.__spfvMotionProbe;
  650. resolve({ transitions: probe.transitions, firstFloatingFrame: probe.firstFloatingFrame });
  651. });
  652. }),
  653. );
  654. }
  655. function centerY(rect: Rect) {
  656. return rect.top + rect.height / 2;
  657. }
  658. function readRect(selector: string): Promise<Rect> {
  659. return browser.execute((target) => {
  660. const element = document.querySelector(target);
  661. if (!element) throw new Error(`缺少元素 ${target}`);
  662. const rect = element.getBoundingClientRect();
  663. return {
  664. left: rect.left,
  665. top: rect.top,
  666. right: rect.right,
  667. bottom: rect.bottom,
  668. width: rect.width,
  669. height: rect.height,
  670. };
  671. }, selector);
  672. }
  673. function rectsClose(a: Rect, b: Rect, tolerance: number) {
  674. return (
  675. Math.abs(a.left - b.left) <= tolerance &&
  676. Math.abs(a.top - b.top) <= tolerance &&
  677. Math.abs(a.width - b.width) <= tolerance &&
  678. Math.abs(a.height - b.height) <= tolerance
  679. );
  680. }
  681. function readLayoutWidths() {
  682. return browser.execute((sideFrameSelector) => {
  683. const width = (selector: string) =>
  684. document.querySelector(selector)?.getBoundingClientRect().width ?? 0;
  685. return {
  686. sidebar: width("#sidebar"),
  687. content: width("#content"),
  688. sideFrame: width(sideFrameSelector),
  689. };
  690. }, SIDE_FRAME);
  691. }
  692. async function ensureSidePaneExpanded() {
  693. const isExpanded = () =>
  694. browser.execute((frameSelector) => {
  695. const panel = document.querySelector(frameSelector)?.parentElement;
  696. return Boolean(
  697. panel &&
  698. panel.getBoundingClientRect().width > 100 &&
  699. getComputedStyle(panel).opacity === "1",
  700. );
  701. }, SIDE_FRAME);
  702. if (await isExpanded()) return;
  703. const toggle = await $(
  704. `[data-testid="${TID_WORKSPACE_HEADER}"] [data-testid="${TID_SIDE_PANE_TOGGLE}"]`,
  705. );
  706. await toggle.waitForClickable({ timeout: 10_000 });
  707. await toggle.click();
  708. await browser.waitUntil(isExpanded, { timeout: 10_000, timeoutMsg: "Side Pane 未实际展开" });
  709. }
  710. async function ensureTerminalTab() {
  711. if ((await $$(TAB).length) > 0) return;
  712. const launcher = await $('[data-side-pane-open-tab-item="terminal"]');
  713. await launcher.waitForClickable({ timeout: 10_000 });
  714. await launcher.click();
  715. await browser.waitUntil(async () => (await $$(TAB).length) === 1, {
  716. timeout: 10_000,
  717. timeoutMsg: "Terminal tab 没有打开",
  718. });
  719. }