import test from 'node:test'; import assert from 'node:assert/strict'; import { DiscordUi } from '../discord-ui.js'; const mapping = { guildId: 'guild', serverId: 'game', displayName: 'Game', controlRoleName: 'Serverwart', statusChannelId: 'text', voiceChannelId: 'voice' }; const lifecycleState = () => ({ known: true, humans: 0, error: null, stopping: false, deadlineAt: null, holdUntil: null, startupUntil: 0 }); const interaction = (action, { userId = 'user', serverId = 'game', roles = new Map([['role', { id: 'role', name: 'Serverwart' }]]), memberRoles = new Set(['role']), reply } = {}) => { const value = { isChatInputCommand: () => true, commandName: 'server', options: { getString: name => name === 'aktion' ? action : serverId }, user: { id: userId }, guildId: 'guild', guild: { id: 'guild', roles: { cache: roles } }, member: { roles: { cache: memberRoles } }, deferred: false, replied: false, deferReply: async function () { this.deferred = true; }, editReply: async function (content) { this.edited = content; }, }; value.reply = reply ?? (async content => { value.response = content; }); return value; }; function entry() { const calls = { start: 0, hold: 0, stop: 0, poll: 0 }; return { calls, mapping, control: { state: lifecycleState, manualStart: async () => (calls.start++, { status: 'success' }), keepAwake: async () => (calls.hold++, { status: 'success' }), confirmedStop: async () => (calls.stop++, { status: 'success' }) }, poller: { state: () => ({ status: 'offline', phase: 'offline' }), poll: async () => (calls.poll++, {}) } }; } function interactionClient(channels = new Map()) { return { guilds: { cache: new Map([['guild', { channels: { cache: channels } }]]) }, rest: { put: async () => {} } }; } test('interactions execute only authorized current-role actions and contain reply failures', async () => { const item = entry(); const ui = new DiscordUi({ client: interactionClient(), config: {}, entries: new Map([['voice', item]]) }); const start = interaction('start'); await ui.interaction(start); assert.equal(item.calls.start, 1); assert.match(start.edited.embeds[0].description, /Start angefordert/); const hold = interaction('stay'); await ui.interaction(hold); assert.equal(item.calls.hold, 1); item.poller.state = () => ({ status: 'running', phase: 'ready' }); const stop = interaction('stop'); await ui.interaction(stop); const confirm = interaction('confirm-stop'); await ui.interaction(confirm); assert.equal(item.calls.stop, 1); const expired = interaction('confirm-stop'); await ui.interaction(expired); assert.match(expired.response.embeds[0].description, /abgelaufen/); ui.confirmations.create({ requesterId: 'user', serverId: 'other' }); const wrongTarget = interaction('confirm-stop'); await ui.interaction(wrongTarget); assert.equal(item.calls.stop, 1); ui.confirmations.create({ requesterId: 'user', serverId: 'game' }); await ui.interaction(interaction('confirm-stop', { userId: 'other' })); assert.equal(item.calls.stop, 1); await ui.interaction(interaction('confirm-stop')); assert.equal(item.calls.stop, 2); const removedRole = interaction('confirm-stop', { memberRoles: new Set() }); await ui.interaction(removedRole); assert.equal(item.calls.stop, 2); const status = interaction('status'); await ui.interaction(status); assert.equal(item.calls.poll, 1); await ui.handleInteraction(interaction('start', { memberRoles: new Set(), reply: async () => { throw new Error('token URL must stay private'); } })); ui.close(); }); test('voice status uses Discord REST route and deduplicates per channel', async () => { const item = entry(); const requests = []; const client = interactionClient(new Map([['voice', { isVoiceBased: () => true }]])); client.rest.put = async (...args) => requests.push(args); const ui = new DiscordUi({ client, config: {}, entries: new Map([['voice', item]]) }); await ui.voiceStatus(item, { status: 'offline', phase: 'offline' }); await ui.voiceStatus(item, { status: 'offline', phase: 'offline' }); assert.deepEqual(requests, [['/channels/voice/voice-status', { body: { status: '💤 Game: Offline' } }]]); }); test('guild command registration upserts only Unfugwart command', async () => { const edits = []; const existing = new Map([['other', { id: 'other', name: 'unrelated' }], ['server', { id: 'server', name: 'server' }]]); const client = { application: { commands: { async fetch() { return existing; }, async edit(...args) { edits.push(['edit', ...args]); }, async create(...args) { edits.push(['create', ...args]); }, } } }; const entry = { mapping: { guildId: 'guild', serverId: 'game', displayName: 'Game' } }; const ui = new DiscordUi({ client, config: {}, entries: new Map([['voice', entry]]) }); await ui.registerCommands(); assert.equal(edits.length, 1); assert.equal(edits[0][0], 'edit'); assert.equal(edits[0][1], 'server'); assert.equal(existing.get('other').name, 'unrelated'); });