aorecomp

Anarchy Online classic in the browser

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


tools/client/test_closed_backpack_split.mjs (5394B)


      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: 'closed-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 = slot => game.state.grids.flatMap(grid => grid.cells).find(cell =>
     22     cell.identity[0] === 107 && cell.identity[1] === (65536 | slot) && !cell.dummy);
     23   const click = async source => {
     24     assert(source, 'the exact source must be usable');
     25     await game.click((source.bounds[0] + source.bounds[2]) / 2, (source.bounds[1] + source.bounds[3]) / 2);
     26   };
     27   const split = async () => {
     28     const sequence = game.sequence;
     29     await page.keyboard.down('Control');
     30     try { await click(cell(0)); } finally { await page.keyboard.up('Control'); }
     31     await game.wait('actual native backpack split', () => game.events.some(event => event.sequence > sequence &&
     32       event.direction === 'framesent' && event.opcode === '5e477770' && Buffer.from(event.hex, 'hex').readUInt32BE(29) === 52));
     33     return sequence;
     34   };
     35   await game.key('KeyI');
     36   await game.click(681, 45, 'right');
     37   await game.wait('original three-charge backpack source opens', () => cell(0));
     38   evidence.initial = await server();
     39   assert((await page.request.post(url + '/test-close-source')).ok());
     40   evidence.closed = await server();
     41   assert.equal(evidence.closed.open, false);
     42   const sequence = await split();
     43   evidence.requested = await server();
     44   await game.save(output + '/requested');
     45   assert.deepEqual(evidence.requested, evidence.closed);
     46   await game.wait('unavailable split window closes', state =>
     47     !state.grids.some(grid => grid.cells.some(cell => cell.identity[0] === 107)));
     48   assert(game.events.some(event => event.sequence > sequence && event.direction === 'framereceived' && event.opcode === '2049527c'));
     49 
     50   // Native splitting retains its selected source; place it through normal refusal.
     51   if (game.state.dragged[0] !== 0) {
     52     assert.deepEqual(game.state.dragged, [107, 65536]);
     53     const placement = game.sequence;
     54     await game.click(735, 45);
     55     await game.wait('closed-source placement refused', state => state.dragged[0] === 0 && game.events.some(event =>
     56       event.sequence > placement && event.direction === 'framereceived' && event.opcode === '5e477770' &&
     57       Buffer.from(event.hex, 'hex').readUInt32BE(29) === 146));
     58   }
     59   evidence.refused = await server();
     60   assert.deepEqual(evidence.refused, evidence.closed);
     61   await game.save(output + '/refused');
     62 
     63   // Ordinary Use must restore authoritative contents and permit a real split/join.
     64   await game.click(681, 45, 'right');
     65   await game.wait('closed bag reopens with no optimistic clone', () => cell(0) && !cell(1));
     66   const retry = await split();
     67   await game.wait('normal split acknowledged', () => game.events.some(event => event.sequence > retry &&
     68     event.direction === 'framereceived' && event.opcode === '5e477770' && Buffer.from(event.hex, 'hex').readUInt32BE(29) === 52));
     69   evidence.split = await server();
     70   assert.deepEqual(evidence.split.inventory.Containers[1].map(item => [item.ID, item.Slot, item.Count]), [[2, 0, 1], [3, 1, 2]]);
     71   if (game.state.dragged[0] !== 0) {
     72     const grid = game.state.grids.find(grid => grid.cells.some(cell => cell.identity[0] === 107));
     73     await click(grid.cells.find(source => source.identity[0] === 0 && source.index > 1));
     74     await game.wait('split source placed', state => state.dragged[0] === 0);
     75   }
     76   const join = game.sequence;
     77   await click(cell(1));
     78   await click(cell(0));
     79   await game.wait('normal join acknowledged', () => game.events.some(event => event.sequence > join &&
     80     event.direction === 'framereceived' && event.opcode === '5e477770' && Buffer.from(event.hex, 'hex').readUInt32BE(29) === 53));
     81   evidence.joined = await server();
     82   assert.deepEqual(evidence.joined.inventory.Containers, evidence.initial.inventory.Containers);
     83   await game.save(output + '/retried');
     84   await page.reload();
     85   await game.ready();
     86   await game.key('KeyI');
     87   await game.click(681, 45, 'right');
     88   await game.wait('page recovery retains the original three charges', () => cell(0) && !cell(1));
     89   assert.deepEqual((await server()).inventory, evidence.joined.inventory);
     90   assert.deepEqual(game.errors, []);
     91   console.log('PASS: closed-session optimistic split refuses without allocation; reopen, real split/join and page/cold ownership retain three charges');
     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 }