message-render-isolation.test.mjs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. import assert from "node:assert/strict";
  2. import { before, after, test } from "node:test";
  3. import { mkdir, writeFile } from "node:fs/promises";
  4. import { fileURLToPath } from "node:url";
  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. const root = fileURLToPath(new URL("../../../../../../", import.meta.url));
  11. const entry =
  12. "/packages/ui/test/browser/manual-review/pending/message-render-isolation.fixture.tsx";
  13. const artifacts = join(root, "packages/desktop/.e2e-artifacts/message-render-isolation");
  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: "render-isolation-fixture",
  23. configureServer(vite) {
  24. vite.middlewares.use(async (req, res, next) => {
  25. if (!req.url?.startsWith("/fixture?")) return next();
  26. res.setHeader("Content-Type", "text/html");
  27. res.end(
  28. await vite.transformIndexHtml(
  29. req.url,
  30. `<!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>`,
  31. ),
  32. );
  33. });
  34. },
  35. },
  36. react(),
  37. tailwindcss(),
  38. ],
  39. resolve: { alias: { "@": `${root}packages/ui/src` } },
  40. optimizeDeps: { entries: [entry.slice(1)] },
  41. server: {
  42. host: "127.0.0.1",
  43. port: 0,
  44. hmr: false,
  45. watch: { ignored: ["**/packages/desktop/out/**", "**/.e2e-artifacts/**"] },
  46. },
  47. });
  48. await server.listen();
  49. origin = server.resolvedUrls.local[0];
  50. browser = await chromium.launch({ channel: "chrome" });
  51. });
  52. after(async () => {
  53. await browser?.close();
  54. await server?.close();
  55. });
  56. for (const mobile of [false, true])
  57. for (const theme of ["light", "dark"])
  58. test(`RRI01–04 ${mobile ? "mobile" : "desktop"}/${theme}`, { timeout: 90000 }, async () => {
  59. const page = await browser.newPage({ viewport: { width: mobile ? 430 : 1200, height: 800 } });
  60. const errors = [];
  61. page.on("pageerror", (e) => errors.push(String(e)));
  62. await page.addInitScript(() => {
  63. window.__turnRenders = {};
  64. window.__REACT_DEVTOOLS_GLOBAL_HOOK__ = {
  65. supportsFiber: true,
  66. inject: () => 1,
  67. onCommitFiberUnmount() {},
  68. onPostCommitFiberRoot() {},
  69. onCommitFiberRoot(_id, root) {
  70. const walk = (f) => {
  71. if (!f) return;
  72. const type = f.type;
  73. if (f.flags & 1 && type?.name === "ConversationTurnGroupImpl") {
  74. const id = f.memoizedProps.unit.turnId;
  75. window.__turnRenders[id] = (window.__turnRenders[id] ?? 0) + 1;
  76. }
  77. walk(f.child);
  78. walk(f.sibling);
  79. };
  80. walk(root.current);
  81. },
  82. };
  83. });
  84. const settle = () =>
  85. page.evaluate(
  86. () => new Promise((r) => requestAnimationFrame(() => requestAnimationFrame(r))),
  87. );
  88. const act = async (action) => {
  89. await page.getByTestId(action).click();
  90. await settle();
  91. };
  92. try {
  93. await page.goto(`${origin}fixture?theme=${theme}&mobile=${mobile}`);
  94. await page.locator('[data-turn-id="history-0"]').waitFor();
  95. await settle();
  96. const mounted = await page.evaluate(() => window.__turnRenders);
  97. assert.ok(
  98. mounted["history-0"] > 0 && mounted.active > 0,
  99. "probe sees mounted real components",
  100. );
  101. await page.evaluate(() => {
  102. window.__turnRenders = {};
  103. });
  104. for (let i = 0; i < 8; i++) await act("stream");
  105. await page.getByText("stream 8", { exact: true }).waitFor();
  106. for (let i = 0; i < 3; i++) await act("clock");
  107. const stream = await page.evaluate(() => window.__turnRenders);
  108. assert.equal(stream["history-0"] ?? 0, 0, "unrelated history must memo-skip");
  109. assert.ok(stream.active >= 8, "active text must render");
  110. await act("complete");
  111. await act("append");
  112. await act("trim");
  113. assert.equal(await page.locator('[data-turn-id="history-0"]').count(), 0);
  114. await act("assert-boundaries");
  115. await page.waitForFunction(
  116. () => document.querySelector('[data-testid="checks"]').textContent !== "pending",
  117. );
  118. assert.equal(await page.getByTestId("checks").textContent(), "passed");
  119. assert.deepEqual(errors, []);
  120. await writeFile(
  121. join(artifacts, `${mobile}-${theme}.json`),
  122. JSON.stringify(
  123. { mounted, stream, checks: await page.getByTestId("evidence").textContent() },
  124. null,
  125. 2,
  126. ),
  127. );
  128. await page.screenshot({ path: join(artifacts, `${mobile}-${theme}.png`) });
  129. } finally {
  130. await page.close();
  131. }
  132. });