Add persistent match history to server pages

This commit is contained in:
2026-09-20 21:35:35 -04:00
parent 070e7fe9f7
commit fc681c56c9
9 changed files with 170 additions and 1 deletions
+36
View File
@@ -34,6 +34,42 @@ async function matches() {
}>;
}
it('records server match counts, unique rejoins, duration, and final round durably', async () => {
await report('game_start', 0, [player('a'), player('b')]);
await report('round_change', 2, [player('b'), player('c')]);
await report('heartbeat', 2, [player('a'), player('c')]);
await report('game_end', 7, [player('c')]);
await report('game_end', 7, [player('c')]);
const { closeDb } = await import('../../src/lib/server/db/index.js');
closeDb();
const { serverMatchHistory } = await import('../../src/lib/server/fleet/match-history.js');
expect(serverMatchHistory(server)).toEqual({ total: 1, matches: [expect.objectContaining({
status: 'completed', startPlayers: 2, uniquePlayers: 3, endPlayers: 1,
durationMs: 3000, finalRound: 7
})] });
expect(serverMatchHistory('other')).toEqual({ total: 0, matches: [] });
});
it('preserves unknown boundaries and distinguishes empty rosters from missing observations', async () => {
await report('heartbeat', 5);
await report('game_end', 6, []);
await report('game_start', 0, []);
await report('round_change', 2);
await report('game_start');
const { serverMatchHistory } = await import('../../src/lib/server/fleet/match-history.js');
expect(serverMatchHistory(server).matches).toEqual([
expect.objectContaining({ status: 'active', startPlayers: 1, endPlayers: null, durationMs: null, finalRound: null }),
expect.objectContaining({ status: 'interrupted', startPlayers: 0, endPlayers: null, durationMs: null, finalRound: null }),
expect.objectContaining({ status: 'completed', startPlayers: null, uniquePlayers: 1, endPlayers: 0, durationMs: null, finalRound: 6 })
]);
const { GET } = await import('../../src/routes/api/servers/[name]/matches/+server.js');
const response = GET({ params: { name: server }, url: new URL('http://localhost/?limit=1') });
const body = await response.json();
expect(body.total).toBe(3);
expect(body.matches).toHaveLength(1);
expect(GET({ params: { name: server }, url: new URL('http://localhost/?limit=-1') }).status).toBe(400);
});
async function records(guid = '2741626') {
const { db } = await import('../../src/lib/server/db/index.js');
return db().prepare('SELECT * FROM leaderboard_match_players WHERE guid = ? ORDER BY match_id').all(guid) as Array<{