-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathindex.js
946 lines (864 loc) · 33.2 KB
/
index.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
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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
const awsContext = require('aws-lambda-mock-context');
const AssertionError = require('assertion-error');
const AWSMOCK = require('aws-sdk-mock');
const uuid = require('uuid');
const CallbackContext = function (framework, sequenceIndex, locale, requestType) {
'use strict';
this.framework = framework;
this.sequenceIndex = sequenceIndex;
this.locale = locale;
this.requestType = requestType;
};
/**
* Formats text via i18n, using the locale that was used for the request.
*/
CallbackContext.prototype.t = function (keys, params) {
'use strict';
if (!params) {
params = {};
}
if (!params.lng) {
params.lng = this.locale;
}
return this.framework.t(keys, params);
};
/**
* Throws an assertion error.
* @param {object} data Data for the error. Can include:
* `message`: Optionally, a message describing the failure.
* `expected`: Optionally, the expected value.
* `actual`: Optionally, the actual value.
* `operator`: Optionally, the comparison operator that was used.
* `showDiff`: Optionally, true if Mocha should diff the expected and actual values.
*/
CallbackContext.prototype.assert = function (data) {
'use strict';
this.framework._assert(this.sequenceIndex, this.requestType, data);
};
/**
* Performs the questionMarkCheck on the response, and asserts if it fails.
*/
CallbackContext.prototype._questionMarkCheck = function (response) {
'use strict';
var actualSay = response.response.outputSpeech ? response.response.outputSpeech.ssml : undefined;
var hasQuestionMark = false;
for (var i = 0; actualSay && i < actualSay.length; i++) {
var c = actualSay[i];
if (c === '?' || c === '\u055E' || c === '\u061F' || c === '\u2E2E' || c === '\uFF1F') {
hasQuestionMark = true;
break;
}
}
if (response.response.shouldEndSession !== false && hasQuestionMark) {
this.assert(
{
message: "Possible Certification Problem: The response ends the session but contains a question mark."
});
}
if (response.response.shouldEndSession === false && !hasQuestionMark) {
this.assert(
{
message: "Possible Certification Problem: The response keeps the session open but does not contain a question mark."
});
}
};
module.exports = {
locale: "en-US",
version: "1.0",
// lambda mock context options
mockContextOptions: {},
// DynamoDB Mock
dynamoDBTable: null,
partitionKeyName: null,
attributesName: null,
dynamoDBGetMock: null,
dynamoDBPutMock: null,
//TODO: allow these to be enabled or disabled on a per-request basis
extraFeatures: {
/**
* If set, responses that end with question marks must not end the session,
* and those without question marks must end the session.
*/
questionMarkCheck: true,
},
/**
* Initializes necessary values before using the test framework.
* @param {object} index The object containing your skill's 'handler' method.
* @param {string} appId The Skill's app ID. Looks like "amzn1.ask.skill.00000000-0000-0000-0000-000000000000".
* @param {string} userId The Amazon User ID to test with. Looks like "amzn1.ask.account.LONG_STRING"
* @param {string} deviceId Optional The Amazon Device ID to test with. Looks like "amzn1.ask.device.LONG_STRING"
*/
initialize: function (index, appId, userId, deviceId) {
'use strict';
this.index = index;
this.appId = appId;
this.userId = userId;
this.deviceId = deviceId || 'amzn1.ask.device.VOID';
},
/**
* Initializes i18n.
* @param {object} resources The 'resources' object to give to i18n.
*/
initializeI18N: function (resources) {
'use strict';
this.i18n = require('i18next');
var sprintf = require('i18next-sprintf-postprocessor');
this.i18n.use(sprintf).init({
overloadTranslationOptionHandler: sprintf.overloadTranslationOptionHandler,
returnObjects: true,
lng: this.locale,
resources: resources
});
},
/**
* Changes the locale used by i18n and to generate requests.
* @param {string} locale E.g. "en-US"
*/
setLocale: function (locale) {
'use strict';
if (!locale) {
throw "'locale' argument must be provided.";
}
this.locale = locale;
if (this.i18n) {
this.i18n.changeLanguage(this.locale);
}
},
/**
* Set lambda mock context options
* @param {object} aws-lambda-mock-context options
*/
setMockContextOptions: function (opts) {
'use strict';
if (!opts) {
throw "'opts' argument must be provided.";
}
this.mockContextOptions = opts;
},
/**
* Activates mocking of DynamoDB backed attributes
* @param {string} tableName name of the DynamoDB Table
* @param {string} partitionKeyName the key to be used as id (default: userId)
* @param {string} attributesName the key to be used for the attributes (default: mapAttr)
*/
setDynamoDBTable: function (tableName, partitionKeyName, attributesName) {
'use strict';
if (!tableName) {
throw "'tableName' argument must be provided.";
}
this.dynamoDBTable = tableName;
this.partitionKeyName = partitionKeyName || 'userId';
this.attributesName = attributesName || 'mapAttr';
let self = this;
AWSMOCK.mock('DynamoDB.DocumentClient', 'get', (params, callback) => {
// Do not inline; resolution has to take place on call
self.dynamoDBGetMock(params, callback);
});
AWSMOCK.mock('DynamoDB.DocumentClient', 'put', (params, callback) => {
// Do not inline; resolution has to take place on call
self.dynamoDBPutMock(params, callback);
});
},
/**
* Reset the mock on the DynamoDB
*/
unmockDynamoDB: function () {
'use strict';
AWSMOCK.restore('DynamoDB.DocumentClient');
},
/**
* Enables or disables an optional testing feature.
* @param {string} key The key of the feature to enable.
* @param {boolean} enabled Whether the feature should be enabled.
*/
setExtraFeature: function (key, enabled) {
'use strict';
if (this.extraFeatures[key] === undefined) {
throw "Framework has no feature with key '" + key + "'.";
}
this.extraFeatures[key] = !!enabled;
},
/**
* Generates a launch request object.
* @param {string} locale Optional locale to use. If not specified, uses the locale specified by `setLocale`.
*/
getLaunchRequest: function (locale) {
'use strict';
return {
"version": this.version,
"session": this._getSessionData(),
"context": this._getContextData(),
"request": {
"type": "LaunchRequest",
"requestId": "EdwRequestId." + uuid.v4(),
"timestamp": new Date().toISOString(),
"locale": locale || this.locale,
}
};
},
/**
* Generates an intent request object.
* @param {string} intentName The name of the intent to call.
* @param {object} slots Slot data to call the intent with.
* @param {string} locale Optional locale to use. If not specified, uses the locale specified by `setLocale`.
*/
getIntentRequest: function (intentName, slots, locale) {
'use strict';
var requestSlots;
if (!slots) {
requestSlots = {};
}
else {
requestSlots = JSON.parse(JSON.stringify(slots));
for (var key in requestSlots) {
requestSlots[key] = {name: key, value: requestSlots[key]};
}
}
return {
"version": this.version,
"session": this._getSessionData(),
"context": this._getContextData(),
"request": {
"type": "IntentRequest",
"requestId": "EdwRequestId." + uuid.v4(),
"timestamp": new Date().toISOString(),
"locale": locale || this.locale,
"intent": {"name": intentName, "slots": requestSlots}
},
};
},
/**
* Generates a sesson ended request object.
* @param {string} reason The reason the session was ended.
* @param {string} locale Optional locale to use. If not specified, uses the locale specified by `setLocale`.
* @see https://developer.amazon.com/public/solutions/alexa/alexa-skills-kit/docs/custom-standard-request-types-reference#sessionendedrequest
*/
getSessionEndedRequest: function (reason, locale) {
'use strict';
return {
"version": this.version,
"session": this._getSessionData(),
"context": this._getContextData(),
"request": {
"type": "SessionEndedRequest",
"requestId": "EdwRequestId." + uuid.v4(),
"timestamp": new Date().toISOString(),
"locale": locale || this.locale,
"reason": reason
//TODO: support error
}
};
},
/**
* Adds an AudioPlayer context to the given request.
* @param {object} request The intent request to modify.
* @param {string} token An opaque token that represents the audio stream described by this AudioPlayer object. You provide this token when sending the Play directive.
* @param {string} offset Identifies a track’s offset in milliseconds at the time the request was sent. This is 0 if the track is at the beginning.
* @param {string} activity Indicates the last known state of audio playback.
* @return {object} the given intent request to allow chaining.
*/
addAudioPlayerContextToRequest: function (request, token, offset, activity) {
'use strict';
if (!request) {
throw 'request must be specified to add entity resolution';
}
if (token) {
request.context.AudioPlayer.token = token;
}
if (offset) {
request.context.AudioPlayer.offsetInMilliseconds = offset;
}
if (activity) {
request.context.AudioPlayer.playerActivity = activity;
}
return request;
},
/**
* Adds an entity resolution to the given request.
* @param {object} request The intent request to modify.
* @param {string} slotName The name of the slot to add the resolution to. If the slot does not exist it is added.
* @param {string} slotType The type of the slot.
* @param {string} value The value of the slot.
* @param {string} id The id of the resolved entity.
* @return {object} the given intent request to allow chaining.
*/
addEntityResolutionToRequest: function (request, slotName, slotType, value, id) {
'use strict';
if (!request) {
throw 'request must be specified to add entity resolution';
}
if (!slotName) {
throw 'slotName must be specified to add entity resolution';
}
if (!value) {
throw 'value must be specified to add entity resolution';
}
if (!request.request.intent.slots[slotName]) {
request.request.intent.slots[slotName] = {name: slotName, value: value};
}
const authority = "amzn1.er-authority.echo-sdk." + this.appId + "." + slotType;
var valueAdded = false;
if (request.request.intent.slots[slotName].resolutions) {
request.request.intent.slots[slotName].resolutions.resolutionsPerAuthority.forEach(rpa => {
if (!valueAdded && (rpa.authority === authority)) {
rpa.values.push({
"value": {
"name": value,
"id": id
}
});
valueAdded = true;
}
});
} else {
request.request.intent.slots[slotName].resolutions = {
"resolutionsPerAuthority": []
};
}
if (!valueAdded) {
request.request.intent.slots[slotName].resolutions.resolutionsPerAuthority.push({
"authority": authority,
"status": {
"code": "ER_SUCCESS_MATCH"
},
"values": [
{
"value": {
"name": value,
"id": id
}
}
]
});
}
return request;
},
/**
* Adds multiple entity resolutions to the given request.
* @param {object} request The intent request to modify.
* @param {array} resolutions The array containing the resolutions to add
* @return {object} the given intent request to allow chaining.
*/
addEntityResolutionsToRequest: function (request, resolutions) {
'use strict';
let alexaTest = this;
resolutions.forEach(resolution => {
alexaTest.addEntityResolutionToRequest(request, resolution.slotName, resolution.slotType, resolution.value, resolution.id);
});
return request;
},
/**
* Adds an entity resolution with code ER_SUCCESS_NO_MATCH to the given request.
* @param {object} request The intent request to modify.
* @param {string} slotName The name of the slot to add the resolution to. If the slot does not exist it is added.
* @param {string} slotType The type of the slot.
* @param {string} value The value of the slot.
* @return {object} the given intent request to allow chaining.
*/
addEntityResolutionNoMatchToRequest: function (request, slotName, slotType, value) {
'use strict';
if (!request) {
throw 'request must be specified to add entity resolution';
}
if (!slotName) {
throw 'slotName must be specified to add entity resolution';
}
if (!value) {
throw 'value must be specified to add entity resolution';
}
if (!request.request.intent.slots[slotName]) {
request.request.intent.slots[slotName] = {name: slotName, value: value};
}
if (!request.request.intent.slots[slotName].resolutions) {
request.request.intent.slots[slotName].resolutions = {
"resolutionsPerAuthority": []
};
}
request.request.intent.slots[slotName].resolutions.resolutionsPerAuthority.push({
"authority": "amzn1.er-authority.echo-sdk." + this.appId + "." + slotType,
"status": {
"code": "ER_SUCCESS_NO_MATCH"
}
});
return request;
},
/**
* Tests the responses of a sequence of requests to the skill.
* @param {object[]} sequence An array of requests to test. Each element can have these properties:
* `request`: The request to run. Generate these with one of the above `getFooRequest` methods.
* `says`: Optional String or Array of Strings. Tests that the speech output from the request is the string specified.
* `saysLike`: Optional String. Tests that the speech output from the request contains the string specified.
* `saysNothing`: Optional Boolean. If true, tests that the response has no speech output.
* `reprompts`: Optional String or Array of Strings. Tests that the reprompt output from the request is the string specified.
* `repromptsLike`: Optional String. Tests that the reprompt output from the request contains the string specified.
* `repromptsNothing`: Optional Boolean. If true, tests that the response has no reprompt output.
* `shouldEndSession`: Optional Boolean. If true, tests that the response to the request ends or does not end the session.
* `saysCallback`: Optional Function. Recieves the speech from the response as a parameter. You can make custom checks against it using any assertion library you like.
* `callback`: Optional Function. Recieves the response object from the request as a parameter. You can make custom checks against the response using any assertion library you like in here.
* `elicitsSlot`: Optional String. Tests that the response asks Alexa to elicit the given slot.
* `confirmsSlot`: Optional String. Tests that the response asks Alexa to confirm the given slot.
* `confirmsIntent`: Optional Boolean. Tests that the response asks Alexa to confirm the intent.
* `hasAttributes`: Optional Object. Tests that the response contains the given attributes and values. Values can be strings or functions testing the value.
* `hasCardTitle`: Optional String. Tests that the card sent by the response has the title specified.
* `hasCardContent`: Optional String. Tests that the card sent by the response is a simple card and has the content specified.
* `hasCardContentLike`: Optional String. Tests that the card sent by the response is a simple card and contains the content specified.
* `hasCardText`: Optional String. Tests that the card sent by the response is a standard card and has the text specified.
* `hasCardTextLike`: Optional String. Tests that the card sent by the response is a standard card and contains the text specified.
* `hasSmallImageUrlLike`: Optional String. Tests that the card sent by the response is a standard card and has a small image URL containing the string specified.
* `hasLargeImageUrlLike`: Optional String. Tests that the card sent by the response is a standard card and has a large image URL containing the string specified.
* `withStoredAttributes`: Optional Object. The attributes to initialize the handler with. Used with DynamoDB mock. Values can be strings or functions testing the value.
* `withSessionAttributes`: Optional Object. The session attributes to initialize the an intent request with. Values can be strings, booleans, or integers.
* `storesAttributes`: Optional Object. Tests that the given attributes were stored in the DynamoDB.
* `playsStream`: Optional Object. Tests that the AudioPlayer is used to play a stream.
* `stopsStream`: Optional Boolean. Tests that the AudioPlayer is stopped.
* `clearsQueue`: Optional String. Tests that the AudioPlayer clears the queue with the given clear behavior.
* @param {string} testDescription An optional description for the mocha test
*/
test: function (sequence, testDescription) {
'use strict';
if (!this.index) {
throw "The module is not initialized. You must call 'initialize' before calling 'test'.";
}
if (!sequence) {
throw "'sequence' argument must be provided.";
}
var randomSessionId = `SessionId.${uuid.v4()}`;
var index = this.index;
var locale = this.locale;
var self = this;
it(testDescription || "returns the correct responses", function (done) {
var run = function (handler, sequenceIndex, attributes) {
if (sequenceIndex >= sequence.length) {
// all requests were executed
done();
}
else {
var ctx = awsContext(self.mockContextOptions);
var currentItem = sequence[sequenceIndex];
var request = currentItem.request;
request.session.new = sequenceIndex === 0;
if (attributes) {
request.session.attributes = JSON.parse(JSON.stringify(attributes));
} else {
request.session.attributes = {};
}
request.session.sessionId = randomSessionId;
var callback = function (err, result) {
if (err) {
return ctx.fail(err);
}
return ctx.succeed(result);
};
// adds values from withSessionAttributes to the session
if (currentItem.withSessionAttributes) {
var session = request.session.attributes;
for (var newAttribute in currentItem.withSessionAttributes) {
session[newAttribute] = currentItem.withSessionAttributes[newAttribute];
}
}
var requestType = request.request.type;
if (requestType === "IntentRequest") {
requestType = request.request.intent.name;
}
var context = new CallbackContext(self, sequenceIndex, locale, requestType);
if (self.dynamoDBTable) {
self.dynamoDBGetMock = (params, callback) => {
self._assertStringEqual(context, "TableName", params.TableName, self.dynamoDBTable);
self._assertStringEqual(context, "UserId", params.Key[self.partitionKeyName], self.userId);
const Item = {};
Item[self.partitionKeyName] = self.userId;
Item[self.attributesName] = currentItem.withStoredAttributes || {};
callback(null, {TableName: self.dynamoDBTable, Item});
};
self.dynamoDBPutMock = (params, callback) => {
self._assertStringEqual(context, "TableName", params.TableName, self.dynamoDBTable);
self._assertStringEqual(context, "UserId", params.Item[self.partitionKeyName], self.userId);
let storesAttributes = currentItem.storesAttributes;
if (storesAttributes) {
for (let att in storesAttributes) {
if (storesAttributes.hasOwnProperty(att)) {
const storedAttr = params.Item[self.attributesName][att];
if (typeof storesAttributes[att] === "function") {
if (!storesAttributes[att](storedAttr)) {
context.assert({message: "the stored attribute " + att + " did not contain the correct value. Value was: " + storedAttr});
}
} else {
self._assertStringEqual(context, att, storedAttr, storesAttributes[att]);
}
}
}
}
callback(null, {});
};
}
var result = handler(request, ctx, callback, true);
if (result) {
if (result.then) {
result.then(ctx.succeed, ctx.fail);
} else {
ctx.succeed(result);
}
}
ctx.Promise
.then(response => {
//TODO: null checks
if (response.toJSON) {
response = response.toJSON();
}
var actualSay = response.response && response.response.outputSpeech ? response.response.outputSpeech.ssml : undefined;
var actualReprompt = response.response && response.response.reprompt && response.response.reprompt.outputSpeech ? response.response.reprompt.outputSpeech.ssml : undefined;
// check the returned speech
if (currentItem.says !== undefined) {
self._assertStringPresent(context, 'speech', actualSay);
var trimActualSay = actualSay.substring(7);
trimActualSay = trimActualSay.substring(0, trimActualSay.length - 8).trim();
if (Array.isArray(currentItem.says)) {
self._assertStringOneOf(context, "speech", trimActualSay, currentItem.says);
} else {
self._assertStringEqual(context, "speech", trimActualSay, currentItem.says);
}
}
if (currentItem.saysLike !== undefined) {
self._assertStringContains(context, "speech", actualSay, currentItem.saysLike);
}
if (currentItem.saysNothing) {
self._assertStringMissing(context, "speech", actualSay);
}
if (currentItem.reprompts !== undefined) {
self._assertStringPresent(context, 'reprompt', actualReprompt);
var trimActualReprompt = actualReprompt.substring(7);
trimActualReprompt = trimActualReprompt.substring(0, trimActualReprompt.length - 8).trim();
if (Array.isArray(currentItem.reprompts)) {
self._assertStringOneOf(context, "reprompt", trimActualReprompt, currentItem.reprompts);
} else {
self._assertStringEqual(context, "reprompt", trimActualReprompt, currentItem.reprompts);
}
}
if (currentItem.repromptsLike !== undefined) {
self._assertStringContains(context, "reprompt", actualReprompt, currentItem.repromptsLike);
}
if (currentItem.repromptsNothing) {
self._assertStringMissing(context, "reprompt", actualReprompt);
}
if (currentItem.elicitsSlot) {
let elicitSlotDirective = self._getDirectiveFromResponse(response, 'Dialog.ElicitSlot');
let slot = elicitSlotDirective ? elicitSlotDirective.slotToElicit : '';
self._assertStringEqual(context, "elicitSlot", slot, currentItem.elicitsSlot);
}
if (currentItem.confirmsSlot) {
let confirmSlotDirective = self._getDirectiveFromResponse(response, 'Dialog.ConfirmSlot');
let slot = confirmSlotDirective ? confirmSlotDirective.slotToConfirm : '';
self._assertStringEqual(context, "confirmSlot", slot, currentItem.confirmsSlot);
}
if (currentItem.confirmsIntent) {
let confirmSlotDirective = self._getDirectiveFromResponse(response, 'Dialog.ConfirmIntent');
if (!confirmSlotDirective) {
context.assert({message: "the response did not ask Alexa to confirm the intent"});
}
}
if (currentItem.hasAttributes) {
for (let att in currentItem.hasAttributes) {
if (currentItem.hasAttributes.hasOwnProperty(att)) {
if (typeof currentItem.hasAttributes[att] === "function") {
if (!currentItem.hasAttributes[att](response.sessionAttributes[att])) {
context.assert({message: "the attribute " + att + " did not contain the correct value. Value was: " + response.sessionAttributes[att]});
}
} else {
self._assertStringEqual(context, att, response.sessionAttributes[att], currentItem.hasAttributes[att]);
}
}
}
}
if (currentItem.hasCardTitle) {
if (!response.response.card) {
context.assert({message: "the response did not contain a card"});
} else {
self._assertStringEqual(context, "cardTitle", response.response.card.title, currentItem.hasCardTitle);
}
}
if (currentItem.hasCardContent) {
if (!response.response.card) {
context.assert({message: "the response did not contain a card"});
} else if (response.response.card.type !== "Simple") {
context.assert({ message: "the card in the response was not a simple card" });
} else {
self._assertStringEqual(context, "cardContent", response.response.card.content, currentItem.hasCardContent);
}
}
if (currentItem.hasCardContentLike) {
if (!response.response.card) {
context.assert({ message: "the response did not contain a card" });
} else if (response.response.card.type !== "Simple") {
context.assert({ message: "the card in the response was not a simple card" });
} else {
self._assertStringContains(context, "cardContent", response.response.card.content, currentItem.hasCardContentLike);
}
}
if (currentItem.hasCardText) {
if (!response.response.card) {
context.assert({ message: "the response did not contain a card" });
} else if (response.response.card.type !== "Standard") {
context.assert({ message: "the card in the response was not a standard card" });
} else {
self._assertStringEqual(context, "cardText", response.response.card.text, currentItem.hasCardText);
}
}
if (currentItem.hasCardTextLike) {
if (!response.response.card) {
context.assert({ message: "the response did not contain a card" });
} else if (response.response.card.type !== "Standard") {
context.assert({ message: "the card in the response was not a standard card" });
} else {
self._assertStringContains(context, "cardText", response.response.card.text, currentItem.hasCardTextLike);
}
}
if (currentItem.hasSmallImageUrlLike) {
if (!response.response.card) {
context.assert({ message: "the response did not contain a card" });
} else if (response.response.card.type !== "Standard") {
context.assert({ message: "the card in the response was not a standard card" });
} else if (!response.response.card.image) {
context.assert({ message: "the card in the response did not contain an image" });
} else {
self._assertStringContains(context, "smallImageUrl", response.response.card.image.smallImageUrl, currentItem.hasSmallImageUrlLike);
}
}
if (currentItem.hasLargeImageUrlLike) {
if (!response.response.card) {
context.assert({ message: "the response did not contain a card" });
} else if (response.response.card.type !== "Standard") {
context.assert({ message: "the card in the response was not a standard card" });
} else if (!response.response.card.image) {
context.assert({ message: "the card in the response did not contain an image" });
} else {
self._assertStringContains(context, "largeImageUrl", response.response.card.image.largeImageUrl, currentItem.hasLargeImageUrlLike);
}
}
// check the shouldEndSession flag
if (currentItem.shouldEndSession === true && response.response.shouldEndSession === false) {
context.assert(
{
message: "the response did not end the session",
expected: "the response ends the session",
actual: "the response did not end the session"
});
}
else if (currentItem.shouldEndSession === false && response.response.shouldEndSession !== false) {
context.assert(
{
message: "the response ended the session",
expected: "the response does not end the session",
actual: "the response ended the session"
});
}
checkAudioPlayer(self, context, response, currentItem);
// custom checks
if (currentItem.saysCallback) {
currentItem.saysCallback(context, actualSay);
}
if (currentItem.callback) {
currentItem.callback(context, response);
}
// extra checks
if (self.extraFeatures.questionMarkCheck) {
context._questionMarkCheck(response);
}
run(handler, sequenceIndex + 1, response.sessionAttributes);
})
.catch(done);
}
};
run(index.handler, 0, {});
});
},
/**
* Formats text via i18n.
*/
t: function () {
'use strict';
if (!this.i18n) {
throw "i18n is not initialized. You must call 'initializeI18N' before calling 't'.";
}
return this.i18n.t.apply(this.i18n, arguments);
},
/**
* Internal method. Asserts if the strings are not equal.
*/
_assertStringEqual: function (context, name, actual, expected) {
'use strict';
if (expected !== actual) {
context.assert(
{
message: "the response did not return the correct " + name + " value",
expected: expected, actual: actual ? actual : String(actual),
operator: "==", showDiff: true
});
}
},
/**
* Internal method. Asserts if the string is not part of the array.
*/
_assertStringOneOf: function (context, name, actual, expectedArray) {
'use strict';
for (let i = 0; i < expectedArray.length; i++) {
if (actual === expectedArray[i]) {
return;
}
}
context.assert(
{
message: "the response did not contain a valid speechOutput",
expected: "one of [" + expectedArray.map(text => `"${text}"`).join(', ') + "]",
actual: actual,
operator: "==", showDiff: true
});
},
/**
* Internal method. Asserts if the strings are not equal.
*/
_assertStringContains: function (context, name, actual, substring) {
'use strict';
if (actual.indexOf(substring) < 0) {
context.assert(
{
message: "the response did not contain the correct " + name + " value",
expected: substring, actual: actual ? actual : String(actual),
operator: "LIKE", showDiff: true
});
}
},
/**
* Internal method. Asserts if the string exists.
*/
_assertStringMissing: function (context, name, actual) {
'use strict';
if (actual) {
context.assert(
{
message: "the response unexpectedly returned a " + name + " value",
expected: "undefined", actual: actual ? actual : String(actual),
operator: "==", showDiff: true
});
}
},
/**
* Internal method. Asserts if the string exists.
*/
_assertStringPresent: function (context, name, actual) {
'use strict';
if (!actual) {
context.assert(
{
message: "the response did not return a " + name + " value",
expected: "some value", actual: actual ? actual : String(actual),
operator: "==", showDiff: true
});
}
},
/**
* Internal method.
*/
_assert: function (sequenceIndex, requestType, data) {
'use strict';
var message = "Request #" + (sequenceIndex + 1) + " (" + requestType + ")";
if (data.message) {
message += ": " + data.message;
}
data.message = message;
// the message has information that should be displayed by the test runner
data.generatedMessage = false;
data.name = "AssertionError";
throw new AssertionError(message, data);
},
/**
* Internal method.
*/
_getSessionData: function () {
'use strict';
return {
// randomized for every session and set before calling the handler
"sessionId": "SessionId.00000000-0000-0000-0000-000000000000",
"application": {"applicationId": this.appId},
"attributes": {},
"user": {"userId": this.userId},
"new": true
};
},
/**
* Internal method.
*/
_getContextData: function () {
'use strict';
return {
"System": {
"application": {"applicationId": this.appId},
"user": {"userId": this.userId},
"device": {
"deviceId": this.deviceId,
"supportedInterfaces": {
"AudioPlayer": {}
}
},
"apiEndpoint": "https://api.amazonalexa.com/"
},
"AudioPlayer": {
"playerActivity": "IDLE"
}
};
},
/**
* Internal method.
*/
_getDirectiveFromResponse: function (response, type) {
'use strict';
let directives = response.response.directives;
if (directives) {
for (let i = 0; i < directives.length; i++) {
if (directives[i].type === type) {
return directives[i];
}
}
}
return undefined;
}
};
const checkAudioPlayer = (self, context, response, currentItem) => {
'use strict';
if (currentItem.playsStream) {
let playDirective = self._getDirectiveFromResponse(response, 'AudioPlayer.Play');
if (!playDirective) {
context.assert({message: "the response did not play a stream"});
return;
}
let playConfig = currentItem.playsStream;
self._assertStringEqual(context, "playBehavior", playDirective.playBehavior, playConfig.behavior);
let stream = playDirective.audioItem.stream;
if (!stream.url.startsWith('https://')) {
context.assert({message: "the stream URL is not https"});
}
self._assertStringEqual(context, "url", stream.url, playConfig.url);
if (playConfig.token) {
self._assertStringEqual(context, "token", stream.token, playConfig.token);
}
if (playConfig.previousToken) {
self._assertStringEqual(context, "expectedPreviousToken", stream.expectedPreviousToken, playConfig.previousToken);
}
if (playConfig.offset || playConfig.offset === 0) {
self._assertStringEqual(context, "offsetInMilliseconds", stream.offsetInMilliseconds.toString(), playConfig.offset.toString());
}
}
if (currentItem.stopsStream) {
if (!self._getDirectiveFromResponse(response, 'AudioPlayer.Stop')) {
context.assert({message: "the response did not stop the stream"});
return;
}
}
if (currentItem.clearsQueue) {
let clearDirective = self._getDirectiveFromResponse(response, 'AudioPlayer.ClearQueue');
if (!clearDirective) {
context.assert({message: "the response did not clear the audio queue"});
return;
}
self._assertStringEqual(context, "clearBehavior", clearDirective.clearBehavior, currentItem.clearsQueue);
}
};