// 실제 STT 워커(CPU, tiny)와 코어를 붙여 "발화 저장 → 대기열 → STT → DB → 실시간 이벤트 → API" 를 확인한다. // 워커 venv(worker/.venv)와 HF 캐시의 tiny 모델이 있어야 한다. 없으면 건너뛴다. import { test } from 'node:test'; import assert from 'node:assert/strict'; import fs from 'node:fs'; import os from 'node:os'; import path from 'node:path'; const root = path.resolve(import.meta.dirname, '..', '..'); const py = path.join(root, 'worker', '.venv', 'bin', 'python'); const hfTiny = path.join(os.homedir(), '.cache/huggingface/hub/models--Systran--faster-whisper-tiny/snapshots'); const skip = !fs.existsSync(py) || !fs.existsSync(hfTiny); test('E2E: 발화 → STT 워커 → DB → 실시간 이벤트 → 기록 API', { skip, timeout: 180000 }, async () => { const { startCore } = await import('../src/core/index.ts'); const dataDir = fs.mkdtempSync(path.join(process.env.TMPDIR || os.tmpdir(), 'gvr-e2e-')); const models = path.join(dataDir, 'models'); fs.mkdirSync(models, { recursive: true }); fs.symlinkSync(path.join(hfTiny, fs.readdirSync(hfTiny)[0]), path.join(models, 'tiny')); fs.writeFileSync( path.join(dataDir, 'settings.json'), JSON.stringify({ stt: { realtimeModel: 'tiny', preciseModel: 'tiny', device: 'cpu', computeType: 'int8' }, games: { valorant: false, lol: false } }), ); const core = await startCore({ dataDir, resourcesDir: path.join(root, 'app', 'resources'), worker: { cmd: py, args: ['-m', 'gvr_worker'], cwd: path.join(root, 'worker') }, version: 'test', }); const live: any[] = []; core.bus.on('live', (e) => live.push(e)); try { const wait = async (fn: () => boolean, ms: number, label: string) => { const t = Date.now(); while (!fn()) { if (Date.now() - t > ms) throw new Error(`timeout ${label}: ${JSON.stringify(core.stt.status)}`); await new Promise((r) => setTimeout(r, 100)); } }; await wait(() => core.stt.status.slots.realtime.state === 'loaded', 60000, 'model load'); core.store.upsertUser({ id: 'u1', name: '승한', avatar: null }); const fx = path.join(root, 'worker', 'tests', 'fixtures'); const items: [string, string][] = [ ['speech', 'speech_ko.ogg'], ['quiet', 'speech_ko_quiet.ogg'], ['keys', 'keyboard_clicks.ogg'], ]; const t0 = Date.now(); for (const [id, f] of items) { const dst = path.join(core.paths().audio, 'test', `${id}.ogg`); fs.mkdirSync(path.dirname(dst), { recursive: true }); fs.copyFileSync(path.join(fx, f), dst); core.store.insertUtterance({ id, session_id: null, user_id: 'u1', started_at: t0, ended_at: t0 + 3000, audio_path: dst, match_id: null }); core.stt.enqueue(id, 'realtime'); } await wait(() => items.every(([id]) => core.store.getUtterance(id)!.status !== 'pending'), 120000, 'transcribe'); const sp = core.store.getUtterance('speech')!; console.log('speech →', sp.text, '| raw:', sp.raw_text, '| gain', sp.gain_db); assert.ok(['realtime', 'precise'].includes(sp.status), sp.status); assert.ok(sp.text && sp.text.includes('레이즈'), sp.text ?? ''); assert.ok(JSON.parse(sp.words).length >= 3); const q = core.store.getUtterance('quiet')!; assert.ok(q.gain_db > 15, `quiet gain ${q.gain_db}`); const k = core.store.getUtterance('keys')!; assert.equal(k.noise, 1); assert.ok(live.filter((e) => e.type === 'utterance.updated').length >= 3); // 기록 API (사이트가 중계로 부르는 것과 같은 경로) const res = await core.api.inject({ method: 'GET', url: '/api/timeline?limit=10' }); const page = res.json(); assert.equal(page.items.length, 3); const audio = await core.api.inject({ method: 'GET', url: '/api/audio/speech', headers: { range: 'bytes=0-99' } }); assert.equal(audio.statusCode, 206); assert.equal(audio.rawPayload.length, 100); // 사이트 교정 → 사전·학습 데이터 const patched = await core.api.inject({ method: 'PATCH', url: '/api/utterances/speech', payload: { text: '레이즈 궁 빠졌어 B 사이트 로테 하자' } }); assert.equal(patched.json().corrected, true); assert.equal(core.store.trainingSamples().length, 1); assert.ok(fs.existsSync(core.store.trainingSamples()[0].audio_path)); const search = (await core.api.inject({ method: 'GET', url: '/api/timeline?q=' + encodeURIComponent('궁 빠졌어') })).json(); assert.deepEqual(search.items.map((i: any) => i.id), ['speech']); } finally { await core.stop(); } });