tools/client/test_game_errors.mjs (2659B)
1 import assert from 'node:assert/strict'; 2 import {once} from 'node:events'; 3 import fs from 'node:fs/promises'; 4 import os from 'node:os'; 5 import path from 'node:path'; 6 import {createRequire} from 'node:module'; 7 import {test} from 'node:test'; 8 import {GameTest} from './game_test.mjs'; 9 10 const {chromium} = createRequire(import.meta.url)('playwright'); 11 12 test('browser traces retain stacks for Error objects and primitive unwind throws', {timeout: 10000}, async () => { 13 const output = await fs.mkdtemp(path.join(os.tmpdir(), 'ao-game-errors-')); 14 const browser = await chromium.launch({executablePath: '/usr/bin/chromium', 15 args: ['--no-sandbox', '--disable-dev-shm-usage']}); 16 try { 17 const page = await browser.newPage(); 18 const game = await GameTest.attach(page); 19 await page.route('http://error-capture.test/**', route => route.fulfill( 20 route.request().url().endsWith('/failure.js') ? { 21 contentType: 'application/javascript', body: ` 22 function failDuringLiveEdit() { throw new Error('live edit diagnostic'); } 23 function unwindDuringLiveEdit() { throw 'unwind'; } 24 function reloadPlayfield(fail) { fail(); } 25 document.querySelector('#error').onclick = () => setTimeout(() => reloadPlayfield(failDuringLiveEdit), 0); 26 document.querySelector('#unwind').onclick = () => setTimeout(() => reloadPlayfield(unwindDuringLiveEdit), 0); 27 `, 28 } : {contentType: 'text/html', body: '<button id="error">Error</button><button id="unwind">Unwind</button><script src="/failure.js"></script>'})); 29 30 await page.goto('http://error-capture.test/'); 31 for (const id of ['error', 'unwind']) { 32 const captured = once(game.inputEvents, 'input', {signal: AbortSignal.timeout(5000)}); 33 await page.locator('#' + id).click(); 34 await captured; 35 } 36 await game.save(output); 37 const trace = JSON.parse(await fs.readFile(path.join(output, 'trace.json'), 'utf8')); 38 39 assert.deepEqual(trace.errors.map(error => error.message), ['Error: live edit diagnostic', 'unwind']); 40 assert.deepEqual(trace.errors, game.errors); 41 for (const [index, name] of ['failDuringLiveEdit', 'unwindDuringLiveEdit'].entries()) { 42 const frames = trace.errors[index].details.stackTrace.callFrames; 43 assert.equal(frames[0].functionName, name); 44 assert.equal(frames[1].functionName, 'reloadPlayfield'); 45 assert.equal(frames[0].url, 'http://error-capture.test/failure.js'); 46 assert.equal(frames[0].lineNumber, index + 1); 47 assert.equal(trace.errors[index].navigation, 1); 48 } 49 } finally { 50 await browser.close(); 51 await fs.rm(output, {recursive: true}); 52 } 53 });