aorecomp

Anarchy Online classic in the browser

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


tools/client/test_stale_container_deposit.mjs (6352B)


      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-container-deposit', 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 = (state, page, slot) => state.grids.flatMap(grid => grid.cells).find(cell =>
     22     cell.identity[0] === page && cell.identity[1] === slot && !cell.dummy);
     23   const click = async (cell, button = 'left') => {
     24     assert(cell, 'native inventory cell exists');
     25     await game.click((cell.bounds[0] + cell.bounds[2]) / 2, (cell.bounds[1] + cell.bounds[3]) / 2, button);
     26   };
     27   const bagGrid = state => state.grids.find(grid => grid.totalCells === 21);
     28   const toggle = async (slot, open) => {
     29     await click(cell(game.state, 104, slot), 'right');
     30     await game.wait(open ? 'bag window opens' : 'bag window closes', state => Boolean(bagGrid(state)) === open);
     31   };
     32   const deposit = async () => {
     33     const sequence = game.sequence;
     34     await click(cell(game.state, 104, 50));
     35     await game.wait('carried source picked', state => state.dragged[0] === 104 && state.dragged[1] === 50);
     36     await click(bagGrid(game.state).cells.find(cell => cell.identity[0] === 0));
     37     await game.wait('actual native carried-to-backpack deposit', () => game.events.some(event => event.sequence > sequence &&
     38       event.direction === 'framesent' && event.opcode === '47537a24' &&
     39       Buffer.from(event.hex, 'hex').readUInt32BE(29) === 104 && Buffer.from(event.hex, 'hex').readUInt32BE(33) === 50 &&
     40       Buffer.from(event.hex, 'hex').readUInt32BE(37) === 51017));
     41     return sequence;
     42   };
     43 
     44   await game.key('KeyI');
     45   await toggle(48, true);
     46   assert((await page.request.post(url + '/test-stale-view')).ok());
     47   evidence.transferred = await server();
     48   assert.deepEqual(evidence.transferred.inventory.Containers[2], [{ID: 3, Type: 51018, Template: 123742, Quality: 2, Slot: 0, Count: 1, Ammo: 3}]);
     49   const equip = game.sequence;
     50   await click(cell(game.state, 104, 51), 'right');
     51   await game.wait('actual unrelated axe wear request', () => game.events.some(event => event.sequence > equip &&
     52     event.direction === 'framesent' && event.opcode === '47537a24'));
     53   evidence.pending = await server();
     54   assert.equal(evidence.pending.pending.itemID, 4);
     55   await game.wait('native axe animation ends', state => !game.local(state).actions.includes(3) && !game.local(state).actions.includes(6));
     56   await game.save(output + '/stale');
     57   const sequence = await deposit();
     58   await game.save(output + '/requested');
     59   await game.wait('stale carried deposit cancelled and empty row cleared', state => !cell(state, 104, 50) &&
     60     game.events.some(event => event.sequence > sequence && event.direction === 'framereceived' && event.opcode === '5e477770' &&
     61       Buffer.from(event.hex, 'hex').readUInt32BE(29) === 112));
     62   const replies = game.events.filter(event => event.sequence > sequence && event.direction === 'framereceived' &&
     63     event.opcode === '5e477770').map(event => Array.from({length: 5}, (_, index) => Buffer.from(event.hex, 'hex').readUInt32BE(29 + index * 4)));
     64   assert.deepEqual(replies, [[146, 104, 50, 0, 0], [112, 104, 50, 0, 0]]);
     65   assert.deepEqual(game.state.dragged, [0, 0]);
     66   evidence.refused = await server();
     67   assert.deepEqual(evidence.refused, evidence.pending);
     68   await game.save(output + '/refused');
     69   assert((await page.request.post(url + '/test-complete')).ok());
     70   await game.wait('original axe wear completes', state => game.local(state).inventory.some(item => item.id === 4 && item.slot === 6));
     71   assert.equal((await server()).pending, null);
     72 
     73   // Recover the exact earlier-transferred instance and retry on the same page.
     74   await toggle(48, false);
     75   await toggle(49, true);
     76   await game.wait('earlier transferred pistol visible', state => cell(state, 107, 2 << 16));
     77   await click(cell(game.state, 107, 2 << 16));
     78   await game.wait('contained pistol picked', state => state.dragged[0] === 107 && state.dragged[1] === (2 << 16));
     79   await click(game.state.grids.find(grid => grid.totalCells === 30).cells.find(cell => cell.identity[0] === 0));
     80   await game.wait('exact pistol withdrawn', state => game.local(state).inventory.some(item => item.id === 3 && item.slot === 50));
     81   await toggle(49, false);
     82   await toggle(48, true);
     83   await deposit();
     84   await game.wait('same-page deposit succeeds', state => cell(state, 107, 1 << 16) && !cell(state, 104, 50));
     85   evidence.after = await server();
     86   assert.deepEqual(evidence.after.inventory.Items.map(item => [item.ID, item.Slot]), [[1, 64], [2, 65], [4, 6]]);
     87   assert.deepEqual(evidence.after.inventory.Containers, {1: [{ID: 3, Type: 51018, Template: 123742, Quality: 2, Slot: 0, Count: 1, Ammo: 3}]});
     88   await game.save(output + '/retried');
     89   await page.reload();
     90   await game.ready();
     91   await page.waitForFunction(() => touchRegion(500, 400)?.world === true);
     92   await game.snapshot();
     93   if (!game.state.grids.some(grid => grid.totalCells === 30)) await game.key('KeyI');
     94   await toggle(48, true);
     95   await game.wait('retained loaded pistol visible after page replay', state => cell(state, 107, 1 << 16));
     96   assert.deepEqual(await server(), evidence.after);
     97   assert.deepEqual(game.errors, []);
     98   assert.equal(game.dropped, 0);
     99   console.log('PASS: stale deposit clears during unrelated equipment; completion/retry and page/cold retain four items and loaded rounds');
    100 } finally {
    101   try {
    102     await fs.mkdir(output, {recursive: true});
    103     await fs.writeFile(output + '/verification.json', JSON.stringify(evidence, null, 2) + '\n');
    104     if (game) await game.save(output + '/final');
    105   } finally { await browser.close(); }
    106 }