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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640
  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 "../helpers/desktop-app.js";
  24. import { prepareV4ConversationE2E } from "../helpers/v4-conversation.js";
  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. describe("Side Pane 完整视图(SPFV)", () => {
  52. before(async () => {
  53. await prepareV4ConversationE2E({ skipProvider: true });
  54. await browser.execute(() =>
  55. window.localStorage.removeItem("zcode:side-pane-full-view:floating-anchor"),
  56. );
  57. });
  58. after(async () => {
  59. await browser.electron.restoreAllMocks();
  60. await clearAppData();
  61. });
  62. it("SPFV-E2E-001 进入、退出且不重新挂载对话", async () => {
  63. await ensureSidePaneExpanded();
  64. await ensureTerminalTab();
  65. const before = await readLayoutWidths();
  66. await markMainPane();
  67. await clickFullViewToggle();
  68. await waitForHostMode("floating");
  69. const entered = await readLayoutWidths();
  70. // 完整视图保留左侧任务列表与左上角按钮组。
  71. expect(Math.abs(entered.sidebar - before.sidebar)).toBeLessThanOrEqual(2);
  72. expect(await $('[data-testid="desktop-top-nav-back"]').isExisting()).toBe(true);
  73. // tab 栏是窗口拖拽区;对话 tab 不是 no-drag 时真实鼠标点击会被当成拖动窗口(CDP 点击测不出来)。
  74. expect(
  75. await browser.execute(
  76. (tabId) =>
  77. getComputedStyle(document.querySelector(`[data-testid="${tabId}"]`)!).getPropertyValue(
  78. "-webkit-app-region",
  79. ),
  80. TID_SIDE_PANE_FULL_VIEW_CONVERSATION_TAB,
  81. ),
  82. ).toBe("no-drag");
  83. expect(await $(`[data-testid="${TID_WORKSPACE_HEADER}"]`).isExisting()).toBe(false);
  84. expect(entered.content - entered.sideFrame).toBeLessThanOrEqual(16);
  85. expect(
  86. await browser.execute(
  87. (conversationTabId, tabSelector) => {
  88. const conversationTab = document.querySelector(`[data-testid="${conversationTabId}"]`);
  89. const firstPaneTab = document.querySelector(tabSelector);
  90. return Boolean(
  91. conversationTab &&
  92. firstPaneTab &&
  93. conversationTab.compareDocumentPosition(firstPaneTab) &
  94. Node.DOCUMENT_POSITION_FOLLOWING,
  95. );
  96. },
  97. TID_SIDE_PANE_FULL_VIEW_CONVERSATION_TAB,
  98. TAB,
  99. ),
  100. ).toBe(true);
  101. // 侧栏收起时,tab 栏按 WorkspaceHeader 的规则给左上角按钮组让位,对话 tab 不被按钮盖住。
  102. await realKeys(["Control", "b"]);
  103. await browser.waitUntil(async () => (await readLayoutWidths()).sidebar <= 8, {
  104. timeout: 5_000,
  105. timeoutMsg: "Cmd/Ctrl+B 后侧栏没有收起",
  106. });
  107. await browser.waitUntil(
  108. async () =>
  109. (await readRect(`[data-testid="${TID_SIDE_PANE_FULL_VIEW_CONVERSATION_TAB}"]`)).left >
  110. (await readRect('[data-testid="desktop-top-nav-back"]')).right + 24,
  111. { timeout: 5_000, timeoutMsg: "侧栏收起后 tab 栏没有给左上角按钮组让位" },
  112. );
  113. await realKeys(["Control", "b"]);
  114. await browser.waitUntil(
  115. async () => Math.abs((await readLayoutWidths()).sidebar - before.sidebar) <= 2,
  116. { timeout: 5_000, timeoutMsg: "Cmd/Ctrl+B 后侧栏没有恢复" },
  117. );
  118. await realKeys(["Control", "Shift", "f"]);
  119. await waitForHostMode("normal");
  120. await browser.waitUntil(
  121. async () => Math.abs((await readLayoutWidths()).sidebar - before.sidebar) <= 2,
  122. { timeout: 5_000, timeoutMsg: "退出完整视图后左侧任务列表没有恢复原宽度" },
  123. );
  124. const exited = await readLayoutWidths();
  125. expect(Math.abs(exited.sidebar - before.sidebar)).toBeLessThanOrEqual(2);
  126. expect(Math.abs(exited.sideFrame - before.sideFrame)).toBeLessThanOrEqual(2);
  127. expect(await $(`[data-testid="${TID_WORKSPACE_HEADER}"]`).isExisting()).toBe(true);
  128. await clickFullViewToggle();
  129. await waitForHostMode("floating");
  130. expect(await readMainPaneMarker()).toBe(PANE_MARKER);
  131. });
  132. it("SPFV-E2E-002 对话 tab 全宽,面板 tab 显示浮窗", async () => {
  133. await $(`[data-testid="${TID_SIDE_PANE_FULL_VIEW_CONVERSATION_TAB}"]`).click();
  134. await waitForHostMode("conversation");
  135. const slot = await readRect(SLOT);
  136. await browser.waitUntil(async () => rectsClose(await readRect(HOST), slot, 2), {
  137. timeout: 5_000,
  138. timeoutMsg: "对话 tab 激活后宿主没有铺满面板内容区",
  139. });
  140. expect(await $(`[data-testid="${TID_SIDE_PANE_FULL_VIEW_HEADER}"]`).isExisting()).toBe(false);
  141. // 对话 tab 保持普通两行布局,草稿顶部仍有「选择项目」栏。
  142. expect(await $(COMPOSER_SHELL).getAttribute("data-composer-layout")).toBe("stacked");
  143. expect(await $(COMPOSER_PROJECT_TRIGGER).isExisting()).toBe(true);
  144. await markComposerEditor();
  145. await $(TAB).click();
  146. await waitForHostMode("floating");
  147. await browser.waitUntil(
  148. async () => {
  149. const host = await readRect(HOST);
  150. const currentSlot = await readRect(SLOT);
  151. return (
  152. Math.abs(currentSlot.right - MARGIN - host.right) <= 2 &&
  153. Math.abs(currentSlot.bottom - MARGIN - host.bottom) <= 2 &&
  154. host.height < 320
  155. );
  156. },
  157. { timeout: 5_000, timeoutMsg: "浮窗没有收起在右下角" },
  158. );
  159. // 修复原因:对话 tab 切回浮窗时宿主几何有过渡,过渡中分两次读宿主和输入区会得到不同帧的坐标。
  160. await waitForHostTransitionsSettled();
  161. // 收起态没有外层:输入区与浮窗同宽、底边对齐,抽屉藏在输入框后面。
  162. const host = await readRect(HOST);
  163. const dock = await readRect(COMPOSER_DOCK);
  164. expect(Math.abs(dock.width - host.width)).toBeLessThanOrEqual(1);
  165. expect(Math.abs(dock.bottom - host.bottom)).toBeLessThanOrEqual(1);
  166. expect(await $(DRAWER).getAttribute("data-open")).toBe("false");
  167. // 浮窗默认是一条:[+] 输入区 [控件][发送] 在同一行,没有草稿项目栏。
  168. expect(await $(COMPOSER_SHELL).getAttribute("data-composer-layout")).toBe("inline");
  169. expect(await $(COMPOSER_PROJECT_TRIGGER).isExisting()).toBe(false);
  170. await browser.waitUntil(async () => (await readRect(HOST)).height <= 64, {
  171. timeout: 5_000,
  172. timeoutMsg: "单行浮窗高度没有收到一行",
  173. });
  174. const plus = await readRect(COMPOSER_ACTION_MENU);
  175. const input = await readRect(COMPOSER_INPUT);
  176. const send = await readRect(COMPOSER_SEND);
  177. expect(plus.right).toBeLessThanOrEqual(input.left);
  178. expect(input.right).toBeLessThanOrEqual(send.left);
  179. expect(Math.abs(centerY(plus) - centerY(send))).toBeLessThanOrEqual(2);
  180. expect(Math.abs(centerY(input) - centerY(send))).toBeLessThanOrEqual(2);
  181. expect(input.width).toBeGreaterThanOrEqual((await readRect(COMPOSER_SHELL)).width / 2 - 16);
  182. // 布局切换只换样式,编辑器节点不重新挂载。
  183. expect(await readComposerEditorMarker()).toBe(EDITOR_MARKER);
  184. });
  185. it("SPFV-E2E-003 操作栏、最小化与恢复共享同一份草稿", async () => {
  186. await focusFloatingComposer();
  187. await waitForHeader(true);
  188. // 抽屉从输入框后面滑出:操作栏只有按钮,抽屉一直延伸到输入框底边,两侧没有凹口。
  189. expect(await $(DRAWER).getAttribute("data-open")).toBe("true");
  190. expect(await $(`[data-testid="${TID_SIDE_PANE_FULL_VIEW_HEADER}"] span`).isExisting()).toBe(
  191. false,
  192. );
  193. // 草稿空闲:操作栏不显示运行中的 loading。
  194. expect(await $(`[data-testid="${TID_SIDE_PANE_FULL_VIEW_RUNNING}"]`).isExisting()).toBe(false);
  195. await browser.waitUntil(
  196. async () =>
  197. Math.abs((await readRect(DRAWER)).bottom - (await readRect(COMPOSER_DOCK)).bottom) <= 1.5,
  198. { timeout: 5_000, timeoutMsg: "抽屉没有延伸到输入框底边" },
  199. );
  200. await browser.keys(DRAFT_TEXT);
  201. await waitForComposerText(COMPOSER_INPUT, DRAFT_TEXT);
  202. // 面板 tab trigger 不抢焦点;点进面板内容区才会让焦点离开浮窗。
  203. await clickSlotTopLeft();
  204. await waitForHeader(false);
  205. await focusFloatingComposer();
  206. await waitForHeader(true);
  207. await $(`[data-testid="${TID_SIDE_PANE_FULL_VIEW_MINIMIZE}"]`).click();
  208. const bubble = await $(`[data-testid="${TID_SIDE_PANE_FULL_VIEW_BUBBLE}"]`);
  209. await bubble.waitForDisplayed({ timeout: 5_000 });
  210. await browser.waitUntil(async () => (await readRect(HOST)).width <= 52, {
  211. timeout: 5_000,
  212. timeoutMsg: "最小化后浮窗没有收成圆形按钮",
  213. });
  214. // 恢复动画逐帧采样:浮窗与输入区高度不能先变高再缩回(回归:宽度过渡时单行输入框折行)。
  215. await browser.execute(
  216. (hostSelector, dockSelector) => {
  217. const samples: Array<{ host: number; dock: number }> = [];
  218. (window as unknown as { __spfvRestoreSamples: typeof samples }).__spfvRestoreSamples =
  219. samples;
  220. const start = performance.now();
  221. const tick = () => {
  222. const host = document.querySelector(hostSelector)?.getBoundingClientRect();
  223. const dock = document.querySelector(dockSelector)?.getBoundingClientRect();
  224. samples.push({ host: host?.height ?? 0, dock: dock?.height ?? 0 });
  225. if (performance.now() - start < 500) requestAnimationFrame(tick);
  226. };
  227. document.addEventListener("pointerdown", () => requestAnimationFrame(tick), {
  228. capture: true,
  229. once: true,
  230. });
  231. },
  232. HOST,
  233. COMPOSER_DOCK,
  234. );
  235. await bubble.click();
  236. await bubble.waitForExist({ reverse: true, timeout: 5_000 });
  237. await waitForHeader(true);
  238. await browser.waitUntil(
  239. () =>
  240. browser.execute(
  241. (selector) => Boolean(document.activeElement?.closest(selector)),
  242. COMPOSER_INPUT,
  243. ),
  244. { timeout: 5_000, timeoutMsg: "恢复后输入框没有获得焦点" },
  245. );
  246. await waitForComposerText(COMPOSER_INPUT, DRAFT_TEXT);
  247. await browser.waitUntil(
  248. async () =>
  249. (await browser.execute(
  250. () =>
  251. (window as unknown as { __spfvRestoreSamples: unknown[] }).__spfvRestoreSamples.length,
  252. )) > 20,
  253. { timeout: 5_000, timeoutMsg: "恢复动画采样不足" },
  254. );
  255. await waitForHostTransitionsSettled();
  256. const finalHost = await readRect(HOST);
  257. const finalDock = await readRect(COMPOSER_DOCK);
  258. const restoreSamples = await browser.execute(
  259. () =>
  260. (window as unknown as { __spfvRestoreSamples: Array<{ host: number; dock: number }> })
  261. .__spfvRestoreSamples,
  262. );
  263. expect(Math.max(...restoreSamples.map((sample) => sample.host))).toBeLessThanOrEqual(
  264. finalHost.height + 1,
  265. );
  266. expect(Math.max(...restoreSamples.map((sample) => sample.dock))).toBeLessThanOrEqual(
  267. finalDock.height + 1,
  268. );
  269. await $(`[data-testid="${TID_SIDE_PANE_FULL_VIEW_CONVERSATION_TAB}"]`).click();
  270. await waitForHostMode("conversation");
  271. await waitForComposerText(COMPOSER_INPUT, DRAFT_TEXT);
  272. expect(await readMainPaneMarker()).toBe(PANE_MARKER);
  273. await $(TAB).click();
  274. await waitForHostMode("floating");
  275. });
  276. it("SPFV-E2E-004 拖动吸附到左上并通过菜单打开对话", async () => {
  277. await focusFloatingComposer();
  278. await waitForHeader(true);
  279. const handle = await $(`[data-testid="${TID_SIDE_PANE_FULL_VIEW_HANDLE}"]`);
  280. // 修复原因:上一个用例刚从对话 tab 切回浮窗,浮窗几何还在从铺满 slot 过渡到浮窗大小;
  281. // 过渡中读到的 ⠿ 坐标会让按下点落在浮窗外(面板内容上),拖动变成终端选区。
  282. // 两次读数比较不可靠(同一渲染帧内读数天然相同),改为等宿主没有正在运行的过渡。
  283. await waitForHostTransitionsSettled();
  284. const handleRect = await readRect(`[data-testid="${TID_SIDE_PANE_FULL_VIEW_HANDLE}"]`);
  285. const slot = await readRect(SLOT);
  286. await browser
  287. .action("pointer", { parameters: { pointerType: "mouse" } })
  288. .move({ x: Math.round(handleRect.left + 8), y: Math.round(handleRect.top + 8) })
  289. .down({ button: 0 })
  290. .move({
  291. x: Math.round(handleRect.left + 20),
  292. y: Math.round(handleRect.top + 8),
  293. duration: 50,
  294. })
  295. .move({ x: Math.round(slot.left + 300), y: Math.round(slot.top + 40), duration: 200 })
  296. .up({ button: 0 })
  297. .perform();
  298. await browser.waitUntil(
  299. async () => {
  300. const host = await readRect(HOST);
  301. const currentSlot = await readRect(SLOT);
  302. return (
  303. Math.abs(host.left - (currentSlot.left + MARGIN)) <= 2 &&
  304. Math.abs(host.top - (currentSlot.top + MARGIN)) <= 2
  305. );
  306. },
  307. { timeout: 5_000, timeoutMsg: "松手后浮窗没有吸附到左上角" },
  308. );
  309. expect(
  310. await browser.execute(() =>
  311. window.localStorage.getItem("zcode:side-pane-full-view:floating-anchor"),
  312. ),
  313. ).toBe("top-left");
  314. await focusFloatingComposer();
  315. await waitForHeader(true);
  316. await handle.click();
  317. const openConversation = await $(
  318. `[data-testid="${TID_SIDE_PANE_FULL_VIEW_MENU_OPEN_CONVERSATION}"]`,
  319. );
  320. await openConversation.waitForDisplayed({ timeout: 5_000 });
  321. await openConversation.click();
  322. await waitForHostMode("conversation");
  323. await $(TAB).click();
  324. await waitForHostMode("floating");
  325. // 回归:菜单 portal 的点击沿 React 树冒泡,曾顺带把浮窗切成展开态。
  326. expect(await $(HOST).getAttribute("data-floating")).toBe("collapsed");
  327. });
  328. it("SPFV-E2E-005 用上 / 下按钮展开与收起消息流", async () => {
  329. await focusFloatingComposer();
  330. await waitForHeader(true);
  331. expect(await $(EXPAND).getAttribute("aria-expanded")).toBe("false");
  332. await $(EXPAND).click();
  333. await browser.waitUntil(
  334. async () =>
  335. (await $(HOST).getAttribute("data-floating")) === "expanded" &&
  336. (await readRect(HOST)).height > 400,
  337. { timeout: 5_000, timeoutMsg: "点上按钮后浮窗没有展开" },
  338. );
  339. expect(await $(EXPAND).getAttribute("aria-expanded")).toBe("true");
  340. const timeline = await readRect(`${HOST} [data-testid="${TID_V4_TIMELINE}"]`);
  341. expect(timeline.height).toBeGreaterThan(100);
  342. // 滚动容器(含滚动条)止于输入框上方,不再延伸到输入框后面。
  343. expect(timeline.bottom).toBeLessThanOrEqual((await readRect(COMPOSER_DOCK)).top - 8);
  344. await saveThemeScreenshots("spfv-expanded");
  345. await $(EXPAND).click();
  346. await browser.waitUntil(
  347. async () =>
  348. (await $(HOST).getAttribute("data-floating")) === "collapsed" &&
  349. (await readRect(HOST)).height < 360,
  350. { timeout: 5_000, timeoutMsg: "点下按钮后浮窗没有收起" },
  351. );
  352. await saveThemeScreenshots("spfv-collapsed-with-header");
  353. await clickSlotTopLeft();
  354. await waitForHeader(false);
  355. await saveThemeScreenshots("spfv-collapsed");
  356. await focusFloatingComposer();
  357. await waitForHeader(true);
  358. await $(`[data-testid="${TID_SIDE_PANE_FULL_VIEW_MINIMIZE}"]`).click();
  359. await $(`[data-testid="${TID_SIDE_PANE_FULL_VIEW_BUBBLE}"]`).waitForDisplayed({
  360. timeout: 5_000,
  361. });
  362. await saveThemeScreenshots("spfv-minimized");
  363. await $(`[data-testid="${TID_SIDE_PANE_FULL_VIEW_BUBBLE}"]`).click();
  364. await waitForHeader(true);
  365. });
  366. // 插件 UI 页面不在面板 DOM 里:它挂在 body 末尾的停靠层(position:fixed,无 z-index),
  367. // 按 tab 占位元素的坐标摆放。这里按 pluginUiPagePlane 的同一套样式复刻停靠层。
  368. it("SPFV-E2E-009 body 级插件页面层不被完整视图的面板外框遮挡", async () => {
  369. await waitForHostMode("floating");
  370. const slot = await readRect(SLOT);
  371. const hit = await browser.execute((rect) => {
  372. let parking = document.querySelector<HTMLElement>("[data-sandbox-page-parking]");
  373. if (!parking) {
  374. parking = document.createElement("div");
  375. parking.dataset.sandboxPageParking = "true";
  376. parking.style.cssText = "position:fixed;inset:0;pointer-events:none;overflow:visible";
  377. document.body.append(parking);
  378. }
  379. const page = document.createElement("div");
  380. page.dataset.e2eSpfvPluginPage = "true";
  381. Object.assign(page.style, {
  382. position: "fixed",
  383. inset: "auto",
  384. zIndex: "10",
  385. left: `${rect.left}px`,
  386. top: `${rect.top}px`,
  387. width: `${rect.width}px`,
  388. height: `${rect.height}px`,
  389. pointerEvents: "auto",
  390. });
  391. parking.append(page);
  392. // 取 slot 右下区域,避开吸附在左上的浮窗。
  393. const element = document.elementFromPoint(
  394. rect.left + rect.width * 0.85,
  395. rect.top + rect.height * 0.7,
  396. );
  397. const result = element?.closest("[data-e2e-spfv-plugin-page]")
  398. ? "plugin-page"
  399. : (element?.closest("[data-testid]")?.getAttribute("data-testid") ??
  400. element?.tagName ??
  401. null);
  402. page.remove();
  403. return result;
  404. }, slot);
  405. expect(hit).toBe("plugin-page");
  406. });
  407. it("SPFV-E2E-006 面板开关退出完整视图,重新打开不回到完整视图", async () => {
  408. await $(`${SIDE_FRAME} [data-testid="${TID_SIDE_PANE_TOGGLE}"]`).click();
  409. await waitForHostMode("normal");
  410. // 收起有过渡动画,动画结束前再次切换会被吞掉;等面板真正收起再重新打开。
  411. await browser.waitUntil(
  412. () =>
  413. browser.execute((frameSelector) => {
  414. const panel = document.querySelector(frameSelector)?.parentElement;
  415. return !panel || panel.getBoundingClientRect().width < 10;
  416. }, SIDE_FRAME),
  417. { timeout: 5_000, timeoutMsg: "面板开关点击后面板没有收起" },
  418. );
  419. await ensureSidePaneExpanded();
  420. await browser.pause(300);
  421. expect(await $(HOST).getAttribute("data-side-pane-full-view-host")).toBe("normal");
  422. expect(await $(`[data-testid="${TID_WORKSPACE_HEADER}"]`).isExisting()).toBe(true);
  423. expect(await readMainPaneMarker()).toBe(PANE_MARKER);
  424. });
  425. });
  426. /** 视觉验收:深浅两套主题各存一张整窗截图到 artifact 目录。 */
  427. async function saveThemeScreenshots(name: string) {
  428. const artifactDir = process.env.ZCODE_E2E_ARTIFACT_DIR;
  429. if (!artifactDir) return;
  430. for (const theme of ["zai-light", "zai-dark"]) {
  431. await browser.execute((value) => {
  432. const actions = (
  433. window as typeof window & { __testActions?: { setTheme(theme: string): void } }
  434. ).__testActions;
  435. actions?.setTheme(value);
  436. }, theme);
  437. await browser.pause(300);
  438. await browser.saveScreenshot(join(artifactDir, `${name}-${theme}.png`));
  439. }
  440. }
  441. async function realKeys(keys: string[]) {
  442. const isMac = await browser.execute(() => /Mac|iPhone|iPad/.test(navigator.userAgent));
  443. const mapped = keys.map((key) => (key === "Control" && isMac ? "Command" : key));
  444. await browser.keys(mapped as never);
  445. }
  446. async function clickSlotTopLeft() {
  447. const slot = await readRect(SLOT);
  448. await browser
  449. .action("pointer", { parameters: { pointerType: "mouse" } })
  450. .move({ x: Math.round(slot.left + 60), y: Math.round(slot.top + 60) })
  451. .down({ button: 0 })
  452. .up({ button: 0 })
  453. .perform();
  454. }
  455. async function clickFullViewToggle() {
  456. const toggle = await $(`${SIDE_FRAME} [data-testid="${TID_SIDE_PANE_FULL_VIEW_TOGGLE}"]`);
  457. await toggle.waitForClickable({ timeout: 10_000 });
  458. await toggle.click();
  459. }
  460. async function waitForHostMode(mode: "normal" | "conversation" | "floating") {
  461. await browser.waitUntil(
  462. async () => (await $(HOST).getAttribute("data-side-pane-full-view-host")) === mode,
  463. { timeout: 5_000, timeoutMsg: `完整视图宿主没有进入 ${mode}` },
  464. );
  465. }
  466. async function waitForHeader(visible: boolean) {
  467. await browser.waitUntil(
  468. async () =>
  469. (await $(`[data-testid="${TID_SIDE_PANE_FULL_VIEW_HEADER}"]`).isExisting()) === visible,
  470. { timeout: 5_000, timeoutMsg: `浮窗操作栏可见性应为 ${visible}` },
  471. );
  472. }
  473. async function focusFloatingComposer() {
  474. const input = await $(COMPOSER_INPUT);
  475. await input.waitForDisplayed({ timeout: 5_000 });
  476. await input.click();
  477. }
  478. async function waitForComposerText(selector: string, expected: string) {
  479. await browser.waitUntil(
  480. async () =>
  481. (await browser.execute(
  482. (inputSelector) => document.querySelector(inputSelector)?.textContent ?? "",
  483. selector,
  484. )) === expected,
  485. { timeout: 5_000, timeoutMsg: `输入框文字应为 ${expected}` },
  486. );
  487. }
  488. async function markMainPane() {
  489. await browser.execute(
  490. (selector, marker) => {
  491. const pane = document.querySelector<HTMLElement>(selector);
  492. if (!pane) throw new Error("缺少主 SessionPane");
  493. pane.dataset.e2eSpfvMarker = marker;
  494. },
  495. MAIN_PANE,
  496. PANE_MARKER,
  497. );
  498. }
  499. function readMainPaneMarker() {
  500. return browser.execute(
  501. (selector) => document.querySelector<HTMLElement>(selector)?.dataset.e2eSpfvMarker ?? null,
  502. MAIN_PANE,
  503. );
  504. }
  505. async function markComposerEditor() {
  506. await browser.execute(
  507. (selector, marker) => {
  508. const editor = document.querySelector<HTMLElement>(selector);
  509. if (!editor) throw new Error("缺少输入框");
  510. editor.dataset.e2eSpfvMarker = marker;
  511. },
  512. COMPOSER_INPUT,
  513. EDITOR_MARKER,
  514. );
  515. }
  516. function readComposerEditorMarker() {
  517. return browser.execute(
  518. (selector) => document.querySelector<HTMLElement>(selector)?.dataset.e2eSpfvMarker ?? null,
  519. COMPOSER_INPUT,
  520. );
  521. }
  522. async function waitForHostTransitionsSettled() {
  523. await browser.waitUntil(
  524. () =>
  525. browser.execute(
  526. (selector) =>
  527. (document.querySelector(selector)?.getAnimations() ?? []).every(
  528. (animation) => animation.playState !== "running",
  529. ),
  530. HOST,
  531. ),
  532. { timeout: 5_000, timeoutMsg: "完整视图宿主的几何过渡没有结束" },
  533. );
  534. }
  535. function centerY(rect: Rect) {
  536. return rect.top + rect.height / 2;
  537. }
  538. function readRect(selector: string): Promise<Rect> {
  539. return browser.execute((target) => {
  540. const element = document.querySelector(target);
  541. if (!element) throw new Error(`缺少元素 ${target}`);
  542. const rect = element.getBoundingClientRect();
  543. return {
  544. left: rect.left,
  545. top: rect.top,
  546. right: rect.right,
  547. bottom: rect.bottom,
  548. width: rect.width,
  549. height: rect.height,
  550. };
  551. }, selector);
  552. }
  553. function rectsClose(a: Rect, b: Rect, tolerance: number) {
  554. return (
  555. Math.abs(a.left - b.left) <= tolerance &&
  556. Math.abs(a.top - b.top) <= tolerance &&
  557. Math.abs(a.width - b.width) <= tolerance &&
  558. Math.abs(a.height - b.height) <= tolerance
  559. );
  560. }
  561. function readLayoutWidths() {
  562. return browser.execute((sideFrameSelector) => {
  563. const width = (selector: string) =>
  564. document.querySelector(selector)?.getBoundingClientRect().width ?? 0;
  565. return {
  566. sidebar: width("#sidebar"),
  567. content: width("#content"),
  568. sideFrame: width(sideFrameSelector),
  569. };
  570. }, SIDE_FRAME);
  571. }
  572. async function ensureSidePaneExpanded() {
  573. const isExpanded = () =>
  574. browser.execute((frameSelector) => {
  575. const panel = document.querySelector(frameSelector)?.parentElement;
  576. return Boolean(
  577. panel &&
  578. panel.getBoundingClientRect().width > 100 &&
  579. getComputedStyle(panel).opacity === "1",
  580. );
  581. }, SIDE_FRAME);
  582. if (await isExpanded()) return;
  583. const toggle = await $(
  584. `[data-testid="${TID_WORKSPACE_HEADER}"] [data-testid="${TID_SIDE_PANE_TOGGLE}"]`,
  585. );
  586. await toggle.waitForClickable({ timeout: 10_000 });
  587. await toggle.click();
  588. await browser.waitUntil(isExpanded, { timeout: 10_000, timeoutMsg: "Side Pane 未实际展开" });
  589. }
  590. async function ensureTerminalTab() {
  591. if ((await $$(TAB).length) > 0) return;
  592. const launcher = await $('[data-side-pane-open-tab-item="terminal"]');
  593. await launcher.waitForClickable({ timeout: 10_000 });
  594. await launcher.click();
  595. await browser.waitUntil(async () => (await $$(TAB).length) === 1, {
  596. timeout: 10_000,
  597. timeoutMsg: "Terminal tab 没有打开",
  598. });
  599. }