aorecomp

Anarchy Online classic in the browser

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


client/tests/browser_startup_prefetch.mjs (3426B)


      1 import assert from 'node:assert/strict';
      2 import fs from 'node:fs';
      3 import vm from 'node:vm';
      4 import {test} from 'node:test';
      5 
      6 test('startup files overlap, whole archives share demand reads, and failures retry', async () => {
      7   const names = ['Prefs/Main.cfg', 'cd_image/OCC/OCCV.i',
      8     'cd_image/data/db/ResourceDatabase.idx', 'cd_image/textures/Archives/GUIGFX.UVGA',
      9     'cd_image/data/db/ResourceDatabase.dat', 'cd_image/sound/music/unrelated.wav'];
     10   const requests = [], pending = new Map();
     11   let library;
     12   const context = vm.createContext({
     13     AOProfile: {begin: () => 0, end: () => {}},
     14     Map, Set, URL, Response, Uint8Array, TextDecoder, AbortController, TypeError,
     15     setTimeout, clearTimeout, console, HEAPU8: new Uint8Array(64),
     16     UTF8ToString: value => value,
     17     __wasmfs_fetch_get_file_url: handle => 'http://example.test/browser-assets//' + names[handle],
     18     __wasmfs_fetch_get_chunk_size: () => 4,
     19     wasmFS$backends: {}, AOResourcePrefetch: {}, _browser_prefetch_resource_ready: () => {},
     20     addToLibrary: value => { library = value; },
     21     fetch: async (url, options) => {
     22       if (url.endsWith('/browser-manifest')) return Response.json(Object.fromEntries(
     23         names.map(name => [name, {size: 12, version: '"one"'}])));
     24       const name = url.split('/browser-assets/')[1];
     25       requests.push([name, options.headers.Range]);
     26       const status = await new Promise(resolve => pending.set(name, resolve));
     27       pending.delete(name);
     28       return new Response(status === 206 ? 'abcdefghijkl' : null, {status,
     29         headers: {'Content-Range': 'bytes 0-11/12', ETag: '"one"'}});
     30     }
     31   });
     32   vm.runInContext(fs.readFileSync(new URL('../runtime/browser_host.js', import.meta.url), 'utf8'), context);
     33   context.AOAssetRequest = library.$AOAssetRequest;
     34   const source = fs.readFileSync(new URL('../runtime/browser_fetch.js', import.meta.url), 'utf8')
     35     .replaceAll('{{{ cDefs.EROFS }}}', '69').replaceAll('{{{ cDefs.ENOENT }}}', '44')
     36     .replaceAll('{{{ cDefs.EBADF }}}', '8');
     37   vm.runInContext(source, context);
     38   await library._wasmfs_create_fetch_backend_js(1);
     39   const backend = context.wasmFS$backends[1];
     40   for (let handle = 0; handle < names.length; ++handle) await backend.allocFile(handle);
     41 
     42   const preferences = backend.read(0, 0, 12, 0);
     43   await new Promise(setImmediate);
     44   const index = backend.read(2, 16, 4, 8);
     45   await new Promise(setImmediate);
     46 
     47   assert.deepEqual(requests, [
     48     ['Prefs/Main.cfg', 'bytes=0-11'],
     49     ['cd_image/OCC/OCCV.i', 'bytes=0-11'],
     50     ['cd_image/data/db/ResourceDatabase.idx', 'bytes=0-11'],
     51     ['cd_image/textures/Archives/GUIGFX.UVGA', 'bytes=0-11']
     52   ]);
     53   pending.get(names[3])(404);
     54   for (const name of names.slice(0, 3)) pending.get(name)(206);
     55   assert.equal(await preferences, 12);
     56   assert.equal(await index, 4);
     57   assert.equal(new TextDecoder().decode(context.HEAPU8.subarray(0, 12)), 'abcdefghijkl');
     58   assert.equal(new TextDecoder().decode(context.HEAPU8.subarray(16, 20)), 'ijkl');
     59 
     60   // Failed speculation leaves the ordinary read free to retry the same archive.
     61   const retry = backend.read(3, 32, 12, 0);
     62   await new Promise(setImmediate);
     63   assert.equal(requests.length, 5);
     64   assert.deepEqual(requests[4], ['cd_image/textures/Archives/GUIGFX.UVGA', 'bytes=0-11']);
     65   pending.get(names[3])(206);
     66   assert.equal(await retry, 12);
     67   assert.equal(new TextDecoder().decode(context.HEAPU8.subarray(32, 44)), 'abcdefghijkl');
     68 });