share-failure.test.mjs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. import assert from "node:assert/strict";
  2. import { before, after, test } from "node:test";
  3. import { fileURLToPath } from "node:url";
  4. import { mkdir } from "node:fs/promises";
  5. import { join } from "node:path";
  6. import { tmpdir } from "node:os";
  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/share-failure.fixture.tsx";
  13. const artifacts = join(tmpdir(), "share-failure-0917");
  14. let server, browser, origin;
  15. before(async () => {
  16. await mkdir(artifacts, { recursive: true });
  17. server = await createServer({
  18. root,
  19. configFile: false,
  20. plugins: [
  21. react(),
  22. tailwindcss(),
  23. {
  24. name: "share-selection-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. `<!doctype html><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. for (const kind of ["invalid_contract", "network"])
  56. for (const width of [390, 1200])
  57. for (const theme of ["light", "dark"])
  58. for (const locale of ["zh-CN", "en-US"])
  59. test(`SHARE33 ${kind} ${width} ${theme} ${locale}`, async () => {
  60. const page = await browser.newPage({ viewport: { width, height: 900 } });
  61. const errors = [];
  62. page.on("pageerror", (error) => errors.push(String(error)));
  63. await page.goto(`${origin}fixture?kind=${kind}&theme=${theme}&locale=${locale}`);
  64. const message = page.getByTestId("conversation-share-error-message");
  65. await message.waitFor();
  66. assert.match(
  67. await message.innerText(),
  68. locale === "zh-CN"
  69. ? kind === "network"
  70. ? /中断或超时/
  71. : /接口异常(HTTP 405)/
  72. : kind === "network"
  73. ? /interrupted or timed out/
  74. : /returned HTTP 405/,
  75. );
  76. assert.doesNotMatch(
  77. await page.locator("body").innerText(),
  78. /安全检查失败|Safety checks failed|包含内嵌图片|内容需要调整/,
  79. );
  80. await page.getByTestId("conversation-share-request-id-trigger").click();
  81. const details = page.getByTestId("conversation-share-request-id-content");
  82. await details.waitFor();
  83. const text = await details.innerText();
  84. for (const id of ["server-request-33", "client-request-33", "share-operation-33"])
  85. assert.ok(text.includes(id));
  86. assert.ok(text.includes(kind === "invalid_contract" ? "405" : "200"));
  87. await page.evaluate(async () => {
  88. await Promise.all(
  89. document.getAnimations().map((animation) => animation.finished.catch(() => {})),
  90. );
  91. });
  92. await page.screenshot({
  93. path: join(artifacts, `${kind}-${width}-${theme}-${locale}.png`),
  94. fullPage: true,
  95. });
  96. await page.keyboard.press("Escape");
  97. await page.getByTestId("conversation-share-confirm").click();
  98. assert.equal(await page.getByTestId("attempts").innerText(), "1");
  99. assert.ok((await page.getByText("9 / 9", { exact: false }).count()) > 0);
  100. assert.deepEqual(errors, []);
  101. await page.close();
  102. });