import test from 'node:test'; import assert from 'node:assert/strict'; import { Collection } from 'discord.js'; import { DiscordUi } from '../discord-ui.js'; import { emojiPalette, renderStatus, voiceSummary } from '../ui.js'; import { validateConfig } from '../config.js'; const mapping = { guildId: 'guild', serverId: 'game', voiceChannelId: 'voice', statusChannelId: 'text', displayName: 'Valheim', controlRoleName: 'Unfugwart', panelUrl: 'https://panel.example', connectAddress: 'games.example:2456' }; const lifecycle = { known: true, humans: 1, stopping: false }; const emoji = (id, name, animated = false) => ({ id, name, animated }); const item = { mapping, control: { state: () => lifecycle }, poller: { state: () => ({ status: 'offline', phase: 'offline' }) } }; const fixture = client => new DiscordUi({ client, config: {}, entries: new Map([['voice', item]]) }); function interaction(action, guildId = 'guild') { return { isChatInputCommand: () => true, commandName: 'server', guildId, options: { getString: name => name === 'aktion' ? action : 'game' }, async deferReply() { this.deferred = true; }, async editReply(value) { assert.equal(this.deferred, true); this.output = value; }, async reply(value) { this.output = value; }, }; } test('emoji lookup prefers configured names and a generic icon, validates IDs and ambiguity', () => { const pack = [emoji('11', 'sleeping'), emoji('12', 'custom_sleep'), emoji('13', 'adventurer'), emoji('14', 'valheim')]; assert.equal(emojiPalette(pack, { sleeping: 'custom_sleep' }).sleeping.id, '12'); assert.equal(emojiPalette(pack).adventurer.id, '13'); assert.equal(typeof emojiPalette([...pack, emoji('15', 'sleeping')]).sleeping, 'string'); assert.equal(typeof emojiPalette([emoji('invalid', 'ready')]).ready, 'string'); }); test('application emoji fetch resolves pack, refreshes names, and safely falls back on error', async () => { const client = { application: { emojis: { async fetch() { return new Collection([['11', emoji('11', 'ready', true)]]); } } } }; const ui = fixture(client); await ui.refreshEmojis(); assert.equal(ui.emojis.ready.id, '11'); assert.equal(ui.appEmojiCount, 1); client.application.emojis.fetch = async () => { throw new Error('private URL must not leak'); }; await ui.refreshEmojis(); assert.equal(typeof ui.emojis.ready, 'string'); assert.equal(ui.appEmojiCount, 0); ui.close(); }); test('each card phase renders custom icons, bounded payloads and a join address without losing status copy', () => { const names = ['sleeping', 'starting', 'ready', 'saving', 'stopping', 'adventurer']; const pack = emojiPalette(names.map((name, index) => emoji(String(100 + index), name, name === 'ready'))); for (const phase of ['offline', 'starting', 'updating', 'loading-world', 'saving', 'ready', 'stopping', 'unknown']) { const payload = renderStatus(mapping, lifecycle, { status: phase === 'offline' ? 'offline' : 'running', phase, progress: ['saving', 'updating'].includes(phase) ? 60 : null }, 1, pack); const rendered = JSON.stringify(payload); assert.ok(rendered.length < 6000); assert.match(payload.embeds[0].title, /<:adventurer:105>/); assert.ok(payload.embeds[0].fields.some(field => field.value.includes('games.example:2456'))); assert.equal(payload.components[0].components[0].emoji.id, '101'); assert.equal(payload.components[0].components[1].emoji.animated, true); assert.ok(!payload.embeds[0].description.startsWith('🧭 Adresse')); } }); test('setup defers before app fetch and diagnoses effective channel permissions and the human role', async () => { let request; const text = { id: 'text', isTextBased: () => true }; const voice = { id: 'voice', isVoiceBased: () => true }; const guild = { roles: { cache: new Map([['bot-role', { name: 'Unfugwart', managed: true }], ['human-role', { name: 'Unfugwart', managed: false }]]) }, channels: { cache: new Map([['text', text], ['voice', voice]]) }, members: { me: { permissionsIn() { return { has: name => name !== 'ManageChannels' }; } } } }; const client = { guilds: { cache: new Map([['guild', guild]]) }, application: { emojis: { async fetch() { assert.equal(request.deferred, true); return new Collection(); } } } }; const ui = fixture(client); request = interaction('setup'); await ui.interaction(request); const report = JSON.stringify(request.output); assert.match(report, /Kanäle verwalten/); assert.match(report, /Mitglieder der Rolle/); assert.match(report, /Standard-Emojis/); assert.doesNotMatch(report, /token|secret/i); const outsider = interaction('setup', 'other'); await ui.interaction(outsider); assert.match(JSON.stringify(outsider.output), /Nicht verfügbar/); ui.close(); }); test('orakel is same-guild read-only and never calls game actions', async () => { const ui = fixture({}); const request = interaction('orakel'); await ui.interaction(request); assert.match(JSON.stringify(request.output), /Goblin-Orakel/); const outsider = interaction('orakel', 'other'); await ui.interaction(outsider); assert.match(JSON.stringify(outsider.output), /Nicht verfügbar/); ui.close(); }); test('voice summary uses readable Unicode and coarse idle countdown rather than custom emoji syntax', () => { const summary = voiceSummary(mapping, { deadlineAt: 120_001, holdUntil: 0 }, { status: 'running', phase: 'ready' }, 1); assert.match(summary, /Feierabend in 2 min/); assert.doesNotMatch(summary, / { const config = { discordToken: 'test', panelUrl: 'https://panel.example', emojiNames: { ready: 'uw_ready' }, mappings: [{ guildId: '1', voiceChannelId: '2', serverId: 'game', oauthClientId: 'test', oauthClientSecret: 'test', connectAddress: 'games.example:2456' }] }; assert.equal(validateConfig(config).emojiNames.ready, 'uw_ready'); for (const address of ['games.example:password', 'user:secret@games.example:2456', 'games.example:0', 'games.example:99999']) { assert.throws(() => validateConfig({ ...config, mappings: [{ ...config.mappings[0], connectAddress: address }] })); } });