aorecomp

Anarchy Online classic in the browser

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


tools/client/test_pending_stack.mjs (3860B)


      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',
      9   args: ['--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: 'pending-stack', url}]);
     16   const page = await context.newPage();
     17   game = await GameTest.attach(page);
     18   await page.goto(url);
     19   await game.ready();
     20   const server = async () => (await page.request.get(url + '/test-state')).json();
     21   const cell = slot => game.state.grids.flatMap(grid => grid.cells).find(cell =>
     22     cell.identity[0] === 104 && cell.identity[1] === slot && !cell.dummy);
     23   const click = async slot => {
     24     const source = cell(slot);
     25     assert(source, 'the exact inventory source must be visible');
     26     await game.click((source.bounds[0] + source.bounds[2]) / 2, (source.bounds[1] + source.bounds[3]) / 2);
     27   };
     28   await game.key('i');
     29   await page.keyboard.press('Control+1');
     30   await click(48);
     31   await game.click(867, 206);
     32   await game.wait('admitted weapon move', () => game.events.some(event =>
     33     event.direction === 'framesent' && event.opcode === '47537a24' && Buffer.from(event.hex, 'hex').readUInt32BE(29) === 104));
     34   evidence.pending = await server();
     35   assert(evidence.pending.pending);
     36   await game.wait('local equipment animation ends', state =>
     37     !game.local(state).actions.includes(3) && !game.local(state).actions.includes(6));
     38   const join = async () => {
     39     const sequence = game.sequence;
     40     await click(49);
     41     await click(50);
     42     await game.wait('actual native join request', () => game.events.some(event => event.sequence > sequence &&
     43       event.direction === 'framesent' && event.opcode === '5e477770' && Buffer.from(event.hex, 'hex').readUInt32BE(29) === 53));
     44   };
     45   await join();
     46   evidence.refused = await server();
     47   assert.deepEqual(evidence.refused, evidence.pending);
     48   await game.save(output + '/refused');
     49   await game.wait('both refused stack icons remain usable', () => cell(49) && cell(50));
     50   evidence.pendingWeaponVisible = Boolean(cell(48));
     51   assert.deepEqual(game.state.dragged, [0, 0]);
     52   assert((await page.request.post(url + '/test-complete')).ok());
     53   await game.wait('weapon completes unchanged', state => game.local(state).inventory.some(item => item.id === 1 && item.slot === 6));
     54   await join();
     55   await game.wait('normal join retry conserves six charges', state => game.local(state).inventory.some(item =>
     56     item.slot === 50 && item.count === 6) && game.local(state).inventory.length === 2);
     57   evidence.after = await server();
     58   assert.equal(evidence.after.pending, null);
     59   assert.deepEqual(evidence.after.inventory.Items, [
     60     {...evidence.pending.inventory.Items[0], Slot: 6}, {...evidence.pending.inventory.Items[2], Count: 6}]);
     61   await game.save(output + '/retried');
     62   await page.reload();
     63   await game.ready();
     64   assert.deepEqual(game.local().inventory.map(item => [item.id, item.slot, item.count]).sort((a, b) => a[0] - b[0]),
     65     [[1, 6, 1], [3, 50, 6]]);
     66   assert.deepEqual(game.errors, []);
     67   console.log('PASS: pending equipment join refusal preserves usable stacks and permits six-charge retry');
     68 } finally {
     69   try {
     70     await fs.mkdir(output, {recursive: true});
     71     await fs.writeFile(output + '/verification.json', JSON.stringify(evidence, null, 2) + '\n');
     72     if (game) await game.save(output + '/final');
     73   } finally { await browser.close(); }
     74 }