plugin-installed-strip.test.mjs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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/plugin-installed-strip.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-plugin-strip-clipping");
  56. for (const width of [390, 1200]) {
  57. for (const locale of ["en-US", "zh-CN"]) {
  58. for (const theme of ["light", "dark"]) {
  59. test(`PLM-CARD-ACTIONS ${width}/${locale}/${theme}`, 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?locale=${locale}&theme=${theme}`);
  65. const badges = page.getByTestId("plugin-store-card-update");
  66. await badges.first().waitFor();
  67. assert.equal(await page.getByTestId("plugin-store-installed-strip").count(), 0);
  68. // 插件管理入口已移到页头操作组;列表首行只有来源分段与搜索。
  69. assert.equal(await page.getByTestId("plugin-store-manage-open").count(), 0);
  70. assert.equal(await page.getByTestId("plugin-store-search").count(), 1);
  71. for (const badge of [badges.first(), badges.last()]) {
  72. await badge.scrollIntoViewIfNeeded();
  73. await badge.focus();
  74. const bounds = await badge.boundingBox();
  75. assert.ok(bounds.x >= 0 && bounds.x + bounds.width <= width);
  76. }
  77. await badges.last().click();
  78. assert.equal(
  79. await page.getByTestId("updated").textContent(),
  80. await badges.last().getAttribute("data-plugin-id"),
  81. );
  82. assert.equal(await page.getByTestId("opened").textContent(), "");
  83. assert.equal(
  84. await page.evaluate(() => document.documentElement.scrollWidth <= innerWidth),
  85. true,
  86. );
  87. assert.deepEqual(errors, []);
  88. await mkdir(artifacts, { recursive: true });
  89. await page.screenshot({ path: join(artifacts, `${width}-${locale}-${theme}.png`) });
  90. } finally {
  91. await page.close();
  92. }
  93. });
  94. }
  95. }
  96. }
  97. for (const [width, locale, theme] of [
  98. [1200, "en-US", "light"],
  99. [390, "zh-CN", "dark"],
  100. ]) {
  101. test(`PLM-UI-PREVIEW ${width}/${locale}/${theme}`, async () => {
  102. const page = await browser.newPage({ viewport: { width, height: 800 } });
  103. const errors = [];
  104. page.on("pageerror", (error) => errors.push(error.message));
  105. await page.route("https://example.com/panel.png", (route) =>
  106. route.fulfill({
  107. contentType: "image/png",
  108. body: Buffer.from(
  109. "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/x8AAwMCAO+jH+cAAAAASUVORK5CYII=",
  110. "base64",
  111. ),
  112. }),
  113. );
  114. try {
  115. await page.goto(`${origin}fixture?detail=1&locale=${locale}&theme=${theme}`);
  116. await page.getByTestId("plugin-store-ui-capability").waitFor();
  117. const preview = page.getByTestId("plugin-store-preview-open");
  118. await preview.click();
  119. await page.getByRole("dialog").waitFor();
  120. assert.equal(await page.getByRole("dialog").getByRole("img").getAttribute("alt"), "Panel");
  121. await page.keyboard.press("Escape");
  122. await page.getByRole("dialog").waitFor({ state: "hidden" });
  123. assert.equal(await preview.evaluate((element) => element === document.activeElement), true);
  124. await page.getByTestId("plugin-store-open-ui").click();
  125. assert.equal(await page.getByTestId("opened").textContent(), "plugin-00@personal");
  126. assert.equal(await page.getByTestId("plugin-store-try-now").count(), 0);
  127. assert.equal(
  128. await page.evaluate(() => document.documentElement.scrollWidth <= innerWidth),
  129. true,
  130. );
  131. assert.deepEqual(errors, []);
  132. } finally {
  133. await page.close();
  134. }
  135. });
  136. }
  137. for (const [width, locale, theme] of [
  138. [1200, "en-US", "light"],
  139. [390, "zh-CN", "dark"],
  140. ]) {
  141. test(`PLM-UI-DETAILS ${width}/${locale}/${theme}`, async () => {
  142. const page = await browser.newPage({ viewport: { width, height: 800 } });
  143. try {
  144. await page.goto(`${origin}fixture?ui=1&paused=1&locale=${locale}&theme=${theme}`);
  145. // 卡片整行进入详情,不再有独立「详情」按钮;进入详情不打开也不启用 UI。
  146. assert.equal(await page.getByTestId("plugin-store-ui-details").count(), 0);
  147. const card = page.getByTestId("plugin-store-card");
  148. await card.focus();
  149. await page.keyboard.press("Enter");
  150. await page.getByTestId("plugin-store-detail").waitFor();
  151. assert.equal(await page.getByTestId("ui-opened").textContent(), "");
  152. await page.getByTestId("plugin-store-ui-capability").waitFor();
  153. assert.equal(
  154. await page.evaluate(() => document.documentElement.scrollWidth <= innerWidth),
  155. true,
  156. );
  157. } finally {
  158. await page.close();
  159. }
  160. });
  161. }
  162. for (const [width, locale, theme] of [
  163. [1200, "en-US", "light"],
  164. [390, "zh-CN", "dark"],
  165. ]) {
  166. test(`PLM-UI-MAIN-NAV ${width}/${locale}/${theme}`, async () => {
  167. const page = await browser.newPage({ viewport: { width, height: 800 } });
  168. try {
  169. // 主导航市场:已安装 UI 插件只有更多菜单,整行进详情,详情恢复「立即试用」。
  170. await page.goto(`${origin}fixture?ui=1&mainNav=1&locale=${locale}&theme=${theme}`);
  171. await page.getByTestId("plugin-store-card").waitFor();
  172. assert.equal(await page.getByTestId("plugin-store-open-ui").count(), 0);
  173. assert.equal(await page.getByTestId("plugin-store-ui-details").count(), 0);
  174. await page.getByTestId("plugin-store-card").click();
  175. await page.getByTestId("plugin-store-detail").waitFor();
  176. assert.equal(await page.getByTestId("ui-opened").textContent(), "");
  177. assert.equal(await page.getByTestId("plugin-store-open-ui").count(), 0);
  178. assert.equal(await page.getByTestId("plugin-store-try-now").count(), 1);
  179. await mkdir(artifacts, { recursive: true });
  180. await page.screenshot({
  181. path: join(artifacts, `main-nav-detail-${width}-${locale}-${theme}.png`),
  182. });
  183. } finally {
  184. await page.close();
  185. }
  186. });
  187. }