tests/e2e/startup.test.mjs (7149B)
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 {GameTest} from '../../tools/client/game_test.mjs'; 6 7 before(buildServer); 8 9 for (const latency of [0, 120]) { 10 test(`cold and cached startup with ${latency} ms asset latency`, {timeout: 180_000}, async () => { 11 await withFixture('startup-' + latency, async fixture => { 12 const context = await fixture.visitor(); 13 const account = await context.request.post(fixture.url + '/browser-account/create', {form: { 14 name: 'Startupbench', password: 'startup regression password', head_mesh: '40681', 15 profession: '9', side: '2', build: '1', height: '100' 16 }}); 17 assert.equal(account.status(), 204, await account.text()); 18 await fixture.setup(context, 'position', { 19 playfield: 705, position: [960, 16.5, 510], rotation: [0, 0, 0, 1] 20 }); 21 let requests = []; 22 await context.route(/\/browser-(assets|manifest|resource-index|startup)|\/wasm\/game\.wasm$/, async route => { 23 const request = route.request(); 24 const entry = {path: new URL(request.url()).pathname, method: request.method(), 25 range: request.headers().range, time: Date.now()}; 26 requests.push(entry); 27 if (latency) await new Promise(resolve => setTimeout(resolve, latency)); 28 await route.continue(); 29 if (entry.path === '/wasm/game.wasm') entry.status = (await request.response()).status(); 30 if (entry.path === '/browser-startup') { 31 const response = await request.response(); 32 const headers = await response.allHeaders(); 33 entry.chunks = headers['x-ao-chunks'].split(',').map(Number); 34 entry.bytes = Number(headers['content-length']); 35 entry.version = headers['x-ao-asset-version']; 36 } 37 }); 38 const page = await context.newPage(); 39 await page.addInitScript(() => { 40 window.startupDownloadProgress = []; 41 document.addEventListener('DOMContentLoaded', () => { 42 const bar = document.querySelector('#download-progress'); 43 new MutationObserver(() => window.startupDownloadProgress.push({ 44 percent: Number(bar.getAttribute('aria-valuenow')), hidden: bar.hidden 45 })).observe(bar, {attributes: true, attributeFilter: ['aria-valuenow', 'hidden']}); 46 }); 47 }); 48 const game = await GameTest.attach(page); 49 fixture.games.push(game); 50 const results = []; 51 52 for (const cache of ['cold', 'warm', 'evicted']) { 53 if (cache === 'evicted') { 54 await page.evaluate(async () => { 55 const cache = await caches.open('ao-asset-chunks-v1'); 56 const url = new URL('browser-assets/cd_image/data/db/ResourceDatabase.dat', document.baseURI); 57 url.searchParams.set('ao-chunk', '1048576:7'); 58 if (!await cache.delete(url.href)) throw new Error('Expected startup chunk in cache'); 59 }); 60 } 61 requests = []; 62 const start = Date.now(); 63 await page.goto(fixture.url); 64 await game.ready(); 65 const ready = Date.now() - start; 66 const position = game.local().position; 67 await game.hold('KeyS', 'startup accepts movement', state => 68 game.local(state).position[2] < position[2] - 0.5); 69 await game.waitFor('server accepts startup movement', async () => 70 (await fixture.state(context)).position[2] < position[2] - 0.4); 71 const playable = Date.now() - start; 72 const preload = await page.evaluate(() => { 73 const bundles = performance.getEntriesByType('resource').filter(entry => entry.name.endsWith('/browser-startup')); 74 const script = performance.getEntriesByType('resource').find(entry => entry.name.endsWith('/wasm/game.js')); 75 return {start: bundles[0].startTime, end: bundles.at(-1).responseEnd, 76 bytes: bundles.reduce((sum, entry) => sum + entry.decodedBodySize, 0), 77 clientStart: script.startTime}; 78 }); 79 assert.ok(preload.end <= preload.clientStart, 'bundle must finish before game code downloads'); 80 const bundles = requests.filter(request => request.path === '/browser-startup'); 81 assert.deepEqual(bundles.map(request => request.method), cache === 'warm' ? ['HEAD'] : ['HEAD', 'GET']); 82 assert.equal(bundles[0].chunks.length, 185); 83 assert.equal(bundles[0].bytes, 193986560); 84 assert.equal(preload.bytes, cache === 'warm' ? 0 : 193986560); 85 const binaries = requests.filter(request => request.path === '/wasm/game.wasm'); 86 assert.deepEqual(binaries.map(request => [request.method, request.status]), 87 cache === 'cold' ? [['GET', 200]] : [['HEAD', 304]]); 88 const database = requests.filter(request => request.path.endsWith('/ResourceDatabase.dat')); 89 const requestedChunks = database.flatMap(request => { 90 const [first, last] = request.range.match(/\d+/g).map(Number); 91 return Array.from({length: Math.floor(last / 1048576) - first / 1048576 + 1}, 92 (_, i) => first / 1048576 + i); 93 }); 94 assert.deepEqual(requestedChunks.filter(index => bundles[0].chunks.includes(index)), [], 95 'preloaded database chunks must not be downloaded again'); 96 const downloadProgress = await page.evaluate(() => window.startupDownloadProgress); 97 if (cache === 'warm') { 98 assert.ok(downloadProgress.every(sample => sample.hidden), '304 must not show download progress'); 99 } else { 100 assert.ok(downloadProgress.length > 1); 101 assert.deepEqual(downloadProgress.at(-1), {percent: 100, hidden: true}); 102 assert.ok(downloadProgress.every((sample, i) => (!sample.hidden || sample.percent === 100) && 103 (i === 0 || sample.percent >= downloadProgress[i - 1].percent)), 104 'download bar must advance continuously through assets and game binary'); 105 } 106 results.push({cache, ready, playable, preload, requestedChunks, requests, downloadProgress}); 107 await game.save(fixture.output + '/' + cache); 108 } 109 110 assert.deepEqual(game.errors, []); 111 const indexPath = '/browser-assets/cd_image/data/db/ResourceDatabase.idx'; 112 assert.deepEqual(results[0].requests.filter(request => request.path === indexPath) 113 .map(request => request.range), ['bytes=0-7340031']); 114 assert.equal(results[1].requests.filter(request => request.path === indexPath).length, 0); 115 assert.equal(results[1].requests.filter(request => request.path.startsWith('/browser-assets/Prefs/') || 116 request.path.startsWith('/browser-assets/cd_image/OCC/')).length, 0); 117 await fs.writeFile(fixture.output + '/startup.json', JSON.stringify({latency, results}, null, 2)); 118 console.log(JSON.stringify({latency, output: fixture.output, results: results.map(result => ({ 119 cache: result.cache, ready: result.ready, playable: result.playable, requests: result.requests.length, 120 preload: result.preload, requestedChunks: result.requestedChunks 121 }))})); 122 }); 123 }); 124 }