aorecomp

Anarchy Online classic in the browser

git clone https://aorecomp.ubeagle.com/code/aorecomp.git


tools/client/test_stale_backpack.mjs (5374B)


      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, phase] = 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-backpack', 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] === 107 && cell.identity[1] === (65536 | slot) && !cell.dummy);
     21   const carried = (state, slot) => state.grids.flatMap(grid => grid.cells).find(cell =>
     22     cell.identity[0] === 104 && cell.identity[1] === slot && !cell.dummy);
     23   const clickCell = async cell => game.click((cell.bounds[0] + cell.bounds[2]) / 2, (cell.bounds[1] + cell.bounds[3]) / 2);
     24   const inventory = async () => {
     25     if (!game.state.grids.some(grid => grid.totalCells === 30)) await game.key('KeyI');
     26   };
     27   await page.goto(url);
     28   await game.ready();
     29   await page.waitForFunction(() => touchRegion(500, 400)?.world === true);
     30   await game.snapshot();
     31   await inventory();
     32   if (phase === 'withdrawal') {
     33     const bag = carried(game.state, 48);
     34     await game.click((bag.bounds[0] + bag.bounds[2]) / 2, (bag.bounds[1] + bag.bounds[3]) / 2, 'right');
     35     await game.wait('both backpack sources open', state => source(state, 0) && source(state, 1));
     36     assert((await page.request.post(url + '/test-stale-view')).ok());
     37     evidence.transferred = await server();
     38     assert.deepEqual(evidence.transferred.Containers[1].map(item => [item.ID, item.Slot]), [[3, 1]]);
     39     assert.equal(evidence.transferred.Containers[4][0].ID, 2);
     40     await game.save(output + '/stale');
     41     const withdraw = async slot => {
     42       const sequence = game.sequence;
     43       await clickCell(source(game.state, slot));
     44       await game.wait('backpack source picked', state => state.dragged[0] === 107 && state.dragged[1] === (65536 | slot));
     45       await clickCell(game.state.grids.find(grid => grid.totalCells === 30).cells.find(cell => cell.identity[0] === 0));
     46       await game.wait('actual backpack withdrawal', () => game.events.some(event => event.sequence > sequence &&
     47         event.direction === 'framesent' && event.opcode === '47537a24' &&
     48         Buffer.from(event.hex, 'hex').readUInt32BE(29) === 107 && Buffer.from(event.hex, 'hex').readUInt32BE(33) === (65536 | slot)));
     49       return sequence;
     50     };
     51     const sequence = await withdraw(0);
     52     await game.wait('stale backpack request cancelled', () => game.events.some(event => event.sequence > sequence &&
     53       event.direction === 'framereceived' && event.opcode === '5e477770' &&
     54       Buffer.from(event.hex, 'hex').readUInt32BE(29) === 146));
     55     await game.wait('backpack refresh removes ghost and retains remaining source', state => !source(state, 0) && source(state, 1));
     56     assert.deepEqual(game.state.dragged, [0, 0]);
     57     evidence.refused = await server();
     58     assert.deepEqual(evidence.refused, evidence.transferred);
     59     await game.save(output + '/refused');
     60     await withdraw(1);
     61     await game.wait('remaining source transfers in same window', state => game.local(state).inventory.some(item =>
     62       item.id === 3 && item.slot === 50 && item.quality === 2 && item.count === 3));
     63     evidence.after = await server();
     64     assert.deepEqual(evidence.after.Items.map(item => [item.ID, item.Slot]), [[1, 64], [4, 65], [3, 66]]);
     65     assert.equal(evidence.after.Containers[1], undefined);
     66     await game.save(output + '/collected');
     67     await page.reload();
     68     await game.ready();
     69     await page.waitForFunction(() => touchRegion(500, 400)?.world === true);
     70     await game.snapshot();
     71     assert.deepEqual(await server(), evidence.after);
     72     await inventory();
     73   }
     74   assert.deepEqual(game.local().inventory.map(item => [item.id, item.slot, item.templates, item.quality, item.count]), [
     75     [1, 48, [99228, 99228], 1, 1], [4, 49, [99228, 99228], 1, 1], [3, 50, [25812, 25811], 2, 3]]);
     76   const bag = carried(game.state, 49);
     77   await game.click((bag.bounds[0] + bag.bounds[2]) / 2, (bag.bounds[1] + bag.bounds[3]) / 2, 'right');
     78   await game.wait('earlier transferred weapon visible in its retained backpack', state => source(state, 0));
     79   evidence.final = await server();
     80   assert.deepEqual(evidence.final.Containers[4], [{ID: 2, Type: 51018, Template: 123742, Quality: 2, Slot: 0, Count: 1, Ammo: 3}]);
     81   assert.deepEqual(game.errors, []);
     82   console.log('PASS: stale backpack withdrawal cancelled; same-window retry/page/cold conserve four IDs, three charges and three loaded rounds');
     83 } finally {
     84   try {
     85     await fs.mkdir(output, {recursive: true});
     86     await fs.writeFile(output + '/verification.json', JSON.stringify(evidence, null, 2) + '\n');
     87     if (game) await game.save(output + '/final');
     88   } finally { await browser.close(); }
     89 }