aorecomp

Anarchy Online classic in the browser

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


tools/client/test_worn_container_deposit.mjs (6653B)


      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: 'worn-container-deposit', url}]);
     16   const page = await context.newPage();
     17   game = await GameTest.attach(page);
     18   await page.goto(url);
     19   await game.ready();
     20   const server = async () => (await page.request.get(url + '/test-state')).json();
     21   const cell = (state, page, slot) => state.grids.flatMap(grid => grid.cells).find(cell =>
     22     cell.identity[0] === page && cell.identity[1] === slot && !cell.dummy);
     23   const click = async (cell, button = 'left') => {
     24     assert(cell);
     25     await game.click((cell.bounds[0] + cell.bounds[2]) / 2, (cell.bounds[1] + cell.bounds[3]) / 2, button);
     26   };
     27   await game.key('i');
     28   await click(cell(game.state, 104, 48), 'right');
     29   await game.wait('empty backpack open', state => state.grids.some(grid => grid.totalCells === 21));
     30   await page.keyboard.press('Control+1');
     31   const protection = [5, 5, 1, 1, 5, 5, 1, 5];
     32   assert.deepEqual(game.local().effectiveArmor, protection);
     33   for (const item of [
     34     {name: 'weapon', id: 1, page: 101, slot: 6, tab: 867, x: 867, y: 206},
     35     {name: 'armor', id: 3, page: 102, slot: 21, tab: 953, x: 927, y: 148}]) {
     36     // Refuse the real direct deposit without changing ownership or worn effects.
     37     await game.click(item.tab, 53);
     38     const before = await server(), sequence = game.sequence;
     39     await game.click(item.x, item.y);
     40     await game.wait('worn source picked', state => state.dragged[0] === item.page && state.dragged[1] === item.slot);
     41     await click(game.state.grids.find(grid => grid.totalCells === 21).cells.find(cell => cell.identity[0] === 0));
     42     await game.wait('real worn-to-backpack request', () => game.events.some(event => event.sequence > sequence &&
     43       event.direction === 'framesent' && event.opcode === '47537a24' &&
     44       Buffer.from(event.hex, 'hex').readUInt32BE(29) === item.page && Buffer.from(event.hex, 'hex').readUInt32BE(37) === 51017));
     45     await game.save(output + '/' + item.name + '-requested');
     46     await game.wait('worn deposit receives cancellation', () => game.events.some(event => event.sequence > sequence &&
     47       event.direction === 'framereceived' && event.opcode === '5e477770'));
     48     const replies = game.events.filter(event => event.sequence > sequence && event.direction === 'framereceived' &&
     49       event.opcode === '5e477770').map(event => Array.from({length: 5}, (_, index) => Buffer.from(event.hex, 'hex').readUInt32BE(29 + index * 4)));
     50     assert.deepEqual(replies, [[146, item.page, item.slot, 0, 0]]);
     51     evidence[item.name] = {before, refused: await server()};
     52     assert.deepEqual(evidence[item.name].refused, before);
     53     assert.deepEqual(game.local().effectiveArmor, protection);
     54     assert.deepEqual(game.state.dragged, [0, 0]);
     55     await game.save(output + '/' + item.name + '-corrected');
     56 
     57     // Pick that same icon again, remove normally, then deposit its carried proxy.
     58     await game.click(item.x, item.y);
     59     await game.wait('corrected worn icon can be picked again', state => state.dragged[0] === item.page && state.dragged[1] === item.slot);
     60     const removal = game.sequence;
     61     await click(game.state.grids.find(grid => grid.totalCells === 30).cells.find(cell => cell.identity[0] === 0));
     62     await game.wait('real ordinary equipment removal', () => game.events.some(event => event.sequence > removal &&
     63       event.direction === 'framesent' && event.opcode === '47537a24' && Buffer.from(event.hex, 'hex').readUInt32BE(37) === 50000));
     64     evidence[item.name].pending = await server();
     65     assert.equal(evidence[item.name].pending.pending.itemID, item.id);
     66     assert((await page.request.post(url + '/test-complete')).ok());
     67     await game.wait('exact item becomes carried', state => game.local(state).inventory.some(owned => owned.id === item.id && owned.slot >= 48));
     68     await game.wait('ordinary removal animation ends', state => !game.local(state).actions.includes(3) && !game.local(state).actions.includes(6));
     69     const carried = game.local().inventory.find(owned => owned.id === item.id);
     70     await click(cell(game.state, 104, carried.slot));
     71     await game.wait('retained carried source picked', state => state.dragged[0] === 104 && state.dragged[1] === carried.slot);
     72     await click(game.state.grids.find(grid => grid.totalCells === 21).cells.find(cell => cell.identity[0] === 0));
     73     const packed = 1 << 16 | (item.id === 1 ? 0 : 1);
     74     await game.wait('ordinary deposit succeeds', state => cell(state, 107, packed));
     75     evidence[item.name].after = await server();
     76     assert.equal(evidence[item.name].after.pending, null);
     77     if (item.id === 3) protection.fill(0);
     78     await game.wait('worn protection changes exactly once', state => game.local(state).effectiveArmor.every((value, index) => value === protection[index]));
     79     assert.deepEqual(evidence[item.name].after.armor, protection);
     80     await game.save(output + '/' + item.name + '-retried');
     81   }
     82   evidence.after = await server();
     83   assert.deepEqual(evidence.after.inventory.Items.map(item => [item.ID, item.Slot]), [[2, 64]]);
     84   assert.deepEqual(evidence.after.inventory.Containers[2].map(item => [item.ID, item.Slot]), [[1, 0], [3, 1]]);
     85   await page.reload();
     86   await game.ready();
     87   if (!cell(game.state, 104, 48)) await game.key('i');
     88   await click(cell(game.state, 104, 48), 'right');
     89   await game.wait('both exact stored items replay', state => cell(state, 107, 65536) && cell(state, 107, 65537));
     90   assert.deepEqual(await server(), evidence.after);
     91   assert.deepEqual(game.local().effectiveArmor, [0, 0, 0, 0, 0, 0, 0, 0]);
     92   console.log('PASS: worn weapon/armor refusals restore usable icons; normal removal/deposit and page/cold recovery conserve all three instances');
     93   assert.deepEqual(game.errors, []);
     94   assert.equal(game.dropped, 0);
     95 } finally {
     96   try {
     97     await fs.mkdir(output, {recursive: true});
     98     await fs.writeFile(output + '/verification.json', JSON.stringify(evidence, null, 2) + '\n');
     99     if (game) await game.save(output + '/final');
    100   } finally { await browser.close(); }
    101 }