// Drives the app in the REAL Chrome on the USB-connected Android phone (only our localhost:4175 tab). import { chromium } from '@playwright/test'; const out = process.argv[2]; const step = process.argv[3] || 'fresh'; const b = await chromium.connectOverCDP('http://localhost:9333'); const ctx = b.contexts()[0]; const p = ctx.pages().find((x) => x.url().startsWith('http://localhost:4175')); if (!p) throw new Error('our tab not found'); p.on('console', (m) => { if (['error', 'warning'].includes(m.type())) console.log('[phone]', m.type(), m.text().slice(0, 160)); }); const eng = () => p.evaluate(() => { const e = window.__earshelf?.engine; return e && { voice: e.voice, model: e.model, index: e.index, playing: e.playing, t: e.audio.currentTime, stats: e.stats }; }); if (step === 'fresh') { const s = await (await ctx.newCDPSession(p)).send('Storage.clearDataForOrigin', { origin: 'http://localhost:4175', storageTypes: 'all' }); await p.reload(); await p.waitForTimeout(1500); console.log(await p.evaluate(() => ({ w: innerWidth, h: innerHeight, dpr: devicePixelRatio, iso: crossOriginIsolated, cores: navigator.hardwareConcurrency, mem: navigator.deviceMemory, conn: navigator.connection?.type, eff: navigator.connection?.effectiveType, voices: speechSynthesis.getVoices().length }))); await p.screenshot({ path: `${out}/real-1-empty.png` }); const t0 = Date.now(); await p.getByRole('button', { name: /Try a sample/ }).click(); await p.waitForFunction(() => window.__earshelf.engine.index >= 1 || speechSynthesis.speaking, null, { timeout: 15000 }).catch(() => console.log('no speech within 15s')); console.log('time to speech ms', Date.now() - t0, await p.evaluate(() => ({ speaking: speechSynthesis.speaking, voices: speechSynthesis.getVoices().length }))); console.log(JSON.stringify(await eng())); await p.waitForTimeout(2500); await p.screenshot({ path: `${out}/real-2-device-voice.png` }); const t1 = Date.now(); await p.waitForFunction(() => ['ready', 'error'].includes(window.__earshelf.engine.model.s), null, { timeout: 600000 }); console.log('model', JSON.stringify((await eng()).model), 'after s', (Date.now() - t1) / 1000); await p.waitForFunction(() => /slower|Switching/.test(document.querySelector('#model-banner').textContent + document.querySelector('#toast').textContent), null, { timeout: 180000 }).catch(() => console.log('no decision within 180s')); console.log('decision after s', (Date.now() - t1) / 1000, '|', (await p.locator('#model-banner').innerText()).slice(0, 160), '|', await p.locator('#toast').innerText()); await p.screenshot({ path: `${out}/real-3-offer.png` }); const prep = p.getByRole('button', { name: 'Prepare this chapter' }); if (await prep.isVisible()) { await p.getByRole('button', { name: 'Pause', exact: true }).click().catch(() => {}); await p.evaluate(() => { const e = window.__earshelf.engine; e.gotoChapter(0); }); await p.waitForTimeout(500); const tp = Date.now(); await prep.click(); await p.waitForTimeout(120000); console.log('prepare for', (Date.now() - tp) / 1000, 's'); console.log('after 90s:', (await p.locator('#model-banner').innerText()).slice(0, 120), JSON.stringify((await eng()).voice)); } await p.screenshot({ path: `${out}/real-3-natural.png` }); } if (step === 'natural') { await p.evaluate(() => { const e = window.__earshelf.engine; e.stats.length = 0; e.gotoChapter(0); }); await p.waitForTimeout(500); const t0 = Date.now(); await p.getByRole('button', { name: 'Play', exact: true }).click(); await p.waitForFunction(() => window.__earshelf.engine.audio.currentTime > 0, null, { timeout: 120000 }); console.log('natural: time to first audio ms', Date.now() - t0); await p.waitForTimeout(60000); const e = await eng(); const rtf = e.stats.reduce((a, x) => a + x.genMs, 0) / Math.max(1, e.stats.reduce((a, x) => a + x.audioMs, 0)); console.log('index', e.index, 'playing', e.playing, 'RTF', rtf.toFixed(2), JSON.stringify(e.stats)); await p.getByRole('button', { name: 'Pause', exact: true }).click(); await p.screenshot({ path: `${out}/real-4-natural-playing.png` }); } await b.close().catch(() => {});