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

78 lines
4.1 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 { createServer } from 'node:http';
import { readFile } from 'node:fs/promises';
import { resolve, dirname, basename, extname, join } from 'node:path';
const file = resolve(process.argv[2] ?? 'index.html');
// ES module imports are blocked on file:// (CORS), so the game's directory is served over local http, as any real deployment would.
const types = { '.html': 'text/html', '.js': 'text/javascript', '.mjs': 'text/javascript', '.css': 'text/css', '.json': 'application/json', '.png': 'image/png' };
const root = dirname(file);
const server = createServer(async (req, res) => {
try {
const path = decodeURIComponent(new URL(req.url, 'http://x').pathname);
const target = path === '/' ? file : join(root, path);
if (!target.startsWith(root)) { res.writeHead(403).end(); return; }
const body = await readFile(target);
res.writeHead(200, { 'content-type': types[extname(target)] ?? 'application/octet-stream' }).end(body);
} catch { res.writeHead(404).end(); }
});
await new Promise((ok) => server.listen(0, '127.0.0.1', ok));
const url = `http://127.0.0.1:${server.address().port}/${basename(file)}`;
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(url);
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.
if (!box) { await browser.close(); server.close(); console.log('SUMMARY ' + results.filter((r) => r.ok).length + '/6'); process.exit(1); }
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();
server.close();
const passed = results.filter((r) => r.ok).length;
console.log(`SUMMARY ${passed}/6`);
process.exit(passed === 6 ? 0 : 1);