UXInteract / task_041 /tests /validate_enhanced_cases.js
DojiSum's picture
Publish the current 141-task UXInteract release
ef987bb verified
Raw History Blame Contribute Delete
10.2 kB
const fs = require('fs');
const path = require('path');
const {pathToFileURL} = require('url');
const {chromium} = require('playwright');
const assert = require('assert');
function assertDefaultField(items) {
assert(items.filter(item => item.type === 'diamond').length >= 3, 'default level needs at least three diamonds');
for (const type of ['goldS', 'goldL', 'stone', 'pig', 'bomb']) {
assert(items.some(item => item.type === type), 'default level omitted ' + type);
}
for (const item of items) {
assert(Number.isFinite(item.x) && Number.isFinite(item.y) && item.weight > 0);
if (item.type === 'diamond') assert.equal(item.value, 800);
else if (item.type === 'bomb') assert.equal(item.value, 2);
else {
assert(Number.isFinite(item.value) && item.value > 2 && item.value < 800,
item.type + ' must be worth more than a bomb and less than a diamond');
if (item.type === 'pig') assert.equal(item.value, 3);
}
}
const stones = items.filter(item => item.type === 'stone');
assert(new Set(stones.map(item => item.weight)).size >= 2 && new Set(stones.map(item => item.value)).size >= 2,
'different stone sizes need different weight and price');
}
async function startFromHome(page, entry, mode) {
await page.goto(entry);
const menu = page.getByTestId('mode-menu');
await menu.waitFor({state: 'visible'});
const select = menu.getByTestId('game-mode');
if (await select.isVisible()) {
await select.selectOption(mode);
} else {
await menu.getByTestId('mode-' + mode).click();
}
const start = menu.getByTestId('start-game');
await start.waitFor({state: 'visible'});
await start.focus();
const state = () => page.evaluate(() => gameTest.state());
for (const key of ['s', 'ArrowDown']) {
await page.keyboard.press(key);
assert((await state()).hooks.every(hook => hook.state === 'swing' && hook.len === 0),
'selecting a mode must not allow a hook to launch before Start');
}
await start.click();
await page.locator('canvas').first().waitFor({state: 'visible'});
const players = mode === 'single' ? ['A'] : ['A', 'B'];
const current = await state();
assert.deepStrictEqual(current.hooks.map(hook => hook.player).sort(), players,
'Start must activate the hooks for the selected mode');
assertDefaultField(current.items);
for (const player of players) {
await page.keyboard.press(player === 'A' ? 's' : 'ArrowDown');
const after = await state();
assert(after.hooks.some(hook => hook.player === player && (hook.state !== 'swing' || hook.len > 0))
|| after.scores[player] > current.scores[player], 'the live game did not launch hook ' + player);
}
}
(async () => {
const browser = await chromium.launch({headless: true});
try {
const page = await browser.newPage({viewport: {width: 1000, height: 760}});
const errors = [];
page.on('pageerror', error => errors.push(error.message));
const entry = pathToFileURL(path.join(process.cwd(), 'index.html')).href;
const state = () => page.evaluate(() => gameTest.state());
const reset = config => page.evaluate(config => gameTest.reset(config), config);
const step = ms => page.evaluate(ms => gameTest.step(ms), ms);
const waitForCatch = async (player, initialCount) => {
for (let elapsed = 0; elapsed < 15_000; elapsed += 250) {
await step(250);
const result = await state();
if (result.items.length < initialCount) return result;
const hook = result.hooks.find(hook => hook.player === player);
if (hook.state === 'swing' && hook.holding === null) break;
}
throw Error(`hook ${player} did not retrieve an on-path target`);
};
const capture = async name => {
fs.mkdirSync('/logs/verifier/visual-audit', {recursive: true});
await page.screenshot({path: `/logs/verifier/visual-audit/${name}.png`, fullPage: true});
};
await startFromHome(page, entry, 'single');
await capture('default-level');
await startFromHome(page, entry, 'double');
await reset({items: [{type: 'pig', x: 200, y: 300}]});
await step(160);
let current = await state();
assert(Number.isFinite(current.items[0].x) && current.items[0].x !== 200 && current.items[0].y === 300, 'pig must move horizontally using the actual injected state');
await reset({mode: 'double', items: []});
await page.keyboard.press('s');
await page.keyboard.press('ArrowDown');
current = await state();
assert.deepStrictEqual(current.hooks.map(hook => hook.player).sort(), ['A', 'B']);
assert(current.hooks.every(hook => hook.state === 'extend'), 'both keyboard controls must launch their corresponding hook');
await step(160);
assert((await state()).hooks.every(hook => hook.len > 0));
await capture('two-player-hooks');
await step(4000);
current = await state();
assert(current.hooks.every(hook => hook.state === 'swing' && hook.len === 0), 'empty catches must return from the boundary');
assert.deepStrictEqual(current.scores, {A: 0, B: 0});
// The problem does not specify B's anchor. Supply targets across the
// field rather than binding the test to the reference's x coordinate.
const canvas = page.locator('canvas').first();
const dimensions = await canvas.evaluate(({width, height}) => ({width, height}));
const targets = [];
for (let y = 8; y < dimensions.height; y += 12) {
for (let x = 8; x < dimensions.width; x += 12) targets.push({type: 'goldS', x, y});
}
let value;
let caught = false;
for (const swingTime of [0, 176, 368, 640, 1008, 1536]) {
await reset({mode: 'double', items: targets, targetScore: 10_000});
await step(swingTime);
value = (await state()).items[0].value;
await page.keyboard.press('ArrowDown');
try {
current = await waitForCatch('B', targets.length);
caught = true;
break;
} catch (error) {
if (!error.message.includes('did not retrieve an on-path target')) throw error;
}
}
assert(caught, 'B could not catch an item at any tested swing phase');
assert.deepStrictEqual(current.scores, {A: 0, B: value}, 'B must be able to catch and receive credit without changing A’s score');
assert.equal(current.items.length, targets.length - 1, 'a catch removes exactly its own target');
await step(160);
assert.deepStrictEqual((await state()).scores, current.scores, 'one catch must not be credited repeatedly');
// A starts down at x=320. Include one on-path object at a new
// distance and a tempting off-path target on the same row.
await reset({items: [{type: 'diamond', x: 100, y: 161}, {type: 'goldS', x: 320, y: 161}]});
const goldValue = (await state()).items[1].value;
await page.keyboard.press('s');
current = await waitForCatch('A', 2);
assert.deepStrictEqual(current.scores, {A: goldValue, B: 0}, 'the hook should catch the object it intersects');
assert.equal(current.items.length, 1, 'the off-path object should remain');
assert.equal(current.items[0].type, 'diamond', 'the off-path object must not be selected by array order or value');
const catchLength = type => page.evaluate(type => {
gameTest.reset({items: [{type, x: 320, y: 110}]});
gameTest.dropClaw(); gameTest.step(100);
return gameTest.state().hooks[0].len;
}, type);
assert(await catchLength('stone') > await catchLength('goldS'), 'heavier catch must reel more slowly');
await reset({items: [{type: 'bomb', x: 320, y: 110}], targetScore: 999});
await page.evaluate(() => gameTest.dropClaw());
await step(80);
current = await state();
assert.equal(current.explosions, 1);
assert.equal(current.scores.A, 2);
assert.equal(current.items.length, 0);
assert.equal(current.hooks[0].holding, null);
assert.equal(await page.locator('#scoreA').innerText(), '2');
const explosionFrame = await page.locator('canvas').first().evaluate(canvas => Array.from(canvas.getContext('2d').getImageData(0, 0, canvas.width, canvas.height).data));
await capture('visible-bomb-explosion');
await reset({items: [], targetScore: 999});
const changedPixels = await page.locator('canvas').first().evaluate((canvas, before) => {
const current = canvas.getContext('2d').getImageData(0, 0, canvas.width, canvas.height).data;
let count = 0;
for (let i = 0; i < current.length; i += 4) if (Math.abs(current[i]-before[i])+Math.abs(current[i+1]-before[i+1])+Math.abs(current[i+2]-before[i+2]) > 15) count++;
return count;
}, explosionFrame);
assert(changedPixels >= 100, 'the visible bomb effect must change at least 100 canvas pixels');
const diamond = {type: 'diamond', x: 320, y: 110};
await reset({items: [diamond]});
await page.evaluate(() => gameTest.dropClaw());
await step(3000);
current = await state();
assert.equal(current.scores.A, 800);
assert(current.won && /胜利|victory|彩蛋/i.test(await page.locator('body').innerText()));
assert.equal(await page.locator('#scoreA').innerText(), '800');
await capture('diamond-victory');
for (const targetScore of [600, 1000]) {
await reset({items: [diamond], targetScore});
await page.evaluate(() => gameTest.dropClaw());
await step(3000);
const result = await state();
assert.equal(result.target, targetScore, 'reset must report the injected target');
assert.equal(result.scores.A, 800, 'the custom target must not change the item value');
assert.equal(result.won, result.scores.A >= targetScore, 'victory must follow the injected target');
if (result.won) {
assert(/胜利|victory|彩蛋/i.test(await page.locator('body').innerText()), 'meeting the injected target must show victory');
}
}
await reset({items: [], targetScore: 800});
assert(!(await state()).won && (await state()).explosions === 0);
assert(!/victory!|胜利彩蛋/i.test(await page.locator('body').innerText()), 'reset must clear the previous victory message');
assert.equal(errors.length, 0, errors.join(' | '));
} finally {await browser.close();}
})().catch(error => {console.error(error); process.exit(1);});