plugin-store-mode-order.test.mjs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  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-store-mode-order.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, watch: null, hmr: false },
  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 official = "zcode-plugins-official";
  56. const config = {
  57. code: {
  58. categoryOrder: ["legal", "developer-tools", "productivity"],
  59. pluginOrder: { productivity: [`p7@${official}`, `p6@${official}`] },
  60. },
  61. work: {
  62. categoryOrder: ["other", "legal", "productivity", "developer-tools"],
  63. pluginOrder: { productivity: [`p5@${official}`, `p4@${official}`] },
  64. },
  65. };
  66. const artifacts = join(tmpdir(), "zcode-plugin-mode-order");
  67. for (const [width, locale, theme] of [
  68. [1200, "en-US", "light"],
  69. [1200, "zh-CN", "dark"],
  70. [390, "zh-CN", "light"],
  71. [390, "en-US", "dark"],
  72. ]) {
  73. test(`PLM-UI-GROUP inline collapse ${width}/${locale}/${theme}`, async () => {
  74. const page = await browser.newPage({ viewport: { width, height: 900 } });
  75. const errors = [];
  76. page.on("pageerror", (error) => errors.push(error.message));
  77. await page.route("**/order", (route) => route.fulfill({ json: null }));
  78. try {
  79. await page.goto(`${origin}fixture?uiGroups=1&locale=${locale}&theme=${theme}`);
  80. const section = page.getByTestId("plugin-store-ui-section");
  81. const cards = section.getByTestId("plugin-store-card");
  82. for (const segment of ["public", "community", "personal"]) {
  83. await page.getByTestId(`plugin-store-segment-${segment}`).click();
  84. await section.waitFor();
  85. assert.equal(await cards.count(), 8);
  86. const first = await cards.nth(0).boundingBox();
  87. const second = await cards.nth(1).boundingBox();
  88. assert.equal(first.y === second.y, width >= 640);
  89. assert.equal(await page.getByTestId("plugin-store-ui-all").count(), 0);
  90. assert.equal(await page.getByTestId("plugin-store-ui-back").count(), 0);
  91. const regular = page.locator(
  92. `[data-testid="plugin-store-card"][data-plugin-id^="${segment}-regular@"]`,
  93. );
  94. assert.equal(await regular.isVisible(), true);
  95. const toggle = section.getByTestId("plugin-store-group-toggle");
  96. await toggle.focus();
  97. await page.keyboard.press("Enter");
  98. assert.equal(await cards.count(), 6);
  99. assert.equal(await regular.isVisible(), true);
  100. await page.getByTestId("plugin-store-search").fill("panel-7");
  101. assert.equal(await page.getByTestId("plugin-store-card").count(), 1);
  102. await page.getByTestId("plugin-store-search").fill("");
  103. assert.equal(await cards.count(), 6);
  104. await toggle.click();
  105. assert.equal(await cards.count(), 8);
  106. await toggle.click();
  107. }
  108. await page.getByTestId("plugin-store-segment-public").click();
  109. assert.equal(await cards.count(), 6);
  110. await section.getByTestId("plugin-store-group-toggle").click();
  111. assert.equal(await cards.count(), 8);
  112. assert.equal(
  113. await page.evaluate(() => document.documentElement.scrollWidth <= innerWidth),
  114. true,
  115. );
  116. await mkdir(artifacts, { recursive: true });
  117. await page.screenshot({
  118. path: join(artifacts, `ui-groups-${width}-${locale}-${theme}.png`),
  119. fullPage: true,
  120. });
  121. assert.deepEqual(errors, []);
  122. } finally {
  123. await page.close();
  124. }
  125. });
  126. }
  127. for (const [width, locale, theme] of [
  128. [1200, "en-US", "light"],
  129. [390, "zh-CN", "dark"],
  130. ]) {
  131. test(`PSO-01/04/06/07/09 ${width}/${locale}/${theme}`, async () => {
  132. const page = await browser.newPage({ viewport: { width, height: 900 } });
  133. const errors = [];
  134. page.on("pageerror", (error) => errors.push(error.message));
  135. let requests = 0;
  136. let release;
  137. let status = 200;
  138. let body = config;
  139. const gate = new Promise((resolve) => {
  140. release = resolve;
  141. });
  142. await page.route("**/order", async (route) => {
  143. requests += 1;
  144. if (requests === 1) await gate;
  145. await route.fulfill({ status, contentType: "application/json", body: JSON.stringify(body) });
  146. });
  147. const cards = (scope) =>
  148. scope
  149. .getByTestId("plugin-store-card")
  150. .evaluateAll((elements) =>
  151. elements.map((e) => e.getAttribute("data-plugin-id").split("@")[0]),
  152. );
  153. const productSection = () =>
  154. page
  155. .locator("section")
  156. .filter({ has: page.locator('[data-group-key="category:productivity"]') });
  157. try {
  158. await page.goto(`${origin}fixture?locale=${locale}&theme=${theme}`);
  159. await page.getByTestId("plugin-store-list").waitFor();
  160. // 配置尚未返回,默认分类已经可以展开和浏览。
  161. assert.deepEqual(await cards(productSection()), [
  162. "p0",
  163. "p1",
  164. "p2",
  165. "p3",
  166. "p4",
  167. "p5",
  168. "p6",
  169. "p7",
  170. ]);
  171. release();
  172. await page.waitForFunction(() =>
  173. document
  174. .querySelector('[data-group-key="category:productivity"]')
  175. ?.closest("section")
  176. ?.querySelector("[data-plugin-id]")
  177. ?.getAttribute("data-plugin-id")
  178. ?.startsWith("p7@"),
  179. );
  180. assert.deepEqual(await cards(productSection()), [
  181. "p7",
  182. "p6",
  183. "p0",
  184. "p1",
  185. "p2",
  186. "p3",
  187. "p4",
  188. "p5",
  189. ]);
  190. // 第一块是精选,其固定名单没有受排序配置影响。
  191. const sections = () =>
  192. page.locator("section").filter({ has: page.getByTestId("plugin-store-card") });
  193. assert.deepEqual(await cards(sections().nth(0)), ["p1", "p0"]);
  194. assert.deepEqual(await cards(sections().nth(1)), ["dev"]);
  195. await productSection().getByTestId("plugin-store-group-toggle").click();
  196. assert.equal((await cards(productSection())).length, 6);
  197. await productSection().getByTestId("plugin-store-group-toggle").click();
  198. assert.deepEqual(await cards(productSection()), [
  199. "p7",
  200. "p6",
  201. "p0",
  202. "p1",
  203. "p2",
  204. "p3",
  205. "p4",
  206. "p5",
  207. ]);
  208. await page.getByTestId("work").click();
  209. assert.deepEqual(await cards(sections().nth(1)), ["other"]);
  210. assert.deepEqual(await cards(productSection()), [
  211. "p5",
  212. "p4",
  213. "p0",
  214. "p1",
  215. "p2",
  216. "p3",
  217. "p6",
  218. "p7",
  219. ]);
  220. assert.deepEqual(await cards(sections().nth(0)), ["p1", "p0"]);
  221. assert.equal(requests, 1);
  222. assert.equal(
  223. await page.evaluate(() => localStorage.getItem("zcode-interface-mode")),
  224. "office",
  225. );
  226. // 个人与当前来源搜索继续按各自原来的名称顺序。
  227. await page.getByTestId("plugin-store-segment-personal").click();
  228. assert.deepEqual(await cards(page), ["personal-a", "personal-b"]);
  229. await page.getByTestId("code").click();
  230. assert.deepEqual(await cards(page), ["personal-a", "personal-b"]);
  231. await page.getByTestId("plugin-store-search").fill("p");
  232. const searchOrder = await cards(page);
  233. await page.getByTestId("work").click();
  234. assert.deepEqual(await cards(page), searchOrder);
  235. await page.getByTestId("plugin-store-search").fill("");
  236. await page.getByTestId("plugin-store-segment-public").click();
  237. // 失败刷新保留现有投影,成功撤销则恢复默认顺序。
  238. status = 503;
  239. const failed = page.waitForResponse((response) => response.url().endsWith("/order"));
  240. await page.getByTestId("refresh").click();
  241. await failed;
  242. assert.deepEqual(await cards(sections().nth(1)), ["other"]);
  243. status = 200;
  244. body = null;
  245. await page.getByTestId("refresh").click();
  246. await page.waitForFunction(() =>
  247. document
  248. .querySelector('[data-group-key="category:productivity"]')
  249. ?.closest("section")
  250. ?.querySelector("[data-plugin-id]")
  251. ?.getAttribute("data-plugin-id")
  252. ?.startsWith("p0@"),
  253. );
  254. assert.deepEqual(await cards(sections().nth(1)), [
  255. "p0",
  256. "p1",
  257. "p2",
  258. "p3",
  259. "p4",
  260. "p5",
  261. "p6",
  262. "p7",
  263. ]);
  264. assert.equal(requests, 3);
  265. const categoryTitles = await page.locator("section h2").allTextContents();
  266. assert.equal(categoryTitles.includes(locale === "zh-CN" ? "指南" : "Guides"), false);
  267. assert.equal(categoryTitles.includes(locale === "zh-CN" ? "法律" : "Legal"), false);
  268. const legacyCategory = await page
  269. .locator(
  270. '[data-testid="plugin-store-card"][data-plugin-id="legacy-guide@zcode-plugins-official"]',
  271. )
  272. .evaluate((element) => element.closest("section").querySelector("h2").textContent);
  273. assert.equal(legacyCategory, locale === "zh-CN" ? "实用工具" : "Utilities");
  274. assert.equal(
  275. await page.evaluate(() => document.documentElement.scrollWidth <= innerWidth),
  276. true,
  277. );
  278. assert.deepEqual(errors, []);
  279. await mkdir(artifacts, { recursive: true });
  280. await page.screenshot({
  281. path: join(artifacts, `${width}-${locale}-${theme}.png`),
  282. fullPage: true,
  283. });
  284. // 仅测试目录加入一条未来法律插件;当前真实目录仍没有法律插件。
  285. body = config;
  286. await page.goto(`${origin}fixture?locale=${locale}&theme=${theme}&legal=1`);
  287. const legalCard = page.locator(
  288. '[data-testid="plugin-store-card"][data-plugin-id="legal-example@zcode-plugins-official"]',
  289. );
  290. await legalCard.waitFor();
  291. assert.equal(
  292. await legalCard.evaluate(
  293. (element) => element.closest("section").querySelector("h2").textContent,
  294. ),
  295. locale === "zh-CN" ? "法律" : "Legal",
  296. );
  297. await page.waitForFunction(() => {
  298. const sections = Array.from(document.querySelectorAll("section")).filter((s) =>
  299. s.querySelector('[data-testid="plugin-store-card"]'),
  300. );
  301. return (
  302. sections[1]?.querySelector("[data-plugin-id]")?.getAttribute("data-plugin-id") ===
  303. "legal-example@zcode-plugins-official"
  304. );
  305. });
  306. assert.deepEqual(errors, []);
  307. // 退出市场的官方插件即使已安装、被精选或仍在旧目录中也不可见;个人同名插件保留。
  308. await page.goto(`${origin}fixture?locale=${locale}&theme=${theme}&retired=1`);
  309. await page.getByTestId("plugin-store-list").waitFor();
  310. const retired = page.locator(`[data-plugin-id="restore-legacy-sessions@${official}"]`);
  311. for (const mode of ["code", "work"]) {
  312. await page.getByTestId(mode).click();
  313. assert.equal(await retired.count(), 0);
  314. assert.equal(await page.getByTestId("plugin-store-installed-strip").count(), 0);
  315. await page.getByTestId("plugin-store-search").fill("restore-legacy-sessions");
  316. assert.equal(await retired.count(), 0);
  317. assert.equal(await page.getByTestId("plugin-store-card").count(), 0);
  318. await page.getByTestId("plugin-store-search").fill("");
  319. }
  320. await page.getByTestId("plugin-store-segment-personal").click();
  321. assert.equal(
  322. await page
  323. .locator(
  324. '[data-testid="plugin-store-card"][data-plugin-id="restore-legacy-sessions@personal"]',
  325. )
  326. .count(),
  327. 1,
  328. );
  329. assert.deepEqual(errors, []);
  330. } finally {
  331. release();
  332. await page.close();
  333. }
  334. });
  335. }
  336. for (const width of [1200, 390]) {
  337. for (const locale of ["en-US", "zh-CN"]) {
  338. for (const theme of ["light", "dark"]) {
  339. test(`PLM-MISSING-CONFIG ${width}/${locale}/${theme}`, async () => {
  340. const page = await browser.newPage({ viewport: { width, height: 900 } });
  341. const errors = [];
  342. page.on("pageerror", (error) => errors.push(error.message));
  343. try {
  344. await page.route("**/order", (route) => route.fulfill({ json: config }));
  345. await page.goto(`${origin}fixture?missingConfig=1&locale=${locale}&theme=${theme}`);
  346. const card = (name) =>
  347. page.locator(`[data-testid="plugin-store-card"][data-plugin-id="${name}@${official}"]`);
  348. await card("documents").waitFor();
  349. assert.equal(await card("document-skills").count(), 0);
  350. for (const name of ["documents", "pdf", "presentations", "spreadsheets", "image-search"])
  351. assert.equal(await card(name).count(), 1, name);
  352. assert.equal(
  353. await card("repairable")
  354. .getByRole("button", { name: locale === "zh-CN" ? "安装" : "Install", exact: true })
  355. .count(),
  356. 1,
  357. );
  358. const bundledIcons = [
  359. ["plugin-creator", "plugin-creator.png"],
  360. ["documents", "documents.png"],
  361. ["pdf", "pdf.png"],
  362. ["presentations", "presentations.png"],
  363. ["spreadsheets", "spreadsheets.png"],
  364. ["image-search", "image-search.png"],
  365. ];
  366. await Promise.all(bundledIcons.map(([name]) => card(name).locator("img").waitFor()));
  367. await page.waitForFunction(() => {
  368. const images = document.querySelectorAll(
  369. '[data-plugin-id$="@zcode-plugins-official"] img',
  370. );
  371. return [...images].every((image) => image.complete && image.naturalWidth > 0);
  372. });
  373. for (const [name, assetName] of bundledIcons)
  374. assert.match(
  375. await card(name).locator("img").getAttribute("src"),
  376. new RegExp(assetName),
  377. );
  378. await mkdir(artifacts, { recursive: true });
  379. await page.screenshot({
  380. path: join(artifacts, `missing-config-${width}-${locale}-${theme}.png`),
  381. });
  382. await page.getByRole("searchbox").fill("document-skills");
  383. assert.equal(await page.getByTestId("plugin-store-card").count(), 0);
  384. assert.deepEqual(errors, []);
  385. } finally {
  386. await page.close();
  387. }
  388. });
  389. }
  390. }
  391. }
  392. for (const width of [1200, 390]) {
  393. for (const locale of ["en-US", "zh-CN"]) {
  394. for (const theme of ["light", "dark"]) {
  395. test(`PLM-DOCUMENT-ORDER ${width}/${locale}/${theme}`, async () => {
  396. const page = await browser.newPage({ viewport: { width, height: 900 } });
  397. try {
  398. await page.route("**/order", (route) => route.fulfill({ json: null }));
  399. await page.goto(`${origin}fixture?documentOrder=1&locale=${locale}&theme=${theme}`);
  400. await page.getByTestId("plugin-store-card").first().waitFor();
  401. const expected = ["pdf", "presentations", "spreadsheets", "documents"].map(
  402. (name) => `${name}@${official}`,
  403. );
  404. for (const mode of ["code", "work"]) {
  405. await page.getByTestId(mode).click();
  406. for (const testId of ["plugin-store-card"]) {
  407. const ids = await page
  408. .getByTestId(testId)
  409. .evaluateAll((elements) =>
  410. elements.slice(0, 4).map((element) => element.getAttribute("data-plugin-id")),
  411. );
  412. assert.deepEqual(ids, expected);
  413. }
  414. }
  415. await mkdir(artifacts, { recursive: true });
  416. await page.screenshot({
  417. path: join(artifacts, `document-order-${width}-${locale}-${theme}.png`),
  418. });
  419. } finally {
  420. await page.close();
  421. }
  422. });
  423. }
  424. }
  425. }