share-popover.test.mjs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. import { registerShareDisclosureTests } from "./share-disclosure-feedback.mjs";
  2. import { registerShareBoundaryTests } from "./share-selection-boundaries.mjs";
  3. import assert from "node:assert/strict";
  4. import { verifySelectionSelectAll } from "./share-selection-all.mjs";
  5. import { verifySelectionScrollMask } from "./share-selection-mask.mjs";
  6. import { before, after, test } from "node:test";
  7. import { fileURLToPath } from "node:url";
  8. import { mkdir } from "node:fs/promises";
  9. import { join } from "node:path";
  10. import { tmpdir } from "node:os";
  11. import { createServer } from "vite";
  12. import react from "@vitejs/plugin-react";
  13. import tailwindcss from "@tailwindcss/vite";
  14. import { chromium } from "playwright-core";
  15. const root = fileURLToPath(new URL("../../../../../../", import.meta.url));
  16. const entry = "/packages/ui/test/browser/manual-review/pending/share-popover.fixture.tsx";
  17. const artifacts = join(tmpdir(), "share-popover-0923");
  18. let server, browser, origin;
  19. registerShareBoundaryTests(() => ({ browser, origin }));
  20. registerShareDisclosureTests(() => ({ browser, origin }));
  21. before(async () => {
  22. await mkdir(artifacts, { recursive: true });
  23. server = await createServer({
  24. root,
  25. configFile: false,
  26. plugins: [
  27. react(),
  28. tailwindcss(),
  29. {
  30. name: "share-selection-fixture",
  31. configureServer(vite) {
  32. vite.middlewares.use(async (req, res, next) => {
  33. if (!req.url?.startsWith("/fixture?")) return next();
  34. res.setHeader("Content-Type", "text/html");
  35. res.end(
  36. await vite.transformIndexHtml(
  37. req.url,
  38. `<!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>`,
  39. ),
  40. );
  41. });
  42. },
  43. },
  44. ],
  45. resolve: { alias: { "@": `${root}packages/ui/src` } },
  46. optimizeDeps: { entries: [entry.slice(1)] },
  47. server: { host: "127.0.0.1", port: 0 },
  48. });
  49. await server.listen();
  50. origin = server.resolvedUrls.local[0];
  51. browser = await chromium.launch(
  52. process.env.CHROME_EXECUTABLE_PATH
  53. ? { executablePath: process.env.CHROME_EXECUTABLE_PATH }
  54. : { channel: "chrome" },
  55. );
  56. });
  57. after(async () => {
  58. await browser?.close();
  59. await server?.close();
  60. });
  61. for (const width of [390, 1200])
  62. for (const theme of ["light", "dark"])
  63. for (const locale of ["zh-CN", "en-US"])
  64. test(`SHARE34 ${width} ${theme} ${locale}`, async () => {
  65. const page = await browser.newPage({ viewport: { width, height: 900 } });
  66. page.setDefaultTimeout(5000);
  67. const errors = [];
  68. page.on("pageerror", (error) => errors.push(String(error)));
  69. await page.goto(`${origin}fixture?theme=${theme}&locale=${locale}`);
  70. const popover = page.getByTestId("conversation-share-popover");
  71. const panel = page.getByTestId("conversation-share-selection-panel");
  72. await page.getByTestId("conversation-share-trigger").click();
  73. await popover.waitFor();
  74. await page.evaluate(async () => {
  75. await Promise.all(document.getAnimations().map((a) => a.finished.catch(() => {})));
  76. });
  77. const scopeButton = page.getByTestId("conversation-share-adjust");
  78. const normalBounds = await popover.boundingBox();
  79. await page.getByTestId("conversation-share-confirm").click();
  80. const disclosure = page.getByTestId("conversation-share-disclosure");
  81. const reviewCheckbox = page.getByTestId("conversation-share-disclosure-checkbox");
  82. assert.equal(await reviewCheckbox.getAttribute("aria-invalid"), "true");
  83. assert.equal(await disclosure.evaluate((node) => node.getAnimations().length), 0);
  84. assert.equal(await page.getByTestId("conversation-share-publish-progress").count(), 0);
  85. assert.deepEqual(await popover.boundingBox(), normalBounds);
  86. await reviewCheckbox.check();
  87. assert.equal(await reviewCheckbox.getAttribute("aria-invalid"), "false");
  88. assert.equal(await page.getByTestId("conversation-share-publish-progress").count(), 0);
  89. await reviewCheckbox.uncheck();
  90. await page.getByTestId("conversation-share-confirm").click();
  91. await popover.screenshot({
  92. path: join(artifacts, `${width}-${theme}-${locale}-unchecked.png`),
  93. });
  94. await page.getByTestId("conversation-share-disclosure-scope-trigger").click();
  95. const reviewScope = page.getByTestId("conversation-share-disclosure-scope-content");
  96. await reviewScope.waitFor();
  97. await page.evaluate(async () => {
  98. await Promise.all(document.getAnimations().map((a) => a.finished.catch(() => {})));
  99. });
  100. await reviewScope.screenshot({
  101. path: join(artifacts, `${width}-${theme}-${locale}-scope.png`),
  102. });
  103. const reviewBounds = await reviewScope.boundingBox();
  104. assert.ok(reviewBounds.x >= 0 && reviewBounds.x + reviewBounds.width <= width + 1);
  105. assert.ok(reviewBounds.y >= 0 && reviewBounds.y + reviewBounds.height <= 901);
  106. await page.keyboard.press("Escape");
  107. await reviewScope.waitFor({ state: "hidden" });
  108. assert.equal(await popover.isVisible(), true);
  109. const scopeArrow = await scopeButton.locator("svg").last().boundingBox();
  110. const selectArrow = await popover.getByRole("combobox").locator("svg").last().boundingBox();
  111. assert.equal(scopeArrow.width, selectArrow.width);
  112. assert.ok(Math.abs(scopeArrow.x - selectArrow.x) < 1);
  113. await page.getByTestId("toggle-preflight").click();
  114. assert.equal(await scopeButton.getAttribute("aria-busy"), "true");
  115. assert.match(
  116. await scopeButton.innerText(),
  117. locale === "zh-CN" ? /正在检查分享内容/ : /checking/i,
  118. );
  119. assert.equal(
  120. await page.getByTestId("conversation-share-selection-preflight-status").count(),
  121. 0,
  122. );
  123. assert.equal((await popover.boundingBox()).height, normalBounds.height);
  124. await page.getByTestId("toggle-preflight").click();
  125. assert.equal(await scopeButton.getAttribute("aria-busy"), "false");
  126. await page.getByTestId("conversation-share-adjust").click();
  127. await panel.waitFor();
  128. await page.evaluate(async () => {
  129. await Promise.all(document.getAnimations().map((a) => a.finished.catch(() => {})));
  130. });
  131. const rows = panel.locator("[data-conversation-share-selection-item]");
  132. const previews = rows.getByTestId("conversation-share-selection-toggle");
  133. const firstTitle = previews.nth(0).locator("span").first();
  134. const secondTitle = previews.nth(1).locator("span").first();
  135. assert.equal((await firstTitle.boundingBox()).x, (await secondTitle.boundingBox()).x);
  136. const titleRect = await firstTitle.boundingBox();
  137. const checkRect = await rows.nth(0).getByRole("checkbox").boundingBox();
  138. const firstMessage = previews.nth(0).locator("span").last();
  139. const messageRect = await firstMessage.boundingBox();
  140. assert.equal(messageRect.x, titleRect.x);
  141. assert.equal((await panel.boundingBox()).width, Math.min(320, width - 16));
  142. assert.equal(messageRect.height, 32);
  143. assert.ok(
  144. await firstMessage.evaluate(
  145. (node) =>
  146. node.scrollHeight > node.clientHeight &&
  147. getComputedStyle(node).webkitLineClamp === "2",
  148. ),
  149. );
  150. assert.ok(
  151. Math.abs(checkRect.y + checkRect.height / 2 - titleRect.y - titleRect.height / 2) < 1,
  152. );
  153. assert.deepEqual(
  154. await firstTitle.evaluate((node) => ({
  155. size: getComputedStyle(node).fontSize,
  156. weight: getComputedStyle(node).fontWeight,
  157. })),
  158. { size: "14px", weight: "400" },
  159. );
  160. assert.equal(
  161. await firstMessage.evaluate((node) => getComputedStyle(node).fontSize),
  162. "12px",
  163. );
  164. assert.equal(
  165. (await page.getByTestId("conversation-share-selection-apply").boundingBox()).height,
  166. 32,
  167. );
  168. assert.equal(
  169. (await page.getByTestId("conversation-share-selection-cancel").boundingBox()).height,
  170. 32,
  171. );
  172. await panel.screenshot({
  173. path: join(artifacts, `${width}-${theme}-${locale}-selector.png`),
  174. });
  175. await verifySelectionScrollMask(
  176. panel,
  177. join(artifacts, `${width}-${theme}-${locale}-mask.png`),
  178. );
  179. await verifySelectionSelectAll(panel);
  180. const checkbox = rows.first().getByRole("checkbox");
  181. await checkbox.click();
  182. assert.match(await page.getByTestId("conversation-share-range-summary").innerText(), /8/);
  183. assert.equal(await page.getByTestId("conversation-share-confirm").isDisabled(), true);
  184. await rows.first().getByTestId("conversation-share-selection-locate").click();
  185. assert.equal(await page.getByTestId("inspected").innerText(), "1");
  186. assert.equal(await panel.isVisible(), true);
  187. await page.getByTestId("conversation-share-selection-cancel").click();
  188. await panel.waitFor({ state: "hidden" });
  189. await page.getByTestId("conversation-share-adjust").click();
  190. await panel.waitFor();
  191. assert.equal(await checkbox.getAttribute("data-state"), "checked");
  192. await checkbox.click();
  193. await page.getByTestId("conversation-share-selection-apply").click();
  194. await panel.waitFor({ state: "hidden" });
  195. assert.match(await page.getByTestId("conversation-share-range-summary").innerText(), /7/);
  196. await page.getByTestId("conversation-share-adjust").click();
  197. await panel.waitFor();
  198. await page.keyboard.press("Escape");
  199. await panel.waitFor({ state: "hidden" });
  200. assert.equal(await popover.isVisible(), true);
  201. await page.keyboard.press("Escape");
  202. await popover.waitFor({ state: "hidden" });
  203. await page.getByTestId("conversation-share-trigger").click();
  204. await popover.waitFor();
  205. assert.match(await page.getByTestId("conversation-share-range-summary").innerText(), /7/);
  206. await page.evaluate(async () => {
  207. await Promise.all(document.getAnimations().map((a) => a.finished.catch(() => {})));
  208. });
  209. await page.screenshot({ path: join(artifacts, `${width}-${theme}-${locale}.png`) });
  210. const bounds = await popover.boundingBox();
  211. await page.screenshot({
  212. path: join(artifacts, `${width}-${theme}-${locale}-popover.png`),
  213. clip: {
  214. x: Math.max(0, bounds.x - 8),
  215. y: Math.max(0, bounds.y - 8),
  216. width: bounds.width + 16,
  217. height: bounds.height + 16,
  218. },
  219. });
  220. assert.ok(bounds.x >= 0 && bounds.x + bounds.width <= width + 1);
  221. await page.getByTestId("conversation-share-adjust").click();
  222. await panel.waitFor();
  223. await page.getByRole("textbox", { name: "Composer", exact: true }).click();
  224. await panel.waitFor({ state: "hidden" });
  225. assert.equal(await popover.isVisible(), true);
  226. await page.getByRole("textbox", { name: "Composer", exact: true }).click();
  227. await popover.waitFor({ state: "hidden" });
  228. await page.getByTestId("conversation-share-trigger").click();
  229. await popover.waitFor();
  230. await page.getByTestId("conversation-share-adjust").click();
  231. await panel.waitFor();
  232. assert.equal(await rows.last().getByRole("checkbox").isDisabled(), true);
  233. const checks = rows.getByRole("checkbox");
  234. for (let i = 0; i < 8; i++) {
  235. if ((await checks.nth(i).getAttribute("data-state")) === "checked")
  236. await checks.nth(i).click();
  237. }
  238. assert.equal(
  239. await page.getByTestId("conversation-share-selection-apply").isDisabled(),
  240. true,
  241. );
  242. await page.evaluate(async () => {
  243. await Promise.all(document.getAnimations().map((a) => a.finished.catch(() => {})));
  244. });
  245. await page.screenshot({
  246. path: join(artifacts, `${width}-${theme}-${locale}-selection.png`),
  247. });
  248. const footer = await page.getByTestId("conversation-share-selection-footer").boundingBox();
  249. assert.ok(footer.y >= 0 && footer.y + footer.height <= 900);
  250. await page.getByTestId("conversation-share-selection-cancel").click();
  251. await panel.waitFor({ state: "hidden" });
  252. await popover.getByRole("checkbox").check();
  253. await page.getByTestId("conversation-share-confirm").click();
  254. await page.getByTestId("conversation-share-publish-progress").waitFor();
  255. assert.equal(await page.getByTestId("conversation-share-confirm").count(), 0);
  256. const readTypeSizes = () =>
  257. popover.evaluate((node) => {
  258. const size = (selector) => getComputedStyle(node.querySelector(selector)).fontSize;
  259. return {
  260. title: size('[data-testid="conversation-share-confirmation-header"] p'),
  261. task: size('[data-testid="conversation-share-publish-progress"] > p'),
  262. phase: size('[data-testid="conversation-share-publish-phase-collecting"] strong'),
  263. progress: size(
  264. '[data-testid="conversation-share-publish-phase-collecting"] strong + span',
  265. ),
  266. summary: size('[data-testid="conversation-share-confirmation-actions"] > p'),
  267. };
  268. });
  269. assert.deepEqual(await readTypeSizes(), {
  270. title: "16px",
  271. task: "14px",
  272. phase: "14px",
  273. progress: "14px",
  274. summary: "12px",
  275. });
  276. const progressIcon = page
  277. .getByTestId("conversation-share-publish-phase-collecting")
  278. .locator("svg");
  279. const iconSize = await progressIcon.evaluate((node) => getComputedStyle(node).width);
  280. await page.evaluate(() =>
  281. document.documentElement.style.setProperty("--ui-font-size", "18px"),
  282. );
  283. assert.deepEqual(await readTypeSizes(), {
  284. title: "20px",
  285. task: "18px",
  286. phase: "18px",
  287. progress: "18px",
  288. summary: "16px",
  289. });
  290. assert.equal(await progressIcon.evaluate((node) => getComputedStyle(node).width), iconSize);
  291. await page.evaluate(() => document.documentElement.style.removeProperty("--ui-font-size"));
  292. for (const phase of ["collecting", "uploading", "checking"]) {
  293. if (phase !== "collecting") await page.getByTestId("advance-publish").click();
  294. assert.equal(
  295. await page
  296. .getByTestId(`conversation-share-publish-phase-${phase}`)
  297. .getAttribute("data-phase-state"),
  298. "active",
  299. );
  300. await popover.screenshot({
  301. path: join(artifacts, `${width}-${theme}-${locale}-${phase}.png`),
  302. });
  303. }
  304. await page.keyboard.press("Escape");
  305. assert.equal(await popover.isVisible(), true);
  306. assert.equal(await page.getByTestId("conversation-share-trigger").isDisabled(), true);
  307. await page.getByTestId("resolve-publish").click();
  308. await page.getByTestId("conversation-share-success-dock").waitFor();
  309. await page.getByTestId("conversation-share-copy-link").click();
  310. assert.match(
  311. await page.getByTestId("conversation-share-copy-link").innerText(),
  312. locale === "zh-CN" ? /已复制/ : /copied/i,
  313. );
  314. assert.equal(await popover.isVisible(), true);
  315. await page.evaluate(async () => {
  316. await Promise.all(document.getAnimations().map((a) => a.finished.catch(() => {})));
  317. });
  318. await page.screenshot({ path: join(artifacts, `${width}-${theme}-${locale}-success.png`) });
  319. await popover.screenshot({
  320. path: join(artifacts, `${width}-${theme}-${locale}-success-popover.png`),
  321. });
  322. const openButton = await page.getByTestId("conversation-share-open-browser").boundingBox();
  323. const copyButton = await page.getByTestId("conversation-share-copy-link").boundingBox();
  324. assert.equal(openButton.height, 40);
  325. assert.equal(copyButton.height, 40);
  326. assert.ok(Math.abs(openButton.width - copyButton.width) < 1);
  327. const linkInput = popover.getByRole("textbox");
  328. assert.equal((await linkInput.boundingBox()).height, 32);
  329. await linkInput.focus();
  330. assert.equal(
  331. await linkInput.evaluate((input) => input.selectionEnd - input.selectionStart),
  332. (await linkInput.inputValue()).length,
  333. );
  334. await page.getByTestId("conversation-share-success-dismiss").click();
  335. await popover.waitFor({ state: "hidden" });
  336. await page.getByTestId("conversation-share-trigger").click();
  337. await popover.waitFor();
  338. assert.equal(await page.getByTestId("conversation-share-success-dock").count(), 0);
  339. assert.equal(await popover.getByRole("checkbox").isChecked(), false);
  340. assert.match(await page.getByTestId("conversation-share-range-summary").innerText(), /8/);
  341. await page.getByTestId("conversation-share-adjust").click();
  342. await panel.waitFor();
  343. await rows.first().getByRole("checkbox").click();
  344. await page.getByTestId("conversation-share-selection-apply").click();
  345. await panel.waitFor({ state: "hidden" });
  346. await popover.getByRole("checkbox").check();
  347. await page.getByTestId("conversation-share-confirm").click();
  348. await page.getByTestId("conversation-share-publish-progress").waitFor();
  349. await page.getByTestId("resolve-publish").click();
  350. await page.getByTestId("conversation-share-success-dock").waitFor();
  351. assert.deepEqual(errors, []);
  352. await page.close();
  353. });