aorecomp

Anarchy Online classic in the browser

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


tools/client/test_selected_stack_resync.mjs (5148B)


      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: 'selected-stack-resync', 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 click = async (slot, button = 'left') => {
     22     const source = game.state.grids.flatMap(grid => grid.cells).find(cell =>
     23       cell.identity[0] === 104 && cell.identity[1] === slot && !cell.dummy);
     24     assert(source, 'the exact source must be visible');
     25     await game.click((source.bounds[0] + source.bounds[2]) / 2, (source.bounds[1] + source.bounds[3]) / 2, button);
     26   };
     27   await game.key('i');
     28   await page.keyboard.press('Control+1');
     29   await click(48);
     30   await game.click(867, 206);
     31   await game.wait('native weapon request', () => game.events.some(event =>
     32     event.direction === 'framesent' && event.opcode === '47537a24'));
     33   evidence.pending = await server();
     34   assert(evidence.pending.pending);
     35   await game.wait('local equipment animation ends', state =>
     36     !game.local(state).actions.includes(3) && !game.local(state).actions.includes(6));
     37 
     38   // Refuse real optimistic splits while the authoritative weapon move is held.
     39   for (const slot of [49, 50]) {
     40     const source = game.state.grids.flatMap(grid => grid.cells).find(cell =>
     41       cell.identity[0] === 104 && cell.identity[1] === slot && !cell.dummy);
     42     const sequence = game.sequence;
     43     await page.keyboard.down('Control');
     44     try { await click(slot); } finally { await page.keyboard.up('Control'); }
     45     await game.wait('native optimistic split request', () => game.events.some(event => event.sequence > sequence &&
     46       event.direction === 'framesent' && event.opcode === '5e477770' && Buffer.from(event.hex, 'hex').readUInt32BE(29) === 52));
     47     await game.wait('authoritative three-charge source restored', state => game.local(state).inventory.some(item =>
     48       item.slot === slot && item.count === 3) && game.local(state).inventory.length === 3);
     49     await game.save(output + '/refused-' + slot);
     50     for (const item of game.local().inventory.filter(item => item.slot === 49 || item.slot === 50))
     51       assert.deepEqual(item.templates, [25812, 25811], 'refused split must preserve selected endpoints');
     52     assert.deepEqual(await server(), evidence.pending);
     53     if (game.state.dragged[0] !== 0) {
     54       // Place the selected source in a different empty visual cell.
     55       const grid = game.state.grids.find(grid => grid.cells.some(cell => cell.identity[0] === 104));
     56       const destination = grid.cells.find(cell => cell.identity[0] === 0 && cell.index !== source.index);
     57       await game.click((destination.bounds[0] + destination.bounds[2]) / 2, (destination.bounds[1] + destination.bounds[3]) / 2);
     58       await game.wait('picked source placed and restored', state => state.dragged[0] === 0 &&
     59         state.grids.some(grid => grid.cells.some(cell => cell.identity[0] === 104 && cell.identity[1] === slot && !cell.dummy)));
     60     }
     61     assert.deepEqual(await server(), evidence.pending);
     62   }
     63   assert((await page.request.post(url + '/test-complete')).ok());
     64   await game.wait('original weapon completes', state => game.local(state).inventory.some(item => item.id === 1 && item.slot === 6));
     65 
     66   // The restored QL2 source must pass its native requirement and heal once.
     67   await game.key('F1');
     68   await game.key('KeyX');
     69   await game.wait('seated laboratory user', state => game.local(state).stats[430] === 2);
     70   await click(49, 'right');
     71   await game.wait('selected QL2 laboratory heals', state => game.local(state).stats[27] === 38 &&
     72     game.local(state).inventory.some(item => item.slot === 49 && item.count === 2));
     73   evidence.used = await server();
     74   assert.equal(evidence.used.health, 38);
     75   await game.save(output + '/used');
     76   await page.reload();
     77   await game.ready();
     78   assert.deepEqual(game.local().inventory.map(item => [item.id, item.slot, item.quality, item.count]).sort((a, b) => a[0] - b[0]),
     79     [[1, 6, 1, 1], [2, 49, 2, 2], [3, 50, 101, 3]]);
     80   assert.deepEqual(game.errors, []);
     81   console.log('PASS: refused selected-quality splits preserve endpoints, equipment and charges, then permit native healing and page/cold recovery');
     82 } finally {
     83   try {
     84     await fs.mkdir(output, {recursive: true});
     85     await fs.writeFile(output + '/verification.json', JSON.stringify(evidence, null, 2) + '\n');
     86     if (game) await game.save(output + '/final');
     87   } finally { await browser.close(); }
     88 }