aorecomp

Anarchy Online classic in the browser

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


tools/client/test_pending_loot.mjs (5015B)


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