aorecomp

Anarchy Online classic in the browser

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


tools/client/test_basic_shop_armor.mjs (4191B)


      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: 'basic-shop-armor', 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 waitServer = (label, predicate) => game.waitFor(label, async () => predicate(await server()));
     20   const cell = (type, slot) => game.state.grids.flatMap(grid => grid.cells).find(cell =>
     21     cell.identity[0] === type && cell.identity[1] === slot && !cell.dummy);
     22   const click = async (source, button = 'left') => {
     23     assert(source, 'expected native catalog/inventory cell');
     24     await game.click((source.bounds[0]+source.bounds[2])/2, (source.bounds[1]+source.bounds[3])/2, button);
     25   };
     26   await page.goto(url);
     27   await game.ready();
     28   if (!game.state.camera.firstPerson) await game.key('F8');
     29   await game.key('KeyI');
     30   evidence.before = await server();
     31   const open = async keyboard => {
     32     await game.click(...await game.pick(51035, evidence.before.machine), keyboard ? 'left' : 'right');
     33     if (keyboard) await game.key('KeyE');
     34     await game.wait('basic armor catalog opens', state => state.grids.some(grid =>
     35       grid.rows === 4 && grid.cells.some(cell => cell.identity[0] === 111 && cell.identity[1] === 6 && !cell.dummy)));
     36   };
     37   await open(false);
     38   await game.save(output + '/catalog');
     39   await click(cell(111, 6));
     40   await waitServer('gloves selected for cancellation', state => state.purchases === 1);
     41   await game.click(945, 475);
     42   await waitServer('cancelled without charge', state => !state.open);
     43   assert.deepEqual((await server()).inventory, evidence.before.inventory);
     44 
     45   await open(true);
     46   await click(cell(111, 6));
     47   await waitServer('gloves selected again', state => state.purchases === 1);
     48   await game.click(873, 475);
     49   await waitServer('purchase committed', state => !state.open && state.inventory.Items.length === 1);
     50   evidence.purchased = await server();
     51   assert.equal(evidence.purchased.inventory.Credits, evidence.before.playfield === 1183 ? 875 : 869);
     52   assert.deepEqual(evidence.purchased.inventory.Items.map(item => [item.ID, item.Template, item.Quality, item.Count, item.Slot]), [[1, 70562, 1, 1, 64]]);
     53   await game.wait('native gloves delivered', state => game.local(state).inventory.some(item => item.templates[0] === 70562 && item.slot === 48));
     54   await click(cell(104, 48), 'right');
     55   await waitServer('server wears purchased gloves', state => state.inventory.Items[0].Slot === 24 && state.armor === 3);
     56   await game.wait('native armor agrees', state => game.local(state).effectiveArmor[0] === 3);
     57   evidence.worn = await server();
     58   await game.save(output + '/worn');
     59 
     60   await page.reload();
     61   await game.ready();
     62   await game.wait('native armor retained', state => game.local(state).effectiveArmor[0] === 3);
     63   evidence.reconnected = await server();
     64   assert.deepEqual(evidence.reconnected.inventory, evidence.worn.inventory);
     65   assert.equal(game.local().stats[61], evidence.worn.inventory.Credits);
     66   assert(game.local().inventory.some(item => item.id === 1 && item.slot === 24));
     67   await game.save(output + '/reconnected');
     68   assert.deepEqual(game.errors, []);
     69   console.log('PASS:', evidence.before.playfield, 'native basic armor cancel, purchase, wear and reconnect');
     70 } finally {
     71   try {
     72     await fs.mkdir(output, {recursive: true});
     73     await fs.writeFile(output + '/verification.json', JSON.stringify(evidence, null, 2) + '\n');
     74     if (game) await game.save(output + '/final');
     75   } finally { await browser.close(); }
     76 }