plugin-update-all.test.mjs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. import assert from "node:assert/strict";
  2. import { after, before, test } from "node:test";
  3. import { mkdir, readFile, writeFile } 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/plugin-update-all.fixture.tsx";
  12. const artifacts = join(root, "packages/desktop/.e2e-artifacts/plugin-update-all-context");
  13. let server, browser, origin;
  14. before(async () => {
  15. await mkdir(artifacts, { recursive: true });
  16. server = await createServer({
  17. root,
  18. configFile: false,
  19. plugins: [
  20. {
  21. name: "plugin-update-all-fixture",
  22. enforce: "pre",
  23. async load(id) {
  24. if (id.endsWith("/settings/PluginsSection.tsx"))
  25. return `${await readFile(id, "utf8")}\nexport { PluginList };`;
  26. },
  27. configureServer(vite) {
  28. vite.middlewares.use(async (req, res, next) => {
  29. if (!req.url?.startsWith("/fixture?")) return next();
  30. res.setHeader("Content-Type", "text/html");
  31. res.end(
  32. await vite.transformIndexHtml(
  33. req.url,
  34. `<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>`,
  35. ),
  36. );
  37. });
  38. },
  39. },
  40. react(),
  41. tailwindcss(),
  42. ],
  43. resolve: { alias: { "@": `${root}packages/ui/src` } },
  44. optimizeDeps: { entries: [entry.slice(1)] },
  45. server: { host: "127.0.0.1", port: 0, watch: null, hmr: false },
  46. });
  47. await server.listen();
  48. origin = server.resolvedUrls.local[0];
  49. browser = await chromium.launch(
  50. process.env.CHROME_EXECUTABLE_PATH
  51. ? { executablePath: process.env.CHROME_EXECUTABLE_PATH }
  52. : { channel: "chrome" },
  53. );
  54. });
  55. after(async () => {
  56. await browser?.close();
  57. await server?.close();
  58. });
  59. for (const [width, locale, theme] of [
  60. [1200, "en-US", "light"],
  61. [390, "zh-CN", "dark"],
  62. ]) {
  63. for (const change of ["replace", "scope", "unmount", "unchanged"]) {
  64. test(`update-all ${change} ${width}/${locale}/${theme}`, { timeout: 60000 }, async () => {
  65. const page = await browser.newPage({ viewport: { width, height: 900 } });
  66. const logs = [],
  67. errors = [];
  68. page.on("console", (message) => logs.push(message.text()));
  69. page.on("pageerror", (error) => errors.push(error.message));
  70. const label = `${change}-${width}-${locale}-${theme}`;
  71. try {
  72. await page.goto(
  73. `${origin}fixture?locale=${locale}&theme=${theme}&mobile=${width < 600 ? 1 : 0}`,
  74. );
  75. const update = page.getByTestId("plugin-settings-update-all");
  76. await update.click();
  77. await page.waitForFunction(() =>
  78. document
  79. .querySelector('[data-testid="calls"]')
  80. .textContent.includes("original:first@fixture"),
  81. );
  82. assert.equal(await update.isDisabled(), true);
  83. if (change !== "unchanged") await page.getByTestId(change).click();
  84. if (change === "scope") await page.getByTestId(change).click();
  85. await page.getByTestId("release").click();
  86. const toast = page.locator("#zcode-toast-host");
  87. await toast
  88. .getByText(
  89. change === "unchanged"
  90. ? locale === "zh-CN"
  91. ? /已更新 2/
  92. : /Updated 2/
  93. : locale === "zh-CN"
  94. ? /已中止/
  95. : /stopped/,
  96. )
  97. .waitFor({ timeout: 5000 });
  98. assert.deepEqual(
  99. JSON.parse(await page.getByTestId("calls").textContent()),
  100. change === "unchanged"
  101. ? ["original:first@fixture", "original:second@fixture"]
  102. : ["original:first@fixture"],
  103. );
  104. assert.deepEqual(errors, []);
  105. await page.screenshot({ path: join(artifacts, `${label}.png`) });
  106. } finally {
  107. await writeFile(join(artifacts, `${label}.log`), [...logs, ...errors].join("\n"));
  108. await page.close();
  109. }
  110. });
  111. }
  112. }