|
| 1 | +/** |
| 2 | + * Game rules tests using an injected mock online match state. |
| 3 | + * |
| 4 | + * window.__e2e__ (exposed in DEV mode by App) lets tests inject specific game |
| 5 | + * states without needing a real Firebase backend. A match whose game ID is |
| 6 | + * '__test__' is treated as a sentinel by the match observer: it marks the |
| 7 | + * match as online (so turn-enforcement logic applies) but skips the Firebase |
| 8 | + * subscription that would otherwise overwrite injected state. |
| 9 | + */ |
| 10 | +import { test, expect } from '@playwright/test'; |
| 11 | +import { load, setGame, setMatch } from './helpers'; |
| 12 | + |
| 13 | +const TEST_MATCH = { game: '__test__', chat: '__test__', sort: '' }; |
| 14 | + |
| 15 | +test.describe('Game rules (online match simulation)', () => { |
| 16 | + test.describe('Turn enforcement', () => { |
| 17 | + test.beforeEach(({ page }) => load(page)); |
| 18 | + |
| 19 | + test('dice are disabled when it is not your turn', async ({ page }) => { |
| 20 | + await setMatch(page, TEST_MATCH); |
| 21 | + await setGame(page, { turn: 'opponent-uid', status: 'ROLLING' }); |
| 22 | + // All dice images should carry the 'used' class when the disabled prop is true |
| 23 | + await expect(page.locator('.dice img').first()).toHaveClass(/used/); |
| 24 | + }); |
| 25 | + |
| 26 | + test('dice are not disabled when it is your turn', async ({ page }) => { |
| 27 | + // Local mode (no match): isMyTurn is always true |
| 28 | + await expect(page.locator('.dice.pulsate')).toBeVisible(); |
| 29 | + await expect(page.locator('.dice img').first()).not.toHaveClass(/used/); |
| 30 | + }); |
| 31 | + |
| 32 | + test('pieces cannot be selected when it is not your turn', async ({ page }) => { |
| 33 | + await setMatch(page, TEST_MATCH); |
| 34 | + await setGame(page, { turn: 'opponent-uid', color: 'white', status: 'MOVING', dice: [3, 5] }); |
| 35 | + // With match set and game.turn pointing to someone else, enabled=false on all points |
| 36 | + const point = page.locator('.point').nth(18); |
| 37 | + await point.click(); |
| 38 | + await expect(point).not.toHaveClass(/selected/); |
| 39 | + }); |
| 40 | + }); |
| 41 | + |
| 42 | + test.describe('Valid move highlighting', () => { |
| 43 | + test.beforeEach(({ page }) => load(page)); |
| 44 | + |
| 45 | + test('valid source points are highlighted after rolling', async ({ page }) => { |
| 46 | + await setGame(page, { color: 'white', status: 'MOVING', dice: [3, 5] }); |
| 47 | + // White has pieces at indices 0, 11, 16 and 18 in the default board; all |
| 48 | + // have at least one reachable empty point with the given dice. |
| 49 | + await expect(page.locator('.point.valid')).not.toHaveCount(0); |
| 50 | + }); |
| 51 | + |
| 52 | + test('valid destinations are shown after selecting a source piece', async ({ page }) => { |
| 53 | + await setGame(page, { color: 'white', status: 'MOVING', dice: [3, 5] }); |
| 54 | + // Click white piece at index 18 (5 white pieces in default board) |
| 55 | + await page.locator('.point').nth(18).click(); |
| 56 | + // die=3 → index 21 is empty → valid |
| 57 | + await expect(page.locator('.point').nth(21)).toHaveClass(/valid/); |
| 58 | + }); |
| 59 | + |
| 60 | + test('blocked points with 2+ opponent pieces are not valid destinations', async ({ page }) => { |
| 61 | + await setGame(page, { color: 'white', status: 'MOVING', dice: [3, 5] }); |
| 62 | + // Click white piece at index 18 |
| 63 | + await page.locator('.point').nth(18).click(); |
| 64 | + // die=5 → index 23 has -2 (two black pieces) → BLOCKED, must not be valid |
| 65 | + await expect(page.locator('.point').nth(23)).not.toHaveClass(/valid/); |
| 66 | + }); |
| 67 | + }); |
| 68 | + |
| 69 | + test.describe('Bar (Prison)', () => { |
| 70 | + test.beforeEach(({ page }) => load(page)); |
| 71 | + |
| 72 | + test('bar is highlighted as valid source when a piece is on it', async ({ page }) => { |
| 73 | + // White has a piece on the bar; die=3 re-enters at index 9 (empty) |
| 74 | + await setGame(page, { |
| 75 | + color: 'white', |
| 76 | + status: 'MOVING', |
| 77 | + dice: [3], |
| 78 | + prison: { white: 1, black: 0 }, |
| 79 | + }); |
| 80 | + // The first .bar element in DOM order is the white bar |
| 81 | + await expect(page.locator('.bar').first()).toHaveClass(/valid/); |
| 82 | + }); |
| 83 | + |
| 84 | + test('no regular points are valid sources when a piece is on the bar', async ({ page }) => { |
| 85 | + await setGame(page, { |
| 86 | + color: 'white', |
| 87 | + status: 'MOVING', |
| 88 | + dice: [3], |
| 89 | + prison: { white: 1, black: 0 }, |
| 90 | + }); |
| 91 | + // All source selection must go through the bar; regular points must not be valid |
| 92 | + await expect(page.locator('.point.valid')).toHaveCount(0); |
| 93 | + }); |
| 94 | + }); |
| 95 | + |
| 96 | + test.describe('Doubles', () => { |
| 97 | + test('doubles produce four dice', async ({ page }) => { |
| 98 | + await load(page); |
| 99 | + // Inject a 4-dice state directly (equivalent to a doubles roll) |
| 100 | + await setGame(page, { status: 'MOVING', dice: [4, 4, 4, 4] }); |
| 101 | + await expect(page.locator('.dice img')).toHaveCount(4); |
| 102 | + }); |
| 103 | + }); |
| 104 | +}); |
0 commit comments