aorecomp

Anarchy Online classic in the browser

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


tools/client/test_healing.mjs (6038B)


      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: 'healing-' + 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();
     34     assert.equal((await game.page.request.post(url + '/test-defer-recovery')).status(), 204);
     35   }
     36   const [owner] = games;
     37   const initial = await state();
     38   evidence.initial = initial;
     39   await Promise.all(games.map(game => game.wait('both patients and the Leet', () =>
     40     actor(game, initial.owner) && actor(game, initial.observer) && actor(game, initial.enemy))));
     41   await owner.key('KeyI');
     42   await owner.click(681, 45, 'right');
     43   await owner.wait('Doctor crystal learned', snapshot => owner.local(snapshot).inventory.length === 0);
     44   await owner.page.keyboard.press('Control+8');
     45   await owner.click(974, 76);
     46   await owner.key('F1');
     47   await owner.save(output + '/ready');
     48 
     49   // Ordinary movement cancels the first cast and refunds its reservation.
     50   await owner.click(857, 128, 'right');
     51   await waitState('first healing cast', state => state.cast !== null);
     52   assert.equal((await state()).reserved, 13);
     53   await owner.key('KeyW');
     54   await waitState('movement cancellation and refund', state => state.cast === null && state.nano === 50 && state.reserved === 0);
     55   assert.equal((await state()).health, 200);
     56   await owner.wait('refunded native nano', snapshot => owner.local(snapshot).stats[214] === 50);
     57   evidence.cancelled = await state();
     58   await owner.save(output + '/cancelled');
     59 
     60   const sequence = owner.sequence;
     61   await owner.click(857, 128, 'right');
     62   await waitState('second healing cast near completion', state => state.cast && Date.now() >= Date.parse(state.cast.readyAt) - 500);
     63   assert.equal((await owner.page.request.post(url + '/test-attack')).status(), 204);
     64   evidence.pending = {server: await state()};
     65   assert(evidence.pending.server.cast !== null);
     66   assert(evidence.pending.server.health < 200);
     67   await Promise.all(games.map(game => game.wait('incoming result before heal completion', () => pending(game, initial.enemy))));
     68   evidence.pending.clients = games.map(game => ({health: actor(game, initial.owner).stats[27], enemy: actor(game, initial.enemy)}));
     69   assert(evidence.pending.clients.every(client => client.health === 200));
     70   await Promise.all(games.map(async game => {
     71     await game.wait('healing completes before queued impact', () => actor(game, initial.owner).stats[27] !== 200 && pending(game, initial.enemy));
     72     return {health: actor(game, initial.owner).stats[27], enemy: actor(game, initial.enemy)};
     73   })).then(clients => { evidence.beforeImpact = clients; });
     74   const healed = await state();
     75   evidence.healed = healed;
     76   assert(healed.health >= evidence.pending.server.health + 16 && healed.health <= evidence.pending.server.health + 30);
     77   assert.equal(healed.nano, 37);
     78   assert.equal(healed.reserved, 0);
     79   assert.equal(healed.cast, null);
     80   assert(evidence.beforeImpact.every(client => client.health === healed.health));
     81   assert.equal(healed.nextAttack, evidence.pending.server.nextAttack);
     82   assert.equal(healed.weaponRecharge, evidence.pending.server.weaponRecharge);
     83   await Promise.all(games.map(game => game.wait('healed health remains after impact', () =>
     84     actor(game, initial.owner).stats[27] === healed.health && !pending(game, initial.enemy))));
     85   evidence.afterImpact = games.map(game => ({health: actor(game, initial.owner).stats[27], enemy: actor(game, initial.enemy)}));
     86   await owner.wait('native nano debit', snapshot => owner.local(snapshot).stats[214] === 37);
     87   const results = owner.events.filter(event => event.sequence > sequence && event.direction === 'framereceived' &&
     88     ['46002f16', '5c654b28'].includes(event.opcode));
     89   assert.equal(results.length, 1);
     90   const actions = owner.events.filter(event => event.direction === 'framereceived' && event.opcode === '5e477770')
     91     .map(event => Buffer.from(event.hex, 'hex').readUInt32BE(29));
     92   assert.equal(actions.filter(action => action === 18).length, 2);
     93   assert.equal(actions.filter(action => action === 108).length, 1);
     94   assert.equal(actions.filter(action => action === 107).length, 1);
     95   assert(games.every(game => game.errors.length === 0));
     96   console.log('PASS: native Doctor upload, cancellation/refund, timed combat heal and both clients retain healed HP after the original impact');
     97 } finally {
     98   await fs.mkdir(output, {recursive: true});
     99   await fs.writeFile(output + '/verification.json', JSON.stringify(evidence, null, 2));
    100   try { await Promise.all(games.map((game, index) => game.save(output + '/final-' + index))); }
    101   finally { await browser.close(); }
    102 }