Skip to content

Commit e60bdfc

Browse files
committed
added unit tests
1 parent 062a67e commit e60bdfc

File tree

1 file changed

+51
-2
lines changed

1 file changed

+51
-2
lines changed

src/sources/audio.spec.ts

+51-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ class MockOfflineAudioContext {
1616
const audioBuffer = {
1717
duration: 5, // Mock duration
1818
sampleRate: this.sampleRate,
19-
getChannelData: () => new Float32Array(5000), // Return a dummy Float32Array
19+
length: 5000,
20+
getChannelData: () => new Float32Array(5000).fill(0.5), // Return a dummy Float32Array
2021
} as any as AudioBuffer;
2122
return Promise.resolve(audioBuffer);
2223
}
@@ -33,11 +34,59 @@ describe('AudioSource', () => {
3334
});
3435

3536
it('should decode an audio buffer correctly', async () => {
36-
const buffer = await audioSource.decode(2, 44100);
37+
const buffer = await audioSource.decode(2, 44100, true);
3738
expect(buffer.duration).toBe(5); // Mock duration
3839
expect(buffer.sampleRate).toBe(44100);
3940
expect(audioSource.audioBuffer).toBe(buffer);
4041
expect(audioSource.duration.seconds).toBe(5); // Ensure duration is set
42+
43+
audioSource.audioBuffer = undefined;
44+
await audioSource.decode(2, 44100, false);
45+
expect(audioSource.audioBuffer).toBe(undefined);
46+
});
47+
48+
it('should (fast) sample an audio buffer correctly', async () => {
49+
const samples = await audioSource.fastsampler({ length: 20 });
50+
expect(samples.length).toBe(20);
51+
52+
for (const sample of samples) {
53+
expect(sample).toBe(0.5);
54+
}
55+
});
56+
57+
it('should (fast) sample an audio buffer correctly with start', async () => {
58+
const samples = await audioSource.fastsampler({
59+
length: 20,
60+
start: 10,
61+
});
62+
expect(samples.length).toBe(20);
63+
64+
for (const sample of samples) {
65+
expect(sample).toBe(0.5);
66+
}
67+
});
68+
69+
it('should (fast) sample an audio buffer correctly with stop', async () => {
70+
const samples = await audioSource.fastsampler({
71+
length: 20,
72+
stop: 1000,
73+
start: 20,
74+
});
75+
expect(samples.length).toBe(20);
76+
77+
for (const sample of samples) {
78+
expect(sample).toBe(0.5);
79+
}
80+
});
81+
82+
it('should (fast) sample an audio buffer correctly with logarithmic scale', async () => {
83+
const samples = await audioSource.fastsampler({ logarithmic: true });
84+
expect(samples.length).toBe(60);
85+
86+
for (const sample of samples) {
87+
expect(sample).toBeGreaterThanOrEqual(0.5);
88+
expect(sample).toBeLessThanOrEqual(1);
89+
}
4190
});
4291

4392
it('should create a thumbnail with correct DOM elements', async () => {

0 commit comments

Comments
 (0)