channel-mention.test.mjs 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. import assert from "node:assert/strict";
  2. import { after, before, test } from "node:test";
  3. import { mkdir } from "node:fs/promises";
  4. import { join } from "node:path";
  5. import { tmpdir } from "node:os";
  6. import { fileURLToPath } from "node:url";
  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/channel-mention.fixture.tsx";
  13. let server;
  14. let browser;
  15. let origin;
  16. before(async () => {
  17. server = await createServer({
  18. root,
  19. configFile: false,
  20. plugins: [
  21. react(),
  22. tailwindcss(),
  23. {
  24. name: "responsive-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. `<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. const artifacts = join(tmpdir(), "zcode-channel-mention");
  56. for (const width of [390, 1200]) {
  57. for (const theme of ["light", "dark"]) {
  58. test(`BOT-UI-MN ${width}/${theme}`, async () => {
  59. const page = await browser.newPage({ viewport: { width, height: 800 } });
  60. page.setDefaultTimeout(8000);
  61. try {
  62. await page.goto(`${origin}fixture?theme=${theme}`);
  63. await page.locator('[data-channel-mention="feishu"]').first().waitFor();
  64. assert.equal(await page.locator('[data-channel-mention="feishu"]').count(), 3);
  65. assert.equal(await page.locator('[data-testid="typed"] [data-channel-mention]').count(), 0);
  66. assert.equal(
  67. await page.locator("body").evaluate((el) => el.scrollWidth <= window.innerWidth),
  68. true,
  69. );
  70. const chip = page.locator('[data-channel-mention="feishu"]').first();
  71. assert.match(await chip.textContent(), /Ryan Bot/);
  72. assert.equal(await chip.evaluate((el) => getComputedStyle(el).fontStyle), "normal");
  73. assert.equal(
  74. await page
  75. .locator("body")
  76. .textContent()
  77. .then((text) => text.includes("ou_target")),
  78. false,
  79. );
  80. await mkdir(artifacts, { recursive: true });
  81. await page.screenshot({ path: join(artifacts, `${width}-${theme}.png`), fullPage: true });
  82. } finally {
  83. await page.close();
  84. }
  85. });
  86. }
  87. }