'use strict' ; import * as assert from 'assert' ; import { afterEach , describe , it } from 'mocha' ; import * as sinon from 'sinon' ; import * as stream from 'stream' ; import * as speech from '../src' ; describe ( 'Speech helper methods' , ( ) => { const sandbox = sinon . createSandbox ( ) ; afterEach ( ( ) => { sandbox . restore ( ) ; } ) ; describe ( 'streamingRecognize' , ( ) => { const CONFIG = { config : { encoding : 'LINEAR16' , languageCode : 'en-us' } , } ; const OPTIONS = { timeout : Infinity } ; it ( 'writes the config to the resulting stream' , done => { const client = new speech . v1 . SpeechClient ( { credentials : { client_email : 'bogus' , private_key : 'bogus' } , projectId : 'bogus' , } ) ; client . initialize ( ) ; const requestStream = new stream . PassThrough ( { objectMode : true } ) ; const sr = sandbox . stub ( client . innerApiCalls , 'streamingRecognize' ) . returns ( requestStream ) ; const userStream = client . streamingRecognize ( CONFIG , OPTIONS ) ; assert ( sr . calledOnce ) ; assert ( sr . calledWithExactly ( OPTIONS ) ) ; requestStream . _write = ( data , enc , next ) => { assert . deepStrictEqual ( data , { streamingConfig : CONFIG , } ) ; setImmediate ( done ) ; next ( null ) ; } ; userStream . write ( undefined ) ; } ) ; it ( 'does not require options' , ( ) => { const client = new speech . v1 . SpeechClient ( { credentials : { client_email : 'bogus' , private_key : 'bogus' } , projectId : 'bogus' , } ) ; client . initialize ( ) ; const requestStream = new stream . PassThrough ( { objectMode : true } ) ; const sr = sandbox . stub ( client . innerApiCalls , 'streamingRecognize' ) . returns ( requestStream ) ; const userStream = client . streamingRecognize ( CONFIG ) ; userStream . emit ( 'writing' ) ; assert ( sr . calledOnce ) ; assert ( sr . calledWithExactly ( { } ) ) ; } ) ; it ( 'destroys the user stream when the request stream errors' , done => { const client = new speech . v1 . SpeechClient ( { credentials : { client_email : 'bogus' , private_key : 'bogus' } , projectId : 'bogus' , } ) ; client . initialize ( ) ; const requestStream = new stream . PassThrough ( { objectMode : true } ) ; sandbox . stub ( client . innerApiCalls , 'streamingRecognize' ) . returns ( requestStream ) ; const userStream = client . streamingRecognize ( CONFIG , OPTIONS ) ; const error = new Error ( 'Request stream error' ) ; userStream . once ( 'error' , ( err ) => { assert . strictEqual ( err , error ) ; done ( ) ; } ) ; requestStream . emit ( 'error' , error ) ; } ) ; it ( 're-emits response from the request stream' , done => { const client = new speech . v1 . SpeechClient ( { credentials : { client_email : 'bogus' , private_key : 'bogus' } , projectId : 'bogus' , } ) ; client . initialize ( ) ; const requestStream = new stream . PassThrough ( { objectMode : true } ) ; sandbox . stub ( client . innerApiCalls , 'streamingRecognize' ) . returns ( requestStream ) ; const userStream = client . streamingRecognize ( CONFIG , OPTIONS ) ; const response = { } ; userStream . on ( 'response' , ( _response ) => { assert . strictEqual ( _response , response ) ; done ( ) ; } ) ; userStream . emit ( 'writing' ) ; requestStream . emit ( 'response' , response ) ; } ) ; it ( 'wraps incoming audio data' , done => { const client = new speech . v1 . SpeechClient ( { credentials : { client_email : 'bogus' , private_key : 'bogus' } , projectId : 'bogus' , } ) ; client . initialize ( ) ; const requestStream = new stream . PassThrough ( { objectMode : true } ) ; sandbox . stub ( client . innerApiCalls , 'streamingRecognize' ) . returns ( requestStream ) ; const userStream = client . streamingRecognize ( CONFIG , OPTIONS ) ; const audioContent = Buffer . from ( 'audio content' ) ; let count = 0 ; requestStream . _write = ( data , enc , next ) => { if ( count === 0 ) { assert . deepStrictEqual ( data , { streamingConfig : CONFIG , } ) ; } else if ( count === 1 ) { assert . deepStrictEqual ( data , { audioContent , } ) ; setImmediate ( done ) ; } count ++ ; next ( null ) ; } ; userStream . end ( audioContent ) ; } ) ; } ) ; } ) ;