tools/client/test_full_backpack.mjs (5369B)
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 let game; 12 const evidence = {}; 13 try { 14 const context = await browser.newContext({viewport: {width: 1024, height: 768}}); 15 await context.addCookies([{name: 'ao_client', value: 'full-backpack', url}]); 16 const page = await context.newPage(); 17 game = await GameTest.attach(page); 18 await page.goto(url); 19 await game.ready(); 20 const server = async () => (await page.request.get(url + '/test-state')).json(); 21 const carried = state => state.grids.flatMap(grid => grid.cells).find(cell => 22 cell.identity[0] === 104 && cell.identity[1] === 49 && !cell.dummy); 23 const open = async () => { 24 const cell = game.state.grids.flatMap(grid => grid.cells).find(cell => 25 cell.identity[0] === 104 && cell.identity[1] === 48 && !cell.dummy); 26 assert(cell, 'carried backpack is usable'); 27 await game.click((cell.bounds[0] + cell.bounds[2]) / 2, (cell.bounds[1] + cell.bounds[3]) / 2, 'right'); 28 }; 29 await game.key('i'); 30 await open(); 31 const bagGrid = state => state.grids.find(grid => grid.totalCells === 21 && 32 grid.cells.some(cell => cell.identity[0] === 107 && !cell.dummy)); 33 const scrollToEnd = async () => { 34 while (!bagGrid(game.state).cells.some(cell => cell.index === 20)) { 35 const grid = bagGrid(game.state); 36 const cell = grid.cells[0]; 37 const point = await game.point((cell.bounds[0] + cell.bounds[2]) / 2, (cell.bounds[1] + cell.bounds[3]) / 2); 38 await page.mouse.move(point.x, point.y); 39 await page.mouse.wheel(0, 120); 40 await game.wait('bag scroll row advances', state => bagGrid(state)?.firstRow > grid.firstRow); 41 } 42 }; 43 await game.wait('backpack opens', bagGrid); 44 await scrollToEnd(); 45 assert((await page.request.post(url + '/test-capacity')).ok()); 46 evidence.full = await server(); 47 assert.equal(evidence.full.Containers[1].length, 21); 48 const deposit = async () => { 49 const sequence = game.sequence; 50 const cell = carried(game.state); 51 assert(cell, 'carried source is usable'); 52 await game.click((cell.bounds[0] + cell.bounds[2]) / 2, (cell.bounds[1] + cell.bounds[3]) / 2); 53 await game.wait('carried source picked', state => state.dragged[0] === 104 && state.dragged[1] === 49); 54 const target = bagGrid(game.state).cells.find(cell => cell.index === 20); 55 await game.click((target.bounds[0] + target.bounds[2]) / 2, (target.bounds[1] + target.bounds[3]) / 2); 56 await game.wait('actual backpack deposit', () => game.events.some(event => event.sequence > sequence && 57 event.direction === 'framesent' && event.opcode === '47537a24' && 58 Buffer.from(event.hex, 'hex').readUInt32BE(37) === 51017)); 59 return sequence; 60 }; 61 const sequence = await deposit(); 62 await game.wait('refused full-bag source restored', state => carried(state) && game.events.some(event => 63 event.sequence > sequence && event.direction === 'framereceived' && event.opcode === '47537a24')); 64 evidence.refused = await server(); 65 assert.deepEqual(evidence.refused, evidence.full); 66 assert.deepEqual(game.state.dragged, [0, 0]); 67 await game.save(output + '/refused'); 68 69 // Free only the fixture slot, then retry through the same native window. 70 assert((await page.request.post(url + '/test-capacity')).ok()); 71 await deposit(); 72 await game.wait('same-window retry succeeds', state => !game.local(state).inventory.some(item => item.id === 2) && bagGrid(state)); 73 evidence.after = await server(); 74 assert.equal(evidence.after.Items.length, 1); 75 assert.equal(evidence.after.Items[0].ID, 1); 76 assert.equal(evidence.after.Containers[1][20].ID, 2); 77 assert.equal(evidence.after.Bank[0].ID, 23); 78 assert.deepEqual([...evidence.after.Items, ...evidence.after.Containers[1], ...evidence.after.Bank] 79 .map(item => item.ID).sort((a, b) => a - b), Array.from({length: 23}, (_, index) => index + 1)); 80 await game.save(output + '/retried'); 81 await page.reload(); 82 await game.ready(); 83 assert.deepEqual(game.local().inventory.map(item => [item.id, item.slot]), [[1, 48]]); 84 await page.waitForFunction(() => touchRegion(500, 400)?.world === true); 85 await game.snapshot(); 86 if (!game.state.grids.some(grid => grid.cells.some(cell => cell.identity[0] === 104 && !cell.dummy))) await game.key('i'); 87 await open(); 88 await game.wait('backpack retained after page replay', bagGrid); 89 await scrollToEnd(); 90 assert(bagGrid(game.state).cells.some(cell => cell.identity[0] === 107 && 91 cell.identity[1] === (1 << 16 | 20) && !cell.dummy)); 92 assert.equal(game.errors.length, 0); 93 console.log('PASS: full-backpack deposit restores its source and permits same-window retry/page/cold recovery'); 94 } finally { 95 try { 96 await fs.mkdir(output, {recursive: true}); 97 await fs.writeFile(output + '/verification.json', JSON.stringify(evidence, null, 2) + '\n'); 98 if (game) await game.save(output + '/final'); 99 } finally { await browser.close(); } 100 }