File size: 6,697 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 | const fs = require('fs');
const path = require('path');
const {pathToFileURL} = require('url');
const {chromium} = require('playwright');
const assert = require('assert');
(async () => {
const browser = await chromium.launch({headless: true});
try {
const page = await browser.newPage({viewport: {width: 1100, height: 760}});
await page.goto(pathToFileURL(path.join(process.cwd(), 'index.html')).href);
const reset = config => page.evaluate(config => gameTest.reset(config), config);
const state = () => page.evaluate(() => gameTest.state());
const capture = async name => {
fs.mkdirSync('/logs/verifier/visual-audit', {recursive: true});
await page.screenshot({path: `/logs/verifier/visual-audit/${name}.png`, fullPage: true});
};
assert.equal(await page.locator('[data-expand]').count(), 4);
await reset({oxygen: 2, duplicant: {r: 5, c: 2, water: 2}, openCells: [[5, 2], [6, 2], [6, 3]]});
let current = await page.evaluate(() => gameTest.step(2000));
assert(current.oxygen === 0 && current.duplicants[0].sick);
assert.equal(current.duplicants[0].r, 6, 'gravity must move through known open space');
current = await page.evaluate(() => gameTest.move(6, 3));
assert.equal(current.duplicants[0].c, 3);
assert.equal(await page.locator('#oxygen').innerText(), '0');
await capture('depleted-and-sick');
await reset({oxygen: 60, duplicant: {r: 6, c: 2, water: 60}, openCells: [[6, 2]]});
current = await state();
await page.waitForTimeout(1100);
assert.deepStrictEqual(await state(), current, 'real timers must not advance virtual-clock mode');
await page.evaluate(() => gameTest.step(999));
assert.equal((await state()).oxygen, 60);
await page.evaluate(() => gameTest.step(1));
assert.equal((await state()).oxygen, 59);
current = await page.evaluate(() => gameTest.move(6, 3));
assert(!current.map[6][3].known && current.duplicants[0].c === 2, 'move must not reveal unknown rock');
current = await page.evaluate(() => gameTest.dig(3, 2));
assert(current.map[3][2].known);
current = await page.evaluate(() => gameTest.dig(2, 2));
assert(!current.map[2][2].known, 'upward reach is at most three cells');
current = await page.evaluate(() => gameTest.dig(6, 4));
assert(!current.map[6][4].known, 'horizontal digging reach is at most one cell');
const runAction = (name, row, col) => page.evaluate(
([name, row, col]) => gameTest[name](row, col),
[name, row, col],
);
await reset({oxygen: 60, duplicant: {r: 6, c: 2, water: 0}, openCells: [[5, 2], [6, 2]]});
current = await page.evaluate(() => gameTest.step(1000));
assert(current.oxygen > 0 && current.duplicants[0].sick, 'dehydration alone must cause illness');
current = await runAction('dig', 5, 2);
assert.equal(current.duplicants[0].r, 6, 'digging known empty space must not move the duplicant');
// Use the returned dimensions instead of baking in the reference map size.
const {rows, cols} = (await state()).dimensions;
await reset({duplicant: {r: rows - 1, c: cols - 1, water: 60}, openCells: [[rows - 1, cols - 1]]});
current = await runAction('move', rows - 1, cols);
assert(current.dimensions.cols > cols, 'an outside move should expand the requested edge');
assert(!current.map[rows - 1][cols].known && current.duplicants[0].c === cols - 1,
'an outside move may not excavate newly discovered rock');
await reset({duplicant: {r: rows - 1, c: cols - 1, water: 60}, openCells: [[rows - 1, cols - 1]]});
current = await runAction('dig', rows - 1, cols);
assert(current.dimensions.cols > cols && current.map[rows - 1][cols].known,
'a reachable outside rock should expand and be excavated');
assert.equal(await page.locator('.cell').count(), current.dimensions.rows * current.dimensions.cols);
const distant = await runAction('dig', -1, 0);
assert(distant.dimensions.rows > current.dimensions.rows, 'an outside dig should expand even when out of reach');
const addedRows = distant.dimensions.rows - current.dimensions.rows;
assert(!distant.map[addedRows - 1][0].known, 'expansion may not bypass digging range');
for (const resource of ['open', 'oxygen', 'water']) {
await reset({oxygen: 40, duplicant: {r: 6, c: 2, water: 50}, openCells: [[6, 2]], resourceCells: [{r: 6, c: 3, type: resource}]});
await page.locator('.cell[data-r="6"][data-c="2"]').click();
await page.locator('.cell[data-r="6"][data-c="3"]').click();
current = await state();
assert(current.map[6][3].known && current.map[6][3].type === resource, 'real click must reveal injected resource');
if (resource === 'open') assert(current.duplicants[0].water < 49, 'digging must consume water faster than a quiet one-second tick');
if (resource === 'oxygen') assert(current.oxygen > 40, 'oxygen discovery must replenish environment');
if (resource === 'water') assert(current.duplicants[0].water > 50, 'water discovery must alleviate dehydration');
assert.equal(Number(await page.locator('#oxygen').innerText()), current.oxygen);
}
await capture('water-discovery');
await reset({});
const count = (await state()).duplicants.length;
await page.evaluate(() => gameTest.answerPrint(true));
assert.equal((await state()).duplicants.length, count, 'no pending request may not print a free duplicant');
await page.evaluate(() => gameTest.step(9999));
assert(!(await state()).printPending);
await page.evaluate(() => gameTest.step(1));
assert((await state()).printPending && await page.locator('[data-testid="print-prompt"]').isVisible());
await capture('ten-second-print-prompt');
await page.locator('[data-answer-print="no"]').click();
assert(!(await state()).printPending && (await state()).duplicants.length === count);
await page.evaluate(() => gameTest.step(10000));
await page.locator('[data-answer-print="yes"]').click();
assert.equal((await state()).duplicants.length, count + 1);
for (const direction of ['up', 'down', 'left', 'right']) {
const before = (await state()).dimensions;
await page.locator(`[data-expand="${direction}"]`).click();
current = await state();
const key = ['up', 'down'].includes(direction) ? 'rows' : 'cols';
assert(current.dimensions[key] > before[key], `${direction} navigation failed to expand`);
assert.equal(await page.locator('.cell').count(), current.dimensions.rows * current.dimensions.cols);
}
await capture('expanded-map');
} finally {await browser.close();}
})().catch(error => {console.error(error); process.exit(1);});
|