aorecomp

Anarchy Online classic in the browser

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


client/tests/browser_prefetch.mjs (4838B)


      1 import assert from 'node:assert/strict';
      2 import fs from 'node:fs';
      3 import vm from 'node:vm';
      4 
      5 const names = new Map([[1,'boots_sand_01.wav'],[2,'boots_sand_02.wav'],[3,'boots_sand_03.wav'],
      6   [4,'boots_sand_04.wav'],[5,'boots_sand_05.wav'],[6,'boots_con_01.wav']]);
      7 const requests = [], pending = [], timers = [];
      8 let library;
      9 const context = vm.createContext({
     10     AOProfile: {begin: () => 0, end: () => {}},Map, Set, URL, Response, Uint8Array, console,
     11   AbortController, TypeError, TextDecoder, clearTimeout,
     12   HEAPU8:new Uint8Array(64), UTF8ToString: value => value,
     13   __wasmfs_fetch_get_file_url: handle => {
     14     assert(names.has(handle), 'Must not inspect a freed file');
     15     return 'http://example.test/browser-assets//cd_image/sound/sfx/foots/' + names.get(handle);
     16   },
     17   __wasmfs_fetch_get_chunk_size: () => 1048576,
     18   wasmFS$backends:{}, AOResourcePrefetch:{}, _browser_prefetch_resource_ready:() => {},
     19   addToLibrary:value => { library = value; },
     20   setTimeout:(callback, delay) => delay === undefined ? timers.push(callback) : setTimeout(callback, delay),
     21   fetch:url => url === 'http://example.test/browser-manifest'
     22     ? Promise.resolve(Response.json(Object.fromEntries([...names.values()].map(name =>
     23       ['cd_image/sound/sfx/foots/' + name, {size:4, version:'"one"'}]))))
     24     : new Promise(resolve => {
     25     requests.push(url.split('/').at(-1));
     26     pending.push(status => resolve(new Response(status === 206 ? 'abcd' : null,
     27       {status,headers:{'Content-Range':'bytes 0-3/4',ETag:'"one"'}})));
     28   })
     29 });
     30 vm.runInContext(fs.readFileSync(new URL('../runtime/browser_host.js', import.meta.url), 'utf8'), context);
     31 context.AOAssetRequest = library.$AOAssetRequest;
     32 let source = fs.readFileSync(new URL('../runtime/browser_fetch.js', import.meta.url),'utf8');
     33 for (const [name,value] of Object.entries({EROFS:69,ENOENT:44,EBADF:8}))
     34   source = source.replaceAll('{{{ cDefs.' + name + ' }}}',String(value));
     35 vm.runInContext(source,context);
     36 await library._wasmfs_create_fetch_backend_js(1);
     37 const backend = context.wasmFS$backends[1];
     38 for (const handle of names.keys()) await backend.allocFile(handle);
     39 const settle = () => new Promise(setImmediate);
     40 
     41 // Fetch one sound, then run its deferred family scan.
     42 assert.equal(await backend.getSize(1),4);
     43 assert.deepEqual(requests,[]);
     44 assert.equal(timers.length,0);
     45 const first = backend.read(1,0,4,0);
     46 await settle();
     47 pending[0](206);
     48 assert.equal(await first,4);
     49 assert.equal(timers.length,1);
     50 timers.shift()();
     51 await settle();
     52 assert.deepEqual(requests,['boots_sand_01.wav','boots_sand_02.wav','boots_sand_03.wav']);
     53 
     54 // Foreground reads join pending prefetch; queued freed files are skipped.
     55 await backend.freeFile(4);
     56 names.delete(4);
     57 const foreground = backend.read(2,0,4,0);
     58 await settle();
     59 assert.equal(requests.length,3);
     60 pending[1](206);
     61 assert.equal(await foreground,4);
     62 assert.equal(new TextDecoder().decode(context.HEAPU8.subarray(0,4)),'abcd');
     63 await settle();
     64 assert.equal(requests.at(-1),'boots_sand_05.wav');
     65 
     66 // A failed speculative fetch is retried on demand, without repeating the scan.
     67 pending[2](404);
     68 pending[3](206);
     69 await settle();
     70 const retried = backend.read(3,0,4,0);
     71 await settle();
     72 assert.equal(requests.at(-1),'boots_sand_03.wav');
     73 pending[4](206);
     74 assert.equal(await retried,4);
     75 assert.equal(requests.length,5);
     76 assert.equal(timers.length,0);
     77 assert.equal(requests.includes('boots_con_01.wav'), false);
     78 console.log('PASS: bounded prefetch, shared foreground request, freed-file skip and retry');
     79 
     80 
     81 // Surface and action changes retain the same footwear, but other footwear stays unloaded.
     82 names.clear();
     83 for (const [handle,name] of [[1,'boots/boots_con_01.wav'],[2,'boots/boots_con_02.wav'],
     84   [3,'boots/boots_dirt_01.wav'],[4,'boots/boots_grassdry_01.wav'],
     85   [5,'boots/boots_con_jump.wav'],[6,'heavy/heavy_con_01.wav']]) names.set(handle,name);
     86 requests.length = pending.length = 0;
     87 await library._wasmfs_create_fetch_backend_js(2);
     88 const footwear = context.wasmFS$backends[2];
     89 for (const handle of names.keys()) await footwear.allocFile(handle);
     90 
     91 const concrete = footwear.read(1,0,4,0);
     92 await settle();
     93 pending[0](206);
     94 assert.equal(await concrete,4);
     95 timers.shift()();
     96 await settle();
     97 assert.deepEqual(requests,['boots_con_01.wav','boots_con_02.wav','boots_dirt_01.wav']);
     98 pending[1](206); pending[2](206);
     99 await settle();
    100 assert.deepEqual(requests,['boots_con_01.wav','boots_con_02.wav','boots_dirt_01.wav',
    101   'boots_grassdry_01.wav','boots_con_jump.wav']);
    102 pending[3](206); pending[4](206);
    103 await settle();
    104 const grass = await footwear.read(4,0,4,0);
    105 const jump = await footwear.read(5,4,4,0);
    106 
    107 assert.equal(grass,4);
    108 assert.equal(jump,4);
    109 assert.equal(requests.length,5);
    110 assert.equal(timers.length,0);
    111 assert.equal(requests.includes('heavy_con_01.wav'),false);
    112 console.log('PASS: terrain and action variants reuse footwear prefetch');