fix: limit trainer jumps and exit positions
This commit is contained in:
1 parent
27b3e0aaae
commit
cc6bcb23c8
12 files changed
+174
-79
No files matched your search
@@ -7,14 +7,31 @@ const {chromium} = require('playwright');
|
||||
const origin = process.env.BELKI_TEST_URL || 'http://127.0.0.1:8876';
|
||||
assert(['127.0.0.1','localhost','[::1]'].includes(new URL(origin).hostname));
|
||||
const shotDir = process.env.BELKI_SCREENSHOT_DIR;
|
||||
const allowedExits = [19,29,39,49,55,56,57,58,59];
|
||||
function seeded(seed = 19) { return () => { seed = (Math.imul(seed,1664525)+1013904223) >>> 0; return seed / 2 ** 32; }; }
|
||||
const cellIds = locator => locator.evaluateAll(nodes=>nodes.map(n=>Number(n.dataset.id)));
|
||||
async function checkTransitions(page, expectedExit) {
|
||||
const sceneExit = await cellIds(page.locator('#scene-grid .exit button'));
|
||||
assert.equal(sceneExit.length,1);
|
||||
const [exit] = sceneExit;
|
||||
assert(allowedExits.includes(exit),`unexpected exit cell: ${exit}`);
|
||||
if (expectedExit !== undefined) assert.equal(exit,expectedExit);
|
||||
for (const grid of ['scene-grid','map-grid']) {
|
||||
assert.deepEqual(await cellIds(page.locator(`#${grid} .entrance button`)),[0]);
|
||||
assert.deepEqual(await cellIds(page.locator(`#${grid} .exit button`)),[exit]);
|
||||
assert.equal(await page.locator(`#${grid} .transition.available`).count(),0);
|
||||
}
|
||||
return exit;
|
||||
}
|
||||
|
||||
async function run() {
|
||||
const {generateField, DEFAULTS, coordinate, neighbors} = await import(pathToFileURL(path.resolve(__dirname,'../../web/lu-trainer/engine.js')));
|
||||
const field = generateField(DEFAULTS,seeded());
|
||||
const [first, second, note] = field.base;
|
||||
const hintCell = field.base.find(id => field.values[id] > 0);
|
||||
assert.notEqual(hintCell, undefined);
|
||||
const first = 1, second = 2, note = 30;
|
||||
const hintIndex = field.base.findIndex(id => field.values[id] > 0);
|
||||
assert(hintIndex > 0,'hint fixture needs a quiet approach cell to revisit from');
|
||||
const hintRoute = field.base.slice(0,hintIndex + 1);
|
||||
const hintCell = hintRoute.at(-1), beforeHint = hintRoute.at(-2);
|
||||
const hintText = `Подсказка: ${coordinate(neighbors(hintCell).find(id => field.hazards.has(id)))}`;
|
||||
const browser = await chromium.launch({headless:true, ...(process.env.CHROMIUM_PATH ? {executablePath:process.env.CHROMIUM_PATH} : {})});
|
||||
const captures = [];
|
||||
@@ -40,21 +57,33 @@ async function run() {
|
||||
const map = id => page.locator(`#map-grid button[data-id="${id}"]`);
|
||||
const noOverflow = async () => assert(await page.evaluate(()=>document.documentElement.scrollWidth <= innerWidth),`overflow at ${width}`);
|
||||
const select = async id => { await scene(id).click(); if (width === 390) await page.locator('#jump').click(); };
|
||||
const assertAvailable = async ids => {
|
||||
for (const grid of ['scene-grid','map-grid']) {
|
||||
assert.deepEqual(await cellIds(page.locator(`#${grid} .available button`)),ids,`${grid} jump targets`);
|
||||
}
|
||||
};
|
||||
await noOverflow();
|
||||
assert.match(await scene(0).getAttribute('aria-label'), /^x1 y1: вход, персонаж здесь$/);
|
||||
for (const grid of ['scene-grid','map-grid']) {
|
||||
assert.equal(await page.locator(`#${grid} .entrance button`).getAttribute('data-id'),'0');
|
||||
assert.equal(await page.locator(`#${grid} .exit button`).getAttribute('data-id'),String(field.exit));
|
||||
}
|
||||
await checkTransitions(page,field.exit);
|
||||
assert(await page.locator('#jump').isDisabled());
|
||||
assert(await page.locator('#mark').isDisabled());
|
||||
assert.match(await page.locator('#transition-coordinates').textContent(), /Вход: x1 y1/);
|
||||
assert((await page.locator('#transition-coordinates').textContent()).includes(`Выход: ${coordinate(field.exit)}`));
|
||||
assert.equal(await page.locator('#scene-grid .hazard').count(),0);
|
||||
assert.equal(await page.locator('#scene-grid .opened').count(),0);
|
||||
await assertAvailable([1,10,11]);
|
||||
// Modal help must not modify the field, and Escape must restore focus.
|
||||
await page.locator('#help-open').click();
|
||||
assert(await page.locator('#help-dialog').isVisible());
|
||||
await page.keyboard.press('Escape');
|
||||
assert(await page.locator('#help-open').evaluate(n=>n===document.activeElement));
|
||||
// Even a safe upper-row cell is out of reach two columns from the entrance.
|
||||
await select(second);
|
||||
assert(await page.locator('#jump').isEnabled());
|
||||
assert.match(await page.locator('#status').textContent(),/соседнюю клетку/);
|
||||
assert.deepEqual(await cellIds(page.locator('#scene-grid .current button')),[0]);
|
||||
assert.equal(await page.locator('#history li').count(),0);
|
||||
assert.equal(await page.locator('#scene-grid .opened').count(),0);
|
||||
if (width === 390) {
|
||||
await scene(first).tap();
|
||||
assert.equal(await page.locator('#scene-grid .opened').count(),0,'touch overview must only select');
|
||||
@@ -66,6 +95,7 @@ async function run() {
|
||||
assert.equal(await page.locator('#history li').count(),1);
|
||||
assert.match(await scene(first).getAttribute('aria-label'),/персонаж здесь/);
|
||||
assert.match(await page.locator('#messages').textContent(),/Опасностей рядом/);
|
||||
await assertAvailable([2,10,11,12]);
|
||||
// Native button keyboard actions and spatial focus; focus is not character position.
|
||||
await scene(first).focus(); await page.keyboard.press('ArrowRight');
|
||||
assert(await scene(second).evaluate(n=>n===document.activeElement));
|
||||
@@ -73,8 +103,20 @@ async function run() {
|
||||
await page.keyboard.press('Enter');
|
||||
assert.equal(await page.locator('#scene-grid .opened').count(),2);
|
||||
assert.equal(await page.locator('#history li').count(),2);
|
||||
await assertAvailable([1,3,11,12,13]);
|
||||
await page.keyboard.press(' ');
|
||||
assert.equal(await page.locator('#history li').count(),3,'one keyboard action, one history item');
|
||||
assert.equal(await page.locator('#history li').count(),2,'cannot jump to the current cell');
|
||||
// Cell 10 still touches opened cell 1, but not the current position (cell 2).
|
||||
await select(10);
|
||||
assert.match(await page.locator('#status').textContent(),/соседнюю клетку/);
|
||||
assert.deepEqual(await cellIds(page.locator('#scene-grid .current button')),[second]);
|
||||
assert.equal(await page.locator('#history li').count(),2);
|
||||
await scene(second).focus(); await page.keyboard.press('ArrowLeft');
|
||||
await page.keyboard.press(' ');
|
||||
assert.equal(await page.locator('#history li').count(),3,'one keyboard revisit, one history item');
|
||||
assert.match(await page.locator('#history li').last().textContent(),/Повторный прыжок/);
|
||||
assert.deepEqual(await cellIds(page.locator('#scene-grid .current button')),[first]);
|
||||
assert.equal(await page.locator('#scene-grid .opened').count(),2);
|
||||
await page.keyboard.press('End');
|
||||
assert(await scene(9).evaluate(n=>n===document.activeElement));
|
||||
await page.keyboard.press('Home'); assert(await scene(0).evaluate(n=>n===document.activeElement));
|
||||
@@ -83,8 +125,9 @@ async function run() {
|
||||
await map(note).click();
|
||||
assert.equal(await page.locator('#scene-grid .opened').count(),2);
|
||||
await page.locator('#map-mark').click();
|
||||
assert.equal(await page.locator('#map-grid .marked').count(),1);
|
||||
assert.equal(await page.locator('#scene-grid .marked').count(),1);
|
||||
assert.deepEqual(await cellIds(page.locator('#map-grid .marked button')),[note]);
|
||||
assert.deepEqual(await cellIds(page.locator('#scene-grid .marked button')),[note]);
|
||||
assert.deepEqual(await cellIds(page.locator('#scene-grid .current button')),[first]);
|
||||
await page.locator('#project-marks').uncheck();
|
||||
assert.equal(await page.locator('#scene-grid .marked').count(),0);
|
||||
assert.equal(await page.locator('#map-grid .marked').count(),1);
|
||||
@@ -94,7 +137,7 @@ async function run() {
|
||||
await page.locator('#clear-marks').click(); await page.keyboard.press('Escape');
|
||||
assert.equal(await page.locator('#map-grid .marked').count(),1);
|
||||
await page.locator('#clear-marks').click(); await page.locator('#confirm-action').click();
|
||||
assert.equal(await page.locator('#map-grid .marked').count(),0);
|
||||
await page.locator('#map-grid .marked').waitFor({state:'detached'});
|
||||
assert.equal(await page.locator('#map-grid .opened').count(),2);
|
||||
assert.equal(await page.locator('#history li').count(),5);
|
||||
if (width === 390) {
|
||||
@@ -125,11 +168,34 @@ async function run() {
|
||||
assert.equal(await page.locator('#history li').count(),0);
|
||||
assert.equal(await page.locator('#timer').textContent(),'00:00');
|
||||
assert.match(await scene(0).getAttribute('aria-label'), /вход, персонаж здесь/);
|
||||
assert.equal(await page.locator('#scene-grid .exit button').getAttribute('data-id'), await page.locator('#map-grid .exit button').getAttribute('data-id'));
|
||||
await checkTransitions(page);
|
||||
await assertAvailable([1,10,11]);
|
||||
assert.deepEqual(errors,[]);
|
||||
console.log(`PASS ${width}x${height}: render, keyboard/touch, synchronized map, notes, dialogs, settings, console`);
|
||||
console.log(`PASS ${width}x${height}: render, adjacent jumps, keyboard/touch, synchronized map, notes, dialogs, settings, console`);
|
||||
} finally { await context.close(); }
|
||||
}
|
||||
// Sample all exit buckets without coupling this test to their internal order.
|
||||
const exits = new Set();
|
||||
const boundary = await browser.newContext({viewport:{width:1366,height:768}});
|
||||
try {
|
||||
for (let i = 0; i < allowedExits.length; i++) {
|
||||
const page = await boundary.newPage();
|
||||
try {
|
||||
await page.addInitScript(roll => { Math.random = () => roll; },(i + .5) / allowedExits.length);
|
||||
await page.goto(`${origin}/trainer`);
|
||||
await page.locator('#scene-grid button').last().waitFor();
|
||||
const exit = await checkTransitions(page);
|
||||
exits.add(exit);
|
||||
await page.locator('#map-grid .exit button').click();
|
||||
assert(await page.locator('#jump').isDisabled());
|
||||
assert(await page.locator('#map-mark').isDisabled());
|
||||
assert.equal(await page.locator('#scene-grid .opened').count(),0);
|
||||
assert.equal(await page.locator('#history li').count(),0);
|
||||
} finally { await page.close(); }
|
||||
}
|
||||
assert.deepEqual([...exits].sort((a,b)=>a-b),allowedExits);
|
||||
console.log('PASS all permitted exit cells, synchronized transitions, exit cannot be jumped to or marked');
|
||||
} finally { await boundary.close(); }
|
||||
// Deterministic result/clock/hint lifetime checks at desktop size.
|
||||
const context = await browser.newContext({viewport:{width:1366,height:768}});
|
||||
try {
|
||||
@@ -138,17 +204,17 @@ async function run() {
|
||||
await page.goto(`${origin}/trainer`);
|
||||
await page.locator('#scene-grid button').last().waitFor();
|
||||
const cell = id=>page.locator(`#scene-grid button[data-id="${id}"]`);
|
||||
// Open safe reachable cells until a known adjacent hazard can be reached.
|
||||
let hazard;
|
||||
for (let guard=0;guard<60;guard++) {
|
||||
const ids = await page.locator('#scene-grid .available:not(.opened):not(.transition) button').evaluateAll(nodes=>nodes.map(n=>Number(n.dataset.id)));
|
||||
hazard = ids.find(id=>field.hazards.has(id));
|
||||
if (hazard !== undefined) break;
|
||||
const next = ids.find(id=>!field.hazards.has(id)); assert.notEqual(next,undefined); await cell(next).click();
|
||||
// The seeded route includes a diagonal jump from the entrance.
|
||||
const hazard = 31;
|
||||
for (const id of [11,20]) {
|
||||
assert(!field.hazards.has(id),`loss fixture: approach cell ${id} must be safe`);
|
||||
await cell(id).click();
|
||||
assert.match(await cell(id).getAttribute('aria-label'),/персонаж здесь/);
|
||||
}
|
||||
assert.notEqual(hazard,undefined); await cell(hazard).click();
|
||||
assert(field.hazards.has(hazard),'loss fixture: final cell must be hazardous');
|
||||
await cell(hazard).click();
|
||||
assert(await page.locator('#result').isVisible());
|
||||
assert.match(await page.locator('#result').textContent(),/Падение/);
|
||||
assert((await page.locator('#result').textContent()).includes(`Падение на ${coordinate(hazard)}`));
|
||||
assert.equal(await page.locator('#scene-grid .hazard').count(),field.hazards.size);
|
||||
const count = await page.locator('#history li').count();
|
||||
await cell(1).click(); assert.equal(await page.locator('#history li').count(),count);
|
||||
@@ -172,17 +238,18 @@ async function run() {
|
||||
await page.setViewportSize({width:1366,height:768});
|
||||
await page.reload(); await page.locator('#scene-grid button').last().waitFor();
|
||||
await page.evaluate(()=>Math.random=()=>0);
|
||||
await cell(hintCell).click();
|
||||
for (const id of hintRoute) await cell(id).click();
|
||||
assert.equal(await page.locator('#scene-grid .hint').count(),1);
|
||||
assert.equal(await page.locator('#map-grid .hint').count(),1);
|
||||
assert((await page.locator('#messages').textContent()).includes(hintText));
|
||||
await page.waitForTimeout(1100); await cell(hintCell).click();
|
||||
const revisitHint = async () => { await cell(beforeHint).click(); await cell(hintCell).click(); };
|
||||
await page.waitForTimeout(1100); await revisitHint();
|
||||
await page.waitForTimeout(1100);
|
||||
assert.equal(await page.locator('#scene-grid .hint').count(),1,'repeat hint renews the full display duration');
|
||||
await page.waitForTimeout(1000);
|
||||
assert.equal(await page.locator('#scene-grid .hint').count(),0);
|
||||
assert((await page.locator('#messages').textContent()).includes(hintText));
|
||||
await cell(hintCell).click();
|
||||
await revisitHint();
|
||||
assert.equal(await page.locator('#scene-grid .hint').count(),1);
|
||||
await page.locator('#new-game').click(); await page.locator('#confirm-action').click();
|
||||
await page.waitForTimeout(2100);
|
||||
|
||||
Reference in new issue
Block a user