aorecomp

Anarchy Online classic in the browser

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


tools/client/test_npc_shops.mjs (5429B)


      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: 'npc-shops', 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   const cell = (state, page, slot) => state.grids.flatMap(grid => grid.cells).find(cell =>
     20     cell.identity[0] === page && cell.identity[1] === slot && !cell.dummy);
     21   const clickCell = async source => game.click(
     22     (source.bounds[0] + source.bounds[2]) / 2, (source.bounds[1] + source.bounds[3]) / 2);
     23   const waitServer = async (description, predicate) => {
     24     const deadline = Date.now() + 5000;
     25     while (!predicate(await server())) {
     26       assert(Date.now() < deadline, description);
     27       await game.snapshot();
     28     }
     29   };
     30   await page.goto(url);
     31   await game.ready();
     32   await page.waitForFunction(() => touchRegion(500, 400)?.world === true);
     33   evidence.before = await server();
     34   if (!game.state.camera.firstPerson) await game.key('F8');
     35   await game.key('KeyI');
     36   const open = async keyboard => {
     37     await game.click(...await game.pick(50000, evidence.before.npc), keyboard ? 'left' : 'right');
     38     if (keyboard) await game.key('KeyE');
     39     await game.wait('NPC catalog opens', state => cell(state, 111, 0));
     40     assert((await server()).open);
     41   };
     42   await open(false);
     43   await game.save(output + '/catalog');
     44   await clickCell(cell(game.state, 111, 0));
     45   await waitServer('catalog purchase selected', state => state.purchases === 1);
     46   await clickCell(cell(game.state, 104, 48));
     47   await game.wait('sale item picked', state => state.dragged[0] === 104);
     48   await game.click(860, 412);
     49   await waitServer('axe offered', state => state.sales === 1);
     50   await game.save(output + '/basket');
     51   await game.click(873, 475);
     52   await waitServer('NPC sale and purchase committed', state => !state.open && state.inventory.Items.length === 1 && state.inventory.Items[0].ID === 2);
     53   await game.wait('completed catalog removed', state => !state.actors.some(actor => actor.type === 51035 && actor.id === evidence.before.npc));
     54   evidence.purchased = await server();
     55   const food = evidence.before.route.startsWith('710/');
     56   assert.equal(evidence.purchased.inventory.Credits, food ? 1702 : 1327);
     57   assert.deepEqual(evidence.purchased.inventory.Items, [{ID: 2, Type: food ? 51018 : 51005,
     58     Template: food ? 130581 : 25812, Quality: 1, Slot: 64, Count: food ? 1 : 50, Ammo: 0}]);
     59 
     60   // E uses the selected living NPC; cancellation retains the same owned item.
     61   await open(true);
     62   console.log('Verified E-key catalog', evidence.before.route);
     63   await clickCell(cell(game.state, 111, 0));
     64   await waitServer('second selection', state => state.purchases === 1);
     65   await game.click(945, 475);
     66   await waitServer('cancelled', state => !state.open);
     67   await game.wait('cancelled catalog removed', state => !state.actors.some(actor => actor.type === 51035 && actor.id === evidence.before.npc));
     68   assert.deepEqual((await server()).inventory, evidence.purchased.inventory);
     69   await page.reload({waitUntil: 'domcontentloaded'});
     70   await page.waitForFunction(() => typeof Module !== 'undefined' && typeof Module._browser_test_request === 'function', null, {timeout: 30000});
     71   await game.ready();
     72   console.log('Reload ready', evidence.before.route);
     73   evidence.reloaded = await server();
     74   assert.deepEqual(evidence.reloaded.inventory, evidence.purchased.inventory);
     75   await game.wait('durable purchased identity replayed', state => game.local(state).inventory.some(item => item.id === 2));
     76 
     77   // Keep a draft open while ordinary backward movement leaves the seller's range.
     78   if (!game.state.camera.firstPerson) await game.key('F8');
     79   await open(false);
     80   await clickCell(cell(game.state, 111, 0));
     81   await waitServer('range draft selected', state => state.purchases === 1);
     82   await game.hold('KeyS', 'walk out of NPC range', state => Math.hypot(
     83     game.local(state).position[0] - evidence.before.position[0],
     84     game.local(state).position[2] - evidence.before.position[2]) > 6);
     85   await waitServer('leaving NPC range cancels the draft', state => !state.open);
     86   await game.wait('out-of-range catalog removed', state => !state.actors.some(actor => actor.type === 51035 && actor.id === evidence.before.npc));
     87   evidence.outside = await server();
     88   assert.deepEqual(evidence.outside.inventory, evidence.purchased.inventory);
     89   assert.deepEqual(game.errors, []);
     90   console.log('PASS:', evidence.before.route, 'right-click/E, purchase/sale, cancel, reconnect and range rejection');
     91 } finally {
     92   try {
     93     await fs.mkdir(output, {recursive: true});
     94     await fs.writeFile(output + '/verification.json', JSON.stringify(evidence, null, 2) + '\n');
     95     if (game) await game.save(output + '/final');
     96   } finally { await browser.close(); }
     97 }