import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; import { currentRound, fleetIdentity, matchesFleet, watchSatisfied, type FleetServer, type FleetOperation } from '../../src/lib/fleet-overview.js'; import { gameEvent } from '../helpers/telemetry.js'; import { newFixture, type Fixture } from '../helpers/fixture.js'; const now = Date.parse('2026-09-18T12:00:00Z'); const iso = (offset = 0) => new Date(now + offset).toISOString(); const server: FleetServer = { name: 't6_zombie_town_1', game: 't6', map: 'zm_transit', last_map: 'zm_transit', last_players: 4, last_gametype: 'zstandard', last_seen_at: iso(-1000), last_error: null, roster: ['Alex', 'Blake', 'Casey', 'Josh'].map((name, i) => ({ name, num: i, score: 100 - i, ping: 30, guid: `${i}` })), health: 'online', held: [], active_since: iso(-120_000), inactive_since: null, game_started_at: iso(-60_000), game_ended_at: null }; const operation: FleetOperation = { key: 'campaign:one:server', group: 'campaign:one', server: server.name, kind: 'campaign', label: 'Queued', detail: '', href: '/campaigns/one', state: 'active', startedAt: iso() }; describe('Fleet discovery and match freshness', () => { it('finds a player outside the displayed top three, including with a map query', () => { expect(matchesFleet(server, 'josh town', 'all', '', [], [], now)).toBe(true); expect(matchesFleet(server, 'josh farm', 'all', '', [], [], now)).toBe(false); }); it('keeps T6 variants distinct and launches the exact selection', () => { const town = fleetIdentity(server); const farm = fleetIdentity({ ...server, name: 't6_zombie_farm_1' }); expect(town.map).toBe('Town Survival'); expect(town.group).not.toBe(farm.group); expect(town.launch).toEqual({ game: 't6', slug: 'town' }); expect(fleetIdentity({ ...server, name: 'custom', map_slug: 'town' }).launch).toEqual(town.launch); expect(fleetIdentity({ ...server, last_map: 'zm_tomb' }).launch).toBeNull(); expect(fleetIdentity({ ...server, game_mode: 'mp' }).launch).toBeNull(); }); it('does not classify stale, missing, or starting occupancy as an empty lobby', () => { for (const change of [{ last_seen_at: iso(-46_000) }, { last_players: null }, { health: 'starting' as const }, { health: 'offline' as const }]) { expect(matchesFleet({ ...server, last_players: 0, ...change }, '', 'empty', '', [], [], now)).toBe(false); } expect(matchesFleet({ ...server, last_players: 0 }, '', 'empty', '', [], [], now)).toBe(true); expect(matchesFleet(server, '', 'occupied', 't5', [], [], now)).toBe(false); }); it('includes stale health and failed operations in attention without flagging normal startup', () => { expect(matchesFleet({ ...server, last_seen_at: iso(-46_000) }, '', 'attention', '', [], [], now)).toBe(true); expect(matchesFleet(server, '', 'attention', '', [], [{ ...operation, state: 'attention' }], now)).toBe(true); expect(matchesFleet({ ...server, health: 'starting' }, '', 'attention', '', [], [], now)).toBe(false); }); it('only spotlights fresh telemetry from the current occupied match', () => { const report = { ...gameEvent('round_change', 38, 4, server.name), receivedAt: iso(-1000) }; expect(currentRound(server, report, now)).toBe(38); for (const change of [{ receivedAt: iso(-121_000) }, { receivedAt: iso(-61_000) }, { receivedAt: iso(60_000) }]) { expect(currentRound(server, { ...report, ...change }, now)).toBeNull(); } for (const change of [{ game_ended_at: iso() }, { last_players: 0 }, { health: 'stale' as const }, { active_since: iso() }]) { expect(currentRound({ ...server, ...change }, report, now)).toBeNull(); } }); }); describe('one-shot lobby watches', () => { it('requires a fresh zero-player reading, never an absent server or unknown count', () => { const watch = { id: 'empty', server: server.name, kind: 'empty' as const, href: '/' }; expect(watchSatisfied(watch, { ...server, last_players: 0 }, [], now)).toBe(true); expect(watchSatisfied(watch, undefined, [], now)).toBe(false); expect(watchSatisfied(watch, { ...server, last_players: null }, [], now)).toBe(false); expect(watchSatisfied(watch, { ...server, last_players: 0, last_seen_at: iso(-46_000) }, [], now)).toBe(false); }); it('only completes the watched operation on explicit success', () => { const watch = { id: 'op', server: server.name, kind: 'operation' as const, operationKey: operation.key, href: '/' }; expect(watchSatisfied(watch, server, [], now)).toBe(false); expect(watchSatisfied(watch, server, [{ ...operation, state: 'attention' }], now)).toBe(false); expect(watchSatisfied(watch, server, [{ ...operation, key: 'different', state: 'done' }], now)).toBe(false); expect(watchSatisfied(watch, undefined, [{ ...operation, state: 'done' }], now)).toBe(true); }); }); describe('persisted Fleet operations', () => { let fixture: Fixture; beforeEach(async () => { vi.resetModules(); fixture = await newFixture(); }); afterEach(async () => { await fixture.cleanup(); }); it('recovers waiting and recovering campaigns without needing a live event', async () => { const { db, closeDb } = await import('../../src/lib/server/db/index.js'); const handle = db(); handle.prepare("INSERT INTO servers (name, komodo_name) VALUES ('server', 'server')").run(); handle.prepare("INSERT INTO deployments (id, action, started_at) VALUES ('one', 'promote', ?)").run(iso(-2 * 86_400_000)); handle.prepare("INSERT INTO deployment_servers (deployment_id, server, files_changed, restart_state, players) VALUES ('one', 'server', 1, 'pending', 4)").run(); const { fleetOperations } = await import('../../src/lib/server/fleet/overview.js'); expect(fleetOperations(now)).toEqual([expect.objectContaining({ label: 'Waiting for an empty lobby', state: 'active', href: '/campaigns/one' })]); handle.prepare("UPDATE deployment_servers SET restarted_at = ?, players = NULL").run(iso()); handle.prepare("INSERT INTO restart_rollout (server, requested_at) VALUES ('server', ?)").run(iso()); expect(fleetOperations(now)).toHaveLength(1); expect(fleetOperations(now)[0].label).toBe('Recovering'); closeDb(); expect(fleetOperations(now)[0].label).toBe('Recovering'); db().prepare("UPDATE deployment_servers SET restart_state = 'done', checked_at = ?").run(iso()); db().prepare('UPDATE restart_rollout SET healthy_at = ?').run(iso()); expect(fleetOperations(now)[0].state).toBe('done'); }); it('keeps lifecycle failures actionable until superseded and retains successful completions for watches', async () => { const { db } = await import('../../src/lib/server/db/index.js'); const handle = db(); const insert = handle.prepare("INSERT INTO server_deployments (id, server, action, state, stage, started_at, finished_at, triggered_by) VALUES (?, ?, ?, ?, ?, ?, ?, 'test')"); insert.run('failed', 'server', 'create', 'failed', 'Creating container', iso(-86_400_001), iso(-86_400_001)); const { fleetOperations } = await import('../../src/lib/server/fleet/overview.js'); expect(fleetOperations(now)[0].state).toBe('attention'); insert.run('retry', 'server', 'create', 'done', 'Ready', iso(), iso()); expect(fleetOperations(now)).toEqual([expect.objectContaining({ key: 'deployment:retry', state: 'done' })]); insert.run('queued', 'new-server', 'create', 'queued', 'Waiting to launch', iso(), null); expect(fleetOperations(now)).toContainEqual(expect.objectContaining({ server: 'new-server', label: 'Waiting to launch', state: 'active' })); }); it('surfaces orphaned recovery errors and beta gates', async () => { const { db } = await import('../../src/lib/server/db/index.js'); const handle = db(); handle.prepare("INSERT INTO servers (name, komodo_name) VALUES ('server', 'server')").run(); handle.prepare("INSERT INTO restart_rollout (server, requested_at, error) VALUES ('server', ?, 'RCON not answering')").run(iso()); const { fleetOperations } = await import('../../src/lib/server/fleet/overview.js'); expect(fleetOperations(now)[0]).toMatchObject({ kind: 'recovery', state: 'attention', detail: 'RCON not answering' }); handle.prepare("INSERT INTO scripts (id, name, target_rel_path) VALUES (1, 'test', 'test.gsc')").run(); handle.prepare("INSERT INTO script_versions (id, script_id, version, sha256, size, content) VALUES (1, 1, 1, 'hash', 0, X'')").run(); handle.prepare("INSERT INTO betas (id, script_id, script_version_id, trigger_kind, state) VALUES (1, 1, 1, 'ui', 'running')").run(); handle.prepare("INSERT INTO beta_servers (beta_id, server, had_original, stage) VALUES (1, 'server', 0, 'awaiting-match')").run(); expect(fleetOperations(now)).toContainEqual(expect.objectContaining({ kind: 'beta', label: 'Observing a match', href: '/betas/1' })); expect(fleetOperations(now)).toContainEqual(expect.objectContaining({ kind: 'recovery', state: 'attention' })); }); });