aorecomp

Anarchy Online classic in the browser

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


tools/client/test_replaced_backpack_split.mjs (5409B)


      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: 'replaced-backpack-split', 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 cell = position => game.state.grids.flatMap(grid => grid.cells).find(cell =>
     22     cell.identity[0] === 107 && cell.identity[1] === position && !cell.dummy);
     23   const click = async source => {
     24     assert(source, 'the exact native source must be usable');
     25     await game.click((source.bounds[0] + source.bounds[2]) / 2, (source.bounds[1] + source.bounds[3]) / 2);
     26   };
     27   await game.key('KeyI');
     28   await game.click(681, 45, 'right');
     29   await game.wait('original stack opens', () => cell(65536));
     30   evidence.initial = await server();
     31   assert((await page.request.post(url + '/test-move-source')).ok());
     32   evidence.replaced = await server();
     33   assert.equal(evidence.replaced.open, true);
     34   const empty = !evidence.replaced.inventory.Containers[1];
     35   if (!empty) assert.equal(evidence.replaced.inventory.Containers[1][0].ID, 4);
     36   assert(evidence.replaced.inventory.Containers[3].some(item => item.ID === 2 && item.Count === 3));
     37   const sequence = game.sequence;
     38   await page.keyboard.down('Control');
     39   try { await click(cell(65536)); } finally { await page.keyboard.up('Control'); }
     40   await game.wait('real split from the stale source', () => game.events.some(event => event.sequence > sequence &&
     41     event.direction === 'framesent' && event.opcode === '5e477770' && Buffer.from(event.hex, 'hex').readUInt32BE(29) === 52));
     42   await game.save(output + '/requested');
     43   await game.wait('stale-source split window closes', state =>
     44     !state.grids.some(grid => grid.cells.some(cell => cell.identity[0] === 107)));
     45   assert(game.events.some(event => event.sequence > sequence && event.direction === 'framereceived' && event.opcode === '2049527c'));
     46   if (game.state.dragged[0] !== 0) {
     47     await game.click(789, 45);
     48     await game.wait('selected stale source placement refuses', state => state.dragged[0] === 0);
     49   }
     50   evidence.refused = await server();
     51   assert.deepEqual(evidence.refused, {...evidence.replaced, open: false});
     52   await game.save(output + '/refused');
     53 
     54   // Reopen current contents and withdraw both actual items through normal input.
     55   const reopen = game.sequence;
     56   await game.click(681, 45, 'right');
     57   if (empty) {
     58     await game.wait('empty source bag reopens', () => game.events.some(event => event.sequence > reopen &&
     59       event.direction === 'framereceived' && event.opcode === '4e536976' && Buffer.from(event.hex, 'hex').readUInt32BE(29) === 1009));
     60     assert(!cell(65536) && !cell(65537));
     61     assert.equal((await server()).open, true);
     62     await game.click(735, 45, 'right');
     63     await game.wait('both retained items open in their actual bag', () => cell(131072) && cell(131073));
     64     await click(cell(131072));
     65   } else {
     66     await game.wait('actual replacement pistol opens', () => cell(65536));
     67     assert(!cell(65537));
     68     await click(cell(65536));
     69   }
     70   await game.click(789, 45);
     71   await game.wait('actual replacement pistol withdrawn', state => game.local(state).inventory.some(item =>
     72     item.id === 4 && item.slot === 50 && item.count === 1));
     73   if (!empty) {
     74     await game.click(735, 45, 'right');
     75     await game.wait('retained stack opens in its actual bag', () => cell(131072));
     76   }
     77   await click(cell(empty ? 131073 : 131072));
     78   await click(game.state.grids.find(grid => grid.totalCells === 30).cells.find(cell => cell.identity[0] === 0));
     79   await game.wait('retained three-charge stack withdrawn', state => game.local(state).inventory.some(item =>
     80     item.id === 2 && item.slot === 51 && item.count === 3));
     81   evidence.recovered = await server();
     82   assert.equal(evidence.recovered.inventory.Containers, null);
     83   assert.equal(evidence.recovered.inventory.LastID, 4);
     84   await game.save(output + '/recovered');
     85   await page.reload();
     86   await game.ready();
     87   assert.deepEqual(game.local().inventory.map(item => [item.id, item.slot, item.quality, item.count]).sort((a, b) => a[0] - b[0]),
     88     [[1, 48, 1, 1], [2, 51, 2, 3], [3, 49, 1, 1], [4, 50, 2, 1]]);
     89   assert.deepEqual((await server()).inventory, evidence.recovered.inventory);
     90   assert.deepEqual(game.errors, []);
     91   console.log('PASS: stale-source split closes its window and retains four IDs, three charges and three loaded rounds through reopening, normal withdrawals and page/cold recovery');
     92 } finally {
     93   try {
     94     await fs.mkdir(output, {recursive: true});
     95     await fs.writeFile(output + '/verification.json', JSON.stringify(evidence, null, 2) + '\n');
     96     if (game) await game.save(output + '/final');
     97   } finally { await browser.close(); }
     98 }