aorecomp

Anarchy Online classic in the browser

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


tools/client/test_generated_mission.mjs (5405B)


      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: 'generated-mission', url}]);
     16   const page = await context.newPage();
     17   game = await GameTest.attach(page);
     18   const server = async () => (await page.request.get(url + '/test-state')).json();
     19   await page.goto(url);
     20   await game.ready();
     21   evidence.initial = await server();
     22   await game.walk(89.3, 151.2);
     23   await game.wait('generated mission entered', snapshot => game.local(snapshot)?.playfield === evidence.initial.mission);
     24   await game.ready();
     25   evidence.entered = await server();
     26   await game.save(output + '/entered');
     27 
     28   // Clear the entrance's original raised lips using ordinary forward jumps.
     29   for (const z of [14.5, 23.5]) {
     30     await game.hold('KeyW', 'reach the jump takeoff', snapshot => game.local(snapshot).position[2] <= 60 - z);
     31     const start = game.local().position;
     32     await page.keyboard.down('KeyW');
     33     try {
     34       await game.key('Space');
     35       await game.wait('raised doorway cleared', snapshot => {
     36         const position = game.local(snapshot).position;
     37         return Math.hypot(position[0] - start[0], position[2] - start[2]) >= 2.5;
     38       });
     39     } finally {
     40       const sequence = game.sequence;
     41       await page.keyboard.up('KeyW');
     42       await game.waitInput(sequence, 769);
     43     }
     44   }
     45   await game.hold('KeyW', 'reach the second room', snapshot => game.local(snapshot).position[2] <= 28.5);
     46   await game.save(output + '/chamber');
     47   await page.reload();
     48   await game.ready();
     49   await page.waitForFunction(() => touchRegion(500, 400)?.world === true);
     50   await game.snapshot();
     51 
     52   // Frame and collect the original book through the native picker and pickup action.
     53   if (!game.state.camera.firstPerson) await game.key('F8');
     54   const target = () => game.state.actors.find(actor => actor.type === 51005 && actor.id === evidence.entered.objective);
     55   await game.wait('first-person camera and projected objective', snapshot => snapshot.camera.firstPerson && target()?.screenPosition);
     56   const point = await game.point(512, 300), deadline = Date.now() + 10000;
     57   let y = point.y;
     58   await page.mouse.move(point.x, y);
     59   await page.mouse.down({button: 'right'});
     60   try {
     61     for (;;) {
     62       await game.snapshot();
     63       const screenY = target().screenPosition[1];
     64       if (screenY >= 350 && screenY <= 540) break;
     65       assert(Date.now() < deadline, 'Objective did not enter the camera view');
     66       y += screenY > 540 ? 20 : -20;
     67       await page.mouse.move(point.x, y, {steps: 2});
     68     }
     69   } finally { await page.mouse.up({button: 'right'}); }
     70   await game.click(...await game.pick(51005, evidence.entered.objective));
     71   await game.key('KeyR');
     72   await game.wait('objective proxy collected', snapshot => game.local(snapshot).inventory.some(item => item.templates.includes(40822)));
     73   await game.wait('collected objective floor model removed', snapshot => !snapshot.actors.some(actor =>
     74     actor.type === 51005 && actor.id === evidence.entered.objective));
     75   evidence.collected = await server();
     76   assert.equal(evidence.collected.inventory.Items.filter(item => item.ID === evidence.entered.objective && item.Template === 40822).length, 1);
     77   await game.save(output + '/collected');
     78 
     79   // Reach the back of the second room, then return through the original exit.
     80   await game.hold('KeyW', 'reach the far chamber', snapshot => game.local(snapshot).position[2] <= 17);
     81   await game.save(output + '/far-room');
     82   await game.walk(25, 32);
     83   await game.hold('KeyW', 'return to the entrance lip', snapshot => game.local(snapshot).position[2] >= 33.5);
     84   const start = game.local().position;
     85   await page.keyboard.down('KeyW');
     86   try {
     87     await game.key('Space');
     88     await game.wait('return lip cleared', snapshot => Math.hypot(
     89       game.local(snapshot).position[0] - start[0], game.local(snapshot).position[2] - start[2]) >= 2.5);
     90   } finally { await page.keyboard.up('KeyW'); }
     91   await game.hold('KeyW', 'walk through the exterior portal', snapshot => game.local(snapshot)?.playfield === 3000);
     92   await game.wait('original exterior reached', snapshot => game.local(snapshot)?.playfield === 3000);
     93   await game.ready();
     94   await page.waitForFunction(() => touchRegion(500, 400)?.world === true);
     95   await game.snapshot();
     96   evidence.final = await server();
     97   assert.equal(evidence.final.inventory.Items.filter(item => item.ID === evidence.entered.objective).length, 1);
     98   assert.deepEqual(game.errors, []);
     99   console.log('PASS: seeded south-facing mission, native entrance/objective/exit and page recovery');
    100 } finally {
    101   try {
    102     await fs.mkdir(output, {recursive: true});
    103     await fs.writeFile(output + '/verification.json', JSON.stringify(evidence, null, 2) + '\n');
    104     if (game) await game.save(output + '/final');
    105   } finally { await browser.close(); }
    106 }