/** * @jest-environment jsdom */ import { Rook } from '../../../js/pieces/Rook.js'; import { Board } from '../../../js/game/Board.js'; describe('Rook', () => { let board; beforeEach(() => { board = new Board(); board.clear(); }); describe('Straight Line Movement', () => { test('rook in center can move to 14 squares', () => { const rook = new Rook('white', { row: 4, col: 4 }); board.setPiece(4, 4, rook); const moves = rook.getValidMoves(board); // 7 horizontal + 7 vertical = 14 squares expect(moves).toHaveLength(14); }); test('rook moves only horizontally or vertically', () => { const rook = new Rook('white', { row: 4, col: 4 }); board.setPiece(4, 4, rook); const moves = rook.getValidMoves(board); moves.forEach(move => { const rowSame = move.row === 4; const colSame = move.col === 4; // Must be same row OR same column, not both expect(rowSame || colSame).toBe(true); expect(rowSame && colSame).toBe(false); }); }); test('rook can move entire row', () => { const rook = new Rook('white', { row: 4, col: 4 }); board.setPiece(4, 4, rook); const moves = rook.getValidMoves(board); // All columns in row 4 for (let col = 0; col < 8; col++) { if (col !== 4) { expect(moves).toContainEqual({ row: 4, col }); } } }); test('rook can move entire column', () => { const rook = new Rook('white', { row: 4, col: 4 }); board.setPiece(4, 4, rook); const moves = rook.getValidMoves(board); // All rows in column 4 for (let row = 0; row < 8; row++) { if (row !== 4) { expect(moves).toContainEqual({ row, col: 4 }); } } }); test('rook in corner has 14 moves', () => { const rook = new Rook('white', { row: 0, col: 0 }); board.setPiece(0, 0, rook); const moves = rook.getValidMoves(board); expect(moves).toHaveLength(14); // 7 right + 7 down }); }); describe('Blocking and Obstacles', () => { test('rook blocked by own piece', () => { const rook = new Rook('white', { row: 4, col: 4 }); const blockingPawn = { type: 'pawn', color: 'white', position: { row: 4, col: 6 } }; board.setPiece(4, 4, rook); board.setPiece(4, 6, blockingPawn); const moves = rook.getValidMoves(board); // Can move to (4,5) but not (4,6) or beyond expect(moves).toContainEqual({ row: 4, col: 5 }); expect(moves).not.toContainEqual({ row: 4, col: 6 }); expect(moves).not.toContainEqual({ row: 4, col: 7 }); }); test('rook can capture opponent piece but not move beyond', () => { const rook = new Rook('white', { row: 4, col: 4 }); const opponentPawn = { type: 'pawn', color: 'black', position: { row: 4, col: 6 } }; board.setPiece(4, 4, rook); board.setPiece(4, 6, opponentPawn); const moves = rook.getValidMoves(board); expect(moves).toContainEqual({ row: 4, col: 5 }); expect(moves).toContainEqual({ row: 4, col: 6 }); // Can capture expect(moves).not.toContainEqual({ row: 4, col: 7 }); }); test('rook cannot jump over pieces', () => { const rook = new Rook('white', { row: 4, col: 4 }); board.setPiece(4, 4, rook); board.setPiece(4, 2, { type: 'pawn', color: 'white', position: { row: 4, col: 2 } }); board.setPiece(2, 4, { type: 'pawn', color: 'black', position: { row: 2, col: 4 } }); const moves = rook.getValidMoves(board); // Left: can't reach column 2 or beyond expect(moves).toContainEqual({ row: 4, col: 3 }); expect(moves).not.toContainEqual({ row: 4, col: 2 }); expect(moves).not.toContainEqual({ row: 4, col: 1 }); // Up: can capture at row 2 but not beyond expect(moves).toContainEqual({ row: 3, col: 4 }); expect(moves).toContainEqual({ row: 2, col: 4 }); expect(moves).not.toContainEqual({ row: 1, col: 4 }); }); }); describe('Castling Rights', () => { test('rook tracks if it has moved', () => { const rook = new Rook('white', { row: 7, col: 0 }); expect(rook.hasMoved).toBe(false); board.setPiece(7, 0, rook); board.movePiece(7, 0, 7, 1); expect(rook.hasMoved).toBe(true); }); test('unmoved rook can participate in castling', () => { const rook = new Rook('white', { row: 7, col: 7 }); expect(rook.hasMoved).toBe(false); expect(rook.canCastle()).toBe(true); }); test('moved rook cannot castle', () => { const rook = new Rook('white', { row: 7, col: 7 }); board.setPiece(7, 7, rook); board.movePiece(7, 7, 7, 6); expect(rook.hasMoved).toBe(true); expect(rook.canCastle()).toBe(false); }); }); describe('Capture Mechanics', () => { test('rook can capture opponent pieces in all directions', () => { const rook = new Rook('white', { row: 4, col: 4 }); board.setPiece(4, 4, rook); board.setPiece(4, 6, { type: 'pawn', color: 'black', position: { row: 4, col: 6 } }); board.setPiece(4, 2, { type: 'knight', color: 'black', position: { row: 4, col: 2 } }); board.setPiece(2, 4, { type: 'bishop', color: 'black', position: { row: 2, col: 4 } }); board.setPiece(6, 4, { type: 'queen', color: 'black', position: { row: 6, col: 4 } }); const moves = rook.getValidMoves(board); expect(moves).toContainEqual({ row: 4, col: 6 }); expect(moves).toContainEqual({ row: 4, col: 2 }); expect(moves).toContainEqual({ row: 2, col: 4 }); expect(moves).toContainEqual({ row: 6, col: 4 }); }); test('rook cannot capture own pieces', () => { const rook = new Rook('white', { row: 4, col: 4 }); const whitePawn = { type: 'pawn', color: 'white', position: { row: 4, col: 6 } }; board.setPiece(4, 4, rook); board.setPiece(4, 6, whitePawn); const moves = rook.getValidMoves(board); expect(moves).not.toContainEqual({ row: 4, col: 6 }); }); }); describe('Board Boundaries', () => { test('rook respects board edges', () => { const rook = new Rook('white', { row: 4, col: 4 }); board.setPiece(4, 4, rook); const moves = rook.getValidMoves(board); moves.forEach(move => { expect(move.row).toBeGreaterThanOrEqual(0); expect(move.row).toBeLessThan(8); expect(move.col).toBeGreaterThanOrEqual(0); expect(move.col).toBeLessThan(8); }); }); test('rook on all edges has correct move count', () => { // Top edge const rook1 = new Rook('white', { row: 0, col: 4 }); board.setPiece(0, 4, rook1); expect(rook1.getValidMoves(board)).toHaveLength(14); // Right edge board.clear(); const rook2 = new Rook('white', { row: 4, col: 7 }); board.setPiece(4, 7, rook2); expect(rook2.getValidMoves(board)).toHaveLength(14); }); }); describe('Initial Position', () => { test('rooks on initial board have no moves', () => { board = new Board(); board.setupInitialPosition(); const whiteRook1 = board.getPiece(7, 0); const whiteRook2 = board.getPiece(7, 7); expect(whiteRook1.type).toBe('rook'); expect(whiteRook2.type).toBe('rook'); // Blocked by pawns and knights expect(whiteRook1.getValidMoves(board)).toHaveLength(0); expect(whiteRook2.getValidMoves(board)).toHaveLength(0); }); test('rook can move after pieces clear', () => { board = new Board(); board.setupInitialPosition(); // Remove knight to open path board.setPiece(7, 1, null); const whiteRook = board.getPiece(7, 0); const moves = whiteRook.getValidMoves(board); expect(moves.length).toBeGreaterThan(0); }); }); });