const fs = require('fs'); const path = require('path'); const {pathToFileURL} = require('url'); const {chromium} = require('playwright'); const {pixelChanges} = require('./pixel_changes'); const assert = (value, message) => { if (!value) throw Error(message); }; const entry = fs.readdirSync(process.cwd()).find(name => name.endsWith('.html')); (async () => { assert(entry, 'missing HTML entry'); const browser = await chromium.launch({headless: true, args: ['--no-sandbox']}); try { const page = await browser.newPage({viewport: {width: 1280, height: 1000}}); await page.clock.install({time: new Date('2026-01-01T00:00:00Z')}); await page.clock.pauseAt(new Date('2026-01-01T00:00:01Z')); await page.goto(pathToFileURL(path.join(process.cwd(), entry)).href); const image = await browser.newPage(); 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]); assert(await page.evaluate(() => !!window.gameTest), 'missing public gameTest API'); await reset(); let initial = await state(); assert(initial.size === 15 && initial.board.length === 15 && initial.board.every(row => row.length === 15 && row.every(cell => cell === null)), 'reset must create empty 15x15 board'); assert(!initial.winner && !initial.easterEgg, 'reset must clear winner and easter egg'); await move(4, 4); const oneMove = await state(); assert(oneMove.board[4][4] !== null && !oneMove.winner && !oneMove.easterEgg, 'one stone cannot win'); for (const invalid of [[4, 4], [-1, 4], [15, 0], [-1, 15], [1, -1], [0, 15]]) { await move(...invalid); assert(JSON.stringify(await state()) === JSON.stringify(oneMove), 'occupied or out-of-range move changed state'); } // The test API must operate the visible board, not a hidden parallel // state machine. Compare two even-move positions: the same player is to // move and any turn label is unchanged, so new stones must alter pixels. await reset(); await move(2, 3); await move(11, 12); const twoStones = await page.screenshot({animations: 'disabled'}); await move(4, 5); await move(9, 10); const fourStones = await page.screenshot({animations: 'disabled'}); assert(!twoStones.equals(fourStones), 'gameTest moves changed internal state but not the visible board'); // Recover the click area from the painted stone itself. This also works // for canvas boards and does not require a named hook or a fitted grid. const frame = async () => { await page.clock.runFor(40); return page.screenshot({animations: 'disabled'}); }; await reset(); const beforeClick = await frame(); await move(7, 7); const {points} = await pixelChanges(image, [beforeClick, await frame()]); let physicalMove = false; for (const {x, y} of points) { await reset(); await page.mouse.click(x, y); const board = (await state()).board; if (board[7][7] !== null && board.flat().filter(cell => cell !== null).length === 1) { physicalMove = true; break; } } assert(physicalMove, 'clicking the rendered board must place exactly one stone in its own state'); await page.mouse.move(0, 0); const play = async (moves, winnerLabel) => { 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, `${winnerLabel} declared victory early`); } const finished = await state(); assert(finished.winner && finished.easterEgg, `${winnerLabel} five-in-a-row not recognized`); return finished; }; const winMoves = [[1,1],[9,0],[2,2],[9,2],[3,3],[9,4],[4,4],[9,6],[5,5]]; const blackDiagonal = await play(winMoves, 'diagonal'); const whiteHorizontal = await play([[10,0],[0,0],[11,2],[0,1],[12,4],[0,2],[13,6],[0,3],[14,8],[0,4]], 'white horizontal'); assert(blackDiagonal.winner !== whiteHorizontal.winner, 'both players reported the same winner'); assert(blackDiagonal.board[1][1] !== blackDiagonal.board[9][0], 'player stones must have distinct values'); const trace = async moves => { await reset(); for (const position of moves.slice(0, -1)) await move(...position); const before = await frame(); await move(...moves.at(-1)); await page.clock.runFor(750); return [before, await frame()]; }; const winning = await trace(winMoves); const ordinary = await trace([[1,1],[9,0],[2,2],[9,2],[3,3],[9,4],[4,3],[9,6],[5,5]]); assert(!(await state()).winner, 'comparison placement unexpectedly won'); const {novel} = await pixelChanges(image, [...winning, ...ordinary]); assert(novel >= 12, 'victory must alter visible output beyond an ordinary final stone or turn update'); await reset(); initial = await state(); assert(initial.board.every(row => row.every(cell => cell === null)) && !initial.winner && !initial.easterEgg, 'reset after victory did not clear game'); console.log('Gomoku reset, illegal move, diagonal and both-player win checks passed'); } finally { await browser.close(); } })().catch(error => { console.error(error); process.exit(1); });