UXInteract / task_002 /tests /validate_enhanced_cases.js
DojiSum's picture
Publish the current 141-task UXInteract release
ef987bb verified
Raw History Blame Contribute Delete
7.19 kB
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 = 'index.html';
(async () => {
assert(fs.existsSync(entry), 'missing public index.html entry');
const browser = await chromium.launch({headless: true, args: ['--no-sandbox']});
try {
const page = await browser.newPage({viewport: {width: 1280, height: 1000}});
const image = await browser.newPage();
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 deck = ['A','B','A','B','C','C','D','D','E','E','F','F','G','G','H','H'];
const state = () => page.evaluate(() => gameTest.state());
const flip = index => page.evaluate(i => gameTest.flip(i), index);
const advance = ms => page.evaluate(t => gameTest.advance(t), ms);
await page.evaluate(d => gameTest.reset(d), deck);
let current = await state();
assert(current.cards.length === 16 && current.cards.every((card, index) => card.value === deck[index] && !card.faceUp && !card.matched), 'reset must preserve deck order and hide all cards');
assert(current.moves === 0 && current.matchedPairs === 0, 'reset counters must be zero');
// Two one-card states have the same counters and selected position but
// different underlying values. A highlight-only "flip" cannot pass.
const covered = await page.screenshot({animations: 'disabled'});
await flip(0); await page.waitForTimeout(450);
const firstFace = await page.screenshot({animations: 'disabled'});
const swapped = [...deck]; [swapped[0], swapped[1]] = [swapped[1], swapped[0]];
await page.evaluate(d => gameTest.reset(d), swapped);
await flip(0); await page.waitForTimeout(450);
const secondFace = await page.screenshot({animations: 'disabled'});
assert(!firstFace.equals(secondFace), 'logical card flips did not reveal the card value');
const {points} = await pixelChanges(image, [covered, firstFace]);
let userFlip = false;
let animated = false;
for (const {x, y} of points) {
await page.evaluate(d => gameTest.reset(d), deck);
await page.mouse.move(x, y);
const clip = {x: Math.max(0, Math.floor(x) - 20), y: Math.max(0, Math.floor(y) - 20),
width: 40, height: 40};
const beforeMotion = await page.evaluateHandle(({x, y}) => {
const current = new Set(document.getAnimations().filter(animation => {
const bounds = animation.effect?.target?.getBoundingClientRect();
return animation.playState === 'running' && bounds && x >= bounds.left && x <= bounds.right
&& y >= bounds.top && y <= bounds.bottom;
}));
for (const animation of current) animation.pause();
return current;
}, {x, y});
const before = await page.screenshot({clip, animations: 'allow'});
await page.mouse.click(x, y);
const clicked = await state();
if (!clicked.cards[0].faceUp || clicked.cards.filter(card => card.faceUp).length !== 1) {
await beforeMotion.evaluate(previous => { for (const animation of previous) animation.play(); });
await beforeMotion.dispose();
continue;
}
userFlip = true;
animated = await page.evaluate(({x, y, previous}) => document.getAnimations().some(animation => {
const bounds = animation.effect?.target?.getBoundingClientRect();
return !previous.has(animation) && animation.playState === 'running' && bounds && x >= bounds.left && x <= bounds.right
&& y >= bounds.top && y <= bounds.bottom;
}), {x, y, previous: beforeMotion});
await beforeMotion.dispose();
if (!animated) {
const during = [];
for (const elapsed of [0, 16, 32, 64, 128, 256, 512]) {
await page.clock.runFor(elapsed);
during.push(await page.screenshot({clip, animations: 'allow'}));
}
const after = during.at(-1);
animated = during.some(frame => !frame.equals(before) && !frame.equals(after));
}
break;
}
assert(userFlip, 'clicking the rendered card must flip the same card tracked by the game');
assert(animated, 'user flips must produce an observable card animation');
await page.evaluate(d => gameTest.reset(d), deck);
await flip(0);
current = await state();
assert(current.cards[0].faceUp && current.moves === 0, 'first flip must not count a move');
await flip(0);
assert(JSON.stringify(await state()) === JSON.stringify(current), 're-flipping same card changed state');
await flip(1);
current = await state();
assert(current.cards[0].faceUp && current.cards[1].faceUp && current.moves === 1 && current.matchedPairs === 0, 'mismatch must stay visible and count one move');
await flip(4);
assert(JSON.stringify(await state()) === JSON.stringify(current), 'third card flipped during mismatch lock');
await advance(299);
current = await state();
assert(current.cards[0].faceUp && current.cards[1].faceUp, 'mismatch delay was shorter than 300ms');
await advance(1701);
current = await state();
assert(!current.cards[0].faceUp && !current.cards[1].faceUp && current.moves === 1, 'mismatch did not flip back without extra move');
await flip(0);
await flip(2);
await advance(1000);
current = await state();
assert(current.cards[0].matched && current.cards[2].matched && current.cards[0].faceUp && current.cards[2].faceUp, 'match must remain face up');
assert(current.moves === 2 && current.matchedPairs === 1, 'match counters wrong');
await flip(0);
assert(JSON.stringify(await state()) === JSON.stringify(current), 'matched card was flipped again');
await flip(1);
await flip(3);
current = await state();
assert(current.matchedPairs === 2 && current.moves === 3, 'second pair counters wrong');
for (let i = 4; i < 16; i += 2) {
await flip(i); await flip(i + 1);
}
current = await state();
assert(current.matchedPairs === 8 && current.moves === 9
&& current.cards.every(card => card.faceUp && card.matched), 'all eight pairs must be matchable');
await page.evaluate(d => gameTest.reset(d), deck);
current = await state();
assert(current.moves === 0 && current.matchedPairs === 0 && current.cards.every(card => !card.faceUp && !card.matched), 'reset after matches did not clear state');
await flip(0); await flip(1);
await page.evaluate(d => gameTest.reset(d), deck);
await flip(4); await flip(5);
await advance(2500);
current = await state();
assert(current.moves === 1 && current.matchedPairs === 1
&& current.cards[4].faceUp && current.cards[5].faceUp,
'stale mismatch state affected the new game');
console.log('memory-game deck, mismatch lock, time and counter checks passed');
} finally {
await browser.close();
}
})().catch(error => { console.error(error); process.exit(1); });