aorecomp

Anarchy Online classic in the browser

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


tools/client/test_healing_over_time.mjs (6026B)


      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', 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 = {};
     12 const state = async () => (await games[0].page.request.get(url + '/test-state')).json();
     13 const actor = (game, id) => game.state.actors.find(actor => actor.type === 50000 && actor.id === id);
     14 const pending = (game, id) => actor(game, id)?.weapons.some(weapon => weapon.pending.length);
     15 const waitState = async (label, predicate) => {
     16   const deadline = Date.now() + 10000;
     17   for (;;) {
     18     const current = await state();
     19     if (predicate(current)) return current;
     20     assert(Date.now() < deadline, 'Timed out waiting for ' + label);
     21     await games[0].snapshot();
     22   }
     23 };
     24 try {
     25   // Load the observer first so the caster's regeneration phase stays outside this short check.
     26   for (const index of [1, 0]) {
     27     const context = await browser.newContext({viewport: {width: 1024, height: 768}});
     28     await context.addCookies([{name: 'ao_client', value: 'hot-' + index, url}]);
     29     const page = await context.newPage();
     30     const game = await GameTest.attach(page);
     31     games[index] = game;
     32     await page.goto(url);
     33     await game.ready(); assert.equal((await game.page.request.post(url + '/test-defer-recovery')).status(), 204);
     34   }
     35   const [owner] = games;
     36   const initial = await state();
     37   evidence.initial = initial;
     38   await Promise.all(games.map(game => game.wait('both patients and the Leet', () =>
     39     actor(game, initial.owner) && actor(game, initial.observer) && actor(game, initial.enemy))));
     40   await owner.key('KeyI');
     41   await owner.click(681, 45, 'right');
     42   await owner.wait('Fixer HoT crystal learned', snapshot => owner.local(snapshot).inventory.length === 0);
     43   await owner.page.keyboard.press('Control+8');
     44   await owner.click(974, 76);
     45   await owner.key('F1');
     46   await owner.save(output + '/ready');
     47 
     48   const sequence = owner.sequence;
     49   await owner.click(857, 128, 'right');
     50   await waitState('HoT installed', state => state.effects?.length === 1);
     51   await waitState('first HoT tick near deadline', state => Date.now() >= Date.parse(state.effects[0].nextTick) - 500);
     52   assert.equal((await owner.page.request.post(url + '/test-attack')).status(), 204);
     53   evidence.pending = {server: await state()};
     54   assert.equal(evidence.pending.server.cast, null);
     55   assert.equal(evidence.pending.server.effects[0].ticks, 40);
     56   assert(evidence.pending.server.health < 200);
     57   await Promise.all(games.map(game => game.wait('incoming result before the HoT tick', () => pending(game, initial.enemy))));
     58   evidence.pending.clients = games.map(game => ({health: actor(game, initial.owner).stats[27], enemy: actor(game, initial.enemy)}));
     59   assert(evidence.pending.clients.every(client => client.health === 200));
     60   await Promise.all(games.map(async game => {
     61     await game.wait('HoT recovery before queued impact', () => actor(game, initial.owner).stats[27] !== 200 && pending(game, initial.enemy));
     62     return {health: actor(game, initial.owner).stats[27], enemy: actor(game, initial.enemy)};
     63   })).then(clients => { evidence.beforeImpact = clients; });
     64   const healed = await state();
     65   evidence.healed = healed;
     66   assert(healed.health >= evidence.pending.server.health + 2 && healed.health <= evidence.pending.server.health + 6);
     67   assert.equal(healed.nano, 116);
     68   assert.equal(healed.reserved, 0);
     69   assert.equal(healed.cast, null);
     70   assert.equal(healed.effects[0].ticks, 39);
     71   assert.equal(healed.effects[0].program, 85215);
     72   assert(evidence.beforeImpact.every(client => client.health === healed.health));
     73   assert.equal(healed.nextAttack, evidence.pending.server.nextAttack);
     74   assert.equal(healed.weaponRecharge, evidence.pending.server.weaponRecharge);
     75   await Promise.all(games.map(game => game.wait('healed health remains after impact', () =>
     76     actor(game, initial.owner).stats[27] === healed.health && !pending(game, initial.enemy))));
     77   evidence.afterImpact = games.map(game => ({health: actor(game, initial.owner).stats[27], enemy: actor(game, initial.enemy)}));
     78   await owner.wait('native nano debit', snapshot => owner.local(snapshot).stats[214] === 116);
     79   const results = owner.events.filter(event => event.sequence > sequence && event.direction === 'framereceived' &&
     80     ['46002f16', '5c654b28'].includes(event.opcode));
     81   assert.equal(results.length, 1);
     82   const actions = owner.events.filter(event => event.direction === 'framereceived' && event.opcode === '5e477770')
     83     .map(event => Buffer.from(event.hex, 'hex').readUInt32BE(29));
     84   assert.equal(actions.filter(action => action === 18).length, 1);
     85   assert.equal(actions.filter(action => action === 108).length, 0);
     86   assert.equal(actions.filter(action => action === 107).length, 1);
     87   const installs = owner.events.filter(event => event.direction === 'framereceived' && event.opcode === '5e477770')
     88     .map(event => Buffer.from(event.hex, 'hex')).filter(packet => packet.readUInt32BE(29) === 177);
     89   assert.equal(installs.length, 1);
     90   assert.deepEqual([29, 33, 37, 41, 45].map(offset => installs[0].readUInt32BE(offset)),
     91     [177, 53019, 85215, initial.owner, 20000]);
     92   assert(games.every(game => game.errors.length === 0));
     93   console.log('PASS: native Fixer HoT upload, timed recovery during queued weapon damage and both clients retain recovered HP after impact');
     94 } finally {
     95   try {
     96     await fs.mkdir(output, {recursive: true});
     97     await fs.writeFile(output + '/verification.json', JSON.stringify(evidence, null, 2));
     98     await Promise.all(games.map((game, index) => game.save(output + '/final-' + index)));
     99   } finally { await browser.close(); }
    100 }