-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
response_test.go
133 lines (118 loc) · 3.66 KB
/
response_test.go
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
package alexa
import (
"github.com/drpsychick/go-alexa-lambda/ssml"
"github.com/stretchr/testify/assert"
"strings"
"testing"
)
func TestWith_Functions(t *testing.T) {
b := &ResponseBuilder{}
b.WithSpeech("speech")
b.WithSimpleCard("title", "text")
b.WithShouldEndSession(true)
b.WithReprompt("reprompt")
res := b.Build()
assert.Equal(t, "title", res.Response.Card.Title)
assert.Equal(t, "text", res.Response.Card.Content)
assert.Equal(t, "speech", res.Response.OutputSpeech.Text)
assert.Equal(t, "reprompt", res.Response.Reprompt.OutputSpeech.Text)
b.WithSpeech(ssml.Speak("speech"))
b.WithReprompt(ssml.Speak("reprompt"))
res = b.Build()
assert.Equal(t, ssml.Speak("speech"), res.Response.OutputSpeech.SSML)
assert.Equal(t, ssml.Speak("reprompt"), res.Response.Reprompt.OutputSpeech.SSML)
b.WithStandardCard("title", "text", &Image{})
res = b.Build()
assert.Equal(t, "title", res.Response.Card.Title)
assert.Equal(t, "text", res.Response.Card.Text)
assert.Equal(t, &Image{}, res.Response.Card.Image)
assert.Empty(t, res.Response.Card.Content)
}
func TestCanFulfillIntent(t *testing.T) {
b := ResponseBuilder{}
b.WithCanFulfillIntent(&CanFulfillIntent{
CanFulfill: string(TypeLaunchRequest),
Slots: map[string]CanFulfillSlot{},
})
res := b.Build()
assert.Equal(t, string(TypeLaunchRequest), res.Response.CanFulfillIntent.CanFulfill)
}
func TestAddDirective(t *testing.T) {
b := &ResponseBuilder{}
b.AddDirective(&Directive{
Type: DirectiveTypeDialogDelegate,
SlotToElicit: "",
UpdatedIntent: nil,
PlayBehavior: "",
AudioItem: nil,
})
res := b.Build()
assert.Equal(t, DirectiveTypeDialogDelegate, res.Response.Directives[0].Type)
}
func TestSessionAttributes(t *testing.T) {
b := &ResponseBuilder{}
b.WithSessionAttributes(map[string]interface{}{
"foo": "Bar",
})
res := b.Build()
assert.Equal(t, "Bar", res.SessionAttributes["foo"])
}
func TestResponseBuilder_With(t *testing.T) {
type fields struct {
speech *OutputSpeech
card *Card
reprompt *OutputSpeech
directives []*Directive
shouldEndSession bool
sessionAttr map[string]interface{}
canFulfillIntent *CanFulfillIntent
}
type args struct {
resp Response
}
tests := []struct {
name string
fields fields
args args
}{
{"WithEmptyResponse", fields{}, args{}},
{"WithTextResponse", fields{}, args{Response{Title: "title", Text: "text"}}},
{"WithSpeechResponse", fields{}, args{Response{Text: "text", Speech: "<speak>hello</speak>"}}},
{"WithRepromptResponse", fields{}, args{Response{Text: "text", Speech: "<speak>hello</speak>", Reprompt: true}}},
{"WithTextSpeechResponse", fields{}, args{Response{Text: "text", Speech: "hello", Reprompt: true}}},
{"WithEndResponse", fields{}, args{Response{End: true}}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
b := &ResponseBuilder{}
b.With(tt.args.resp)
if tt.args.resp.Title == "" {
assert.Equal(t, "", b.card.Title)
}
if tt.args.resp.Title != "" {
assert.Equal(t, tt.args.resp.Title, b.card.Title)
}
if tt.args.resp.Text != "" {
assert.Equal(t, tt.args.resp.Text, b.card.Content)
}
if tt.args.resp.End {
assert.True(t, b.shouldEndSession)
}
if tt.args.resp.Speech != "" {
if strings.HasPrefix(tt.args.resp.Speech, `<speak>`) {
if tt.args.resp.Reprompt {
assert.Equal(t, tt.args.resp.Speech, b.reprompt.SSML)
} else {
assert.Equal(t, tt.args.resp.Speech, b.speech.SSML)
}
} else {
if tt.args.resp.Reprompt {
assert.Equal(t, tt.args.resp.Speech, b.reprompt.Text)
} else {
assert.Equal(t, tt.args.resp.Speech, b.speech.Text)
}
}
}
})
}
}