settings-window-size-persistence.test.ts 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import { access, readFile } from "node:fs/promises";
  2. import { join } from "node:path";
  3. import {
  4. clearAppData,
  5. getE2EAppDataPaths,
  6. waitForDefaultWorkspaceReady,
  7. } from "#e2e/helpers/desktop-app";
  8. describe("桌面主窗口尺寸持久化 E2E", () => {
  9. after(async () => {
  10. await browser.electron.restoreAllMocks();
  11. await clearAppData();
  12. });
  13. it("拖拽调整后保存尺寸,不依赖 close/quit 写入", async function () {
  14. this.timeout(60_000);
  15. await waitForDefaultWorkspaceReady(30_000);
  16. const paths = getE2EAppDataPaths();
  17. const targetSize = await browser.electron.execute((electron) => {
  18. // 后台 E2E 不保证 OS 焦点;按主 renderer 定位,避免 getFocusedWindow 返回空。
  19. const win = electron.BrowserWindow.getAllWindows().find((window) =>
  20. /\/renderer\/index\.html(?:[?#]|$)/u.test(window.webContents.getURL()),
  21. );
  22. if (!win || win.isDestroyed()) throw new Error("没有找到 Electron 主窗口");
  23. win.blur();
  24. const current = win.getNormalBounds();
  25. const requested = { width: current.width + 80, height: current.height + 40 };
  26. // 当前 Electron 的 WebDriver window/rect 命令不支持 Browser.getWindowForTarget,
  27. // 直接调用原生 API 触发与用户拖拽一致的 resize 事件。
  28. win.setSize(requested.width, requested.height);
  29. const applied = win.getNormalBounds();
  30. return { width: applied.width, height: applied.height };
  31. });
  32. await browser.waitUntil(
  33. async () => {
  34. const settings = await readSettings(paths.appDataDir);
  35. const size = settings.desktopWindowSize;
  36. return (
  37. size?.width === targetSize.width &&
  38. size.height === targetSize.height &&
  39. size.maximized === false
  40. );
  41. },
  42. {
  43. timeout: 15_000,
  44. timeoutMsg: "窗口 resize 防抖后没有将尺寸写入 setting.json",
  45. },
  46. );
  47. expect(await pathExists(`${join(paths.appDataDir, "setting.json")}.lock`)).toBe(false);
  48. });
  49. });
  50. async function readSettings(appDataDir: string): Promise<{
  51. desktopWindowSize?: { width?: number; height?: number; maximized?: boolean };
  52. }> {
  53. try {
  54. return JSON.parse(await readFile(join(appDataDir, "setting.json"), "utf-8")) as {
  55. desktopWindowSize?: { width?: number; height?: number; maximized?: boolean };
  56. };
  57. } catch {
  58. return {};
  59. }
  60. }
  61. async function pathExists(path: string): Promise<boolean> {
  62. try {
  63. await access(path);
  64. return true;
  65. } catch {
  66. return false;
  67. }
  68. }