11/**
2- * Tests for the `seedRandomFromFrame` gate on `buildVirtualTimeShim`.
3- *
4- * 1. Backwards compatibility — `buildVirtualTimeShim({
5- * seedRandomFromFrame: false })` returns a string byte-identical to the
6- * legacy `VIRTUAL_TIME_SHIM`. Existing in-process callers see no
7- * difference.
8- *
9- * 2. Distributed determinism — with `seedRandomFromFrame: true`, the shim's
10- * `seekToTime(ms)` reseeds a Mulberry32 PRNG keyed by the virtual time
11- * and replaces `Math.random` / `crypto.getRandomValues` with that PRNG.
12- * `seekToTime(N)` → N `Math.random()` calls is a deterministic
13- * sequence; reseeking to the same time restarts the sequence.
2+ * Tests for the seeded `Math.random` / `crypto.getRandomValues` in `VIRTUAL_TIME_SHIM`.
143 *
154 * The shim is executed inside `node:vm` with a synthetic `window`/`Math` so
165 * tests don't need real Chrome.
176 */
187
198import { describe , expect , it } from "bun:test" ;
209import { Script , createContext , type Context } from "node:vm" ;
21- import { buildVirtualTimeShim , VIRTUAL_TIME_SHIM } from "./fileServer.js" ;
10+ import { VIRTUAL_TIME_SHIM } from "./fileServer.js" ;
2211
2312/**
2413 * Build a fresh VM context with its own globals (its own Math, its own
@@ -62,50 +51,16 @@ function runShim(shimSource: string): {
6251 return ctx ;
6352}
6453
65- describe ( "buildVirtualTimeShim — backwards compatibility" , ( ) => {
66- it ( "default (seedRandomFromFrame: false) is byte-identical to VIRTUAL_TIME_SHIM" , ( ) => {
67- // The const that existing call sites import:
68- // renderOrchestrator.ts: preHeadScripts: [VIRTUAL_TIME_SHIM]
69- // probeStage.ts: preHeadScripts: [VIRTUAL_TIME_SHIM]
70- // Must continue to emit the same script.
71- const built = buildVirtualTimeShim ( { seedRandomFromFrame : false } ) ;
72- expect ( built ) . toBe ( VIRTUAL_TIME_SHIM ) ;
73- } ) ;
74-
75- it ( "default shim does not mention any seeded-RNG identifiers" , ( ) => {
76- const shim = buildVirtualTimeShim ( { seedRandomFromFrame : false } ) ;
77- expect ( shim ) . not . toContain ( "mulberry32" ) ;
78- expect ( shim ) . not . toContain ( "reseedRngFromTime" ) ;
79- expect ( shim ) . not . toContain ( "__seededGetRandomValues" ) ;
80- expect ( shim ) . not . toContain ( "Math.random = " ) ;
81- } ) ;
82-
83- it ( "default shim leaves Math.random pointing at the VM's native function" , ( ) => {
84- const shim = buildVirtualTimeShim ( { seedRandomFromFrame : false } ) ;
85- const { run } = runShim ( shim ) ;
86- // toString() of native Math.random is `function random() { [native code] }`
87- const isNative = run < boolean > ( `/\\[native code\\]/.test(Math.random.toString())` ) ;
88- expect ( isNative ) . toBe ( true ) ;
89- } ) ;
90- } ) ;
91-
92- describe ( "buildVirtualTimeShim — seedRandomFromFrame: true" , ( ) => {
93- it ( "emits the seeded-RNG block" , ( ) => {
94- const shim = buildVirtualTimeShim ( { seedRandomFromFrame : true } ) ;
95- expect ( shim ) . toContain ( "mulberry32" ) ;
96- expect ( shim ) . toContain ( "reseedRngFromTime" ) ;
97- expect ( shim ) . toContain ( "__seededGetRandomValues" ) ;
98- } ) ;
99-
54+ describe ( "VIRTUAL_TIME_SHIM seeded randomness" , ( ) => {
10055 it ( "replaces Math.random with a non-native PRNG" , ( ) => {
101- const shim = buildVirtualTimeShim ( { seedRandomFromFrame : true } ) ;
56+ const shim = VIRTUAL_TIME_SHIM ;
10257 const { run } = runShim ( shim ) ;
10358 const isNative = run < boolean > ( `/\\[native code\\]/.test(Math.random.toString())` ) ;
10459 expect ( isNative ) . toBe ( false ) ;
10560 } ) ;
10661
10762 it ( "produces identical Math.random sequences across two fresh VMs at the same time" , ( ) => {
108- const shim = buildVirtualTimeShim ( { seedRandomFromFrame : true } ) ;
63+ const shim = VIRTUAL_TIME_SHIM ;
10964 const drawSequence = `(() => {
11065 window.__HF_VIRTUAL_TIME__.seekToTime(1234);
11166 const seq = [];
@@ -126,7 +81,7 @@ describe("buildVirtualTimeShim — seedRandomFromFrame: true", () => {
12681 it ( "re-seeking to the same time produces the same Math.random sequence" , ( ) => {
12782 // The core determinism contract for retries: same (planDir, chunkIndex) →
12883 // same frame N → same seekToTime(t_N) → same Math.random outputs.
129- const shim = buildVirtualTimeShim ( { seedRandomFromFrame : true } ) ;
84+ const shim = VIRTUAL_TIME_SHIM ;
13085 const { run } = runShim ( shim ) ;
13186 const observed = run < { first : number [ ] ; second : number [ ] } > ( `(() => {
13287 window.__HF_VIRTUAL_TIME__.seekToTime(42);
@@ -141,7 +96,7 @@ describe("buildVirtualTimeShim — seedRandomFromFrame: true", () => {
14196 } ) ;
14297
14398 it ( "different times produce different Math.random sequences" , ( ) => {
144- const shim = buildVirtualTimeShim ( { seedRandomFromFrame : true } ) ;
99+ const shim = VIRTUAL_TIME_SHIM ;
145100 const { run } = runShim ( shim ) ;
146101 const observed = run < { t0 : number [ ] ; t1 : number [ ] } > ( `(() => {
147102 window.__HF_VIRTUAL_TIME__.seekToTime(0);
@@ -154,7 +109,7 @@ describe("buildVirtualTimeShim — seedRandomFromFrame: true", () => {
154109 } ) ;
155110
156111 it ( "seeded crypto.getRandomValues writes deterministic bytes" , ( ) => {
157- const shim = buildVirtualTimeShim ( { seedRandomFromFrame : true } ) ;
112+ const shim = VIRTUAL_TIME_SHIM ;
158113 const drawBytes = `(() => {
159114 window.__HF_VIRTUAL_TIME__.seekToTime(7);
160115 const buf = new Uint8Array(64);
@@ -168,7 +123,7 @@ describe("buildVirtualTimeShim — seedRandomFromFrame: true", () => {
168123 } ) ;
169124
170125 it ( "seeded crypto.getRandomValues handles odd byte lengths" , ( ) => {
171- const shim = buildVirtualTimeShim ( { seedRandomFromFrame : true } ) ;
126+ const shim = VIRTUAL_TIME_SHIM ;
172127 const { run } = runShim ( shim ) ;
173128 const lengths = run < number [ ] > ( `(() => {
174129 window.__HF_VIRTUAL_TIME__.seekToTime(3);
@@ -182,4 +137,16 @@ describe("buildVirtualTimeShim — seedRandomFromFrame: true", () => {
182137 })()` ) ;
183138 expect ( lengths ) . toEqual ( [ 1 , 2 , 3 , 4 , 5 , 7 , 31 , 33 ] ) ;
184139 } ) ;
140+
141+ it ( "draws the same Math.random sequence at page load in two fresh pages" , ( ) => {
142+ // A GSAP stagger { from: "random" } draws its order when the page script runs, before any seek.
143+ const drawAtLoad = `(() => {
144+ const seq = [];
145+ for (let i = 0; i < 16; i++) seq.push(Math.random());
146+ return seq;
147+ })()` ;
148+ const seqA = runShim ( VIRTUAL_TIME_SHIM ) . run < number [ ] > ( drawAtLoad ) ;
149+ const seqB = runShim ( VIRTUAL_TIME_SHIM ) . run < number [ ] > ( drawAtLoad ) ;
150+ expect ( seqA ) . toEqual ( seqB ) ;
151+ } ) ;
185152} ) ;
0 commit comments