/** * @jest-environment jsdom */ import { Bishop } from '../../../js/pieces/Bishop.js'; import { Board } from '../../../js/game/Board.js'; describe('Bishop', () => { let board; beforeEach(() => { board = new Board(); board.clear(); }); describe('Diagonal Movement', () => { test('bishop in center can move to 13 squares', () => { const bishop = new Bishop('white', { row: 4, col: 4 }); board.setPiece(4, 4, bishop); const moves = bishop.getValidMoves(board); // 4 diagonals: 4+3+3+3 = 13 squares expect(moves).toHaveLength(13); }); test('bishop moves only diagonally', () => { const bishop = new Bishop('white', { row: 4, col: 4 }); board.setPiece(4, 4, bishop); const moves = bishop.getValidMoves(board); moves.forEach(move => { const rowDiff = Math.abs(move.row - 4); const colDiff = Math.abs(move.col - 4); // Diagonal means row and column difference are equal expect(rowDiff).toBe(colDiff); }); }); test('bishop can move to all four diagonal directions', () => { const bishop = new Bishop('white', { row: 4, col: 4 }); board.setPiece(4, 4, bishop); const moves = bishop.getValidMoves(board); // Check all four diagonals are represented const upLeft = moves.some(m => m.row < 4 && m.col < 4); const upRight = moves.some(m => m.row < 4 && m.col > 4); const downLeft = moves.some(m => m.row > 4 && m.col < 4); const downRight = moves.some(m => m.row > 4 && m.col > 4); expect(upLeft).toBe(true); expect(upRight).toBe(true); expect(downLeft).toBe(true); expect(downRight).toBe(true); }); test('bishop in corner has 7 moves', () => { const bishop = new Bishop('white', { row: 0, col: 0 }); board.setPiece(0, 0, bishop); const moves = bishop.getValidMoves(board); expect(moves).toHaveLength(7); // One diagonal from corner }); test('bishop on edge has limited moves', () => { const bishop = new Bishop('white', { row: 0, col: 3 }); board.setPiece(0, 3, bishop); const moves = bishop.getValidMoves(board); // Can move along two diagonals expect(moves.length).toBeGreaterThan(0); moves.forEach(move => { expect(move.row).toBeGreaterThan(0); // All moves go down from top edge }); }); }); describe('Blocking and Obstacles', () => { test('bishop blocked by own piece', () => { const bishop = new Bishop('white', { row: 4, col: 4 }); const blockingPawn = { type: 'pawn', color: 'white', position: { row: 2, col: 2 } }; board.setPiece(4, 4, bishop); board.setPiece(2, 2, blockingPawn); const moves = bishop.getValidMoves(board); // Can move to (3,3) but not (2,2) or beyond expect(moves).toContainEqual({ row: 3, col: 3 }); expect(moves).not.toContainEqual({ row: 2, col: 2 }); expect(moves).not.toContainEqual({ row: 1, col: 1 }); expect(moves).not.toContainEqual({ row: 0, col: 0 }); }); test('bishop blocked by opponent piece but can capture', () => { const bishop = new Bishop('white', { row: 4, col: 4 }); const opponentPawn = { type: 'pawn', color: 'black', position: { row: 2, col: 2 } }; board.setPiece(4, 4, bishop); board.setPiece(2, 2, opponentPawn); const moves = bishop.getValidMoves(board); // Can move to (3,3) and capture at (2,2) but not beyond expect(moves).toContainEqual({ row: 3, col: 3 }); expect(moves).toContainEqual({ row: 2, col: 2 }); // Can capture expect(moves).not.toContainEqual({ row: 1, col: 1 }); }); test('bishop cannot jump over pieces', () => { const bishop = new Bishop('white', { row: 4, col: 4 }); board.setPiece(4, 4, bishop); board.setPiece(3, 3, { type: 'pawn', color: 'black', position: { row: 3, col: 3 } }); board.setPiece(3, 5, { type: 'pawn', color: 'white', position: { row: 3, col: 5 } }); const moves = bishop.getValidMoves(board); // Can capture at (3,3) but not beyond expect(moves).toContainEqual({ row: 3, col: 3 }); expect(moves).not.toContainEqual({ row: 2, col: 2 }); // Cannot move to (3,5) because it's own piece expect(moves).not.toContainEqual({ row: 3, col: 5 }); expect(moves).not.toContainEqual({ row: 2, col: 6 }); }); test('multiple pieces blocking different diagonals', () => { const bishop = new Bishop('white', { row: 4, col: 4 }); board.setPiece(4, 4, bishop); board.setPiece(2, 2, { type: 'pawn', color: 'white', position: { row: 2, col: 2 } }); board.setPiece(2, 6, { type: 'pawn', color: 'black', position: { row: 2, col: 6 } }); board.setPiece(6, 2, { type: 'knight', color: 'black', position: { row: 6, col: 2 } }); const moves = bishop.getValidMoves(board); // Upper-left diagonal: blocked at (2,2) expect(moves).toContainEqual({ row: 3, col: 3 }); expect(moves).not.toContainEqual({ row: 2, col: 2 }); // Upper-right diagonal: can capture at (2,6) expect(moves).toContainEqual({ row: 3, col: 5 }); expect(moves).toContainEqual({ row: 2, col: 6 }); // Lower-left diagonal: can capture at (6,2) expect(moves).toContainEqual({ row: 5, col: 3 }); expect(moves).toContainEqual({ row: 6, col: 2 }); // Lower-right diagonal: clear expect(moves).toContainEqual({ row: 5, col: 5 }); expect(moves).toContainEqual({ row: 6, col: 6 }); expect(moves).toContainEqual({ row: 7, col: 7 }); }); }); describe('Capture Mechanics', () => { test('bishop can capture opponent pieces', () => { const bishop = new Bishop('white', { row: 4, col: 4 }); const blackPawn1 = { type: 'pawn', color: 'black', position: { row: 2, col: 2 } }; const blackPawn2 = { type: 'pawn', color: 'black', position: { row: 6, col: 6 } }; board.setPiece(4, 4, bishop); board.setPiece(2, 2, blackPawn1); board.setPiece(6, 6, blackPawn2); const moves = bishop.getValidMoves(board); expect(moves).toContainEqual({ row: 2, col: 2 }); expect(moves).toContainEqual({ row: 6, col: 6 }); }); test('bishop cannot capture own pieces', () => { const bishop = new Bishop('white', { row: 4, col: 4 }); const whitePawn = { type: 'pawn', color: 'white', position: { row: 2, col: 2 } }; board.setPiece(4, 4, bishop); board.setPiece(2, 2, whitePawn); const moves = bishop.getValidMoves(board); expect(moves).not.toContainEqual({ row: 2, col: 2 }); }); }); describe('Board Boundaries', () => { test('bishop respects board edges', () => { const bishop = new Bishop('white', { row: 4, col: 4 }); board.setPiece(4, 4, bishop); const moves = bishop.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('bishop from all corners reaches opposite corner', () => { // Top-left to bottom-right const bishop1 = new Bishop('white', { row: 0, col: 0 }); board.setPiece(0, 0, bishop1); expect(bishop1.getValidMoves(board)).toContainEqual({ row: 7, col: 7 }); // Top-right to bottom-left board.clear(); const bishop2 = new Bishop('white', { row: 0, col: 7 }); board.setPiece(0, 7, bishop2); expect(bishop2.getValidMoves(board)).toContainEqual({ row: 7, col: 0 }); }); }); describe('Color-Bound Movement', () => { test('bishop on light square can only reach light squares', () => { const bishop = new Bishop('white', { row: 0, col: 2 }); // Light square board.setPiece(0, 2, bishop); const moves = bishop.getValidMoves(board); moves.forEach(move => { // Light squares: (row + col) is even expect((move.row + move.col) % 2).toBe(0); }); }); test('bishop on dark square can only reach dark squares', () => { const bishop = new Bishop('white', { row: 0, col: 0 }); // Dark square board.setPiece(0, 0, bishop); const moves = bishop.getValidMoves(board); moves.forEach(move => { // Dark squares: (row + col) is odd expect((move.row + move.col) % 2).toBe(0); }); }); }); describe('Initial Position', () => { test('bishops on initial board have no moves', () => { board = new Board(); board.setupInitialPosition(); const whiteBishop1 = board.getPiece(7, 2); const whiteBishop2 = board.getPiece(7, 5); expect(whiteBishop1.type).toBe('bishop'); expect(whiteBishop2.type).toBe('bishop'); // Blocked by pawns initially expect(whiteBishop1.getValidMoves(board)).toHaveLength(0); expect(whiteBishop2.getValidMoves(board)).toHaveLength(0); }); test('bishop can move after pawn advances', () => { board = new Board(); board.setupInitialPosition(); // Move pawn to open diagonal board.movePiece(6, 3, 4, 3); // d2 to d4 const whiteBishop = board.getPiece(7, 2); // c1 bishop const moves = whiteBishop.getValidMoves(board); // Now has moves available expect(moves.length).toBeGreaterThan(0); }); }); });