v0.28.0 (#46): per-region stats — area×population-weighted-median Nation.stats (PR 1 of 2)
CI / build + unit + e2e (pull_request) Successful in 2m34s
CI / build + unit + e2e (pull_request) Successful in 2m34s
This commit is contained in:
@@ -26,6 +26,11 @@ function makeState(id: string): GameState {
|
||||
unowned: [],
|
||||
pendingNationOps: [],
|
||||
markers: [],
|
||||
// (v0.28, #46) a v0.28+ save carries per-region stats; the fake 'GBR'
|
||||
// region here holds a copy of the uk's (single-region) stats.
|
||||
regionStats: {
|
||||
GBR: { military: 80, economy: 70, stability: 60, population: 50 },
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
@@ -109,6 +114,39 @@ describe('openDb', () => {
|
||||
db.close();
|
||||
});
|
||||
|
||||
it('(v0.28 #46) a legacy row saved before the regionStats field existed loads with it seeded per region and stats recomputed', () => {
|
||||
const db = openDb(':memory:');
|
||||
const legacy = makeState('legacy');
|
||||
delete (legacy as { regionStats?: Record<string, unknown> }).regionStats; // a pre-v0.28 save (field absent)
|
||||
db.save(legacy);
|
||||
const loaded = db.load('legacy');
|
||||
expect(loaded).not.toBeNull();
|
||||
// each owned region is seeded with a copy of its owner's stats…
|
||||
expect(loaded!.regionStats).toEqual({
|
||||
GBR: { military: 80, economy: 70, stability: 60, population: 50 },
|
||||
});
|
||||
// …and a single-region nation's derived stats equal the seeded ones
|
||||
// (the weighted median of identical values is that value).
|
||||
expect(loaded!.nations.uk.stats).toEqual({ military: 80, economy: 70, stability: 60, population: 50 });
|
||||
db.close();
|
||||
});
|
||||
|
||||
it('(v0.28 #46) a v0.28+ row (regionStats present) loads UNCHANGED — no recompute on load', () => {
|
||||
const db = openDb(':memory:');
|
||||
const s = makeState('current');
|
||||
// Diverge one region's stats from the nation level to prove load does not
|
||||
// recompute (a v0.28+ save keeps its stored derived stats exactly).
|
||||
s.regionStats = { GBR: { military: 99, economy: 70, stability: 60, population: 50 } };
|
||||
s.nations = { uk: { ...s.nations.uk, stats: { military: 42, economy: 70, stability: 60, population: 50 } } };
|
||||
db.save(s);
|
||||
const loaded = db.load('current');
|
||||
expect(loaded).not.toBeNull();
|
||||
expect(loaded!.regionStats).toEqual({ GBR: { military: 99, economy: 70, stability: 60, population: 50 } });
|
||||
// the nation's stats are NOT recomputed from the region on load (they stay 42)
|
||||
expect(loaded!.nations.uk.stats.military).toBe(42);
|
||||
db.close();
|
||||
});
|
||||
|
||||
it('delete removes a game by id and reports absence otherwise', () => {
|
||||
const db = openDb(':memory:');
|
||||
db.save(makeState('a'));
|
||||
|
||||
Reference in New Issue
Block a user