aorecomp

Anarchy Online classic in the browser

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


tools/client/test_starter_shops.mjs (6161B)


      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: 'starter-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 = (type, slot) => game.state.grids.flatMap(grid => grid.cells).find(cell =>
     20     cell.identity[0] === type && cell.identity[1] === slot && !cell.dummy);
     21   const click = async (source, button = 'left') => {
     22     assert(source, 'expected native inventory/catalog cell is present');
     23     await game.click((source.bounds[0] + source.bounds[2]) / 2, (source.bounds[1] + source.bounds[3]) / 2, button);
     24   };
     25   const waitServer = (label, predicate) => game.waitFor(label, async () => predicate(await server()));
     26   await page.goto(url);
     27   await game.ready(); assert.equal((await game.page.request.post(url + '/test-defer-recovery')).status(), 204);
     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('all seven starter catalog rows', () => [0, 1, 2, 3, 4, 5, 6].every(index => cell(111, index)));
     35     assert((await server()).open);
     36   };
     37   await open(false);
     38   await game.save(output + '/catalog');
     39 
     40   // Buy every authored package and sell one controlled loot item atomically.
     41   for (let index = 0; index < 7; ++index) {
     42     await click(cell(111, index));
     43     await waitServer('purchase row selected', state => state.purchases === index + 1);
     44   }
     45   await click(cell(104, 48));
     46   await game.wait('sale loot picked', state => state.dragged[0] === 104);
     47   await game.click(860, 412);
     48   await waitServer('loot offered', state => state.sales === 1);
     49   await game.save(output + '/basket');
     50   await game.click(873, 475);
     51   await waitServer('supplies committed', state => !state.open && state.inventory.Items.length === 8);
     52   evidence.purchased = await server();
     53   assert.equal(evidence.purchased.inventory.Credits, evidence.before.playfield === 950 ? 950 : 798);
     54   assert.deepEqual(evidence.purchased.inventory.Items.slice(1).map(item => [item.ID, item.Template, item.Quality, item.Count]), [
     55     [3, 25812, 1, 50], [4, 85366, 1, 25], [5, 25820, 1, 50],
     56     [6, 21605, 1, 100], [7, 21609, 1, 100], [8, 21613, 1, 100], [9, 21601, 1, 100],
     57   ]);
     58 
     59   // Reopen with E and cancel a second basket without another debit or delivery.
     60   await open(true);
     61   await click(cell(111, 0));
     62   await waitServer('cancel draft selected', state => state.purchases === 1);
     63   await game.click(945, 475);
     64   await waitServer('draft cancelled', state => !state.open);
     65   assert.deepEqual((await server()).inventory, evidence.purchased.inventory);
     66 
     67   // Use all three purchased recovery families through their real inventory cells.
     68   await game.key('F1');
     69   await game.key('KeyX');
     70   await game.wait('patient seated', state => game.local(state).stats[430] === 2);
     71   for (const [slot, id, remaining] of [[48, 3, 49], [49, 4, 24], [50, 5, 49]]) {
     72     await click(cell(104, slot), 'right');
     73     await waitServer('one purchased medical charge consumed', state => state.inventory.Items.find(item => item.ID === id)?.Count === remaining);
     74     const treated = await server();
     75     // Compare the item effect before later native passive recovery ticks.
     76     await game.wait('native charge and recovered resource agree', state =>
     77       game.local(state).inventory.find(item => item.slot === slot)?.count === remaining &&
     78       (id === 3 ? game.local(state).stats[27] === treated.health : game.local(state).stats[214] === treated.nano));
     79   }
     80   evidence.treated = await server();
     81   assert.equal(evidence.treated.health, 31);
     82   assert(evidence.treated.nano >= 41 && evidence.treated.nano <= 43);
     83   await game.save(output + '/recovery');
     84 
     85   // Put the purchased bullets into the controlled empty finite pistol.
     86   await game.key('KeyX');
     87   await page.keyboard.press('Control+1');
     88   await click(cell(104, 51));
     89   await game.wait('purchased bullets picked', state => state.dragged[0] === 104 && state.dragged[1] === 51);
     90   await game.click(867, 206, 'right');
     91   await waitServer('reload commits both inventory sides', state => state.inventory.Items[0].Ammo === 20 && state.inventory.Items.find(item => item.ID === 6).Count === 80);
     92   await game.wait('native pistol and ammunition agree', state => game.local(state).weapons.some(weapon => weapon.loadedAmmo === 20) &&
     93     game.local(state).inventory.find(item => item.slot === 51)?.count === 80);
     94   evidence.used = await server();
     95   await game.save(output + '/reloaded-pistol');
     96 
     97   await page.reload();
     98   await game.ready(); assert.equal((await game.page.request.post(url + '/test-defer-recovery')).status(), 204);
     99   evidence.reconnected = await server();
    100   assert.deepEqual(evidence.reconnected.inventory, evidence.used.inventory);
    101   assert.equal(evidence.reconnected.health, evidence.used.health);
    102   assert.equal(evidence.reconnected.nano, evidence.used.nano);
    103   assert.deepEqual(game.errors, []);
    104   console.log('PASS:', evidence.before.machine, 'seven stock rows, buy/sell, E/cancel, three recovery uses, bullet reload and reconnect');
    105 } finally {
    106   try {
    107     await fs.mkdir(output, {recursive: true});
    108     await fs.writeFile(output + '/verification.json', JSON.stringify(evidence, null, 2) + '\n');
    109     if (game) await game.save(output + '/final');
    110   } finally { await browser.close(); }
    111 }