plugin-ui-launcher.test.mjs 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. import assert from "node:assert/strict";
  2. import { before, after, 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 { tmpdir } from "node:os";
  7. import { createServer } from "vite";
  8. import react from "@vitejs/plugin-react";
  9. import tailwindcss from "@tailwindcss/vite";
  10. import { chromium } from "playwright-core";
  11. const root = fileURLToPath(new URL("../../../../../../", import.meta.url));
  12. const entry = "/packages/ui/test/browser/manual-review/pending/plugin-ui-launcher.fixture.tsx";
  13. const artifacts = join(tmpdir(), "plugin-ui-launcher");
  14. let server, browser, origin;
  15. before(async () => {
  16. await mkdir(artifacts, { recursive: true });
  17. server = await createServer({
  18. root,
  19. configFile: false,
  20. plugins: [
  21. react(),
  22. tailwindcss(),
  23. {
  24. name: "plugin-ui-launcher-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 },
  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. for (const width of [390, 1200])
  56. for (const theme of ["light", "dark"])
  57. for (const locale of ["zh-CN", "en-US"])
  58. test(`MCP launcher ${width} ${theme} ${locale}`, async () => {
  59. const page = await browser.newPage({ viewport: { width, height: 500 } });
  60. const errors = [];
  61. page.on("pageerror", (error) => errors.push(error.message));
  62. await page.goto(`${origin}fixture?theme=${theme}&locale=${locale}&long=1`);
  63. const card = page.getByTestId("plugin-ui-side-pane-placeholder");
  64. await card.waitFor();
  65. assert.equal(await page.getByTestId("plugin-ui-card").count(), 0);
  66. assert.equal(await page.getByTestId("plugin-ui-pin-toggle").count(), 0);
  67. await card.locator("img").waitFor();
  68. await card.locator("img").evaluate((img) => img.decode());
  69. assert.equal(
  70. await card.locator("img").evaluate((img) => img.complete && img.naturalWidth > 0),
  71. true,
  72. );
  73. const button = page.getByRole("button", {
  74. name: locale === "zh-CN" ? "在侧栏打开" : "Open in side pane",
  75. exact: true,
  76. });
  77. await button.focus();
  78. await page.keyboard.press("Enter");
  79. await card
  80. .getByText(locale === "zh-CN" ? "已在侧栏打开" : "Opened in side pane", { exact: true })
  81. .waitFor();
  82. assert.equal(
  83. await page.evaluate(() => document.documentElement.scrollWidth <= innerWidth),
  84. true,
  85. );
  86. const box = await button.boundingBox();
  87. assert.ok(box && box.x >= 0 && box.x + box.width <= width);
  88. assert.deepEqual(errors, []);
  89. await page.screenshot({ path: join(artifacts, `${width}-${theme}-${locale}.png`) });
  90. await card.screenshot({ path: join(artifacts, `card-${width}-${theme}-${locale}.png`) });
  91. await page.close();
  92. });