update-confirmation.test.mjs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. import assert from "node:assert/strict";
  2. import { before, after, test } from "node:test";
  3. import { mkdir } from "node:fs/promises";
  4. import { fileURLToPath } from "node:url";
  5. import { join } from "node:path";
  6. import { createServer } from "vite";
  7. import react from "@vitejs/plugin-react";
  8. import tailwindcss from "@tailwindcss/vite";
  9. import { chromium } from "playwright-core";
  10. const root = fileURLToPath(new URL("../../../../../../", import.meta.url));
  11. const entry = "/packages/ui/test/browser/manual-review/pending/update-confirmation.fixture.tsx";
  12. const artifacts = join(root, "packages/desktop/.e2e-artifacts/update-confirmation-browser");
  13. let server, browser, origin;
  14. before(async () => {
  15. await mkdir(artifacts, { recursive: true });
  16. server = await createServer({
  17. root,
  18. configFile: false,
  19. cacheDir: join(root, "node_modules/.vite-update-confirmation"),
  20. plugins: [
  21. react(),
  22. tailwindcss(),
  23. {
  24. name: "update-confirmation-fixture",
  25. configureServer(vite) {
  26. vite.middlewares.use(async (req, res, next) => {
  27. if (!req.url?.startsWith("/fixture?")) return next();
  28. res.setHeader("Content-Type", "text/html");
  29. res.end(
  30. await vite.transformIndexHtml(
  31. req.url,
  32. `<!DOCTYPE html><html><head><meta name="viewport" content="width=device-width, initial-scale=1" /></head><body><div id="root"></div><script type="module" src="${entry}"></script></body></html>`,
  33. ),
  34. );
  35. });
  36. },
  37. },
  38. ],
  39. resolve: { alias: { "@": `${root}packages/ui/src` } },
  40. optimizeDeps: { entries: [entry.slice(1)] },
  41. server: { host: "127.0.0.1", port: 0, hmr: false },
  42. });
  43. await server.listen();
  44. origin = server.resolvedUrls.local[0];
  45. browser = await chromium.launch(
  46. process.env.CHROME_EXECUTABLE_PATH
  47. ? { executablePath: process.env.CHROME_EXECUTABLE_PATH }
  48. : { channel: "chrome" },
  49. );
  50. });
  51. after(async () => {
  52. await browser?.close();
  53. await server?.close();
  54. });
  55. async function readLayout(dialog) {
  56. return dialog.evaluate((element) => {
  57. const footer = element.querySelector("[data-slot='dialog-footer']");
  58. const buttons = [...footer.querySelectorAll("button")];
  59. const title = element.querySelector("[data-slot='dialog-title']");
  60. const description = element.querySelector("[data-slot='dialog-description']");
  61. return {
  62. fits: element.scrollWidth <= element.clientWidth,
  63. width: element.getBoundingClientRect().width,
  64. left: element.getBoundingClientRect().left,
  65. right: element.getBoundingClientRect().right,
  66. footerWidth: footer.getBoundingClientRect().width,
  67. titleSize: parseFloat(getComputedStyle(title).fontSize),
  68. descriptionSize: parseFloat(getComputedStyle(description).fontSize),
  69. differentTextColors: getComputedStyle(title).color !== getComputedStyle(description).color,
  70. buttons: buttons.map((button) => {
  71. const rect = button.getBoundingClientRect();
  72. return {
  73. x: rect.x,
  74. top: rect.top,
  75. width: rect.width,
  76. height: parseFloat(getComputedStyle(button).height),
  77. };
  78. }),
  79. };
  80. });
  81. }
  82. for (const width of [390, 512, 1200])
  83. for (const theme of ["light", "dark"])
  84. for (const locale of ["zh-CN", "en-US"])
  85. test(`update confirmation ${width}/${theme}/${locale}`, { timeout: 90_000 }, async () => {
  86. const page = await browser.newPage({ viewport: { width, height: 600 } });
  87. const errors = [];
  88. page.on("pageerror", (error) => errors.push(error.message));
  89. try {
  90. await page.goto(`${origin}fixture?theme=${theme}&locale=${locale}`, { timeout: 60_000 });
  91. await page.getByTestId("open-update").click();
  92. const dialog = page.getByTestId("confirmation-fixture");
  93. await dialog.waitFor();
  94. await dialog.evaluate((element) =>
  95. Promise.all(element.getAnimations().map((animation) => animation.finished)),
  96. );
  97. const layout = await readLayout(dialog);
  98. assert.ok(layout.fits, "更新确认不能横向溢出");
  99. assert.ok(layout.width <= 448, "更新确认保持紧凑宽度");
  100. assert.ok(layout.left >= 16 && layout.right <= width - 16, "窄窗口保留16px侧边距");
  101. assert.equal(layout.buttons.length, 2);
  102. assert.ok(Math.abs(layout.buttons[0].top - layout.buttons[1].top) < 1, "按钮保持同排");
  103. assert.ok(layout.buttons[0].x < layout.buttons[1].x, "取消在前,确认在后");
  104. assert.ok(
  105. layout.buttons.every((button) => button.height === 32),
  106. "共享 lg 按钮为32px",
  107. );
  108. assert.ok(
  109. layout.buttons.every((button) => button.width < layout.footerWidth * 0.7),
  110. "按钮不铺满操作区",
  111. );
  112. assert.equal(layout.titleSize, 14);
  113. assert.equal(layout.descriptionSize, 12);
  114. assert.ok(layout.differentTextColors, "说明保留次级文字层级");
  115. assert.ok(!(await dialog.textContent()).includes("esc"), "更新确认隐藏快捷键文字");
  116. await page.screenshot({ path: join(artifacts, `${width}-${theme}-${locale}.png`) });
  117. await page.keyboard.press("Escape");
  118. await dialog.waitFor({ state: "hidden" });
  119. assert.equal(await page.getByTestId("choice").textContent(), "dismiss");
  120. await page.getByTestId("open-update").click();
  121. await dialog.waitFor();
  122. await page.keyboard.press("Enter");
  123. await dialog.waitFor({ state: "hidden" });
  124. assert.equal(await page.getByTestId("choice").textContent(), "confirm");
  125. await page.getByTestId("open-default").click();
  126. await dialog.waitFor();
  127. await dialog.evaluate((element) =>
  128. Promise.all(element.getAnimations().map((animation) => animation.finished)),
  129. );
  130. const original = await readLayout(dialog);
  131. assert.equal(original.titleSize, 16, "默认确认标题不受影响");
  132. assert.equal(original.descriptionSize, 14, "默认说明不受影响");
  133. assert.ok(
  134. original.buttons.every((button) => button.height === 36),
  135. "默认按钮高度不受影响",
  136. );
  137. if (width < 640)
  138. assert.ok(
  139. Math.abs(original.buttons[0].top - original.buttons[1].top) > 1,
  140. "默认窄屏堆叠不受影响",
  141. );
  142. assert.deepEqual(errors, []);
  143. } finally {
  144. await page.close();
  145. }
  146. });