// Node 22+, a local preview, and a dedicated Chrome with --remote-debugging-port=9224. // Each run uses a fresh browser context; existing tabs and saved games are untouched. import assert from 'node:assert/strict'; const appUrl = process.argv[2] || 'http://127.0.0.1:8080/'; const debuggerUrl = process.env.CHROME_DEBUG_URL || 'http://127.0.0.1:9224'; const browser = await (await fetch(`${debuggerUrl}/json/version`)).json(); const socket = new WebSocket(browser.webSocketDebuggerUrl); await new Promise((resolve, reject) => { socket.onopen = resolve; socket.onerror = reject; }); let serial = 0; const pending = new Map(); socket.onmessage = ({ data }) => { const response = JSON.parse(data); if (!response.id) return; const request = pending.get(response.id); if (!request) return; clearTimeout(request.timer); pending.delete(response.id); if (response.error) request.reject(new Error(JSON.stringify(response.error))); else request.resolve(response.result); }; function call(method, params = {}, sessionId) { return new Promise((resolve, reject) => { const id = ++serial; const timer = setTimeout(() => { pending.delete(id); reject(new Error(`Timed out: ${method}`)); }, 15000); pending.set(id, { resolve, reject, timer }); socket.send(JSON.stringify({ id, method, params, sessionId })); }); } const wait = ms => new Promise(resolve => setTimeout(resolve, ms)); const { browserContextId } = await call('Target.createBrowserContext'); try { const { targetId } = await call('Target.createTarget', { url: 'about:blank', browserContextId }); const { sessionId } = await call('Target.attachToTarget', { targetId, flatten: true }); const page = (method, params) => call(method, params, sessionId); async function evaluate(expression) { const result = await page('Runtime.evaluate', { expression, awaitPromise: true, returnByValue: true, userGesture: true }); if (result.exceptionDetails) throw new Error(JSON.stringify(result.exceptionDetails)); return result.result.value; } const controls = async () => (await page('Accessibility.getFullAXTree')).nodes; async function until(check, description, timeout = 15000) { const deadline = Date.now() + timeout; do { if (await check()) return; await wait(100); } while (Date.now() < deadline); throw new Error(`Timed out waiting for ${description}`); } async function click(prefix, role = 'button') { let node; await until(async () => { node = (await controls()).find(n => n.role?.value === role && n.name?.value?.startsWith(prefix)); return !!node; }, prefix); const { object } = await page('DOM.resolveNode', { backendNodeId: node.backendDOMNodeId }); if (role === 'button') { await page('Runtime.callFunctionOn', { objectId: object.objectId, functionDeclaration: 'function(){this.click()}', userGesture: true }); } else { // Book cards currently merge their progress bar into their semantics. const result = await page('Runtime.callFunctionOn', { objectId: object.objectId, returnByValue: true, functionDeclaration: 'function(){const r=this.getBoundingClientRect();return {x:r.x+r.width/2,y:r.y+r.height/2}}', }); for (const type of ['mousePressed', 'mouseReleased']) { await page('Input.dispatchMouseEvent', { type, ...result.result.value, button: 'left', clickCount: 1 }); } } await wait(150); } const audio = () => evaluate(`window.__narrationTestAudio.map(a=>({src:a.src,paused:a.paused,time:a.currentTime}))`); async function expectRecording(filename) { await until(async () => (await audio()).some(a => a.src.endsWith(filename) && !a.paused && a.time > .1), filename, 7000); const voices = (await audio()).filter(a => /narration_|midnight_/.test(a.src) && !/ambience|atmosphere/.test(a.src)); assert(voices.filter(a => !a.paused).every(a => a.src.endsWith(filename)), 'An old recording is still playing'); console.log(`Playing correct file: ${filename}`); } await page('Page.enable'); await page('Emulation.setDeviceMetricsOverride', { width: 390, height: 844, deviceScaleFactor: 1, mobile: true }); await page('Page.addScriptToEvaluateOnNewDocument', { source: ` localStorage.setItem('language', JSON.stringify('en')); window.__narrationTestAudio = []; const play = HTMLMediaElement.prototype.play; HTMLMediaElement.prototype.play = function(...args) { if (!window.__narrationTestAudio.includes(this)) window.__narrationTestAudio.push(this); return play.apply(this, args); }; ` }); await page('Page.navigate', { url: appUrl }); await until(() => evaluate(`!!document.querySelector('flt-semantics-placeholder')`), 'game startup', 30000); await evaluate(`document.querySelector('flt-semantics-placeholder').click()`); await click('Mara’s notebook', 'progressbar'); await click('Open Mara’s notebook'); await expectRecording('narration_cottage_dedication.mp3'); await click('Next leaf'); await expectRecording('narration_cottage_window.mp3'); await click('← Previous leaf'); await expectRecording('narration_cottage_dedication.mp3'); await click('Next leaf'); await click('Next leaf'); await wait(3500); assert((await audio()).filter(a => a.src.includes('narration_cottage_')).every(a => a.paused), 'A skipped leaf started narrating'); await click('← Previous leaf'); await expectRecording('narration_cottage_window.mp3'); console.log('Next, previous and rapid page turns use the correct narration.'); } finally { await call('Target.disposeBrowserContext', { browserContextId }); socket.close(); }