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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. const menuNames = () =>
  31. browser.execute(() =>
  32. [
  33. ...document.querySelectorAll('[data-testid="composer-project-menu"] [data-project-id]'),
  34. ].map((n) => n.getAttribute("data-project-name")),
  35. );
  36. expect(await menuNames()).toEqual(sidebarNames);
  37. await menu.$("input").setValue(names[6]!);
  38. expect(await menuNames()).toEqual([names[6]]);
  39. await browser.saveScreenshot(
  40. join(process.env.ZCODE_E2E_ARTIFACT_DIR!, "project-composer-search.png"),
  41. );
  42. await menu.$("[data-project-id]").click();
  43. const trigger = $('[data-testid="composer-workspace-trigger"]');
  44. await browser.waitUntil(async () => (await trigger.getText()).includes(names[6]!));
  45. const project = $(`[data-project-name="${names[6]}"]`);
  46. await project.$('[data-testid="virtual-project-row"]').click({ button: "right" });
  47. await $('[data-testid="virtual-project-edit"]').click();
  48. await $('[data-testid="virtual-project-name"]').setValue("E2E_PROJECT_PICKER_RENAMED");
  49. await $('[data-testid="virtual-project-save"]').click();
  50. await browser.waitUntil(async () =>
  51. (await trigger.getText()).includes("E2E_PROJECT_PICKER_RENAMED"),
  52. );
  53. await trigger.click();
  54. // WebDriver clearValue 不保证触发 React input;用真实键盘清空受控搜索框。
  55. await menu.$("input").click();
  56. await browser.keys([process.platform === "darwin" ? "Meta" : "Control", "a"]);
  57. await browser.keys("Backspace");
  58. await browser.waitUntil(async () => (await menuNames()).length === sidebarNames.length);
  59. await browser.saveScreenshot(
  60. join(process.env.ZCODE_E2E_ARTIFACT_DIR!, "project-composer-directory.png"),
  61. );
  62. const checked = menu.$('[data-project-id][aria-checked="true"]');
  63. expect(await checked.getAttribute("data-project-name")).toBe("E2E_PROJECT_PICKER_RENAMED");
  64. expect((await menuNames()).includes(names[6]!)).toBe(false);
  65. await browser.saveScreenshot(
  66. join(process.env.ZCODE_E2E_ARTIFACT_DIR!, "project-composer-directory.png"),
  67. );
  68. await browser.keys("Escape");
  69. await $(
  70. '[data-project-name="E2E_PROJECT_PICKER_RENAMED"] [data-testid="virtual-project-row"]',
  71. ).click({ button: "right" });
  72. await $('[data-testid="virtual-project-delete"]').click();
  73. await $('[data-project-name="E2E_PROJECT_PICKER_RENAMED"]').waitForExist({ reverse: true });
  74. await trigger.click();
  75. expect(await menuNames()).not.toContain("E2E_PROJECT_PICKER_RENAMED");
  76. await captureMenuLayouts();
  77. await browser.keys("Escape");
  78. });
  79. });
  80. async function captureMenuLayouts() {
  81. const originalTheme = await browser.execute(() =>
  82. (window as typeof window & { __testActions: { getTheme(): string } }).__testActions.getTheme(),
  83. );
  84. const emulate = (width?: number) =>
  85. browser.electron.execute(async (electron, width) => {
  86. const window = electron.BrowserWindow.getAllWindows().find(
  87. (w) => !w.isDestroyed() && w.isVisible(),
  88. )!;
  89. if (!window.webContents.debugger.isAttached()) window.webContents.debugger.attach("1.3");
  90. await window.webContents.debugger.sendCommand(
  91. width ? "Emulation.setDeviceMetricsOverride" : "Emulation.clearDeviceMetricsOverride",
  92. width ? { width, height: 844, deviceScaleFactor: 1, mobile: false } : {},
  93. );
  94. }, width);
  95. const theme = (value: string) =>
  96. browser.execute(
  97. (value) =>
  98. (
  99. window as typeof window & { __testActions: { setTheme(value: string): void } }
  100. ).__testActions.setTheme(value),
  101. value,
  102. );
  103. try {
  104. for (const width of [1200, 390]) {
  105. await emulate(width);
  106. await browser.waitUntil(() => browser.execute((expected) => innerWidth === expected, width));
  107. for (const value of ["zai-light", "zai-dark"]) {
  108. await theme(value);
  109. await browser.waitUntil(() =>
  110. browser.execute(
  111. (value) => document.documentElement.classList.contains(`theme-${value}`),
  112. value,
  113. ),
  114. );
  115. // 只验证共享菜单窄屏布局,不把设备模拟当成手机 shared-host 验证。
  116. const bounds = await browser.execute(() => {
  117. const menu = document.querySelector('[data-testid="composer-project-menu"]')!;
  118. const footer = menu.querySelector('[data-testid="composer-work-outside-project"]')!;
  119. const list = menu.querySelector("[data-project-id]")!.closest(".overflow-y-auto")!;
  120. const box = menu.getBoundingClientRect();
  121. return {
  122. left: box.left,
  123. right: box.right,
  124. top: box.top,
  125. bottom: box.bottom,
  126. width: innerWidth,
  127. height: innerHeight,
  128. footerBottom: footer.getBoundingClientRect().bottom,
  129. scrolls: getComputedStyle(list).overflowY === "auto",
  130. };
  131. });
  132. expect(bounds.left).toBeGreaterThanOrEqual(0);
  133. expect(bounds.right).toBeLessThanOrEqual(bounds.width);
  134. expect(bounds.top).toBeGreaterThanOrEqual(0);
  135. expect(bounds.bottom).toBeLessThanOrEqual(bounds.height);
  136. expect(bounds.footerBottom).toBeLessThanOrEqual(bounds.bottom);
  137. expect(bounds.scrolls).toBe(true);
  138. await browser.saveScreenshot(
  139. join(process.env.ZCODE_E2E_ARTIFACT_DIR!, `project-composer-${width}-${value}.png`),
  140. );
  141. }
  142. }
  143. } finally {
  144. await emulate();
  145. await theme(originalTheme);
  146. }
  147. }