Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions audio.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,16 @@
names.slice(1).forEach(function (key) {
if (!assets[key] || buffers[key] || loading[key]) return;
loading[key] = true;
// Every exit clears the flag, so a failed load can be tried again; while
// it is set, parallel calls skip the effect instead of racing it.
var settle = function () { delete loading[key]; };
var xhr = new XMLHttpRequest();
xhr.open('GET', assets[key], true); xhr.responseType = 'arraybuffer'; xhr.timeout = 8000;
xhr.onload = function () {
if ((xhr.status === 0 || xhr.status === 200) && xhr.response) decode(key, xhr.response);
else errors[key] = 'Sound file unavailable.';
if ((xhr.status === 0 || xhr.status === 200) && xhr.response) decode(key, xhr.response, settle);
else { errors[key] = 'Sound file unavailable.'; settle(); }
};
xhr.onerror = xhr.ontimeout = function () { errors[key] = 'Unable to load sound effect.'; };
xhr.onerror = xhr.ontimeout = function () { errors[key] = 'Unable to load sound effect.'; settle(); };
xhr.send();
});
}
Expand Down
90 changes: 90 additions & 0 deletions tests/audio.test.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
'use strict';
const test = require('node:test');
const assert = require('node:assert/strict');
const fs = require('node:fs');
const path = require('node:path');
const vm = require('node:vm');

const ASSETS = {
menu: 'assets/audio/menu.mp3', hover: 'assets/audio/hover.mp3',
select: 'assets/audio/select.mp3', back: 'assets/audio/back.mp3',
page: 'assets/audio/page.mp3', start: 'assets/audio/start.mp3'
};

/**
* A sandbox whose XHRs record themselves: each instance the page creates is
* pushed onto `requests`, and `xhr.respond()` drives its callbacks.
* `decode` is the AudioContext.decodeAudioData the run ends in: 'ok' decodes a
* 1s buffer, anything else reports an unsupported format.
*/
function environment(decode) {
const requests = [];
function XMLHttpRequest() {
requests.push(this);
this.onload = this.onerror = this.ontimeout = null;
}
XMLHttpRequest.prototype.open = function () {};
XMLHttpRequest.prototype.send = function () {};
function Audio() {
return {loop: false, preload: '', volume: 1, play() { return {catch() {}}; }, pause() {}};
}
function AudioContext() {
this.state = 'running';
this.resume = () => ({catch() {}});
this.createGain = () => ({connect() {}, gain: {value: 1}});
this.decodeAudioData = (data, ok, fail) => {
if (decode === 'ok') ok({duration: 1});
else fail();
};
}
const window = {
localStorage: {getItem: () => null, setItem() {}},
WII_AUDIO_ASSETS: ASSETS,
AudioContext,
addEventListener() {},
XMLHttpRequest,
URL: {createObjectURL: () => 'blob:x', revokeObjectURL() {}}
};
vm.runInNewContext(fs.readFileSync(path.join(__dirname, '..', 'audio.js'), 'utf8'),
{window, document: {addEventListener() {}, hidden: false}, Audio, XMLHttpRequest, localStorage: window.localStorage,
URL: window.URL, JSON, Object, Array, String, Math, RegExp, Number});
return {window, requests};
}

test('a failed load can be retried', () => {
const {window, requests} = environment('fail');
window.WiiAudio.unlock();
assert.equal(requests.length, 5);
requests.forEach(xhr => xhr.onerror());
window.WiiAudio.unlock();
assert.equal(requests.length, 10);
});

test('a decoded effect is cached and not requested again', () => {
const {window, requests} = environment('ok');
window.WiiAudio.unlock();
assert.equal(requests.length, 5);
requests.forEach(xhr => {
xhr.status = 200; xhr.response = 'audio bytes'; xhr.onload();
});
window.WiiAudio.unlock();
assert.equal(requests.length, 5);
});

test('a decode failure leaves the effect retryable', () => {
const {window, requests} = environment('fail');
window.WiiAudio.unlock();
requests.forEach(xhr => {
xhr.status = 200; xhr.response = 'audio bytes'; xhr.onload();
});
window.WiiAudio.unlock();
assert.equal(requests.length, 10);
});

test('parallel unlocks never duplicate an in-flight request', () => {
const {window, requests} = environment('ok');
window.WiiAudio.unlock();
window.WiiAudio.unlock();
window.WiiAudio.unlock();
assert.equal(requests.length, 5);
});