topic-preparation.test.mjs 6.8 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/topic-preparation.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 },
  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 artifacts = join(tmpdir(), "zcode-topic-preparation");
  56. for (const width of [390, 1200]) {
  57. for (const locale of ["en-US", "zh-CN"]) {
  58. for (const theme of ["light", "dark"]) {
  59. test(`BOT-UI-TP-PREPARATION ${width}/${locale}/${theme}`, async () => {
  60. const page = await browser.newPage({ viewport: { width, height: 1000 } });
  61. page.setDefaultTimeout(5000);
  62. const errors = [];
  63. page.on("pageerror", (error) => errors.push(error.message));
  64. try {
  65. await page.goto(`${origin}fixture?locale=${locale}&theme=${theme}`);
  66. const first = page.locator('[data-topic-preparation="first"]');
  67. const bubble = first.locator("[data-v4-user-input-bubble]");
  68. await bubble.waitFor();
  69. const formal = page.getByTestId("formal").locator("[data-v4-user-input-bubble]");
  70. const style = (locator) =>
  71. locator.evaluate((el) => {
  72. const s = getComputedStyle(el);
  73. const r = el.getBoundingClientRect();
  74. return {
  75. width: r.width,
  76. background: s.backgroundColor,
  77. radius: s.borderRadius,
  78. padding: s.padding,
  79. font: s.fontSize,
  80. };
  81. });
  82. assert.deepEqual(await style(bubble), await style(formal));
  83. const sourceBounds = await first.locator("[data-topic-preparation-source]").boundingBox();
  84. const bubbleBounds = await bubble.boundingBox();
  85. assert.ok(sourceBounds.y + sourceBounds.height <= bubbleBounds.y);
  86. const loading = first.locator("[data-topic-preparation-loading]");
  87. assert.equal(
  88. await loading.evaluate((el) => Boolean(el.closest("[data-topic-preparation-source]"))),
  89. true,
  90. );
  91. assert.equal(await bubble.locator("[data-topic-preparation-loading]").count(), 0);
  92. assert.equal(
  93. await bubble.textContent(),
  94. locale === "zh-CN" ? "北京今天天气如何?" : "What is the weather in Beijing today?",
  95. );
  96. assert.ok(
  97. (await first.locator("[data-topic-preparation-source]").textContent()).includes(
  98. locale === "zh-CN" ? "飞书 · 莫汝舰" : "Feishu · 莫汝舰",
  99. ),
  100. );
  101. const label = locale === "zh-CN" ? "正在准备材料" : "Preparing materials";
  102. await loading.hover();
  103. await page.getByRole("tooltip", { name: label, exact: true }).waitFor();
  104. await page.mouse.move(0, 0);
  105. await loading.focus();
  106. await page.getByRole("tooltip", { name: label, exact: true }).waitFor();
  107. await loading.evaluate((el) => el.blur());
  108. await page.getByRole("tooltip").waitFor({ state: "hidden" });
  109. assert.equal(
  110. await page
  111. .locator('[data-topic-preparation="empty"] [data-v4-user-input-bubble]')
  112. .count(),
  113. 0,
  114. );
  115. assert.equal(
  116. await page.evaluate(() => document.documentElement.scrollWidth <= innerWidth),
  117. true,
  118. );
  119. for (const el of await page
  120. .locator("[data-topic-preparation] [data-v4-user-input-bubble]")
  121. .all()) {
  122. const bounds = await el.boundingBox();
  123. assert.ok(bounds.x >= 0 && bounds.x + bounds.width <= width);
  124. assert.equal(await el.evaluate((node) => node.scrollWidth <= node.clientWidth), true);
  125. }
  126. const waiting = page.locator('[data-topic-preparation="waiting"]');
  127. const expand = waiting.getByRole("button", {
  128. name: locale === "zh-CN" ? "展开" : "Expand",
  129. exact: true,
  130. });
  131. await expand.waitFor();
  132. await expand.click();
  133. await page.waitForFunction(() => {
  134. const content = document.querySelector(
  135. '[data-topic-preparation="waiting"] [data-v4-user-input-collapsible-content]',
  136. );
  137. return content && content.clientHeight >= content.scrollHeight - 1;
  138. });
  139. assert.equal(await waiting.getByRole("button").getAttribute("aria-expanded"), "true");
  140. await mkdir(artifacts, { recursive: true });
  141. await page.screenshot({
  142. path: join(artifacts, `${width}-${locale}-${theme}.png`),
  143. fullPage: true,
  144. });
  145. assert.equal(await page.locator('[data-topic-preparation="failed"]').count(), 0);
  146. assert.equal(await page.locator("[data-topic-preparation-status]").count(), 0);
  147. await page.getByRole("button", { name: "Fail waiting" }).click();
  148. await waiting.waitFor({ state: "detached" });
  149. assert.equal(await page.getByText("failed after preparing").count(), 0);
  150. await page.getByRole("button", { name: "Accept first" }).click();
  151. await first.waitFor({ state: "detached" });
  152. assert.equal(await page.locator("[data-topic-preparation]").count(), 1);
  153. assert.deepEqual(errors, []);
  154. } finally {
  155. await page.close();
  156. }
  157. });
  158. }
  159. }
  160. }