aorecomp

Anarchy Online classic in the browser

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


tools/client/test_idle_health.mjs (3533B)


      1 import assert from 'node:assert/strict';
      2 import {createRequire} from 'node:module';
      3 import {GameTest} from './game_test.mjs';
      4 
      5 const {chromium} = createRequire(import.meta.url)('playwright');
      6 const [url, output] = process.argv.slice(2);
      7 const browser = await chromium.launch({executablePath: '/usr/bin/chromium', args: [
      8   '--enable-gpu', '--use-gl=angle', '--use-angle=gl', '--no-sandbox', '--disable-dev-shm-usage',
      9   '--disable-background-timer-throttling', '--disable-renderer-backgrounding']});
     10 const games = [];
     11 const actor = game => game.state.actors.find(actor => actor.type === 50000 && actor.id === 1 && !actor.deleting);
     12 const corrections = (game, since) => game.events.filter(event => event.sequence > since &&
     13   event.direction === 'framereceived' && event.opcode === '3710256c' && event.actor?.[1] === 1);
     14 try {
     15   for (let index = 0; index < 2; index++) {
     16     const context = await browser.newContext({viewport: {width: 1024, height: 768}});
     17     await context.addCookies([{name: 'ao_client', value: 'idle-health-' + index, url}]);
     18     const page = await context.newPage(), game = await GameTest.attach(page);
     19     games.push(game);
     20     await page.goto(url);
     21     await game.ready();
     22   }
     23   const [owner, observer] = games;
     24   const state = async () => (await owner.page.request.get(url + '/test-state')).json();
     25   const initial = await state();
     26   await Promise.all(games.map(game => game.wait('full owner visible', () => actor(game)?.stats[27] === initial.maximum)));
     27   await owner.key('KeyX');
     28   await owner.wait('real sit changes recovery posture', () => owner.local().stats[430] === 2);
     29   assert.equal((await state()).sitting, true);
     30 
     31   // Watch native recovery across two corrections, including the gap between them.
     32   await Promise.all(games.map(async game => {
     33     const since = game.sequence;
     34     await game.wait('two full-health reconciliations', () => {
     35       assert.equal(actor(game).stats[27], initial.maximum, 'full native health drifted');
     36       return corrections(game, since).length >= 2;
     37     }, 25000);
     38     const packets = corrections(game, since);
     39     assert(packets[1].time - packets[0].time >= 8500, 'full health still broadcasts every two seconds');
     40   }));
     41 
     42   // Standing and an unannounced controlled injury restore the frequent correction.
     43   await owner.key('KeyX');
     44   await owner.wait('owner stands', () => owner.local().stats[430] !== 2);
     45   const sequences = games.map(game => game.sequence);
     46   assert.equal((await owner.page.request.post(url + '/test-injure')).status(), 204);
     47   const injured = await state();
     48   assert.equal(injured.health, initial.maximum - 9);
     49   await Promise.all(games.map(async (game, index) => {
     50     await game.wait('three injured-health corrections', () => corrections(game, sequences[index]).length >= 3, 9000);
     51     const packets = corrections(game, sequences[index]);
     52     assert(packets[2].time - packets[0].time < 6000, 'injury retained the slow full-health cadence');
     53     assert(packets.every(packet => Buffer.from(packet.hex, 'hex').readUInt32BE(29) === injured.health));
     54     await game.wait('native health matches injury', () => actor(game).stats[27] === injured.health);
     55   }));
     56   assert.equal((await state()).health, injured.health);
     57   assert(games.every(game => game.errors.length === 0));
     58   console.log('PASS: native full-health stability with slower reconciliation, real posture input and prompt injury corrections');
     59 } finally {
     60   try { await Promise.all(games.map((game, index) => game.save(output + '/final-' + index))); }
     61   finally { await browser.close(); }
     62 }