tools/client/test_inventory_grid.mjs (6396B)
1 import assert from 'node:assert/strict'; 2 import fs from 'node:fs/promises'; 3 import {createRequire} from 'node:module'; 4 import {GameTest} from './game_test.mjs'; 5 6 const {chromium} = createRequire(import.meta.url)('playwright'); 7 const [url, output] = process.argv.slice(2); 8 const browser = await chromium.launch({executablePath: '/usr/bin/chromium', 9 args: ['--enable-gpu', '--use-gl=angle', '--use-angle=gl', '--no-sandbox', '--disable-dev-shm-usage', 10 '--disable-background-timer-throttling', '--disable-renderer-backgrounding']}); 11 const evidence = {}; 12 let game; 13 try { 14 const context = await browser.newContext({viewport: {width: 1024, height: 480}}); 15 await context.addCookies([{name: 'ao_client', value: 'inventory-grid', url}]); 16 const page = await context.newPage(); 17 game = await GameTest.attach(page); 18 await page.goto(url); 19 await game.ready(); 20 await page.waitForFunction(() => touchRegion(500, 400)?.world === true); 21 evidence.before = await (await page.request.get(url + '/test-state')).json(); 22 await game.key('i'); 23 console.log('Initial grids:', JSON.stringify(game.state.grids.map(grid => ({...grid, cells: grid.cells.slice(0, 4)})))); 24 const inventoryGrid = state => state.grids.find(grid => grid.totalCells >= 28 && 25 grid.cells.some(cell => cell.identity[0] === 104 && cell.identity[1] >= 48 && cell.identity[1] < 78)); 26 await game.wait('native inventory grid', state => inventoryGrid(state)); 27 await game.save(output + '/top'); 28 29 // Cover every visual cell while observing the native scroll row. 30 const seen = new Set(); 31 const identities = new Set(); 32 evidence.rows = []; 33 for (let attempt = 0; attempt < 30; ++attempt) { 34 const grid = inventoryGrid(game.state); 35 evidence.rows.push(grid); 36 for (const cell of grid.cells) { 37 seen.add(cell.index); 38 if (cell.identity[0] === 104 && !cell.dummy) identities.add(cell.identity[1]); 39 } 40 if (grid.cells.some(cell => cell.index === grid.totalCells - 1)) break; 41 const [left, top, right, bottom] = grid.cells[0].bounds; 42 const point = await game.point((left + right) / 2, (top + bottom) / 2); 43 await page.mouse.move(point.x, point.y); 44 await page.mouse.wheel(0, 120); 45 await game.wait('native inventory row advances', state => inventoryGrid(state)?.firstRow > grid.firstRow); 46 } 47 let grid = inventoryGrid(game.state); 48 assert.equal(seen.size, grid.totalCells); 49 assert.deepEqual([...identities].sort((a, b) => a - b), game.local().inventory.map(item => item.slot).sort((a, b) => a - b)); 50 assert(grid.firstRow > 0); 51 const source = grid.cells.find(cell => cell.identity[0] === 104 && cell.identity[1] === 77); 52 const target = grid.cells.find(cell => !cell.dummy && cell.identity[0] === 104 && cell.identity[1] === 76); 53 assert(source && target); 54 assert.notEqual(source.index, 29, 'Sparse logical slots must not be interpreted as visual indices'); 55 evidence.source = source; 56 evidence.target = target; 57 const visible = grid.cells.filter(cell => !cell.dummy).map(cell => cell.identity[1]).sort((a, b) => a - b); 58 await game.save(output + '/bottom'); 59 60 await game.click((source.bounds[0] + source.bounds[2]) / 2, (source.bounds[1] + source.bounds[3]) / 2); 61 await game.wait('picked icon becomes a dummy', state => state.dragged[0] === 104 && state.dragged[1] === 77 && 62 inventoryGrid(state).cells.some(cell => cell.identity[0] === 104 && cell.identity[1] === 77 && cell.dummy)); 63 evidence.picked = game.state.dragged; 64 await game.click((target.bounds[0] + target.bounds[2]) / 2, (target.bounds[1] + target.bounds[3]) / 2); 65 await game.wait('occupied cell displaces the other item', state => state.dragged[0] === 104 && state.dragged[1] === 76 && 66 inventoryGrid(state).cells.some(cell => cell.index === target.index && cell.identity[0] === 104 && cell.identity[1] === 77 && !cell.dummy)); 67 evidence.displaced = game.state.dragged; 68 await game.click((source.bounds[0] + source.bounds[2]) / 2, (source.bounds[1] + source.bounds[3]) / 2); 69 await game.wait('visual swap completes with no held item', state => state.dragged[0] === 0 && state.dragged[1] === 0 && 70 inventoryGrid(state).cells.some(cell => cell.index === source.index && cell.identity[0] === 104 && cell.identity[1] === 76 && !cell.dummy)); 71 assert.deepEqual(inventoryGrid(game.state).cells.filter(cell => !cell.dummy).map(cell => cell.identity[1]).sort((a, b) => a - b), visible); 72 assert.deepEqual(await (await page.request.get(url + '/test-state')).json(), evidence.before); 73 await game.save(output + '/moved'); 74 75 await page.reload(); 76 await game.ready(); 77 await page.waitForFunction(() => touchRegion(500, 400)?.world === true); 78 await game.snapshot(); 79 if (!inventoryGrid(game.state)) await game.key('i'); 80 await game.wait('inventory after reload', state => inventoryGrid(state)); 81 for (let attempt = 0; attempt < 30; ++attempt) { 82 grid = inventoryGrid(game.state); 83 if (grid.cells.some(cell => cell.index === target.index && cell.identity[0] === 104 && cell.identity[1] === 77) && 84 grid.cells.some(cell => cell.index === source.index && cell.identity[0] === 104 && cell.identity[1] === 76)) break; 85 const [left, top, right, bottom] = grid.cells[0].bounds; 86 const point = await game.point((left + right) / 2, (top + bottom) / 2); 87 await page.mouse.move(point.x, point.y); 88 await page.mouse.wheel(0, 120); 89 await game.wait('reload scroll row advances', state => inventoryGrid(state)?.firstRow > grid.firstRow); 90 } 91 assert(inventoryGrid(game.state).cells.some(cell => cell.index === target.index && cell.identity[0] === 104 && cell.identity[1] === 77 && !cell.dummy)); 92 assert.deepEqual(await (await page.request.get(url + '/test-state')).json(), evidence.before); 93 assert.equal(game.errors.length, 0); 94 evidence.after = inventoryGrid(game.state); 95 assert.deepEqual(game.state.dragged, [0, 0]); 96 await game.save(output + '/replayed'); 97 await game.key('i'); 98 await game.wait('closed inventory has no active grids', state => state.grids.length === 0); 99 console.log('PASS: visible cells, sparse logical identities, complete wheel coverage, dummy state, visual swap and page retention'); 100 } finally { 101 try { 102 await fs.mkdir(output, {recursive: true}); 103 await fs.writeFile(output + '/verification.json', JSON.stringify(evidence, null, 2) + '\n'); 104 if (game) await game.save(output + '/final'); 105 } finally { await browser.close(); } 106 }