File size: 10,242 Bytes
ef987bb | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 | 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);});
|