tools/client/test_stale_bank.mjs (4245B)
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', args: [ 9 '--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: 'stale-bank', url}]); 16 const page = await context.newPage(); 17 game = await GameTest.attach(page); 18 const server = async () => (await page.request.get(url + '/test-state')).json(); 19 const source = (state, slot) => state.grids.flatMap(grid => grid.cells).find(cell => 20 cell.identity[0] === 105 && cell.identity[1] === slot && !cell.dummy); 21 const clickCell = async cell => game.click((cell.bounds[0] + cell.bounds[2]) / 2, (cell.bounds[1] + cell.bounds[3]) / 2); 22 await page.goto(url); 23 await game.ready(); 24 await page.waitForFunction(() => touchRegion(500, 400)?.world === true); 25 await game.snapshot(); 26 await game.key('KeyI'); 27 if (!game.state.camera.firstPerson) await game.key('F8'); 28 await game.click(...await game.pick(51005, 3221357727), 'right'); 29 await game.wait('both bank sources open', state => source(state, 0) && source(state, 1)); 30 assert((await page.request.post(url + '/test-stale-view')).ok()); 31 evidence.transferred = await server(); 32 assert.deepEqual(evidence.transferred.Bank.map(item => [item.ID, item.Slot]), [[3, 1]]); 33 assert.equal(evidence.transferred.Containers[1][0].ID, 2); 34 await game.save(output + '/stale'); 35 const sequence = game.sequence; 36 await clickCell(source(game.state, 0)); 37 await game.wait('actual stale bank withdrawal', () => game.events.some(event => event.sequence > sequence && 38 event.direction === 'framesent' && event.opcode === '47537a24' && 39 Buffer.from(event.hex, 'hex').readUInt32BE(29) === 105 && Buffer.from(event.hex, 'hex').readUInt32BE(33) === 0)); 40 await game.wait('stale bank request cancelled', () => game.events.some(event => event.sequence > sequence && 41 event.direction === 'framereceived' && event.opcode === '5e477770' && 42 Buffer.from(event.hex, 'hex').readUInt32BE(29) === 146)); 43 await game.wait('bank refresh removes stale source and retains remaining item', state => !source(state, 0) && source(state, 1)); 44 assert.deepEqual(game.state.dragged, [0, 0]); 45 assert.deepEqual(await server(), evidence.transferred); 46 await game.save(output + '/refused'); 47 await clickCell(source(game.state, 1)); 48 await game.wait('remaining source transfers in same window', state => game.local(state).inventory.some(item => item.id === 3 && item.slot === 49)); 49 evidence.after = await server(); 50 assert.deepEqual(evidence.after.Items.map(item => [item.ID, item.Slot]), [[1, 64], [3, 65]]); 51 assert.equal(evidence.after.Bank.length, 0); 52 await game.save(output + '/collected'); 53 await page.reload(); 54 await game.ready(); 55 await page.waitForFunction(() => touchRegion(500, 400)?.world === true); 56 await game.snapshot(); 57 assert.deepEqual(game.local().inventory.map(item => [item.id, item.slot, item.templates]), 58 [[1, 48, [99228, 99228]], [3, 49, [42640, 42640]]]); 59 assert.deepEqual(await server(), evidence.after); 60 await game.key('KeyI'); 61 await game.click(684, 45, 'right'); 62 await game.wait('earlier transferred item retained in backpack after page replay', state => state.grids.some(grid => 63 grid.cells.some(cell => cell.identity[0] === 107 && cell.identity[1] === 65536 && !cell.dummy))); 64 assert.deepEqual(game.errors, []); 65 console.log('PASS: stale bank withdrawal cancelled, current contents usable and all three instances retained after page/cold recovery'); 66 } finally { 67 try { 68 await fs.mkdir(output, {recursive: true}); 69 await fs.writeFile(output + '/verification.json', JSON.stringify(evidence, null, 2) + '\n'); 70 if (game) await game.save(output + '/final'); 71 } finally { await browser.close(); } 72 }