tetris.spec.ts (2589B)
1 import { test, expect, type Page } from "@playwright/test"; 2 3 // Try common entry points until one loads successfully. 4 async function loadGame(page: Page) { 5 const candidates = [ 6 "/", 7 "/index.html", 8 "/dist/index.html", 9 "/public/index.html", 10 "/build/index.html", 11 ]; 12 13 for (const path of candidates) { 14 try { 15 const resp = await page.goto(path, { timeout: 5000 }); 16 if (resp?.ok()) return; 17 } catch { 18 continue; 19 } 20 } 21 } 22 23 test.describe("Tetris Game", () => { 24 test.beforeEach(async ({ page }) => { 25 await loadGame(page); 26 await page.waitForLoadState("domcontentloaded"); 27 }); 28 29 test("page loads without console errors", async ({ page }) => { 30 const errors: string[] = []; 31 page.on("pageerror", (err) => errors.push(err.message)); 32 33 // Give the page a moment to finish initializing 34 await page.waitForTimeout(2000); 35 36 expect(errors).toEqual([]); 37 }); 38 39 test("game board is visible", async ({ page }) => { 40 // A Tetris game should render either a <canvas> or a grid of DOM elements 41 const canvas = page.locator("canvas"); 42 const gridContainer = page.locator( 43 [ 44 '[class*="board"]', 45 '[class*="grid"]', 46 '[class*="game"]', 47 '[class*="field"]', 48 '[id*="board"]', 49 '[id*="grid"]', 50 '[id*="game"]', 51 '[id*="field"]', 52 "table", 53 ].join(", ") 54 ); 55 56 const canvasCount = await canvas.count(); 57 const gridCount = await gridContainer.count(); 58 59 expect( 60 canvasCount + gridCount, 61 "Expected a <canvas> or a container with board/grid/game/field in its class or id" 62 ).toBeGreaterThan(0); 63 }); 64 65 test("keyboard input does not crash the game", async ({ page }) => { 66 const errors: string[] = []; 67 page.on("pageerror", (err) => errors.push(err.message)); 68 69 // Press every key a Tetris game should handle 70 const keys = [ 71 "ArrowLeft", 72 "ArrowRight", 73 "ArrowDown", 74 "ArrowUp", 75 "Space", 76 ]; 77 for (const key of keys) { 78 await page.keyboard.press(key); 79 await page.waitForTimeout(150); 80 } 81 82 expect(errors).toEqual([]); 83 }); 84 85 test("game state changes over time", async ({ page }) => { 86 // If the game is running, the visual output should change as pieces fall 87 const shot1 = await page.screenshot(); 88 await page.waitForTimeout(3000); 89 const shot2 = await page.screenshot(); 90 91 expect( 92 Buffer.from(shot1).equals(Buffer.from(shot2)), 93 "Expected the page to visually change over 3 seconds (pieces should be falling)" 94 ).toBe(false); 95 }); 96 });