import assert from 'node:assert/strict'; import { test } from 'node:test'; import { ChannelType } from 'discord.js'; import { FollowBot } from '../src/bot.ts'; import { loadConfig } from '../src/config.ts'; import { Store } from '../src/db.ts'; /** 정원이 찬 채널 A(봇이 들어가 있음, joinable=false)와 같은 상태의 채널 B */ function fakeGuild() { const guild: any = { id: 'g', members: { me: {} }, voiceStates: { cache: new Map() } }; const channel = (id: string) => ({ id, guild, type: ChannelType.GuildVoice, joinable: false, permissionsFor: () => ({ has: () => true }) }); const A = channel('A'); const B = channel('B'); guild.voiceStates.cache.set('u1', { id: 'u1', channelId: 'A', channel: A, member: { user: { bot: false } } }); guild.voiceStates.cache.set('u2', { id: 'u2', channelId: 'B', channel: B, member: { user: { bot: false } } }); return guild; } test('봇이 들어가 정원이 찬 지금 채널의 동의자는 그대로 따라간다 (나갔다 들어오기 반복 X)', async () => { const store = new Store(':memory:'); store.consent({ id: 'u1', username: 'a' }); store.consent({ id: 'u2', username: 'b' }); store.setConsentGuilds('u1', ['g']); store.setConsentGuilds('u2', ['g']); const cfg = loadConfig({ PUBLIC_URL: 'http://localhost', DISCORD_CLIENT_ID: 'c', DISCORD_CLIENT_SECRET: 's' }, false); const bot = new FollowBot(cfg, store); const guild = fakeGuild(); assert.deepEqual(bot.consentedPresence(guild), []); // 들어가 있지 않으면 꽉 찬 채널은 못 들어감 (bot as any).recorders.set('g', { channelId: 'A' }); assert.deepEqual(bot.consentedPresence(guild), [{ userId: 'u1', channelId: 'A' }]); // 지금 채널 A 는 유지, 꽉 찬 B 는 제외 (bot as any).recorders.clear(); await bot.client.destroy(); store.close(); }); test('녹음할 서버로 고르지 않은 서버에서는 동의자라도 따라가지 않는다', async () => { const store = new Store(':memory:'); store.consent({ id: 'u1', username: 'a' }); store.consent({ id: 'u2', username: 'b' }); store.setConsentGuilds('u1', ['g']); store.setConsentGuilds('u2', ['other']); const cfg = loadConfig({ PUBLIC_URL: 'http://localhost', DISCORD_CLIENT_ID: 'c', DISCORD_CLIENT_SECRET: 's' }, false); const bot = new FollowBot(cfg, store); const guild = fakeGuild(); (bot as any).recorders.set('g', { channelId: 'A' }); assert.deepEqual(bot.consentedPresence(guild), [{ userId: 'u1', channelId: 'A' }]); // 서버를 빼면 진행 중인 녹음도 멈춘다 const stopped: string[] = []; (bot as any).recorders.set('g', { channelId: 'A', stopUser: (id: string) => stopped.push(id) }); store.setConsentGuilds('u1', []); bot.consentChanged('u1'); assert.deepEqual(stopped, ['u1']); assert.deepEqual(bot.consentedPresence(guild), []); (bot as any).recorders.clear(); await bot.client.destroy(); store.close(); }); test('녹음 중 표시는 그 서버를 고른 사람에게만 (다른 동의자를 녹음 중인 채널에 같이 있어도)', async () => { const store = new Store(':memory:'); store.consent({ id: 'u1', username: 'a' }); store.consent({ id: 'u3', username: 'c' }); store.setConsentGuilds('u1', ['g']); store.setConsentGuilds('u3', ['other']); const cfg = loadConfig({ PUBLIC_URL: 'http://localhost', DISCORD_CLIENT_ID: 'c', DISCORD_CLIENT_SECRET: 's' }, false); const bot = new FollowBot(cfg, store); const guild = fakeGuild(); guild.voiceStates.cache.set('u3', { id: 'u3', channelId: 'A', member: { user: { bot: false } } }); guild.channels = { cache: new Map([['A', { name: 'A채널' }]]) }; (bot.client.guilds.cache as any).set('g', guild); (bot as any).recorders.set('g', { channelId: 'A' }); assert.equal(bot.recordingNow('u1'), 'A채널'); assert.equal(bot.recordingNow('u3'), null); (bot as any).recorders.clear(); (bot.client.guilds.cache as any).clear(); await bot.client.destroy(); store.close(); }); test('운영자가 앱에서 끈 서버는 따라가지 않는다', async () => { const store = new Store(':memory:'); const cfg = loadConfig({ PUBLIC_URL: 'http://localhost', DISCORD_CLIENT_ID: 'c', DISCORD_CLIENT_SECRET: 's' }, false); const bot = new FollowBot(cfg, store); const guild = { ...fakeGuild(), name: '서버' }; (bot.client.guilds.cache as any).set('g', guild); assert.equal((bot as any).allowedGuild(guild), true); // 처음에는 켜짐 store.setGuildEnabled('admin', { g: false }); assert.equal((bot as any).allowedGuild(guild), false); assert.deepEqual(bot.adminGuilds(), [{ id: 'g', name: '서버', enabled: false }]); (bot.client.guilds.cache as any).clear(); await bot.client.destroy(); store.close(); });