aorecomp

Anarchy Online classic in the browser

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


tools/client/test_npc_outfits.mjs (5070B)


      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 const games = [];
     12 // Bound startup below the fixture deadline so native failures retain diagnostics.
     13 async function ready(game) {
     14   let timer;
     15   try {
     16     await Promise.race([game.ready(), new Promise((_, reject) => {
     17       timer = setTimeout(() => reject(new Error('NPC appearance client startup timed out')), 45000);
     18     })]);
     19   } finally { clearTimeout(timer); }
     20 }
     21 try {
     22   for (let index = 0; index < 2; ++index) {
     23     const context = await browser.newContext({viewport: {width: 1024, height: 768}});
     24     await context.addCookies([{name: 'ao_client', value: `npc-outfit-${index}`, url}]);
     25     const page = await context.newPage();
     26     const game = await GameTest.attach(page);
     27     games.push(game);
     28     await page.goto(url, {waitUntil: 'domcontentloaded'});
     29     await ready(game);
     30     console.log(`client ${index}: ready`);
     31   }
     32   const expected = await (await games[0].page.request.get(url + '/test-state')).json();
     33   expected.items ??= [];
     34   await fs.mkdir(output, {recursive: true});
     35   await fs.writeFile(output + '/server.json', JSON.stringify(expected, null, 2) + '\n');
     36 
     37   for (const phase of ['initial', 'reconnected']) {
     38     for (const [index, game] of games.entries()) {
     39       if (phase === 'reconnected') {
     40         await game.page.reload({waitUntil: 'domcontentloaded'});
     41         await game.page.waitForFunction(() => typeof Module !== 'undefined' && typeof Module._browser_test_request === 'function');
     42         await ready(game);
     43       }
     44       await game.page.waitForFunction(() => touchRegion(500, 400)?.world === true);
     45       if (!game.state.camera.firstPerson) await game.key('F8');
     46       await game.wait('NPC appearance and armor applied', state => {
     47         const actor = state.actors.find(actor => actor.type === 50000 && actor.id === expected.id);
     48         return actor && (!(expected.items.length || expected.visual.Head) || (actor.stats[223] === 1 &&
     49           expected.armor.every((value, i) => actor.effectiveArmor[i] === value))) &&
     50           (!expected.visual.Head || actor.stats[64] === expected.visual.Head);
     51       });
     52       if (expected.visual.Head) {
     53         const actor = game.state.actors.find(actor => actor.type === 50000 && actor.id === expected.id);
     54         const rotationDot = actor.rotation.reduce((sum, value, i) => sum + value * expected.rotation[i], 0);
     55         assert(Math.abs(rotationDot) > 0.9999, 'Native NPC facing differs from the server heading');
     56       }
     57       const wear = game.events.filter(event => event.kind === 'packet' && event.navigation === game.navigation &&
     58         event.direction === 'framereceived' && event.opcode === '35505644' && event.actor?.[1] === expected.id)
     59         .map(event => { const packet = Buffer.from(event.hex, 'hex');
     60           return [packet.readUInt32BE(29), packet.readUInt32BE(37), packet.readUInt32BE(45), packet.readUInt32BE(53)]; });
     61       assert.deepEqual(wear, expected.items.map(item => [item.Template, item.Quality, 6, item.Slot]));
     62 
     63       // Aim at the actor above the chat panel even when nearby terrain is steep.
     64       const cameraPoint = await game.point(512, 300);
     65       let mouseY = cameraPoint.y, sequence = game.sequence;
     66       await game.page.mouse.move(cameraPoint.x, mouseY);
     67       await game.page.mouse.down({button: 'right'});
     68       try {
     69         await game.waitInput(sequence, 1025);
     70         const deadline = Date.now() + 10000;
     71         for (;;) {
     72           await game.snapshot();
     73           const y = game.state.actors.find(actor => actor.type === 50000 && actor.id === expected.id).screenPosition[1];
     74           if (y >= 500 && y <= 600) break;
     75           assert(Date.now() < deadline, 'Camera did not frame the NPC above chat');
     76           mouseY += y > 600 ? 20 : -20;
     77           await game.page.mouse.move(cameraPoint.x, mouseY, {steps: 2});
     78         }
     79       } finally {
     80         sequence = game.sequence;
     81         await game.page.mouse.up({button: 'right'});
     82       }
     83       await game.waitInput(sequence, 1026);
     84       await game.click(...await game.pick(50000, expected.id));
     85       await game.wait('NPC selected', state => state.selected[0] === 50000 && state.selected[1] === expected.id);
     86       await game.save(`${output}/${phase}-${index}`);
     87       assert.deepEqual(game.errors, []);
     88     }
     89   }
     90   console.log('PASS:', expected.route, 'native appearance, picking and equipment replay on two clients before and after reconnect');
     91 } catch (error) {
     92   console.error(error);
     93   throw error;
     94 } finally {
     95   try { await Promise.all(games.map((game, index) => game.save(`${output}/final-${index}`))); }
     96   finally { await browser.close(); }
     97 }