aorecomp

Anarchy Online classic in the browser

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


tools/client/test_snake_jab.mjs (4413B)


      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 let game;
     12 const evidence = {};
     13 try {
     14   const context = await browser.newContext({viewport: {width: 1024, height: 768}});
     15   await context.addCookies([{name: 'ao_client', value: 'snake-jab', url}]);
     16   const page = await context.newPage();
     17   game = await GameTest.attach(page);
     18   const server = async () => (await page.request.get(url + '/test-state')).json();
     19   const waitServer = (label, predicate) => game.waitFor(label, async () => predicate(await server()));
     20   const use = async () => {
     21     const cell = game.state.grids.flatMap(grid => grid.cells).find(cell => cell.identity[0] === 104 && cell.identity[1] === 51 && !cell.dummy);
     22     assert(cell, 'issued Jab is visible in its inventory cell');
     23     await game.click((cell.bounds[0]+cell.bounds[2])/2, (cell.bounds[1]+cell.bounds[3])/2, 'right');
     24   };
     25   await page.goto(url);
     26   await game.ready();
     27   assert.equal((await game.page.request.post(url + '/test-defer-recovery')).status(), 204);
     28   if (!game.state.camera.firstPerson) await game.key('F8');
     29   await game.key('KeyI');
     30   const initial = evidence.initial = await server();
     31   assert.equal(initial.level, 1);
     32   assert.equal(game.local().stats[100], 10);
     33   assert.equal(initial.inventory.Items[3].Template, 117610);
     34 
     35   // The native action requires an active unarmed fight before it will send Use.
     36   await use();
     37   assert.deepEqual(await server(), initial);
     38   await game.key('Tab');
     39   await game.wait('original Leet selected', state => state.selected[0] === 50000 && state.selected[1] === initial.target);
     40   await game.key('KeyQ');
     41   await game.wait('native unarmed fight state', state => game.local(state).stats[434] === 10 && game.local(state).stats[274] === 1);
     42   await use();
     43   await waitServer('Jab cost, hit and lock settled', state => state.nano === initial.nano-2 && state.health === 96 && state.locked);
     44   await game.wait('native target HP and reusable item agree', state =>
     45     state.actors.find(actor => actor.type === 50000 && actor.id === initial.target)?.stats[27] === 96 &&
     46     game.local(state).inventory.some(item => item.templates[0] === 117610 && item.count === 1));
     47   evidence.first = await server();
     48   assert.deepEqual(evidence.first.inventory, initial.inventory);
     49   await game.save(output + '/first');
     50 
     51   await use();
     52   assert.equal((await server()).nano, initial.nano-2);
     53   await waitServer('original ten-second MA lock expires', state => !state.locked);
     54   await game.wait('native MA unlock received', () => game.events.some(event => {
     55     if (event.direction !== 'framereceived' || event.opcode !== '5e477770') return false;
     56     const packet = Buffer.from(event.hex, 'hex');
     57     return packet.readUInt32BE(29) === 164 && packet.readUInt32BE(45) === 100;
     58   }));
     59   await use();
     60   await waitServer('same reusable Jab applies a second time', state => state.nano === initial.nano-4 && state.health === 92 && state.locked);
     61   await game.wait('second native hit agrees', state => state.actors.find(actor => actor.type === 50000 && actor.id === initial.target)?.stats[27] === 92);
     62   evidence.second = await server();
     63   await game.save(output + '/second');
     64   await page.reload();
     65   await game.ready();
     66   assert.equal((await game.page.request.post(url + '/test-defer-recovery')).status(), 204);
     67   evidence.reconnected = await server();
     68   assert.deepEqual(evidence.reconnected.inventory, initial.inventory);
     69   assert.equal(evidence.reconnected.nano, initial.nano-4);
     70   assert.equal(evidence.reconnected.health, 92);
     71   assert(evidence.reconnected.locked);
     72   assert.deepEqual(game.errors, []);
     73   console.log('PASS: issued level-one Jab, unarmed use, two hits/costs, cooldown, reusable item and reconnect');
     74 } finally {
     75   try {
     76     await fs.mkdir(output, {recursive: true});
     77     await fs.writeFile(output + '/verification.json', JSON.stringify(evidence, null, 2) + '\n');
     78     if (game) await game.save(output + '/final');
     79   } finally { await browser.close(); }
     80 }