aorecomp

Anarchy Online classic in the browser

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


tools/client/test_pending_reclaim.mjs (5110B)


      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',
      9   args: ['--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: 'pending-reclaim', 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 [x, y] = await game.pick(51059, 0xc00a02c1);
     22   await game.click(x, y, 'right');
     23   await game.wait('reclaim source icon', state => state.grids.some(grid => grid.cells.some(cell => cell.identity[0] === 106 && !cell.dummy)));
     24   await game.key('i');
     25   await page.keyboard.press('Control+1');
     26   const weapon = game.state.grids.flatMap(grid => grid.cells).find(cell => cell.identity[0] === 104 && !cell.dummy);
     27   assert(weapon);
     28   const equipSequence = game.sequence;
     29   await game.click((weapon.bounds[0] + weapon.bounds[2]) / 2, (weapon.bounds[1] + weapon.bounds[3]) / 2);
     30   await game.click(867, 206);
     31   await game.wait('actual equipment request', () => game.events.some(event => event.sequence > equipSequence &&
     32     event.direction === 'framesent' && event.opcode === '47537a24' && Buffer.from(event.hex, 'hex').readUInt32BE(29) === 104));
     33   evidence.pending = await server();
     34   assert(evidence.pending.pending);
     35   assert.deepEqual(evidence.pending.reclaimSlots, [2]);
     36   await game.wait('local equip ends while server completion remains pending', state =>
     37     !game.local(state).actions.includes(3) && !game.local(state).actions.includes(6));
     38   const sequence = game.sequence;
     39   await game.click(30, 45);
     40   await game.wait('actual reclaim pickup during pending equipment', () => game.events.some(event => event.sequence > sequence &&
     41     event.direction === 'framesent' && event.opcode === '47537a24' && Buffer.from(event.hex, 'hex').readUInt32BE(29) === 106));
     42   evidence.refused = await server();
     43   assert.deepEqual(evidence.refused, evidence.pending);
     44   await game.save(output + '/pending');
     45   await game.wait('pending reclaim pickup explicitly cancelled', () => game.events.some(event => event.sequence > sequence &&
     46     event.direction === 'framereceived' && event.opcode === '5e477770' && Buffer.from(event.hex, 'hex').readUInt32BE(29) === 146));
     47   await game.wait('reclaim source icon restored', state => state.grids.some(grid => grid.cells.some(cell =>
     48     cell.identity[0] === 106 && cell.identity[1] === 0 && !cell.dummy)));
     49   assert.deepEqual(game.state.dragged, [0, 0]);
     50   await game.save(output + '/refused');
     51   assert((await page.request.post(url + '/test-complete')).ok());
     52   await game.wait('original equipment completes', state => game.local(state).inventory.some(item => item.id === 1 && item.slot === 6));
     53   await game.click(30, 45);
     54   await game.wait('same-window reclaim retry succeeds', state => game.local(state).inventory.some(item => item.id === 2 && item.slot === 48));
     55   evidence.after = await server();
     56   assert.equal(evidence.after.pending, null);
     57   assert.deepEqual(evidence.after.reclaimSlots, [0]);
     58   assert.deepEqual(evidence.after.inventory, {...evidence.pending.inventory, Reclaim: null, Items: [
     59     {...evidence.pending.inventory.Items[0], Slot: 6}, {...evidence.pending.inventory.Reclaim[0].Item, Slot: 64}]});
     60   assert.equal(game.events.filter(event => event.sequence > sequence && event.direction === 'framesent' &&
     61     event.opcode === '47537a24' && Buffer.from(event.hex, 'hex').readUInt32BE(29) === 106).length, 2);
     62   await game.save(output + '/picked');
     63   await page.reload();
     64   await game.ready();
     65   await page.waitForFunction(() => touchRegion(500, 400)?.world === true);
     66   await game.snapshot();
     67   if (!game.state.grids.some(grid => grid.cells.some(cell => cell.identity[0] === 104 && !cell.dummy))) await game.key('i');
     68   await game.wait('recovered part visible after replay', state => state.grids.some(grid => grid.cells.some(cell =>
     69     cell.identity[0] === 104 && cell.identity[1] === 48 && !cell.dummy)));
     70   assert.deepEqual(game.local().inventory.map(item => [item.id, item.slot, item.quality, item.count]).sort((a, b) => a[0] - b[0]),
     71     [[1, 6, 1, 1], [2, 48, 1, 1]]);
     72   assert.equal(game.errors.length, 0);
     73   console.log('PASS: pending equipment reclaim refusal restores its source, retains the move and permits exact retry/page/cold recovery');
     74 } finally {
     75   try {
     76     await fs.mkdir(output, {recursive: true});
     77     await fs.writeFile(output + '/verification.json', JSON.stringify(evidence, null, 2) + '\n');
     78     if (game) await game.save(output + '/final');
     79   } finally { await browser.close(); }
     80 }