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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. import { access, readFile } from "node:fs/promises";
  2. import { join } from "node:path";
  3. import {
  4. clearAppData,
  5. getE2EAppDataPaths,
  6. waitForDefaultWorkspaceReady,
  7. } from "../helpers/desktop-app.js";
  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. const win = electron.BrowserWindow.getFocusedWindow();
  19. if (!win || win.isDestroyed()) throw new Error("没有找到 Electron 主窗口");
  20. const current = win.getNormalBounds();
  21. const requested = { width: current.width + 80, height: current.height + 40 };
  22. // 当前 Electron 的 WebDriver window/rect 命令不支持 Browser.getWindowForTarget,
  23. // 直接调用原生 API 触发与用户拖拽一致的 resize 事件。
  24. win.setSize(requested.width, requested.height);
  25. const applied = win.getNormalBounds();
  26. return { width: applied.width, height: applied.height };
  27. });
  28. await browser.waitUntil(
  29. async () => {
  30. const settings = await readSettings(paths.appDataDir);
  31. const size = settings.desktopWindowSize;
  32. return (
  33. size?.width === targetSize.width &&
  34. size.height === targetSize.height &&
  35. size.maximized === false
  36. );
  37. },
  38. {
  39. timeout: 15_000,
  40. timeoutMsg: "窗口 resize 防抖后没有将尺寸写入 setting.json",
  41. },
  42. );
  43. expect(await pathExists(`${join(paths.appDataDir, "setting.json")}.lock`)).toBe(false);
  44. });
  45. });
  46. async function readSettings(appDataDir: string): Promise<{
  47. desktopWindowSize?: { width?: number; height?: number; maximized?: boolean };
  48. }> {
  49. try {
  50. return JSON.parse(await readFile(join(appDataDir, "setting.json"), "utf-8")) as {
  51. desktopWindowSize?: { width?: number; height?: number; maximized?: boolean };
  52. };
  53. } catch {
  54. return {};
  55. }
  56. }
  57. async function pathExists(path: string): Promise<boolean> {
  58. try {
  59. await access(path);
  60. return true;
  61. } catch {
  62. return false;
  63. }
  64. }