aorecomp

Anarchy Online classic in the browser

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


tools/client/test_backpack_medical.mjs (6787B)


      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 try {
     13   for (let index = 0; index < 2; index++) {
     14     const context = await browser.newContext({viewport: {width: 1024, height: 768}});
     15     await context.addCookies([{name: 'ao_client', value: 'backpack-medical-' + index, url}]);
     16     const page = await context.newPage();
     17     const game = await GameTest.attach(page);
     18     games.push(game);
     19     await page.goto(url);
     20     await game.ready();
     21     assert.equal((await game.page.request.post(url + '/test-defer-recovery')).status(), 204);
     22   }
     23   const [owner, patient] = games;
     24   const server = async () => (await owner.page.request.get(url + '/test-state')).json();
     25   const actor = (game, id) => game.state.actors.find(actor => actor.type === 50000 && actor.id === id);
     26   const cell = (page, slot) => owner.state.grids.flatMap(grid => grid.cells).find(cell =>
     27     cell.identity[0] === page && cell.identity[1] === slot && !cell.dummy);
     28   const click = async (source, button = 'left') => {
     29     assert(source, 'exact source cell is usable');
     30     await owner.click((source.bounds[0] + source.bounds[2]) / 2, (source.bounds[1] + source.bounds[3]) / 2, button);
     31   };
     32   const use = async slot => {
     33     const sequence = owner.sequence;
     34     await click(cell(107, 65536 | slot), 'right');
     35     await owner.wait('actual contained medical Use', () => owner.events.some(event => event.sequence > sequence &&
     36       event.direction === 'framesent' && event.opcode === '52526858' && Buffer.from(event.hex, 'hex').readUInt32BE(53) === 107));
     37     const request = owner.events.find(event => event.sequence > sequence && event.direction === 'framesent' && event.opcode === '52526858');
     38     const reference = Buffer.from(request.hex, 'hex').readUInt32BE(33);
     39     await owner.wait('exact medical completion', () => owner.events.some(event => event.sequence > sequence &&
     40       event.direction === 'framereceived' && event.opcode === '52526858' && Buffer.from(event.hex, 'hex').readUInt32BE(33) === reference));
     41     const reply = owner.events.find(event => event.sequence > sequence && event.direction === 'framereceived' &&
     42       event.opcode === '52526858' && Buffer.from(event.hex, 'hex').readUInt32BE(33) === reference);
     43     return {reference, result: Buffer.from(reply.hex, 'hex').readUInt32BE(29)};
     44   };
     45   await Promise.all(games.map(game => game.wait('both patients present', () => actor(game, 1) && actor(game, 2))));
     46   await owner.key('F1');
     47   await owner.key('KeyI');
     48   await click(cell(104, 48), 'right');
     49   await owner.wait('all three contained items visible', () => cell(107, 65536) && cell(107, 65537) && cell(107, 65538));
     50   evidence.before = await server();
     51   evidence.firstAid = await use(0);
     52   await owner.save(output + '/first-aid-result');
     53   assert.equal(evidence.firstAid.result, 1);
     54   evidence.healed = await server();
     55   assert(evidence.healed[0].health >= 30 && evidence.healed[0].health <= 32);
     56   assert.equal(evidence.healed[0].inventory.Containers[1][0].Count, 1);
     57   assert.deepEqual(evidence.healed[1], evidence.before[1]);
     58   await Promise.all(games.map(game => game.wait('both see the healed owner', () => actor(game, 1).stats[27] === evidence.healed[0].health)));
     59 
     60   // The teammate sits through real input, then receives the last laboratory charge.
     61   await patient.key('KeyX');
     62   await patient.wait('native patient seated', state => patient.local(state).stats[430] === 2);
     63   assert((await server())[1].sitting, 'patient seated on server');
     64   await owner.click(...await owner.pick(50000, 2));
     65   await owner.wait('teammate selected', state => state.selected[0] === 50000 && state.selected[1] === 2);
     66   evidence.treatment = await use(1);
     67   assert.equal(evidence.treatment.result, 1);
     68   await owner.wait('last laboratory charge disappears', () => !cell(107, 65537) && cell(107, 65536) && cell(107, 65538));
     69   evidence.treated = await server();
     70   assert.equal(evidence.treated[1].health, 35);
     71   assert.equal(evidence.treated[1].nano, 16);
     72   assert.equal(evidence.treated[0].health, evidence.healed[0].health);
     73   assert.equal(evidence.treated[0].nano, 1);
     74   assert.deepEqual(evidence.treated[0].locks, evidence.healed[0].locks);
     75   assert(evidence.treated[1].locks[124]);
     76   assert(!evidence.treated[1].locks[123]);
     77   await Promise.all(games.map(game => game.wait('both see treated health', () => actor(game, 2).stats[27] === 35)));
     78   await patient.wait('patient receives its nano', () => patient.local().stats[214] === 16);
     79 
     80   // The unchanged companion can still be withdrawn through the same bag window.
     81   await click(cell(107, 65538));
     82   await owner.wait('companion picked', state => state.dragged[0] === 107 && state.dragged[1] === 65538);
     83   await click(owner.state.grids.find(grid => grid.totalCells === 30).cells.find(cell => cell.identity[0] === 0));
     84   await owner.wait('exact companion withdraws', state => owner.local(state).inventory.some(item => item.id === 4 && item.slot === 49));
     85   evidence.after = await server();
     86   await Promise.all(games.map((game, index) => game.save(output + '/verified-' + index)));
     87   for (const game of games) {
     88     await game.page.reload();
     89     await game.ready();
     90     assert.equal((await game.page.request.post(url + '/test-defer-recovery')).status(), 204);
     91   }
     92   evidence.replayed = await server();
     93   for (let index = 0; index < 2; index++) {
     94     assert.equal(evidence.replayed[index].health, evidence.after[index].health);
     95     assert.equal(evidence.replayed[index].nano, evidence.after[index].nano);
     96     assert.deepEqual(evidence.replayed[index].locks, evidence.after[index].locks);
     97     assert.deepEqual(evidence.replayed[index].inventory, evidence.after[index].inventory);
     98   }
     99   assert.deepEqual(owner.local().inventory.map(item => [item.id, item.slot]), [[1, 48], [4, 49]]);
    100   for (const game of games) {
    101     assert.deepEqual(game.errors, []);
    102     assert.equal(game.dropped, 0);
    103   }
    104   console.log('PASS: contained first aid and teammate treatment consume exact charges, preserve actors and companion, and survive page/cold recovery');
    105 } finally {
    106   try {
    107     await fs.mkdir(output, {recursive: true});
    108     await fs.writeFile(output + '/verification.json', JSON.stringify(evidence, null, 2) + '\n');
    109     await Promise.all(games.map((game, index) => game.save(output + '/final-' + index)));
    110   } finally { await browser.close(); }
    111 }