plugin-marketplace-risk.test.mjs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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-marketplace-risk.fixture.tsx";
  13. const artifacts = join(tmpdir(), "zcode-marketplace-risk");
  14. let server;
  15. let browser;
  16. let origin;
  17. before(async () => {
  18. await mkdir(artifacts, { recursive: true });
  19. server = await createServer({
  20. root,
  21. configFile: false,
  22. plugins: [
  23. react(),
  24. tailwindcss(),
  25. {
  26. name: "marketplace-risk-fixture",
  27. configureServer(vite) {
  28. vite.middlewares.use(async (req, res, next) => {
  29. if (!req.url?.startsWith("/fixture?")) return next();
  30. res.setHeader("Content-Type", "text/html");
  31. res.end(
  32. await vite.transformIndexHtml(
  33. req.url,
  34. `<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>`,
  35. ),
  36. );
  37. });
  38. },
  39. },
  40. ],
  41. resolve: { alias: { "@": `${root}packages/ui/src` } },
  42. optimizeDeps: { entries: [entry.slice(1)] },
  43. server: { host: "127.0.0.1", port: 0 },
  44. });
  45. await server.listen();
  46. origin = server.resolvedUrls.local[0];
  47. browser = await chromium.launch(
  48. process.env.CHROME_EXECUTABLE_PATH
  49. ? { executablePath: process.env.CHROME_EXECUTABLE_PATH }
  50. : { channel: "chrome" },
  51. );
  52. });
  53. after(async () => {
  54. await browser?.close();
  55. await server?.close();
  56. });
  57. for (const [platform, width] of [
  58. ["desktop", 1200],
  59. ["web", 390],
  60. ]) {
  61. for (const locale of ["zh-CN", "en-US"]) {
  62. for (const theme of ["light", "dark"]) {
  63. test(`marketplace risk: ${platform}/${locale}/${theme}`, async () => {
  64. const page = await browser.newPage({ viewport: { width, height: 844 } });
  65. page.setDefaultTimeout(10000);
  66. const errors = [];
  67. page.on("pageerror", (error) => errors.push(error.message));
  68. const requests = [];
  69. let release;
  70. const pending = new Promise((resolve) => (release = resolve));
  71. await page.route("**/add-marketplace", async (route) => {
  72. requests.push(route.request().postData());
  73. if (requests.length === 1) return route.fulfill({ status: 400, body: "unavailable" });
  74. await pending;
  75. return route.fulfill({ status: 200, body: "ok" });
  76. });
  77. try {
  78. await page.goto(`${origin}fixture?platform=${platform}&locale=${locale}&theme=${theme}`);
  79. await page.getByTestId("open").click();
  80. const dialog = page.getByTestId("plugin-store-add-source-dialog");
  81. const input = page.getByTestId("plugin-store-add-source-input");
  82. const checkbox = page.getByTestId("plugin-store-add-source-risk-acknowledgement");
  83. const submit = page.getByTestId("plugin-store-add-source-submit");
  84. const warning = page.getByTestId("plugin-store-add-source-risk-warning");
  85. await input.fill(" example/marketplace ");
  86. assert.equal(await checkbox.isChecked(), false);
  87. assert.equal(await submit.isDisabled(), true);
  88. assert.match(
  89. await warning.innerText(),
  90. locale === "zh-CN" ? /恶意插件/ : /malicious plugins/,
  91. );
  92. await input.press("Enter");
  93. // 等待浏览器处理输入事件,不用时间延迟推断是否发起请求。
  94. await page.evaluate(() => new Promise((resolve) => requestAnimationFrame(resolve)));
  95. assert.equal(requests.length, 0);
  96. await page
  97. .getByText(
  98. locale === "zh-CN"
  99. ? "我已了解风险,并已核实来源、做好风险防范。"
  100. : "I understand the risks, have verified the source, and have taken precautions.",
  101. { exact: true },
  102. )
  103. .click();
  104. assert.equal(await checkbox.isChecked(), true);
  105. assert.equal(await submit.isEnabled(), true);
  106. await checkbox.press("Space");
  107. assert.equal(await submit.isDisabled(), true);
  108. await input.press("Enter");
  109. assert.equal(requests.length, 0);
  110. await checkbox.press("Space");
  111. assert.equal(await checkbox.isChecked(), true);
  112. await input.fill(" ");
  113. assert.equal(await submit.isDisabled(), true);
  114. await input.fill("example/marketplace");
  115. await page.evaluate(() =>
  116. Promise.all(document.getAnimations().map((animation) => animation.finished)),
  117. );
  118. await page.screenshot({ path: join(artifacts, `${platform}-${locale}-${theme}.png`) });
  119. const bounds = await dialog.boundingBox();
  120. assert.ok(bounds.x >= 0 && bounds.x + bounds.width <= width);
  121. assert.ok(bounds.y >= 0 && bounds.y + bounds.height <= 844);
  122. await page.keyboard.press("Escape");
  123. await dialog.waitFor({ state: "hidden" });
  124. await page.getByTestId("open").click();
  125. assert.equal(await checkbox.isChecked(), false);
  126. assert.equal(await submit.isDisabled(), true);
  127. await checkbox.check();
  128. await input.dispatchEvent("compositionstart");
  129. await input.press("Enter");
  130. assert.equal(requests.length, 0);
  131. await input.dispatchEvent("compositionend");
  132. await input.press("Enter");
  133. await page.getByTestId("plugin-store-add-source-error").waitFor();
  134. assert.deepEqual(requests, ["example/marketplace"]);
  135. assert.equal(await input.inputValue(), "example/marketplace");
  136. assert.equal(await checkbox.isChecked(), true);
  137. const retryRequest = page.waitForRequest("**/add-marketplace");
  138. await submit.click();
  139. await retryRequest;
  140. assert.equal(await submit.isDisabled(), true);
  141. await input.press("Enter");
  142. release();
  143. await dialog.waitFor({ state: "hidden" });
  144. assert.deepEqual(requests, ["example/marketplace", "example/marketplace"]);
  145. await page.getByTestId("open").click();
  146. assert.equal(await checkbox.isChecked(), false);
  147. assert.equal(await input.inputValue(), "");
  148. assert.equal(await submit.isDisabled(), true);
  149. assert.deepEqual(errors, []);
  150. } finally {
  151. release();
  152. await page.close();
  153. }
  154. });
  155. }
  156. }
  157. }