-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7f20b22
commit a1a6649
Showing
7 changed files
with
147 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"compilerOptions": { | ||
"moduleResolution": "node", | ||
"allowSyntheticDefaultImports": true | ||
}, | ||
"extends": "../tsconfig.json", | ||
"include": ["./**/*.ts"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters