-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.py
More file actions
187 lines (153 loc) · 6.39 KB
/
test.py
File metadata and controls
187 lines (153 loc) · 6.39 KB
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
# import json
import main
import uuid
from datetime import datetime
from config import settings
context = {}
def new_request(name, slots={}):
is_intent = bool(name.endswith('Intent') or slots)
app_id = f"amzn1.echo-sdk-ams.app.{uuid.uuid1()}"
user_id = f"amzn1.ask.account.{uuid.uuid1()}"
session = {
"sessionId": f"amzn1.echo-api.session.{uuid.uuid1()}",
"application": {
"applicationId": app_id
},
"user": {
"userId": user_id
},
"new": False,
}
request = {
"requestId": f"amzn1.echo-api.request.{uuid.uuid1()}",
"timestamp": f"{datetime.now().replace(second=0, microsecond=0).isoformat()}",
"locale": "en-US",
"type": "IntentRequest" if is_intent else name,
}
context = {
"System": {
"device": {
"deviceId": "string",
"supportedInterfaces": {
"AudioPlayer": {}
}
},
"application": {
"applicationId": app_id
},
"user": {
"userId": user_id,
"accessToken": "Atza|AAAAAAAA...",
"permissions": {
"consentToken": "ZZZZZZZ..."
}
},
"apiEndpoint": "https://api.amazonalexa.com",
"apiAccessToken": "AxThk..."
},
# "AudioPlayer": {
# "playerActivity": "PLAYING",
# "token": "audioplayer-token",
# "offsetInMilliseconds": 0
# }
}
req_env = {
"session": session,
"request": request,
"context": context,
}
if is_intent:
req_env['request']['intent'] = {
'name': name,
'slots': {k: {'name': k, 'value': v} for k, v in slots.items()}
}
return req_env
def test_default():
response = main.handler(new_request('NotAThingIntent'), context={})
assert "Sorry, I didn't get that" in response['response']['outputSpeech']['ssml']
assert "Please say it again" in response['response']['reprompt']['outputSpeech']['ssml']
assert response['response']['shouldEndSession'] is False
def test_launch():
response = main.handler(new_request('LaunchRequest'), context={})
assert 'Welcome to Even Better Button Trivia.' in response['response']['outputSpeech']['ssml']
assert 'How many players' in response['response']['reprompt']['outputSpeech']['ssml']
assert response['sessionAttributes'] == {}
assert response['response']['directives'][0]['type'] == 'GadgetController.SetLight'
def test_relaunch():
request = new_request('LaunchRequest')
request['session']['attributes'] = {
'player_count': 2,
'current_question': 2,
}
response = main.handler(request, context={})
output = 'It looks like you have a 2 player'
assert output in response['response']['outputSpeech']['ssml']
assert 'Would you like to resume' in response['response']['reprompt']['outputSpeech']['ssml']
assert response['response']['directives'][0]['type'] == 'GadgetController.SetLight'
assert response['sessionAttributes'] == {}
def test_player_count_invalid():
request = new_request('PlayerCount', {'players': 100})
request['session']['attributes'] = {'STATE': settings.STATES['start_game']}
response = main.handler(request, context={})
assert 'between 1 and 4' in response['response']['outputSpeech']['ssml']
assert 'between 1 and 4' in response['response']['reprompt']['outputSpeech']['ssml']
assert response['sessionAttributes'] == {
'player_count': 100,
'STATE': settings.STATES['start_game'],
}
def test_player_count():
request = new_request('PlayerCount', {'players': 1})
request['session']['attributes'] = {'STATE': settings.STATES['start_game']}
response = main.handler(request, context={})
assert 'audio src=' in response['response']['outputSpeech']['ssml']
assert 'Fantastic! Are you ready' in response['response']['outputSpeech']['ssml']
assert 'Ready to start' in response['response']['reprompt']['outputSpeech']['ssml']
assert response['sessionAttributes'] == {
'STATE': settings.STATES['buttonless_game'],
'player_count': 1,
}
def test_start_yes_invalid():
request = new_request('AMAZON.YesIntent')
request['session']['attributes'] = {
'player_count': 99,
'STATE': settings.STATES['start_game'],
}
response = main.handler(request, context={})
assert 'How many players' in response['response']['outputSpeech']['ssml']
assert 'How many players' in response['response']['reprompt']['outputSpeech']['ssml']
assert response['sessionAttributes'] == {
'STATE': settings.STATES['start_game'],
'player_count': 99,
}
def test_start_yes_valid_one():
request = new_request('AMAZON.YesIntent')
request['session']['attributes'] = {
'player_count': 1,
'STATE': settings.STATES['start_game'],
}
response = main.handler(request, context={})
assert 'audio src=' in response['response']['outputSpeech']['ssml']
assert 'Fantastic! Are you ready' in response['response']['outputSpeech']['ssml']
assert 'Ready to start' in response['response']['reprompt']['outputSpeech']['ssml']
assert response['sessionAttributes'] == {
'STATE': settings.STATES['buttonless_game'],
'player_count': 1,
}
def test_start_yes_valid_four():
request = new_request('AMAZON.YesIntent')
request['session']['attributes'] = {
'player_count': 4,
'STATE': settings.STATES['start_game'],
}
response = main.handler(request, context={})
assert 'Ok. Players, press your buttons' in response['response']['outputSpeech']['ssml']
assert response['response']['directives'][0]['type'] == 'GameEngine.StartInputHandler'
assert response['response']['directives'][1]['type'] == 'GadgetController.SetLight'
assert response['response']['directives'][2]['type'] == 'GadgetController.SetLight'
assert 'input_handler_id' in response['sessionAttributes']
assert response['sessionAttributes']['STATE'] == settings.STATES['rollcall']
assert response['sessionAttributes']['player_count'] == 4
assert response['sessionAttributes']['buttons'] == []
def test_end_session():
response = main.handler(new_request('SessionEndedRequest'), context={})
assert response['response'] == {'shouldEndSession': True}