Luigit
repositories / Unfugwart

Unfugwart

Discord caretaker for Abteilung Unfug

owned by admin

test/clear-commands.test.js

Raw
import test from 'node:test';
import assert from 'node:assert/strict';
import { renderStatus, voiceSummary } from '../ui.js';
import { DiscordUi } from '../discord-ui.js';

const mapping = { guildId: 'guild', serverId: 'game', displayName: 'Valheim', voiceChannelId: 'voice', controlRoleName: 'Unfugwart', panelUrl: 'https://panel.example', connectAddress: 'games.example:2456' };
const text = payload => payload.embeds.map(embed => [embed.title, embed.description, ...embed.fields.map(field => `${field.name}: ${field.value}`), embed.footer.text].join('\n')).join('\n');

function fixture() {
  let phase = 'offline';
  const entry = { mapping, control: { state: () => ({ known: true, humans: 0 }), manualStart: async () => ({ status: 'success' }), keepAwake: async () => ({ status: 'success' }), confirmedStop: async () => ({ status: 'success' }) }, poller: { state: () => ({ status: phase === 'offline' ? 'offline' : 'running', phase }), poll: async () => {} } };
  const ui = new DiscordUi({ client: { user: { id: 'bot' } }, config: { panelUrl: mapping.panelUrl }, entries: new Map([['voice', entry]]) });
  const interaction = (action, authorized = true) => ({ isChatInputCommand: () => true, commandName: 'server', guildId: 'guild', guild: { id: 'guild', roles: { cache: new Map([['role', { id: 'role', name: 'Unfugwart' }]]) } }, member: { roles: { cache: new Set(authorized ? ['role'] : []) } }, user: { id: 'user' }, options: { getString: name => name === 'aktion' ? action : 'game' }, async deferReply() { this.deferred = true; }, async editReply(payload) { this.output = payload; }, async reply(payload) { this.output = payload; } });
  return { ui, entry, interaction, setPhase(value) { phase = value; } };
}

test('accepted status hierarchy puts state and shutdown ahead of address, without technical IDs or competing timestamps', () => {
  const payload = renderStatus(mapping, { known: true, humans: 0, deadlineAt: 480_000 }, { status: 'running', phase: 'process-running', updatedAt: 10_000 }, 1);
  assert.match(payload.embeds[0].description, /Läuft[\s\S]*Spielbereitschaft noch nicht bestätigt[\s\S]*Stoppt <t:480:R>/);
  assert.doesNotMatch(text(payload), /servergemeldet|Glaskugel|unfugwart:game|Beobachtet|Start-Frist/);
  assert.equal(payload.embeds[0].fields[0].value, '`games.example:2456`');
  assert.equal(payload.components[0].components[0].disabled, true);
  assert.equal(payload.components[0].components[3].disabled, false);
});

test('shutdown keeps save progress visible under the stopping headline', () => {
  const payload = renderStatus(mapping, { stopping: true, known: true }, { status: 'stopping', phase: 'saving', progress: 60 }, 1);
  assert.match(payload.embeds[0].description, /Stoppt/);
  assert.ok(payload.embeds[0].fields.some(field => field.name === 'Welt speichern' && field.value.includes('60 %')));
});

test('offline has no misleading countdown, and hold/disconnect are explicit', () => {
  const offline = renderStatus(mapping, { known: true, humans: 0, deadlineAt: 480_000 }, { status: 'offline' }, 1);
  assert.match(text(offline), /Offline[\s\S]*Kein Stopp geplant/);
  assert.doesNotMatch(text(offline), /<t:480:R>/);
  assert.equal(offline.components[0].components[0].disabled, false);
  assert.equal(offline.components[0].components[3].disabled, true);
  assert.doesNotMatch(voiceSummary(mapping, { deadlineAt: 480_000 }, { status: 'offline' }, 1), /Feierabend/);
  assert.match(text(renderStatus(mapping, { known: true, holdUntil: 480_000 }, { status: 'running' }, 1)), /Stopp pausiert/);
  assert.match(text(renderStatus(mapping, { known: false }, { status: 'running' }, 1)), /Automatik pausiert[\s\S]*Kein automatischer Stopp/);
});

test('status, success, permission denial, confirmation and oracle use the same concise rich layout', async () => {
  const { ui, entry, interaction, setPhase } = fixture();
  try {
    for (const action of ['status', 'start', 'stay', 'orakel']) {
      const request = interaction(action);
      await ui.interaction(request);
      assert.equal(request.output.embeds[0].footer.text, 'Unfugwart · Abteilung Unfug');
      assert.doesNotMatch(text(request.output), /Statusnachricht wird aktualisiert/);
    }
    const denied = interaction('start', false);
    await ui.interaction(denied);
    assert.match(text(denied.output), /Steuerrolle fehlt/);
    setPhase('ready');
    const stop = interaction('stop');
    await ui.interaction(stop);
    assert.match(text(stop.output), /Server wirklich stoppen/);
    assert.equal(stop.output.components[0].components[0].label, 'Ja, stoppen');
    entry.control.confirmedStop = async () => ({ status: 'failure' });
    const confirm = interaction('confirm-stop');
    await ui.interaction(confirm);
    assert.match(text(confirm.output), /Aktion fehlgeschlagen/);
    const expired = interaction('confirm-stop');
    await ui.interaction(expired);
    assert.match(text(expired.output), /Bestätigung abgelaufen/);
  } finally { ui.close(); }
});

test('legacy card is recognized for in-place migration while wrong authors or targets are rejected', () => {
  const { ui } = fixture();
  try {
    assert.equal(ui.validMessage({ author: { id: 'bot' }, embeds: [{ footer: { text: 'unfugwart:game' } }] }, 'game'), true);
    const embed = renderStatus(mapping, {}, { status: 'offline' }).embeds[0];
    assert.equal(ui.validMessage({ author: { id: 'bot' }, embeds: [embed] }, 'game'), true);
    assert.equal(ui.validMessage({ author: { id: 'other' }, embeds: [embed] }, 'game'), false);
    assert.equal(ui.validMessage({ author: { id: 'bot' }, embeds: [embed] }, 'different'), false);
  } finally { ui.close(); }
});

test('error after deferred interaction preserves embed payload instead of losing feedback', async () => {
  const { ui } = fixture();
  try {
    let edited;
    const request = { deferred: true, replied: false, async editReply(payload) { edited = payload; } };
    await ui.safeReply(request, ui.response(null, 'Aktion fehlgeschlagen', 'Erneut versuchen.'));
    assert.match(text(edited), /Aktion fehlgeschlagen/);
  } finally { ui.close(); }
});