Skip to content

Commit

Permalink
add more tests and set up .test.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
timothyhale committed Feb 27, 2024
1 parent 7f20b22 commit a1a6649
Show file tree
Hide file tree
Showing 7 changed files with 147 additions and 14 deletions.
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@
"scripts": {
"build": "tsc",
"test": "npm run build && web-test-runner",
"test:watch": "npm run build && wtr --watch",
"prepublishOnly": "npm run clean && npm run build",
"pretty": "prettier --config ./prettierrc.json --write \"example/js/*.js\" \"src/**/*.ts\" \"test/**/*.js\"",
"pretty": "prettier --config ./prettierrc.json --write \"example/js/*.js\" \"src/**/*.ts\" \"test/**/*.ts\"",
"clean": "rimraf dist"
},
"repository": {
Expand All @@ -43,11 +44,10 @@
},
"devDependencies": {
"@esm-bundle/chai": "^4.3.4-fix.0",
"@web/dev-server-esbuild": "^1.0.2",
"@web/test-runner": "^0.15.3",
"prettier": "3.0.0",
"rimraf": "^5.0.1",
"typescript": "^5.1.3"
},
"dependencies": {
}
}
68 changes: 68 additions & 0 deletions test/audio-manager.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// audio-manager.spec.ts

import {expect} from '@esm-bundle/chai';
import {AudioManager} from '../dist';
import {_audioContext} from '../dist/audio-listener';
import {expectSuccess} from './chai/promise';

/* Audio sources */
const SRC_WELCOME = 'test/resources/welcome.wav'; // Adjust the source path as needed

let audioManager: AudioManager;

describe('AudioManager', () => {

beforeEach(() => {
audioManager = new AudioManager();
});

it('should be able to create nodes, add and remove files', async () => {
const playableNode = await audioManager.load(SRC_WELCOME);

await audioManager._add(SRC_WELCOME);

expect(audioManager['_bufferCache'].has(SRC_WELCOME)).to.be.true;
expect(playableNode).to.exist;
audioManager._remove(SRC_WELCOME);
/* There should be two references, so first remove should still contain the file */
expect(audioManager['_bufferCache'].has(SRC_WELCOME)).to.be.true;
audioManager._remove(SRC_WELCOME);
expect(audioManager['_bufferCache'].has(SRC_WELCOME)).to.be.false;
});

it('should be able to maintain multiple references to the same file', async () => {
const p1 = await audioManager.load(SRC_WELCOME);
expect(audioManager['_bufferCache'].size).to.equal(1);
expect(audioManager['_bufferCache'].get(SRC_WELCOME)[1]).to.equal(1);
const p2 = await audioManager.load(SRC_WELCOME);
const p3 = await audioManager.load(SRC_WELCOME);
const p4 = await audioManager.load(SRC_WELCOME);

expect(audioManager['_bufferCache'].size).to.equal(1);
expect(audioManager['_bufferCache'].get(SRC_WELCOME)[1]).to.equal(4);
expect(audioManager['_bufferCache'].get(SRC_WELCOME)[0]).to.instanceOf(AudioBuffer);

expect(audioManager['_bufferCache'].has(SRC_WELCOME)).to.be.true;
p1.destroy();
expect(audioManager['_bufferCache'].has(SRC_WELCOME)).to.be.true;
p3.destroy();
expect(audioManager['_bufferCache'].has(SRC_WELCOME)).to.be.true;
p2.destroy();
expect(audioManager['_bufferCache'].has(SRC_WELCOME)).to.be.true;
p4.destroy();
expect(audioManager['_bufferCache'].has(SRC_WELCOME)).to.be.false;
});
});

describe('PlayableNode', () => {
it('should be able to play sound', async () => {
const audio = await audioManager.load(SRC_WELCOME);

expectSuccess(audio.play()).then(() => {
expect(audio.isPlaying).to.be.true;
expect(_audioContext.state).to.equal('running');
audio.stop();
expect(audio['_isPlaying']).to.be.false;
});
});
});
16 changes: 7 additions & 9 deletions test/audio-source.test.js → test/audio-source.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {expect} from '@esm-bundle/chai';

