aorecomp

Anarchy Online classic in the browser

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


tests/e2e/audio.test.mjs (3529B)


      1 import assert from 'node:assert/strict';
      2 import {before, test} from 'node:test';
      3 import {buildServer, withFixture} from './fixture.mjs';
      4 import {GameTest} from '../../tools/client/game_test.mjs';
      5 
      6 before(buildServer);
      7 
      8 test('first touch release unlocks audio after an early resume rejection', {timeout: 120_000}, async () => {
      9   await withFixture('audio-touch', async fixture => {
     10     const context = await fixture.browser.newContext({viewport: {width: 390, height: 844},
     11       isMobile: true, hasTouch: true,
     12       userAgent: 'Mozilla/5.0 (Android 17; Mobile; rv:159.0) Gecko/159.0 Firefox/159.0'});
     13     assert.equal((await context.request.post(fixture.url + '/browser-launch')).status(), 200);
     14     const batches = [];
     15     await context.route('**/admin/browser-profile/*', async route => {
     16       batches.push(route.request().postDataJSON());
     17       await route.fulfill({status: 204});
     18     });
     19     const page = await context.newPage();
     20     const game = await GameTest.attach(page);
     21     fixture.games.push(game);
     22     const input = await context.newCDPSession(page);
     23     const read = async expression => (await input.send('Runtime.evaluate', {expression, returnByValue: true})).result.value;
     24 
     25     // Require an already-activated gesture instead of queuing a premature resume.
     26     await page.addInitScript(() => {
     27       window.audioResumes = [];
     28       const resume = AudioContext.prototype.resume;
     29       AudioContext.prototype.resume = function() {
     30         const active = navigator.userActivation.isActive;
     31         window.audioResumes.push({type: window.event?.type, active});
     32         if (!active) return Promise.reject(new DOMException('Touch has not ended', 'NotAllowedError'));
     33         return resume.call(this);
     34       };
     35     });
     36     await page.goto(fixture.url + '?profile=1');
     37     await game.waitFor('audio context initialized', () => read("typeof Module !== 'undefined' && !!Module.audioContext"));
     38     await game.waitFor('first game frame', () => read("typeof gameReady !== 'undefined' && gameReady"));
     39     assert.equal(await read('Module.audioContext.state'), 'suspended');
     40     assert.equal(await read('navigator.userActivation.hasBeenActive'), false);
     41 
     42     // CDP reads do not grant activation; only these real browser touch events do.
     43     await input.send('Input.dispatchTouchEvent', {type: 'touchStart', touchPoints: [{x: 195, y: 200}]});
     44     assert.equal(await read('Module.audioContext.state'), 'suspended');
     45     assert.equal(await read('audioResumes.some(attempt => !attempt.active)'), true);
     46     await input.send('Input.dispatchTouchEvent', {type: 'touchEnd', touchPoints: []});
     47     await game.waitFor('audio running after finger release', () => read("Module.audioContext.state === 'running'"));
     48     assert.equal(await read('audioResumes.some(attempt => attempt.active)'), true);
     49     const clock = await read('Module.audioContext.currentTime');
     50     await game.waitFor('audio clock advancing', async () => await read('Module.audioContext.currentTime') > clock);
     51     await game.ready();
     52     await game.waitFor('audio state profile uploaded', () => batches.some(batch =>
     53       batch.traceEvents.some(event => event.name === 'audio context state' && event.args.state === 'running')));
     54     const states = batches.flatMap(batch => batch.traceEvents)
     55       .filter(event => event.name === 'audio context state').map(event => event.args.state);
     56     assert.deepEqual(states, ['suspended', 'running']);
     57     assert.equal(batches.reduce((sum, batch) => sum + batch.dropped, 0), 0);
     58     assert.deepEqual(game.errors, []);
     59   });
     60 });