-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.spec.ts
176 lines (153 loc) · 5.25 KB
/
index.spec.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
import {
Alexam,
AlexamBuilder,
LambdaHandler,
Session,
Context,
User,
SkillContext,
} from "../../../lib/";
import { handler } from "../src";
import { ui } from "ask-sdk-model";
test("LaunchRequest", async () => {
expect.assertions(1);
const handlerObj = new LambdaHandler(handler);
const alexam: Alexam = new AlexamBuilder().setHandler(handlerObj).build();
const launchRequest = alexam.requestFactory.launchRequest();
await alexam.send(launchRequest).then(res => {
expect((res.response.outputSpeech as ui.SsmlOutputSpeech).ssml).toMatch(
"Welcome to the Alexa Skills Kit, you can say hello!",
);
});
});
test("IntentRequest", async () => {
expect.assertions(1);
const handlerObj = new LambdaHandler(handler);
const alexam: Alexam = new AlexamBuilder().setHandler(handlerObj).build();
const helloWorldIntent =
alexam.requestFactory.intentRequest("HelloWorldIntent");
return alexam.send(helloWorldIntent).then(res => {
expect((res.response.outputSpeech as ui.SsmlOutputSpeech).ssml).toMatch(
"Hello world!!",
);
});
});
test("Retain session attributes", async () => {
expect.assertions(1);
const handlerObj = new LambdaHandler(handler);
const alexam: Alexam = new AlexamBuilder().setHandler(handlerObj).build();
const requestFactory = alexam.requestFactory;
const countUpIntent = requestFactory.intentRequest("CountUpIntent");
return alexam
.send(countUpIntent)
.then(() => {
const countUpIntent2 = requestFactory.intentRequest("CountUpIntent");
return alexam.send(countUpIntent2);
})
.then(res => {
expect(res.sessionAttributes?.count).toBe(2);
});
});
test("Simulate request from display device", async () => {
expect.assertions(1);
const handlerObj = new LambdaHandler(handler);
const skillContext = new SkillContext();
skillContext.setDisplay();
const alexam: Alexam = new AlexamBuilder()
.setHandler(handlerObj)
.setSkillContext(skillContext)
.build();
const requestFactory = alexam.requestFactory;
const displayDeviceIntent = requestFactory.intentRequest(
"DisplayDeviceIntent",
);
return alexam.send(displayDeviceIntent).then(res => {
expect((res.response.outputSpeech as ui.SsmlOutputSpeech).ssml).toMatch(
"Request from display device",
);
});
});
test("Simulate request of Alexa.Presentation.APL.UserEvent", async () => {
expect.assertions(1);
const handlerObj = new LambdaHandler(handler);
const skillContext = new SkillContext();
skillContext.setDisplay();
const alexam: Alexam = new AlexamBuilder()
.setHandler(handlerObj)
.setSkillContext(skillContext)
.build();
const requestFactory = alexam.requestFactory;
const aplUserEventRequest = requestFactory.aplUserEventRequest({});
return alexam.send(aplUserEventRequest).then(res => {
expect((res.response.outputSpeech as ui.SsmlOutputSpeech).ssml).toMatch(
"Request from UserEvent",
);
});
});
test("Use pre defined session attributes", async () => {
const handlerObj = new LambdaHandler(handler);
const skillContext = new SkillContext();
skillContext.setSession(
new Session({
applicationId: skillContext.applicationId,
attributes: { count: 10 },
}),
);
const alexam: Alexam = new AlexamBuilder()
.setHandler(handlerObj)
.setSkillContext(skillContext)
.build();
const countUpIntent = alexam.requestFactory.intentRequest("CountUpIntent");
const resp = await alexam.send(countUpIntent);
expect(resp.sessionAttributes?.count).toBe(11);
});
test("Test with account linked user", async () => {
const handlerObj = new LambdaHandler(handler);
const user = new User();
user.linkAccount();
const skillContext = new SkillContext();
skillContext.setContext(new Context({ user }));
const alexam: Alexam = new AlexamBuilder()
.setHandler(handlerObj)
.setSkillContext(skillContext)
.build();
const requestFactory = alexam.requestFactory;
const launchRequest = requestFactory.launchRequest();
const resp = await alexam.send(launchRequest);
expect((resp.response.outputSpeech as ui.SsmlOutputSpeech).ssml).toMatch(
"Request from account linked user",
);
});
describe("Multiple test cases with one alexam object for demonstrating resetSession", () => {
const alexam = new AlexamBuilder()
.setHandler(new LambdaHandler(handler))
.build();
afterEach(() => {
alexam.resetSession();
});
test("Retain session attributes", async () => {
expect.assertions(1);
const requestFactory = alexam.requestFactory;
const countUpIntent = requestFactory.intentRequest("CountUpIntent");
return alexam
.send(countUpIntent)
.then(() => {
const countUpIntent2 = requestFactory.intentRequest("CountUpIntent");
return alexam.send(countUpIntent2);
})
.then(res => {
expect(res.sessionAttributes?.count).toBe(2);
});
});
test("Use pre defined session attributes", async () => {
alexam.skillContext.setSession(
new Session({
applicationId: alexam.skillContext.applicationId,
attributes: { count: 10 },
}),
);
const countUpIntent = alexam.requestFactory.intentRequest("CountUpIntent");
const resp = await alexam.send(countUpIntent);
expect(resp.sessionAttributes?.count).toBe(11);
});
});