import {init} from './setup.js';
import {AudioSource} from '../dist/index.js';
import {AudioSource} from '../dist';

before(init);

Expand All @@ -20,7 +20,7 @@ describe('AudioSource', function () {
'isStationary',
'loop',
'maxDistance',
'maxVolume',
'volume',
'refDistance',
'rolloffFactor',
'spatial',
Expand All @@ -32,14 +32,12 @@ describe('AudioSource', function () {
const audioObject = WL.scene.addObject();
audioObject.name = 'o';

let source = audioObject.addComponent(AudioSource, {});

source = audioObject.addComponent(AudioSource, {
src: 'audio-files/welcome.wav',
const audio = audioObject.addComponent(AudioSource, {
src: 'test/resources/welcome.wav',
});

expect(source).to.not.be.null;
expect(source.src).to.equal('audio-files/welcome.wav');
expect(source.maxVolume).to.equal(1.0);
expect(audio).to.not.be.null;
expect(audio.src).to.equal('test/resources/welcome.wav');
expect(audio.volume).to.equal(1.0);
});
});
47 changes: 47 additions & 0 deletions test/chai/promise.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import {expect} from '@esm-bundle/chai';

/**
* Expects the promise to fulfill.
*
* @param {Promise} promise The promise to check.
* @param {number} timeout Maximum amount of time it can take to fulfill, in ms.
* @returns The promise result.
*/
export function expectSuccess(promise: Promise<unknown>, timeout = 1500) {
return new Promise((res, rej) => {
setTimeout(() => {
rej(`expected promise to fulfill before timeout of ${timeout}`);
}, timeout);
promise.then(res).catch(rej);
}).catch((e) => {
expect.fail(
'promise expected to succeed but failed.\n' +
'\tExpected: Success\n' +
'\tBut rejected with: ' +
e
);
});
}

/**
* Expects the promise to fail.
*
* @param {Promise} promise The promise to check.
* @param {number} timeout Maximum amount of time it can take to fail, in ms.
* @returns The promise result.
*/
export function expectFail(promise: Promise<unknown>, timeout = 1500) {
return new Promise((res, rej) => {
setTimeout(() => {
rej(`expected promise to fail before timeout of ${timeout}`);
}, timeout);
promise.then(rej).catch(res);
}).catch((data) => {
expect.fail(
'promise expected to fail but resolved.\n' +
'\tExpected: Error\n' +
'\tBut resolved with: ' +
data
);
});
}
Binary file added test/resources/welcome.wav
Binary file not shown.
8 changes: 8 additions & 0 deletions test/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"compilerOptions": {
"moduleResolution": "node",
"allowSyntheticDefaultImports": true
},
"extends": "../tsconfig.json",
"include": ["./**/*.ts"]
}
16 changes: 14 additions & 2 deletions web-test-runner.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import {chromeLauncher} from '@web/test-runner';
import {resolve} from 'path';
import {symlinkSync, existsSync, unlinkSync, lstatSync, rmdirSync} from 'fs';
import {fileURLToPath} from 'url';

import {esbuildPlugin} from '@web/dev-server-esbuild';

console.log(`[TestRunner]: Reading configuration file`);

function findDeployFolder() {
if ('DEPLOY_FOLDER' in process.env) return process.env['DEPLOY_FOLDER'];
Expand Down Expand Up @@ -39,7 +44,7 @@ symlinkSync(deployRoot, 'deploy', 'junction');
export default {
concurrency: 10,
nodeResolve: true,
files: ['test/**/*.test.js'],
files: ['test/**/*.test.ts'],

browsers: [
chromeLauncher({
Expand All @@ -54,4 +59,11 @@ export default {
timeout: '15000',
},
},
};

plugins: [
esbuildPlugin({
ts: true,
tsconfig: fileURLToPath(new URL('./test/tsconfig.json', import.meta.url)),
})
],
};

0 comments on commit a1a6649

Please sign in to comment.