// 전체 스택 시연·통합 테스트 (M3·M4·M7): // 실제 코어(+STT 워커) ← 경기 데이터(발로란트 22라운드·롤 28분, 실제 API 구조) + TTS 로 만든 대화 음성 // → 로컬 사이트 서버(실제 코드) ← 코어가 중계로 연결 → 헤드리스 브라우저로 화면 확인·스크린샷 // // node --import tsx scripts/demo-stack.mts [--serve] [--model small] import { execFileSync } from 'node:child_process'; import fs from 'node:fs'; import os from 'node:os'; import path from 'node:path'; import { synthesize } from '../src/core/discord/tts.ts'; import { processMatchDetails } from '../src/core/games/valorant.ts'; import { makeDemoLol, makeDemoValorant } from '../test/fixtures/demo-matches.ts'; const args = process.argv.slice(2); const serve = args.includes('--serve'); const model = args.includes('--model') ? args[args.indexOf('--model') + 1] : 'small'; const root = path.resolve(import.meta.dirname, '..', '..'); const work = path.join(os.homedir(), '.tmp', 'gvr-demo'); const shotDir = process.env.SHOT_DIR || path.join(root, 'docs', 'dev', 'screens'); fs.rmSync(work, { recursive: true, force: true }); fs.mkdirSync(path.join(work, 'models'), { recursive: true }); const hf = path.join(os.homedir(), `.cache/huggingface/hub/models--Systran--faster-whisper-${model}/snapshots`); fs.symlinkSync(path.join(hf, fs.readdirSync(hf)[0]), path.join(work, 'models', model)); fs.writeFileSync(path.join(work, 'settings.json'), JSON.stringify({ stt: { realtimeModel: model, preciseModel: model, device: 'cpu', computeType: 'int8' }, games: { valorant: false, lol: false } })); const USERS = [ { id: '100000000000000001', name: '승한', voice: 'ko-KR-InJoonNeural', val: 'p-sh', lol: 'l-sh' }, { id: '100000000000000002', name: '민수', voice: 'ko-KR-HyunsuMultilingualNeural', val: 'p-ms', lol: 'l-ms' }, { id: '100000000000000003', name: '지우', voice: 'ko-KR-SunHiNeural', val: 'p-jw', lol: 'l-jw' }, { id: '100000000000000004', name: '현우', voice: 'ko-KR-InJoonNeural', pitch: '-8Hz', val: 'p-hw', lol: 'l-hw' }, ]; type Line = { u: number; text: string; at: { val: [number, number] } | { lol: number } | { off: number } }; const LINES: Line[] = [ { u: 0, text: '오늘 랭겜 몇 판 할까', at: { off: -200 } }, { u: 2, text: '나 과제 끝내고 왔어 두 판만 하자', at: { off: -190 } }, { u: 1, text: '피스톨 라운드 A 메인 러시 가자', at: { val: [1, 2] } }, { u: 0, text: '제트 대시로 먼저 들어갈게', at: { val: [1, 12] } }, { u: 2, text: '세이지 벽 칠게 미드 막을게', at: { val: [1, 20] } }, { u: 3, text: '스모크 깔았어 들어가', at: { val: [2, 15] } }, { u: 1, text: '소바 화살 B 메인에 두 명 보여', at: { val: [3, 18] } }, { u: 0, text: '레이즈 궁 빠졌어 B 사이트 로테 하자', at: { val: [5, 30] } }, { u: 2, text: '스파이크 설치됐어 해체 들어가', at: { val: [7, 55] } }, { u: 3, text: '내가 스모크 깔게 들어가 들어가', at: { val: [7, 38] } }, { u: 1, text: '나이스 원탭', at: { val: [7, 41] } }, { u: 0, text: '이번 라운드 세이브하자 돈 없어', at: { val: [9, 3] } }, { u: 2, text: '나 오퍼레이터 살게', at: { val: [11, 2] } }, { u: 3, text: '클러치 가능 침착하게 해', at: { val: [13, 60] } }, { u: 0, text: '에이스 미쳤다 레전드', at: { val: [16, 50] } }, { u: 1, text: '마지막 라운드 집중하자', at: { val: [22, 5] } }, { u: 2, text: '이겼다 지지', at: { val: [22, 95] } }, { u: 0, text: '롤 한 판 하자 나 미드 갈게', at: { lol: -60 } }, { u: 1, text: '정글 탑 갱 갈게 라인 당겨줘', at: { lol: 4 } }, { u: 3, text: '가렌 점멸 빠졌어', at: { lol: 7 } }, { u: 2, text: '바텀 와드 박았어 쓰레쉬 어디 있어', at: { lol: 9.5 } }, { u: 1, text: '드래곤 가자 바텀으로 모여', at: { lol: 11.6 } }, { u: 0, text: '아리 궁 있어 한타 열자', at: { lol: 15 } }, { u: 3, text: '바론 치자 시야 잡아줘', at: { lol: 21.4 } }, { u: 2, text: '억제기 밀고 귀환하자', at: { lol: 26.5 } }, { u: 0, text: '배고프다 치킨 시킬까', at: { off: -25 } }, { u: 3, text: '내일 팀플 회의 몇 시였지', at: { off: -15 } }, { u: 1, text: '중간고사 끝나면 피시방 가자', at: { off: -8 } }, ]; // ---- 대화 음성 (Edge TTS → Ogg Opus 48kHz). 캐시해서 다시 쓴다 const clipDir = path.join(os.homedir(), '.tmp', 'gvr-demo-clips'); fs.mkdirSync(clipDir, { recursive: true }); for (const [i, l] of LINES.entries()) { const ogg = path.join(clipDir, `${i}.ogg`); if (fs.existsSync(ogg)) continue; const webm = path.join(clipDir, `${i}.webm`); const u = USERS[l.u]; await synthesize(l.text, u.voice, webm); execFileSync('ffmpeg', ['-v', 'error', '-y', '-i', webm, ...(u.pitch ? ['-af', 'asetrate=48000*0.92,aresample=48000,atempo=1.087'] : []), '-ac', '2', '-ar', '48000', '-c:a', 'libopus', '-b:a', '32k', ogg]); await new Promise((r) => setTimeout(r, 400)); } const durMs = (f: string) => Math.round(Number(execFileSync('ffprobe', ['-v', 'error', '-show_entries', 'format=duration', '-of', 'csv=p=0', f]).toString()) * 1000); // ---- 코어 const { startCore } = await import('../src/core/index.ts'); const core = await startCore({ dataDir: work, resourcesDir: path.join(root, 'app', 'resources'), worker: { cmd: path.join(root, 'worker/.venv/bin/python'), args: ['-m', 'gvr_worker'], cwd: path.join(root, 'worker') }, version: '1.0.0' }); const { store, games } = core; await core.assets.refresh(true); for (const u of USERS) { store.upsertUser({ id: u.id, name: u.name, avatar: null }); store.setAccounts(u.id, [ { puuid: u.val, riotId: `${u.name}#V` }, { puuid: u.lol, riotId: `${u.name}#L` }, ]); } store.upsertUser({ id: '100000000000000005', name: '도윤', avatar: null }); store.setExcluded('100000000000000005', true); const now = Date.now(); const VS = now - 3 * 3600_000; // 발로란트 시작 const LS = now - 80 * 60_000; // 롤 시작 const md = makeDemoValorant(VS); const pre = processMatchDetails(md, 'p-sh', core.assets, 'x', VS); const lol = makeDemoLol(); const audioDir = path.join(core.paths().audio, 'demo'); fs.mkdirSync(audioDir, { recursive: true }); function addUtterance(i: number, ts: number) { const src = path.join(clipDir, `${i}.ogg`); const id = `demo${String(i).padStart(3, '0')}`; const dst = path.join(audioDir, `${id}.ogg`); fs.copyFileSync(src, dst); const row = store.insertUtterance({ id, session_id: null, user_id: USERS[LINES[i].u].id, started_at: ts, ended_at: ts + durMs(src), audio_path: dst, match_id: games.liveMatchAt(ts) }); core.stt.enqueue(row.id, 'realtime'); } // 발로란트 const valId = 'val-demo-ascent-01'; games.matchStarted({ id: valId, game: 'valorant', startedAt: VS, map: md.matchInfo.mapId, mode: 'competitive', queue: 'competitive', detail: { live: true } }); LINES.forEach((l, i) => { if (!('val' in l.at)) return; const [r, s] = l.at.val; const rr = pre.rounds[r - 1]; addUtterance(i, rr.combat_at + s * 1000); }); for (const [t, who, ch, text] of [ [3, 'p-ms', 'team', '미드 조심 오퍼 있음'], [10, 'e-lm', 'all', 'gg ez'], [16, 'p-dk', 'team', '나 돈 없음 드랍 좀'], ] as const) { const r = pre.rounds[t - 1]; games.event({ id: `${valId}:chat:demo${t}`, match_id: valId, game: 'valorant', type: 'chat', ts: r.combat_at + 25000, game_time_ms: null, round: null, actor_puuid: who, target_puuid: null, data: JSON.stringify({ sender: { puuid: who, name: md.players.find((p: any) => p.subject === who)!.gameName }, channel: ch, text, source: 'api' }) }); } games.matchEnded(valId, VS + md.matchInfo.gameLengthMillis); (games.adapters[0] as any).applyDetails(valId, md, 'p-sh', VS); // 롤 const lolId = 'lol-7700123'; games.matchStarted({ id: lolId, game: 'lol', startedAt: LS, map: 'Map11', mode: 'CLASSIC', queue: '420', detail: { live: true } }); LINES.forEach((l, i) => { if ('lol' in l.at) addUtterance(i, LS + l.at.lol * 60_000); }); for (const [m, name, ch, text, src] of [ [-1.5, '민수', 'team', '정글 갱 갈게요', 'lcu'], [5.2, 'Lumi', 'all', '아리 잘하네', 'ocr'], [11.4, '지우', 'team', '드래곤 가자', 'ocr'], [28.5, 'Kiwi', 'all', 'gg', 'lcu'], ] as const) { games.event({ id: `${lolId}:chat:demo${m}`, match_id: lolId, game: 'lol', type: 'chat', ts: LS + m * 60_000, game_time_ms: m * 60_000, round: null, actor_puuid: null, target_puuid: null, data: JSON.stringify({ sender: { name }, channel: ch, text, source: src }) }); } games.matchEnded(lolId, LS + lol.durationMs); store.setMeta('riot_self_puuid', 'l-sh'); (games.adapters[1] as any).applyHistory(lolId, lol.game, lol.timeline, LS); // 경기 밖 LINES.forEach((l, i) => { if ('off' in l.at) addUtterance(i, now + l.at.off * 60_000); }); // STT 끝날 때까지 const total = LINES.length; const t0 = Date.now(); for (;;) { const done = (store.db.prepare(`SELECT count(*) n FROM utterances WHERE status!='pending'`).get() as any).n; process.stdout.write(`\rSTT ${done}/${total}`); if (done >= total) break; if (Date.now() - t0 > 15 * 60_000) throw new Error('STT timeout'); await new Promise((r) => setTimeout(r, 1000)); } console.log(`\nSTT 완료 (${((Date.now() - t0) / 1000).toFixed(0)}초)`); let exact = 0; for (const [i, l] of LINES.entries()) { const r = store.getUtterance(`demo${String(i).padStart(3, '0')}`)!; const ok = (r.text || '').replace(/\s/g, '') === l.text.replace(/\s/g, ''); if (ok) exact++; console.log(`${ok ? '=' : '≠'} ${l.text} → ${r.text}`); } console.log(`완전 일치 ${exact}/${total}`); // ---- 로컬 사이트 서버 + 중계 const { buildServer } = await import('../../site/server/src/app.ts'); const site = await buildServer({ port: 0, host: '127.0.0.1', publicUrl: 'http://127.0.0.1', clientId: 'x', clientSecret: 'x', botToken: 'x', allowedGuilds: [], owners: [USERS[0].id], sessionSecret: 'demo-secret-'.repeat(3), dataDir: path.join(work, 'site'), webDir: path.join(root, 'site', 'web', 'dist'), version: '1.0.0', discordApi: 'http://127.0.0.1:1', assetUpstream: { valmedia: 'https://media.valorant-api.com/', ddragon: 'https://ddragon.leagueoflegends.com/', cdragon: 'https://raw.communitydragon.org/' }, }); site.app.log.level = 'warn'; await site.app.listen({ host: '127.0.0.1', port: Number(process.env.DEMO_PORT || 0) }); const base = `http://127.0.0.1:${(site.app.server.address() as any).port}`; const { registerDevice } = await import('../src/core/relay/client.ts'); const code = site.store.createCode('demo'); const reg = await registerDevice(base, code.code, 'GAME-PC'); core.settings.update({ site: { serverUrl: base, deviceId: reg.deviceId, deviceName: 'GAME-PC' } }); core.settings.setSecret('deviceKey', reg.deviceKey); core.relay.restart(); const sess = site.store.createSession({ id: USERS[0].id, name: '승한', avatar: null }); const cookie = { name: 'gvr_sid', value: site.app.signCookie(sess.id), url: base }; while (core.relay.status.state !== 'online') await new Promise((r) => setTimeout(r, 100)); console.log(`사이트: ${base} (쿠키 gvr_sid=${cookie.value})`); // ---- 화면 확인 const puppeteer = (await import(path.join(os.homedir(), '.tmp/gvr-shot/node_modules/puppeteer-core/lib/esm/puppeteer/puppeteer-core.js'))).default; const browser = await puppeteer.launch({ executablePath: '/usr/bin/google-chrome', headless: true, args: ['--no-sandbox', '--autoplay-policy=no-user-gesture-required', '--lang=ko-KR'] }); const page = await browser.newPage(); await page.setCookie(cookie); const errs: string[] = []; page.on('pageerror', (e: any) => errs.push(e.message)); page.on('console', (m: any) => m.type() === 'error' && errs.push(m.text())); fs.mkdirSync(shotDir, { recursive: true }); const shot = async (url: string, name: string, w = 1440, h = 900, after?: () => Promise) => { await page.setViewport({ width: w, height: h }); await page.goto(base + url, { waitUntil: 'networkidle2' }); await new Promise((r) => setTimeout(r, 1800)); if (after) await after(); await page.screenshot({ path: path.join(shotDir, name) }); console.log('screenshot', name); }; await shot('/', 'real-timeline.png'); await shot('/', 'real-timeline-playing.png', 1440, 900, async () => { await page.evaluate(() => { const rows = [...document.querySelectorAll('.it.utt')] as HTMLElement[]; const target = rows.find((r) => r.textContent?.includes('치킨')) || rows[0]; target?.click(); }); await new Promise((r) => setTimeout(r, 900)); }); await shot(`/matches/${valId}`, 'real-match-valorant.png', 1440, 900, async () => { await page.evaluate(() => { const b = [...document.querySelectorAll('button, .rnd, [data-round]')].find((x) => x.textContent?.trim() === '7') as HTMLElement | undefined; b?.click(); }); await new Promise((r) => setTimeout(r, 800)); }); await shot(`/matches/${lolId}`, 'real-match-lol.png'); await shot(`/users/${USERS[0].id}`, 'real-user.png'); await shot('/dictionary', 'real-dictionary.png'); await shot('/', 'real-mobile.png', 390, 844); // 연결 끊김 → 잠금, 다시 연결 → 해제 core.relay.stop(); await new Promise((r) => setTimeout(r, 2500)); await page.setViewport({ width: 1440, height: 900 }); await page.screenshot({ path: path.join(shotDir, 'real-locked.png') }); const lockedVisible = await page.evaluate(() => !!document.querySelector('.lock')); core.relay.start(); await new Promise((r) => setTimeout(r, 4000)); const unlocked = await page.evaluate(() => !document.querySelector('.lock')); console.log(`잠금 표시 ${lockedVisible}, 재연결 후 해제 ${unlocked}`); console.log('브라우저 오류:', errs.filter((e) => !/favicon|401/.test(e))); await browser.close(); if (!serve) { await core.stop(); await site.app.close(); process.exit(lockedVisible && unlocked ? 0 : 1); } console.log('--serve: Ctrl+C 로 종료');