conversation-session-project-composer.test.ts 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. import { join } from "node:path";
  2. import { waitForWorkspaceApp, DEFAULT_WORKSPACE } from "#e2e/helpers/desktop-app";
  3. describe("输入框项目选择器", () => {
  4. it("VP30:未打开项目与侧栏一致、搜索项目、零 root 选择与改名同步", async function () {
  5. this.timeout(120_000);
  6. await waitForWorkspaceApp(DEFAULT_WORKSPACE, 45_000);
  7. await browser.execute(() => {
  8. (
  9. window as Window & { __testActions?: { setLocale(locale: "zh-CN"): void } }
  10. ).__testActions!.setLocale("zh-CN");
  11. });
  12. const names = Array.from({ length: 7 }, (_, i) => `E2E_PROJECT_PICKER_${i}`);
  13. for (const name of names) {
  14. await $('[data-testid="virtual-project-add"]').click();
  15. await $('[data-testid="virtual-project-name"]').setValue(name);
  16. await $('[data-testid="virtual-project-save"]').click();
  17. await $(`[data-project-name="${name}"]`).waitForExist();
  18. // 列表先收到保存结果,编辑器遮罩在退出动画结束后才卸载;不能先点击下一个创建入口。
  19. await $('[data-slot="dialog-overlay"]').waitForExist({ reverse: true });
  20. }
  21. const sidebarNames = await browser.execute(() =>
  22. [...document.querySelectorAll("aside [data-project-name]")].map((n) =>
  23. n.getAttribute("data-project-name"),
  24. ),
  25. );
  26. await $('[data-testid="composer-workspace-trigger"]').click();
  27. const menu = $('[data-testid="composer-project-menu"]');
  28. await menu.waitForDisplayed();
  29. expect(await menu.$('input[placeholder="搜索项目或设备"]').isExisting()).toBe(true);
  30. // 原生拖拽命中不受 DOM 层级影响;CDP 点击可绕过它,必须同时检查菜单的 no-drag。
  31. expect(
  32. await browser.execute(() =>
  33. getComputedStyle(
  34. document.querySelector('[data-testid="composer-project-menu"]')!,
  35. ).getPropertyValue("-webkit-app-region"),
  36. ),
  37. ).toBe("no-drag");
  38. await menu.$("input").click();
  39. expect(
  40. await browser.execute(
  41. () =>
  42. document.activeElement ===
  43. document.querySelector('[data-testid="composer-project-menu"] input'),
  44. ),
  45. ).toBe(true);
  46. const menuNames = () =>
  47. browser.execute(() =>
  48. [
  49. ...document.querySelectorAll('[data-testid="composer-project-menu"] [data-project-id]'),
  50. ].map((n) => n.getAttribute("data-project-name")),
  51. );
  52. expect(await menuNames()).toEqual(sidebarNames);
  53. await menu.$("input").setValue(names[6]!);
  54. expect(await menuNames()).toEqual([names[6]]);
  55. await browser.saveScreenshot(
  56. join(process.env.ZCODE_E2E_ARTIFACT_DIR!, "project-composer-search.png"),
  57. );
  58. await menu.$("[data-project-id]").click();
  59. const trigger = $('[data-testid="composer-workspace-trigger"]');
  60. await browser.waitUntil(async () => (await trigger.getText()).includes(names[6]!));
  61. const project = $(`[data-project-name="${names[6]}"]`);
  62. await project.$('[data-testid="virtual-project-row"]').click({ button: "right" });
  63. await $('[data-testid="virtual-project-edit"]').click();
  64. await $('[data-testid="virtual-project-name"]').setValue("E2E_PROJECT_PICKER_RENAMED");
  65. await $('[data-testid="virtual-project-save"]').click();
  66. await browser.waitUntil(async () =>
  67. (await trigger.getText()).includes("E2E_PROJECT_PICKER_RENAMED"),
  68. );
  69. await trigger.click();
  70. // WebDriver clearValue 不保证触发 React input;用真实键盘清空受控搜索框。
  71. await menu.$("input").click();
  72. await browser.keys([process.platform === "darwin" ? "Meta" : "Control", "a"]);
  73. await browser.keys("Backspace");
  74. await browser.waitUntil(async () => (await menuNames()).length === sidebarNames.length);
  75. await browser.saveScreenshot(
  76. join(process.env.ZCODE_E2E_ARTIFACT_DIR!, "project-composer-directory.png"),
  77. );
  78. const checked = menu.$('[data-project-id][aria-checked="true"]');
  79. expect(await checked.getAttribute("data-project-name")).toBe("E2E_PROJECT_PICKER_RENAMED");
  80. expect((await menuNames()).includes(names[6]!)).toBe(false);
  81. await browser.saveScreenshot(
  82. join(process.env.ZCODE_E2E_ARTIFACT_DIR!, "project-composer-directory.png"),
  83. );
  84. await browser.keys("Escape");
  85. await $(
  86. '[data-project-name="E2E_PROJECT_PICKER_RENAMED"] [data-testid="virtual-project-row"]',
  87. ).click({ button: "right" });
  88. await $('[data-testid="virtual-project-delete"]').click();
  89. await $('[data-project-name="E2E_PROJECT_PICKER_RENAMED"]').waitForExist({ reverse: true });
  90. await trigger.click();
  91. expect(await menuNames()).not.toContain("E2E_PROJECT_PICKER_RENAMED");
  92. await captureMenuLayouts();
  93. await browser.keys("Escape");
  94. });
  95. });
  96. async function captureMenuLayouts() {
  97. const originalTheme = await browser.execute(() =>
  98. (window as typeof window & { __testActions: { getTheme(): string } }).__testActions.getTheme(),
  99. );
  100. const emulate = (width?: number) =>
  101. browser.electron.execute(async (electron, width) => {
  102. const window = electron.BrowserWindow.getAllWindows().find(
  103. (w) => !w.isDestroyed() && w.isVisible(),
  104. )!;
  105. if (!window.webContents.debugger.isAttached()) window.webContents.debugger.attach("1.3");
  106. await window.webContents.debugger.sendCommand(
  107. width ? "Emulation.setDeviceMetricsOverride" : "Emulation.clearDeviceMetricsOverride",
  108. width ? { width, height: 844, deviceScaleFactor: 1, mobile: false } : {},
  109. );
  110. }, width);
  111. const theme = (value: string) =>
  112. browser.execute(
  113. (value) =>
  114. (
  115. window as typeof window & { __testActions: { setTheme(value: string): void } }
  116. ).__testActions.setTheme(value),
  117. value,
  118. );
  119. try {
  120. for (const width of [1200, 390]) {
  121. await emulate(width);
  122. await browser.waitUntil(() => browser.execute((expected) => innerWidth === expected, width));
  123. for (const value of ["zai-light", "zai-dark"]) {
  124. await theme(value);
  125. await browser.waitUntil(() =>
  126. browser.execute(
  127. (value) => document.documentElement.classList.contains(`theme-${value}`),
  128. value,
  129. ),
  130. );
  131. // 只验证共享菜单窄屏布局,不把设备模拟当成手机 shared-host 验证。
  132. const bounds = await browser.execute(() => {
  133. const menu = document.querySelector('[data-testid="composer-project-menu"]')!;
  134. const footer = menu.querySelector('[data-testid="composer-work-outside-project"]')!;
  135. const list = menu.querySelector("[data-project-id]")!.closest(".overflow-y-auto")!;
  136. const box = menu.getBoundingClientRect();
  137. return {
  138. left: box.left,
  139. right: box.right,
  140. top: box.top,
  141. bottom: box.bottom,
  142. width: innerWidth,
  143. height: innerHeight,
  144. footerBottom: footer.getBoundingClientRect().bottom,
  145. scrolls: getComputedStyle(list).overflowY === "auto",
  146. };
  147. });
  148. expect(bounds.left).toBeGreaterThanOrEqual(0);
  149. expect(bounds.right).toBeLessThanOrEqual(bounds.width);
  150. expect(bounds.top).toBeGreaterThanOrEqual(0);
  151. expect(bounds.bottom).toBeLessThanOrEqual(bounds.height);
  152. expect(bounds.footerBottom).toBeLessThanOrEqual(bounds.bottom);
  153. expect(bounds.scrolls).toBe(true);
  154. await browser.saveScreenshot(
  155. join(process.env.ZCODE_E2E_ARTIFACT_DIR!, `project-composer-${width}-${value}.png`),
  156. );
  157. }
  158. }
  159. } finally {
  160. await emulate();
  161. await theme(originalTheme);
  162. }
  163. }