aorecomp

Anarchy Online classic in the browser

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


tests/e2e/browser.test.mjs (8302B)


      1 import assert from 'node:assert/strict';
      2 import fs from 'node:fs/promises';
      3 import {before, test} from 'node:test';
      4 import {buildServer, withFixture} from './fixture.mjs';
      5 import {checkStarterCold} from './starter-cold.mjs';
      6 
      7 before(buildServer);
      8 
      9 // These scenarios use the normal client protocol after preparing their fixtures.
     10 test('movement, targeting, posture and page recovery', {timeout: 180_000}, async () => {
     11   await withFixture('interaction', async fixture => {
     12     const context = await fixture.visitor();
     13     await fixture.setup(context, 'position', {playfield: 705, position: [960, 16.5, 510], rotation: [0, 0, 0, 1]});
     14     const target = await fixture.setup(context, 'leet', {position: [960, 16.5, 498]});
     15     const game = await fixture.game(context);
     16     const page = game.page;
     17     assert.equal(game.state.localId, 1);
     18     assert.deepEqual(game.local().inventory.map(item => item.templates[0]).sort((a, b) => a - b),
     19       [29092, 29426, 99719, 116628, 116629, 116693, 121569]);
     20     const inventory = game.local().inventory;
     21 
     22     const z = game.local().position[2];
     23     await game.hold('KeyS', 'backward movement', state => game.local(state).position[2] < z - 0.5);
     24     await game.hold('KeyW', 'forward movement', state => game.local(state).position[2] > z + 0.5);
     25     await game.key('F1');
     26     await game.wait('self selection', state => state.selected[0] === 50000 && state.selected[1] === 1);
     27     await game.key('x');
     28     await game.wait('sitting posture', state => game.local(state).stats[430] === 2);
     29     assert.equal((await fixture.state(context)).sitting, true);
     30     await game.key('x');
     31     await game.wait('standing posture', state => game.local(state).stats[430] !== 2);
     32     await assert.rejects(game.hold('w', 'intentional timeout', () => false, 1), /intentional timeout/);
     33     await game.snapshot();
     34 
     35     if (!game.state.camera.firstPerson) await game.key('F8');
     36     await game.hold('KeyD', 'turn toward the open street', state => Math.abs(game.local(state).rotation[3]) < 0.05);
     37     const [x, y] = await game.pick(50000, target.id);
     38     await game.click(x, y);
     39     await game.wait('native world selection', state => state.selected[0] === 50000 && state.selected[1] === target.id);
     40     await game.key('i');
     41     await game.save(fixture.output + '/inventory');
     42     await game.key('i');
     43     await page.setViewportSize({width: 800, height: 600});
     44     await game.wait('native resolution change', state => state.screen[0] === 800 && state.screen[1] === 600);
     45     const position = game.local().position;
     46     await page.reload();
     47     await game.ready();
     48     assert.deepEqual(game.local().inventory, inventory);
     49     assert(Math.abs(game.local().position[2] - position[2]) < 0.2);
     50     assert(game.events.some(event => event.kind === 'packet' && event.direction === 'framesent'));
     51     assert(game.events.some(event => event.kind === 'packet' && event.direction === 'framereceived'));
     52     assert(game.events.some(event => event.kind === 'input' && event.stage === 'consumed'));
     53     const state = await fixture.state(context);
     54     assert.equal(state.sitting, false);
     55     assert(state.position[2] > 510.5);
     56     assert.equal(state.items.length, 7);
     57     assert.deepEqual(game.errors, []);
     58   });
     59 });
     60 
     61 test('overflow refusal preserves both stacks through restart', {timeout: 180_000}, async () => {
     62   await withFixture('stack-overflow', async fixture => {
     63     const context = await fixture.visitor();
     64     await fixture.setup(context, 'position', {playfield: 950, position: [52.625, 14.75, 56.875], rotation: [0, 0, 0, 1]});
     65     for (let index = 0; index < 2; ++index) await fixture.setup(context, 'grant-item', {template: 152030});
     66     await fixture.setup(context, 'item', {id: 8, slot: 71, count: 1});
     67     const before = await fixture.setup(context, 'item', {id: 9, slot: 72, count: 65535});
     68     const cookies = await context.cookies();
     69     const game = await fixture.game(context);
     70     await game.key('i');
     71     const cell = slot => game.state.grids.flatMap(grid => grid.cells).find(cell =>
     72       cell.identity[0] === 104 && cell.identity[1] === slot && !cell.dummy);
     73     const click = async slot => {
     74       const source = cell(slot);
     75       assert(source, 'the exact source must be usable');
     76       await game.click((source.bounds[0] + source.bounds[2]) / 2, (source.bounds[1] + source.bounds[3]) / 2);
     77     };
     78     for (const attempt of [0, 1]) {
     79       const sequence = game.sequence;
     80       await click(55);
     81       await click(56);
     82       await game.wait('actual overflow join request', () => game.events.some(event => event.sequence > sequence &&
     83         event.direction === 'framesent' && event.opcode === '5e477770' && Buffer.from(event.hex, 'hex').readUInt32BE(29) === 53));
     84       await game.save(fixture.output + '/refused-' + attempt);
     85       await game.wait('both overflow sources remain usable', () => cell(55) && cell(56));
     86       assert.deepEqual(game.state.dragged, [0, 0]);
     87       assert.deepEqual(game.local().inventory.filter(item => item.id >= 8).map(item => [item.slot, item.count]).sort((a, b) => a[0] - b[0]),
     88         [[55, 1], [56, 65535]]);
     89       const state = await fixture.state(context);
     90       assert.deepEqual(state.items, before.items);
     91       assert.equal(state.lastItem, 9);
     92     }
     93     await game.page.reload();
     94     await game.ready();
     95     assert.deepEqual(game.local().inventory.filter(item => item.id >= 8).map(item => [item.id, item.slot, item.count]).sort((a, b) => a[0] - b[0]),
     96       [[8, 55, 1], [9, 56, 65535]]);
     97     assert.deepEqual(game.errors, []);
     98     await game.save(fixture.output + '/before-restart');
     99     await fixture.restart();
    100     const restored = await fixture.context();
    101     await restored.addCookies(cookies);
    102     const state = await fixture.state(restored);
    103     assert.deepEqual(state.items, before.items);
    104     assert.equal(state.lastItem, 9);
    105   });
    106 });
    107 
    108 test('two starter accounts retain exact private progress after cold restart', {timeout: 240_000}, async () => {
    109   await withFixture('starter-cold', async fixture => {
    110     const context = await fixture.visitor();
    111     const expected = {};
    112     for (const name of ['Starterowner', 'Starterwatch']) {
    113       const response = await context.request.post(fixture.url + '/browser-account/create', {form: {
    114         name, password: 'starter regression password', head_mesh: '40681',
    115         profession: '9', side: '2', build: '1', height: '100'}});
    116       assert.equal(response.status(), 204, await response.text());
    117       if (name === 'Starterowner') {
    118         await fixture.setup(context, 'progress', {level: 2, experience: 150, ip: 5006, credits: 50});
    119         const state = await fixture.state(context);
    120         for (const item of state.items) {
    121           if (item.templates[0] === 121564) await fixture.setup(context, 'item', {id: item.id, slot: 6, count: item.count});
    122           if (item.templates[0] === 116628) await fixture.setup(context, 'item', {id: item.id, slot: item.slot, count: 17});
    123         }
    124       }
    125       const state = await fixture.state(context);
    126       expected[name] = {items: state.items, level: state.level, experience: state.experience, ip: state.ip, credits: state.credits};
    127     }
    128     await fixture.restart();
    129     const evidence = await checkStarterCold(fixture.browser, fixture.url, fixture.output, async page => {
    130       const state = await fixture.state(page.context());
    131       assert.deepEqual({items: state.items, level: state.level, experience: state.experience, ip: state.ip, credits: state.credits}, expected[state.name]);
    132       return {...state, items: state.items.map(item => [item.id, item.type, item.slot, item.quality, item.count, ...item.templates])};
    133     });
    134     assert.equal(evidence.Starterowner.credits, 50);
    135     assert.equal(evidence.Starterowner.ip, 5006);
    136   });
    137 });
    138 
    139 test('a failing scenario closes its browser, server and temporary database', {timeout: 120_000}, async () => {
    140   let fixture;
    141   await assert.rejects(withFixture('cleanup-failure', async running => {
    142     fixture = running;
    143     await running.game(await running.visitor());
    144     throw new Error('intentional scenario failure');
    145   }), /intentional scenario failure/);
    146   assert.equal(fixture.browser.isConnected(), false);
    147   assert.equal(fixture.process.exitCode, 0);
    148   await assert.rejects(fs.stat(fixture.directory), {code: 'ENOENT'});
    149   await assert.rejects(fetch(fixture.url), /fetch failed/);
    150 });