aorecomp

Anarchy Online classic in the browser

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


tools/client/test_stale_reclaim.mjs (3990B)


      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-reclaim', 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] === 106 && cell.identity[1] === slot && !cell.dummy);
     21   const click = 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   await game.click(...await game.pick(51059, 3221881537), 'right');
     28   await game.wait('both reclaim sources open', state => source(state, 0) && source(state, 1));
     29   assert((await page.request.post(url + '/test-stale-view')).ok());
     30   evidence.transferred = await server();
     31   assert.equal(evidence.transferred.Items, null);
     32   assert.deepEqual(evidence.transferred.Bank.map(item => [item.ID, item.Slot]), [[1, 0]]);
     33   assert.deepEqual(evidence.transferred.Reclaim.map(entry => entry.Item.ID), [2]);
     34   await game.save(output + '/stale');
     35   const sequence = game.sequence;
     36   await click(source(game.state, 0));
     37   await game.wait('actual stale reclaim withdrawal', () => game.events.some(event => event.sequence > sequence &&
     38     event.direction === 'framesent' && event.opcode === '47537a24' &&
     39     Buffer.from(event.hex, 'hex').readUInt32BE(29) === 106 && Buffer.from(event.hex, 'hex').readUInt32BE(33) === 0));
     40   await game.wait('stale reclaim 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('reclaim refresh removes ghost and preserves remaining slot', 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 click(source(game.state, 1));
     48   await game.wait('remaining item collected in same window', state => game.local(state).inventory.some(item => item.id === 2 && item.slot === 48));
     49   evidence.after = await server();
     50   assert.deepEqual(evidence.after.Items.map(item => [item.ID, item.Slot]), [[2, 64]]);
     51   assert.deepEqual(evidence.after.Bank, evidence.transferred.Bank);
     52   assert.equal(evidence.after.Reclaim, null);
     53   await game.save(output + '/collected');
     54   await page.reload();
     55   await game.ready();
     56   await page.waitForFunction(() => touchRegion(500, 400)?.world === true);
     57   await game.snapshot();
     58   assert.deepEqual(game.local().inventory.map(item => [item.id, item.slot, item.count]), [[2, 48, 1]]);
     59   assert.deepEqual(await server(), evidence.after);
     60   assert.deepEqual(game.errors, []);
     61   console.log('PASS: stale reclaim pickup cancelled, stable remaining slot collected and both exact instances retained through page/cold recovery');
     62 } finally {
     63   try {
     64     await fs.mkdir(output, {recursive: true});
     65     await fs.writeFile(output + '/verification.json', JSON.stringify(evidence, null, 2) + '\n');
     66     if (game) await game.save(output + '/final');
     67   } finally { await browser.close(); }
     68 }