|
| 1 | +import { test, expect } from '@playwright/test'; |
| 2 | + |
| 3 | +test.describe('Chess Game UI Tests', () => { |
| 4 | + test.beforeEach(async ({ page }) => { |
| 5 | + await page.goto('/'); |
| 6 | + // Wait for the chessboard to load |
| 7 | + await page.waitForSelector('[class*="chessboard"], table, [data-testid="chessboard"]', { timeout: 10000 }); |
| 8 | + }); |
| 9 | + |
| 10 | + test('should load the chessboard', async ({ page }) => { |
| 11 | + // Verify chessboard is visible |
| 12 | + const chessboard = page.locator('[class*="chessboard"], table, [data-testid="chessboard"]'); |
| 13 | + await expect(chessboard).toBeVisible(); |
| 14 | + |
| 15 | + // Verify initial pieces are present (at least some squares have pieces) |
| 16 | + const squares = page.locator('td, [data-square], [class*="square"]'); |
| 17 | + await expect(squares).toHaveCount(64); // 8x8 board |
| 18 | + }); |
| 19 | + |
| 20 | + test('should start a new game', async ({ page }) => { |
| 21 | + // Click start game button if present, or assume game starts automatically |
| 22 | + const startButton = page.locator('button:has-text("Start"), [data-testid="start-game"]'); |
| 23 | + if (await startButton.isVisible()) { |
| 24 | + await startButton.click(); |
| 25 | + } |
| 26 | + |
| 27 | + // Verify game is started (turn indicator shows White) |
| 28 | + const turnIndicator = page.locator('[class*="turn"], [data-testid="turn"]'); |
| 29 | + if (await turnIndicator.isVisible()) { |
| 30 | + await expect(turnIndicator).toContainText(/White|white/i); |
| 31 | + } |
| 32 | + }); |
| 33 | + |
| 34 | + test('should play a complete game (Scholar\'s Mate)', async ({ page }) => { |
| 35 | + // Start game |
| 36 | + const startButton = page.locator('button:has-text("Start"), [data-testid="start-game"]'); |
| 37 | + if (await startButton.isVisible()) { |
| 38 | + await startButton.click(); |
| 39 | + } |
| 40 | + |
| 41 | + // Scholar's Mate sequence: |
| 42 | + // 1. e4 e5 |
| 43 | + // 2. Bc4 Nc6 |
| 44 | + // 3. Qh5 Nf6 |
| 45 | + // 4. Qxf7# |
| 46 | + |
| 47 | + // Helper function to click square by algebraic notation |
| 48 | + const clickSquare = async (square: string) => { |
| 49 | + const [file, rank] = square.split(''); |
| 50 | + const col = file.charCodeAt(0) - 'a'.charCodeAt(0) + 1; // a=1, b=2, ..., h=8 |
| 51 | + const row = parseInt(rank); |
| 52 | + |
| 53 | + // Try different selector patterns |
| 54 | + let squareLocator = page.locator(`td[data-row="${row}"][data-col="${col}"]`); |
| 55 | + if (!(await squareLocator.isVisible())) { |
| 56 | + squareLocator = page.locator(`[data-square="${square}"]`); |
| 57 | + } |
| 58 | + if (!(await squareLocator.isVisible())) { |
| 59 | + squareLocator = page.locator(`.${square}, #${square}`); |
| 60 | + } |
| 61 | + if (!(await squareLocator.isVisible())) { |
| 62 | + // Fallback: find by text content or position |
| 63 | + squareLocator = page.locator('td, div').filter({ hasText: new RegExp(square.toUpperCase()) }); |
| 64 | + } |
| 65 | + |
| 66 | + await squareLocator.click(); |
| 67 | + }; |
| 68 | + |
| 69 | + // 1. White: e2 to e4 |
| 70 | + await clickSquare('e2'); |
| 71 | + await clickSquare('e4'); |
| 72 | + |
| 73 | + // Verify move was made (e4 should have white pawn) |
| 74 | + await page.waitForTimeout(500); // Wait for UI update |
| 75 | + |
| 76 | + // 2. Black: e7 to e5 |
| 77 | + await clickSquare('e7'); |
| 78 | + await clickSquare('e5'); |
| 79 | + |
| 80 | + // 3. White: Bf1 to c4 |
| 81 | + await clickSquare('f1'); |
| 82 | + await clickSquare('c4'); |
| 83 | + |
| 84 | + // 4. Black: Nb8 to c6 |
| 85 | + await clickSquare('b8'); |
| 86 | + await clickSquare('c6'); |
| 87 | + |
| 88 | + // 5. White: Qd1 to h5 |
| 89 | + await clickSquare('d1'); |
| 90 | + await clickSquare('h5'); |
| 91 | + |
| 92 | + // 6. Black: Ng8 to f6 |
| 93 | + await clickSquare('g8'); |
| 94 | + await clickSquare('f6'); |
| 95 | + |
| 96 | + // 7. White: Qh5 to f7 (checkmate) |
| 97 | + await clickSquare('h5'); |
| 98 | + await clickSquare('f7'); |
| 99 | + |
| 100 | + // Verify checkmate |
| 101 | + await page.waitForTimeout(1000); // Wait for checkmate detection |
| 102 | + |
| 103 | + // Check for checkmate message |
| 104 | + const checkmateMessage = page.locator('[class*="checkmate"], [data-testid="checkmate"], text=/checkmate|Checkmate/i'); |
| 105 | + await expect(checkmateMessage).toBeVisible(); |
| 106 | + |
| 107 | + // Verify game ended (no more moves possible) |
| 108 | + const gameOverIndicator = page.locator('[class*="game-over"], [data-testid="game-over"]'); |
| 109 | + if (await gameOverIndicator.isVisible()) { |
| 110 | + await expect(gameOverIndicator).toContainText(/over|ended|finished/i); |
| 111 | + } |
| 112 | + }); |
| 113 | + |
| 114 | + test('should highlight valid moves', async ({ page }) => { |
| 115 | + // Start game |
| 116 | + const startButton = page.locator('button:has-text("Start"), [data-testid="start-game"]'); |
| 117 | + if (await startButton.isVisible()) { |
| 118 | + await startButton.click(); |
| 119 | + } |
| 120 | + |
| 121 | + // Click on e2 pawn |
| 122 | + const e2Square = page.locator('td[data-row="2"][data-col="5"], [data-square="e2"]'); |
| 123 | + await e2Square.click(); |
| 124 | + |
| 125 | + // Verify valid moves are highlighted |
| 126 | + const highlightedSquares = page.locator('[class*="valid"], [class*="highlight"], [data-valid="true"]'); |
| 127 | + await expect(highlightedSquares).toHaveCount(2); // e3 and e4 |
| 128 | + |
| 129 | + // Click on highlighted e4 |
| 130 | + const e4Square = page.locator('td[data-row="4"][data-col="5"], [data-square="e4"]'); |
| 131 | + await e4Square.click(); |
| 132 | + |
| 133 | + // Verify move was made and highlights are gone |
| 134 | + await expect(highlightedSquares).toHaveCount(0); |
| 135 | + }); |
| 136 | + |
| 137 | + test('should reject invalid moves', async ({ page }) => { |
| 138 | + // Start game |
| 139 | + const startButton = page.locator('button:has-text("Start"), [data-testid="start-game"]'); |
| 140 | + if (await startButton.isVisible()) { |
| 141 | + await startButton.click(); |
| 142 | + } |
| 143 | + |
| 144 | + // Try to move knight like a bishop (invalid) |
| 145 | + const b1Square = page.locator('td[data-row="1"][data-col="2"], [data-square="b1"]'); |
| 146 | + await b1Square.click(); |
| 147 | + |
| 148 | + // Try to move to d3 (invalid for knight) |
| 149 | + const d3Square = page.locator('td[data-row="3"][data-col="4"], [data-square="d3"]'); |
| 150 | + await d3Square.click(); |
| 151 | + |
| 152 | + // Verify move was not made (turn should still be White) |
| 153 | + const turnIndicator = page.locator('[class*="turn"], [data-testid="turn"]'); |
| 154 | + if (await turnIndicator.isVisible()) { |
| 155 | + await expect(turnIndicator).toContainText(/White|white/i); |
| 156 | + } |
| 157 | + |
| 158 | + // Verify no error message or invalid move feedback |
| 159 | + const errorMessage = page.locator('[class*="error"], [data-testid="error"]'); |
| 160 | + if (await errorMessage.isVisible()) { |
| 161 | + await expect(errorMessage).toBeVisible(); // If error UI exists |
| 162 | + } |
| 163 | + }); |
| 164 | +}); |
0 commit comments