const fs = require('fs'); const path = require('path'); const {pathToFileURL} = require('url'); const {chromium} = require('playwright'); const assert = (value, message) => { if (!value) throw Error(message); }; const OUTPUT = '/logs/verifier/visual'; function winningMoves(start, direction, secondPlayer) { // Last stone joins two shorter runs, exercising both sides of the new stone. const line = [0, 4, 1, 3, 2].map(i => [start[0] + direction[0] * i, start[1] + direction[1] * i]); const distractors = [[14, 0], [14, 2], [14, 4], [14, 6], [14, 8]]; return line.flatMap((point, i) => secondPlayer ? [distractors[i], point] : i === line.length - 1 ? [point] : [point, distractors[i]]); } (async () => { const entry = path.join(process.cwd(), 'index.html'); assert(fs.existsSync(entry), 'missing public index.html entry'); const browser = await chromium.launch({headless: true, args: ['--no-sandbox']}); try { fs.mkdirSync(OUTPUT, {recursive: true}); const page = await browser.newPage({viewport: {width: 1100, height: 900}}); await page.route(/^https?:/, route => route.abort()); await page.goto(pathToFileURL(entry).href); const state = () => page.evaluate(() => window.gameTest.state()); const reset = () => page.evaluate(() => window.gameTest.reset()); const move = (r, c) => page.evaluate(([row, col]) => window.gameTest.move(row, col), [r, c]); const capture = name => page.screenshot({path: path.join(OUTPUT, `${name}.png`), animations: 'disabled'}); await reset(); const firstPlayer = (await state()).current; await capture('01-empty'); await move(2, 3); await move(11, 12); await capture('02-two-stones'); await move(4, 5); await move(9, 10); await capture('03-four-stones'); const directions = [ ['horizontal', [0, 0], [0, 1]], ['vertical', [0, 14], [1, 0]], ['diagonal', [0, 0], [1, 1]], ['anti-diagonal', [0, 14], [1, -1]], ['interior-horizontal', [7, 5], [0, 1]], ['interior-vertical', [5, 7], [1, 0]], ]; const winners = [null, null]; for (const [label, start, direction] of directions) { for (const secondPlayer of [false, true]) { const moves = winningMoves(start, direction, secondPlayer); await reset(); for (let i = 0; i < moves.length; i++) { await move(...moves[i]); const snapshot = await state(); if (i < moves.length - 1) assert(!snapshot.winner && !snapshot.easterEgg, `${label}/${secondPlayer}: premature victory`); } const finished = await state(); assert(finished.winner && finished.easterEgg, `${label}/${secondPlayer}: missed five-in-a-row`); const player = Number(secondPlayer); if (winners[player] !== null) assert(winners[player] === finished.winner, 'winner labels must be consistent'); winners[player] = finished.winner; if (label === 'horizontal' && !secondPlayer) await capture('04-first-player-win'); if (label === 'anti-diagonal' && secondPlayer) await capture('05-second-player-win'); } } assert(winners[0] !== winners[1], 'players reported the same winner'); for (const points of [ [[3, 0], [3, 2], [3, 4], [3, 6], [3, 8]], [[4, 0], [4, 1], [4, 3], [4, 4], [4, 5]], ]) { await reset(); for (let i = 0; i < points.length; i++) { await move(...points[i]); const snapshot = await state(); assert(!snapshot.winner && !snapshot.easterEgg, 'disconnected stones declared a win'); if (i < points.length - 1) await move(12, i * 2); } } await reset(); const cleared = await state(); assert(cleared.current === firstPlayer && !cleared.winner && !cleared.easterEgg && cleared.board.every(row => row.every(cell => cell === null)), 'reset must restore initial state'); await capture('06-reset'); console.log('All win directions, both players, joined runs, gaps and reset passed'); } finally { await browser.close(); } })().catch(error => { console.error(error); process.exit(1); });