Luigit
repositories / Unfugwart

Unfugwart

Discord caretaker for Abteilung Unfug

owned by admin

test/telemetry.test.js

Raw
import test from 'node:test';
import assert from 'node:assert/strict';
import { PollController, Telemetry } from '../telemetry.js';

const bytes = text => Buffer.from(text, 'utf8');

function deferred() {
  let resolve;
  const promise = new Promise(done => { resolve = done; });
  return { promise, resolve };
}

test('telemetry parses only allowlisted fresh state across UTF-8 and CR fragments', () => {
  const telemetry = new Telemetry({ game: 'valheim', now: () => 9 });
  const snowman = bytes('☃');
  telemetry.feed(Buffer.concat([bytes('Update state (x): verifying install, progress:0.5 (5/10)\r'), snowman.subarray(0, 1)]));
  telemetry.feed(Buffer.concat([snowman.subarray(1), bytes('Game server connected\n')]));
  assert.equal(telemetry.snapshot().phase, 'ready');
  assert.equal(telemetry.snapshot().progress, null);
  telemetry.feed(bytes('[x] Connections 4 ZDOS: 1\n'), 12);
  assert.equal(telemetry.snapshot().players, 4);
  assert.equal(telemetry.snapshot().lastPlayerObservationAt, 12);
});

test('telemetry recognizes realistic Valheim lines without exposing them', () => {
  const telemetry = new Telemetry({ game: 'valheim' });
  telemetry.feed(bytes('08:12:44 Connections 3 ZDOS: 55\nInitialize engine version: 2022\nZNet.LoadWorld: My World\nUpdate state (0x1): downloading, progress: 0.5\nWorld save (1/0)\nWorld saved\n'));
  assert.equal(telemetry.snapshot().players, null);
  assert.equal(telemetry.snapshot().phase, 'saving');
  telemetry.feed(bytes('Update state (0x1): downloading, progress: 0.5\n'));
  assert.equal(telemetry.snapshot().progress, 0.5);
});

test('telemetry ignores dungeon Connections text and resets stale observations', () => {
  const telemetry = new Telemetry({ game: 'valheim' });
  telemetry.feed(bytes('Connections 4 during dungeon generation\n'), 2);
  assert.equal(telemetry.snapshot().players, null);
  telemetry.reset({ status: 'offline' });
  assert.equal(telemetry.snapshot().lastPlayerObservationAt, null);
});

test('polling establishes history cursor, parses only later console bytes, and rebaselines after failure', async () => {
  const calls = [];
  const replies = [
    { epoch: 10, logs: bytes('Game server connected\n') },
    { epoch: 11, logs: bytes('Game server connected\n') },
    new Error('unavailable'),
    { epoch: 20, logs: bytes('Game server connected\n') },
    { epoch: 21, logs: bytes('Game server connected\n') },
  ];
  const api = {
    async status() { return true; },
    async console(_id, cursor) {
      calls.push(cursor);
      const reply = replies.shift();
      if (reply instanceof Error) throw reply;
      return reply;
    },
  };
  const poller = new PollController({ game: 'valheim', serverId: 'game', api, onState() {} });
  await poller.poll();
  assert.equal(poller.state().phase, 'process-running');
  await poller.poll();
  assert.equal(poller.state().phase, 'ready');
  await poller.poll();
  assert.equal(poller.state().status, 'unknown');
  await poller.poll();
  assert.equal(poller.state().phase, 'process-running');
  await poller.poll();
  assert.equal(poller.state().phase, 'ready');
  assert.deepEqual(calls, [0, 10, 11, 0, 20]);
});

test('timestamped SteamCMD, world, and player lines retain real values', () => {
  const telemetry = new Telemetry({ game: 'valheim' });
  telemetry.feed(bytes('10/17/2026 22:26:51: Update state (0x1): downloading, progress: 0.5\n10/17/2026 22:26:51: Initialize engine version: x\n10/17/2026 22:26:51: ZNet.LoadWorld: Bugalonia\n10/17/2026 22:26:51: Connections 2 ZDOS: 9\n'));
  assert.equal(telemetry.snapshot().phase, 'loading-world');
  assert.equal(telemetry.snapshot().players, 2);
  const progress = new Telemetry({ game: 'valheim' });
  progress.feed(bytes('10/17/2026 22:26:51: Update state (0x1): downloading, progress: 0.5\n'));
  assert.equal(progress.snapshot().progress, 0.5);
});

test('save completion cannot invent readiness or override stopping', () => {
  const telemetry = new Telemetry({ game: 'valheim' });
  telemetry.feed(bytes('World saved\n'));
  assert.equal(telemetry.snapshot().phase, 'offline');
  telemetry.feed(bytes('Shutting down\nWorld saved\n'));
  assert.equal(telemetry.snapshot().phase, 'stopping');
});

test('prepareStartup baselines before start and stale poll cannot clobber it', async () => {
  const pending = deferred();
  const api = { async status() { return pending.promise; }, async console(_id, cursor) { return { epoch: cursor ? 12 : 10, logs: bytes('Game server connected\n') }; } };
  const poller = new PollController({ game: 'valheim', serverId: 'game', api });
  const oldPoll = poller.poll();
  await poller.prepareStartup();
  pending.resolve(true);
  await oldPoll;
  assert.equal(poller.cursor, 10);
  await poller.poll();
  assert.equal(poller.state().phase, 'ready');
});

test('startup intent tolerates transient stopped status then expires offline', async () => {
  let now = 0;
  const api = { async status() { return false; }, async console() { return { epoch: 1, logs: bytes('') }; } };
  const poller = new PollController({ game: 'valheim', serverId: 'game', api, now: () => now, startupTimeoutMs: 10 });
  poller.beginStartup();
  await poller.poll();
  assert.equal(poller.state().status, 'starting');
  now = 11;
  await poller.poll();
  assert.equal(poller.state().status, 'offline');
});