Files
game_voice_recorder/app/test/bot-token.test.ts
EJClaw 6a03c67b54 앱 v1.2.1: 겜녹이 봇 토큰을 앱에 보내지 않고 사이트 서버가 봇을 대신 연결
- 사이트 서버: discord.js 로 디스코드 게이트웨이에 한 번 붙고(discord-source.ts), 로그인한 앱에 WS /app/bot/ws 로 중계(bot-broker.ts)
  · 앱이 보낸 op 4 는 지켜보는 서버의 음성채널 참여 · 나가기만 허용하고 필드를 다시 만들어 보낸다
  · 디스코드가 준 일회용 음성 접속 정보(VOICE_STATE/SERVER_UPDATE)는 음성을 가진 앱에만 넘긴다. 음성은 앱 ↔ 디스코드로 바로
  · 한 서버의 봇 음성은 마지막으로 요청한 앱이 가져가고 이전 앱은 taken. 이전 앱의 늦은 나가기 무시, 음성 가진 앱이 끊기면 봇을 뺀다
  · 슬래시 명령은 서버가 받아 defer 후 그 서버를 지켜보는 앱으로 보낸다
  · 등록 해제 · 로그아웃하면 그 유저의 중계 연결을 바로 끊는다 (4401), 30초마다 다시 확인
- /app/me 에서 botToken 제거. 앱은 v1.2.0 이 저장해 둔 봇 토큰을 시작할 때 지운다
- 앱: BotGateway 로 디스코드 연결을 분리 (RemoteGateway = 겜녹이 봇 서버 중계, LocalGateway = 다른 봇 토큰 직접 연결)
  Bot 은 discord.js 객체 대신 이 인터페이스만 쓴다. 중계가 로그인 만료를 알리면 바로 다시 확인해서 잠근다
- 로그인 확인: 잠그라는 응답(401 · 403)은 겹친 확인 때문에 늦게 와도 같은 토큰이면 따른다
  (겹친 두 번째 확인이 401 을 받아 "등록되지 않은 아이디" 대신 "로그인 만료"로 잠기던 문제). 미등록 화면은 다시 확인해도 유지
- 테스트: 봇 중계 4건(토큰 미노출 · 음성 정보 전달 · 다른 PC 인계 · 명령 라우팅 · 해제 시 끊김), 겹친 확인 1건
  실제 디스코드: voice-e2e GVR_E2E_RELAY=1 (서버 중계로 따라 들어가 녹음 · STT · 녹음 제외), login-e2e 19/19

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-15 10:30:14 +09:00

85 lines
3.3 KiB
TypeScript

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';
import { SettingsStore } from '../src/core/settings.ts';
const tmp = () => fs.mkdtempSync(path.join(process.env.TMPDIR || os.tmpdir(), 'gvr-bot-'));
const BUILTIN = 'MTExMTExMTExMTExMTExMTEx.FAKE00.builtin-token-for-tests-only-xxxxxxxx';
const APP = 'app-login-token-for-tests';
const OTHER = 'OTE4MTIzMzQ4NzIxMDA4NzAw.FAKE11.other-bot-token-for-tests-only-yyyy';
delete process.env.GVR_BOT_TOKEN;
/** 로그인한 상태 (앱 토큰만 있다. 겜녹이 봇 토큰은 서버에만 있다) */
const loggedIn = (dir = tmp()) => {
const s = new SettingsStore(dir);
s.setSecret('appToken', APP);
return s;
};
test('로그인 전: 겜녹이 봇 토큰이 없다 (exe 에 들어 있지 않다)', () => {
const s = new SettingsStore(tmp());
assert.equal(s.get().discord.botMode, 'builtin');
assert.equal(s.hasBuiltinBot, false);
assert.equal(s.activeBotToken(), null);
});
test('로그인 후: 겜녹이 봇이 기본이고, 이 PC 가 직접 로그인할 봇 토큰은 없다 (서버 중계)', () => {
const s = loggedIn();
assert.equal(s.hasBuiltinBot, true);
assert.equal(s.activeBotToken(), null);
});
test('v1.2.0 이 받아 둔 겜녹이 봇 토큰은 시작할 때 지운다', () => {
const dir = tmp();
loggedIn(dir).setSecret('builtinBotToken', BUILTIN);
const s = new SettingsStore(dir);
assert.equal(s.hasSecret('builtinBotToken'), false);
assert.ok(!fs.readFileSync(path.join(dir, 'secrets.bin'), 'utf8').includes(BUILTIN));
assert.equal(s.activeBotToken(), null);
});
test('다른 봇으로 진행하기: 넣은 토큰을 쓰고, 다시 겜녹이 봇으로 오가도 각자 유지된다', () => {
const dir = tmp();
const s = loggedIn(dir);
s.update({ discord: { botMode: 'custom' } });
assert.equal(s.activeBotToken(), null, '다른 봇인데 토큰이 아직 없으면 로그인하지 않는다');
s.setSecret('botToken', OTHER);
assert.equal(s.activeBotToken(), OTHER);
s.update({ discord: { botMode: 'builtin' } });
assert.equal(s.activeBotToken(), null);
s.update({ discord: { botMode: 'custom' } });
assert.equal(s.activeBotToken(), OTHER, '저장한 다른 봇 토큰은 지워지지 않는다');
// 다시 켜도 모드·토큰이 남는다
const again = new SettingsStore(dir);
assert.equal(again.get().discord.botMode, 'custom');
assert.equal(again.activeBotToken(), OTHER);
// 평문 설정 파일(settings.json)에는 토큰이 들어가지 않는다
assert.ok(!fs.readFileSync(path.join(dir, 'settings.json'), 'utf8').includes(OTHER));
});
test('v1.0.0 에서 직접 토큰을 넣어 쓰던 설치는 다른 봇 모드로 이어진다', () => {
const dir = tmp();
const old = new SettingsStore(dir);
old.setSecret('botToken', OTHER);
const saved = JSON.parse(JSON.stringify(old.get()));
delete saved.discord.botMode;
fs.writeFileSync(path.join(dir, 'settings.json'), JSON.stringify(saved));
const s = new SettingsStore(dir);
assert.equal(s.get().discord.botMode, 'custom');
assert.equal(s.activeBotToken(), OTHER);
});
test('개발용 GVR_BOT_TOKEN 은 모드와 상관없이 우선한다', () => {
process.env.GVR_BOT_TOKEN = OTHER;
try {
assert.equal(loggedIn().activeBotToken(), OTHER);
} finally {
delete process.env.GVR_BOT_TOKEN;
}
});