aorecomp

Anarchy Online classic in the browser

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


tools/client/test_backpack_equipment.mjs (6491B)


      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 => game.state.actors.find(actor => actor.type === 50000 && actor.id === 1);
     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: 'backpack-equipment-' + 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();
     21   }
     22   const [owner] = games;
     23   const carried = owner.local().inventory.filter(item => item.id >= 4);
     24   assert.equal(carried.length, 29);
     25   const server = async () => (await owner.page.request.get(url + '/test-state')).json();
     26   const cell = (state, page, slot) => state.grids.flatMap(grid => grid.cells).find(cell =>
     27     cell.identity[0] === page && cell.identity[1] === slot && !cell.dummy);
     28   const click = async (cell, button = 'left') => {
     29     assert(cell);
     30     await owner.click((cell.bounds[0] + cell.bounds[2]) / 2, (cell.bounds[1] + cell.bounds[3]) / 2, button);
     31   };
     32   await owner.key('i');
     33   await click(cell(owner.state, 104, 48), 'right');
     34   await owner.wait('both contained equipment sources available', state => cell(state, 107, 65541) && cell(state, 107, 65536));
     35   await owner.page.keyboard.press('Control+1');
     36   for (const item of [
     37     {name: 'failed', id: 2, source: 65541, destination: 6, tab: 867, x: 867, y: 206},
     38     {name: 'weapon', id: 2, source: 65541, destination: 6, tab: 867, x: 867, y: 206},
     39     {name: 'armor', id: 3, source: 65536, destination: 21, tab: 953, x: 927, y: 148}]) {
     40     await owner.wait('previous native equipment action ended', state => !owner.local(state).actions.includes(3) && !owner.local(state).actions.includes(6));
     41     await owner.click(item.tab, 53);
     42     const before = await server(), sequence = owner.sequence;
     43     await click(cell(owner.state, 107, item.source));
     44     await owner.wait('real contained equipment pickup', state => state.dragged[0] === 107 && state.dragged[1] === item.source);
     45     await owner.click(item.x, item.y);
     46     await owner.wait('real contained wear request', () => owner.events.some(event => event.sequence > sequence &&
     47       event.direction === 'framesent' && event.opcode === '47537a24' &&
     48       Buffer.from(event.hex, 'hex').readUInt32BE(29) === 107 && Buffer.from(event.hex, 'hex').readUInt32BE(33) === item.source));
     49     evidence[item.name] = {before, admitted: await server()};
     50     await owner.save(output + '/' + item.name + '-requested');
     51     assert(evidence[item.name].admitted.pending, 'contained wear must enter shared equipment admission');
     52     assert.equal(evidence[item.name].admitted.pending.itemID, item.id);
     53     assert.equal(evidence[item.name].admitted.pending.source, item.source);
     54     assert.equal(evidence[item.name].admitted.pending.destination, item.destination);
     55     assert.deepEqual(evidence[item.name].admitted.inventory, before.inventory);
     56     if (item.name === 'failed') assert((await owner.page.request.post(url + '/test-fail-placement')).ok());
     57     assert((await owner.page.request.post(url + '/test-complete')).ok());
     58     if (item.name === 'failed') {
     59       await owner.wait('failed wear restores the same contained source', state => cell(state, 107, item.source));
     60       evidence.failed.refused = await server();
     61       assert.deepEqual(evidence.failed.refused, before);
     62       assert(!owner.local().inventory.some(owned => owned.id === 2));
     63       assert((await owner.page.request.post(url + '/test-allow-placement')).ok());
     64       await Promise.all(games.map((game, index) => game.save(output + '/failed-restored-' + index)));
     65       continue;
     66     }
     67     await owner.wait('exact contained item reaches equipment', state => owner.local(state).inventory.some(owned => owned.id === item.id && owned.slot === item.destination));
     68     await owner.wait('contained source removed', state => !cell(state, 107, item.source));
     69     const armor = item.id === 3 ? [5, 5, 1, 1, 5, 5, 1, 5] : [0, 0, 0, 0, 0, 0, 0, 0];
     70     await Promise.all(games.map(game => game.wait('both replicas retain exact wear effects', () => actor(game).effectiveArmor.every((value, index) => value === armor[index]))));
     71     evidence[item.name].completed = await server();
     72     assert.equal(evidence[item.name].completed.pending, null);
     73     assert.deepEqual(evidence[item.name].completed.armor, armor);
     74     await Promise.all(games.map((game, index) => game.save(output + '/' + item.name + '-worn-' + index)));
     75   }
     76   evidence.final = await server();
     77   assert.deepEqual(evidence.final.inventory.Items.filter(item => item.ID < 4).map(item => [item.ID, item.Slot]), [[1, 64], [2, 6], [3, 21]]);
     78   assert.deepEqual(evidence.final.inventory.Items.filter(item => item.ID >= 4), evidence.weapon.before.inventory.Items.filter(item => item.ID >= 4));
     79   assert.equal(evidence.final.inventory.Containers, null);
     80   await Promise.all(games.map(async game => { await game.page.reload(); await game.ready(); }));
     81   await Promise.all(games.map(game => game.wait('native effects survive page replay', () => actor(game).effectiveArmor.every((value, index) => value === [5, 5, 1, 1, 5, 5, 1, 5][index]))));
     82   assert.deepEqual(await server(), evidence.final);
     83   assert.deepEqual(owner.local().inventory.filter(item => item.id < 4).map(item => [item.id, item.slot]), [[2, 6], [3, 21], [1, 48]]);
     84   assert.deepEqual(owner.local().inventory.filter(item => item.id >= 4), carried);
     85   assert(games.every(game => game.errors.length === 0 && game.dropped === 0));
     86   console.log('PASS: full-inventory contained weapon/armor wear shares delay/effects; both native replicas, page/cold retain all 32 identities');
     87 } finally {
     88   try {
     89     await fs.mkdir(output, {recursive: true});
     90     await fs.writeFile(output + '/verification.json', JSON.stringify(evidence, null, 2) + '\n');
     91     await Promise.all(games.map((game, index) => game.save(output + '/final-' + index)));
     92   } finally { await browser.close(); }
     93 }