aorecomp

Anarchy Online classic in the browser

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


tools/client/test_incoming_equipment.mjs (5526B)


      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, phase] = process.argv.slice(2);
      8 const browser = await chromium.launch({executablePath: '/usr/bin/chromium', headless: false,
      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 const games = [], evidence = {moves: []};
     12 try {
     13   for (let index = 0; index < 2; ++index) {
     14     const context = await browser.newContext({viewport: {width: 1024, height: 768}});
     15     await context.addCookies([{name: 'ao_client', value: 'incoming-' + index, url}]);
     16     const page = await context.newPage(), game = await GameTest.attach(page);
     17     games.push(game);
     18     await page.goto(url);
     19     await game.ready();
     20   }
     21   const [owner, observer] = games;
     22   const state = async () => (await owner.page.request.get(url + '/test-state')).json();
     23   evidence.initial = await state();
     24   const npc = game => game.state.actors.find(actor => actor.type === 50000 && actor.id === evidence.initial.npc);
     25   await Promise.all(games.map(game => game.wait('both players and original Leet', () =>
     26     npc(game) && game.state.actors.some(actor => actor.id === 1) && game.state.actors.some(actor => actor.id === 2))));
     27   if (phase === 'live') {
     28     await owner.key('KeyI');
     29     await owner.page.keyboard.press('Control+1');
     30     assert((await owner.page.request.post(url + '/test-start')).ok());
     31     await Promise.all(games.map(game => game.wait('actual incoming fight', () => npc(game).combatTarget === 1)));
     32     assert.equal(owner.local().combatTarget, 0);
     33     for (const action of ['replace', 'remove', 'equip']) {
     34       await owner.wait('native equipment action finishes', snapshot =>
     35         !owner.local(snapshot).actions.includes(3) && !owner.local(snapshot).actions.includes(6));
     36       const before = await state(), sequence = owner.sequence;
     37       if (action === 'remove') await owner.click(867, 206, 'right');
     38       else {
     39         const cell = owner.local().inventory.find(item => item.id === 2).slot - 48;
     40         await owner.click(681 + cell % 3 * 54, 45 + Math.floor(cell / 3) * 54, 'right');
     41       }
     42       await owner.wait('real equipment request', () => owner.events.some(event =>
     43         event.sequence > sequence && event.direction === 'framesent' && event.opcode === '47537a24'));
     44       const pending = await state();
     45       evidence.moves.push({action, before, pending});
     46       await owner.save(output + '/' + action + '-pending');
     47       assert.equal(pending.pending, true, 'Incoming attack refused valid native equipment');
     48       assert.deepEqual(pending.inventory, before.inventory);
     49       const slot = action === 'remove' ? 49 : 6;
     50       await owner.wait('native equipment completes under attack', snapshot =>
     51         owner.local(snapshot).inventory.some(item => item.id === 2 && item.slot === slot));
     52       const after = await state();
     53       evidence.moves.at(-1).after = after;
     54       assert.equal(after.fighting, 0);
     55       assert.equal(after.target, 1);
     56       assert.equal(after.pending, false);
     57       assert.deepEqual(owner.local().inventory.map(item => [item.id, item.slot, item.quality, item.count, item.templates]).sort((a, b) => a[0] - b[0]),
     58         [[1, 48, 1, 1, [209283, 209283]], [2, slot, 3, 1, [122691, 122692]]]);
     59       assert.equal(owner.local().combatTarget, 0);
     60       assert(owner.local().weapons.every(weapon => weapon.pending.length === 0));
     61       assert.equal(npc(owner).combatTarget, 1);
     62       assert.equal(npc(observer).combatTarget, 1);
     63       if (action === 'replace') {
     64         const received = owner.events.filter(event => event.sequence > sequence && event.direction === 'framereceived');
     65         const hit = received.find(event => event.opcode === '46002f16');
     66         const acknowledgement = received.find(event => event.opcode === '47537a24');
     67         assert(hit && acknowledgement && hit.sequence < acknowledgement.sequence, 'No real hit arrived during the equipment delay');
     68         assert(after.health < pending.health);
     69       }
     70       await owner.save(output + '/' + action + '-complete');
     71     }
     72     assert((await state()).health < evidence.initial.health, 'Incoming combat did not actually hit');
     73     assert((await owner.page.request.post(url + '/test-stop')).ok());
     74     await Promise.all(games.map(game => game.wait('incoming result queue drains', () =>
     75       npc(game).combatTarget === 0 && npc(game).weapons.every(weapon => weapon.pending.length === 0))));
     76     for (const game of games) { await game.page.reload(); await game.ready(); }
     77   }
     78   assert.deepEqual(owner.local().inventory.map(item => [item.id, item.slot, item.quality, item.count]).sort((a, b) => a[0] - b[0]),
     79     [[1, 48, 1, 1], [2, 6, 3, 1]]);
     80   assert.deepEqual(observer.local().inventory, []);
     81   evidence.final = {server: await state(), native: owner.local().inventory};
     82   assert(games.every(game => game.errors.length === 0));
     83   console.log('PASS:', phase, 'idle recipient replaces, removes and equips under real incoming hits; both instances survive page/cold recovery');
     84 } finally {
     85   try {
     86     await fs.mkdir(output, {recursive: true});
     87     await fs.writeFile(output + '/verification.json', JSON.stringify(evidence, null, 2));
     88     await Promise.all(games.map((game, index) => game.save(output + '/final-' + index)));
     89   } finally { await browser.close(); }
     90 }