aorecomp

Anarchy Online classic in the browser

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


tools/client/test_social_swimming.mjs (3711B)


      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 try {
     14   const context = await browser.newContext({viewport: {width: 1024, height: 768}});
     15   await context.addCookies([{name: 'ao_client', value: 'social-swimming', 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 wave = async () => {
     22     const sequence = game.sequence;
     23     await game.key('Enter');
     24     await game.key('Slash');
     25     await page.keyboard.type('wave');
     26     await game.key('Enter');
     27     await game.wait('actual native wave command', () => game.events.some(event => event.sequence > sequence &&
     28       event.direction === 'framesent' && event.opcode === '3b290771'));
     29     const request = game.events.find(event => event.sequence > sequence && event.direction === 'framesent' && event.opcode === '3b290771');
     30     const reference = Buffer.from(request.hex, 'hex').readUInt32BE(33);
     31     assert.equal(Buffer.from(request.hex, 'hex').readUInt32BE(37), 61);
     32     await game.wait('matching social command completion', () => game.events.some(event => event.sequence > sequence &&
     33       event.direction === 'framereceived' && event.opcode === '3b290771' && Buffer.from(event.hex, 'hex').readUInt32BE(33) === reference));
     34     const response = game.events.find(event => event.sequence > sequence && event.direction === 'framereceived' &&
     35       event.opcode === '3b290771' && Buffer.from(event.hex, 'hex').readUInt32BE(33) === reference);
     36     return {reference, verification: Buffer.from(response.hex, 'hex').readUInt32BE(29)};
     37   };
     38   assert((await page.request.post(url + '/test-water')).ok());
     39   evidence.before = await server();
     40   assert.equal(evidence.before.mode, 4);
     41   evidence.refusedCommand = await wave();
     42   evidence.refused = await server();
     43   await game.save(output + '/refused');
     44   assert.equal(evidence.refusedCommand.verification, 2);
     45   assert.deepEqual(evidence.refused, evidence.before);
     46 
     47   assert((await page.request.post(url + '/test-water')).ok());
     48   evidence.acceptedCommand = await wave();
     49   evidence.after = await server();
     50   assert.equal(evidence.acceptedCommand.verification, 1);
     51   assert.equal(evidence.after.mode, 2);
     52   assert.equal(evidence.after.observerCommands, 1);
     53   assert.deepEqual(evidence.after.inventory, evidence.before.inventory);
     54   assert.equal(evidence.after.health, evidence.before.health);
     55   assert.equal(evidence.after.nano, evidence.before.nano);
     56   await game.save(output + '/accepted');
     57   await page.reload();
     58   await game.ready();
     59   assert(!game.events.some(event => event.navigation === game.navigation &&
     60     event.direction === 'framereceived' && event.opcode === '3b290771'));
     61   assert.equal(game.errors.length, 0);
     62   assert.equal(game.dropped, 0);
     63   console.log('PASS: late swimming social command receives exact rejection, stays private and succeeds again on land');
     64 } finally {
     65   try {
     66     await fs.mkdir(output, {recursive: true});
     67     await fs.writeFile(output + '/verification.json', JSON.stringify(evidence, null, 2) + '\n');
     68     if (game) await game.save(output + '/final');
     69   } finally { await browser.close(); }
     70 }