const fs = require('fs'); const path = require('path'); const {pathToFileURL} = require('url'); const {chromium} = require('playwright'); const assert = require('assert'); async function checkElapsedTime(browser, entry, config) { const timed = await browser.newPage(); try { await timed.clock.install({time: new Date('2026-01-01T00:00:00Z')}); await timed.clock.pauseAt(new Date('2026-01-01T00:00:01Z')); await timed.addInitScript(() => { const pending = new Map(); let nextId = 0; window.requestAnimationFrame = callback => { pending.set(++nextId, callback); return nextId; }; window.cancelAnimationFrame = id => pending.delete(id); window.flushVerifierFrame = () => { const frame = [...pending]; const timestamp = performance.now(); for (const [id, callback] of frame) { if (pending.delete(id)) callback(timestamp); } }; }); await timed.goto(entry); const elapsed = 704; const manualWindow = await timed.evaluate(({config, elapsed}) => { return [elapsed - 50, elapsed, elapsed + 50].map(duration => { gameTest.reset(config); return gameTest.step(duration).objects; }); }, {config, elapsed}); // Run the same elapsed time at two frame rates. Native timer callbacks // also run against the virtual clock, so requestAnimationFrame is optional. for (const tick of [16, 64]) { await timed.evaluate(config => { gameTest.reset(config); document.querySelector('#start').click(); window.flushVerifierFrame(); }, config); for (let ms = 0; ms < elapsed; ms += tick) { await timed.clock.runFor(tick); await timed.evaluate(() => window.flushVerifierFrame()); } const animated = await timed.evaluate(() => gameTest.state().objects); const nearManualStep = manualWindow.some(expected => expected.every((object, index) => { const actual = animated[index]; return actual.landed === object.landed && Math.abs(actual.height - object.height) < 0.01 && Math.abs(actual.velocity - object.velocity) < 0.01; })); assert(nearManualStep, `animation with ${tick} ms frames did not advance by real elapsed time`); } } finally { await timed.close(); } } (async () => { const browser = await chromium.launch({headless: true}); try { const page = await browser.newPage({viewport: {width: 1100, height: 800}}); const entry = pathToFileURL(path.resolve('index.html')).href; await page.goto(entry); const reset = config => page.evaluate(config => gameTest.reset(config), config); const step = ms => page.evaluate(ms => gameTest.step(ms), ms); const state = () => page.evaluate(() => gameTest.state()); const pixels = () => page.locator('#scene').evaluate(canvas => canvas.toDataURL()); const capture = async name => { fs.mkdirSync('/logs/verifier/visual-audit', {recursive: true}); await page.screenshot({path: '/logs/verifier/visual-audit/' + name + '.png', fullPage: true}); }; const base = {types: ['ball', 'feather'], vacuum: true, gravity: 9.8, drag: .08, mass: 1}; const run = async config => {await reset({...base, ...config}); return step(20000);}; const vacuum = await run({}); assert(vacuum.objects.every(object => object.landed)); assert(Math.abs(vacuum.objects[0].landingTime - vacuum.objects[1].landingTime) < .02); assert.deepStrictEqual((await run({mass: 5})).objects, vacuum.objects, 'mass may not alter vacuum trajectory'); assert.deepStrictEqual((await run({vacuum: false, drag: 0})).objects, vacuum.objects, 'zero drag must match vacuum'); assert((await run({gravity: 20})).objects[0].landingTime < vacuum.objects[0].landingTime); const low = await run({vacuum: false, drag: .01}); const high = await run({vacuum: false, drag: .08}); const heavy = await run({vacuum: false, drag: .08, mass: 5}); assert(high.objects[1].landingTime > low.objects[1].landingTime); assert(heavy.objects[1].landingTime < high.objects[1].landingTime); assert(high.objects[1].landingTime > high.objects[0].landingTime, 'feather must experience stronger drag'); const reversed = await run({types: ['feather', 'ball'], vacuum: false, drag: .08}); assert(reversed.objects[0].landingTime > reversed.objects[1].landingTime, 'switching the selectors must switch which object experiences more drag'); await run({vacuum: false, drag: .08, mass: 5}); const result = await page.locator('#result').innerText(); assert.equal(Number(await page.locator('#weightVal').innerText()), 5, 'visible mass value must match injected control'); for (const object of heavy.objects) assert(result.includes(object.landingTime.toFixed(2)), 'visible landing time must match state'); await capture('air-drag-landed'); const initial = await reset(base); const before = await pixels(); await step(20); assert.deepStrictEqual((await state()).objects, initial.objects); const partitioned = await step(30); await reset(base); assert.deepStrictEqual((await step(50)).objects, partitioned.objects, 'fractional virtual time must accumulate'); await step(500); const falling = await state(); assert(falling.objects.every((object, index) => object.height < initial.objects[index].height && object.velocity > 0 && !object.landed)); assert.notEqual(await pixels(), before, 'state changes must actually render'); await capture('virtual-falling'); await page.locator('#reset').click(); assert.deepStrictEqual((await state()).objects, initial.objects); assert(!/\d+\.\d+s/.test(await page.locator('#result').innerText()), 'reset must clear landing results'); await page.waitForTimeout(180); assert.deepStrictEqual((await state()).objects, initial.objects, 'reset must stop real animation'); await page.locator('#start').click(); await page.waitForFunction(() => gameTest.state().objects.some(object => object.velocity > 0), null, {timeout: 5000}); for (const [id, value] of [['gravity', '15'], ['weight', '2'], ['drag', '0.04']]) { await page.locator('#' + id).evaluate((input, value) => {input.value = value; input.dispatchEvent(new Event('input', {bubbles: true}));}, value); } await page.locator('#vacuum').uncheck(); assert.deepStrictEqual((await state()).config, {gravity: 15, mass: 2, drag: .04, vacuum: false}); await capture('real-animation-controls'); await page.locator('#reset').click(); await checkElapsedTime(browser, entry, base); } finally {await browser.close();} })().catch(error => {console.error(error); process.exit(1);});