-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
107 lines (93 loc) · 2.69 KB
/
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
var gospel = require("./index.js")
const appId = gospel.appId || "no-application-id"
//// Helpers
// Each test case is when(intent, inputEvent).then(validationFunction)
var when = function(intent, slots) {
var event = baseEvent;
if (intent === "LaunchRequest" || intent === "SessionEndedRequest") {
event.request = {type: intent, requestId: "request00"}
} else {
event.request = {type: "IntentRequest", requestId: "request01"
, intent: {name: intent, slots: slots}}
}
return new Promise((resolve, reject) => {
gospel.handler(event,
{fail: (err) => reject(err), succeed: (a) => resolve(a)}
, report)
})
}
var baseEvent = {
session: {
application: {applicationId: appId}
, user: {userId: "mormon1830"}
, new: true }
, request: {type: "LaunchRequest", requestId: "request00"}
}
var report = (err, data) => {
if (err) {console.error("ERR: "+err)}
else {console.log("OUT: "+JSON.stringify(data))}
}
var listResponse =
(reply) => console.log("\n******\n"+JSON.stringify(reply, undefined, 2))
var responseHas = (propName) =>
function(reply) {
if (reply.response[propName] !== undefined) {return Promise.resolve(reply)}
else {
var message = "FAIL: Missing field '"+propName+"' in response."+
"\n" + JSON.stringify(reply, undefined, 2)
return Promise.reject(message)}
}
//// test cases
// New Session, Launch Request
when("LaunchRequest", baseEvent)
.then(responseHas("card"))
.catch(report)
// HelpIntent
when("AMAZON.HelpIntent")
.then(responseHas("card"))
.catch(report)
// PauseIntent
when("AMAZON.PauseIntent")
.then(responseHas("directives"))
.catch(report)
// ResumeIntent
// -- this test system doesn't have AudioPlayer support
when("AMAZON.ResumeIntent")
.then(listResponse)
.catch(report)
// StopIntent
when("AMAZON.StopIntent")
.then(responseHas("card"))
.catch(report)
// CancelIntent
when("AMAZON.CancelIntent")
.then(responseHas("card"))
.catch(report)
// PlayTalk
when("PlayTalk")
.then(responseHas("directives"))
.catch(report)
// ReadScripture
when("ReadScripture", {Book: {name: "Book", value: "Helaman"}})
.then(function(reply) {
if (reply.response.outputSpeech.text === "Reading Helaman") {
return Promise.resolve(reply)
} else {
var message = reply.response.outputSpeech.text+
"\n"+JSON.stringify(reply, undefined, 2)
return Promise.reject(message)
}
})
.catch(report)
// Unhandled Intent
when("NoSuchIntent")
.then(function(reply){
if (reply.response.outputSpeech.text === "Sorry, I can't do that.") {
return Promise.resolve(reply)
} else {
var message = reply.response.outputSpeech.text+
"\n"+JSON.stringify(reply, undefined, 2)
return Promise.reject(message)
}
})
.catch(report)