font-smoothing.test.mjs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. import assert from "node:assert/strict";
  2. import { test } from "node:test";
  3. import { fileURLToPath } from "node:url";
  4. import { mkdir } from "node:fs/promises";
  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 = join(root, "font-smoothing-preview.tsx");
  12. const artifacts = join(root, "packages/desktop/.e2e-artifacts/font-smoothing");
  13. const fixture = `
  14. import { createRoot } from "react-dom/client";
  15. import { useStore } from "zustand";
  16. import { ZCodeIntlProvider } from "@/i18n/IntlProvider.js";
  17. import { AppearanceSectionContent } from "@/settingsCodePreview.js";
  18. import { createZCodeStore } from "@/store/index.js";
  19. import "@/styles.css";
  20. const query = new URLSearchParams(location.search);
  21. const platform = query.get("platform");
  22. document.documentElement.classList.toggle("platform-mac-desktop", platform === "mac");
  23. const channel = new BroadcastChannel("font-smoothing-test");
  24. window.sent = 0;
  25. const store = createZCodeStore({send(message) { window.sent++; channel.postMessage(message); }, onMessage(callback) {channel.onmessage = event => callback(event.data); return () => {channel.onmessage = null;};}});
  26. store.getState().setTheme("zai-" + query.get("theme"));
  27. window.store = store;
  28. function Fixture() {
  29. const state = useStore(store);
  30. return <ZCodeIntlProvider initialLocale={query.get("locale")}>
  31. <main className="bg-background text-foreground p-4" style={{height:"100vh",overflow:"auto"}}>
  32. <AppearanceSectionContent isMacDesktop={platform === "mac"}
  33. fontSmoothingEnabled={state.fontSmoothingEnabled} setFontSmoothingEnabled={state.setFontSmoothingEnabled}
  34. theme={state.theme} setTheme={state.setTheme} uiFontSizePx={state.uiFontSizePx} setUiFontSizePx={state.setUiFontSizePx}
  35. codePreviewSettings={state.codePreviewSettings} setCodePreviewSettings={state.setCodePreviewSettings} />
  36. </main></ZCodeIntlProvider>;
  37. }
  38. createRoot(document.getElementById("root")).render(<Fixture />);
  39. `;
  40. test("macOS 字体平滑:切换、重载、多窗口与平台隔离", async () => {
  41. await mkdir(artifacts, { recursive: true });
  42. const server = await createServer({
  43. root,
  44. configFile: false,
  45. plugins: [
  46. {
  47. name: "font-smoothing-preview",
  48. resolveId(id) {
  49. if (id === "/font-smoothing-preview.tsx") return entry;
  50. },
  51. load(id) {
  52. if (id === entry) return fixture;
  53. },
  54. configureServer(vite) {
  55. vite.middlewares.use(async (req, res, next) => {
  56. if (!req.url?.startsWith("/fixture?")) return next();
  57. res.setHeader("Content-Type", "text/html");
  58. res.end(
  59. await vite.transformIndexHtml(
  60. req.url,
  61. '<html><head><meta name="viewport" content="width=device-width, initial-scale=1" /></head><body><div id="root"></div><script type="module" src="/font-smoothing-preview.tsx"></script></body></html>',
  62. ),
  63. );
  64. });
  65. },
  66. },
  67. react(),
  68. tailwindcss(),
  69. ],
  70. resolve: { dedupe: ["react", "react-dom"], alias: { "@": join(root, "packages/ui/src") } },
  71. optimizeDeps: {
  72. entries: [],
  73. include: [
  74. "react",
  75. "react-dom/client",
  76. "react/jsx-runtime",
  77. "class-variance-authority",
  78. "clsx",
  79. "radix-ui",
  80. "tailwind-merge",
  81. "zod",
  82. "zustand",
  83. "lucide-react",
  84. "@pierre/diffs/react",
  85. "@streamdown/mermaid",
  86. ],
  87. },
  88. server: { host: "127.0.0.1", port: 0, hmr: false },
  89. });
  90. let browser;
  91. try {
  92. await server.listen();
  93. browser = await chromium.launch({ channel: "chrome", headless: true });
  94. const context = await browser.newContext();
  95. const url = (platform, theme = "light", locale = "zh-CN") =>
  96. `${server.resolvedUrls.local[0]}fixture?platform=${platform}&theme=${theme}&locale=${locale}`;
  97. const page = await context.newPage();
  98. page.on("pageerror", (error) => process.stderr.write(String(error) + "\n"));
  99. await page.goto(url("mac"));
  100. const toggle = page.getByTestId("font-smoothing-switch");
  101. await toggle.waitFor();
  102. assert.equal(await toggle.getAttribute("aria-checked"), "true");
  103. const read = (p) =>
  104. p.evaluate(() => getComputedStyle(document.body).getPropertyValue("-webkit-font-smoothing"));
  105. assert.equal(await read(page), "antialiased");
  106. await toggle.click();
  107. assert.equal(await read(page), "auto");
  108. const other = await context.newPage();
  109. await other.goto(url("mac"));
  110. await other.getByTestId("font-smoothing-switch").waitFor();
  111. const sentBefore = await other.evaluate(() => window.sent);
  112. await toggle.click();
  113. await other.waitForFunction(() => window.store.getState().fontSmoothingEnabled === true);
  114. assert.equal(await read(other), "antialiased");
  115. assert.equal(await other.evaluate(() => window.sent), sentBefore);
  116. await page.reload();
  117. await toggle.waitFor();
  118. assert.equal(await toggle.getAttribute("aria-checked"), "true");
  119. assert.equal(await read(page), "antialiased");
  120. for (const width of [1200, 390])
  121. for (const theme of ["light", "dark"])
  122. for (const locale of ["zh-CN", "en-US"]) {
  123. await page.setViewportSize({ width, height: 900 });
  124. await page.goto(url("mac", theme, locale));
  125. await toggle.waitFor();
  126. assert.equal(await read(page), "antialiased");
  127. const row = page.locator('[data-settings-search-id="settings.fontSmoothing"]');
  128. await row.scrollIntoViewIfNeeded();
  129. assert.ok(await row.evaluate((e) => e.scrollWidth <= e.clientWidth));
  130. await page.screenshot({ path: join(artifacts, `${width}-${theme}-${locale}.png`) });
  131. }
  132. for (const platform of ["windows", "linux", "web"]) {
  133. await page.goto(url(platform));
  134. await page.locator('[data-settings-search-id="settings.uiFontSize"]').waitFor();
  135. assert.equal(await toggle.count(), 0);
  136. assert.equal(await read(page), "auto");
  137. }
  138. await page.goto(url("mac"));
  139. await toggle.waitFor();
  140. await toggle.click();
  141. await other.waitForFunction(() => window.store.getState().fontSmoothingEnabled === false);
  142. assert.equal(await read(other), "auto");
  143. await page.reload();
  144. await toggle.waitFor();
  145. assert.equal(await toggle.getAttribute("aria-checked"), "false");
  146. await context.close();
  147. } finally {
  148. await browser?.close();
  149. await server.close();
  150. }
  151. });