@@ -80,66 +80,77 @@ async function collectBuffers(stream: Readable): Promise<Buffer[]> {
8080 return chunks ;
8181}
8282
83- describe ( "discord voice opus codec" , ( ) => {
84- it ( "defaults to libopus-wasm for receive decoding" , async ( ) => {
85- const verbose : string [ ] = [ ] ;
86- const warnings : string [ ] = [ ] ;
87-
88- const decoded = await decodeOpusStream ( Readable . from ( [ ] ) , {
89- onVerbose : ( message ) => verbose . push ( message ) ,
90- onWarn : ( message ) => warnings . push ( message ) ,
91- } ) ;
83+ const decodeModes = [
84+ { mode : "buffered" , decode : decodeOpusStream } ,
85+ {
86+ mode : "streaming" ,
87+ decode : async ( stream : Readable , params : Parameters < typeof decodeOpusStream > [ 1 ] ) => {
88+ const chunks : Buffer [ ] = [ ] ;
89+ await decodeOpusStreamChunks ( stream , { ...params , onChunk : ( chunk ) => chunks . push ( chunk ) } ) ;
90+ return Buffer . concat ( chunks ) ;
91+ } ,
92+ } ,
93+ ] ;
9294
93- expect ( decoded . length ) . toBe ( 0 ) ;
94- expect ( verbose ) . toContain ( "opus decoder: libopus-wasm" ) ;
95- expect ( warnings ) . toEqual ( [ ] ) ;
96- } ) ;
95+ describe ( "discord voice opus codec" , ( ) => {
96+ it . each ( decodeModes ) (
97+ "round-trips Discord PCM through $mode Opus decoding" ,
98+ async ( { decode } ) => {
99+ const encoder = createDiscordOpusEncodeStream ( ) ;
100+ const packetsPromise = collectBuffers ( encoder ) ;
101+
102+ encoder . end ( Buffer . alloc ( 960 * 2 * 2 ) ) ;
103+ const packets = await packetsPromise ;
104+
105+ expect ( packets ) . toHaveLength ( 1 ) ;
106+ expect ( packets [ 0 ] ?. length ) . toBeGreaterThan ( 0 ) ;
107+
108+ const onVerbose = vi . fn ( ) ;
109+ const onWarn = vi . fn ( ) ;
110+ const decoded = await decode ( Readable . from ( packets ) , { onVerbose, onWarn } ) ;
111+ expect ( decoded . length ) . toBe ( 960 * 2 * 2 ) ;
112+ expect ( onVerbose ) . toHaveBeenCalledWith ( "opus decoder: libopus-wasm" ) ;
113+ expect ( onWarn ) . not . toHaveBeenCalled ( ) ;
114+ } ,
115+ ) ;
97116
98- it ( "encodes raw Discord PCM into Opus packets for realtime playback " , async ( ) => {
117+ it ( "pads final partial PCM frames before encoding " , async ( ) => {
99118 const encoder = createDiscordOpusEncodeStream ( ) ;
100119 const packetsPromise = collectBuffers ( encoder ) ;
101120
102- encoder . end ( Buffer . alloc ( 960 * 2 * 2 ) ) ;
121+ encoder . end ( Buffer . alloc ( ( 960 * 2 * 2 ) / 2 ) ) ;
103122 const packets = await packetsPromise ;
104123
105124 expect ( packets ) . toHaveLength ( 1 ) ;
106- expect ( packets [ 0 ] ?. length ) . toBeGreaterThan ( 0 ) ;
107-
108125 const decoded = await decodeOpusStream ( Readable . from ( packets ) , {
109126 onVerbose : vi . fn ( ) ,
110127 onWarn : vi . fn ( ) ,
111128 } ) ;
112- expect ( decoded . length ) . toBe ( 960 * 2 * 2 ) ;
129+ expect ( decoded ) . toHaveLength ( 960 * 2 * 2 ) ;
113130 } ) ;
114131
115- it ( "pads final partial PCM frames before encoding" , async ( ) => {
116- const encoder = createDiscordOpusEncodeStream ( ) ;
117- const packetsPromise = collectBuffers ( encoder ) ;
118-
119- encoder . end ( Buffer . alloc ( ( 960 * 2 * 2 ) / 2 ) ) ;
120- const packets = await packetsPromise ;
121-
122- expect ( packets ) . toHaveLength ( 1 ) ;
123- } ) ;
124-
125- it ( "surfaces chunk decode stream failures to callers" , async ( ) => {
126- const err = new Error ( "memory access out of bounds" ) ;
127- const onError = vi . fn ( ) ;
128- const stream = new Readable ( {
129- read ( ) {
130- this . destroy ( err ) ;
131- } ,
132- } ) ;
132+ it . each ( decodeModes ) (
133+ "preserves decoded audio and reports $mode stream failures" ,
134+ async ( { decode } ) => {
135+ const err = new Error ( "memory access out of bounds" ) ;
136+ const onError = vi . fn ( ) ;
137+ const stream = Readable . from (
138+ ( async function * ( ) {
139+ yield Buffer . from ( [ 0xf8 , 0xff , 0xfe ] ) ;
140+ throw err ;
141+ } ) ( ) ,
142+ ) ;
133143
134- await decodeOpusStreamChunks ( stream , {
135- onChunk : vi . fn ( ) ,
136- onError,
137- onVerbose : vi . fn ( ) ,
138- onWarn : vi . fn ( ) ,
139- } ) ;
144+ const decoded = await decode ( stream , {
145+ onError,
146+ onVerbose : vi . fn ( ) ,
147+ onWarn : vi . fn ( ) ,
148+ } ) ;
140149
141- expect ( onError ) . toHaveBeenCalledWith ( err ) ;
142- } ) ;
150+ expect ( onError ) . toHaveBeenCalledWith ( err ) ;
151+ expect ( decoded ) . toHaveLength ( 960 * 2 * 2 ) ;
152+ } ,
153+ ) ;
143154} ) ;
144155
145156describe ( "createDiscordOpusPlaybackStream child stream errors" , ( ) => {
0 commit comments