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 { builtinBotToken, decodeBuiltinToken, encodeBuiltinToken } from '../src/core/builtin-bot.ts'; 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 OTHER = 'OTE4MTIzMzQ4NzIxMDA4NzAw.FAKE11.other-bot-token-for-tests-only-yyyy'; delete process.env.GVR_BOT_TOKEN; test('기본 봇 토큰: 감춘 값은 평문을 담지 않고 되돌리면 원래 토큰', () => { const blob = encodeBuiltinToken(BUILTIN); assert.ok(!blob.includes(BUILTIN)); for (const part of BUILTIN.split('.')) assert.ok(!blob.includes(part), part); assert.equal(decodeBuiltinToken(blob), BUILTIN); // 개발 실행(빌드 상수 없음)에서는 기본 봇이 없다 assert.equal(builtinBotToken(), null); }); test('새 설치: 겜녹이 봇이 기본이고 토큰 입력 없이 기본 토큰을 쓴다', () => { const s = new SettingsStore(tmp(), undefined, { builtinBotToken: BUILTIN }); assert.equal(s.get().discord.botMode, 'builtin'); assert.equal(s.hasBuiltinBot, true); assert.equal(s.activeBotToken(), BUILTIN); }); test('다른 봇으로 진행하기: 넣은 토큰을 쓰고, 다시 겜녹이 봇으로 오가도 각자 유지된다', () => { const dir = tmp(); const s = new SettingsStore(dir, undefined, { builtinBotToken: BUILTIN }); 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(), BUILTIN); s.update({ discord: { botMode: 'custom' } }); assert.equal(s.activeBotToken(), OTHER, '저장한 다른 봇 토큰은 지워지지 않는다'); // 다시 켜도 모드·토큰이 남는다 const again = new SettingsStore(dir, undefined, { builtinBotToken: BUILTIN }); assert.equal(again.get().discord.botMode, 'custom'); assert.equal(again.activeBotToken(), OTHER); // 저장 파일에 기본 봇 토큰은 들어가지 않는다 for (const f of fs.readdirSync(dir)) assert.ok(!fs.readFileSync(path.join(dir, f), 'utf8').includes(BUILTIN), f); }); test('v1.0.0 에서 직접 토큰을 넣어 쓰던 설치는 다른 봇 모드로 이어진다', () => { const dir = tmp(); const old = new SettingsStore(dir, undefined, { builtinBotToken: null }); 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, undefined, { builtinBotToken: BUILTIN }); assert.equal(s.get().discord.botMode, 'custom'); assert.equal(s.activeBotToken(), OTHER); }); test('기본 봇이 없는 빌드: 겜녹이 봇 모드면 토큰 없음, 다른 봇은 그대로 동작', () => { const s = new SettingsStore(tmp(), undefined, { builtinBotToken: null }); assert.equal(s.hasBuiltinBot, false); assert.equal(s.activeBotToken(), null); s.update({ discord: { botMode: 'custom' } }); s.setSecret('botToken', OTHER); assert.equal(s.activeBotToken(), OTHER); }); test('개발용 GVR_BOT_TOKEN 은 모드와 상관없이 우선한다', () => { process.env.GVR_BOT_TOKEN = OTHER; try { const s = new SettingsStore(tmp(), undefined, { builtinBotToken: BUILTIN }); assert.equal(s.activeBotToken(), OTHER); } finally { delete process.env.GVR_BOT_TOKEN; } });