Luigit
repositories / Unfugwart

Unfugwart

Discord caretaker for Abteilung Unfug

owned by admin

test/dashboard-regression.test.js

Raw
import test from 'node:test';
import assert from 'node:assert/strict';
import { mkdtemp, readFile, rm } from 'node:fs/promises';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { pathToFileURL } from 'node:url';
import { Collection } from 'discord.js';
import { DiscordUi } from '../discord-ui.js';
import { PollController, Telemetry } from '../telemetry.js';

const bytes = text => Buffer.from(text);

test('dashboard persists URL state, recovers deleted cards, and never duplicates on transient fetch errors', async () => {
  const root = await mkdtemp(join(tmpdir(), 'unfugwart-dashboard-'));
  let message = null;
  let sends = 0;
  let unavailable = false;
  const channel = {
    isTextBased: () => true,
    messages: { async fetch(value) {
      if (unavailable) throw { code: 50013 };
      if (typeof value === 'object') return new Collection(message ? [[message.id, message]] : []);
      if (!message) throw { code: 10008 };
      return message;
    } },
    async send(payload) {
      sends++;
      message = { id: String(sends), author: { id: 'bot' }, embeds: payload.embeds,
        async edit(next) { this.embeds = next.embeds; return this; } };
      return message;
    },
  };
  const item = {
    mapping: { serverId: 'game', guildId: 'guild', voiceChannelId: 'voice', statusChannelId: 'text', displayName: 'Game' },
    control: { state: () => ({ known: true, humans: 0 }) },
    poller: { state: () => ({ status: 'offline', phase: 'offline', players: null, progress: null }) },
  };
  const client = { user: { id: 'bot' }, guilds: { cache: new Map([['guild', { channels: { cache: new Map([['text', channel]]) } }]]) } };
  const ui = new DiscordUi({ client, config: { panelUrl: 'https://panel.example' }, entries: new Map([['voice', item]]), statePath: pathToFileURL(join(root, 'state.json')) });
  try {
    await ui.update(item);
    assert.deepEqual(JSON.parse(await readFile(join(root, 'state.json'))), { game: '1' });
    await ui.update(item);
    assert.equal(sends, 1);
    message = null;
    await ui.update(item);
    assert.equal(sends, 2);
    unavailable = true;
    await assert.rejects(ui.update(item));
    assert.equal(sends, 2);
  } finally { ui.close(); await rm(root, { recursive: true, force: true }); }
});

test('save completion restores known readiness but never turns shutdown into ready', () => {
  const telemetry = new Telemetry({ game: 'valheim' });
  telemetry.feed(bytes('Game server connected\nWorld save (1/5) done\nWorld save (5/5) done. Total time [441ms]\n'));
  assert.equal(telemetry.snapshot().phase, 'ready');
  telemetry.feed(bytes('Game - OnApplicationQuit\nWorld save (1/5) done\nWorld save (5/5) done. Total time [441ms]\n'));
  assert.equal(telemetry.snapshot().phase, 'stopping');
  assert.equal(telemetry.snapshot().status, 'stopping');
});

test('accepted start does not discard the pre-dispatch cursor or first readiness marker', async () => {
  let cursor = 10;
  const api = { async status() { return true; }, async console() { return { epoch: cursor++, logs: bytes('Game server connected\n') }; } };
  const poller = new PollController({ game: 'valheim', serverId: 'game', api });
  await poller.prepareStartup();
  poller.beginStartup();
  assert.equal(poller.cursor, 10);
  await poller.poll();
  assert.equal(poller.state().phase, 'ready');
  poller.close();
});