Files
electric-cherry/tests/unit/server-observation.test.ts
T

94 lines
6.5 KiB
TypeScript

import { describe, expect, it } from 'vitest';
import { freshTelemetry, selectServerObservation, type ServerObservation } from '../../src/lib/server-observation.js';
import { gameEvent } from '../helpers/telemetry.js';
const now = Date.parse('2026-09-27T12:00:00Z');
const at = (offset = 0) => new Date(now + offset).toISOString();
const report = (offset = 0, players = 3) => ({ ...gameEvent('heartbeat', 15, players, 'managed'), receivedAt: at(offset) });
function server(changes: Partial<ServerObservation> = {}): ServerObservation {
return { name: 'managed', health: 'online', container_state: 'running', container_checked_at: at(),
last_players: 4, last_map: 'zombie_theater', last_gametype: 'zom', last_seen_at: at(),
last_error: null, roster: [], ...changes };
}
describe('shared server observation', () => {
it('tolerates only the first transient failure while retaining diagnostics and expiring evidence', () => {
for (const reason of ['timeout', 'send-error']) {
const raw = { ...server(), last_error: `${reason}:no reply`, rcon_failures: 1 };
const selected = selectServerObservation(server({ rcon_status: raw }), undefined, now);
expect(selected).toMatchObject({ health: 'online', last_error: null, rcon_status: raw });
expect(selectServerObservation(selected, undefined, now + 90_000).health).toBe('online');
expect(selectServerObservation(selected, undefined, now + 90_001).health).toBe('stale');
expect(selectServerObservation(selected, undefined, now + 120_001).health).toBe('offline');
expect(selectServerObservation(server({ rcon_status: { ...raw, rcon_failures: 2 } }), undefined, now))
.toMatchObject({ health: 'stale', last_error: raw.last_error });
}
for (const reason of ['no-config', 'no-port', 'bad-reply', 'no-players']) {
expect(selectServerObservation(server({ last_error: `${reason}:failed`, rcon_failures: 1 }), undefined, now).health).toBe('stale');
}
expect(selectServerObservation(server({ last_error: 'timeout:legacy failure' }), undefined, now).health).toBe('stale');
});
it('accepts telemetry and container evidence through 90 seconds, then expires each independently', () => {
expect(selectServerObservation(server(), report(), now + 90_000)).toMatchObject({ health: 'online', status_source: 'telemetry' });
expect(selectServerObservation(server(), report(90_001), now + 90_001).health).toBe('stale');
const current = server({ container_checked_at: at(90_001) });
expect(selectServerObservation(current, report(), now + 90_001).health).toBe('stale');
expect(freshTelemetry(report(-90_000), now)).toBe(true);
expect(freshTelemetry(report(-90_001), now)).toBe(false);
});
it('selects the telemetry count and roster together and keeps raw RCON evidence', () => {
const raw = server({ last_error: 'timeout' });
const selected = selectServerObservation(server({ rcon_status: raw }), report(), now);
expect(selected).toMatchObject({ health: 'online', status_source: 'telemetry', last_players: 3, last_error: null });
expect(selected.roster).toHaveLength(3);
expect(selected.roster?.[0]).toMatchObject({ ping: null, score: 5000 });
expect(selected.rcon_status).toBe(raw);
});
it('uses the latest HTTP or streamed telemetry and falls back without extending freshness', () => {
const original = server();
const selected = selectServerObservation(server({ rcon_status: original, telemetry: report(0, 2) }), report(-1000), now);
expect(selected.last_players).toBe(2);
const fallback = selectServerObservation({ ...selected, container_checked_at: at(91_000),
rcon_status: { ...original, last_seen_at: at(40_000) } }, undefined, now + 91_000);
expect(fallback).toMatchObject({ status_source: 'rcon', health: 'online', last_players: 4, last_seen_at: at(40_000) });
});
it.each(['starting', 'restarting', 'unhealthy', 'exited'])('telemetry cannot override container state %s', state => {
const selected = selectServerObservation(server({ health: state === 'starting' || state === 'restarting' ? 'starting' : 'offline', container_state: state }), report(), now);
expect(selected.health).not.toBe('online');
expect(selected.last_players).toBeNull();
expect(selected.roster).toBeNull();
});
it('uses fresh RCON with unknown container state, but does not substitute telemetry or extend freshness', () => {
const raw = server({ container_state: 'unknown', container_checked_at: at(-60_000) });
expect(selectServerObservation(raw, report(), now)).toMatchObject({ health: 'online', last_players: 4, status_source: 'rcon' });
expect(selectServerObservation(raw, report(91_000), now + 91_000).health).toBe('stale');
expect(selectServerObservation(raw, report(121_000), now + 121_000).health).toBe('offline');
expect(selectServerObservation({ ...raw, telemetry_after: at() }, report(), now).health).toBe('offline');
expect(selectServerObservation({ ...raw, last_seen_at: null }, report(), now).health).toBe('offline');
});
it('requires fresh container evidence and reports after the latest lifecycle submission', () => {
expect(selectServerObservation(server({ container_checked_at: at(-91_000) }), report(), now).health).not.toBe('online');
expect(selectServerObservation(server({ health: 'provisioning' }), report(), now).health).toBe('provisioning');
expect(selectServerObservation(server({ telemetry_after: at() }), report(-1), now).health).toBe('offline');
expect(freshTelemetry(report(), now, 'invalid')).toBe(false);
expect(freshTelemetry(report(2000), now)).toBe(false);
expect(freshTelemetry(report(-91_000), now)).toBe(false);
expect(freshTelemetry({ ...report(), players: [{}] } as never, now)).toBe(false);
});
it('uses game-start rosters without inventing scores and clears match times on an empty report', () => {
const start = { ...gameEvent('game_start', 0, 2, 'managed'), receivedAt: at() };
const active = selectServerObservation(server(), start, now);
expect(active.roster?.[0]).toMatchObject({ score: null, ping: null });
expect(active.game_started_at).toBe(at());
expect(selectServerObservation({ ...active, game_ended_at: at() }, report(1, 0), now + 1))
.toMatchObject({ last_players: 0, roster: [], game_started_at: null, game_ended_at: null });
});
});