Skip to content

Commit a7d7736

Browse files
committed
Merge commit '556eb2529d4cbbfa81be0d1ea1a8de78a34f6c60' into hrb-tests
* commit '556eb2529d4cbbfa81be0d1ea1a8de78a34f6c60': (32 commits) mixer unit test done speaker_track unit test done utils unit test done speaker_engine unit test done (96,267-269,287,404) buffer_effects_processor unit test done assetPool unit test done user unit test done playlistAudioTrack unit test done playlist unit test done listenHistory unit test done event_emitter unit test done assetPool unit test (122) assetPool unit test done (47,68-95,122,177) TrackStates unit test done utils unit test done (205-207) mixer unit test done (176,186) speaker_utils unit test done buffer_effects_processor (244) speaker_track unit test done (314) speaker_engine unit test done (540) ...
2 parents 14222ba + 556eb25 commit a7d7736

15 files changed

+12576
-846
lines changed

src/TrackStates.test.ts

Lines changed: 672 additions & 44 deletions
Large diffs are not rendered by default.

src/assetPool.test.ts

Lines changed: 2713 additions & 37 deletions
Large diffs are not rendered by default.

src/audioPanner.test.ts

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
import { AudioPanner } from './audioPanner';
2+
import { AudioContext, StereoPannerNode } from 'standardized-audio-context-mock';
3+
import { jest, describe, test, expect, beforeEach, afterEach } from '@jest/globals';
4+
5+
jest.useFakeTimers();
6+
7+
describe('AudioPanner', () => {
8+
let audioContext: AudioContext;
9+
let panNode: StereoPannerNode<AudioContext>;
10+
let audioPanner: AudioPanner;
11+
12+
beforeEach(() => {
13+
audioContext = new AudioContext();
14+
panNode = new StereoPannerNode(audioContext);
15+
audioPanner = new AudioPanner(-1, 1, 1, 2, panNode, audioContext);
16+
});
17+
18+
afterEach(() => {
19+
audioPanner.clear();
20+
jest.clearAllTimers();
21+
});
22+
23+
test('should initialize with correct parameters', () => {
24+
expect(audioPanner.minpanpos).toBe(-1);
25+
expect(audioPanner.maxpanpos).toBe(1);
26+
expect(audioPanner.minpanduration).toBe(1);
27+
expect(audioPanner.maxpanduration).toBe(2);
28+
expect(audioPanner.panNode).toBe(panNode);
29+
expect(audioPanner.audioContext).toBe(audioContext);
30+
});
31+
32+
test('should use default parameters when none provided', () => {
33+
const defaultPanner = new AudioPanner(undefined, undefined, undefined, undefined, panNode, audioContext);
34+
expect(defaultPanner.minpanpos).toBe(0);
35+
expect(defaultPanner.maxpanpos).toBe(0);
36+
expect(defaultPanner.minpanduration).toBe(0);
37+
expect(defaultPanner.maxpanduration).toBe(0);
38+
});
39+
40+
test('should set initial pan value within range', () => {
41+
const initialPanValue = audioPanner.panNode.pan.value;
42+
expect(initialPanValue).toBeGreaterThanOrEqual(-1);
43+
expect(initialPanValue).toBeLessThanOrEqual(1);
44+
});
45+
46+
test('should update parameters with random values within ranges', () => {
47+
audioPanner.updateParams();
48+
49+
expect(audioPanner.finalPosition).toBeGreaterThanOrEqual(-1);
50+
expect(audioPanner.finalPosition).toBeLessThanOrEqual(1);
51+
expect(audioPanner.duration).toBeGreaterThanOrEqual(1);
52+
expect(audioPanner.duration).toBeLessThanOrEqual(2);
53+
});
54+
55+
test('should handle initialPosition in updateParams', () => {
56+
// Set initial position
57+
audioPanner.initialPosition = 0.5;
58+
audioPanner.updateParams();
59+
60+
// First update should use initialPosition
61+
expect(audioPanner.currentPosition).toBe(0.5);
62+
63+
// Second update should use a new random finalPosition
64+
const firstFinalPosition = audioPanner.finalPosition;
65+
audioPanner.updateParams();
66+
expect(audioPanner.finalPosition).not.toBe(firstFinalPosition);
67+
});
68+
69+
test('should start panning animation', () => {
70+
const linearRampSpy = jest.spyOn(audioPanner.panNode.pan, 'linearRampToValueAtTime');
71+
72+
audioPanner.start();
73+
74+
expect(linearRampSpy).toHaveBeenCalled();
75+
expect(audioPanner.timerId).toBeDefined();
76+
});
77+
78+
test('should clear timer when clear is called', () => {
79+
const clearTimeoutSpy = jest.spyOn(global, 'clearTimeout');
80+
81+
audioPanner.start();
82+
audioPanner.clear();
83+
84+
expect(clearTimeoutSpy).toHaveBeenCalledWith(audioPanner.timerId);
85+
});
86+
87+
test('should continue panning after duration expires', () => {
88+
const linearRampSpy = jest.spyOn(audioPanner.panNode.pan, 'linearRampToValueAtTime');
89+
90+
audioPanner.start();
91+
jest.advanceTimersByTime(audioPanner.duration! * 1000);
92+
93+
expect(linearRampSpy).toHaveBeenCalledTimes(2);
94+
});
95+
});

src/event_emitter.test.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,4 +107,40 @@ describe('EventEmitter', () => {
107107
// Should not throw any errors
108108
});
109109
});
110+
111+
describe('clearListeners', () => {
112+
it('should clear all listeners for a given event', () => {
113+
const listener1 = jest.fn();
114+
const listener2 = jest.fn();
115+
emitter.on('test', listener1);
116+
emitter.on('test', listener2);
117+
118+
emitter.clearListeners('test');
119+
emitter.emit('test', 'hello');
120+
121+
expect(listener1).not.toHaveBeenCalled();
122+
expect(listener2).not.toHaveBeenCalled();
123+
});
124+
125+
it('should handle clearing listeners for non-existent events', () => {
126+
expect(() => {
127+
emitter.clearListeners('nonexistent' as keyof TestEvents);
128+
}).not.toThrow();
129+
});
130+
131+
it('should only clear listeners for the specified event', () => {
132+
const testListener = jest.fn();
133+
const numberListener = jest.fn();
134+
135+
emitter.on('test', testListener);
136+
emitter.on('number', numberListener);
137+
138+
emitter.clearListeners('test');
139+
emitter.emit('test', 'hello');
140+
emitter.emit('number', 42);
141+
142+
expect(testListener).not.toHaveBeenCalled();
143+
expect(numberListener).toHaveBeenCalledWith(42);
144+
});
145+
});
110146
});

0 commit comments

Comments
 (0)