0.4.0 — fix queue-add race + per-item remove button (#10, #11)

This commit is contained in:
2026-10-11 16:09:29 +00:00
parent 3287bcaca4
commit ef27b59762
14 changed files with 448 additions and 29 deletions
+63 -1
View File
@@ -9,6 +9,7 @@ import {
isMeaningful,
parseTurnResult,
queueRoundItem,
removeRoundItem,
runTurn,
validateTurnResult,
} from './engine';
@@ -72,6 +73,7 @@ function baseState(): GameState {
},
events: [],
pending: [],
version: 1,
};
}
@@ -206,8 +208,11 @@ describe('runTurn (batched round, deterministic stub)', () => {
expect(state.events[0].nation).toBe('uk');
expect(state.events[0].outcome).toBe('success');
expect(result.stateChanges).toEqual([{ nation: 'uk', stat: 'military', op: 'set', value: 85 }]);
// committing a turn is a new revision too
expect(state.version).toBe(2);
// the original object is not mutated (a fresh queue was consumed)
expect(original.pending).toHaveLength(1);
expect(original.version).toBe(1);
});
it('an empty round: turn advances, one political "world moves on" event, pending stays empty', async () => {
@@ -286,6 +291,61 @@ describe('queueRoundItem', () => {
expect(after.pending[0]).toEqual({ kind: 'diplomacy', to: 'us', text: 'hello' });
// the original state is not mutated
expect(before.pending).toHaveLength(0);
expect(before.version).toBe(1);
});
it('bumps the monotonic version on each queue mutation', () => {
const a = queueRoundItem(baseState(), { kind: 'action', text: 'one' });
expect(a.version).toBe(2);
const b = queueRoundItem(a, { kind: 'action', text: 'two' });
expect(b.version).toBe(3);
// and never backwards: two consecutive adds from the same base both
// advance past it, so a late snapshot of the older state is always older.
const b2 = queueRoundItem(baseState(), { kind: 'action', text: 'two' });
expect(b2.version).toBe(2);
expect(b.version).toBeGreaterThan(b2.version);
});
it('treats a legacy state without a version field as version 0', () => {
const legacy = { ...baseState() } as Omit<GameState, 'version'> & { version?: number };
delete legacy.version; // a row saved before the version field existed
// simulate a legacy row coming back from SQLite: the JSON has no field.
const after = queueRoundItem(legacy as unknown as GameState, {
kind: 'action',
text: 'first after upgrade',
});
expect(after.version).toBe(1);
});
});
describe('removeRoundItem', () => {
it('removes the item at the index, bumps the version, and leaves the input untouched', () => {
const state = baseState();
state.pending = [
{ kind: 'action', text: 'one' },
{ kind: 'diplomacy', to: 'us', text: 'two' },
{ kind: 'action', text: 'three' },
];
const next = removeRoundItem(state, 1);
expect(next).not.toBeNull();
expect(next!.version).toBe(2);
expect(next!.pending).toEqual([
{ kind: 'action', text: 'one' },
{ kind: 'action', text: 'three' },
]);
expect(state.pending).toHaveLength(3); // original untouched
expect(state.version).toBe(1);
});
it('returns null for out-of-bounds or non-integer indices (route answers 400, nothing mutates)', () => {
const state = baseState();
state.pending = [{ kind: 'action', text: 'one' }];
expect(removeRoundItem(state, -1)).toBeNull();
expect(removeRoundItem(state, 1)).toBeNull(); // == length
expect(removeRoundItem(state, 5)).toBeNull();
expect(removeRoundItem(state, 0.5)).toBeNull();
expect(removeRoundItem(state, Number.NaN)).toBeNull();
expect(state.pending).toHaveLength(1); // still untouched
});
});
@@ -346,7 +406,7 @@ describe('applyTurnResult', () => {
expect(JSON.stringify(state)).toBe(snapshot);
});
it('clears the pending round on commit', () => {
it('clears the pending round on commit and bumps the version', () => {
const state = baseState();
state.pending = [{ kind: 'action', text: 'x' }];
const result: TurnResult = {
@@ -358,7 +418,9 @@ describe('applyTurnResult', () => {
};
const next = applyTurnResult(state, result);
expect(next.pending).toEqual([]);
expect(next.version).toBe(2);
expect(state.pending).toHaveLength(1); // original untouched
expect(state.version).toBe(1);
});
});