fix(bot): finalize game on bot checkmate; harden driver dispatch

Extract endGame/finalizeIfEnded to game-end.ts so driver.ts can call
finalizeIfEnded after an applied move (fix: bot checkmate was not
setting game.status='finished'). Wrap entire dispatch() call in
try/catch for exception safety. Move lastSeenAnnouncementCount advance
to after successful dispatch so retry attempts see FSM rejection
announcements. Add checkmate-finalize test; lock retry-cap at 5 calls.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
claude (blind_chess)
2026-04-28 14:04:22 -04:00
parent 3798b9c00d
commit 4407110147
4 changed files with 59 additions and 26 deletions
@@ -127,6 +127,7 @@ describe('BotDriver', () => {
expect(game.status).toBe('finished');
expect(game.endReason).toBe('resign');
expect(game.winner).toBe('w');
expect(brain.decide).toHaveBeenCalledTimes(5);
});
it('respond-draw: when drawOffer is from opponent, driver fires decide and dispatches', async () => {
@@ -145,4 +146,24 @@ describe('BotDriver', () => {
expect(brain.decide).not.toHaveBeenCalled();
expect(brain.dispose).toHaveBeenCalled();
});
it('bot move that delivers checkmate finalizes game.status', async () => {
// FEN: '1k6/8/1K6/8/8/8/8/7Q w - - 0 1'
// White king b6, white queen h1, black king b8.
// Qh8# is mate: queen moves h1→h8, covers h8; white king b6 covers a7,b7,c7,a5,b5,c5.
// Black king b8 escape squares (a7,b7,c7,a8,c8) are all covered. Verified with chess.js.
const fen = '1k6/8/1K6/8/8/8/8/7Q w - - 0 1';
game = makeGame({ fen });
game.aiOpponent = { color: 'w', brain: 'casual' };
brain = new StubBrain();
driver = new BotDriver({ game, brain, color: 'w' });
await driver.init();
brain.enqueue({ type: 'commit', from: 'h1', to: 'h8' });
await driver.onStateChange();
expect(game.status).toBe('finished');
expect(game.endReason).toBe('checkmate');
expect(game.winner).toBe('w');
});
});