share-failure.test.mjs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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-errors-0921");
  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. const messages = {
  56. invalid_contract: [/接口异常(HTTP 405)/, /returned HTTP 405/],
  57. network: [/中断或超时/, /interrupted or timed out/],
  58. safety_check_rejected: [/内容未通过安全检查/, /Content did not pass/],
  59. safety_check_unavailable: [/安全检查服务暂不可用/, /safety check service is unavailable/],
  60. service_unavailable: [/分享服务暂时异常/, /sharing service is temporarily unavailable/],
  61. invalid_conversation: [/所选对话/, /selected conversation/i],
  62. expired: [/分享准备已过期/, /Share preparation expired/],
  63. };
  64. for (const kind of Object.keys(messages))
  65. for (const width of [390, 1200])
  66. for (const theme of ["light", "dark"])
  67. for (const locale of ["zh-CN", "en-US"])
  68. test(`SHARE33 ${kind} ${width} ${theme} ${locale}`, async () => {
  69. const page = await browser.newPage({ viewport: { width, height: 900 } });
  70. const errors = [];
  71. page.on("pageerror", (error) => errors.push(String(error)));
  72. await page.goto(`${origin}fixture?kind=${kind}&theme=${theme}&locale=${locale}`);
  73. const message = page.getByTestId("conversation-share-error-message");
  74. await message.waitFor();
  75. assert.match(await message.innerText(), messages[kind][locale === "zh-CN" ? 0 : 1]);
  76. await page.getByTestId("conversation-share-request-id-trigger").click();
  77. const details = page.getByTestId("conversation-share-request-id-content");
  78. await details.waitFor();
  79. const text = await details.innerText();
  80. for (const id of ["server-request-33", "client-request-33", "share-operation-33"])
  81. assert.ok(text.includes(id));
  82. if (kind === "invalid_conversation") {
  83. assert.ok(text.includes("rows[0].state"));
  84. assert.ok(text.includes("field_type"));
  85. assert.ok(text.includes("3205"));
  86. }
  87. if (kind === "safety_check_rejected") assert.ok(text.includes("3216"));
  88. if (kind === "safety_check_unavailable") assert.ok(text.includes("3217"));
  89. await page.evaluate(async () => {
  90. await Promise.all(
  91. document.getAnimations().map((animation) => animation.finished.catch(() => {})),
  92. );
  93. });
  94. await page.screenshot({
  95. path: join(artifacts, `${kind}-${width}-${theme}-${locale}.png`),
  96. fullPage: true,
  97. });
  98. await page.keyboard.press("Escape");
  99. await page.getByTestId("conversation-share-confirm").click();
  100. assert.equal(await page.getByTestId("attempts").innerText(), "1");
  101. assert.ok((await page.getByText("9 / 9", { exact: false }).count()) > 0);
  102. assert.deepEqual(errors, []);
  103. await page.close();
  104. });
  105. for (const width of [390, 1200])
  106. for (const theme of ["light", "dark"])
  107. for (const locale of ["zh-CN", "en-US"])
  108. test(`SHARE34 preflight auth ${width} ${theme} ${locale}`, async () => {
  109. const page = await browser.newPage({ viewport: { width, height: 900 } });
  110. const errors = [];
  111. page.on("pageerror", (error) => errors.push(error.message));
  112. await page.goto(
  113. `${origin}fixture?surface=preflight&kind=authentication_required&theme=${theme}&locale=${locale}`,
  114. );
  115. await page.getByTestId("conversation-share-selection-preflight-status").click();
  116. const popover = page.getByTestId("conversation-share-selection-preflight-popover");
  117. await popover.waitFor();
  118. const text = await popover.innerText();
  119. assert.match(
  120. text,
  121. locale === "zh-CN" ? /请先登录 ZCode/ : /Sign in to ZCode before sharing/,
  122. );
  123. assert.match(text, locale === "zh-CN" ? /暂时无法检查分享/ : /Unable to check this share/);
  124. assert.doesNotMatch(
  125. text,
  126. /原因未知|cause could not|during —|内容无法分享|Some content cannot/,
  127. );
  128. assert.equal(await page.getByTestId("conversation-share-next").isDisabled(), true);
  129. await page.getByTestId("conversation-share-preflight-retry").click();
  130. assert.equal(await page.getByTestId("attempts").innerText(), "1");
  131. assert.equal(
  132. await page.evaluate(() => document.documentElement.scrollWidth > innerWidth + 1),
  133. false,
  134. );
  135. assert.deepEqual(errors, []);
  136. await page.screenshot({
  137. path: join(artifacts, `preflight-auth-${width}-${theme}-${locale}.png`),
  138. fullPage: true,
  139. });
  140. await page.close();
  141. });