preview-card-actions.test.mjs 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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 { createServer } from "vite";
  7. import react from "@vitejs/plugin-react";
  8. import tailwindcss from "@tailwindcss/vite";
  9. import { chromium } from "playwright-core";
  10. import sharp from "sharp";
  11. const root = fileURLToPath(new URL("../../../../../../", import.meta.url));
  12. const entry = "/packages/ui/test/browser/manual-review/pending/preview-card-actions.fixture.tsx";
  13. const artifacts = join(root, "packages/desktop/.e2e-artifacts/preview-card-actions-browser");
  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. {
  22. name: "preview-card-actions-fixture",
  23. enforce: "pre",
  24. configureServer(vite) {
  25. vite.middlewares.use(async (req, res, next) => {
  26. if (!req.url?.startsWith("/fixture?")) return next();
  27. res.setHeader("Content-Type", "text/html");
  28. res.end(
  29. await vite.transformIndexHtml(
  30. req.url,
  31. `<!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>`,
  32. ),
  33. );
  34. });
  35. },
  36. },
  37. react(),
  38. tailwindcss(),
  39. ],
  40. resolve: { alias: { "@": `${root}packages/ui/src` } },
  41. optimizeDeps: { entries: [entry.slice(1)] },
  42. server: {
  43. host: "127.0.0.1",
  44. port: 0,
  45. hmr: false,
  46. watch: { ignored: ["**/packages/desktop/out/**", "**/.e2e-artifacts/**"] },
  47. },
  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. const FILE_CARD_TITLE = "项目 简介.docx";
  62. const WEB_CARD_TITLE = "APC Local Preview";
  63. async function readPixel(path, x, y) {
  64. const { data, info } = await sharp(path).raw().toBuffer({ resolveWithObject: true });
  65. const offset = (y * info.width + x) * info.channels;
  66. return Array.from(data.subarray(offset, offset + 3));
  67. }
  68. async function openFixture(theme = "zai-dark") {
  69. const page = await browser.newPage({ viewport: { width: 1100, height: 844 } });
  70. // 首次访问要冷编译整条 ui 依赖链,放宽默认超时避免误报。
  71. page.setDefaultTimeout(60_000);
  72. const errors = [];
  73. page.on("pageerror", (error) => errors.push(String(error)));
  74. await page.goto(`${origin}fixture?theme=${theme}&locale=zh-CN`);
  75. const fileCard = page.locator(`[data-preview-card-title="${FILE_CARD_TITLE}"]`);
  76. const webCard = page.locator(`[data-preview-card-title="${WEB_CARD_TITLE}"]`);
  77. await fileCard.waitFor();
  78. await webCard.waitFor();
  79. const readEvents = () => page.getByTestId("preview-events").locator("li").allTextContents();
  80. return { page, errors, fileCard, webCard, readEvents };
  81. }
  82. test("PCA01 file card body click previews exactly once", async () => {
  83. const { page, errors, fileCard, readEvents } = await openFixture();
  84. try {
  85. // 点卡片左上空白/图标区:命中元素是铺满卡片的透明按钮,一次点击必须只产生一次预览。
  86. await fileCard.click({ position: { x: 16, y: 28 } });
  87. assert.deepEqual(await readEvents(), [`codeViewer:${FILE_CARD_TITLE}`]);
  88. assert.equal(
  89. await fileCard
  90. .getByRole("button", { name: `打开 ${FILE_CARD_TITLE}`, exact: true })
  91. .getAttribute("disabled"),
  92. null,
  93. );
  94. } finally {
  95. await page.close();
  96. }
  97. assert.deepEqual(errors, []);
  98. });
  99. test("PCA02 file card Open button previews exactly once", async () => {
  100. const { page, errors, fileCard, readEvents } = await openFixture();
  101. try {
  102. await fileCard.getByRole("button", { name: "打开", exact: true }).click();
  103. assert.deepEqual(await readEvents(), [`codeViewer:${FILE_CARD_TITLE}`]);
  104. } finally {
  105. await page.close();
  106. }
  107. assert.deepEqual(errors, []);
  108. });
  109. test("PCA03 file card dropdown click only expands the menu", async () => {
  110. const { page, errors, fileCard, readEvents } = await openFixture();
  111. try {
  112. await fileCard.getByRole("button", { name: "选择打开方式" }).click();
  113. const menu = page.locator('[data-slot="dropdown-menu-content"][data-state="open"]');
  114. await menu.waitFor();
  115. assert.match(await menu.innerText(), /另存为|Save as/);
  116. assert.deepEqual(await readEvents(), []);
  117. await page.screenshot({ path: join(artifacts, "file-card-dropdown.png") });
  118. await page.keyboard.press("Escape");
  119. } finally {
  120. await page.close();
  121. }
  122. assert.deepEqual(errors, []);
  123. });
  124. test("PCA04 website card body click and Open button each preview exactly once", async () => {
  125. const { page, errors, webCard, readEvents } = await openFixture();
  126. try {
  127. await webCard.click({ position: { x: 16, y: 28 } });
  128. assert.deepEqual(await readEvents(), ["browserUrl:http://127.0.0.1:41789/assistant-preview"]);
  129. await webCard.getByRole("button", { name: "打开", exact: true }).click();
  130. assert.deepEqual(await readEvents(), [
  131. "browserUrl:http://127.0.0.1:41789/assistant-preview",
  132. "browserUrl:http://127.0.0.1:41789/assistant-preview",
  133. ]);
  134. } finally {
  135. await page.close();
  136. }
  137. assert.deepEqual(errors, []);
  138. });
  139. test("PCA05 website card dropdown click only expands the menu", async () => {
  140. const { page, errors, webCard, readEvents } = await openFixture();
  141. try {
  142. await webCard.getByRole("button", { name: "选择打开方式" }).click();
  143. const menu = page.locator('[data-slot="dropdown-menu-content"][data-state="open"]');
  144. await menu.waitFor();
  145. assert.match(await menu.innerText(), /在浏览器中打开|Open in browser/);
  146. assert.deepEqual(await readEvents(), []);
  147. await page.screenshot({ path: join(artifacts, "web-card-dropdown.png") });
  148. await page.keyboard.press("Escape");
  149. } finally {
  150. await page.close();
  151. }
  152. assert.deepEqual(errors, []);
  153. });
  154. test("PCA06 overlay button supports keyboard Enter activation", async () => {
  155. const { page, errors, fileCard, readEvents } = await openFixture();
  156. try {
  157. const overlay = fileCard.getByRole("button", { name: `打开 ${FILE_CARD_TITLE}`, exact: true });
  158. await overlay.focus();
  159. await page.keyboard.press("Enter");
  160. assert.deepEqual(await readEvents(), [`codeViewer:${FILE_CARD_TITLE}`]);
  161. await overlay.focus();
  162. await page.keyboard.press("Space");
  163. assert.deepEqual(await readEvents(), [
  164. `codeViewer:${FILE_CARD_TITLE}`,
  165. `codeViewer:${FILE_CARD_TITLE}`,
  166. ]);
  167. } finally {
  168. await page.close();
  169. }
  170. assert.deepEqual(errors, []);
  171. });
  172. test("PCA07 preview card hover matches summary header across themes", async () => {
  173. for (const theme of ["light", "dark", "zai-light", "zai-dark"]) {
  174. const { page, errors, fileCard, readEvents } = await openFixture(theme);
  175. try {
  176. const overlay = fileCard.locator(':scope > [aria-hidden="true"]');
  177. const header = page
  178. .getByTestId("hover-summary")
  179. .locator('[data-slot="collapsible"] > div')
  180. .first();
  181. const cardBox = await fileCard.boundingBox();
  182. const headerBox = await header.boundingBox();
  183. const point = (box) => ({ x: Math.floor(box.x + box.width / 2), y: Math.floor(box.y + 6) });
  184. const cardPoint = point(cardBox);
  185. const headerPoint = point(headerBox);
  186. const beforePath = join(artifacts, `hover-${theme}-before.png`);
  187. const cardPath = join(artifacts, `hover-${theme}-after.png`);
  188. const headerPath = join(artifacts, `hover-${theme}-summary.png`);
  189. const beforeBackground = await fileCard.evaluate(
  190. (element) => getComputedStyle(element).backgroundColor,
  191. );
  192. await page.screenshot({ path: beforePath, animations: "disabled" });
  193. assert.equal(await overlay.evaluate((element) => getComputedStyle(element).opacity), "0");
  194. await fileCard.hover();
  195. // 修复回归:等待实际过渡完成,不能把 hover 首帧的 opacity=0 当作最终颜色。
  196. await page.waitForFunction((title) => {
  197. const card = document.querySelector(`[data-preview-card-title="${title}"]`);
  198. return getComputedStyle(card.firstElementChild).opacity === "1";
  199. }, FILE_CARD_TITLE);
  200. await page.screenshot({ path: cardPath, animations: "disabled" });
  201. assert.equal(
  202. await fileCard.evaluate((element) => getComputedStyle(element).backgroundColor),
  203. beforeBackground,
  204. `${theme}: card base must remain unchanged`,
  205. );
  206. await header.hover();
  207. await page.screenshot({ path: headerPath, animations: "disabled" });
  208. const beforePixel = await readPixel(beforePath, cardPoint.x, cardPoint.y);
  209. const cardPixel = await readPixel(cardPath, cardPoint.x, cardPoint.y);
  210. const headerPixel = await readPixel(headerPath, headerPoint.x, headerPoint.y);
  211. assert.deepEqual(cardPixel, headerPixel, `${theme}: rendered hover must match summary`);
  212. assert.notDeepEqual(
  213. cardPixel,
  214. beforePixel,
  215. `${theme}: hover must visibly change the surface`,
  216. );
  217. if (theme === "dark" || theme === "zai-dark") {
  218. assert.ok(
  219. cardPixel.every((channel, index) => channel > beforePixel[index]),
  220. `${theme}: hover must brighten the card`,
  221. );
  222. }
  223. console.log(
  224. `${theme}: base=${beforePixel.join(",")} hover=${cardPixel.join(",")} summary=${headerPixel.join(",")}`,
  225. );
  226. await fileCard.click({ position: { x: 16, y: 28 } });
  227. assert.deepEqual(await readEvents(), [`codeViewer:${FILE_CARD_TITLE}`]);
  228. } finally {
  229. await page.close();
  230. }
  231. assert.deepEqual(errors, []);
  232. }
  233. });