forked from watson-developer-cloud/node-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspeech-helpers.test.js
126 lines (107 loc) · 4.38 KB
/
speech-helpers.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
'use strict';
const { BasicAuthenticator } = require('ibm-cloud-sdk-core');
const isStream = require('isstream');
const websocket = require('websocket');
const SpeechToTextV1 = require('../../dist/speech-to-text/v1');
const TextToSpeechV1 = require('../../dist/text-to-speech/v1');
const serviceUrl = 'http://ibm.com:80';
const version = 'v1';
const authenticator = new BasicAuthenticator({
username: 'batman',
password: 'bruce-wayne',
});
const sttService = {
serviceUrl,
version,
authenticator,
};
const ttsService = {
url: serviceUrl,
version,
authenticator,
};
const speechToText = new SpeechToTextV1(sttService);
const textToSpeech = new TextToSpeechV1(ttsService);
const socketSpy = jest.spyOn(websocket, 'w3cwebsocket').mockImplementation(() => {});
describe('speech to text helpers', () => {
describe('recognizeUsingWebSocket()', () => {
it('should return a stream', () => {
expect(isStream(speechToText.recognizeUsingWebSocket())).toBe(true);
});
it('should pass the correct parameters into RecognizeStream', () => {
const stream = speechToText.recognizeUsingWebSocket();
expect(stream.options.serviceUrl).toBe(serviceUrl);
expect(stream.options.headers['User-Agent']).toBeTruthy();
expect(stream.options.headers['X-IBMCloud-SDK-Analytics']).toBe(
'service_name=speech_to_text;service_version=v1;operation_id=recognizeUsingWebSocket;async=true'
);
expect(stream.authenticator).toBeDefined();
});
it('should construct the proper url from all query params', () => {
const stream = speechToText.recognizeUsingWebSocket({
accessToken: 'fake_value',
watsonToken: 'fake_value',
model: 'fake_value',
languageCustomizationId: 'fake_value',
acousticCustomizationId: 'fake_value',
baseModelVersion: 'fake_value',
xWatsonLearningOptOut: true,
xWatsonMetadata: 'customer_id={1234}',
});
// query params get processed here
stream.initialize();
expect(socketSpy).toHaveBeenCalled();
const finalUrl = socketSpy.mock.calls[0][0];
// assert that all service-allowed params are present
expect(finalUrl).toMatch(/access_token/);
expect(finalUrl).toMatch(/watson-token/);
expect(finalUrl).toMatch(/model/);
expect(finalUrl).toMatch(/language_customization_id/);
expect(finalUrl).toMatch(/acoustic_customization_id/);
expect(finalUrl).toMatch(/base_model_version/);
expect(finalUrl).toMatch(/x-watson-learning-opt-out/);
expect(finalUrl).toMatch(/x-watson-metadata/);
socketSpy.mockClear();
});
});
});
describe('text to speech helpers', () => {
describe('synthesizeUsingWebSocket()', () => {
it('should return a stream', () => {
expect(isStream(textToSpeech.synthesizeUsingWebSocket())).toBe(true);
});
it('should pass the correct parameters into RecognizeStream', () => {
const stream = textToSpeech.synthesizeUsingWebSocket();
expect(stream.options.serviceUrl).toBe(serviceUrl);
expect(stream.options.headers.authorization).toBeUndefined();
expect(stream.options.headers['User-Agent']).toBeTruthy();
expect(stream.options.headers['X-IBMCloud-SDK-Analytics']).toBe(
'service_name=text_to_speech;service_version=v1;operation_id=synthesizeUsingWebSocket;async=true'
);
expect(stream.authenticator).toBeDefined();
});
it('should construct the proper url from all query params', () => {
const socketSpy = jest.spyOn(websocket, 'w3cwebsocket').mockImplementation(() => {});
const stream = textToSpeech.synthesizeUsingWebSocket({
accessToken: 'fake_value',
watsonToken: 'fake_value',
voice: 'fake_value',
customizationId: 'fake_value',
xWatsonLearningOptOut: true,
xWatsonMetadata: 'customer_id={1234}',
});
// query params get processed here
stream.initialize();
expect(socketSpy).toHaveBeenCalled();
const finalUrl = socketSpy.mock.calls[0][0];
// assert that all service-allowed params are present
expect(finalUrl).toMatch(/access_token/);
expect(finalUrl).toMatch(/watson-token/);
expect(finalUrl).toMatch(/voice/);
expect(finalUrl).toMatch(/customization_id/);
expect(finalUrl).toMatch(/x-watson-learning-opt-out/);
expect(finalUrl).toMatch(/x-watson-metadata/);
socketSpy.mockClear();
});
});
});