UXInteract / task_005 /tests /validate_enhanced_cases.js
DojiSum's picture
Publish the current 141-task UXInteract release
ef987bb verified
Raw History Blame Contribute Delete
7.44 kB
const fs = require('fs');
const path = require('path');
const {pathToFileURL} = require('url');
const {chromium} = require('playwright');
const ok = (condition, message) => { if (!condition) throw Error(message); };
const entry = () => path.join(process.cwd(), 'index.html');
(async () => {
const browser = await chromium.launch({headless: true, args: ['--no-sandbox']});
try {
const page = await browser.newPage();
await page.clock.install();
await page.goto(pathToFileURL(entry()).href);
await page.clock.pauseAt(new Date(Date.now() + 60000));
const dims = await page.evaluate(() => { gameTest.reset(); const s = gameTest.state(); return {rows:s.rows,cols:s.cols}; });
ok(Number.isInteger(dims.rows) && dims.rows >= 12 && Number.isInteger(dims.cols) && dims.cols >= 8,
'board must have at least twelve rows and eight columns');
const x = Math.floor(dims.cols / 2);
const board = Array.from({length:dims.rows}, (_,r) => Array.from({length:dims.cols}, (_,c) => r === dims.rows-1 && c !== x ? 1 : 0));
await page.evaluate(({board,x,rows}) => { gameTest.setBoard(board); gameTest.setPiece({shape:[[1]],x,y:rows-2}); gameTest.drop(); gameTest.drop(); }, {board,x,rows:dims.rows});
let state = await page.evaluate(() => gameTest.state());
ok(state.lines === 1 && state.score > 0, 'completed line must clear and score');
ok(state.board[0].every(v => !v), 'cleared board must gain an empty top row');
// The public contract does not prescribe HUD wording or a text-only HUD.
// Counters above are checked semantically; rendered labels are reviewed
// in visual evidence rather than matched to an English singular noun.
await page.evaluate(() => gameTest.setPiece({shape:[[1,0],[1,1]],x:2,y:2}));
const before = JSON.stringify((await page.evaluate(() => gameTest.state())).piece.shape);
await page.keyboard.press('ArrowUp');
state = await page.evaluate(() => gameTest.state());
ok(JSON.stringify(state.piece.shape) !== before, 'up key must rotate the visible game piece');
const y = state.piece.y;
await page.evaluate(() => { gameTest.step(0); gameTest.step(499); });
ok((await page.evaluate(() => gameTest.state())).piece.y === y, 'step(0) and sub-frame time must not drop');
await page.evaluate(() => gameTest.step(1));
ok((await page.evaluate(() => gameTest.state())).piece.y === y+1, 'accumulated 500ms must drop once');
await page.evaluate(() => gameTest.step(1000));
ok((await page.evaluate(() => gameTest.state())).piece.y === y+3, 'large step must drop the right number of rows');
for (const operation of ['reset', 'setBoard', 'setPiece']) {
const timed = await page.evaluate((operation) => {
const supplied = {shape: [[1]], x: 0, y: 0};
gameTest.reset();
gameTest.setPiece(supplied);
gameTest.step(499);
if (operation === 'reset') gameTest.reset();
if (operation === 'setBoard') gameTest.setBoard(gameTest.state().board);
if (operation === 'setPiece') gameTest.setPiece(supplied);
const before = gameTest.state().piece.y;
gameTest.step(1);
return [before, gameTest.state().piece.y];
}, operation);
ok(timed[0] === timed[1], `${operation} must clear time remainder`);
}
await page.evaluate(() => { gameTest.reset(); gameTest.setPiece({shape:[[1]],x:0,y:0}); });
state = await page.evaluate(() => gameTest.state());
await page.keyboard.press('ArrowLeft');
ok((await page.evaluate(() => gameTest.state())).piece.x === 0, 'left wall must stop keyboard movement');
const visual = '/logs/verifier/visual';
fs.mkdirSync(visual, {recursive: true});
const beforeMove = await page.screenshot({path: path.join(visual, '01-left.png'), animations: 'disabled'});
await page.keyboard.press('ArrowRight');
ok((await page.evaluate(() => gameTest.state())).piece.x === 1, 'right key must move actual piece');
const afterMove = await page.screenshot({path: path.join(visual, '02-right.png'), animations: 'disabled'});
ok(!beforeMove.equals(afterMove), 'piece moved in state but not in rendered output');
await page.keyboard.press('ArrowDown');
ok((await page.evaluate(() => gameTest.state())).piece.y === 1, 'down key must drop actual piece');
await page.screenshot({path: path.join(visual, '03-down.png'), animations: 'disabled'});
await page.clock.runFor(650);
ok((await page.evaluate(() => gameTest.state())).piece.y === 1, 'deterministic injection must ignore wall-clock rAF');
const beforeBoardInjection = await page.screenshot({animations: 'disabled'});
await page.evaluate(() => {
const state = gameTest.state();
const board = state.board.map(row => [...row]);
board[state.rows - 1] = board[state.rows - 1].map((cell, column) => column % 2 ? 0 : 1);
gameTest.setBoard(board);
});
const afterBoardInjection = await page.screenshot({path: path.join(visual, '04-occupied-row.png'), animations: 'disabled'});
ok(!beforeBoardInjection.equals(afterBoardInjection), 'occupied numeric cells affect state but are not visibly rendered');
const blocked = await page.evaluate(() => {
for (const phase of [0, 1]) {
gameTest.reset();
const {rows, cols} = gameTest.state();
// Incomplete, tightly packed rows with alternating edge holes prevent
// entry without prescribing a particular spawn anchor or shape.
const packed = Array.from({length: rows}, (_, row) => Array.from({length: cols}, (_, col) =>
row < rows - 2 && col !== ((row + phase) % 2 ? cols - 1 : 0) ? 1 : 0));
gameTest.setBoard(packed);
gameTest.setPiece({shape: [[1]], x: 0, y: rows - 1});
for (let i = 0; i <= rows; i++) {
gameTest.drop();
const next = gameTest.state();
if (next.gameOver) return {stopped: true};
const {piece, board} = next;
const overlaps = piece.shape.some((row, dy) =>
row.some((cell, dx) => cell && board[piece.y + dy]?.[piece.x + dx]));
if (overlaps) return {stopped: false, overlap: true};
}
}
return {stopped: false, overlap: false};
});
ok(blocked.stopped, blocked.overlap
? 'a new piece overlapping a locked block must stop play'
: 'a packed board that prevents new pieces from entering must stop play');
state = await page.evaluate(() => gameTest.state());
ok(state.gameOver === true, 'blocked spawn must end the game');
const stopped = JSON.stringify(state);
await page.evaluate(() => { gameTest.move(1); gameTest.rotate(); gameTest.drop(); gameTest.step(1500); });
ok(JSON.stringify(await page.evaluate(() => gameTest.state())) === stopped, 'game-over state must freeze until reset');
await page.screenshot({path: path.join(visual, '04-game-over.png'), animations: 'disabled'});
await page.evaluate(() => gameTest.reset());
state = await page.evaluate(() => gameTest.state());
ok(!state.gameOver && state.score === 0 && state.lines === 0 && state.board.every(row => row.every(cell => !cell)),
'reset must restore a playable empty board');
await page.screenshot({path: path.join(visual, '05-restarted.png'), animations: 'disabled'});
console.log('enhanced tetris cases passed');
} finally { await browser.close(); }
})().catch(error => { console.error(error); process.exit(1); });