// HEL-900 browser check: 她的声音 in settings — reference (file or recording) → fresh preview → apply. Drives headless // Edge over CDP against tests/hel900_mock.mjs; Chromium's fake audio device stands in for a microphone (a beep, not a // person), the preview is a generated tone. No voice panel, ASR, TTS or real clone is involved. // Usage: node tests/hel900_shots.mjs [port]. import { spawn } from 'node:child_process'; import { mkdirSync, writeFileSync } 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 || 8900), base = `http://127.0.0.1:${port}`, cdpPort = 9500; mkdirSync(out, { recursive: true }); const profile = join(tmpdir(), `hel900-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 id = 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 n = ++id; waiting.set(n, { ok, ko }); ws.send(JSON.stringify({ id: n, 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 && !ok ? ` — ${String(detail).slice(0, 300)}` : ''}`); }; const ctl = async path => (await fetch(`${base}${path}`)).json(); const shot = async name => { const { data } = await cdp('Page.captureScreenshot', { format: 'png', captureBeyondViewport: false }); 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.scrollIntoView({ block: 'center' }); el.click(); return true; })()`); const exists = sel => js(`(() => { const e = document.querySelector(${JSON.stringify(sel)}); return !!e && e.offsetParent !== null; })()`); const text = sel => js(`[...document.querySelectorAll(${JSON.stringify(sel)})].filter(e => e.offsetParent !== null).map(e => e.innerText).join('\\n')`); const disabled = (sel, label) => js(`(() => { const el = [...document.querySelectorAll(${JSON.stringify(sel)})].find(e => e.offsetParent !== null && e.textContent.includes(${JSON.stringify(label)})); return el ? el.disabled : null; })()`); const until = async (fn, ms = 6000) => { const end = Date.now() + ms; while (Date.now() < end) { if (await fn()) return true; await sleep(100); } return false; }; const fill = (sel, value) => js(`(() => { const el = [...document.querySelectorAll(${JSON.stringify(sel)})].find(e => e.offsetParent !== null); if (!el) return false; const proto = el.tagName === 'TEXTAREA' ? HTMLTextAreaElement.prototype : HTMLInputElement.prototype; Object.getOwnPropertyDescriptor(proto, 'value').set.call(el, ${JSON.stringify(value)}); el.dispatchEvent(new Event('input', { bubbles: true })); return true; })()`); const live = () => js('window.__live()'); const status = () => text('.voice-sec .status'); 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 }); } // Counts every microphone track the page ever got, so "released" means readyState 'ended' on all of them. const PROBE = `(() => { const md = navigator.mediaDevices; if (!md) return; const orig = md.getUserMedia.bind(md); window.__tracks = []; window.__gum = []; window.__deny = ''; md.getUserMedia = async c => { window.__gum.push(JSON.stringify(c)); if (window.__deny) throw new DOMException('denied', window.__deny); const s = await orig(c); s.getTracks().forEach(t => window.__tracks.push(t)); return s; }; window.__live = () => window.__tracks.filter(t => t.readyState === 'live').length; })();`; async function boot(theme) { await ctl('/__reset'); 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(1500); } async function openVoice() { await click('.nav-item', '设置'); await sleep(700); await click('.snav button', '她的声音'); return until(() => exists('.voice-sec') || exists('.pane-manage .off-card'), 6000); } async function record(seconds = 3.5) { await click('.voice-sec .seg button', '现在录一段'); await click('.voice-sec .rec button', '开始录音'); await until(async () => (await text('.voice-sec .rec')).includes('正在录音')); await sleep(seconds * 1000); } async function stopRecording() { await click('.voice-sec .rec button', '停止'); return until(() => exists('.voice-sec .take .pill'), 8000); } const wav = (seconds, rate = 22050, channels = 2) => { const n = Math.round(seconds * rate), b = Buffer.alloc(44 + n * 2 * channels); b.write('RIFF', 0); b.writeUInt32LE(36 + n * 2 * channels, 4); b.write('WAVE', 8); b.write('fmt ', 12); b.writeUInt32LE(16, 16); b.writeUInt16LE(1, 20); b.writeUInt16LE(channels, 22); b.writeUInt32LE(rate, 24); b.writeUInt32LE(rate * 2 * channels, 28); b.writeUInt16LE(2 * channels, 32); b.writeUInt16LE(16, 34); b.write('data', 36); b.writeUInt32LE(n * 2 * channels, 40); for (let i = 0; i < n * channels; i++) b.writeInt16LE(Math.round(Math.sin(i / 11) * 5000), 44 + i * 2); return b; }; const files = join(tmpdir(), `hel900-files-${Date.now()}`); mkdirSync(files, { recursive: true }); writeFileSync(join(files, 'reference-22k-stereo.wav'), wav(6)); writeFileSync(join(files, 'renamed-webm.wav'), Buffer.concat([Buffer.from([0x1a, 0x45, 0xdf, 0xa3]), Buffer.alloc(4000, 7)])); async function upload(name) { const { result } = await cdp('Runtime.evaluate', { expression: `[...document.querySelectorAll('.voice-sec input[type=file]')].find(e => e.offsetParent !== null)` }); await cdp('DOM.setFileInputFiles', { files: [join(files, name)], objectId: result.objectId }); } await cdp('Page.enable'); await cdp('Runtime.enable'); await cdp('Network.enable'); await cdp('DOM.enable'); await cdp('Page.addScriptToEvaluateOnNewDocument', { source: PROBE }); check('mock is this hel900 mock', (await ctl('/__pid')).mock === 'hel900'); // ── Main path, both themes × desktop/phone ── for (const [theme, v] of [['moon', 'desktop'], ['fog', 'desktop'], ['moon', 'phone'], ['fog', 'phone']]) { const tag = `${theme}-${v}`; await view(v); await boot(theme); check(`${tag}: no microphone before anything is tapped`, (await js('window.__gum.length')) === 0); await openVoice(); await until(() => exists('.voice-sec .voice-list')); const first = await ctl('/__log'); check(`${tag}: opening the section only reads the list`, first.every(l => !l.includes('POST')) && first.some(l => l.includes('GET /api/voice/voices')), first.join(' | ')); check(`${tag}: says when a change takes effect`, (await text('.voice-sec .kv')).includes('下一次通话')); await shot(`${tag}-01-section`); await record(3.2); check(`${tag}: recording state is explicit and the microphone is live`, (await live()) === 1 && (await text('.voice-sec .rec')).includes('麦克风开着')); check(`${tag}: audio only, never the camera`, (await js('window.__gum.at(-1)')).includes('"video":false')); await shot(`${tag}-02-recording`); await sleep(800); check(`${tag}: stopping keeps the take and releases the microphone`, await stopRecording() && (await live()) === 0); check(`${tag}: the take is labelled as your reference, not her voice`, (await text('.voice-sec .take h4')).includes('不是栖的声音')); check(`${tag}: register needs reference text or an explicit transcription opt-in`, (await disabled('.voice-sec .take button', '登记这段声音')) === true); await fill('.voice-sec .take input.input', '轻一点的她'); await fill('.voice-sec .take textarea', '今天天气很好,我们出去走走吧。'); await shot(`${tag}-03-take`); await click('.voice-sec .take button', '登记这段声音'); await until(async () => (await text('.voice-sec .voice-list')).includes('轻一点的她')); const up = (await ctl('/__uploads')).at(-1); check(`${tag}: the upload is real PCM WAV (16 kHz mono 16-bit), raw body + query`, up && up.type === 'audio/wav' && up.magic === 'RIFF' && up.wave === 'WAVE' && up.info?.rate === 16000 && up.info?.channels === 1 && up.info?.bits === 16 && up.info.seconds >= 3 && up.allow_asr === 'false' && up.prompt_text === '今天天气很好,我们出去走走吧。', JSON.stringify(up)); check(`${tag}: the new voice is chosen for a preview; apply waits for it`, (await text('.voice-sec .audition h4')).includes('轻一点的她') && (await disabled('.voice-sec .audition button', '给栖用这个声音')) === true); await shot(`${tag}-04-registered`); await click('.voice-sec .audition button', '生成试听'); check(`${tag}: a fresh preview plays and only then can be applied`, await until(() => exists('.voice-sec .sample.ready')) && (await disabled('.voice-sec .audition button', '给栖用这个声音')) === false); check(`${tag}: the preview is labelled as her synthesized voice, not your reference`, (await text('.voice-sec .sample figcaption')).includes('不是参考原声')); check(`${tag}: the preview actually plays in the page`, await until(() => js(`(() => { const a = document.querySelector('.voice-sec .sample audio'); return !!a && (a.currentTime > 0 || !a.paused); })()`))); await sleep(600); await shot(`${tag}-05-preview`); await click('.voice-sec .audition button', '给栖用这个声音'); await until(async () => (await text('.voice-sec .kv')).includes('轻一点的她')); const st = await ctl('/__state'); check(`${tag}: applied — current voice shown, from the next call`, st.selection.voice?.startsWith('cg-') && (await text('.voice-sec .audition')).includes('她现在就用这个') && (await status()).includes('已经接通的通话不会中途换声'), await status()); await shot(`${tag}-06-applied`); check(`${tag}: no object URL or microphone left after closing settings`, await (async () => { await click('.pane-manage button[aria-label="关闭设置"]'); await sleep(400); return (await live()) === 0 && !(await exists('.voice-sec')); })()); } // ── Edge cases (desktop, moon; the phone fog run repeats the recording ones) ── await view('desktop'); await boot('moon'); await openVoice(); await until(() => exists('.voice-sec .voice-list')); // File path: a real WAV from disk (22 kHz stereo) is re-encoded; a WebM renamed .wav is refused before upload. await upload('renamed-webm.wav'); check('file: a renamed non-WAV is refused in the browser, nothing uploaded', await until(async () => (await status()).includes('解不开')) && (await ctl('/__uploads')).length === 0, await status()); await upload('reference-22k-stereo.wav'); check('file: a 22 kHz stereo WAV becomes 16 kHz mono', await until(() => exists('.voice-sec .take .pill')) && (await text('.voice-sec .take')).includes('16kHz 单声道')); await shot('edge-01-file-take'); // Explicit transcription opt-in. await click('.voice-sec .take .switch-row input'); check('asr: opting in enables register with empty text', (await disabled('.voice-sec .take button', '登记这段声音')) === false); // Offline: nothing is sent; the take and the form stay. await cdp('Network.emulateNetworkConditions', { offline: true, latency: 0, downloadThroughput: -1, uploadThroughput: -1 }); await click('.voice-sec .take button', '登记这段声音'); check('offline: says so, keeps the take, sends nothing', await until(async () => (await status()).includes('网络断开')) && (await exists('.voice-sec .take .pill')) && (await ctl('/__uploads')).length === 0, await status()); await shot('edge-02-offline'); await cdp('Network.emulateNetworkConditions', { offline: false, latency: 0, downloadThroughput: -1, uploadThroughput: -1 }); // Server down once (503): message, take kept; the retry succeeds and carries allow_asr=true. await ctl('/__fail?register=503'); await click('.voice-sec .take button', '登记这段声音'); check('503: honest message, take still there for a retry', await until(async () => (await status()).includes('音色登记服务')) && await exists('.voice-sec .take .pill'), await status()); await shot('edge-03-register-503'); await js(`(() => { const b = [...document.querySelectorAll('.voice-sec .take button')].find(e => e.textContent.includes('登记这段声音')); b.click(); b.click(); return true; })()`); await until(async () => (await text('.voice-sec .voice-list')).includes('cg-')); const ups = await ctl('/__uploads'); check('double tap registers once; transcription only on explicit opt-in', ups.length === 2 && ups.at(-1).allow_asr === 'true' && ups.at(-1).prompt_text === '', JSON.stringify(ups.map(u => u.allow_asr))); check('asr: the transcribed text is shown back', (await status()).includes('服务器转写'), await status()); // Late preview: A is slow, you switch to B; A's sample never shows up as B's (nor at all). await ctl('/__fail?preview=slow'); await click('.voice-sec .audition button', '生成试听'); await sleep(300); await click('.voice-sec details.voice-builtin summary'); await sleep(200); await click('.voice-sec .voice-builtin button', 'rain'); await sleep(3000); check('late: an older preview arriving after switching is dropped', (await text('.voice-sec .audition h4')).includes('rain') && !(await exists('.voice-sec .sample')) && (await disabled('.voice-sec .audition button', '给栖用这个声音')) === true); await shot('edge-04-late-dropped'); // A preview that is not audio is refused, never played. await ctl('/__fail?preview=bad'); await click('.voice-sec .audition button', '生成试听'); check('bad preview: refused, nothing playable shown', await until(async () => (await status()).includes('不是可播放')) && !(await exists('.voice-sec .sample .pill')), await status()); // TTS down. await ctl('/__fail?preview=503'); await click('.voice-sec .audition button', '生成试听'); check('preview 503: honest message', await until(async () => (await status()).includes('声音服务这会儿不在')), await status()); // 409 from the server even after a preview here (another device, a restart): said plainly, nothing changes. await click('.voice-sec .audition button', '生成试听'); await until(() => exists('.voice-sec .sample.ready')); await ctl('/__fail?apply=409'); await click('.voice-sec .audition button', '给栖用这个声音'); check('apply 409: message, selection unchanged', await until(async () => (await status()).includes('还没有试听过')) && (await ctl('/__state')).selection.voice === null, await status()); await shot('edge-05-apply-409'); // Recording: cancel, leave mid-take, denied permission. await record(1.2); await click('.voice-sec .rec button', '取消'); check('cancel: microphone released, take discarded', (await live()) === 0 && (await status()).includes('已取消')); await record(1.2); await click('.snav button', '城市'); await sleep(300); check('leaving the section mid-take releases the microphone', (await live()) === 0); await click('.snav button', '她的声音'); await sleep(300); check('coming back says the take was dropped', (await status()).includes('离开了这里'), await status()); await js(`window.__deny = 'NotAllowedError'; true`); await click('.voice-sec .rec button', '开始录音'); check('denied: says why, nothing recording', await until(async () => (await status()).includes('没有拿到麦克风权限')) && (await live()) === 0, await status()); await js(`window.__deny = ''; true`); await shot('edge-06-denied'); // Unsaved take: closing settings asks first. await record(2.6); await stopRecording(); await click('.pane-manage button[aria-label="关闭设置"]'); check('an unregistered take guards leaving', await until(() => exists('.modal.open')) && (await text('.modal.open')).includes('未保存')); await shot('edge-07-leave-guard'); await click('.modal.open button', '放弃修改并离开'); await sleep(400); check('after leaving, nothing of the section remains', (await live()) === 0 && !(await exists('.voice-sec'))); // Server states for the list. for (const [mode, words, name] of [['off', '还没有在这台服务上配置', 'unconfigured'], ['none', '还没有自定义声音的通路', 'no-route'], ['down', '一时没读出来', 'panel-down']]) { await ctl(`/__mode?list=${mode}`); await cdp('Page.navigate', { url: `${base}/` }); await sleep(1300); await openVoice(); check(`list ${name}: honest card, no microphone, no 'adjustable' tag`, (await text('.pane-manage .sec-body')).includes(words) && (await js('window.__gum.length')) === 0 && !(await text('.pane-manage .sec-h')).includes('可以调整')); await shot(`edge-08-${name}`); } await ctl('/__mode?list=ok'); await click('.pane-manage .off-card button', '再试一次'); check('panel back: retry shows the list', await until(() => exists('.voice-sec .voice-list'))); for (const theme of ['moon', 'fog']) { await view('phone'); await boot(theme); await openVoice(); await until(() => exists('.voice-sec .voice-list')); await ctl('/__mode?list=down'); await cdp('Page.navigate', { url: `${base}/` }); await sleep(1300); await openVoice(); await shot(`phone-${theme}-panel-down`); await ctl('/__mode?list=ok'); } const gave = await ctl('/__gaveup'); check('no registration or apply was ever cut off by the page itself', !gave.some(l => l.startsWith('POST /api/voice/voices') || l.startsWith('POST /api/voice/selection')), gave.join(' | ')); const passed = results.filter(Boolean).length; console.log(`\n${passed}/${results.length} passed`); browser.kill(); process.exit(passed === results.length ? 0 : 1);