11'use strict' ;
22
33const assert = require ( 'assert' ) ;
4- const watson = require ( '../../index ' ) ;
4+ const SpeechToTextV1 = require ( '../../speech-to-text/v1 ' ) ;
55const nock = require ( 'nock' ) ;
66const fs = require ( 'fs' ) ;
77const isStream = require ( 'isstream' ) ;
8+ const sinon = require ( 'sinon' ) ;
9+ const requestWrapper = require ( '../../lib/requestwrapper' ) ;
810
911describe ( 'speech_to_text' , function ( ) {
1012 const noop = function ( ) { } ;
@@ -17,6 +19,13 @@ describe('speech_to_text', function() {
1719 silent : true , // hide deprecation warnings for recognizeLive and friends
1820 } ;
1921
22+ const rc_service = {
23+ iam_apikey : 'abc123' ,
24+ url : 'http://ibm.com:80' ,
25+ version : 'v1' ,
26+ silent : true , // hide deprecation warnings for recognizeLive and friends
27+ } ;
28+
2029 before ( function ( ) {
2130 nock . disableNetConnect ( ) ;
2231 } ) ;
@@ -25,13 +34,15 @@ describe('speech_to_text', function() {
2534 nock . cleanAll ( ) ;
2635 } ) ;
2736
28- const speech_to_text = watson . speech_to_text ( service ) ;
37+ const speech_to_text = new SpeechToTextV1 ( service ) ;
38+ const rc_speech_to_text = new SpeechToTextV1 ( rc_service ) ;
2939
3040 const missingParameter = function ( err ) {
3141 assert . ok ( err instanceof Error ) ;
3242 assert . ok ( / r e q u i r e d p a r a m e t e r s / . test ( err ) ) ;
3343 } ;
3444
45+ // tests `listModels` in the generated code
3546 describe ( 'getModels()' , function ( ) {
3647 const path = '/v1/models' ;
3748 const models = { models : [ { foo : 'foo' } , { bar : 'bar' } ] } ;
@@ -142,6 +153,23 @@ describe('speech_to_text', function() {
142153 speech_to_text . recognize ( { content_type : 'bar' } , missingParameter ) ;
143154 speech_to_text . recognize ( { continuous : 'false' } , missingParameter ) ;
144155 } ) ;
156+
157+ it ( 'should generate a valid payload' , function ( ) {
158+ const path = '/v1/recognize' ;
159+ const requestSpy = sinon . spy ( requestWrapper , 'sendRequest' ) ;
160+ speech_to_text . recognize (
161+ {
162+ audio : fs . createReadStream ( __dirname + '/../resources/weather.wav' ) ,
163+ content_type : 'audio' ,
164+ } ,
165+ noop
166+ ) ;
167+ assert . equal ( requestSpy . called , true ) ;
168+ const req = requestSpy . returnValues [ 0 ] ;
169+
170+ assert . equal ( req . uri . href , service . url + path ) ;
171+ assert . equal ( req . method , 'POST' ) ;
172+ } ) ;
145173 } ) ;
146174
147175 describe ( 'recognizeWebM()' , function ( ) {
@@ -152,63 +180,6 @@ describe('speech_to_text', function() {
152180 } ) ;
153181 } ) ;
154182
155- // this test is severely broken
156- describe ( 'recognizeStream()' , function ( ) {
157- const service_response = {
158- result : [
159- {
160- alternative : [
161- {
162- transcript : 'one two three' ,
163- } ,
164- ] ,
165- final : true ,
166- } ,
167- ] ,
168- result_index : 0 ,
169- } ;
170-
171- const options = {
172- content_type : 'audio/l16;rate=41100' ,
173- continuous : true ,
174- timestamps : true ,
175- inactivity_timeout : - 1 ,
176- max_alternatives : 1 ,
177- interim_results : false ,
178- keywords : [ 'one' , 'Three' ] ,
179- keywords_threshold : 0.9 ,
180- word_alternatives_threshold : 0.25 ,
181- } ;
182- const recognizeStream = speech_to_text . createRecognizeStream ( options ) ;
183- fs . createReadStream ( __dirname + '/../resources/weather.wav' ) . pipe ( recognizeStream ) ;
184- recognizeStream . setEncoding ( 'utf8' ) ;
185-
186- // note: none of these tests actually run(or even register with mocha), but the callbacks let the previous test pass :(
187- recognizeStream . on ( 'open' , function ( socket ) {
188- it ( 'should have a socket connection with a correct config' , function ( done ) {
189- assert . notStrictEqual ( socket , socket . config , socket . config . fragmentOutgoingMessages ) ;
190- assert . notStrictEqual ( socket , socket . config , socket . config . fragmentOutgoingMessages ) ;
191- done ( ) ;
192- } ) ;
193- } ) ;
194-
195- recognizeStream . on ( 'error' , function ( err ) {
196- it ( 'should throw ECONNRESET with bad credentials' , function ( done ) {
197- assert . equal ( err . code , 'ECONNRESET' ) ;
198- assert . equal ( err . errno , 'ECONNRESET' ) ;
199- done ( ) ;
200- } ) ;
201- } ) ;
202-
203- recognizeStream . on ( 'data' , function ( obj ) {
204- console . log ( JSON . stringify ( obj ) ) ; // eslint-disable-line no-console
205- it ( 'should generate a valid response' , function ( done ) {
206- assert . equal ( obj , service_response ) ;
207- done ( ) ;
208- } ) ;
209- } ) ;
210- } ) ;
211-
212183 describe ( 'recognizeLive()' , function ( ) {
213184 const path = '/v1/sessions/foo/recognize' ;
214185 const payload = {
@@ -278,6 +249,22 @@ describe('speech_to_text', function() {
278249 it ( 'should return a stream' , function ( ) {
279250 assert ( isStream ( speech_to_text . createRecognizeStream ( ) ) ) ;
280251 } ) ;
252+
253+ it ( 'should pass the correct parameters into RecognizeStream' , function ( ) {
254+ const stream = speech_to_text . createRecognizeStream ( ) ;
255+ assert . equal ( stream . options . url , service . url ) ;
256+ assert ( stream . options . headers . authorization ) ;
257+ assert ( stream . authenticated ) ;
258+ assert . equal ( stream . options . token_manager , undefined ) ;
259+ } ) ;
260+
261+ it ( 'should create a token manager in RecognizeStream if using IAM' , function ( ) {
262+ const stream = rc_speech_to_text . createRecognizeStream ( ) ;
263+ assert . equal ( stream . options . url , service . url ) ;
264+ assert . equal ( stream . options . headers . authorization , undefined ) ;
265+ assert . equal ( stream . authenticated , false ) ;
266+ assert ( stream . options . token_manager ) ;
267+ } ) ;
281268 } ) ;
282269
283270 describe ( 'asynchronous callback api' , function ( ) {
@@ -383,6 +370,7 @@ describe('speech_to_text', function() {
383370 assert . equal ( req . method , 'POST' ) ;
384371 } ) ;
385372
373+ // tests `checkJobs` in the generated code
386374 it ( 'should get list of jobs' , function ( done ) {
387375 const path = '/v1/recognitions' ;
388376 const response = {
@@ -491,6 +479,7 @@ describe('speech_to_text', function() {
491479 } ) ;
492480 } ) ;
493481
482+ // tests `listCustomizations` in the generated code
494483 describe ( 'getCustomizations()' , function ( ) {
495484 const path = '/v1/customizations' ;
496485
@@ -562,6 +551,7 @@ describe('speech_to_text', function() {
562551 assert . equal ( req . uri . href , service . url + path ) ;
563552 assert . equal ( req . method , 'POST' ) ;
564553
554+ // test `name` param for backwards compatibility
565555 const req2 = speech_to_text . addCorpus (
566556 {
567557 customization_id : 'customer_id_1' ,
@@ -592,6 +582,7 @@ describe('speech_to_text', function() {
592582 assert . equal ( req . uri . href , service . url + path ) ;
593583 assert . equal ( req . method , 'GET' ) ;
594584
585+ // test `name` param for backwards compatibility
595586 const req2 = speech_to_text . getCorpus (
596587 { customization_id : 'customer_id_1' , name : 'corpus_name_1' } ,
597588 noop
@@ -618,6 +609,7 @@ describe('speech_to_text', function() {
618609 assert . equal ( req . uri . href , service . url + path ) ;
619610 assert . equal ( req . method , 'DELETE' ) ;
620611
612+ // test `name` param for backwards compatibility
621613 const req2 = speech_to_text . deleteCorpus (
622614 { customization_id : 'customer_id_1' , name : 'corpus_name_1' } ,
623615 noop
@@ -627,6 +619,7 @@ describe('speech_to_text', function() {
627619 } ) ;
628620 } ) ;
629621
622+ // tests `listCorpora` in generated code
630623 describe ( 'getCorpora()' , function ( ) {
631624 const path = '/v1/customizations/customer_id_1/corpora' ;
632625
@@ -678,6 +671,7 @@ describe('speech_to_text', function() {
678671 } ) ;
679672 } ) ;
680673
674+ // tests `listWords` in the generated code
681675 describe ( 'getWords()' , function ( ) {
682676 const path = '/v1/customizations/customer_id_1/words' ;
683677
0 commit comments