Files
History/server/db.test.ts
kamilkrzywda 17d6cecab9
CI / build + unit + e2e (pull_request) Successful in 1m14s
feat: remove a saved game & redirect on missing/deleted game (#20)
- DELETE /api/games/:id route + Db.delete() (404 when absent)
- PresetPicker: per-saved-game trash button + confirm dialog (Cancel/Delete)
- App: runDeleteGame; useSse onError -> goHome on 'deleted'/'not found'
  (a deleted/missing game now returns the live tab and any deep link home)
- DeleteGameResponse in shared/protocol.ts; api.deleteGame()
- unit test (Db.delete) + 2 e2e tests (home-screen delete; in-flight delete
  returns the active tab home and a fresh deep link lands on home)
- version 0.7.0 -> 0.8.0 + changelog

Fixes #20
2026-10-11 20:32:55 +00:00

91 lines
2.4 KiB
TypeScript

import { describe, expect, it } from 'vitest';
import { openDb } from './db';
import type { GameState } from '../shared/types';
function makeState(id: string): GameState {
return {
id,
presetId: 'p',
presetTitle: 'P',
playerNationId: 'uk',
turn: 1,
date: '1939-09-01',
nations: {
uk: {
id: 'uk',
name: 'UK',
type: 'major',
stats: { military: 80, economy: 70, stability: 60, population: 50 },
territory: ['GBR'],
relations: {},
},
},
events: [],
pending: [],
version: 1,
};
}
describe('openDb', () => {
it('save/load round-trips a game by id and upserts on re-save', () => {
const db = openDb(':memory:');
db.save(makeState('a'));
db.save(makeState('b'));
expect(db.load('a')?.id).toBe('a');
expect(db.load('b')?.id).toBe('b');
expect(db.load('nope')).toBeNull();
expect([...db.listIds()].sort()).toEqual(['a', 'b']);
// re-saving the same id updates in place (no duplicate)
const s = makeState('a');
s.turn = 5;
s.events.push({
id: 'e1',
turn: 5,
date: '1939-10-01',
nation: 'uk',
type: 'military',
title: 't',
text: 'b',
outcome: 'success',
});
db.save(s);
expect(db.load('a')?.turn).toBe(5);
expect(db.load('a')?.events).toHaveLength(1);
expect([...db.listIds()].sort()).toEqual(['a', 'b']); // still two rows
// the monotonic version is persisted with the state and round-trips
s.version = 3;
db.save(s);
expect(db.load('a')?.version).toBe(3);
db.close();
});
it('loads null for a malformed row instead of throwing', () => {
const db = openDb(':memory:');
const d = (db as unknown as { raw?: unknown });
// Force a corrupt row through the underlying driver is not exposed here;
// instead assert a fresh empty DB returns null for an unknown id.
expect(d).toBeDefined();
expect(db.load('missing')).toBeNull();
db.close();
});
it('delete removes a game by id and reports absence otherwise', () => {
const db = openDb(':memory:');
db.save(makeState('a'));
db.save(makeState('b'));
expect(db.delete('nope')).toBe(false); // unknown id
expect(db.delete('a')).toBe(true); // removes a
expect(db.load('a')).toBeNull();
expect(db.load('b')?.id).toBe('b');
expect([...db.listIds()].sort()).toEqual(['b']);
expect(db.delete('a')).toBe(false); // already gone
db.close();
});
});