import Database from 'better-sqlite3'; import { expect, it } from 'vitest'; import { migratePlayerScoreTotal } from '../../src/lib/server/db/player-score-total.js'; import { runMigrations } from '../../src/lib/server/db/migration-runner.js'; import { migrations } from '../../src/lib/server/db/migrations.js'; it('upgrades old balances to unknown totals and preserves totals on migration replay', () => { const handle = new Database(':memory:'); try { const migration = migrations.find( (entry) => entry.name === 'cumulative player score per match' )!; runMigrations(handle, migrations.slice(0, migration.version - 1)); handle.exec(`INSERT INTO leaderboard_matches (id,server,game,map,map_key,map_name,started_at,last_received_at,last_event,last_fingerprint) VALUES (1,'server','t5','zombie_theater','t5:zombie_theater','Kino','start','end','game_end','old'); INSERT INTO leaderboard_match_players (match_id,guid,best_score,first_seen_at,last_seen_at) VALUES (1,'ace',32000,'start','end')`); runMigrations(handle, migrations); expect( handle.prepare('SELECT best_score,score_total FROM leaderboard_match_players').get() ).toEqual({ best_score: 32000, score_total: null }); handle.exec('UPDATE leaderboard_match_players SET score_total=65000'); migratePlayerScoreTotal(handle); expect( handle.prepare('SELECT best_score,score_total FROM leaderboard_match_players').get() ).toEqual({ best_score: 32000, score_total: 65000 }); for (const value of [-1, 1.5, 'bad']) expect(() => handle.prepare('UPDATE leaderboard_match_players SET score_total=?').run(value) ).toThrow('CHECK constraint failed'); expect(handle.pragma('foreign_key_check')).toEqual([]); } finally { handle.close(); } });