aorecomp

Anarchy Online classic in the browser

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


tools/client/test_stale_stack_join.mjs (4430B)


      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 
     14 try {
     15   const context = await browser.newContext({viewport: {width: 1024, height: 768}});
     16   await context.addCookies([{name: 'ao_client', value: 'stale-stack-join', url}]);
     17   const page = await context.newPage();
     18   game = await GameTest.attach(page);
     19   const server = async () => (await page.request.get(url + '/test-state')).json();
     20   const cell = slot => game.state.grids.flatMap(grid => grid.cells).find(cell =>
     21     cell.identity[0] === 104 && cell.identity[1] === slot && !cell.dummy);
     22   const click = async slot => {
     23     const source = cell(slot);
     24     assert(source, `ordinary slot ${slot} must be usable`);
     25     await game.click((source.bounds[0] + source.bounds[2]) / 2, (source.bounds[1] + source.bounds[3]) / 2);
     26   };
     27   await page.goto(url);
     28   await game.ready();
     29   await page.waitForFunction(() => touchRegion(500, 400)?.world === true);
     30   await game.snapshot();
     31   await game.key('i');
     32   await game.wait('three ordinary stacks', () => cell(48) && cell(49) && cell(50));
     33   evidence.initial = await server();
     34   const checkpoint = await page.request.post(url + '/test-stale-view');
     35   assert(checkpoint.ok());
     36   evidence.gesture = await checkpoint.json();
     37   evidence.stale = await server();
     38   await game.save(output + '/stale');
     39 
     40   // Real input exercises either a missing target or an already-consumed source.
     41   const start = game.sequence;
     42   await click(evidence.gesture.removed);
     43   await click(evidence.gesture.kept);
     44   await game.wait('actual stale ordinary join', () => game.events.some(event => event.sequence > start &&
     45     event.direction === 'framesent' && event.opcode === '5e477770' &&
     46     Buffer.from(event.hex, 'hex').readUInt32BE(29) === 53));
     47   await game.save(output + '/requested');
     48   await game.wait('authoritative stacks restored with usable sources', state =>
     49     cell(49) && cell(50) && !cell(48) && state.dragged[0] === 0 &&
     50     JSON.stringify(game.local(state).inventory.map(item => [item.slot, item.quality, item.count, ...item.templates])
     51       .sort((a, b) => a[0] - b[0])) === JSON.stringify([
     52       [49, 2, 8, 25812, 25811], [50, 2, 7, 25812, 25811]]));
     53   evidence.refused = await server();
     54   assert.deepEqual(evidence.refused, evidence.stale);
     55   await game.save(output + '/refused');
     56 
     57   // The same open inventory can immediately merge into the surviving target.
     58   const retry = game.sequence;
     59   await click(50);
     60   await click(49);
     61   await game.wait('valid retry retains fifteen charges', state => cell(49) && !cell(50) &&
     62     game.local(state).inventory.some(item => item.slot === 49 && item.count === 15));
     63   evidence.joined = await server();
     64   assert.deepEqual(evidence.joined.Items.map(item => [item.ID, item.Slot, item.Quality, item.Count]), [[2, 65, 2, 15]]);
     65   const acknowledgements = game.events.filter(event => event.sequence > start && event.direction === 'framereceived' &&
     66     event.opcode === '5e477770' && Buffer.from(event.hex, 'hex').readUInt32BE(29) === 53);
     67   assert.equal(acknowledgements.length, 1);
     68   assert(acknowledgements[0].sequence > retry);
     69   await game.save(output + '/joined');
     70 
     71   await page.reload();
     72   await game.ready();
     73   await page.waitForFunction(() => touchRegion(500, 400)?.world === true);
     74   await game.snapshot();
     75   assert.deepEqual(game.local().inventory.map(item => [item.id, item.slot, item.quality, item.count, ...item.templates]),
     76     [[2, 49, 2, 15, 25812, 25811]]);
     77   evidence.replayed = await server();
     78   assert.deepEqual(evidence.replayed, evidence.joined);
     79   assert.deepEqual(game.errors, []);
     80   console.log('PASS: stale join resynchronization, usable source, same-window retry and page recovery preserve fifteen charges');
     81 } finally {
     82   try {
     83     await fs.mkdir(output, {recursive: true});
     84     await fs.writeFile(output + '/verification.json', JSON.stringify(evidence, null, 2) + '\n');
     85     if (game) await game.save(output + '/final');
     86   } finally { await browser.close(); }
     87 }