queued-summary-streaming.test.mjs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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 { execFile } from "node:child_process";
  5. import { promisify } from "node:util";
  6. import { fileURLToPath } from "node:url";
  7. import { join } from "node:path";
  8. import { build, preview } from "vite";
  9. import react from "@vitejs/plugin-react";
  10. import tailwindcss from "@tailwindcss/vite";
  11. import { chromium } from "playwright-core";
  12. const root = fileURLToPath(new URL("../../../../../../", import.meta.url));
  13. const artifacts =
  14. process.env.ZCODE_SUMMARY_ARTIFACT_DIR ??
  15. join(root, "packages/desktop/.e2e-artifacts/queued-summary-streaming");
  16. let server, browser;
  17. before(async () => {
  18. await mkdir(artifacts, { recursive: true });
  19. await writeFile(
  20. join(artifacts, "index.html"),
  21. '<!doctype html><meta name="viewport" content="width=device-width, initial-scale=1"><link rel="icon" href="data:,"><div id="root"></div><script type="module" src="/summary-fixture.tsx"></script>',
  22. );
  23. const source = root + "packages/ui/src/ToolCallBlocks/QueuedSummaryContent.tsx";
  24. const baseline = process.env.ZCODE_SUMMARY_BASELINE_REF
  25. ? (
  26. await promisify(execFile)(
  27. "git",
  28. [
  29. "show",
  30. process.env.ZCODE_SUMMARY_BASELINE_REF +
  31. ":packages/ui/src/ToolCallBlocks/QueuedSummaryContent.tsx",
  32. ],
  33. { cwd: root },
  34. )
  35. ).stdout
  36. : null;
  37. await build({
  38. configFile: false,
  39. root: artifacts,
  40. base: "./",
  41. logLevel: "warn",
  42. plugins: [
  43. { name: "summary-baseline", enforce: "pre", load: (id) => (id === source ? baseline : null) },
  44. react(),
  45. tailwindcss(),
  46. ],
  47. resolve: {
  48. alias: {
  49. "@": root + "packages/ui/src",
  50. "/summary-fixture.tsx":
  51. root +
  52. "packages/ui/test/browser/manual-review/pending/queued-summary-streaming.fixture.tsx",
  53. },
  54. },
  55. build: {
  56. outDir: join(artifacts, "out"),
  57. emptyOutDir: true,
  58. minify: false,
  59. chunkSizeWarningLimit: 5000,
  60. },
  61. });
  62. server = await preview({
  63. configFile: false,
  64. root: artifacts,
  65. build: { outDir: join(artifacts, "out") },
  66. preview: { host: "127.0.0.1", port: 0 },
  67. });
  68. browser = await chromium.launch(
  69. process.env.CHROME_EXECUTABLE_PATH
  70. ? { executablePath: process.env.CHROME_EXECUTABLE_PATH }
  71. : { channel: "chrome" },
  72. );
  73. });
  74. after(async () => {
  75. await browser?.close();
  76. await server?.close();
  77. });
  78. async function openPage(options, query = "") {
  79. const page = await browser.newPage({
  80. viewport: { width: options.width, height: 850 },
  81. reducedMotion: "no-preference",
  82. });
  83. page.setDefaultTimeout(10000);
  84. const errors = [];
  85. page.on("pageerror", (error) => errors.push(error.message));
  86. page.on("console", (message) => {
  87. if (message.type() === "error") errors.push(message.text());
  88. });
  89. await page.goto(
  90. `${server.resolvedUrls.local[0]}?theme=${options.theme}&locale=${options.locale}&${query}`,
  91. );
  92. await page.locator("[data-summary-fixture]").waitFor();
  93. return { page, errors };
  94. }
  95. async function update(page, patch) {
  96. await page.evaluate((value) => window.summaryFixture.update(value), patch);
  97. }
  98. async function textIs(page, text) {
  99. await page.waitForFunction(
  100. (value) => document.querySelector("[data-summary-fixture]")?.textContent.includes(value),
  101. text,
  102. );
  103. }
  104. for (const width of [390, 1200]) {
  105. for (const theme of ["light", "dark"]) {
  106. const options = { width, theme, locale: theme === "light" ? "en-US" : "zh-CN" };
  107. for (const mode of ["reasoning", "group"]) {
  108. test(`XSG03 stream ${mode} ${width}/${theme}`, async () => {
  109. const { page, errors } = await openPage(options, `${mode}=1`);
  110. try {
  111. await page.locator("[data-reasoning-streaming-text]").waitFor();
  112. await page.evaluate(() => {
  113. window.originalTextNode = document.querySelector("[data-reasoning-streaming-text]");
  114. });
  115. await page.evaluate(() => window.summaryFixture.burst());
  116. await textIs(page, "A token 200");
  117. assert.equal(
  118. await page.evaluate(
  119. () =>
  120. window.originalTextNode ===
  121. document.querySelector("[data-reasoning-streaming-text]"),
  122. ),
  123. true,
  124. );
  125. await page.screenshot({ path: join(artifacts, `stream-${mode}-${width}-${theme}.png`) });
  126. const viewport = page.locator("[data-reasoning-streaming-line]");
  127. assert.equal(await viewport.getAttribute("data-reasoning-streaming-mask"), "both");
  128. const scroll = await viewport.evaluate((node) => ({
  129. left: node.scrollLeft,
  130. remaining: node.scrollWidth - node.clientWidth - node.scrollLeft,
  131. }));
  132. assert.ok(scroll.left > 0 && scroll.remaining <= 2);
  133. await update(page, { text: "Short" });
  134. await page.waitForFunction(
  135. () =>
  136. document
  137. .querySelector("[data-reasoning-streaming-line]")
  138. ?.getAttribute("data-reasoning-streaming-mask") === "none",
  139. );
  140. await update(page, { text: "Short\nNext reasoning line" });
  141. await textIs(page, "Next reasoning line");
  142. await update(page, { streaming: false });
  143. await page.waitForFunction(
  144. () => !document.querySelector("[data-reasoning-streaming-text]"),
  145. );
  146. assert.match(await page.locator("[data-reasoning-label]").textContent(), /Thought|思考/);
  147. assert.deepEqual(errors, []);
  148. } finally {
  149. await page.close();
  150. }
  151. });
  152. }
  153. test(`XSG03 queue and animation ${width}/${theme}`, async () => {
  154. const { page, errors } = await openPage(options);
  155. try {
  156. await page.locator("[data-primary]").waitFor();
  157. await page.evaluate(() => {
  158. window.originalTextNode = document.querySelector("[data-primary]");
  159. });
  160. await page.evaluate(() => window.summaryFixture.burst());
  161. await textIs(page, "A token 200");
  162. assert.equal(
  163. await page.evaluate(
  164. () => window.originalTextNode === document.querySelector("[data-primary]"),
  165. ),
  166. true,
  167. );
  168. assert.equal(await page.locator("[data-secondary]").textContent(), "secondary 200");
  169. assert.equal(await page.locator("[data-trailing]").textContent(), "+200");
  170. await update(page, { key: "B", text: "B initial" });
  171. await textIs(page, "B initial");
  172. // 采集真实运动样式,而非仅检查 motion 类名或 key。
  173. const frames = await page.evaluate(async () => {
  174. const samples = [];
  175. for (let i = 0; i < 8; i++) {
  176. await new Promise(requestAnimationFrame);
  177. const nodes = [...document.querySelectorAll("[data-primary]")];
  178. samples.push(
  179. nodes.map((node) => ({
  180. text: node.textContent,
  181. transform: getComputedStyle(node.parentElement).transform,
  182. opacity: getComputedStyle(node.parentElement).opacity,
  183. })),
  184. );
  185. }
  186. return samples;
  187. });
  188. await writeFile(
  189. join(artifacts, `animation-${width}-${theme}.json`),
  190. JSON.stringify(frames, null, 2),
  191. );
  192. assert.ok(
  193. frames.flat().some((frame) => frame.text === "B initial" && Number(frame.opacity) < 1),
  194. );
  195. assert.ok(frames.flat().some((frame) => frame.text.endsWith("A token 200")));
  196. const incomingTransforms = frames
  197. .flat()
  198. .filter((frame) => frame.text === "B initial")
  199. .map((frame) => frame.transform);
  200. assert.ok(new Set(incomingTransforms).size > 1, "new summary must move vertically");
  201. assert.ok(
  202. frames
  203. .flat()
  204. .some(
  205. (frame) => frame.text.endsWith("A token 200") && /, -[\d.]+\)$/.test(frame.transform),
  206. ),
  207. "previous summary must exit upward",
  208. );
  209. await update(page, { text: "B latest", trailing: "+201" });
  210. await textIs(page, "B latest");
  211. await page.evaluate(() => {
  212. window.summarySeen = [];
  213. window.summaryObserver = new MutationObserver(() =>
  214. window.summarySeen.push(document.querySelector("[data-summary-fixture]")?.textContent),
  215. );
  216. window.summaryObserver.observe(document.querySelector("[data-summary-fixture]"), {
  217. childList: true,
  218. subtree: true,
  219. characterData: true,
  220. });
  221. });
  222. await update(page, { key: "C", text: "C initial", trailing: "+202" });
  223. await update(page, { text: "C latest", trailing: "+203" });
  224. await update(page, { key: "D", text: "D skipped" });
  225. await update(page, { key: "E", text: "E latest", trailing: "+205" });
  226. assert.match(await page.locator("[data-summary-fixture]").textContent(), /B latest/);
  227. assert.equal(await page.locator("[data-trailing]").textContent(), "+201");
  228. await textIs(page, "C latest");
  229. assert.equal(await page.locator("[data-trailing]").textContent(), "+203");
  230. await textIs(page, "E latest");
  231. await page.waitForFunction(() => document.querySelectorAll("[data-primary]").length === 1);
  232. assert.equal(await page.locator("[data-primary]").textContent(), "E latest");
  233. assert.equal(await page.locator("[data-trailing]").textContent(), "+205");
  234. assert.equal(
  235. await page.evaluate(() => window.summarySeen.some((text) => text.includes("D skipped"))),
  236. false,
  237. );
  238. assert.deepEqual(errors, []);
  239. } finally {
  240. await page.close();
  241. }
  242. });
  243. test(`XSG03 animation gates ${width}/${theme}`, async () => {
  244. const { page, errors } = await openPage(options);
  245. try {
  246. for (const gate of ["enabled", "disabled", "reducedMotion"]) {
  247. await update(page, { key: `${gate}-B`, text: "B queued" });
  248. await textIs(page, "B queued");
  249. await update(page, { key: `${gate}-C`, text: "C latest" });
  250. if (gate === "reducedMotion") await page.emulateMedia({ reducedMotion: "reduce" });
  251. else await update(page, { [gate]: gate === "disabled" });
  252. await textIs(page, "C latest");
  253. assert.equal(await page.locator("[data-primary]").count(), 1);
  254. await page.evaluate(() => window.summaryFixture.burst());
  255. await textIs(page, "A token 200");
  256. if (gate === "reducedMotion") await page.emulateMedia({ reducedMotion: "no-preference" });
  257. else await update(page, { [gate]: gate === "enabled" });
  258. await textIs(page, "A token 200");
  259. assert.equal(await page.locator("[data-primary]").count(), 1);
  260. }
  261. await update(page, { key: "unmount-next", text: "Unmount next" });
  262. await textIs(page, "Unmount next");
  263. await update(page, { key: "unmount-pending", text: "Must not play" });
  264. await update(page, { mounted: false });
  265. await page.waitForFunction(() => !document.querySelector("[data-summary-fixture]"));
  266. await page.waitForTimeout(900); // 超过既有播放周期,确认卸载后没有迟到更新。
  267. assert.deepEqual(errors, []);
  268. } finally {
  269. await page.close();
  270. }
  271. });
  272. }
  273. }