// M1 실제 음성 E2E: 테스트봇이 음성채널에 들어가 한국어 문장을 말하면, 겜녹이(코어)가 따라 들어와 녹음 → STT → DB 저장까지. // node --import tsx scripts/voice-e2e.mts // 필요: secrets.json 의 겜녹이·테스트봇 토큰, worker/.venv, tiny 모델(HF 캐시) import { Client, GatewayIntentBits } from 'discord.js'; import { joinVoiceChannel, entersState, VoiceConnectionStatus, createAudioPlayer, createAudioResource, StreamType, AudioPlayerStatus } from '@discordjs/voice'; import fs from 'node:fs'; import os from 'node:os'; import path from 'node:path'; import { synthesize } from '../src/core/discord/tts.ts'; const [guildId, channelId] = process.argv.slice(2); const secrets = JSON.parse(fs.readFileSync('/home/claude/.config/ejclaw/secrets.json', 'utf8')).credentials; const GVR_TOKEN = secrets['discord.com/bot/game-voice-recorder'].token; const SPEAKER_TOKEN = secrets['discord.com/bot/testbot'].token; const SPEAKER_ID = secrets['discord.com/bot/testbot'].client_id || '1538122882528321536'; const root = path.resolve(import.meta.dirname, '..', '..'); const work = fs.mkdtempSync(path.join(process.env.TMPDIR || os.tmpdir(), 'gvr-voice-e2e-')); const models = path.join(work, 'models'); fs.mkdirSync(models, { recursive: true }); const hf = path.join(os.homedir(), '.cache/huggingface/hub/models--Systran--faster-whisper-small/snapshots'); const model = fs.existsSync(hf) ? 'small' : 'tiny'; fs.symlinkSync(path.join(hf.replace('small', model), fs.readdirSync(hf.replace('small', model))[0]), path.join(models, model)); fs.writeFileSync( path.join(work, 'settings.json'), JSON.stringify({ discord: { guildId, followUserId: SPEAKER_ID, followEnabled: true, ttsEnabled: true, ttsOnNewMember: false }, stt: { realtimeModel: model, preciseModel: model, device: 'cpu', computeType: 'int8' }, games: { valorant: false, lol: false }, }), ); process.env.GVR_RECORD_BOTS = '1'; process.env.GVR_BOT_TOKEN = GVR_TOKEN; const SENTENCES = ['레이즈 궁 빠졌어 B 사이트 로테 하자', '오늘 저녁에 치킨 먹을래', '나 오퍼레이터 살게']; const clips: string[] = []; const clipDir = path.join(os.homedir(), '.tmp', 'gvr-e2e-clips'); for (const [i, s] of SENTENCES.entries()) { const f = path.join(clipDir, `say-${i}.webm`); if (!fs.existsSync(f)) await synthesize(s, i % 2 ? 'ko-KR-InJoonNeural' : 'ko-KR-SunHiNeural', f); clips.push(f); } 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: 'e2e' }); 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); await new Promise((r) => setTimeout(r, 200)); } }; let ok = false; const spk = new Client({ intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildVoiceStates] }); try { await wait(() => core.bot.state.status === 'ready', 30000, 'gvr bot ready'); await wait(() => core.stt.status.slots.realtime.state === 'loaded', 120000, 'model'); await spk.login(SPEAKER_TOKEN); await new Promise((r) => spk.once('clientReady', r)); const ch: any = await spk.channels.fetch(channelId); const conn = joinVoiceChannel({ channelId, guildId, adapterCreator: ch.guild.voiceAdapterCreator, selfDeaf: false, selfMute: false, daveEncryption: true }); await entersState(conn, VoiceConnectionStatus.Ready, 20000); console.log('speaker joined', ch.name); await wait(() => core.bot.state.recording.active, 20000, 'gvr follows speaker'); console.log('겜녹이 따라 들어옴 (follow):', core.bot.state.recording.channel); await new Promise((r) => setTimeout(r, 9000)); // 안내 음성이 끝나고 DAVE 키 교환이 안정될 때까지 const player = createAudioPlayer(); conn.subscribe(player); for (const f of clips) { player.play(createAudioResource(fs.createReadStream(f), { inputType: StreamType.WebmOpus })); await entersState(player, AudioPlayerStatus.Playing, 5000); await entersState(player, AudioPlayerStatus.Idle, 30000); await new Promise((r) => setTimeout(r, 1500)); } await wait(() => core.store.db.prepare(`SELECT count(*) n FROM utterances WHERE status!='pending'`).get().n >= SENTENCES.length, 90000, 'stt'); const rows = core.store.db.prepare('SELECT user_id, started_at, ended_at, text, gain_db, status FROM utterances ORDER BY started_at').all() as any[]; for (const r of rows) console.log(` [${r.user_id === SPEAKER_ID ? '테스트봇' : r.user_id}] ${((r.ended_at - r.started_at) / 1000).toFixed(1)}s gain ${r.gain_db?.toFixed(1)}dB ${r.status}: ${r.text}`); // 녹음 제외 확인 core.store.setExcluded(SPEAKER_ID, true); const before = rows.length; player.play(createAudioResource(fs.createReadStream(clips[0]), { inputType: StreamType.WebmOpus })); await entersState(player, AudioPlayerStatus.Idle, 30000).catch(() => undefined); await new Promise((r) => setTimeout(r, 2500)); const after = (core.store.db.prepare('SELECT count(*) n FROM utterances').get() as any).n; console.log(`녹음 제외 후 새 발화: ${after - before}개 (0 이어야 함)`); const texts = rows.map((r) => r.text || '').join(' '); ok = rows.length >= SENTENCES.length && /레이즈|오퍼|치킨/.test(texts) && after === before; conn.destroy(); await wait(() => !core.bot.state.recording.active, 20000, 'gvr leaves after speaker').catch(() => console.log('(겜녹이 퇴장 대기 시간 초과)')); console.log('겜녹이 퇴장:', !core.bot.state.recording.active); } catch (e) { console.error('E2E 실패', e); } finally { await spk.destroy(); await core.stop(); console.log(ok ? 'VOICE E2E PASS' : 'VOICE E2E FAIL'); process.exit(ok ? 0 : 1); }