aorecomp

Anarchy Online classic in the browser

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


tools/client/test_pet_summon.mjs (4651B)


      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', 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, id) => game.state.actors.find(actor => actor.type === 50000 && 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: 'pet-cast-' + 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 state = async () => (await owner.page.request.get(url + '/test-state')).json();
     26   if (phase === 'live') {
     27     await owner.page.keyboard.press('Control+8');
     28     await owner.click(987, 96);
     29     await owner.snapshot();
     30     await owner.save(output + '/programs');
     31     const program = owner.state.grids.flatMap(grid => grid.cells).find(cell =>
     32       cell.identity[0] === 53019 && cell.identity[1] === 43324 && !cell.dummy);
     33     assert(program, 'original summon program must be visible in Programs');
     34     const cast = async () => {
     35       const cell = owner.state.grids.flatMap(grid => grid.cells).find(cell => cell.identity[0] === 53019 && cell.identity[1] === 43324);
     36       await owner.click((cell.bounds[0] + cell.bounds[2]) / 2, (cell.bounds[1] + cell.bounds[3]) / 2, 'right');
     37     };
     38     await cast();
     39     await Promise.all(games.map(game => game.wait('one owned manifestation appears', () => actor(game, 1000001) && actor(game, 1).stats[458] === 1)));
     40     evidence.first = await state();
     41     assert.equal(evidence.first.nano, 25);
     42     assert.deepEqual(evidence.first.pets.map(pet => [pet.id, pet.owner, pet.program, pet.health]), [[1000001, 1, 43324, 17]]);
     43     await Promise.all(games.map((game, index) => game.save(output + '/summoned-' + index)));
     44     while (!(await state()).ready) await owner.snapshot();
     45     await cast();
     46     await owner.snapshot();
     47     evidence.repeated = await state();
     48     assert.deepEqual(evidence.repeated.pets, evidence.first.pets);
     49     assert.equal(evidence.repeated.nano, 25);
     50     assert.equal(evidence.repeated.ready, true);
     51     assert.equal(owner.local().stats[251], 1);
     52     assert((await owner.page.request.post(url + '/test-expire')).ok());
     53     await Promise.all(games.map(game => game.wait('expired owned pet retires and counter clears', () =>
     54       !actor(game, 1000001) && actor(game, 1).stats[458] === 0 && actor(game, 1).stats[251] === 0)));
     55     evidence.expired = await state();
     56     while (!(await state()).ready) await owner.snapshot();
     57     await cast();
     58     await Promise.all(games.map(game => game.wait('replacement summon has a new owned identity', () => actor(game, 1000002) && actor(game, 1).stats[458] === 1)));
     59     evidence.replacement = await state();
     60     await Promise.all(games.map(async game => { await game.page.reload(); await game.ready(); assert.equal((await game.page.request.post(url + '/test-defer-recovery')).status(), 204); await game.snapshot(); }));
     61   }
     62   await Promise.all(games.map(game => game.wait('owned pet replays once', () => actor(game, 1000002) && actor(game, 1).stats[458] === 1)));
     63   evidence.final = await state();
     64   assert.deepEqual(evidence.final.pets.map(pet => [pet.id, pet.owner, pet.program, pet.health]), [[1000002, 1, 43324, 17]]);
     65   assert.equal(evidence.final.nano, 15);
     66   assert.equal(evidence.final.observerNano, 35);
     67   assert.equal(evidence.final.nextNPC, 1000003);
     68   assert.equal(owner.local().stats[251], 1);
     69   assert(games.every(game => game.errors.length === 0));
     70   console.log('PASS:', phase, 'native summon, ownership limit, controlled expiry/replacement and page/cold recovery');
     71 } finally {
     72   try {
     73     await fs.mkdir(output, {recursive: true});
     74     await fs.writeFile(output + '/verification.json', JSON.stringify(evidence, null, 2) + '\n');
     75     await Promise.all(games.map((game, index) => game.save(output + '/final-' + index)));
     76   } finally { await browser.close(); }
     77 }