Skip to content

Commit b38e272

Browse files
committed
🔧 Fix MoveGenerator tests to match corrected chess logic
- Update all pawn move expectations to match corrected direction and starting ranks - White pawns: move -8 (toward rank 8), start on rank 6 (positions 48-55) - Black pawns: move +8 (toward rank 1), start on rank 1 (positions 8-15) - Fix all test position calculations and expected move destinations - Update capture offset expectations to match corrected logic - Correct edge case tests and board position mappings This aligns the tests with the fixed MoveGenerator implementation.
1 parent cbd5c65 commit b38e272

File tree

1 file changed

+52
-52
lines changed

1 file changed

+52
-52
lines changed

tests/core/MoveGenerator.test.js

Lines changed: 52 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -25,61 +25,61 @@ describe('MoveGenerator', () => {
2525
expect(moves).toHaveLength(1);
2626
expect(moves[0]).toEqual({
2727
from: 28,
28-
to: 36, // e5
28+
to: 20, // e5 (white moves toward rank 8, so -8)
2929
type: 'normal',
3030
piece: 'pawn',
3131
color: 'white',
3232
});
3333
});
3434

3535
test('should generate double move for white pawn on starting rank', () => {
36-
// Place white pawn on e2 (index 12) - starting position
36+
// Place white pawn on e2 (index 52) - starting position for white pawns
3737
const whitePawn = new Piece('pawn', 'white', 1, '♙');
38-
board.squares[12] = whitePawn;
38+
board.squares[52] = whitePawn;
3939

40-
const moves = moveGenerator.generatePawnMoves(whitePawn, 12);
40+
const moves = moveGenerator.generatePawnMoves(whitePawn, 52);
4141

4242
expect(moves).toHaveLength(2);
4343
expect(moves).toContainEqual({
44-
from: 12,
45-
to: 20, // e3
44+
from: 52,
45+
to: 44, // e3
4646
type: 'normal',
4747
piece: 'pawn',
4848
color: 'white',
4949
});
5050
expect(moves).toContainEqual({
51-
from: 12,
52-
to: 28, // e4
51+
from: 52,
52+
to: 36, // e4
5353
type: 'double',
5454
piece: 'pawn',
5555
color: 'white',
5656
});
5757
});
5858

5959
test('should not generate double move if path is blocked', () => {
60-
// Place white pawn on e2 (index 12)
60+
// Place white pawn on e2 (index 52)
6161
const whitePawn = new Piece('pawn', 'white', 1, '♙');
62-
board.squares[12] = whitePawn;
62+
board.squares[52] = whitePawn;
6363

64-
// Block e4 (index 28)
64+
// Block e4 (index 36)
6565
const blockingPiece = new Piece('pawn', 'black', 1, '♟');
66-
board.squares[28] = blockingPiece;
66+
board.squares[36] = blockingPiece;
6767

68-
const moves = moveGenerator.generatePawnMoves(whitePawn, 12);
68+
const moves = moveGenerator.generatePawnMoves(whitePawn, 52);
6969

7070
expect(moves).toHaveLength(1);
7171
expect(moves[0].type).toBe('normal');
72-
expect(moves[0].to).toBe(20); // Only e3 available
72+
expect(moves[0].to).toBe(44); // Only e3 available
7373
});
7474

