conversation-session-purpose-section-drag.test.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. import { join } from "node:path";
  2. import {
  3. DEFAULT_WORKSPACE,
  4. readModelProviders,
  5. readSettings,
  6. waitForWorkspaceApp,
  7. } from "#e2e/helpers/desktop-app";
  8. const project = '[data-testid="project-section"]';
  9. const tasks = '[data-testid="conversation-section"]';
  10. const trigger = "button[data-purpose-section-drag-trigger]";
  11. const indicator = '[data-testid="purpose-section-drop-indicator"]';
  12. const preferenceKey = "zcode-sidebar-purpose-section-preferences";
  13. describe("CWP02 分区标题拖拽", () => {
  14. before(async () => {
  15. await waitForWorkspaceApp(DEFAULT_WORKSPACE, 30_000);
  16. await browser.execute((key) => localStorage.removeItem(key), preferenceKey);
  17. await browser.refresh();
  18. await $(project).waitForDisplayed();
  19. });
  20. afterEach(async () => {
  21. await browser.keys("Escape");
  22. await browser.releaseActions();
  23. });
  24. it("标题直接向上拖动,蓝线、纵轴、折叠独立与重载持久化", async () => {
  25. expect(await $("[data-purpose-section-drag-handle]").isExisting()).toBe(false);
  26. await $(project).$(trigger).click();
  27. await expect($(project).$(trigger)).toHaveAttribute("aria-expanded", "false");
  28. await expect($(tasks).$(trigger)).toHaveAttribute("aria-expanded", "true");
  29. await beginDrag(tasks, project);
  30. await expect($(project).$(indicator)).toHaveAttribute("data-drop-position", "before");
  31. await assertDragLayout(tasks);
  32. await browser.saveScreenshot(join(process.env.ZCODE_E2E_ARTIFACT_DIR!, "section-drag-up.png"));
  33. await browser.performActions([
  34. {
  35. type: "pointer",
  36. id: "section-pointer",
  37. parameters: { pointerType: "mouse" },
  38. actions: [{ type: "pointerUp", button: 0 }],
  39. },
  40. ]);
  41. await browser.releaseActions();
  42. await browser.waitUntil(async () => (await order())[0] === "conversations");
  43. expect(await $(indicator).isExisting()).toBe(false);
  44. await expect($(project).$(trigger)).toHaveAttribute("aria-expanded", "false");
  45. await expect($(tasks).$(trigger)).toHaveAttribute("aria-expanded", "true");
  46. await browser.refresh();
  47. try {
  48. await $(project).waitForDisplayed();
  49. } catch (cause) {
  50. // 正式回放曾在此刷新后进入欢迎页;保留配置事实,不能用重载或重试掩盖启动异常。
  51. const [providers, settings] = await Promise.all([readModelProviders(), readSettings()]);
  52. throw new Error(
  53. `CWP02 持久化验证刷新失败: ${JSON.stringify({
  54. providerFamilyDomain: settings.providerFamilyDomain,
  55. providers: providers.map((provider) => ({
  56. id: provider.id,
  57. enabled: provider.enabled,
  58. apiKeyPresent: Boolean(provider.apiKey),
  59. systemDisabledReason: provider.systemDisabledReason,
  60. models: provider.models.map((model) => ({
  61. id: model.id,
  62. disabledReason: model.disabledReason,
  63. })),
  64. })),
  65. })}`,
  66. { cause },
  67. );
  68. }
  69. expect(await order()).toEqual(["conversations", "projects"]);
  70. await expect($(project).$(trigger)).toHaveAttribute("aria-expanded", "false");
  71. });
  72. it("键盘从标题向下移动,Escape 不提交,Space 提交且不误折叠", async () => {
  73. const focusTasks = () =>
  74. browser.execute(
  75. (selector) => document.querySelector<HTMLButtonElement>(selector)!.focus(),
  76. `${tasks} ${trigger}`,
  77. );
  78. await focusTasks();
  79. await browser.keys("Space");
  80. await browser.keys("ArrowDown");
  81. await expect($(project).$(indicator)).toHaveAttribute("data-drop-position", "after");
  82. await browser.keys("Escape");
  83. expect(await $(indicator).isExisting()).toBe(false);
  84. expect(await order()).toEqual(["conversations", "projects"]);
  85. await expect($(tasks).$(trigger)).toHaveAttribute("aria-expanded", "true");
  86. await focusTasks();
  87. await browser.keys("Space");
  88. await browser.keys("ArrowDown");
  89. await expect($(project).$(indicator)).toHaveAttribute("data-drop-position", "after");
  90. await browser.keys("Space");
  91. await browser.waitUntil(async () => (await order())[0] === "projects");
  92. await expect($(tasks).$(trigger)).toHaveAttribute("aria-expanded", "true");
  93. expect(await $(indicator).isExisting()).toBe(false);
  94. });
  95. it("新建按钮保持独立,点击仍能打开向导而不折叠或拖拽", async () => {
  96. await $('[data-testid="virtual-project-add"]').click();
  97. await $('[role="dialog"]').waitForDisplayed();
  98. await expect($(project).$(trigger)).toHaveAttribute("aria-expanded", "false");
  99. expect(await $(indicator).isExisting()).toBe(false);
  100. });
  101. it("明暗主题与 390px 下向右拖动仍不横向溢出,取消清除蓝线", async () => {
  102. const originalTheme = await browser.execute(() =>
  103. (window as Window & { __testActions?: { getTheme(): string } }).__testActions!.getTheme(),
  104. );
  105. try {
  106. for (const width of [1200, 390]) {
  107. await emulate(width);
  108. if (width === 390) {
  109. // 窄屏按原策略自动收起,等待后再通过产品入口打开侧栏。
  110. await browser.waitUntil(
  111. async () => (await $("aside").getAttribute("aria-hidden")) === "true",
  112. );
  113. await $('button[aria-label="Toggle sidebar"], button[aria-label="切换侧边栏"]').click();
  114. }
  115. for (const theme of ["zai-light", "zai-dark"]) {
  116. await browser.execute(
  117. (value) =>
  118. (
  119. window as Window & { __testActions?: { setTheme(value: string): void } }
  120. ).__testActions!.setTheme(value),
  121. theme,
  122. );
  123. await browser.waitUntil(() =>
  124. browser.execute(
  125. (value) => document.documentElement.classList.contains(`theme-${value}`),
  126. theme,
  127. ),
  128. );
  129. await beginDrag(project, tasks);
  130. await expect($(tasks).$(indicator)).toHaveAttribute("data-drop-position", "after");
  131. await assertDragLayout(project);
  132. await browser.saveScreenshot(
  133. join(process.env.ZCODE_E2E_ARTIFACT_DIR!, `section-drag-${width}-${theme}.png`),
  134. );
  135. await browser.keys("Escape");
  136. await browser.releaseActions();
  137. expect(await $(indicator).isExisting()).toBe(false);
  138. expect(await order()).toEqual(["projects", "conversations"]);
  139. }
  140. }
  141. } finally {
  142. await emulate();
  143. await browser.execute(
  144. (value) =>
  145. (
  146. window as Window & { __testActions?: { setTheme(value: string): void } }
  147. ).__testActions!.setTheme(value),
  148. originalTheme,
  149. );
  150. }
  151. });
  152. });
  153. async function order() {
  154. return browser.execute(() =>
  155. Array.from(document.querySelectorAll("[data-purpose-section-list] > *")).map((node) =>
  156. node
  157. .querySelector("[data-purpose-section-drag-trigger]")
  158. ?.getAttribute("data-purpose-section-drag-trigger"),
  159. ),
  160. );
  161. }
  162. async function beginDrag(source: string, target: string) {
  163. // Escape 后取消动画仍可能移动分区;等待实际 transform 复位再量取下一次拖拽坐标。
  164. await browser.waitUntil(
  165. () =>
  166. browser.execute(
  167. (selectors) =>
  168. selectors.every((selector) => {
  169. const element = document.querySelector(selector)!;
  170. const matrix = new DOMMatrix(getComputedStyle(element).transform);
  171. return Math.abs(matrix.m41) < 0.5 && Math.abs(matrix.m42) < 0.5;
  172. }),
  173. [source, target],
  174. ),
  175. { timeout: 5000, timeoutMsg: "上一轮拖拽取消后分区位置未复位" },
  176. );
  177. const coords = await browser.execute(
  178. (source, target, trigger) => {
  179. const from = document.querySelector(`${source} ${trigger}`)!.getBoundingClientRect();
  180. const sourceSection = document.querySelector(source)!.getBoundingClientRect();
  181. const to = document.querySelector(target)!.getBoundingClientRect();
  182. return {
  183. x: Math.round(from.left + 28),
  184. y: Math.round(from.top + from.height / 2),
  185. // closestCenter 比较整个分区,标题只是抓取点;直接把标题移到目标中心会多算内容高度。
  186. targetY: Math.round(
  187. from.top +
  188. from.height / 2 +
  189. to.top +
  190. to.height / 2 -
  191. sourceSection.top -
  192. sourceSection.height / 2,
  193. ),
  194. right: Math.min(innerWidth - 5, Math.round(from.right + 120)),
  195. };
  196. },
  197. source,
  198. target,
  199. trigger,
  200. );
  201. await browser.performActions([
  202. {
  203. type: "pointer",
  204. id: "section-pointer",
  205. parameters: { pointerType: "mouse" },
  206. actions: [
  207. { type: "pointerMove", duration: 0, x: coords.x, y: coords.y },
  208. { type: "pointerDown", button: 0 },
  209. { type: "pointerMove", duration: 100, x: coords.x + 8, y: coords.y },
  210. { type: "pointerMove", duration: 250, x: coords.right, y: coords.targetY },
  211. ],
  212. },
  213. ]);
  214. }
  215. async function assertDragLayout(source: string) {
  216. await $(indicator).waitForDisplayed();
  217. const state = await browser.execute(
  218. (source, indicator) => {
  219. const section = document.querySelector<HTMLElement>(source)!;
  220. const line = document.querySelector<HTMLElement>(indicator)!;
  221. const reference = document.createElement("span");
  222. reference.className = "bg-icon-blue";
  223. line.append(reference);
  224. const expectedColor = getComputedStyle(reference).backgroundColor;
  225. reference.remove();
  226. const scrolls: Array<{ width: number; scrollWidth: number; overflowX: string }> = [];
  227. for (let node = section.parentElement; node; node = node.parentElement) {
  228. if (getComputedStyle(node).overflowY === "auto")
  229. scrolls.push({
  230. width: node.clientWidth,
  231. scrollWidth: node.scrollWidth,
  232. overflowX: getComputedStyle(node).overflowX,
  233. });
  234. }
  235. return {
  236. count: document.querySelectorAll(indicator).length,
  237. x: new DOMMatrix(getComputedStyle(section).transform).m41,
  238. height: line.getBoundingClientRect().height,
  239. color: getComputedStyle(line).backgroundColor,
  240. expectedColor,
  241. pointerEvents: getComputedStyle(line).pointerEvents,
  242. circle: line.firstElementChild!.getBoundingClientRect().width,
  243. scrolls,
  244. };
  245. },
  246. source,
  247. indicator,
  248. );
  249. expect(state.count).toBe(1);
  250. expect(state.x).toBe(0);
  251. expect(state.height).toBe(2);
  252. expect(state.color).toBe(state.expectedColor);
  253. expect(state.pointerEvents).toBe("none");
  254. expect(state.circle).toBe(8);
  255. expect(state.scrolls.length).toBeGreaterThan(0);
  256. for (const scroll of state.scrolls) {
  257. expect(scroll.overflowX).toBe("hidden");
  258. expect(scroll.scrollWidth).toBeLessThanOrEqual(scroll.width + 1);
  259. }
  260. }
  261. async function emulate(width?: number) {
  262. await browser.electron.execute(async (electron, width) => {
  263. const window = electron.BrowserWindow.getAllWindows().find(
  264. (item) => !item.isDestroyed() && item.isVisible(),
  265. )!;
  266. if (!window.webContents.debugger.isAttached()) window.webContents.debugger.attach("1.3");
  267. await window.webContents.debugger.sendCommand(
  268. width ? "Emulation.setDeviceMetricsOverride" : "Emulation.clearDeviceMetricsOverride",
  269. width ? { width, height: 844, deviceScaleFactor: 1, mobile: false } : {},
  270. );
  271. }, width);
  272. }