aorecomp

Anarchy Online classic in the browser

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


tools/client/test_shop_refusal.mjs (4725B)


      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: 'shop-sale-refusal', 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 = slot => game.state.grids.flatMap(grid => grid.cells).find(cell =>
     20     cell.identity[0] === 104 && 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   await page.goto(url);
     24   await game.ready();
     25   await page.waitForFunction(() => touchRegion(500, 400)?.world === true);
     26   await game.snapshot();
     27   evidence.before = await server();
     28   await game.key('KeyI');
     29   if (!game.state.camera.firstPerson) await game.key('F8');
     30   await game.click(...await game.pick(51035, 3221488799), 'right');
     31   await game.wait('Tools shop opens', state => state.grids.some(grid =>
     32     grid.cells.some(cell => cell.identity[0] === 111 && !cell.dummy)));
     33   assert((await page.request.post(url + '/test-stale-view')).ok());
     34   evidence.merged = await server();
     35   await game.save(output + '/stale');
     36   await clickCell(cell(48));
     37   await game.wait('stale source picked', state => state.dragged[0] === 104 && state.dragged[1] === 48);
     38   const sequence = game.sequence;
     39   await game.click(860, 412);
     40   await game.wait('native stale sale request', () => game.events.some(event => event.sequence > sequence &&
     41     event.direction === 'framesent' && event.opcode === '36284f6e' &&
     42     Buffer.from(event.hex, 'hex').readUInt32BE(33) === 4));
     43   await game.wait('stale offer aborts shop', () => game.events.some(event => event.sequence > sequence &&
     44     event.direction === 'framereceived' && event.opcode === '36284f6e' &&
     45     Buffer.from(event.hex, 'hex').readUInt32BE(33) === 2));
     46   await game.wait('current source counts restored without a ghost', state => !cell(48) && cell(49) && cell(50) &&
     47     game.local(state).inventory.find(item => item.slot === 49)?.count === 8);
     48   evidence.refused = await server();
     49   assert.deepEqual(evidence.refused.inventory, evidence.merged.inventory);
     50   await game.save(output + '/refused');
     51 
     52   // Retry through ordinary native input on the same page, then cancel the sale.
     53   await game.click(...await game.pick(51035, 3221488799), 'right');
     54   await game.wait('shop reopens', state => state.grids.some(grid =>
     55     grid.cells.some(cell => cell.identity[0] === 111 && !cell.dummy)));
     56   await clickCell(cell(49));
     57   await game.wait('surviving stack picked', state => state.dragged[0] === 104 && state.dragged[1] === 49);
     58   await game.click(860, 412);
     59   await game.wait('current stack offered', state => state.grids.some(grid =>
     60     grid.cells.some(cell => cell.identity[0] === 108 && !cell.dummy)));
     61   await game.save(output + '/retry');
     62   await game.click(945, 475);
     63   await game.wait('cancelled offer remains usable in its restored slot', state => cell(48) && cell(50) &&
     64     game.local(state).inventory.some(item => item.slot === 48 && item.count === 8));
     65   evidence.after = await server();
     66   assert.deepEqual(evidence.after.inventory.Items.map(item => [item.ID, item.Slot, item.Count]), [[2, 64, 8], [3, 66, 7]]);
     67   assert.equal(evidence.after.inventory.Credits, 0);
     68   await page.reload();
     69   await game.ready();
     70   await page.waitForFunction(() => touchRegion(500, 400)?.world === true);
     71   await game.snapshot();
     72   assert.deepEqual(game.local().inventory.map(item => [item.id, item.slot, item.quality, item.count, ...item.templates]),
     73     [[2, 48, 2, 8, 25812, 25811], [3, 50, 2, 7, 25812, 25811]]);
     74   assert.deepEqual(await server(), evidence.after);
     75   assert.deepEqual(game.errors, []);
     76   console.log('PASS: stale shop source removed, current counts restored, native retry and page/cold ownership retain fifteen charges');
     77 } finally {
     78   try {
     79     await fs.mkdir(output, {recursive: true});
     80     await fs.writeFile(output + '/verification.json', JSON.stringify(evidence, null, 2) + '\n');
     81     if (game) await game.save(output + '/final');
     82   } finally { await browser.close(); }
     83 }