@@ -5,6 +5,7 @@ import { Buffer } from 'node:buffer';
55import  {  once  }  from  'node:events' ; 
66import  process  from  'node:process' ; 
77import  {  Readable  }  from  'node:stream' ; 
8+ import  {  describe ,  test ,  expect ,  vitest ,  type  Mock ,  beforeEach ,  afterEach  }  from  'vitest' ; 
89import  {  addAudioPlayer ,  deleteAudioPlayer  }  from  '../src/DataStore' ; 
910import  {  VoiceConnection ,  VoiceConnectionStatus  }  from  '../src/VoiceConnection' ; 
1011import  type  {  AudioPlayer  }  from  '../src/audio/AudioPlayer' ; 
@@ -13,14 +14,31 @@ import { AudioPlayerError } from '../src/audio/AudioPlayerError';
1314import  {  AudioResource  }  from  '../src/audio/AudioResource' ; 
1415import  {  NoSubscriberBehavior  }  from  '../src/index' ; 
1516
16- jest . mock ( '../src/DataStore' ) ; 
17- jest . mock ( '../src/VoiceConnection' ) ; 
18- jest . mock ( '../src/audio/AudioPlayerError' ) ; 
17+ vitest . mock ( '../src/DataStore' ,  ( )  =>  { 
18+ 	return  { 
19+ 		addAudioPlayer : vitest . fn ( ) , 
20+ 		deleteAudioPlayer : vitest . fn ( ) , 
21+ 	} ; 
22+ } ) ; 
1923
20- const  addAudioPlayerMock  =  addAudioPlayer  as  unknown  as  jest . Mock < typeof  addAudioPlayer > ; 
21- const  deleteAudioPlayerMock  =  deleteAudioPlayer  as  unknown  as  jest . Mock < typeof  deleteAudioPlayer > ; 
22- const  AudioPlayerErrorMock  =  AudioPlayerError  as  unknown  as  jest . Mock < typeof  AudioPlayerError > ; 
23- const  VoiceConnectionMock  =  VoiceConnection  as  unknown  as  jest . Mock < VoiceConnection > ; 
24+ vitest . mock ( '../src/VoiceConnection' ,  async  ( importOriginal )  =>  { 
25+ 	// eslint-disable-next-line @typescript-eslint/consistent-type-imports 
26+ 	const  actual  =  await  importOriginal < typeof  import ( '../src/VoiceConnection' ) > ( ) ; 
27+ 	const  VoiceConnection  =  vitest . fn ( ) ; 
28+ 	VoiceConnection . prototype . setSpeaking  =  vitest . fn ( ) ; 
29+ 	VoiceConnection . prototype . dispatchAudio  =  vitest . fn ( ) ; 
30+ 	VoiceConnection . prototype . prepareAudioPacket  =  vitest . fn ( ) ; 
31+ 	return  { 
32+ 		...actual , 
33+ 		VoiceConnection, 
34+ 	} ; 
35+ } ) ; 
36+ 
37+ vitest . mock ( '../src/audio/AudioPlayerError' ,  ( )  =>  { 
38+ 	return  { 
39+ 		AudioPlayerError : vitest . fn ( ) , 
40+ 	} ; 
41+ } ) ; 
2442
2543function *  silence ( )  { 
2644	while  ( true )  { 
@@ -29,15 +47,15 @@ function* silence() {
2947} 
3048
3149function  createVoiceConnectionMock ( )  { 
32- 	const  connection  =  new  VoiceConnectionMock ( ) ; 
50+ 	const  connection  =  new  VoiceConnection ( { }   as   any ,   { }   as   any ) ; 
3351	connection . state  =  { 
3452		status : VoiceConnectionStatus . Signalling , 
3553		adapter : { 
36- 			sendPayload : jest . fn ( ) , 
37- 			destroy : jest . fn ( ) , 
54+ 			sendPayload : vitest . fn ( ) , 
55+ 			destroy : vitest . fn ( ) , 
3856		} , 
3957	} ; 
40- 	connection . subscribe  =  jest . fn ( ( player )  =>  player [ 'subscribe' ] ( connection ) ) ; 
58+ 	connection . subscribe  =  vitest . fn ( ( player )  =>  player [ 'subscribe' ] ( connection ) ) ; 
4159	return  connection ; 
4260} 
4361
@@ -57,10 +75,7 @@ async function started(resource: AudioResource) {
5775let  player : AudioPlayer  |  undefined ; 
5876
5977beforeEach ( ( )  =>  { 
60- 	AudioPlayerErrorMock . mockReset ( ) ; 
61- 	VoiceConnectionMock . mockReset ( ) ; 
62- 	addAudioPlayerMock . mockReset ( ) ; 
63- 	deleteAudioPlayerMock . mockReset ( ) ; 
78+ 	vitest . resetAllMocks ( ) ; 
6479} ) ; 
6580
6681afterEach ( ( )  =>  { 
@@ -71,8 +86,8 @@ describe('State transitions', () => {
7186	test ( 'Starts in Idle state' ,  ( )  =>  { 
7287		player  =  createAudioPlayer ( ) ; 
7388		expect ( player . state . status ) . toEqual ( AudioPlayerStatus . Idle ) ; 
74- 		expect ( addAudioPlayerMock ) . toBeCalledTimes ( 0 ) ; 
75- 		expect ( deleteAudioPlayerMock ) . toBeCalledTimes ( 0 ) ; 
89+ 		expect ( addAudioPlayer ) . toBeCalledTimes ( 0 ) ; 
90+ 		expect ( deleteAudioPlayer ) . toBeCalledTimes ( 0 ) ; 
7691	} ) ; 
7792
7893	test ( 'Playing resource with pausing and resuming' ,  async  ( )  =>  { 
@@ -86,11 +101,11 @@ describe('State transitions', () => {
86101		expect ( player . state . status ) . toEqual ( AudioPlayerStatus . Idle ) ; 
87102		expect ( player . unpause ( ) ) . toEqual ( false ) ; 
88103		expect ( player . state . status ) . toEqual ( AudioPlayerStatus . Idle ) ; 
89- 		expect ( addAudioPlayerMock ) . toBeCalledTimes ( 0 ) ; 
104+ 		expect ( addAudioPlayer ) . toBeCalledTimes ( 0 ) ; 
90105
91106		player . play ( resource ) ; 
92107		expect ( player . state . status ) . toEqual ( AudioPlayerStatus . Playing ) ; 
93- 		expect ( addAudioPlayerMock ) . toBeCalledTimes ( 1 ) ; 
108+ 		expect ( addAudioPlayer ) . toBeCalledTimes ( 1 ) ; 
94109
95110		// Expect pause() to return true and transition to paused state 
96111		expect ( player . pause ( ) ) . toEqual ( true ) ; 
@@ -109,7 +124,7 @@ describe('State transitions', () => {
109124		expect ( player . state . status ) . toEqual ( AudioPlayerStatus . Playing ) ; 
110125
111126		// The audio player should not have been deleted throughout these changes 
112- 		expect ( deleteAudioPlayerMock ) . toBeCalledTimes ( 0 ) ; 
127+ 		expect ( deleteAudioPlayer ) . toBeCalledTimes ( 0 ) ; 
113128	} ) ; 
114129
115130	test ( 'Playing to Stopping' ,  async  ( )  =>  { 
@@ -122,13 +137,13 @@ describe('State transitions', () => {
122137
123138		player . play ( resource ) ; 
124139		expect ( player . state . status ) . toEqual ( AudioPlayerStatus . Playing ) ; 
125- 		expect ( addAudioPlayerMock ) . toBeCalledTimes ( 1 ) ; 
126- 		expect ( deleteAudioPlayerMock ) . toBeCalledTimes ( 0 ) ; 
140+ 		expect ( addAudioPlayer ) . toBeCalledTimes ( 1 ) ; 
141+ 		expect ( deleteAudioPlayer ) . toBeCalledTimes ( 0 ) ; 
127142
128143		expect ( player . stop ( ) ) . toEqual ( true ) ; 
129144		expect ( player . state . status ) . toEqual ( AudioPlayerStatus . Playing ) ; 
130- 		expect ( addAudioPlayerMock ) . toBeCalledTimes ( 1 ) ; 
131- 		expect ( deleteAudioPlayerMock ) . toBeCalledTimes ( 0 ) ; 
145+ 		expect ( addAudioPlayer ) . toBeCalledTimes ( 1 ) ; 
146+ 		expect ( deleteAudioPlayer ) . toBeCalledTimes ( 0 ) ; 
132147		expect ( resource . silenceRemaining ) . toEqual ( 5 ) ; 
133148	} ) ; 
134149
@@ -142,8 +157,8 @@ describe('State transitions', () => {
142157		await  started ( resource ) ; 
143158
144159		expect ( player . state . status ) . toEqual ( AudioPlayerStatus . Playing ) ; 
145- 		expect ( addAudioPlayerMock ) . toHaveBeenCalled ( ) ; 
146- 		expect ( deleteAudioPlayerMock ) . not . toHaveBeenCalled ( ) ; 
160+ 		expect ( addAudioPlayer ) . toHaveBeenCalled ( ) ; 
161+ 		expect ( deleteAudioPlayer ) . not . toHaveBeenCalled ( ) ; 
147162	} ) ; 
148163
149164	describe ( 'NoSubscriberBehavior transitions' ,  ( )  =>  { 
@@ -188,11 +203,11 @@ describe('State transitions', () => {
188203			player  =  createAudioPlayer ( {  behaviors : {  noSubscriber : NoSubscriberBehavior . Stop  }  } ) ; 
189204
190205			player . play ( resource ) ; 
191- 			expect ( addAudioPlayerMock ) . toBeCalledTimes ( 1 ) ; 
206+ 			expect ( addAudioPlayer ) . toBeCalledTimes ( 1 ) ; 
192207			expect ( player . checkPlayable ( ) ) . toEqual ( true ) ; 
193208			player [ '_stepPrepare' ] ( ) ; 
194209			expect ( player . state . status ) . toEqual ( AudioPlayerStatus . Idle ) ; 
195- 			expect ( deleteAudioPlayerMock ) . toBeCalledTimes ( 1 ) ; 
210+ 			expect ( deleteAudioPlayer ) . toBeCalledTimes ( 1 ) ; 
196211		} ) ; 
197212	} ) ; 
198213
@@ -217,7 +232,7 @@ describe('State transitions', () => {
217232
218233		player . play ( resource ) ; 
219234		expect ( player . state . status ) . toEqual ( AudioPlayerStatus . Playing ) ; 
220- 		expect ( addAudioPlayerMock ) . toBeCalledTimes ( 1 ) ; 
235+ 		expect ( addAudioPlayer ) . toBeCalledTimes ( 1 ) ; 
221236		expect ( player . checkPlayable ( ) ) . toEqual ( true ) ; 
222237
223238		// Run through a few packet cycles 
@@ -241,7 +256,8 @@ describe('State transitions', () => {
241256		expect ( connection . dispatchAudio ) . toHaveBeenCalledTimes ( 6 ) ; 
242257		await  wait ( ) ; 
243258		player [ '_stepPrepare' ] ( ) ; 
244- 		const  prepareAudioPacket  =  connection . prepareAudioPacket  as  unknown  as  jest . Mock < 
259+ 		const  prepareAudioPacket  =  connection . prepareAudioPacket  as  unknown  as  Mock < 
260+ 			[ Buffer ] , 
245261			typeof  connection . prepareAudioPacket 
246262		> ; 
247263		expect ( prepareAudioPacket ) . toHaveBeenCalledTimes ( 6 ) ; 
@@ -251,7 +267,7 @@ describe('State transitions', () => {
251267		expect ( player . state . status ) . toEqual ( AudioPlayerStatus . Idle ) ; 
252268		expect ( connection . setSpeaking ) . toBeCalledTimes ( 1 ) ; 
253269		expect ( connection . setSpeaking ) . toHaveBeenLastCalledWith ( false ) ; 
254- 		expect ( deleteAudioPlayerMock ) . toHaveBeenCalledTimes ( 1 ) ; 
270+ 		expect ( deleteAudioPlayer ) . toHaveBeenCalledTimes ( 1 ) ; 
255271	} ) ; 
256272
257273	test ( 'stop() causes resource to use silence padding frames' ,  async  ( )  =>  { 
@@ -275,7 +291,7 @@ describe('State transitions', () => {
275291
276292		player . play ( resource ) ; 
277293		expect ( player . state . status ) . toEqual ( AudioPlayerStatus . Playing ) ; 
278- 		expect ( addAudioPlayerMock ) . toBeCalledTimes ( 1 ) ; 
294+ 		expect ( addAudioPlayer ) . toBeCalledTimes ( 1 ) ; 
279295		expect ( player . checkPlayable ( ) ) . toEqual ( true ) ; 
280296
281297		player . stop ( ) ; 
@@ -298,15 +314,16 @@ describe('State transitions', () => {
298314
299315		await  wait ( ) ; 
300316		expect ( player . checkPlayable ( ) ) . toEqual ( false ) ; 
301- 		const  prepareAudioPacket  =  connection . prepareAudioPacket  as  unknown  as  jest . Mock < 
317+ 		const  prepareAudioPacket  =  connection . prepareAudioPacket  as  unknown  as  Mock < 
318+ 			[ Buffer ] , 
302319			typeof  connection . prepareAudioPacket 
303320		> ; 
304321		expect ( prepareAudioPacket ) . toHaveBeenCalledTimes ( 5 ) ; 
305322
306323		expect ( player . state . status ) . toEqual ( AudioPlayerStatus . Idle ) ; 
307324		expect ( connection . setSpeaking ) . toBeCalledTimes ( 1 ) ; 
308325		expect ( connection . setSpeaking ) . toHaveBeenLastCalledWith ( false ) ; 
309- 		expect ( deleteAudioPlayerMock ) . toHaveBeenCalledTimes ( 1 ) ; 
326+ 		expect ( deleteAudioPlayer ) . toHaveBeenCalledTimes ( 1 ) ; 
310327	} ) ; 
311328
312329	test ( 'Plays silence 5 times for unreadable stream before quitting' ,  async  ( )  =>  { 
@@ -328,10 +345,11 @@ describe('State transitions', () => {
328345
329346		player . play ( resource ) ; 
330347		expect ( player . state . status ) . toEqual ( AudioPlayerStatus . Playing ) ; 
331- 		expect ( addAudioPlayerMock ) . toBeCalledTimes ( 1 ) ; 
348+ 		expect ( addAudioPlayer ) . toBeCalledTimes ( 1 ) ; 
332349		expect ( player . checkPlayable ( ) ) . toEqual ( true ) ; 
333350
334- 		const  prepareAudioPacket  =  connection . prepareAudioPacket  as  unknown  as  jest . Mock < 
351+ 		const  prepareAudioPacket  =  connection . prepareAudioPacket  as  unknown  as  Mock < 
352+ 			[ Buffer ] , 
335353			typeof  connection . prepareAudioPacket 
336354		> ; 
337355
@@ -351,7 +369,7 @@ describe('State transitions', () => {
351369		expect ( player . state . status ) . toEqual ( AudioPlayerStatus . Idle ) ; 
352370		expect ( connection . setSpeaking ) . toBeCalledTimes ( 1 ) ; 
353371		expect ( connection . setSpeaking ) . toHaveBeenLastCalledWith ( false ) ; 
354- 		expect ( deleteAudioPlayerMock ) . toHaveBeenCalledTimes ( 1 ) ; 
372+ 		expect ( deleteAudioPlayer ) . toHaveBeenCalledTimes ( 1 ) ; 
355373	} ) ; 
356374
357375	test ( 'checkPlayable() transitions to Idle for unreadable stream' ,  async  ( )  =>  { 
@@ -397,6 +415,6 @@ test('Propagates errors from streams', async () => {
397415	const  res  =  await  once ( player ,  'error' ) ; 
398416	const  playerError  =  res [ 0 ]  as  AudioPlayerError ; 
399417	expect ( playerError ) . toBeInstanceOf ( AudioPlayerError ) ; 
400- 	expect ( AudioPlayerErrorMock ) . toHaveBeenCalledWith ( error ,  resource ) ; 
418+ 	expect ( AudioPlayerError ) . toHaveBeenCalledWith ( error ,  resource ) ; 
401419	expect ( player . state . status ) . toEqual ( AudioPlayerStatus . Idle ) ; 
402420} ) ; 
0 commit comments