Skip to content

Commit 3108fd2

Browse files
speech:tests bring coverage back for helpers.js (#2476)
1 parent 9a11b8e commit 3108fd2

File tree

1 file changed

+98
-9
lines changed

1 file changed

+98
-9
lines changed

packages/speech/test/helpers.test.js

Lines changed: 98 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,33 +22,122 @@ var stream = require('stream');
2222

2323
var Speech = require('../');
2424

25-
2625
describe('Speech helper methods', () => {
2726
var sandbox = sinon.sandbox.create();
27+
var speech;
28+
29+
beforeEach(() => {
30+
speech = Speech.v1();
31+
});
2832

2933
afterEach(() => {
3034
sandbox.restore();
3135
});
3236

3337
describe('streamingRecognize', () => {
34-
it('writes the config to the resulting stream', () => {
35-
var speech = Speech.v1();
38+
var CONFIG = {config: {languageCode: 'en-us'}};
39+
var OPTIONS = {timeout: Infinity};
3640

41+
it('writes the config to the resulting stream', (done) => {
3742
// Stub the underlying _streamingRecognize method to just return
3843
// a bogus stream.
39-
var writable = stream.Writable();
40-
var sr = sandbox.stub(speech, '_streamingRecognize').returns(writable);
44+
var requestStream = new stream.PassThrough({ objectMode: true });
45+
var sr = sandbox.stub(speech, '_streamingRecognize')
46+
.returns(requestStream);
4147

4248
// Call the new helper method and establish that the config was
4349
// forwarded as expected.
44-
var config = {config: {languageCode: 'en-us'}};
45-
var options = {timeout: Infinity};
46-
speech.streamingRecognize(config, options);
50+
var userStream = speech.streamingRecognize(CONFIG, OPTIONS);
4751

4852
// Establish that the underlying streamingRecognize was called with
4953
// the options.
5054
assert(sr.calledOnce);
51-
assert(sr.calledWithExactly(options));
55+
assert(sr.calledWithExactly(OPTIONS));
56+
57+
requestStream._write = (data, enc, next) => {
58+
assert.deepStrictEqual(data, {
59+
streamingConfig: CONFIG
60+
});
61+
setImmediate(done);
62+
next(null, data);
63+
};
64+
65+
userStream.emit('writing');
66+
});
67+
68+
it('does not require options', () => {
69+
// Stub the underlying _streamingRecognize method to just return
70+
// a bogus stream.
71+
var requestStream = new stream.PassThrough({ objectMode: true });
72+
var sr = sandbox.stub(speech, '_streamingRecognize')
73+
.returns(requestStream);
74+
75+
var userStream = speech.streamingRecognize(CONFIG);
76+
77+
userStream.emit('writing');
78+
79+
assert(sr.calledOnce);
80+
assert(sr.calledWithExactly({}));
81+
});
82+
83+
it('destroys the user stream when the request stream errors', (done) => {
84+
// Stub the underlying _streamingRecognize method to just return
85+
// a bogus stream.
86+
var requestStream = new stream.PassThrough({ objectMode: true });
87+
sandbox.stub(speech, '_streamingRecognize').returns(requestStream);
88+
89+
var userStream = speech.streamingRecognize(CONFIG, OPTIONS);
90+
91+
var error = new Error('Request stream error');
92+
93+
userStream.once('error', (err) => {
94+
assert.strictEqual(err, error);
95+
done();
96+
});
97+
98+
userStream.emit('writing');
99+
100+
requestStream.emit('error', error);
101+
});
102+
103+
it('re-emits response from the request stream', (done) => {
104+
// Stub the underlying _streamingRecognize method to just return
105+
// a bogus stream.
106+
var requestStream = new stream.PassThrough({ objectMode: true });
107+
sandbox.stub(speech, '_streamingRecognize').returns(requestStream);
108+
109+
var userStream = speech.streamingRecognize(CONFIG, OPTIONS);
110+
111+
var response = {};
112+
113+
userStream.on('response', (_response) => {
114+
assert.strictEqual(_response, response);
115+
done();
116+
});
117+
118+
userStream.emit('writing');
119+
requestStream.emit('response', response);
120+
});
121+
122+
it('wraps incoming audio data', (done) => {
123+
// Stub the underlying _streamingRecognize method to just return
124+
// a bogus stream.
125+
var requestStream = new stream.PassThrough({ objectMode: true });
126+
sandbox.stub(speech, '_streamingRecognize').returns(requestStream);
127+
128+
var userStream = speech.streamingRecognize(CONFIG, OPTIONS);
129+
var audioContent = new Buffer('audio content');
130+
131+
requestStream._write = (data, enc, next) => {
132+
if (data && data.streamingConfig !== CONFIG) {
133+
assert.deepStrictEqual(data, { audioContent });
134+
setImmediate(done);
135+
}
136+
137+
next(null, data);
138+
};
139+
140+
userStream.end(audioContent);
52141
});
53142
});
54143
});

0 commit comments

Comments
 (0)