1- import { describe , expect , it } from 'vitest' ;
1+ import { afterEach , beforeEach , describe , expect , it , test } from 'vitest' ;
22import * as funcs from '../functions' ;
3- import type { Sound } from '../types' ;
3+ import type { Sound , Wave } from '../types' ;
4+ import { mockAudioContext } from './utils' ;
5+
6+ Object . defineProperty ( global , 'AudioContext' , {
7+ value : ( ) => mockAudioContext
8+ } ) ;
49
510describe ( funcs . make_sound , ( ) => {
611 it ( 'Should error gracefully when duration is negative' , ( ) => {
@@ -18,37 +23,72 @@ describe(funcs.make_sound, () => {
1823 } ) ;
1924} ) ;
2025
21- describe ( funcs . play , ( ) => {
22- it ( 'Should error gracefully when duration is negative' , ( ) => {
23- const sound : Sound = [ ( ) => 0 , - 1 ] ;
24- expect ( ( ) => funcs . play ( sound ) )
25- . toThrow ( 'play: duration of sound is negative' ) ;
26+ describe ( 'Concurrent playback functions' , ( ) => {
27+ beforeEach ( ( ) => {
28+ funcs . globalVars . audioplayer = null ;
2629 } ) ;
2730
28- it ( 'Should not error when duration is zero' , ( ) => {
29- const sound = funcs . make_sound ( ( ) => 0 , 0 ) ;
30- expect ( ( ) => funcs . play ( sound ) ) . not . toThrow ( ) ;
31+ afterEach ( ( ) => {
32+ funcs . globalVars . isPlaying = false ;
3133 } ) ;
3234
33- it ( 'Should throw error when given not a sound' , ( ) => {
34- expect ( ( ) => funcs . play ( 0 as any ) ) . toThrow ( 'play is expecting sound, but encountered 0' ) ;
35+ describe ( funcs . play , ( ) => {
36+ it ( 'Should error gracefully when duration is negative' , ( ) => {
37+ const sound : Sound = [ ( ) => 0 , - 1 ] ;
38+ expect ( ( ) => funcs . play ( sound ) )
39+ . toThrow ( 'play: duration of sound is negative' ) ;
40+ } ) ;
41+
42+ it ( 'Should not error when duration is zero' , ( ) => {
43+ const sound = funcs . make_sound ( ( ) => 0 , 0 ) ;
44+ expect ( ( ) => funcs . play ( sound ) ) . not . toThrow ( ) ;
45+ } ) ;
46+
47+ it ( 'Should throw error when given not a sound' , ( ) => {
48+ expect ( ( ) => funcs . play ( 0 as any ) ) . toThrow ( 'play is expecting sound, but encountered 0' ) ;
49+ } ) ;
50+
51+ test . only ( 'Concurrently playing two sounds should error' , ( ) => {
52+ console . log ( AudioContext ) ;
53+ const sound = funcs . silence_sound ( 10 ) ;
54+ expect ( ( ) => funcs . play ( sound ) ) . not . toThrow ( ) ;
55+ expect ( ( ) => funcs . play ( sound ) ) . toThrowError ( 'play: Previous sound still playing' ) ;
56+ } ) ;
3557 } ) ;
36- } ) ;
3758
38- describe ( funcs . play_wave , ( ) => {
39- it ( 'Should error gracefully when duration is negative' , ( ) => {
40- expect ( ( ) => funcs . play_wave ( ( ) => 0 , - 1 ) )
41- . toThrow ( 'play_wave: Sound duration must be greater than or equal to 0' ) ;
59+ describe ( funcs . play_wave , ( ) => {
60+ it ( 'Should error gracefully when duration is negative' , ( ) => {
61+ expect ( ( ) => funcs . play_wave ( ( ) => 0 , - 1 ) )
62+ . toThrow ( 'play_wave: Sound duration must be greater than or equal to 0' ) ;
63+ } ) ;
64+
65+ it ( 'Should error gracefully when duration is not a number' , ( ) => {
66+ expect ( ( ) => funcs . play_wave ( ( ) => 0 , true as any ) )
67+ . toThrow ( 'play_wave expects a number for duration, got true' ) ;
68+ } ) ;
69+
70+ it ( 'Should error gracefully when wave is not a function' , ( ) => {
71+ expect ( ( ) => funcs . play_wave ( true as any , 0 ) )
72+ . toThrow ( 'play_wave expects a wave, got true' ) ;
73+ } ) ;
74+
75+ test ( 'Concurrently playing two sounds should error' , ( ) => {
76+ const wave : Wave = ( ) => 0 ;
77+ expect ( ( ) => funcs . play_wave ( wave , 10 ) ) . not . toThrow ( ) ;
78+ expect ( ( ) => funcs . play_wave ( wave , 10 ) ) . toThrowError ( 'play: Previous sound still playing' ) ;
79+ } ) ;
4280 } ) ;
4381
44- it ( 'Should error gracefully when duration is not a number' , ( ) => {
45- expect ( ( ) => funcs . play_wave ( ( ) => 0 , true as any ) )
46- . toThrow ( 'play_wave expects a number for duration, got true' ) ;
47- } ) ;
82+ describe ( funcs . stop , ( ) => {
83+ test ( 'Calling stop without ever calling any playback functions should not throw an error' , ( ) => {
84+ expect ( funcs . stop ) . not . toThrowError ( ) ;
85+ } ) ;
4886
49- it ( 'Should error gracefully when wave is not a function' , ( ) => {
50- expect ( ( ) => funcs . play_wave ( true as any , 0 ) )
51- . toThrow ( 'play_wave expects a wave, got true' ) ;
87+ it ( 'sets isPlaying to false' , ( ) => {
88+ funcs . globalVars . isPlaying = true ;
89+ funcs . stop ( ) ;
90+ expect ( funcs . globalVars . isPlaying ) . toEqual ( false ) ;
91+ } ) ;
5292 } ) ;
5393} ) ;
5494
@@ -67,6 +107,13 @@ describe(funcs.play_in_tab, () => {
67107 it ( 'Should throw error when given not a sound' , ( ) => {
68108 expect ( ( ) => funcs . play_in_tab ( 0 as any ) ) . toThrow ( 'play_in_tab is expecting sound, but encountered 0' ) ;
69109 } ) ;
110+
111+ test ( 'Multiple calls does not cause an error' , ( ) => {
112+ const sound = funcs . silence_sound ( 10 ) ;
113+ expect ( ( ) => funcs . play_in_tab ( sound ) ) . not . toThrow ( ) ;
114+ expect ( ( ) => funcs . play_in_tab ( sound ) ) . not . toThrow ( ) ;
115+ expect ( ( ) => funcs . play_in_tab ( sound ) ) . not . toThrow ( ) ;
116+ } ) ;
70117} ) ;
71118
72119function evaluateSound ( sound : Sound ) {
0 commit comments