|
| 1 | +/** |
| 2 | + * Copyright 2016, Google, Inc. |
| 3 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | + * you may not use this file except in compliance with the License. |
| 5 | + * You may obtain a copy of the License at |
| 6 | + * |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * Unless required by applicable law or agreed to in writing, software |
| 10 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | + * See the License for the specific language governing permissions and |
| 13 | + * limitations under the License. |
| 14 | + */ |
| 15 | + |
| 16 | +'use strict'; |
| 17 | + |
| 18 | +const path = require(`path`); |
| 19 | +const proxyquire = require(`proxyquire`).noPreserveCache(); |
| 20 | +const speech = proxyquire(`@google-cloud/speech`, {})(); |
| 21 | + |
| 22 | +const fileName = path.join(__dirname, `../resources/audio.raw`); |
| 23 | +const config = { |
| 24 | + encoding: `LINEAR16`, |
| 25 | + sampleRate: 16000 |
| 26 | +}; |
| 27 | + |
| 28 | +describe(`speech:quickstart`, () => { |
| 29 | + let speechMock, SpeechMock; |
| 30 | + |
| 31 | + it(`should detect speech`, (done) => { |
| 32 | + const expectedFileName = `./resources/audio.raw`; |
| 33 | + const expectedText = `how old is the Brooklyn Bridge`; |
| 34 | + |
| 35 | + speechMock = { |
| 36 | + recognize: (_fileName, _config, _callback) => { |
| 37 | + assert.equal(_fileName, expectedFileName); |
| 38 | + assert.deepEqual(_config, config); |
| 39 | + assert.equal(typeof _callback, `function`); |
| 40 | + |
| 41 | + speech.recognize(fileName, config, (err, transcription, apiResponse) => { |
| 42 | + _callback(err, transcription, apiResponse); |
| 43 | + assert.ifError(err); |
| 44 | + assert.equal(transcription, expectedText); |
| 45 | + assert.notEqual(apiResponse, undefined); |
| 46 | + assert.equal(console.log.calledOnce, true); |
| 47 | + assert.deepEqual(console.log.firstCall.args, [`Transcription: ${expectedText}`]); |
| 48 | + done(); |
| 49 | + }); |
| 50 | + } |
| 51 | + }; |
| 52 | + SpeechMock = sinon.stub().returns(speechMock); |
| 53 | + |
| 54 | + proxyquire(`../quickstart`, { |
| 55 | + '@google-cloud/speech': SpeechMock |
| 56 | + }); |
| 57 | + }); |
| 58 | +}); |
0 commit comments