aorecomp

Anarchy Online classic in the browser

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


tools/client/test_stale_inventory_move.mjs (9513B)


      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, sourceSlot] = process.argv.slice(2);
      8 const sourcePage = sourceSlot === '6' || sourceSlot === '9' ? 101 : sourceSlot === '18' || sourceSlot === '19' ? 102 : sourceSlot === '37' ? 103 : 104;
      9 const nativeSlot = sourcePage === 104 ? 48 : Number(sourceSlot);
     10 let wornSlot = sourcePage === 104 ? 6 : Number(sourceSlot);
     11 const retained = sourceSlot === '9' ? [[2, 65], [3, 7]] : [[2, 65]];
     12 const expected = [[1, wornSlot], ...retained];
     13 const browser = await chromium.launch({executablePath: '/usr/bin/chromium', args: [
     14   '--enable-gpu', '--use-gl=angle', '--use-angle=gl', '--no-sandbox', '--disable-dev-shm-usage',
     15   '--disable-background-timer-throttling', '--disable-renderer-backgrounding']});
     16 let game;
     17 const evidence = {};
     18 try {
     19   const context = await browser.newContext({viewport: {width: 1024, height: 768}});
     20   await context.addCookies([{name: 'ao_client', value: 'stale-inventory-move', url}]);
     21   const page = await context.newPage();
     22   game = await GameTest.attach(page);
     23   const server = async () => (await page.request.get(url + '/test-state')).json();
     24   const cell = (pageId, slot) => game.state.grids.flatMap(grid => grid.cells).find(cell =>
     25     cell.identity[0] === pageId && cell.identity[1] === slot && !cell.dummy);
     26   const sourcePresent = () => sourcePage === 104 ? cell(104, 48) :
     27     game.local().inventory.some(item => item.id === 1 && item.slot === nativeSlot);
     28   const click = async (source, button = 'left') => {
     29     assert(source, 'the exact native source must be usable');
     30     await game.click((source.bounds[0] + source.bounds[2]) / 2, (source.bounds[1] + source.bounds[3]) / 2, button);
     31   };
     32   const saveStats = async label => {
     33     await page.keyboard.press('Control+9');
     34     const open = state => state.regions.some(([left, top, right, bottom]) =>
     35       left === 832 && top === 380 && right === 1024 && bottom === 606);
     36     await game.wait('native armor stats visible', open);
     37     await game.save(output + '/' + label);
     38     await page.keyboard.press('Control+9');
     39     await game.wait('native armor stats closed', state => !open(state));
     40   };
     41   const saveSkills = async label => {
     42     const open = state => state.regions.some(([left, top, right, bottom]) =>
     43       left === 315 && top === 98 && right === 571 && bottom === 162);
     44     await game.key('KeyU');
     45     await game.wait('native abilities visible', open);
     46     await game.save(output + '/' + label);
     47     await game.click(543, 622);
     48     await game.wait('native abilities closed', state => !open(state));
     49   };
     50   await page.goto(url);
     51   await game.ready();
     52   await page.waitForFunction(() => touchRegion(500, 400)?.world === true);
     53   await game.snapshot();
     54   await game.key('KeyI');
     55   if (sourcePage !== 104) {
     56     await page.keyboard.press('Control+1');
     57     if (sourcePage === 102) await game.click(953, 53);
     58     if (sourcePage === 103) await game.click(993, 53);
     59     await game.snapshot();
     60   }
     61   assert(sourcePresent() && cell(104, 49));
     62   evidence.initial = await server();
     63   const resourceWeapon = evidence.initial.Items.some(item => item.ID === 1 && item.Template === 124260);
     64   const defensiveRing = evidence.initial.Items.some(item => item.ID === 1 && item.Template === 84141);
     65   if (defensiveRing) {
     66     wornSlot = expected[0][1] = 31;
     67     assert.equal(evidence.initial.Defense, 149);
     68     await page.keyboard.press('Control+1');
     69     await game.click(953, 53);
     70     await game.snapshot();
     71   }
     72   if (sourceSlot === '19') assert.deepEqual(evidence.initial.Resources, [40, 38, 8, 40, 38]);
     73   if (resourceWeapon) {
     74     assert.deepEqual(evidence.initial.Resources, [872, 681, 349, 872, 681]);
     75     await game.wait('initial weapon resource replay agrees', state => [1, 221, 22, 27, 214].every((stat, index) =>
     76       game.local(state).stats[stat] === evidence.initial.Resources[index]));
     77   }
     78   if (sourcePage === 102 || sourcePage === 103 || sourceSlot === '9' || resourceWeapon) await saveStats('initial-armor');
     79   assert((await page.request.post(url + '/test-stale-view')).ok());
     80   evidence.transferred = await server();
     81   assert.deepEqual(evidence.transferred.Items.map(item => [item.ID, item.Slot]), retained);
     82   assert.deepEqual(evidence.transferred.Bank.map(item => [item.ID, item.Slot]), [[1, 0]]);
     83   await game.save(output + '/stale');
     84   const sequence = game.sequence;
     85   if (sourceSlot === '19') {
     86     await game.click(989, 94);
     87     await game.wait('worn backpack picked', state => state.dragged[0] === 102 && state.dragged[1] === 19);
     88     await click(game.state.grids.find(grid => grid.totalCells === 30).cells.find(cell => cell.identity[0] === 0));
     89   }
     90   else if (sourcePage === 102) await game.click(927, 94, 'right');
     91   else if (sourcePage === 103) await game.click(927, 148, 'right');
     92   else if (sourceSlot === '9') await game.click(867, 262, 'right');
     93   else if (sourcePage === 101) await game.click(867, 206, 'right');
     94   else await click(cell(104, 48), 'right');
     95   await game.wait('actual stale inventory move', () => game.events.some(event => event.sequence > sequence &&
     96     event.direction === 'framesent' && event.opcode === '47537a24' &&
     97     Buffer.from(event.hex, 'hex').readUInt32BE(29) === sourcePage && Buffer.from(event.hex, 'hex').readUInt32BE(33) === nativeSlot &&
     98     Buffer.from(event.hex, 'hex').readUInt32BE(45) === (sourcePage === 104 ? wornSlot : 111)));
     99   await game.wait('stale inventory move cancelled', () => game.events.some(event => event.sequence > sequence &&
    100     event.direction === 'framereceived' && event.opcode === '5e477770' && Buffer.from(event.hex, 'hex').readUInt32BE(29) === 146));
    101   await game.wait('empty authoritative source cleared', () => !sourcePresent() && cell(104, 49) && game.state.dragged[0] === 0);
    102   assert.deepEqual(await server(), evidence.transferred);
    103   if (sourcePage === 102 || sourcePage === 103 || sourceSlot === '9' || resourceWeapon) await saveStats('refused');
    104   else await game.save(output + '/refused');
    105   if (sourceSlot === '19' || resourceWeapon) {
    106     evidence.refusedNative = [1, 221, 22, 27, 214].map(stat => game.local().stats[stat]);
    107     assert.deepEqual(evidence.refusedNative, evidence.transferred.Resources);
    108   }
    109   if (resourceWeapon) await saveSkills('refused-skills');
    110 
    111   // The correction must leave the actual banked instance available on this page.
    112   if (!game.state.camera.firstPerson) await game.key('F8');
    113   await game.click(...await game.pick(51005, 3221357727), 'right');
    114   await game.wait('banked source retained', () => cell(105, 0));
    115   await click(cell(105, 0));
    116   await game.wait('same physical weapon withdrawn', state => game.local(state).inventory.some(item => item.id === 1 && item.slot === 48));
    117   await game.click(170, 8);
    118   if (!cell(104, 48)) await game.key('KeyI');
    119   const retry = game.sequence;
    120   if (sourceSlot === '19') {
    121     await click(cell(104, 48));
    122     await game.wait('retained backpack picked for wear', state => state.dragged[0] === 104 && state.dragged[1] === 48);
    123     await game.click(989, 94);
    124   }
    125   else await click(cell(104, 48), 'right');
    126   await game.wait('valid same-page wear completes', state => game.local(state).inventory.some(item => item.id === 1 && item.slot === wornSlot) &&
    127     game.events.some(event => event.sequence > retry && event.direction === 'framereceived' && event.opcode === '47537a24'), 15000);
    128   evidence.after = await server();
    129   assert.deepEqual(evidence.after.Items.map(item => [item.ID, item.Slot]).sort((a, b) => a[0] - b[0]), expected);
    130   assert.equal(evidence.after.Bank.length, 0);
    131   assert.deepEqual(evidence.after.Armor, evidence.initial.Armor);
    132   if (defensiveRing) assert.equal(evidence.after.Defense, 177);
    133   assert.equal(evidence.after.MaximumNCU, evidence.initial.MaximumNCU);
    134   if (sourceSlot === '9') assert.deepEqual([evidence.initial.MaximumNCU, evidence.transferred.MaximumNCU, evidence.after.MaximumNCU], [10, 8, 10]);
    135   if (sourcePage === 102 || sourcePage === 103 || sourceSlot === '9' || resourceWeapon) await saveStats('moved');
    136   else await game.save(output + '/moved');
    137   if (resourceWeapon) await saveSkills('reworn-skills');
    138   if (sourceSlot === '19' || resourceWeapon) {
    139     await game.wait('native re-wear resources agree', state => [1, 221, 22, 27, 214].every((stat, index) =>
    140       game.local(state).stats[stat] === evidence.after.Resources[index]));
    141     evidence.afterNative = [1, 221, 22, 27, 214].map(stat => game.local().stats[stat]);
    142   }
    143   await page.reload();
    144   await game.ready();
    145   await page.waitForFunction(() => touchRegion(500, 400)?.world === true);
    146   await game.snapshot();
    147   assert.deepEqual(game.local().inventory.map(item => [item.id, item.slot]).sort((a, b) => a[0] - b[0]),
    148     expected.map(([id, slot]) => [id, slot >= 64 ? slot - 16 : slot]));
    149   assert.deepEqual(await server(), evidence.after);
    150   if (sourceSlot === '19' || resourceWeapon) assert.deepEqual([1, 221, 22, 27, 214].map(stat => game.local().stats[stat]), evidence.after.Resources);
    151   assert.deepEqual(game.errors, []);
    152   console.log('PASS: stale inventory source cleared, exact banked item withdrawn/moved and both instances retained through page/cold recovery');
    153 } finally {
    154   try {
    155     await fs.mkdir(output, {recursive: true});
    156     await fs.writeFile(output + '/verification.json', JSON.stringify(evidence, null, 2) + '\n');
    157     if (game) await game.save(output + '/final');
    158   } finally { await browser.close(); }
    159 }