Files
leleka/scripts/e2e/codex/check_ttt.mjs
T

61 lines
3.0 KiB
JavaScript

// Shared browser checks for the Three.js tic-tac-toe game (offline test driver, not shipped): 6 checks, real mouse clicks.
// node check_ttt.mjs /path/to/index.html (needs `playwright` + its chromium installed in the test directory only)
import { chromium } from 'playwright';
import { pathToFileURL } from 'node:url';
import { resolve } from 'node:path';
const file = resolve(process.argv[2] ?? 'index.html');
const results = [];
const check = (name, ok, detail = '') => {
results.push({ name, ok: !!ok, detail });
console.log(`${ok ? 'PASS' : 'FAIL'} ${name}${ok ? '' : ' :: ' + detail}`);
};
const browser = await chromium.launch({ args: ['--use-gl=swiftshader', '--enable-unsafe-swiftshader', '--ignore-gpu-blocklist'] });
const page = await browser.newPage({ viewport: { width: 900, height: 900 } });
const errors = [];
page.on('pageerror', (e) => errors.push(String(e)));
page.on('console', (m) => { if (m.type() === 'error') errors.push(m.text()); });
await page.goto(pathToFileURL(file).href);
await page.waitForTimeout(1500);
check('1 loads without page error', errors.length === 0, errors.join(' | ').slice(0, 300));
const box = await page.evaluate(() => {
const c = document.querySelector('canvas');
if (!c) return null;
const r = c.getBoundingClientRect();
return { x: r.x, y: r.y, w: r.width, h: r.height };
});
check('2 visible WebGL canvas', box && box.w > 100 && box.h > 100, JSON.stringify(box));
const status = async () => (await page.evaluate(() => document.getElementById('status')?.textContent ?? '')).trim();
// The board layout is the game's own: the nine regions of a lattice around the canvas centre must map to nine different cells.
const cx = box.x + box.w / 2, cy = box.y + box.h / 2, step = Math.min(box.w, box.h) / 6;
const pts = [];
for (let r = -1; r <= 1; r++) for (let c = -1; c <= 1; c++) pts.push([cx + c * step, cy + r * step]);
const click = async (i) => { await page.mouse.click(pts[i][0], pts[i][1]); await page.waitForTimeout(250); };
const restart = async () => { const b = await page.$('#restart'); if (b) await b.click(); await page.waitForTimeout(200); };
// 3: row win via real clicks: X takes 0,1,2; O takes 3,4
for (const i of [0, 3, 1, 4, 2]) await click(i);
const s3 = await status();
check('3 row win via real clicks', /X/i.test(s3) && /win/i.test(s3), s3);
// 4: moves blocked after the win
const before = await status();
await click(5); await click(6);
const after = await status();
check('4 moves blocked after the win', before === after && /win/i.test(after), `${before} -> ${after}`);
// 5: restart to an empty board
await restart();
const s5 = await status();
check('5 restart to empty board (X to move)', /X/i.test(s5) && !/win|draw/i.test(s5), s5);
// 6: draw via real clicks: X O X / X O O / O X X
for (const i of [0, 1, 2, 4, 3, 5, 7, 6, 8]) await click(i);
const s6 = await status();
check('6 draw via real clicks', /draw/i.test(s6), s6);
await browser.close();
const passed = results.filter((r) => r.ok).length;
console.log(`SUMMARY ${passed}/6`);
process.exit(passed === 6 ? 0 : 1);