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

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