aorecomp

Anarchy Online classic in the browser

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


tools/client/test_icon_persistence.mjs (7112B)


      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 evidence = {};
     12 let game;
     13 try {
     14   const context = await browser.newContext({viewport: {width: 1024, height: 768}});
     15   await context.addCookies([{name: 'ao_client', value: 'icon-persistence', url}]);
     16   const page = await context.newPage();
     17   game = await GameTest.attach(page);
     18   await page.goto(url);
     19   await game.ready();
     20   const grid = state => state.grids.find(grid => grid.cells.some(cell => cell.identity[0] === 104));
     21   const cell = slot => grid(game.state).cells.find(cell => cell.identity[0] === 104 && cell.identity[1] === slot && !cell.dummy);
     22   const clickCell = async target => game.click((target.bounds[0] + target.bounds[2]) / 2, (target.bounds[1] + target.bounds[3]) / 2);
     23   const server = async () => (await page.request.get(url + '/test-state')).json();
     24   const record = async name => {
     25     evidence[name] = {grid: grid(game.state), inventory: game.local().inventory, server: await server(),
     26       files: await page.evaluate(async () => {
     27         const files = [];
     28         async function visit(directory, path) {
     29           for await (const [name, handle] of directory.entries()) {
     30             if (handle.kind === 'directory') await visit(handle, path + '/' + name);
     31             else if (name === 'IconPositions.bin') files.push({path: path + '/' + name,
     32               bytes: Array.from(new Uint8Array(await (await handle.getFile()).arrayBuffer()))});
     33           }
     34         }
     35         await visit(await navigator.storage.getDirectory(), '');
     36         return files;
     37       })};
     38     await game.save(output + '/' + name);
     39     console.log(name, JSON.stringify({cells: evidence[name].grid?.cells.filter(cell => !cell.dummy).map(cell => [cell.index, cell.identity]),
     40       files: evidence[name].files.map(file => [file.path, file.bytes.length])}));
     41     const file = evidence[name].files.find(file => file.path.endsWith('/1/IconPositions.bin'));
     42     assert(file, 'The character icon file disappeared');
     43     const bytes = Buffer.from(file.bytes);
     44     assert.equal(bytes.readUInt32BE(0), bytes.length);
     45     let offset = 8, positions;
     46     for (let group = 0; group < bytes.readUInt32BE(4); ++group) {
     47       const owner = [bytes.readUInt32BE(offset), bytes.readUInt32BE(offset + 4), bytes.readUInt32BE(offset + 8)];
     48       const count = bytes.readUInt32BE(offset + 12);
     49       offset += 16;
     50       const rows = [];
     51       for (let entry = 0; entry < count; ++entry, offset += 12) {
     52         rows.push([bytes.readUInt32BE(offset), bytes.readUInt32BE(offset + 4), bytes.readUInt32BE(offset + 8)]);
     53       }
     54       if (owner[0] === 50000 && owner[1] === 1 && owner[2] === 1) positions = rows;
     55     }
     56     assert.equal(offset, bytes.length);
     57     assert.deepEqual(positions, evidence[name].grid.cells.filter(cell => !cell.dummy).map(cell => [cell.index, ...cell.identity]));
     58   };
     59   const inventoryOpen = async () => {
     60     await page.waitForFunction(() => touchRegion(500, 400)?.world === true);
     61     await game.snapshot();
     62     if (!grid(game.state)) await game.key('i');
     63     await game.wait('inventory grid visible', state => grid(state));
     64   };
     65   const zone = async shop => {
     66     const response = await page.request.post(url + '/test-zone?shop=' + (shop ? '1' : '0'));
     67     assert.equal(response.status(), 200);
     68     await game.wait('controlled zone ready', state => game.local(state)?.playfield === (shop ? 1183 : 705));
     69     await inventoryOpen();
     70   };
     71 
     72   // Make a reset observable without earning or replaying progression.
     73   await inventoryOpen();
     74   evidence.initial = await server();
     75   const source = cell(48);
     76   const target = grid(game.state).cells.find(cell => cell.index === 8);
     77   assert(source && target);
     78   await clickCell(source);
     79   await game.wait('weapon picked', state => state.dragged[0] === 104 && state.dragged[1] === 48);
     80   await clickCell(target);
     81   await game.wait('sparse weapon placement', state => state.dragged[0] === 0 &&
     82     grid(state)?.cells.some(cell => cell.index === 8 && cell.identity[1] === 48 && !cell.dummy));
     83   await record('moved');
     84   assert.deepEqual(await server(), evidence.initial);
     85   await page.reload();
     86   await game.ready();
     87   await inventoryOpen();
     88   await record('initial-reload');
     89   assert.equal(cell(48).index, 8, 'Initial reload reset the retained visual cell');
     90   await zone(true);
     91   await record('shop-zone');
     92   assert.equal(cell(48).index, 8, 'Zoning reset the retained visual cell');
     93 
     94   // Sell two exact instances and buy one item through real browser input.
     95   if (!game.state.camera.firstPerson) await game.key('F8');
     96   await game.click(...await game.pick(51035, 3221226655), 'right');
     97   const shopOpen = state => state.regions.some(([left, top, right, bottom]) => left === 832 && top === 0 && right === 1024 && bottom === 494);
     98   await game.wait('shop open', shopOpen);
     99   for (const slot of [49, 50]) {
    100     await clickCell(cell(slot));
    101     await game.wait('sale icon picked', state => state.dragged[0] === 104 && state.dragged[1] === slot);
    102     const sequence = game.sequence;
    103     await game.click(864, 416);
    104     await game.wait('sale offer sent', () => game.events.some(event => event.sequence > sequence && event.direction === 'framesent' && event.opcode === '36284f6e'));
    105   }
    106   await game.click(874, 475);
    107   await game.wait('parts sold', state => game.local(state).inventory.length === 2);
    108   await game.wait('accepted sale window closes', state => !shopOpen(state));
    109   await record('sold');
    110   await game.click(...await game.pick(51035, 3221226655), 'right');
    111   await game.wait('shop reopened', shopOpen);
    112   await game.click(864, 85);
    113   await game.click(874, 475);
    114   await game.wait('laboratory purchased', state => game.local(state).inventory.length === 3);
    115   await game.wait('accepted purchase window closes', state => !shopOpen(state));
    116   await record('purchased');
    117   assert.equal(cell(48).index, 8, 'Trade reset the retained visual cell');
    118   await zone(false);
    119   await record('city-zone');
    120   assert.equal(cell(48).index, 8, 'Return zoning reset the retained visual cell');
    121   const retained = await server();
    122   await page.reload();
    123   await game.ready();
    124   await inventoryOpen();
    125   await record('reloaded');
    126   assert.equal(cell(48).index, 8, 'Reload reset the retained visual cell');
    127   assert.deepEqual(await server(), retained);
    128   assert.equal(game.errors.length, 0);
    129   console.log('PASS: sparse layout, retained identities, sale/purchase and zoning/reload persistence');
    130 } finally {
    131   try {
    132     await fs.mkdir(output, {recursive: true});
    133     await fs.writeFile(output + '/verification.json', JSON.stringify(evidence, null, 2) + '\n');
    134     if (game) await game.save(output + '/final');
    135   } finally { await browser.close(); }
    136 }