workflow-face-motion.test.mjs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. // 真实组件浏览器验证;仅 renderer 展示,无 provider / host 协议。
  2. import assert from "node:assert/strict";
  3. import test from "node:test";
  4. import { mkdtemp, readFile, writeFile } from "node:fs/promises";
  5. import { tmpdir } from "node:os";
  6. import { join } from "node:path";
  7. import { pathToFileURL } from "node:url";
  8. import { createRequire } from "node:module";
  9. import { chromium } from "playwright-core";
  10. const require = createRequire(import.meta.url);
  11. // esbuild 是 Vite 已安装的构建依赖,不添加运行时依赖。
  12. const { build } = require(createRequire(require.resolve("vite")).resolve("esbuild"));
  13. test("WFA01 四态随机眼睛:真实双眨、开心眼上下抖、机身不动及 reduced-motion", async () => {
  14. const output = await mkdtemp(join(tmpdir(), "workflow-face-"));
  15. const compiled = await build({
  16. entryPoints: ["packages/ui/test/fixtures/workflow-face-motion.fixture.tsx"],
  17. bundle: true,
  18. write: false,
  19. format: "iife",
  20. jsx: "automatic",
  21. alias: { "@": join(process.cwd(), "packages/ui/src") },
  22. });
  23. const source = await readFile("packages/ui/src/styles.css", "utf8");
  24. const start = source.indexOf(".wf-face {");
  25. const reducedStart = source.indexOf(" .wf-face-body,", source.indexOf("/* 瓦片脸:动作归零"));
  26. const css =
  27. source.slice(start, source.indexOf("@keyframes wf-arrive", start)) +
  28. "@media(prefers-reduced-motion:reduce){" +
  29. source.slice(reducedStart, source.indexOf("\n}", reducedStart)) +
  30. "}";
  31. await writeFile(
  32. join(output, "preview.html"),
  33. `<!doctype html><meta charset="utf-8"><title>Workflow face preview</title><style>${css}
  34. body{font:14px system-ui;background:#fafafa;color:#222;padding:24px}body.dark{background:#181818;color:#eee}svg{width:100%;height:100%;overflow:visible}button{padding:8px 16px;margin-right:8px;border-radius:8px;border:1px solid #888;background:transparent;color:inherit}button[aria-pressed=true]{background:#7774}</style><div id="root"></div><script>${compiled.outputFiles[0].text}</script>`,
  35. );
  36. const browser = await chromium.launch({ channel: "chrome", headless: true });
  37. try {
  38. const page = await browser.newPage();
  39. const errors = [];
  40. page.on("pageerror", (e) => errors.push(e.message));
  41. await page.clock.install();
  42. await page.goto(pathToFileURL(join(output, "preview.html")).href);
  43. const face = page.locator("svg").first();
  44. await face.waitFor();
  45. // 请求更小尺寸也不能突破 20px 下限;大尺寸预览仍可放大。
  46. // 参考轮廓的真实 SVG 边界:开心更高、困倦更浅、疑惑右眼为圆形。
  47. await page.emulateMedia({ reducedMotion: "reduce" });
  48. await page.waitForFunction(() => document.querySelector("svg")?.dataset.motion === "idle");
  49. for (const [expression, y, height] of [
  50. ["happy", 6, 5],
  51. ["sleepy", 7, 4],
  52. ["focused", 6, 6],
  53. ["sad", 6, 6],
  54. ]) {
  55. const bounds = await face.evaluate((node, name) => {
  56. node.dataset.expression = name;
  57. return Array.from(node.querySelectorAll(`[data-eye-expression="${name}"] path`), (p) => {
  58. const box = p.getBBox();
  59. return { width: box.width, y: box.y, height: box.height };
  60. });
  61. }, expression);
  62. assert.deepEqual(bounds, [
  63. { width: 4, y, height },
  64. { width: 4, y, height },
  65. ]);
  66. }
  67. const smallFace = page.locator("svg").nth(9);
  68. await smallFace.evaluate((n) => {
  69. n.style.width = "14px";
  70. n.style.height = "14px";
  71. });
  72. assert.deepEqual(
  73. await smallFace.evaluate((n) => ({
  74. width: n.getBoundingClientRect().width,
  75. height: n.getBoundingClientRect().height,
  76. })),
  77. { width: 20, height: 20 },
  78. );
  79. assert.equal(await face.evaluate((n) => n.getBoundingClientRect().width), 48);
  80. await smallFace.evaluate((n) => {
  81. n.style.removeProperty("width");
  82. n.style.removeProperty("height");
  83. });
  84. for (const width of [1200, 390])
  85. for (const dark of [false, true]) {
  86. await page.setViewportSize({ width, height: 420 });
  87. await page.evaluate((d) => {
  88. document.body.classList.toggle("dark", d);
  89. document.documentElement.classList.toggle("dark", d);
  90. Math.random = () => 0.2;
  91. }, dark);
  92. await page.emulateMedia({ reducedMotion: "reduce" });
  93. await page.waitForFunction(() => document.querySelector("svg")?.dataset.motion === "idle");
  94. for (const [status, expression] of [
  95. ["pending", "dots"],
  96. ["running", "pill"],
  97. ["failed", "sad"],
  98. ["done", "happy"],
  99. ]) {
  100. await page.locator(`[data-status="${status}"]`).click();
  101. assert.equal(await face.getAttribute("data-expression"), expression);
  102. assert.equal(await face.getAttribute("viewBox"), "0 0 20 20");
  103. assert.equal(await face.locator(".wf-face-body").getAttribute("rx"), "7");
  104. assert.deepEqual(
  105. await face
  106. .locator('[data-eye-expression="pill"] rect')
  107. .evaluateAll((nodes) =>
  108. nodes.map((n) => ["x", "y", "width", "height", "rx"].map((k) => n.getAttribute(k))),
  109. ),
  110. [
  111. ["7", "6", "4", "6", "2"],
  112. ["13", "6", "4", "6", "2"],
  113. ],
  114. );
  115. assert.equal(await face.getAttribute("data-motion"), "idle");
  116. assert.equal(
  117. await face
  118. .locator(`[data-eye-expression="${expression}"]`)
  119. .evaluate((n) => getComputedStyle(n).display),
  120. "inline",
  121. );
  122. }
  123. await page.emulateMedia({ reducedMotion: "no-preference" });
  124. await page.waitForTimeout(80);
  125. await page.clock.runFor(1600);
  126. assert.equal(await face.getAttribute("data-motion"), "hop");
  127. const bounce = await face.locator(".wf-face-bounce").evaluate((node) => {
  128. const animation = node.getAnimations()[0];
  129. animation.pause();
  130. animation.currentTime = 84;
  131. return getComputedStyle(node).transform;
  132. });
  133. assert.match(bounce, /, -1\)/);
  134. assert.notEqual(bounce, "matrix(1, 0, 0, 1, 0, 0)");
  135. assert.equal(
  136. await face.locator(".wf-face-body").evaluate((n) => getComputedStyle(n).transform),
  137. "none",
  138. );
  139. await page.locator('[data-status="running"]').click();
  140. assert.equal(await face.getAttribute("data-expression"), "pill");
  141. await page.clock.runFor(1600);
  142. assert.equal(await face.getAttribute("data-motion"), "blink");
  143. const scale = await face.locator(".wf-face-lids").evaluate((node) => {
  144. const animation = node.getAnimations()[0];
  145. animation.pause();
  146. animation.currentTime = Number(animation.effect.getTiming().duration) * 0.35;
  147. return getComputedStyle(node).transform;
  148. });
  149. assert.match(scale, /0\.33333/);
  150. const closed = await face.locator(".wf-face-closed").evaluate((node) => {
  151. const animation = node.getAnimations()[0];
  152. animation.pause();
  153. animation.currentTime = Number(animation.effect.getTiming().duration) * 0.35;
  154. return {
  155. opacity: getComputedStyle(node).opacity,
  156. eyes: Array.from(node.querySelectorAll("rect"), (n) =>
  157. ["x", "y", "width", "height", "rx"].map((k) => n.getAttribute(k)),
  158. ),
  159. };
  160. });
  161. assert.equal(closed.opacity, "1");
  162. assert.deepEqual(closed.eyes, [
  163. ["7", "8", "4", "2", "1"],
  164. ["13", "8", "4", "2", "1"],
  165. ]);
  166. await page.clock.runFor(430);
  167. assert.equal(await face.getAttribute("data-motion"), "glance");
  168. assert.equal(await face.evaluate((n) => n.style.getPropertyValue("--wf-face-x")), "-4px");
  169. await page.clock.runFor(2200);
  170. assert.equal(await face.getAttribute("data-motion"), "blink");
  171. await page.clock.runFor(450);
  172. assert.equal(await face.evaluate((n) => n.style.getPropertyValue("--wf-face-x")), "0px");
  173. await page.emulateMedia({ reducedMotion: "reduce" });
  174. await page.waitForFunction(() => document.querySelector("svg")?.dataset.motion === "idle");
  175. assert.equal(await face.getAttribute("data-motion"), "idle");
  176. assert.equal(
  177. await face.locator(".wf-face-lids").evaluate((n) => getComputedStyle(n).animationName),
  178. "none",
  179. );
  180. }
  181. assert.deepEqual(errors, []);
  182. await page.emulateMedia({ reducedMotion: "no-preference" });
  183. await page.locator('[data-status="done"]').click();
  184. await page.clock.runFor(2300);
  185. assert.equal(await face.getAttribute("data-expression"), "pill");
  186. assert.equal(await face.getAttribute("data-motion"), "blink");
  187. assert.equal(await face.evaluate((n) => n.style.getPropertyValue("--wf-face-blinks")), "1");
  188. await page.clock.runFor(1000);
  189. assert.equal(await face.getAttribute("data-expression"), "happy");
  190. assert.equal(await face.evaluate((n) => n.style.getPropertyValue("--wf-face-x")), "0px");
  191. await page.locator('[data-status="failed"]').click();
  192. await page.clock.runFor(1600);
  193. assert.equal(await face.getAttribute("data-motion"), "float");
  194. await page.clock.runFor(750);
  195. assert.equal(await face.getAttribute("data-expression"), "focused");
  196. assert.equal(await face.getAttribute("data-motion"), "shake");
  197. const shaking = await face.locator(".wf-face-bounce").evaluate((node) => {
  198. const animation = node.getAnimations()[0];
  199. animation.pause();
  200. animation.currentTime = 22.5;
  201. return getComputedStyle(node).transform;
  202. });
  203. assert.match(shaking, /-0\.9/);
  204. assert.equal(
  205. await face.locator(".wf-face-body").evaluate((n) => getComputedStyle(n).transform),
  206. "none",
  207. );
  208. await page.clock.runFor(1000);
  209. assert.equal(await face.getAttribute("data-expression"), "sad");
  210. await page.locator('[data-status="pending"]').click();
  211. await page.clock.runFor(1600);
  212. assert.equal(await face.getAttribute("data-motion"), "dots-wave");
  213. assert.deepEqual(
  214. await face
  215. .locator('[data-eye-expression="dots"] circle')
  216. .evaluateAll((nodes) => nodes.map((n) => n.getBBox().width)),
  217. [3, 3, 3],
  218. );
  219. const dots = await face.locator('[data-eye-expression="dots"] circle').evaluateAll((nodes) =>
  220. nodes.map((node) => {
  221. const animation = node.getAnimations()[0];
  222. animation.pause();
  223. const delay = Number(animation.effect.getTiming().delay);
  224. animation.currentTime = delay + 250;
  225. return { delay, transform: getComputedStyle(node).transform };
  226. }),
  227. );
  228. assert.deepEqual(
  229. dots.map((d) => d.delay),
  230. [0, 200, 400],
  231. );
  232. assert.ok(dots.every((d) => d.transform.includes("-1.5")));
  233. assert.equal(
  234. await face.locator(".wf-face-body").evaluate((n) => getComputedStyle(n).transform),
  235. "none",
  236. );
  237. await page.clock.runFor(900);
  238. assert.equal(await face.getAttribute("data-motion"), "idle");
  239. assert.equal(await face.getAttribute("data-expression"), "dots");
  240. await page.screenshot({ path: join(output, "preview.png") });
  241. console.log(`Preview: ${join(output, "preview.html")}`);
  242. } finally {
  243. await browser.close();
  244. }
  245. });