From 0fa09156df577f5632c09c70cbec9a43614581f8 Mon Sep 17 00:00:00 2001 From: Dhanjit Das Date: Thu, 1 Jan 2026 10:44:28 +0530 Subject: [PATCH] Test: Expand AI test suite with chain reactions and full game simulation --- tests/ai.test.js | 60 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/tests/ai.test.js b/tests/ai.test.js index 5305431b..671aa48d 100644 --- a/tests/ai.test.js +++ b/tests/ai.test.js @@ -62,4 +62,64 @@ describe('DotDot AI Logic', () => { expect(isDangerousH).toBe(false); expect(isDangerousV).toBe(false); }); + + test('AI takes multiple squares if possible (Chain Reaction)', () => { + // Setup: Two squares side-by-side that can be chained. + // Sq(0,0) needs H(1,0). + // Sq(0,1) needs H(1,1). + + game.horizontalLines[0][0] = true; + game.verticalLines[0][0] = true; + game.verticalLines[0][1] = true; + + game.horizontalLines[0][1] = true; + game.verticalLines[0][2] = true; + + // If we place H(1,0), we complete Sq(0,0). + // Does AI see both? Yes, `findScoringMoves` returns all. + + const move = ai.getMove(game); + expect(ai.doesMoveCompleteSquare(game, move)).toBe(true); + }); + + test('Full Game Simulation (AI vs Random)', () => { + // Ensure AI doesn't crash or freeze throughout a whole game. + const simGame = new GameState(5, 5); // 5x5 grid + let moves = 0; + const maxMoves = 1000; // Safety break + + while (!simGame.gameOver && moves < maxMoves) { + moves++; + const player = simGame.getCurrentPlayer(); + let move; + + if (player === 'P1') { + // Random player logic for test + const available = ai.getAllAvailableMoves(simGame); + // Simple random move if available + if (available.length > 0) { + move = available[Math.floor(Math.random() * available.length)]; + } else { + break; // Game over or error + } + } else { + // AI + move = ai.getMove(simGame); + } + + if (!move) break; + + const result = simGame.placeLine(move.type, move.r, move.c); + if (!result.success) { + // This might happen if AI returns a move that is already taken? + // Should not happen with valid AI. + throw new Error(`Invalid move generated by ${player}: ${JSON.stringify(move)}`); + } + } + + expect(simGame.gameOver).toBe(true); + // Scores check? + const total = simGame.scores['P1'] + simGame.scores['P2']; + expect(total).toBe(16); // 4x4 squares = 16 + }); });