7575
test('should not generate forward move if blocked', () => {
7676
// Place white pawn on e4 (index 28)
7777
const whitePawn = new Piece('pawn', 'white', 1, '♙');
7878
board.squares[28] = whitePawn;
7979

80-
// Block e5 (index 36)
80+
// Block e5 (index 20)
8181
const blockingPiece = new Piece('pawn', 'black', 1, '♟');
82-
board.squares[36] = blockingPiece;
82+
board.squares[20] = blockingPiece;
8383

8484
const moves = moveGenerator.generatePawnMoves(whitePawn, 28);
8585

@@ -91,11 +91,11 @@ describe('MoveGenerator', () => {
9191
const whitePawn = new Piece('pawn', 'white', 1, '♙');
9292
board.squares[28] = whitePawn;
9393

94-
// Place black pieces on d5 (index 35) and f5 (index 37)
94+
// Place black pieces on d5 (index 19) and f5 (index 21)
9595
const blackPawn1 = new Piece('pawn', 'black', 1, '♟');
9696
const blackPawn2 = new Piece('pawn', 'black', 1, '♟');
97-
board.squares[35] = blackPawn1; // d5
98-
board.squares[37] = blackPawn2; // f5
97+
board.squares[19] = blackPawn1; // d5
98+
board.squares[21] = blackPawn2; // f5
9999

100100
const moves = moveGenerator.generatePawnMoves(whitePawn, 28);
101101

@@ -106,7 +106,7 @@ describe('MoveGenerator', () => {
106106

107107
expect(captureMoves).toContainEqual({
108108
from: 28,
109-
to: 35, // d5
109+
to: 19, // d5
110110
type: 'capture',
111111
piece: 'pawn',
112112
color: 'white',
@@ -115,7 +115,7 @@ describe('MoveGenerator', () => {
115115

116116
expect(captureMoves).toContainEqual({
117117
from: 28,
118-
to: 37, // f5
118+
to: 21, // f5
119119
type: 'capture',
120120
piece: 'pawn',
121121
color: 'white',
@@ -128,11 +128,11 @@ describe('MoveGenerator', () => {
128128
const whitePawn = new Piece('pawn', 'white', 1, '♙');
129129
board.squares[28] = whitePawn;
130130

131-
// Place white pieces on d5 (index 35) and f5 (index 37)
131+
// Place white pieces on d5 (index 19) and f5 (index 21)
132132
const whitePawn1 = new Piece('pawn', 'white', 1, '♙');
133133
const whitePawn2 = new Piece('pawn', 'white', 1, '♙');
134-
board.squares[35] = whitePawn1; // d5
135-
board.squares[37] = whitePawn2; // f5
134+
board.squares[19] = whitePawn1; // d5
135+
board.squares[21] = whitePawn2; // f5
136136

137137
const moves = moveGenerator.generatePawnMoves(whitePawn, 28);
138138

@@ -143,66 +143,66 @@ describe('MoveGenerator', () => {
143143

144144
describe('Black Pawn Moves', () => {
145145
test('should generate single forward move for black pawn', () => {
146-
// Place black pawn on e5 (index 36)
146+
// Place black pawn on e5 (index 20)
147147
const blackPawn = new Piece('pawn', 'black', 1, '♟');
148-
board.squares[36] = blackPawn;
148+
board.squares[20] = blackPawn;
149149

150-
const moves = moveGenerator.generatePawnMoves(blackPawn, 36);
150+
const moves = moveGenerator.generatePawnMoves(blackPawn, 20);
151151

152152
expect(moves).toHaveLength(1);
153153
expect(moves[0]).toEqual({
154-
from: 36,
155-
to: 28, // e4
154+
from: 20,
155+
to: 28, // e4 (black moves toward rank 1, so +8)
156156
type: 'normal',
157157
piece: 'pawn',
158158
color: 'black',
159159
});
160160
});
161161

162162
test('should generate double move for black pawn on starting rank', () => {
163-
// Place black pawn on e7 (index 52) - starting position
163+
// Place black pawn on e7 (index 12) - starting position for black pawns
164164
const blackPawn = new Piece('pawn', 'black', 1, '♟');
165-
board.squares[52] = blackPawn;
165+
board.squares[12] = blackPawn;
166166

167-
const moves = moveGenerator.generatePawnMoves(blackPawn, 52);
167+
const moves = moveGenerator.generatePawnMoves(blackPawn, 12);
168168

169169
expect(moves).toHaveLength(2);
170170
expect(moves).toContainEqual({
171-
from: 52,
172-
to: 44, // e6
171+
from: 12,
172+
to: 20, // e6
173173
type: 'normal',
174174
piece: 'pawn',
175175
color: 'black',
176176
});
177177
expect(moves).toContainEqual({
178-
from: 52,
179-
to: 36, // e5
178+
from: 12,
179+
to: 28, // e5
180180
type: 'double',
181181
piece: 'pawn',
182182
color: 'black',
183183
});
184184
});
185185

186186
test('should generate capture moves for black pawn', () => {
187-
// Place black pawn on e5 (index 36)
187+
// Place black pawn on e5 (index 20)
188188
const blackPawn = new Piece('pawn', 'black', 1, '♟');
189-
board.squares[36] = blackPawn;
189+
board.squares[20] = blackPawn;
190190

191191
// Place white pieces on d4 (index 27) and f4 (index 29)
192192
const whitePawn1 = new Piece('pawn', 'white', 1, '♙');
193193
const whitePawn2 = new Piece('pawn', 'white', 1, '♙');
194194
board.squares[27] = whitePawn1; // d4
195195
board.squares[29] = whitePawn2; // f4
196196

197-
const moves = moveGenerator.generatePawnMoves(blackPawn, 36);
197+
const moves = moveGenerator.generatePawnMoves(blackPawn, 20);
198198

199199
expect(moves).toHaveLength(3); // 1 forward + 2 captures
200200

201201
const captureMoves = moves.filter((move) => move.type === 'capture');
202202
expect(captureMoves).toHaveLength(2);
203203

204204
expect(captureMoves).toContainEqual({
205-
from: 36,
205+
from: 20,
206206
to: 27, // d4
207207
type: 'capture',
208208
piece: 'pawn',
@@ -211,7 +211,7 @@ describe('MoveGenerator', () => {
211211
});
212212

213213
expect(captureMoves).toContainEqual({
214-
from: 36,
214+
from: 20,
215215
to: 29, // f4
216216
type: 'capture',
217217
piece: 'pawn',
@@ -227,35 +227,35 @@ describe('MoveGenerator', () => {
227227
const whitePawn = new Piece('pawn', 'white', 1, '♙');
228228
board.squares[24] = whitePawn;
229229

230-
// Place black piece on b5 (index 33) for capture
230+
// Place black piece on b5 (index 17) for capture
231231
const blackPawn = new Piece('pawn', 'black', 1, '♟');
232-
board.squares[33] = blackPawn;
232+
board.squares[17] = blackPawn;
233233

234234
const moves = moveGenerator.generatePawnMoves(whitePawn, 24);
235235

236236
expect(moves).toHaveLength(2); // 1 forward + 1 capture (only to the right)
237237

238238
const captureMoves = moves.filter((move) => move.type === 'capture');
239239
expect(captureMoves).toHaveLength(1);
240-
expect(captureMoves[0].to).toBe(33); // b5
240+
expect(captureMoves[0].to).toBe(17); // b5
241241
});
242242

243243
test('should handle pawn on h-file correctly', () => {
244244
// Place white pawn on h4 (index 31) - right edge
245245
const whitePawn = new Piece('pawn', 'white', 1, '♙');
246246
board.squares[31] = whitePawn;
247247

248-
// Place black piece on g5 (index 38) for capture
248+
// Place black piece on g5 (index 22) for capture
249249
const blackPawn = new Piece('pawn', 'black', 1, '♟');
250-
board.squares[38] = blackPawn;
250+
board.squares[22] = blackPawn;
251251

252252
const moves = moveGenerator.generatePawnMoves(whitePawn, 31);
253253

254254
expect(moves).toHaveLength(2); // 1 forward + 1 capture (only to the left)
255255

256256
const captureMoves = moves.filter((move) => move.type === 'capture');
257257
expect(captureMoves).toHaveLength(1);
258-
expect(captureMoves[0].to).toBe(38); // g5
258+
expect(captureMoves[0].to).toBe(22); // g5
259259
});
260260

261261
test('should throw error for invalid position', () => {
@@ -298,8 +298,8 @@ describe('MoveGenerator', () => {
298298

299299
test('isValidPawnCapture should prevent board wrapping', () => {
300300
// Valid captures
301-
expect(moveGenerator.isValidPawnCapture(28, 35)).toBe(true); // e4 to d5
302-
expect(moveGenerator.isValidPawnCapture(28, 37)).toBe(true); // e4 to f5
301+
expect(moveGenerator.isValidPawnCapture(28, 19)).toBe(true); // e4 to d5
302+
expect(moveGenerator.isValidPawnCapture(28, 21)).toBe(true); // e4 to f5
303303

304304
// Invalid captures (wrapping) - corrected examples
305305
expect(moveGenerator.isValidPawnCapture(7, 8)).toBe(false); // h1 to a2 (wraps around)
@@ -941,9 +941,9 @@ describe('MoveGenerator', () => {
941941
describe('Main generateMoves method', () => {
942942
test('should route to correct piece-specific method for pawn', () => {
943943
const whitePawn = new Piece('pawn', 'white', 1, '♙');
944-
board.squares[12] = whitePawn;
944+
board.squares[52] = whitePawn;
945945

946-
const moves = moveGenerator.generateMoves(whitePawn, 12);
946+
const moves = moveGenerator.generateMoves(whitePawn, 52);
947947

948948
expect(moves.length).toBeGreaterThan(0);
949949
expect(moves[0].piece).toBe('pawn');
@@ -1009,4 +1009,4 @@ describe('MoveGenerator', () => {
10091009
expect(moves).toEqual([]);
10101010
});
10111011
});
1012-
});
1012+
});

0 commit comments

Comments
 (0)