aorecomp

Anarchy Online classic in the browser

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


client/tests/game_test.mjs (11607B)


      1 import assert from 'node:assert/strict';
      2 import test from 'node:test';
      3 import fs from 'node:fs';
      4 import vm from 'node:vm';
      5 import {GameTest, PacketStream} from '../../tools/client/game_test.mjs';
      6 
      7 function frame(offset, bytes) {
      8   const result = Buffer.alloc(8 + bytes.length);
      9   result.writeBigUInt64BE(BigInt(offset));
     10   bytes.copy(result, 8);
     11   return result;
     12 }
     13 
     14 test('packets survive fragmented headers, padding and replayed stream bytes', () => {
     15   const stream = new PacketStream();
     16   const first = Buffer.alloc(24);
     17   first.writeUInt16BE(21, 6);
     18   first.writeUInt32BE(0x52526858, 16);
     19   first[20] = 1;
     20   const second = Buffer.alloc(20);
     21   second.writeUInt16BE(20, 6);
     22   second.writeUInt32BE(0x2252445f, 16);
     23 
     24   const partial = stream.receive(frame(0, first.subarray(0, 5)));
     25   const completed = stream.receive(frame(0, Buffer.concat([first, second.subarray(0, 10)])));
     26   const replayed = stream.receive(frame(0, first));
     27   const next = stream.receive(frame(24, second));
     28 
     29   assert.deepEqual(partial, []);
     30   assert.deepEqual(completed, [first.subarray(0, 21)]);
     31   assert.deepEqual(replayed, []);
     32   assert.deepEqual(next, [second]);
     33   assert.equal(stream.pending.length, 0);
     34 });
     35 
     36 test('stream gaps fail instead of silently losing protocol evidence', () => {
     37   const stream = new PacketStream();
     38 
     39   const receive = () => stream.receive(frame(1, Buffer.alloc(20)));
     40 
     41   assert.throws(receive, /Gap in browser stream/);
     42 });
     43 
     44 test('input tracing stays inactive until the first requested snapshot', () => {
     45   let library;
     46   const inputs = [], snapshots = [];
     47   const context = vm.createContext({addToLibrary: value => { library = value; },
     48     Module: {onTestEvent: event => inputs.push(event), onTestSnapshot: value => snapshots.push(value)},
     49     UTF8ToString: value => value, performance: {timeOrigin: 1000, now: () => 20}});
     50   vm.runInContext(fs.readFileSync(new URL('../runtime/browser_host.js', import.meta.url), 'utf8'), context);
     51   context.AOTest = library.$AOTest;
     52   context._ao_test_event = library.ao_test_event;
     53   context._ao_test_publish_main = library.ao_test_publish_main;
     54 
     55   library.ao_test_input(1, 768, 119, 1, 0, 0);
     56   const inactive = inputs.length;
     57   library.ao_test_publish('{"frame":7}');
     58   context.AOTest.frame = 8;
     59   library.ao_test_input(1, 768, 119, 1, 0, 0);
     60 
     61   assert.equal(inactive, 0);
     62   assert.equal(snapshots[0].frame, 7);
     63   assert.equal(inputs.length, 1);
     64   assert.equal(inputs[0].stage, 'consumed');
     65   assert.equal(inputs[0].frame, 8);
     66   assert.equal(inputs[0].time, 1020);
     67   assert.equal(inputs[0].key, 119);
     68   assert.equal(inputs[0].down, true);
     69 });
     70 
     71 test('readiness waits past actor construction and ignores a previous connection acknowledgement', async () => {
     72   const game = new GameTest();
     73   game.navigation = 2;
     74   game.worldStream = 'current';
     75   game.page = {waitForFunction: async () => {}};
     76   let samples = 0;
     77   game.snapshot = async () => {
     78     ++samples;
     79     if (samples === 2) game.record({kind: 'packet', direction: 'framereceived', opcode: '570c2039', navigation: 2, stream: 'previous', actor: [50000, 7]});
     80     if (samples === 3) game.record({kind: 'packet', direction: 'framereceived', opcode: '570c2039', navigation: 2, stream: 'current', actor: [50000, 8]});
     81     if (samples === 4) game.record({kind: 'packet', direction: 'framereceived', opcode: '570c2039', navigation: 2, stream: 'current', actor: [50000, 7]});
     82     return {frame: samples, localId: 7, screen: [1024, 768],
     83       actors: [{type: 50000, id: 7, inventory: []}]};
     84   };
     85 
     86   const state = await game.ready();
     87 
     88   assert.equal(samples, 4);
     89   assert.equal(state.frame, 4);
     90 });
     91 
     92 test('a stalled game releases held keys and reports the unmet condition', async () => {
     93   const game = new GameTest();
     94   const keys = [];
     95   game.page = {bringToFront: async () => {}, locator: () => ({focus: async () => {}}),
     96     keyboard: {down: async key => keys.push(['down', key]), up: async key => keys.push(['up', key])}};
     97   game.snapshot = async () => { throw Object.assign(new Error('frame stalled'), {name: 'TimeoutError'}); };
     98 
     99   const action = game.hold('KeyW', 'crossing the doorway', () => false);
    100 
    101   await assert.rejects(action, /Timed out waiting for crossing the doorway/);
    102   assert.deepEqual(keys, [['down', 'KeyW'], ['up', 'KeyW']]);
    103 });
    104 
    105 for (const action of ['key', 'click']) test(`${action} releases consumed input before reading a full snapshot`, async () => {
    106   const game = new GameTest();
    107   const calls = [];
    108   let held = false, consumed = false;
    109   const downType = action === 'key' ? 768 : 1025;
    110   const upType = action === 'key' ? 769 : 1026;
    111   game.record({kind: 'input', stage: 'consumed', type: downType});
    112   const input = {
    113     down: async () => {
    114       held = true;
    115       calls.push('down');
    116       game.record({kind: 'input', stage: 'queued', type: downType});
    117       setImmediate(() => {
    118         consumed = true;
    119         game.record({kind: 'input', stage: 'consumed', type: downType});
    120       });
    121     },
    122     up: async () => {
    123       held = false;
    124       calls.push(['up', consumed]);
    125       game.record({kind: 'input', stage: 'consumed', type: upType});
    126     },
    127   };
    128   game.page = {bringToFront: async () => {}, locator: () => ({focus: async () => {}}),
    129     keyboard: input, mouse: {...input, move: async () => {}}};
    130   game.point = async (x, y) => ({x, y});
    131   game.snapshot = async () => {
    132     assert(!held, 'A full snapshot kept the native button held and allowed autorepeat');
    133     return {frame: 1};
    134   };
    135 
    136   if (action === 'key') await game.key('KeyU');
    137   else await game.click(835, 376);
    138 
    139   assert.deepEqual(calls, ['down', ['up', true]]);
    140 });
    141 
    142 test('an engine failure still releases a short key press', async () => {
    143   const game = new GameTest();
    144   const keys = [];
    145   game.page = {bringToFront: async () => {}, locator: () => ({focus: async () => {}}),
    146     keyboard: {
    147       down: async key => {
    148         keys.push(['down', key]);
    149         setImmediate(() => game.record({kind: 'error', message: 'native client aborted'}));
    150       },
    151       up: async key => keys.push(['up', key]),
    152     }};
    153 
    154   const action = game.key('KeyU');
    155 
    156   await assert.rejects(action, /native client aborted/);
    157   assert.deepEqual(keys, [['down', 'KeyU'], ['up', 'KeyU']]);
    158 });
    159 
    160 test('walking waits for a local actor when started during the zoning gap', async () => {
    161   const game = new GameTest();
    162   game.state = {localId: 0, actors: []};
    163   game.worldStream = 'world';
    164   game.readyActors.add(7);
    165   const keys = [];
    166   let ready = 0;
    167   game.page = {bringToFront: async () => {}, locator: () => ({focus: async () => {}}),
    168     waitForFunction: async () => { ++ready; }, keyboard: {press: async key => keys.push(key)}};
    169   game.snapshot = async () => {
    170     game.state = {localId: 7, screen: [1024, 768], actors: [
    171       {type: 50000, id: 7, playfield: 3000, inventory: [], position: [60, 0, 75]},
    172     ]};
    173     return game.state;
    174   };
    175 
    176   await game.walk(60, 75);
    177 
    178   assert.equal(ready, 1);
    179   assert.deepEqual(keys, []);
    180 });
    181 
    182 test('walking stops for observed zoning before sending another movement pulse', async () => {
    183   const game = new GameTest();
    184   game.state = {localId: 7, actors: [{type: 50000, id: 7, playfield: 950}]};
    185   const keys = [];
    186   let ready = 0;
    187   game.page = {bringToFront: async () => {}, locator: () => ({focus: async () => {}}),
    188     keyboard: {press: async key => keys.push(key)}};
    189   game.snapshot = async () => { game.state.actors[0].playfield = 3000; return game.state; };
    190   game.ready = async () => { ++ready; };
    191 
    192   await game.walk(60, 75);
    193 
    194   assert.equal(ready, 1);
    195   assert.deepEqual(keys, []);
    196 });
    197 
    198 test('client errors still fail waits after ordinary traffic fills the trace', async () => {
    199   const game = new GameTest();
    200   game.snapshot = async () => ({frame: 1});
    201   game.record({kind: 'error', message: 'native client aborted'});
    202   for (let i = 0; i < 2048; ++i) game.record({kind: 'position', position: [1, 2, 3]});
    203 
    204   const action = game.wait('an otherwise satisfied condition', () => true);
    205 
    206   await assert.rejects(action, /native client aborted/);
    207   assert.equal(game.dropped, 1);
    208   assert.equal(game.events.some(event => event.kind === 'error'), false);
    209   assert.equal(game.errors[0].message, 'native client aborted');
    210 });
    211 
    212 test('picking follows a moving native projection between probes', async () => {
    213   const game = new GameTest();
    214   const moves = [];
    215   game.page = {evaluate: async () => true, mouse: {move: async (x, y) => { moves.push([x, y]); }}};
    216   game.point = async (x, y) => ({x, y});
    217   let frame = 0;
    218   game.snapshot = async () => {
    219     const hover = moves.at(-1)?.[0] === 199 ? [50000, 7] : [0, 0];
    220     return {frame: ++frame, screen: [1024, 768],
    221       actors: [{type: 50000, id: 7, screenPosition: [frame === 1 ? 100 : 200, 100, 0.5]}],
    222       hover, pick: {sequence: moves.length, position: moves.at(-1), identity: hover}};
    223   };
    224 
    225   const point = await game.pick(50000, 7);
    226 
    227   assert.deepEqual(point, [199, 100]);
    228   assert.deepEqual(moves, [[100, 100], [199, 100]]);
    229 });
    230 
    231 test('picking waits for the current pixel instead of accepting cached hover', async () => {
    232   const game = new GameTest();
    233   const moves = [];
    234   let pending = 0, frame = 0;
    235   let pick = {sequence: 0, position: [42, 42], identity: [50000, 7]};
    236   let hover = pick.identity;
    237   game.page = {evaluate: async () => true, mouse: {move: async (x, y) => { moves.push([x, y]); pending = 3; }}};
    238   game.point = async (x, y) => ({x, y});
    239   game.snapshot = async () => {
    240     ++frame;
    241     if (pending) {
    242       if (!--pending) pick = {sequence: moves.length, position: moves.at(-1),
    243         identity: moves.at(-1)[0] === 99 ? [50000, 7] : [51005, 75]};
    244     } else hover = pick.identity;
    245     return {frame, pick, hover, screen: [1024, 768],
    246       actors: [{type: 50000, id: 7, screenPosition: [100, 100, 0.5]}]};
    247   };
    248 
    249   const point = await game.pick(50000, 7);
    250 
    251   assert.deepEqual(point, [99, 100]);
    252   assert.deepEqual(moves, [[100, 100], [99, 100]]);
    253   assert.equal(pick.sequence, 2);
    254 });
    255 
    256 test('picking skips pixels outside the canvas before waiting for native selection', async () => {
    257   const game = new GameTest();
    258   const moves = [];
    259   game.page = {evaluate: async () => true, mouse: {move: async (x, y) => {
    260     assert(y < 768, 'An offscreen pointer cannot reach the native picker');
    261     moves.push([x, y]);
    262   }}};
    263   game.point = async (x, y) => ({x, y});
    264   game.snapshot = async () => ({screen: [1024, 768], hover: [50000, 7],
    265     pick: {sequence: moves.length, position: moves.at(-1), identity: [50000, 7]},
    266     actors: [{type: 50000, id: 7, screenPosition: [100, 770, 0.5]}]});
    267 
    268   const point = await game.pick(50000, 7);
    269 
    270   assert.deepEqual(point, [100, 762]);
    271   assert.deepEqual(moves, [[100, 762]]);
    272 });
    273 
    274 test('picking finds visible mesh above a GUI-covered origin before the model leaves view', async () => {
    275   const game = new GameTest();
    276   const moves = [];
    277   let observations = 0;
    278   game.page = {
    279     evaluate: async (callback, {y}) => y < 594,
    280     mouse: {move: async (x, y) => {
    281       assert(y < 594, 'Transparent chat consumes this pointer without a world query');
    282       moves.push([x, y]);
    283     }},
    284   };
    285   game.point = async (x, y) => ({x, y});
    286   game.snapshot = async () => {
    287     ++observations;
    288     const hover = moves.at(-1)?.[1] === 553 ? [50000, 7] : [0, 0];
    289     return {screen: [1024, 768], hover,
    290       pick: {sequence: moves.length, position: moves.at(-1), identity: hover},
    291       actors: [{type: 50000, id: 7, screenPosition: observations < 20 ? [100, 673, 0.5] : null}]};
    292   };
    293 
    294   const point = await game.pick(50000, 7);
    295 
    296   assert.deepEqual(point, [100, 553]);
    297   assert.deepEqual(moves, [[100, 593], [99, 593], [101, 593], [92, 593],
    298     [108, 593], [80, 593], [120, 593], [100, 553]]);
    299 });