worldflight-assert.mjs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. #!/usr/bin/env node
  2. /**
  3. * Worldflight rig assertions. Not a contact sheet: these are the mechanical
  4. * claims the mode makes, checked one at a time.
  5. *
  6. * node lab/worldflight-assert.mjs --url http://localhost:4520
  7. */
  8. import fs from "node:fs";
  9. import path from "node:path";
  10. import { createRequire } from "node:module";
  11. const { chromium } = createRequire(path.join(process.cwd(), "package.json"))("playwright-core");
  12. const argv = process.argv.slice(2);
  13. const arg = (n, d) => { const i = argv.indexOf(n); return i > -1 && argv[i + 1] ? argv[i + 1] : d; };
  14. const URL = arg("--url", "http://localhost:4520");
  15. const CHROME = [
  16. process.env.SCROLLCRAFT_CHROME,
  17. // Windows
  18. "C:/Program Files/Google/Chrome/Application/chrome.exe",
  19. "C:/Program Files (x86)/Google/Chrome/Application/chrome.exe",
  20. "C:/Program Files/Microsoft/Edge/Application/msedge.exe",
  21. // macOS
  22. "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome",
  23. "/Applications/Chromium.app/Contents/MacOS/Chromium",
  24. "/Applications/Microsoft Edge.app/Contents/MacOS/Microsoft Edge",
  25. // Linux
  26. "/usr/bin/google-chrome",
  27. "/usr/bin/google-chrome-stable",
  28. "/usr/bin/chromium",
  29. "/usr/bin/chromium-browser",
  30. "/snap/bin/chromium",
  31. ].find((p) => p && fs.existsSync(p));
  32. let pass = 0, fail = 0;
  33. const ok = (name, cond, note = "") => {
  34. if (cond) { pass++; console.log(` PASS ${name}${note ? " " + note : ""}`); }
  35. else { fail++; console.log(` FAIL ${name}${note ? " " + note : ""}`); }
  36. };
  37. const browser = await chromium.launch({ executablePath: CHROME, headless: true });
  38. // ============================================================ desktop ======
  39. {
  40. const page = await browser.newPage({ viewport: { width: 1440, height: 900 }, deviceScaleFactor: 1 });
  41. const errs = [];
  42. page.on("console", (m) => { if (m.type() === "error") errs.push(m.text()); });
  43. page.on("pageerror", (e) => errs.push(String(e)));
  44. await page.goto(URL, { waitUntil: "domcontentloaded" });
  45. await page.waitForSelector("html.sc-ready", { timeout: 15000 });
  46. await page.evaluate(() => document.fonts.ready);
  47. await page.waitForTimeout(900);
  48. const geom = await page.evaluate(() => {
  49. const segs = [...document.querySelectorAll("[data-sc-segment]")];
  50. const total = segs.reduce((s, el) => s + (parseFloat(el.dataset.scW) || 1.3), 0);
  51. const spacer = document.querySelector("[data-sc-spacer]");
  52. const stage = document.querySelector("[data-sc-world]");
  53. return {
  54. vh: innerHeight, total,
  55. weights: segs.map((el) => parseFloat(el.dataset.scW) || 1.3),
  56. spacerH: spacer.getBoundingClientRect().height,
  57. wantH: Math.round((total + 1) * innerHeight),
  58. docH: document.body.scrollHeight,
  59. stagePos: getComputedStyle(stage).position,
  60. stageRect: (({ top, left, width, height }) => ({ top, left, width, height }))(stage.getBoundingClientRect()),
  61. copyPos: getComputedStyle(document.querySelector("[data-sc-world-copy]")).position,
  62. seam: parseFloat(document.querySelector('[data-sc-mode="worldflight"]').dataset.scSeam),
  63. };
  64. });
  65. console.log(`\nDESKTOP vh=${geom.vh} weights=[${geom.weights}] total=${geom.total}vh`);
  66. ok("spacer height = (sum weights + 1) x vh",
  67. Math.abs(geom.spacerH - geom.wantH) <= 1, `got ${Math.round(geom.spacerH)}px want ${geom.wantH}px`);
  68. ok("document scroll height is the spacer",
  69. Math.abs(geom.docH - geom.wantH) <= 2, `doc ${geom.docH}px`);
  70. ok("stage is position:fixed and fills the viewport",
  71. geom.stagePos === "fixed" && geom.stageRect.height === geom.vh && geom.stageRect.top === 0);
  72. ok("copy layer is position:fixed", geom.copyPos === "fixed");
  73. // Nothing in flow but the spacer: at any scroll position, the only static or
  74. // relative element that extends past the fold is the spacer and its ancestors.
  75. const flow = await page.evaluate(() => {
  76. const bad = [];
  77. document.querySelectorAll("body *").forEach((el) => {
  78. const cs = getComputedStyle(el);
  79. if (cs.position === "fixed" || cs.position === "absolute") return;
  80. if (el.hasAttribute("data-sc-spacer") || el.hasAttribute("data-sc-mode")) return;
  81. const r = el.getBoundingClientRect();
  82. if (r.height > 4 && r.bottom > innerHeight + 4) {
  83. bad.push(el.tagName + "." + (el.className || "").toString().slice(0, 30));
  84. }
  85. });
  86. return bad;
  87. });
  88. ok("nothing in document flow but the spacer", flow.length === 0, flow.join(", "));
  89. // ---- lerp convergence, inside one leg ----------------------------------
  90. const seg0 = geom.weights[0];
  91. const A = Math.round(0.10 * seg0 * geom.vh);
  92. const B = Math.round(0.85 * seg0 * geom.vh);
  93. await page.evaluate((y) => scrollTo({ top: y, behavior: "instant" }), A);
  94. await page.waitForTimeout(1400);
  95. const trace = await page.evaluate(({ y }) => new Promise((res) => {
  96. const v = document.querySelectorAll("[data-sc-segment] video")[0];
  97. const clip = window.__sc.clips.find((c) => c.el === v);
  98. const s = [];
  99. scrollTo({ top: y, behavior: "instant" });
  100. let n = 0;
  101. (function f() {
  102. s.push(+v.currentTime.toFixed(4));
  103. if (++n < 70) requestAnimationFrame(f);
  104. else res({ s, target: clip.target * (v.duration || 1), dur: v.duration, lerp: clip.lerp });
  105. })();
  106. }), { y: B });
  107. const seq = trace.s;
  108. const start = seq[0], end = seq[seq.length - 1];
  109. const distinct = [...new Set(seq)];
  110. const monotone = seq.every((v, i) => i === 0 || v >= seq[i - 1] - 1e-6);
  111. const overshoot = Math.max(...seq) - trace.target;
  112. console.log(` playhead ${start.toFixed(3)}s -> ${end.toFixed(3)}s target ${trace.target.toFixed(3)}s lerp ${trace.lerp} ${distinct.length} distinct values`);
  113. ok("playhead is lerped, not written 1:1", distinct.length >= 4, `${distinct.length} distinct steps across 70 frames`);
  114. ok("playhead is strictly monotone toward its target", monotone);
  115. ok("playhead does not overshoot", overshoot <= 0.02, `max excess ${overshoot.toFixed(4)}s`);
  116. ok("playhead converges on the target", Math.abs(end - trace.target) < 0.05,
  117. `residual ${Math.abs(end - trace.target).toFixed(4)}s`);
  118. ok("lerp rate is the 0.18 default", Math.abs(trace.lerp - 0.18) < 1e-9);
  119. // ---- deadband: no seek thrash while the page is still -------------------
  120. await page.waitForTimeout(900);
  121. const seeks = await page.evaluate(() => new Promise((res) => {
  122. let n = 0;
  123. const vs = [...document.querySelectorAll("video")];
  124. const h = () => n++;
  125. vs.forEach((v) => v.addEventListener("seeked", h));
  126. setTimeout(() => { vs.forEach((v) => v.removeEventListener("seeked", h)); res(n); }, 1000);
  127. }));
  128. ok("deadband holds: no seeks over 1s of stillness", seeks === 0, `${seeks} seeked events`);
  129. // ---- crossfade band ----------------------------------------------------
  130. const boundary = seg0 * geom.vh;
  131. const half = (geom.seam / 2) * geom.vh;
  132. const band = [];
  133. for (let i = 0; i < 5; i++) {
  134. const y = Math.round(boundary - half + (2 * half) * (i / 4));
  135. await page.evaluate((y) => scrollTo({ top: y, behavior: "instant" }), y);
  136. await page.waitForTimeout(120);
  137. band.push(await page.evaluate(() => {
  138. const segs = [...document.querySelectorAll("[data-sc-segment]")];
  139. return segs.map((s) => +(parseFloat(getComputedStyle(s).opacity) || 0).toFixed(4));
  140. }));
  141. }
  142. const incoming = band.map((b) => b[1]);
  143. const outgoing = band.map((b) => b[0]);
  144. console.log(` seam band incoming=[${incoming}] outgoing=[${outgoing}]`);
  145. ok("incoming leg opacity is strictly monotone across the seam",
  146. incoming.every((v, i) => i === 0 || v > incoming[i - 1]));
  147. ok("incoming leg starts at 0 and ends at 1", incoming[0] <= 0.02 && incoming[4] >= 0.98);
  148. // The outgoing leg must hold at full strength for the whole dissolve and only
  149. // release once the incoming one covers it completely. A crossfade where BOTH
  150. // sides are partly transparent shows the page ground through the middle of
  151. // the seam, which is the flash this mode exists to remove.
  152. ok("outgoing leg holds full opacity while the incoming one is still arriving",
  153. incoming.every((v, i) => v >= 0.999 || outgoing[i] >= 0.999), `[${outgoing}]`);
  154. ok("outgoing leg only releases once it is fully covered",
  155. outgoing[4] === 0 && incoming[4] >= 0.999);
  156. // ---- copy transform cap ------------------------------------------------
  157. const maxT = [];
  158. for (let i = 0; i <= 12; i++) {
  159. const y = Math.round((geom.docH - geom.vh) * (i / 12));
  160. await page.evaluate((y) => scrollTo({ top: y, behavior: "instant" }), y);
  161. await page.waitForTimeout(60);
  162. maxT.push(await page.evaluate(() => {
  163. let m = 0;
  164. document.querySelectorAll("[data-sc-copy]").forEach((el) => {
  165. const t = new DOMMatrixReadOnly(getComputedStyle(el).transform);
  166. m = Math.max(m, Math.abs(t.m42), Math.abs(t.m41));
  167. });
  168. return +m.toFixed(2);
  169. }));
  170. }
  171. const worstT = Math.max(...maxT);
  172. ok("copy never translates past 4vh", worstT <= geom.vh * 0.04 + 1,
  173. `worst ${worstT}px, cap ${(geom.vh * 0.04).toFixed(0)}px`);
  174. // ---- every leg reaches full opacity, every clip leaves its poster -------
  175. const reach = {};
  176. for (let i = 0; i <= 24; i++) {
  177. const y = Math.round((geom.docH - geom.vh) * (i / 24));
  178. await page.evaluate((y) => scrollTo({ top: y, behavior: "instant" }), y);
  179. await page.waitForTimeout(260);
  180. const s = await page.evaluate(() => [...document.querySelectorAll("[data-sc-segment]")].map((el, i) => ({
  181. i, op: parseFloat(getComputedStyle(el).opacity) || 0,
  182. painted: el.classList.contains("sc-has-clip"),
  183. })));
  184. s.forEach((x) => {
  185. reach[x.i] = reach[x.i] || { op: 0, painted: false };
  186. reach[x.i].op = Math.max(reach[x.i].op, x.op);
  187. reach[x.i].painted = reach[x.i].painted || x.painted;
  188. });
  189. }
  190. ok("every leg reaches full opacity",
  191. Object.values(reach).every((r) => r.op >= 0.99),
  192. Object.entries(reach).map(([i, r]) => `${i}:${r.op.toFixed(2)}`).join(" "));
  193. ok("every leg paints a real frame (no clip stuck on poster)",
  194. Object.values(reach).every((r) => r.painted),
  195. Object.entries(reach).map(([i, r]) => `${i}:${r.painted ? "clip" : "POSTER"}`).join(" "));
  196. ok("no console errors", errs.length === 0, errs.slice(0, 3).join(" | "));
  197. await page.close();
  198. }
  199. // ==================================================== reduced motion =======
  200. {
  201. const page = await browser.newPage({
  202. viewport: { width: 1440, height: 900 }, deviceScaleFactor: 1, reducedMotion: "reduce",
  203. });
  204. const media = [];
  205. page.on("request", (r) => { if (/\.(mp4|webm)(\?|$)/.test(r.url())) media.push(r.url()); });
  206. await page.goto(URL, { waitUntil: "domcontentloaded" });
  207. await page.waitForSelector("html.sc-ready", { timeout: 15000 });
  208. await page.waitForTimeout(800);
  209. console.log("\nREDUCED MOTION");
  210. const rmSeen = { legs: {}, copy: {} };
  211. for (let i = 0; i <= 20; i++) {
  212. const h = await page.evaluate(() => document.body.scrollHeight - innerHeight);
  213. await page.evaluate((y) => scrollTo({ top: y, behavior: "instant" }), Math.round(h * (i / 20)));
  214. await page.waitForTimeout(90);
  215. const s = await page.evaluate(() => ({
  216. legs: [...document.querySelectorAll("[data-sc-segment]")].map((el) => ({
  217. op: parseFloat(getComputedStyle(el).opacity) || 0,
  218. poster: (() => { const p = el.querySelector(".sc-world__poster"); return p ? getComputedStyle(p).transform : "none"; })(),
  219. })),
  220. copy: [...document.querySelectorAll("[data-sc-copy]")].map((el) => ({
  221. t: (el.textContent || "").trim().replace(/\s+/g, " ").slice(0, 24),
  222. op: parseFloat(getComputedStyle(el).opacity) || 0,
  223. tf: getComputedStyle(el).transform,
  224. })),
  225. }));
  226. s.legs.forEach((l, k) => {
  227. rmSeen.legs[k] = rmSeen.legs[k] || { op: 0, tf: new Set() };
  228. rmSeen.legs[k].op = Math.max(rmSeen.legs[k].op, l.op);
  229. rmSeen.legs[k].tf.add(l.poster);
  230. });
  231. s.copy.forEach((c) => {
  232. rmSeen.copy[c.t] = rmSeen.copy[c.t] || { op: 0, tf: new Set() };
  233. rmSeen.copy[c.t].op = Math.max(rmSeen.copy[c.t].op, c.op);
  234. rmSeen.copy[c.t].tf.add(c.tf);
  235. });
  236. }
  237. await page.waitForTimeout(500);
  238. ok("no clip is ever fetched", media.length === 0, media.join(", "));
  239. ok("every leg's poster still reaches full opacity",
  240. Object.values(rmSeen.legs).every((l) => l.op >= 0.99),
  241. Object.entries(rmSeen.legs).map(([i, l]) => `${i}:${l.op.toFixed(2)}`).join(" "));
  242. ok("no poster transform",
  243. Object.values(rmSeen.legs).every((l) => [...l.tf].every((t) => t === "none")));
  244. ok("every copy block still reaches full opacity",
  245. Object.values(rmSeen.copy).every((c) => c.op >= 0.99),
  246. Object.entries(rmSeen.copy).map(([t, c]) => `${c.op.toFixed(2)} "${t}"`).join(" | "));
  247. ok("no copy transform",
  248. Object.values(rmSeen.copy).every((c) => [...c.tf].every((t) => t === "none")));
  249. await page.close();
  250. }
  251. await browser.close();
  252. console.log(`\n${pass} passed, ${fail} failed`);
  253. process.exit(fail ? 1 : 0);