aorecomp

Anarchy Online classic in the browser

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


tools/client/test_corpse_pickup_expiry.mjs (4474B)


      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 const games = [];
     12 const evidence = {};
     13 const source = (state, slot) => state.grids.flatMap(grid => grid.cells).find(cell =>
     14   cell.identity[0] === 107 && cell.identity[1] === slot && !cell.dummy);
     15 const closed = state => !state.actors.some(actor => actor.id === 1000000 && (actor.type === 51050 || actor.type === 50000)) &&
     16   !state.regions.some(region => region[0] === 0 && region[1] === 0 && region[2] === 178 && region[3] === 128);
     17 try {
     18   for (let id = 1; id <= 2; ++id) {
     19     const context = await browser.newContext({viewport: {width: 1024, height: 768}});
     20     await context.addCookies([{name: 'ao_client', value: 'corpse-peer-' + id, url}]);
     21     const page = await context.newPage();
     22     const game = await GameTest.attach(page);
     23     games.push(game);
     24     await page.goto(url);
     25     await game.ready();
     26     await game.click(...await game.pick(51050, 1000000), 'right');
     27     await game.wait('two shared corpse sources', state => source(state, 0) && source(state, 7));
     28   }
     29   const [first, second] = games;
     30   const server = async () => (await first.page.request.get(url + '/test-state')).json();
     31   await second.click(30, 45);
     32   await second.wait('one item collected before expiry', state => second.local(state).inventory.length === 1);
     33   await first.wait('remaining nonzero source retained', state => source(state, 7));
     34   evidence.partial = await server();
     35 
     36   // Expire the remaining source during a consumed native mouse-down gesture.
     37   const cell = source(first.state, 7);
     38   const point = await first.point((cell.bounds[0] + cell.bounds[2]) / 2, (cell.bounds[1] + cell.bounds[3]) / 2);
     39   await first.page.bringToFront();
     40   await first.page.mouse.move(point.x, point.y);
     41   const sequence = first.sequence;
     42   await first.page.mouse.down();
     43   try {
     44     await first.waitInput(sequence, 1025);
     45     assert.equal((await first.page.request.post(url + '/test-expire')).status(), 204);
     46     await first.wait('expiry closes the clicked source and corpse', closed);
     47     await second.wait('expiry closes the other member window', closed);
     48   } finally {
     49     const release = first.sequence;
     50     await first.page.mouse.up();
     51     await first.waitInput(release, 1026);
     52   }
     53   await first.click((cell.bounds[0] + cell.bounds[2]) / 2, (cell.bounds[1] + cell.bounds[3]) / 2);
     54   await first.snapshot();
     55   evidence.expired = await server();
     56   evidence.pickupsAfterExpiry = first.events.filter(event => event.sequence > sequence &&
     57     event.direction === 'framesent' && event.opcode === '47537a24').length;
     58   assert.equal(evidence.pickupsAfterExpiry, 0);
     59   assert.deepEqual(evidence.expired, {...evidence.partial, present: false, loot: [0, 0], interest: [false, false]});
     60   evidence.retirement = [];
     61   for (const game of games) {
     62     const retirement = game.events.filter(event => event.direction === 'framereceived' && event.actor?.[1] === 1000000 &&
     63       (event.opcode === '2049527c' || event.opcode === '36510078')).map(event => [event.opcode, ...event.actor]);
     64     assert.deepEqual(retirement, [['2049527c', 51050, 1000000], ['36510078', 50000, 1000000], ['36510078', 51050, 1000000]]);
     65     evidence.retirement.push(retirement);
     66     await game.save(output + '/expired-' + (game === first ? 1 : 2));
     67     await game.page.reload();
     68     await game.ready();
     69     assert.deepEqual(game.local().inventory.map(item => [item.type, item.id, item.slot, item.quality, item.count]),
     70       game === second ? [[51005, 1, 48, 1, 1]] : []);
     71     assert(closed(game.state));
     72     assert.deepEqual(game.errors, []);
     73   }
     74   console.log('PASS: corpse expiry during native pickup closes both windows, preserves collected loot and destroys only untaken loot');
     75 } finally {
     76   try {
     77     await fs.mkdir(output, {recursive: true});
     78     await fs.writeFile(output + '/verification.json', JSON.stringify(evidence, null, 2) + '\n');
     79     for (let index = 0; index < games.length; ++index) await games[index].save(output + '/final-' + (index + 1));
     80   } finally { await browser.close(); }
     81 }