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(); });