forked from microsoft/BotFramework-WebChat
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreateCognitiveServicesSpeechServicesPonyfillFactory.spec.js
More file actions
205 lines (166 loc) · 6.31 KB
/
createCognitiveServicesSpeechServicesPonyfillFactory.spec.js
File metadata and controls
205 lines (166 loc) · 6.31 KB
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/**
* @jest-environment @happy-dom/jest-environment
* @jest-environment-options { "customExportConditions": ["node"] }
*
* "uuid" resolved by jest-environment-jsdom use Web Crypto API.
* However, "jsdom" does not support Web Crypto API. Thus, we need to import packages as Node.js instead.
*/
/* eslint-disable prefer-destructuring */
/* eslint-disable no-global-assign */
let consoleWarns;
let createCognitiveServicesSpeechServicesPonyfillFactory;
let createSpeechServicesPonyfill;
let originalConsole;
let originalFetch;
let originalWebSocket;
beforeEach(() => {
jest.mock('web-speech-cognitive-services', () => ({
createSpeechServicesPonyfill: jest.fn(() => ({}))
}));
jest.mock('microsoft-cognitiveservices-speech-sdk/distrib/lib/src/common.browser/Exports', () => ({
...jest.requireActual('microsoft-cognitiveservices-speech-sdk/distrib/lib/src/common.browser/Exports'),
PcmRecorder: class MockPcmRecorder {
// eslint-disable-next-line class-methods-use-this, no-empty-function
record() {}
// eslint-disable-next-line class-methods-use-this, no-empty-function
releaseMediaResources() {}
// eslint-disable-next-line class-methods-use-this, no-empty-function
setWorkletUrl() {}
}
}));
originalConsole = console;
consoleWarns = [];
console = {
...console,
warn: text => consoleWarns.push(text)
};
originalFetch = window.fetch;
originalWebSocket = window.WebSocket;
window.fetch = jest.fn();
window.WebSocket = jest.fn();
createSpeechServicesPonyfill = require('web-speech-cognitive-services').createSpeechServicesPonyfill;
createCognitiveServicesSpeechServicesPonyfillFactory =
require('./createCognitiveServicesSpeechServicesPonyfillFactory').default;
window.AudioContext = class MockAudioContext {
// eslint-disable-next-line class-methods-use-this
createMediaStreamSource() {
// eslint-disable-next-line no-empty-function
return { connect: () => {} };
}
// eslint-disable-next-line class-methods-use-this
createScriptProcessor() {
// eslint-disable-next-line no-empty-function
return { connect: () => {} };
}
};
window.navigator.mediaDevices = {
getUserMedia: jest.fn(() => ({
getAudioTracks: () => ['mock-media-stream-track']
}))
};
});
afterEach(() => {
console = originalConsole;
window.fetch = originalFetch;
window.WebSocket = originalWebSocket;
jest.resetModules();
});
test('providing reference grammar ID', () => {
const ponyfillFactory = createCognitiveServicesSpeechServicesPonyfillFactory({
credentials: {
authorizationToken: 'a1b2c3d',
region: 'westus2'
}
});
ponyfillFactory({ referenceGrammarID: 'a1b2c3d' });
const { referenceGrammars } = createSpeechServicesPonyfill.mock.calls[0][0];
expect(referenceGrammars).toEqual(['luis/a1b2c3d-PRODUCTION']);
});
test('not providing reference grammar ID', () => {
const ponyfillFactory = createCognitiveServicesSpeechServicesPonyfillFactory({
credentials: {
authorizationToken: 'a1b2c3d',
region: 'westus2'
}
});
ponyfillFactory({});
const { referenceGrammars } = createSpeechServicesPonyfill.mock.calls[0][0];
expect(referenceGrammars).toEqual([]);
});
test('supplying audioInputDeviceId', async () => {
// GIVEN: Set up Web Speech with "audioInputDeviceId" of "audio-input-device-1".
const ponyfillFactory = createCognitiveServicesSpeechServicesPonyfillFactory({
audioInputDeviceId: 'audio-input-device-1',
credentials: {
authorizationToken: 'a1b2c3d',
region: 'westus2'
}
});
// WHEN: Polyfill is created.
ponyfillFactory({});
// WHEN: Audio source is attached and audio device is opened.
await createSpeechServicesPonyfill.mock.calls[0][0].audioConfig.privSource.attach();
// THEN: It should call getUserMedia() with "audio" constraints of { deviceId: 'audio-input-device-1' }.
expect(window.navigator.mediaDevices.getUserMedia.mock.calls[0][0]).toHaveProperty(
'audio.deviceId',
'audio-input-device-1'
);
// THEN: It should call getUserMedia() with "video" constraint of false.
expect(window.navigator.mediaDevices.getUserMedia.mock.calls[0][0]).toHaveProperty('video', false);
});
test('supplying both audioConfig and audioInputDeviceId', () => {
const audioConfig = {};
const ponyfillFactory = createCognitiveServicesSpeechServicesPonyfillFactory({
audioConfig,
audioInputDeviceId: 'audio-input-device-1',
credentials: {
authorizationToken: 'a1b2c3d',
region: 'westus2'
}
});
ponyfillFactory({});
expect(consoleWarns[0]).toMatchInlineSnapshot(
`"botframework-webchat: \\"audioConfig\\" and \\"audioInputDeviceId\\" cannot be set at the same time; ignoring \\"audioInputDeviceId\\"."`
);
expect(createSpeechServicesPonyfill.mock.calls[0][0].audioConfig).toBe(audioConfig);
});
test('unsupported environment', () => {
window.navigator.mediaDevices = null;
const ponyfillFactory = createCognitiveServicesSpeechServicesPonyfillFactory({
credentials: {
authorizationToken: 'a1b2c3d',
region: 'westus2'
}
});
const ponyfill = ponyfillFactory({});
expect(consoleWarns[0]).toMatchInlineSnapshot(
`"botframework-webchat: Your browser does not support Web Audio or the page is not loaded via HTTPS or localhost. Cognitive Services Speech Services is disabled. However, you may pass a custom AudioConfig to enable speech in this environment."`
);
expect(ponyfill).toEqual({});
});
test('unsupported environment with audioConfig', () => {
window.navigator.mediaDevices = null;
const audioConfig = {};
const ponyfillFactory = createCognitiveServicesSpeechServicesPonyfillFactory({
audioConfig,
credentials: {
authorizationToken: 'a1b2c3d',
region: 'westus2'
}
});
ponyfillFactory({});
expect(consoleWarns).toHaveProperty('length', 0);
expect(createSpeechServicesPonyfill.mock.calls[0][0].audioConfig).toBe(audioConfig);
});
test('should replace window.fetch and window.WebSocket when managedCognitiveService is provided', () => {
const managedCognitiveService = {
hostname: 'example.com',
directlineToken: 'test-token'
};
createCognitiveServicesSpeechServicesPonyfillFactory({
credentials: {},
managedCognitiveService
});
expect(window.fetch).not.toBe(originalFetch);
expect(window.WebSocket).not.toBe(originalWebSocket);
});