-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex-test.js
126 lines (85 loc) · 4.76 KB
/
index-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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import fs from 'fs';
import test from 'ava';
import LambdaTester from 'lambda-tester';
import fetchMock from 'fetch-mock';
import { handler, flashBriefingHandler } from '../index.js';
test.serial('flashBriefingHandler', async t => {
t.plan(3);
fetchMock.once(process.env.MTA_STATUS_URL, fs.readFileSync(process.cwd() + '/tests/fixtures/mta-status.xml', 'utf-8'));
const result = await LambdaTester(flashBriefingHandler)
.event({})
.expectResult(r => r);
let response = JSON.parse(result.body);
t.is(response.titleText, "Current NYC Subway Status");
t.true(!!response.mainText.match("The A-C-E line is experiencing delays. "));
t.true(!!response.mainText.match("Good service on all other lines. "));
});
test.serial('handling "ask subway status to check on the <subway-line> line?" and the service is not good', async t => {
t.plan(3);
const statusOfLineEvent = JSON.parse(fs.readFileSync(process.cwd() + '/tests/fixtures/events/status-of-line.json'));
fetchMock.once(process.env.MTA_STATUS_URL, fs.readFileSync(process.cwd() + '/tests/fixtures/mta-status.xml', 'utf-8'));
const result = await LambdaTester(handler)
.event(statusOfLineEvent)
.expectSucceed(r => r);
t.is(result.response.outputSpeech.ssml, `<speak> The A-C-E line is experiencing delays. I've added a card with the details on the Alexa App. </speak>`);
t.is(result.response.card.title, 'Subway Status for ACE');
t.true(/Due to an earlier incident at 23 St/.test(result.response.card.content));
fetchMock.restore();
});
test.serial('handling "ask subway status to check on the <subway-line> line?" and the service the service is good', async t => {
t.plan(2);
const statusOfLineEvent = JSON.parse(fs.readFileSync(process.cwd() + '/tests/fixtures/events/status-of-line.json'));
fetchMock.once(process.env.MTA_STATUS_URL, fs.readFileSync(process.cwd() + '/tests/fixtures/mta-status.xml', 'utf-8'));
statusOfLineEvent.request.intent.slots.subwayLineOrGroup.value = '456'; // Fixture has good service on 456
const result = await LambdaTester(handler)
.event(statusOfLineEvent)
.expectSucceed(r => r);
t.is(result.response.outputSpeech.ssml, '<speak> Good service on the 4-5-6 line. </speak>');
t.is(result.response.card, undefined);
fetchMock.restore();
});
test.serial('handling "ask subway status to check on the <subway-line> line?" without a valid slot value', async t => {
t.plan(1);
const statusOfLineEvent = JSON.parse(fs.readFileSync(process.cwd() + '/tests/fixtures/events/bad-status-of-line-without-value.json'));
fetchMock.once(process.env.MTA_STATUS_URL, fs.readFileSync(process.cwd() + '/tests/fixtures/mta-status.xml', 'utf-8'));
const result = await LambdaTester(handler)
.event(statusOfLineEvent)
.expectSucceed(r => r);
t.is(result.response.outputSpeech.ssml, "<speak> Sorry, I didn't hear a subway line I understand </speak>");
fetchMock.restore();
});
test.serial('handling "ask subway status for an update and there are bad services"', async t => {
t.plan(3);
const fullServiceUpdateEvent = JSON.parse(fs.readFileSync(process.cwd() + '/tests/fixtures/events/full-service-update.json'));
fetchMock.once(process.env.MTA_STATUS_URL, fs.readFileSync(process.cwd() + '/tests/fixtures/mta-status.xml', 'utf-8'));
const result = await LambdaTester(handler)
.event(fullServiceUpdateEvent)
.expectSucceed(r => r);
let speechMarkup = result.response.outputSpeech.ssml;
t.true(speechMarkup.search("1-2-3, B-D-F-M, J-Z, N-Q-R") !== -1);
t.true(speechMarkup.search('A-C-E') !== -1);
t.true(speechMarkup.search('Good service on all other lines. ') !== -1);
fetchMock.restore();
});
test.serial('handling "ask subway status for an update and all services are good"', async t => {
t.plan(1);
const fullServiceUpdateEvent = JSON.parse(fs.readFileSync(process.cwd() + '/tests/fixtures/events/full-service-update.json'));
fetchMock.get(process.env.MTA_STATUS_URL, fs.readFileSync(process.cwd() + '/tests/fixtures/mta-status-all-good.xml', 'utf-8'));
const result = await LambdaTester(handler)
.event(fullServiceUpdateEvent)
.expectSucceed(r => r);
let speechMarkup = result.response.outputSpeech.ssml;
t.true(/Good service on all lines/.test(speechMarkup));
fetchMock.restore();
});
test.serial('handling "open subway status"', async t => {
t.plan(1);
const launchRequst = JSON.parse(fs.readFileSync(process.cwd() + '/tests/fixtures/events/launch-request.json'));
fetchMock.get(process.env.MTA_STATUS_URL, fs.readFileSync(process.cwd() + '/tests/fixtures/mta-status-all-good.xml', 'utf-8'));
const result = await LambdaTester(handler)
.event(launchRequst)
.expectSucceed(r => r);
let speechMarkup = result.response.outputSpeech.ssml;
t.true(/Good service on all lines/.test(speechMarkup));
fetchMock.restore();
});