This repository has been archived by the owner on Feb 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
/
bst-virtual-alexa-test.ts
391 lines (338 loc) · 13.6 KB
/
bst-virtual-alexa-test.ts
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
import * as assert from "assert";
import {LambdaServer} from "../../lib/client/lambda-server";
import * as mockery from "mockery";
import * as sinon from "sinon";
import {BSTProcess} from "../../lib/client/bst-config";
let applicationID = "abc";
let globalModule = {
Global: {
initializeCLI: async function () {
},
config: function () {
return {
configuration: {
lambdaDeploy: {},
},
save: function () {
},
applicationID: function() {
return applicationID;
},
updateApplicationID: function (id) {
applicationID = id;
},
loadSession: function () {
return null;
},
saveSession: function () {
},
deleteSession: function () {
},
};
},
running : function() {
let p = new BSTProcess();
p.port = 9999;
return p;
},
version: function () {
return "0.0.0";
},
}
};
describe("BSTVirtualAlexa", async function() {
let alexa = null;
let lambdaServer: LambdaServer = null;
let BSTVirtualAlexa;
before(function () {
BSTVirtualAlexa = require("../../lib/client/bst-virtual-alexa").BSTVirtualAlexa;
});
describe("#start()", function () {
let sandbox: any = null;
beforeEach(function () {
sandbox = sinon.sandbox.create();
mockery.enable({useCleanCache: true});
mockery.warnOnUnregistered(false);
mockery.registerMock("../core/global", globalModule);
BSTVirtualAlexa = require("../../lib/client/bst-virtual-alexa").BSTVirtualAlexa;
});
afterEach(function () {
mockery.deregisterAll();
mockery.disable();
sandbox.restore();
});
describe("BSTAlexa Without Config", function() {
it("#start()", function () {
const speak = new BSTVirtualAlexa("http://localhost:9000",
null,
"test/resources/speechAssets/IntentSchema.json",
"test/resources/speechAssets/SampleUtterances.txt");
speak.start();
assert(true, "Start processed without exceptions");
});
});
it("Start with defaults (intent schema and sample utterances)", function () {
process.chdir("test/resources");
const speak = new BSTVirtualAlexa("http://localhost:9000");
speak.start();
assert(true, "Start processed without exceptions");
process.chdir("../..");
});
it("Start with defaults and non english model", function () {
process.chdir("test/resources/nonUSModel");
const speak = new BSTVirtualAlexa("http://localhost:9000");
speak.start(true);
assert(true, "Start processed without exceptions");
process.chdir("../../..");
});
it("Use provided models even when default is present in folder", function () {
process.chdir("test/resources/allSpeechModels");
const speak = new BSTVirtualAlexa("http://localhost:9000",
null,
"speechAssets/IntentSchema.json",
"speechAssets/SampleUtterances.txt");
speak.start();
assert(true, "Start processed without exceptions");
process.chdir("../../..");
});
it("Start with defaults (intent schema and sample utterances)", function () {
process.chdir("test/resources");
const speak = new BSTVirtualAlexa("http://localhost:9000");
speak.start();
assert(true, "Start processed without exceptions");
process.chdir("../..");
});
it("Start with defaults (interaction model)", function () {
process.chdir("test/resources/interactionModel");
const speak = new BSTVirtualAlexa("http://localhost:9000");
speak.start();
assert(true, "Start processed without exceptions");
process.chdir("../../..");
});
it("Start with defaults (locale change default)", function () {
process.chdir("test/resources/nonUSModel");
const speak = new BSTVirtualAlexa("http://localhost:9000", null, null, null, null, "de-DE");
speak.start();
assert(true, "Start processed without exceptions");
process.chdir("../../..");
});
it("Initializes with specified files", function () {
const speak = new BSTVirtualAlexa("http://localhost:9000",
null,
"test/resources/speechAssets/IntentSchema.json",
"test/resources/speechAssets/SampleUtterances.txt");
speak.start();
assert(true, "Start processed without exceptions");
});
it("Initializes with specified files", function () {
const speak = new BSTVirtualAlexa("http://localhost:9000",
"test/resources/interactionModel/models/en-US.json");
speak.start();
assert(true, "Start processed without exceptions");
});
it("Initializes with application ID", function () {
this.timeout(5000);
const speak = new BSTVirtualAlexa("http://localhost:9000",
null,
"test/resources/speechAssets/IntentSchema.json",
"test/resources/speechAssets/SampleUtterances.txt",
"1234567890J");
speak.start();
assert(globalModule.Global.config().applicationID(), "1234567890J");
});
it("Initializing after setting the application ID initialize with application ID", function () {
this.timeout(5000);
const speak = new BSTVirtualAlexa("http://localhost:9000",
null,
"test/resources/speechAssets/IntentSchema.json",
"test/resources/speechAssets/SampleUtterances.txt");
speak.start();
assert(globalModule.Global.config().applicationID(), "1234567890J");
assert(speak.context().applicationID(), "1234567890J");
});
it("Initializes with error (Intent Schema provided not present)", function (done) {
let errorReceived = false;
sandbox.stub(console, "error", function(data: Buffer) {
if (!errorReceived && data.toString().startsWith("Error loading")) {
errorReceived = true;
}
});
const speak = new BSTVirtualAlexa("http://localhost:9000",
null,
"test/resources/speechAssets/Intent.json",
"test/resources/speechAssets/SampleUtterances.txt");
try {
speak.start();
} catch (error) {
assert(error);
assert.equal(error.message, "Error loading Intent Schema, file not found");
assert(errorReceived);
done();
}
});
it("Initializes with error (Interaction Model provided not present)", function (done) {
let errorReceived = false;
sandbox.stub(console, "error", function(data: Buffer) {
if (!errorReceived && data.toString().startsWith("Error loading")) {
errorReceived = true;
}
});
const speak = new BSTVirtualAlexa("http://localhost:9000",
"test/resources/speechAssets/Intent.json");
try {
speak.start();
} catch (error) {
assert(error);
assert.equal(error.message, "Error loading Interaction Model, file not found");
assert(errorReceived);
done();
}
});
});
describe("Speaks and Intends", function () {
beforeEach(function () {
process.chdir("test/resources");
alexa = new BSTVirtualAlexa("http://localhost:10000");
lambdaServer = new LambdaServer("exampleProject/ExampleLambda.js", 10000);
lambdaServer.start();
alexa.start();
});
afterEach(function (done) {
lambdaServer.stop(function () {
process.chdir("../..");
done();
});
});
describe("#speak()", function () {
it("Speak phrase", function (done) {
alexa.spoken("Hello", function (error: any, response: any) {
assert.equal(response.output, "Well, Hello To You");
done();
});
});
it("Speak non-grammar phrase", function (done) {
alexa.spoken("Dumb", function (error: any, response: any) {
assert(response.output === undefined);
assert(response.success);
assert.equal(response.intent, "Test");
done();
});
});
it("Speak with error", function (done) {
lambdaServer.stop(function () {
lambdaServer = new LambdaServer("exampleProject/ExampleLambda.js", 9000);
lambdaServer.start();
alexa.spoken("Have I Erred", function (error: any, response: any) {
assert(error);
assert.equal(error.message, "connect ECONNREFUSED 127.0.0.1:10000");
assert(!response);
done();
});
});
});
});
describe("#launched()", function() {
it("Launched", function (done) {
alexa.launched(function (error: any, response: any) {
assert(response.launched);
done();
});
});
});
describe("#intended()", function () {
it("Intended successfully", function (done) {
alexa.intended("HelloIntent", null, function (error: any, response: any) {
assert.equal(response.output, "Well, Hello To You");
done();
});
});
it("Intended with bad intent", function (done) {
alexa.intended("Hello", null, function (error, response) {
assert(!response);
assert(error);
done();
});
});
});
});
describe("Manages Session", function () {
let session;
let oldSession;
beforeEach(function () {
process.chdir("test/resources");
globalModule.Global.config = function () {
return {
configuration: {
lambdaDeploy: {},
},
save: function () {
},
applicationID: function() {
return applicationID;
},
updateApplicationID: function (id) {
applicationID = id;
},
loadSession: function () {
oldSession = Object.assign({}, session);
},
saveSession: function () {
session = {
id: "sessionId",
attributes: {
"STATE": "_TEST_STATE",
}
};
},
deleteSession: function () {
session = null;
},
};
};
mockery.enable({useCleanCache: true});
mockery.warnOnUnregistered(false);
mockery.registerMock("../core/global", globalModule);
BSTVirtualAlexa = require("../../lib/client/bst-virtual-alexa").BSTVirtualAlexa;
lambdaServer = new LambdaServer("exampleProject/ExampleLambda.js", 10000);
lambdaServer.start();
});
afterEach(function () {
process.chdir("../..");
lambdaServer.stop();
mockery.deregisterAll();
mockery.disable();
});
it("Saves on launch", function (done) {
alexa = new BSTVirtualAlexa("http://localhost:10000");
alexa.start();
alexa.launched(function (error: any, response: any) {
assert.deepEqual(session, {
id: "sessionId",
attributes: {
"STATE": "_TEST_STATE",
}
});
done();
});
});
it("Loads on intent", function (done) {
alexa = new BSTVirtualAlexa("http://localhost:10000");
alexa.start();
alexa.intended(null, null, function (error: any, response: any) {
assert.deepEqual(oldSession, {
id: "sessionId",
attributes: {
"STATE": "_TEST_STATE",
}
});
done();
});
});
it("Removes on delete", function () {
alexa = new BSTVirtualAlexa("http://localhost:10000");
alexa.start();
alexa.deleteSession();
assert.equal(session, null);
});
});
});