aorecomp

Anarchy Online classic in the browser

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


tools/client/test_minibull.mjs (5042B)


      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', args: [
      9   '--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 actor = (game, type, id) => game.state.actors.find(actor => actor.type === type && actor.id === id);
     13 try {
     14   for (let index = 0; index < 2; index++) {
     15     const context = await browser.newContext({viewport: {width: 1024, height: 768}});
     16     await context.addCookies([{name: 'ao_client', value: 'minibull-' + index, url}]);
     17     const page = await context.newPage(), game = await GameTest.attach(page);
     18     games.push(game);
     19     await page.goto(url);
     20     await game.ready(); assert.equal((await game.page.request.post(url + '/test-defer-recovery')).status(), 204);
     21     await page.waitForFunction(() => touchRegion(500, 400)?.world === true);
     22     await game.snapshot();
     23   }
     24   const [owner] = games;
     25   const server = async () => (await owner.page.request.get(url + '/test-state')).json();
     26   evidence.initial = await server();
     27   const npc = evidence.initial.npc;
     28   assert.equal(evidence.initial.maximum, 40);
     29   assert.equal(evidence.initial.health, 40);
     30   await Promise.all(games.map(game => game.wait('full-health Minibull visible to both clients', () =>
     31     actor(game, 50000, npc)?.stats[27] === 40)));
     32   await Promise.all(games.map((game, index) => game.save(output + '/spawn-' + index)));
     33 
     34   // Deterministic incoming combat isolates the original Headbutt presentation.
     35   assert((await owner.page.request.post(url + '/test-headbutt')).ok());
     36   evidence.headbutt = await server();
     37   assert.equal(evidence.initial.owner_health - evidence.headbutt.owner_health, 23);
     38   await Promise.all(games.map(game => game.wait('both clients consume the Headbutt result', () =>
     39     actor(game, 50000, 1).stats[27] === evidence.headbutt.owner_health &&
     40     actor(game, 50000, npc).weapons.every(weapon => weapon.pending.length === 0))));
     41   await Promise.all(games.map((game, index) => game.save(output + '/headbutt-' + index)));
     42 
     43   // A real native attack kills the full-health target through the shared outcome.
     44   await owner.click(...await owner.pick(50000, npc));
     45   const sequence = owner.sequence;
     46   await owner.key('KeyQ');
     47   await owner.wait('real native attack request', () => owner.events.some(event => event.sequence > sequence &&
     48     event.direction === 'framesent' && event.opcode === '28494070'));
     49   assert.equal((await server()).owner_target, npc);
     50   assert((await owner.page.request.post(url + '/test-owner-swing')).ok());
     51   await Promise.all(games.map(game => game.wait('Minibull corpse visible', () => actor(game, 51050, npc))));
     52   evidence.killed = await server();
     53   assert.equal(evidence.killed.health, 0);
     54   await owner.click(...await owner.pick(51050, npc), 'right');
     55   await owner.wait('selected-quality part in native corpse window', state => state.grids.some(grid =>
     56     grid.cells.some(cell => cell.identity[0] === 107 && cell.identity[1] === 0 && !cell.dummy)));
     57   const source = owner.state.grids.flatMap(grid => grid.cells).find(cell => cell.identity[0] === 107 && cell.identity[1] === 0 && !cell.dummy);
     58   await owner.click((source.bounds[0] + source.bounds[2]) / 2, (source.bounds[1] + source.bounds[3]) / 2);
     59   await owner.wait('exact Monster Part collected', state => owner.local(state).inventory.some(item => item.id === 2 && item.slot === 48 && item.quality === 5));
     60   evidence.collected = await server();
     61   assert.equal(evidence.collected.inventory.Credits, 5);
     62   assert.deepEqual(evidence.collected.inventory.Items.map(item => [item.ID, item.Template, item.Quality, item.Slot, item.Ammo]),
     63     [[1, 124261, 75, 6, 12], [2, 42640, 5, 64, 0]]);
     64   await Promise.all(games.map((game, index) => game.save(output + '/collected-' + index)));
     65   await owner.page.reload();
     66   await owner.ready(); assert.equal((await owner.page.request.post(url + '/test-defer-recovery')).status(), 204);
     67   await owner.page.waitForFunction(() => touchRegion(500, 400)?.world === true);
     68   await owner.snapshot();
     69   assert.deepEqual(owner.local().inventory.map(item => [item.id, item.slot, item.quality]), [[1, 6, 75], [2, 48, 5]]);
     70   assert.deepEqual(await server(), evidence.collected);
     71   assert(games.every(game => game.errors.length === 0));
     72   console.log('PASS: original Minibull Headbutt, two-client combat/corpse, exact selected loot and ammunition through page/cold replay');
     73 } finally {
     74   try {
     75     await fs.mkdir(output, {recursive: true});
     76     await fs.writeFile(output + '/verification.json', JSON.stringify(evidence, null, 2) + '\n');
     77     await Promise.all(games.map((game, index) => game.save(output + '/final-' + index)));
     78   } finally { await browser.close(); }
     79 }