model-submenu-width.test.mjs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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/model-submenu-width.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-model-submenu-width");
  56. for (const width of [390, 1200]) {
  57. for (const theme of ["light", "dark"]) {
  58. for (const name of ["short", "long", "extreme"]) {
  59. test(`model submenu ${width}/${theme}/${name}`, async () => {
  60. const page = await browser.newPage({ viewport: { width, height: 700 } });
  61. const errors = [];
  62. page.on("pageerror", (error) => errors.push(error.message));
  63. try {
  64. await page.goto(`${origin}fixture?theme=${theme}&name=${name}`);
  65. await page.getByRole("button", { name: "Models", exact: true }).click();
  66. const provider = page.locator("[data-model-provider-key=custom]");
  67. await provider.focus();
  68. await page.keyboard.press("ArrowRight");
  69. const menu = page.locator('[data-slot="dropdown-menu-sub-content"]');
  70. await menu.waitFor();
  71. await menu.evaluate(async (element) => {
  72. await Promise.allSettled(
  73. element.getAnimations().map((animation) => animation.finished),
  74. );
  75. });
  76. // Radix 在内容测量后更新碰撞位置;等待定位与入场动画完成再检查几何边界。
  77. await page.waitForFunction(() => {
  78. const element = document.querySelector('[data-slot="dropdown-menu-sub-content"]');
  79. if (!element) return false;
  80. const rect = element.getBoundingClientRect();
  81. return (
  82. rect.left >= 0 &&
  83. rect.right <= innerWidth &&
  84. element.getAnimations().every((animation) => animation.playState !== "running")
  85. );
  86. });
  87. const label = menu.locator("span[title]").filter({ hasText: "GLM" }).last();
  88. const metrics = await label.evaluate((element) => {
  89. const menu = element.closest('[data-slot="dropdown-menu-sub-content"]');
  90. const rect = menu.getBoundingClientRect();
  91. return {
  92. width: rect.width,
  93. left: rect.left,
  94. right: rect.right,
  95. clipped: element.scrollWidth > element.clientWidth,
  96. overflow: menu.scrollWidth > menu.clientWidth,
  97. };
  98. });
  99. assert.ok(metrics.left >= 0 && metrics.right <= width, JSON.stringify(metrics));
  100. assert.equal(metrics.overflow, false);
  101. if (width === 1200 && name !== "extreme") {
  102. assert.ok(metrics.width >= 192);
  103. assert.equal(metrics.clipped, false, JSON.stringify(metrics));
  104. if (name === "long") assert.ok(metrics.width > 192);
  105. }
  106. await mkdir(artifacts, { recursive: true });
  107. await page.screenshot({ path: join(artifacts, `${width}-${theme}-${name}.png`) });
  108. await menu.getByRole("menuitemradio").last().click();
  109. await page.getByTestId("selected").filter({ hasText: "vision" }).waitFor();
  110. assert.deepEqual(errors, []);
  111. } finally {
  112. await page.close();
  113. }
  114. });
  115. }
  116. }
  117. }