webRemoteControlBootstrapShell.test.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import { readFileSync } from "node:fs";
  2. import { resolve } from "node:path";
  3. import { JSDOM } from "jsdom";
  4. import { describe, expect, it } from "vitest";
  5. function readWebIndexHtml(): string {
  6. return readFileSync(resolve(process.cwd(), "packages/web/index.html"), "utf8");
  7. }
  8. function readWorkspaceFile(path: string): Buffer {
  9. return readFileSync(resolve(process.cwd(), path));
  10. }
  11. function readWorkspaceFileDataUrl(path: string, mimeType: string): string {
  12. return `data:${mimeType};base64,${readWorkspaceFile(path).toString("base64")}`;
  13. }
  14. function readCssRule(source: string, selector: string): string {
  15. const escapedSelector = selector.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
  16. const match = source.match(new RegExp(`${escapedSelector}\\s*\\{(?<body>[^}]*)\\}`));
  17. return match?.groups?.body ?? "";
  18. }
  19. function createBootstrapDocument(theme: "zai-dark" | "zai-light"): JSDOM {
  20. return new JSDOM(readWebIndexHtml(), {
  21. beforeParse(window) {
  22. window.localStorage.setItem("zcode-theme", theme);
  23. Object.defineProperty(window, "matchMedia", {
  24. configurable: true,
  25. value: () => ({ matches: false }),
  26. });
  27. },
  28. runScripts: "dangerously",
  29. url: "https://zcode.example/remote",
  30. });
  31. }
  32. describe("web remote control bootstrap shell", () => {
  33. it("内嵌与桌面 App 同源的浏览器标签图标", () => {
  34. const html = readWebIndexHtml();
  35. const document = new JSDOM(html).window.document;
  36. const icon = document.querySelector<HTMLLinkElement>('link[rel="icon"][type="image/png"]');
  37. expect(icon?.href).toBe(readWorkspaceFileDataUrl("public/logo/icons/32x32.png", "image/png"));
  38. expect(icon?.getAttribute("sizes")).toBe("32x32");
  39. expect(readWorkspaceFile("packages/web/public/favicon.ico")).toEqual(
  40. readWorkspaceFile("packages/desktop/build/icon.ico"),
  41. );
  42. expect(readWorkspaceFile("public/logo/icons/icon.ico")).toEqual(
  43. readWorkspaceFile("packages/desktop/build/icon.ico"),
  44. );
  45. });
  46. it("预渲染 loading 容器自身兜底主题背景,避免全局透明 body 露出白底", () => {
  47. const html = readWebIndexHtml();
  48. const loadingRule = readCssRule(html, ".zcode-boot-loading");
  49. expect(loadingRule).toContain("background: var(--zcode-bootstrap-bg");
  50. expect(loadingRule).toContain("#0f172a");
  51. });
  52. it.each([
  53. ["zai-dark", "dark", "#161616"],
  54. ["zai-light", "light", "#f8f8f8"],
  55. ] as const)(
  56. "在业务 bundle 执行前把 %s 同步到浏览器主题表面",
  57. (storedTheme, resolvedTheme, expectedColor) => {
  58. const dom = createBootstrapDocument(storedTheme);
  59. const { document } = dom.window;
  60. expect(document.documentElement.getAttribute("data-zcode-browser-theme-surface")).toBe(
  61. resolvedTheme,
  62. );
  63. expect(document.querySelector('meta[name="theme-color"]')?.getAttribute("content")).toBe(
  64. expectedColor,
  65. );
  66. expect(document.querySelector('meta[name="color-scheme"]')?.getAttribute("content")).toBe(
  67. resolvedTheme,
  68. );
  69. dom.window.close();
  70. },
  71. );
  72. it("Web 根表面以语义背景覆盖 Electron 透明根背景", () => {
  73. const html = readWebIndexHtml();
  74. const browserSurfaceRule = readCssRule(html, "html[data-zcode-browser-theme-surface] #root");
  75. expect(html).toContain("html[data-zcode-browser-theme-surface] body");
  76. expect(browserSurfaceRule).toContain("background: var(--color-background");
  77. expect(browserSurfaceRule).toContain("!important");
  78. });
  79. });