Files
CyberGril/web/tests/hel878_shots.mjs
T

259 lines
24 KiB
JavaScript

// HEL-878 browser check: drives headless Edge over CDP against tests/hel878_mock.mjs (real voice on the approved
// conversation, with tests/fake-livekit.js in place of the LiveKit client and Chromium's fake audio device in place of
// a microphone). Usage: node tests/hel878_shots.mjs <edge.exe> <outDir> [port]. One PASS/FAIL line per check.
import { spawn } from 'node:child_process';
import { mkdirSync, writeFileSync, rmSync } from 'node:fs';
import { join } from 'node:path';
import { tmpdir } from 'node:os';
const [edge, out, portArg] = process.argv.slice(2);
const port = Number(portArg || 8878), base = `http://127.0.0.1:${port}`, cdpPort = 9478;
mkdirSync(out, { recursive: true });
const profile = join(tmpdir(), `hel878-edge-${Date.now()}`);
const browser = spawn(edge, ['--headless=new', `--remote-debugging-port=${cdpPort}`, `--user-data-dir=${profile}`, '--no-first-run', '--hide-scrollbars', '--force-color-profile=srgb', '--use-fake-device-for-media-stream', '--use-fake-ui-for-media-stream', '--autoplay-policy=no-user-gesture-required', 'about:blank'], { stdio: 'ignore' });
const sleep = ms => new Promise(r => setTimeout(r, ms));
let targets;
for (let i = 0; i < 60 && !targets; i++) { try { targets = await (await fetch(`http://127.0.0.1:${cdpPort}/json`)).json(); } catch { await sleep(250); } }
const ws = new WebSocket(targets.find(t => t.type === 'page').webSocketDebuggerUrl);
await new Promise(r => ws.addEventListener('open', r, { once: true }));
let seq = 0; const waiting = new Map();
ws.addEventListener('message', ({ data }) => { const m = JSON.parse(data); if (m.id && waiting.has(m.id)) { const { ok, ko } = waiting.get(m.id); waiting.delete(m.id); m.error ? ko(new Error(m.error.message)) : ok(m.result); } });
const cdp = (method, params = {}) => new Promise((ok, ko) => { const id = ++seq; waiting.set(id, { ok, ko }); ws.send(JSON.stringify({ id, method, params })); });
const js = async expr => { const r = await cdp('Runtime.evaluate', { expression: expr, returnByValue: true, awaitPromise: true }); if (r.exceptionDetails) throw new Error(r.exceptionDetails.exception?.description || 'js error'); return r.result.value; };
const results = [];
const check = (name, ok, detail = '') => { results.push(ok); console.log(`${ok ? 'PASS' : 'FAIL'} ${name}${detail ? ` — ${detail}` : ''}`); };
const ctl = async path => (await fetch(`${base}${path}`)).json();
const shot = async name => { const { data } = await cdp('Page.captureScreenshot', { format: 'png' }); writeFileSync(join(out, `${name}.png`), Buffer.from(data, 'base64')); };
const click = async (selector, text = '') => js(`(() => { const el = [...document.querySelectorAll(${JSON.stringify(selector)})].find(e => e.offsetParent !== null && e.textContent.includes(${JSON.stringify(text)})); if (!el) return false; el.click(); return true; })()`);
const text = sel => js(`[...document.querySelectorAll(${JSON.stringify(sel)})].map(e => e.innerText).join('\\n')`);
const exists = sel => js(`!!document.querySelector(${JSON.stringify(sel)})`);
const said = () => js(`document.querySelector('.conversation .sr')?.textContent || ''`);
const status = () => js(`document.querySelector('.talk-status')?.innerText || ''`);
const row = () => js(`document.querySelector('.voice-row.real')?.innerText || ''`);
const phase = () => js(`document.querySelector('.voice-row.real')?.dataset.phase || 'off'`);
const lk = expr => js(`window.__lk ? (${expr}) : null`);
const type = t => js(`(() => { const el = document.querySelector('textarea'); const set = Object.getOwnPropertyDescriptor(HTMLTextAreaElement.prototype, 'value').set; set.call(el, ${JSON.stringify(t)}); el.dispatchEvent(new Event('input', { bubbles: true })); return true; })()`);
const until = async (fn, ms = 4000) => { const end = Date.now() + ms; while (Date.now() < end) { if (await fn()) return true; await sleep(100); } return false; };
const views = { desktop: { width: 1440, height: 900, deviceScaleFactor: 1, mobile: false }, phone: { width: 390, height: 844, deviceScaleFactor: 2, mobile: true } };
async function view(v) { await cdp('Emulation.setDeviceMetricsOverride', views[v]); await cdp('Emulation.setTouchEmulationEnabled', { enabled: views[v].mobile, maxTouchPoints: 5 }); }
async function boot(theme) {
await cdp('Page.navigate', { url: `${base}/` }); await sleep(500);
await js(`localStorage.setItem('cg.theme', ${JSON.stringify(theme)}); sessionStorage.clear(); true`);
await cdp('Page.navigate', { url: `${base}/` }); await sleep(1600);
}
async function frames(name, ms) { const list = []; const end = Date.now() + ms; while (Date.now() < end) { const { data } = await cdp('Page.captureScreenshot', { format: 'jpeg', quality: 70 }); list.push(data); } list.forEach((d, i) => writeFileSync(join(out, `${name}-${String(i).padStart(2, '0')}.jpg`), Buffer.from(d, 'base64'))); return list.length; }
// Every microphone request is counted (the fake device still answers it); a test can make the next one refuse.
await cdp('Page.enable'); await cdp('Runtime.enable'); await cdp('Network.enable');
await cdp('Page.addScriptToEvaluateOnNewDocument', { source: `window.__asked = 0; window.__refuseMic = ''; try { const md = navigator.mediaDevices, real = md.getUserMedia.bind(md); md.getUserMedia = c => { window.__asked++; if (window.__refuseMic) return Promise.reject(new DOMException('refused', window.__refuseMic)); return real(c); }; } catch {}` });
const micsLive = () => lk(`__lk.mics.filter(t => t.readyState === 'live').length`);
const herAudio = () => js(`document.querySelectorAll('[data-voice="her"] audio').length`);
const calls = async () => (await ctl('/__log'));
async function startVoice() {
await click('.voice-button'); await until(() => exists('.modal.open'));
await click('.modal.open .btn-primary', '开始语音');
await until(async () => (await phase()) === 'joining');
await until(async () => (await lk('__lk.rooms.at(-1)?.state')) === 'connected');
await lk('__lk.join()'); await sleep(200);
}
const cleanEnd = async label => {
await sleep(400);
const s = await ctl('/__sessions'), mics = await micsLive(), audio = await herAudio(), holder = await exists('[data-voice="her"]');
check(`${label}: microphone stopped, her audio and room gone, server room deleted`, mics === 0 && audio === 0 && !holder && s.open.length === 0 && (await phase()) === 'off', `mics=${mics} audio=${audio} holder=${holder} open=${s.open.length}`);
};
// ── 1. Arriving at the conversation: one honest status read; no room, no microphone, no client download. ──
await view('desktop'); await boot('moon'); await calls();
await click('.nav-item', '交谈'); await sleep(1400);
let log = await calls();
check('status read on arrival only: no session, no microphone, no LiveKit client', log.filter(l => l.includes('/api/voice')).join() === 'GET /api/voice/status' && await js('window.__asked') === 0 && await js('!window.LivekitClient'), log.filter(l => l.includes('voice')).join(' | '));
check('voice button offers to start, honestly labelled', await js(`document.querySelector('.voice-button').getAttribute('aria-label')`) === '开始语音');
await shot('01-moon-desktop-talk-voice-off');
// ── 2. Not configured / TTS down: the button says so and nothing starts. ──
await ctl('/__voice?configured=0'); await boot('moon'); await click('.nav-item', '交谈'); await sleep(1400); await calls();
await click('.voice-button'); await sleep(500);
check('not configured: refusal in her own words, no dialog, no session, no microphone', (await status()).includes('语音还没有在这台服务上配置') && !(await exists('.modal.open')) && !(await calls()).some(l => l.startsWith('POST')) && await js('window.__asked') === 0 && await exists('.voice-button.denied'), await status());
await shot('02-moon-desktop-voice-not-configured');
await ctl('/__voice?configured=1&tts=0'); await click('.voice-button'); await sleep(500);
check('TTS down: says her voice service is away', (await status()).includes('她的声音服务这会儿不在'), await status());
await ctl('/__voice?configured=1&tts=1');
// ── 3. The ask: what happens, where the sound goes, that it is not yet real-device accepted. Cancel asks nothing. ──
await click('.voice-button'); await until(() => exists('.modal.open'));
const ask = await text('.modal.open');
check('the dialog explains microphone, destination, same memory, how to stop, and the unverified path', ask.includes('麦克风') && ask.includes('这台服务器') && ask.includes('同一份记忆') && ask.includes('立即关闭') && ask.includes('还没有经过真机验收'), ask.replace(/\n/g, ' '));
await shot('03-moon-desktop-voice-ask');
await click('.modal.open .btn-ghost', '取消'); await sleep(300);
check('cancel: no microphone request, no session', await js('window.__asked') === 0 && (await ctl('/__sessions')).made === 0);
// ── 4. Start: opening → waiting for her → listening. ──
await click('.voice-button'); await until(() => exists('.modal.open')); await calls();
await click('.modal.open .btn-primary', '开始语音');
await until(async () => (await phase()) === 'joining');
await shot('04-moon-desktop-voice-joining');
check('waiting for her is shown as waiting, not as connected', (await row()).includes('在等栖接起') && (await text('.topbar .state')).includes('未经真机验收'), await row());
await until(async () => (await lk('__lk.rooms.at(-1)?.state')) === 'connected');
log = await calls();
check('one session opened with a server token; one microphone; mic published to that room', log.filter(l => l === 'POST /api/voice/session').length === 1 && (await lk('__lk.connects.at(-1).token')).startsWith('token-sess-') && await js('window.__asked') === 1 && await lk('__lk.published.length') === 1);
await lk('__lk.join()'); await sleep(300);
check('her worker joined: listening, wave in your colour, her audio attached', (await phase()) === 'listening' && (await row()).includes('在听你说') && await herAudio() === 1, await row());
await frames('m1-voice-in', 700);
await shot('05-moon-desktop-voice-listening');
// ── 5. A voice turn: your draft, the record row, her thinking and speaking, then her words on the stage. ──
await lk(`__lk.say({ kind: 'interim', role: 'user', text: '今天下班的路上' })`); await sleep(200);
check('interim words show as a draft line beside the state', (await row()).includes('今天下班的路上'));
await shot('06-moon-desktop-voice-heard');
await ctl('/__voice-turn?stage=user&text=' + encodeURIComponent('今天下班的路上看到一只猫'));
await lk(`__lk.say({ kind: 'user_final', role: 'user', text: '今天下班的路上看到一只猫' })`); await lk(`__lk.state('thinking')`);
await until(async () => (await said()).includes('看到一只猫'));
check('your spoken words enter the stage from the server record (not from the subtitle)', (await said()).includes('看到一只猫') && (await phase()) === 'thinking', await said());
await shot('07-moon-desktop-voice-thinking');
await lk(`__lk.state('speaking')`); await sleep(250);
check('speaking: her name, and that you can cut in by speaking', (await row()).includes('栖在说') && (await row()).includes('开口就能打断'));
await frames('m2-voice-speaking', 900);
await shot('08-moon-desktop-voice-speaking');
await ctl('/__voice-turn?stage=reply&text=' + encodeURIComponent('是橘色的那只吗?它最近总在桥下晒太阳。'));
await lk(`__lk.say({ kind: 'turn_start', role: 'assistant', text: '', speech_id: 'sp-878-1' })`); await lk(`__lk.say({ kind: 'assistant_final', role: 'assistant', text: '是橘色的那只吗?它最近总在桥下晒太阳。', speech_id: 'sp-878-1', turn_id: 'voice-1' })`); await lk(`__lk.say({ kind: 'turn_end', role: 'assistant', text: '', speech_id: 'sp-878-1', turn_id: 'voice-1' })`); await lk(`__lk.state('listening')`);
await until(async () => (await said()).includes('晒太阳'), 6000);
check('her spoken answer reaches the stage once it is in the record', (await said()).includes('晒太阳'), await said());
await shot('09-moon-desktop-voice-reply-on-stage');
await click('.log-btn'); await sleep(700);
const rec = await text('.log-panel .scroll');
check('the record holds the voice turn once, same conversation as text', rec.split('看到一只猫').length === 2 && rec.split('晒太阳').length === 2 && rec.includes('下雨天'));
await shot('10-moon-desktop-voice-record');
await click('.log-panel .btn-round'); await sleep(400);
// ── 6. φ: her failure line is heard and shown, never recorded. ──
await lk(`__lk.say({ kind: 'turn_start', role: 'assistant', text: '', speech_id: 'sp-878-phi' })`); await lk(`__lk.say({ kind: 'error', role: 'assistant', text: '我刚刚没接上话,你再说一遍好吗?', speech_id: 'sp-878-phi' })`); await lk(`__lk.say({ kind: 'turn_cancel', role: 'assistant', text: '', speech_id: 'sp-878-phi', reason: 'error_phi' })`); await sleep(400);
check('φ line shown as heard, marked not recorded; not in the record', (await row()).includes('没接上话') && (await row()).includes('未记入聊天') && !(await ctl('/__rows')).some(r => r.text.includes('没接上话')));
await shot('11-moon-desktop-voice-phi');
// ── 7. Typing over her spoken answer: what is left of it goes silent until she listens again. ──
await lk(`__lk.state('speaking')`); await sleep(150);
await type('等等,我先打字说'); await js(`document.querySelector('.send').click(), true`); await sleep(300);
check('text over voice: her remaining audio is muted', await js(`[...document.querySelectorAll('[data-voice="her"] audio')].every(a => a.muted)`));
await lk(`__lk.state('listening')`); await sleep(150);
check('…and unmuted once she is listening again', await js(`[...document.querySelectorAll('[data-voice="her"] audio')].every(a => !a.muted)`));
await until(async () => !(await exists('.reader-bar .stop')), 8000);
// ── 8. Speaking over a text reply: the text turn is cut (not "failed"), your words stay as said. ──
await type('再给我讲讲那只猫'); await js(`document.querySelector('.send').click(), true`); await sleep(1300);
check('text reply streaming', await exists('.reader-bar .stop'));
await ctl('/__cut'); await ctl('/__voice-turn?stage=user&text=' + encodeURIComponent('算了先不说了'));
await lk(`__lk.say({ kind: 'user_final', role: 'user', text: '算了先不说了' })`); await sleep(1500);
const cutStatus = await status();
check('voice over text: no failure notice, no retry, draft not refilled', !cutStatus.includes('连接中断') && !(await exists('.talk-status .retry')) && await js(`document.querySelector('textarea').value`) === '', cutStatus);
await click('.log-btn'); await sleep(700);
const cutRec = await text('.log-panel .scroll');
check('…the cut text turn is marked cancelled in the record; the voice words are there once', cutRec.includes('已取消') && cutRec.split('算了先不说了').length === 2, cutRec.slice(-200).replace(/\n/g, ' '));
await click('.log-panel .btn-round'); await sleep(300);
// ── 9. Autoplay held back: a tap lets her voice out. ──
await lk('__lk.block()'); await sleep(200);
check('blocked audio offers a tap', (await row()).includes('点一下听栖的声音'));
await shot('12-moon-desktop-voice-audio-blocked');
await click('.voice-row.real button', '点一下'); await sleep(200);
check('…and the offer goes away after the tap', !(await row()).includes('点一下'));
// ── 10. 结束语音: everything stops at once; late events from the old room change nothing. ──
await calls();
await click('.voice-row.real .voice-end');
await cleanEnd('结束语音');
check('ended: says the microphone is closed; DELETE sent', (await status()).includes('麦克风已关闭') && (await calls()).some(l => l.startsWith('DELETE /api/voice/session/')), await status());
await shot('13-moon-desktop-voice-ended');
await lk(`__lk.state('speaking')`); await lk(`__lk.say({ kind: 'user_final', role: 'user', text: '幽灵' })`); await sleep(500);
check('late events after the end do not revive anything', (await phase()) === 'off' && !(await exists('.voice-row.real')) && !(await calls()).some(l => l.startsWith('POST')));
// ── 11. Microphone permission refused: nothing opened. ──
await js(`window.__refuseMic = 'NotAllowedError', true`); await calls();
await click('.voice-button'); await until(() => exists('.modal.open')); await click('.modal.open .btn-primary', '开始语音'); await sleep(800);
check('permission refused: told plainly, no session opened, text still there', (await status()).includes('没有拿到麦克风权限') && !(await calls()).some(l => l.startsWith('POST')) && await exists('textarea'), await status());
await shot('14-moon-desktop-voice-denied');
await js(`window.__refuseMic = '', true`);
// ── 12. Permission revoked mid-call / room dropped / server refusal. ──
await startVoice();
await lk(`(__lk.mics.at(-1).dispatchEvent(new Event('ended')), true)`);
await sleep(300);
check('microphone revoked mid-call: call ends, said so', (await status()).includes('麦克风被关掉了'), await status());
await cleanEnd('revoked');
await startVoice(); await lk('__lk.drop()');
await sleep(300);
check('room dropped: call ends, said so', (await status()).includes('通话断开了'), await status());
await cleanEnd('dropped');
// HEL-897: the dropped call's DELETE answers only after a new call is up; the late answer may not touch the new call.
await startVoice();
const firstSession = (await ctl('/__sessions')).open.at(-1);
await ctl(`/__hold-delete?id=${firstSession}`); await lk('__lk.drop()'); await sleep(300);
check('slow DELETE: dropped call says so and its microphone stops before the server answers', (await status()).includes('通话断开了') && await micsLive() === 0 && (await ctl('/__sessions')).open.includes(firstSession), await status());
await startVoice();
const secondSession = (await ctl('/__sessions')).open.find(s => s !== firstSession);
check('…a new call is up meanwhile: listening', (await phase()) === 'listening' && await micsLive() === 1);
await ctl('/__hold-delete'); await sleep(800);
const lateOpen = (await ctl('/__sessions')).open;
check('…the old DELETE answers late: the new call stays listening, no old notice, one live microphone, her audio, only the new room open', (await phase()) === 'listening' && !(await status()).includes('通话断开了') && await micsLive() === 1 && await herAudio() === 1 && lateOpen.length === 1 && lateOpen[0] === secondSession, `phase=${await phase()} status=${await status()} open=${lateOpen.join()}`);
await shot('12b-moon-desktop-voice-new-call-after-late-delete');
await click('.voice-row.real .voice-end'); await cleanEnd('new call after the late old DELETE');
await ctl('/__refuse/503'); await click('.voice-button'); await until(() => exists('.modal.open')); await click('.modal.open .btn-primary', '开始语音'); await sleep(900);
check('server refusal shows its detail; microphone released', (await status()).includes('她的声音服务这会儿不在') && await micsLive() === 0, await status());
await ctl('/__refuse/0');
// ── 13. Repeated on/off: one call at most, nothing left behind. ──
await click('.voice-button'); await until(() => exists('.modal.open')); await click('.modal.open .btn-primary', '开始语音'); await sleep(60);
await click('.voice-row.real .voice-end'); await sleep(40);
await click('.voice-button'); await until(() => exists('.modal.open')); await click('.modal.open .btn-primary', '开始语音');
await until(async () => (await lk('__lk.rooms.at(-1)?.state')) === 'connected'); await sleep(600);
const sess = await ctl('/__sessions');
check('rapid off/on: exactly one open session and one live microphone', sess.open.length === 1 && await micsLive() === 1, JSON.stringify(sess));
await click('.voice-row.real .voice-end'); await cleanEnd('rapid toggle');
// ── 14. Leaving: nav, Esc, 结束交谈, page unload, network loss — each ends the call. ──
await startVoice(); await click('.nav-item', '此刻'); await cleanEnd('nav away');
await click('.nav-item', '交谈'); await sleep(1300); await startVoice();
await js(`document.body.dispatchEvent(new KeyboardEvent('keydown', { key: 'Escape', bubbles: true })), true`); await cleanEnd('Esc');
await click('.nav-item', '交谈'); await sleep(1300); await startVoice();
await click('.end-talk'); await cleanEnd('结束交谈');
await click('.nav-item', '交谈'); await sleep(1300); await startVoice();
await cdp('Network.emulateNetworkConditions', { offline: true, latency: 0, downloadThroughput: -1, uploadThroughput: -1 }); await sleep(500);
check('network lost: call ends with its own words', (await status()).includes('网络断开了'), await status());
const offMics = await micsLive(), offAudio = await herAudio(), offOpen = (await ctl('/__sessions')).open.length;
check('network lost: microphone and her audio stop at once; the server room is owed, not pretended deleted', offMics === 0 && offAudio === 0 && offOpen === 1 && (await js(`localStorage.getItem('cg.voice.owed')`) || '').includes('sess-'), `mics=${offMics} audio=${offAudio} open=${offOpen}`);
await cdp('Network.emulateNetworkConditions', { offline: false, latency: 0, downloadThroughput: -1, uploadThroughput: -1 });
await cleanEnd('offline, after the network is back');
check('…the owed DELETE is sent once back online and forgotten', !(await js(`localStorage.getItem('cg.voice.owed')`)));
await startVoice(); await calls();
await cdp('Page.navigate', { url: 'about:blank' }); await sleep(800);
log = await calls();
check('page unload: the server room is deleted (keepalive)', log.some(l => l.startsWith('DELETE /api/voice/session/')) && (await ctl('/__sessions')).open.length === 0, log.join(' | '));
// ── 15. Phone and the fog theme: the same line, the same states. ──
for (const [theme, v] of [['fog', 'desktop'], ['moon', 'phone'], ['fog', 'phone']]) {
await view(v); await boot(theme); await click('.nav-item', '交谈'); await sleep(1400);
await startVoice();
await lk(`__lk.say({ kind: 'interim', role: 'user', text: '今天下班的路上看到一只很胖的橘猫' })`); await sleep(250);
await shot(`15-${theme}-${v}-voice-listening`);
await lk(`__lk.state('speaking')`); await sleep(300);
await shot(`16-${theme}-${v}-voice-speaking`);
if (v === 'phone') check(`${theme}/${v}: voice line fits the screen`, await js(`(() => { const r = document.querySelector('.voice-row.real').getBoundingClientRect(); return r.left >= 0 && r.right <= innerWidth; })()`));
await click('.voice-row.real .voice-end'); await sleep(400);
}
await shot('17-fog-phone-voice-ended');
// ── 16. The real vendored client: same API names the page uses (no connection is made). ──
await view('desktop');
await cdp('Page.navigate', { url: `${base}/__nothing` }); await sleep(800);
const sdk = await js(`new Promise(ok => { delete window.LivekitClient; const s = document.createElement('script'); s.src = '/__real-sdk.js'; s.onload = () => { const L = window.LivekitClient, E = L.RoomEvent; const room = new L.Room({ adaptiveStream: true, dynacast: true }); ok({ events: ['TrackSubscribed','TrackUnsubscribed','ParticipantConnected','ParticipantAttributesChanged','DataReceived','Disconnected','AudioPlaybackStatusChanged'].every(k => typeof E[k] === 'string'), values: [E.ParticipantAttributesChanged, E.DataReceived, E.AudioPlaybackStatusChanged], mic: L.Track.Source.Microphone, agent: L.ParticipantKind && L.ParticipantKind.AGENT, create: typeof L.createLocalAudioTrack, methods: ['connect','disconnect','startAudio','on'].every(m => typeof room[m] === 'function'), playback: typeof room.canPlaybackAudio, remote: room.remoteParticipants instanceof Map, publish: typeof room.localParticipant.publishTrack, version: L.version }); }; s.onerror = () => ok(null); document.head.append(s); })`);
check('real livekit-client 2.22.4: every name the adapter uses exists', !!sdk && sdk.events && sdk.mic === 'microphone' && typeof sdk.agent === 'number' && sdk.create === 'function' && sdk.methods && sdk.playback === 'boolean' && sdk.remote && sdk.publish === 'function', JSON.stringify(sdk));
// ── 17. The demo entry stays separate: no voice route, no client. ──
await calls();
await cdp('Page.navigate', { url: `${base}/?demo=1` }); await sleep(2500);
check('demo entry makes no voice requests', !(await calls()).some(l => l.includes('/api/voice')));
ws.close(); browser.kill();
try { rmSync(profile, { recursive: true, force: true }); } catch { /* Edge may still hold the profile. */ }
const failed = results.filter(r => !r).length;
console.log(`${results.length - failed}/${results.length} passed`);
process.exit(failed ? 1 : 0);