conversation-hook-footer.test.mjs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. // HK08/HK11:桌面及手机展示参数下的真实组件交互;全链路另由 Hooks lifecycle Electron E2E 验证。
  2. import assert from "node:assert/strict";
  3. import { before, after, test } from "node:test";
  4. import { fileURLToPath } from "node:url";
  5. import { mkdir } from "node:fs/promises";
  6. import { join } from "node:path";
  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 =
  13. "/packages/ui/test/browser/manual-review/pending/conversation-hook-footer.fixture.tsx";
  14. const artifacts = join(root, "packages/desktop/.e2e-artifacts/conversation-hook-footer");
  15. let server, browser, origin;
  16. before(async () => {
  17. await mkdir(artifacts, { recursive: true });
  18. server = await createServer({
  19. root,
  20. configFile: false,
  21. plugins: [
  22. {
  23. name: "hook-footer-fixture",
  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. for (const mobile of [false, true])
  62. for (const theme of ["light", "dark"])
  63. for (const highspeed of [false, true]) {
  64. test(`HK08/HK11 ${mobile ? "mobile" : "desktop"}/${theme}/${highspeed ? "highspeed-no-text" : "normal"}`, async () => {
  65. const page = await browser.newPage({
  66. viewport: { width: mobile ? 390 : 1200, height: 800 },
  67. isMobile: mobile,
  68. hasTouch: mobile,
  69. });
  70. const errors = [];
  71. page.on("pageerror", (error) => errors.push(String(error)));
  72. const prefix = `${mobile ? "mobile" : "desktop"}-${theme}-${highspeed ? "highspeed-no-text" : "normal"}`;
  73. try {
  74. await page.goto(
  75. `${origin}fixture?mobile=${mobile}&theme=${theme}&highspeed=${highspeed}`,
  76. );
  77. const turn = page.locator('[data-turn-id="hook-footer"]');
  78. await turn.waitFor();
  79. const hook = turn.getByTestId("v4-hook-details-trigger-hook-footer");
  80. assert.equal(await hook.count(), 1, "同一轮必须恰好一个 Hook 入口");
  81. assert.equal(
  82. await turn.locator('[data-highspeed-output-footer="true"]').count(),
  83. Number(highspeed),
  84. );
  85. assert.equal(await turn.getByTestId("v4-copy-4").count(), Number(!highspeed));
  86. if (!mobile) await turn.hover();
  87. await hook.waitFor({ state: "visible" });
  88. await hook.evaluate(async (element) => {
  89. await Promise.all(
  90. element.parentElement
  91. .getAnimations({ subtree: true })
  92. .map((animation) => animation.finished),
  93. );
  94. });
  95. if (mobile)
  96. assert.equal(
  97. await hook.evaluate((e) => getComputedStyle(e.parentElement).opacity),
  98. "1",
  99. );
  100. await page.screenshot({ path: join(artifacts, `${prefix}-footer.png`) });
  101. await hook.click();
  102. const details = page.getByTestId("v4-hook-details-content-hook-footer");
  103. await details.waitFor({ state: "visible" });
  104. await details.evaluate(async (element) => {
  105. await Promise.all(
  106. element.getAnimations({ subtree: true }).map((animation) => animation.finished),
  107. );
  108. });
  109. assert.equal(await details.locator("li").count(), 1);
  110. assert.match(await details.innerText(), /UserPromptSubmit/);
  111. assert.match(await details.innerText(), mobile ? /User/ : /用户/);
  112. const bounds = await details.boundingBox();
  113. assert.ok(bounds.x >= 8 && bounds.y >= 8);
  114. assert.ok(bounds.x + bounds.width <= (mobile ? 390 : 1200) - 8);
  115. assert.ok(bounds.y + bounds.height <= 800 - 8);
  116. await page.screenshot({ path: join(artifacts, `${prefix}-details.png`) });
  117. await page.keyboard.press("Escape");
  118. await details.waitFor({ state: "hidden" });
  119. assert.deepEqual(errors, []);
  120. } finally {
  121. await page.close();
  122. }
  123. });
  124. }