-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhome_test.go
92 lines (70 loc) · 3.6 KB
/
home_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
package ghome
import (
"encoding/json"
"errors"
"net/http"
"testing"
"google.golang.org/api/dialogflow/v2"
)
const (
requestJSON = `{"responseId":"CLZizlVv7g","queryResult":{"queryText":"GOOGLE_ASSISTANT_WELCOME","action":"location","parameters":{"hello":"world"},"allRequiredParamsPresent":true,"outputContexts":[{"name":"projects/fake-CLZizlVv7g/agent/sessions/CLZizlVv7g/contexts/google_assistant_welcome"},{"name":"projects/fake-CLZizlVv7g/agent/sessions/CLZizlVv7g/contexts/actions_capability_screen_output"},{"name":"projects/fake-CLZizlVv7g/agent/sessions/CLZizlVv7g/contexts/actions_capability_audio_output"},{"name":"projects/fake-CLZizlVv7g/agent/sessions/CLZizlVv7g/contexts/google_assistant_input_type_keyboard"},{"name":"projects/fake-CLZizlVv7g/agent/sessions/CLZizlVv7g/contexts/actions_capability_media_response_audio"},{"name":"projects/fake-CLZizlVv7g/agent/sessions/CLZizlVv7g/contexts/actions_capability_web_browser"}],"intent":{"name":"projects/fake-CLZizlVv7g/agent/intents/CLZizlVv7g","displayName":"Welcome"},"intentDetectionConfidence":1,"diagnosticInfo":{},"languageCode":"en-us"},"originalDetectIntentRequest":{"source":"google","version":"2","payload":{"isInSandbox":true,"surface":{"capabilities":[{"name":"actions.capability.MEDIA_RESPONSE_AUDIO"},{"name":"actions.capability.SCREEN_OUTPUT"},{"name":"actions.capability.AUDIO_OUTPUT"},{"name":"actions.capability.WEB_BROWSER"}]},"inputs":[{"rawInputs":[{"query":"Talk to my test app","inputType":"KEYBOARD"}],"intent":"actions.intent.MAIN"}],"user":{"lastSeen":"2018-04-14T19:23:26Z","locale":"en-US","userId":"fakeCLZizlVv7g--CLZizlVv7g_CLZizlVv7g"},"conversation":{"conversationId":"1111111111","type":"NEW"},"availableSurfaces":[{"capabilities":[{"name":"actions.capability.SCREEN_OUTPUT"},{"name":"actions.capability.AUDIO_OUTPUT"}]}]}},"session":"projects/fake-CLZizlVv7g/agent/sessions/CLZizlVv7g"}`
)
func createRequest(t *testing.T) *dialogflow.GoogleCloudDialogflowV2WebhookRequest {
var req *dialogflow.GoogleCloudDialogflowV2WebhookRequest
if err := json.Unmarshal([]byte(requestJSON), &req); err != nil {
t.Errorf("Expected nil got error: %v", err)
}
return req
}
func TestApp(t *testing.T) {
app := NewApp()
app.OnIntent(func(w ResponseWriter, r *Request) error {
w.WriteSpeech("Hello, world!")
return nil
})
req, err := NewRequest(createRequest(t))
if err != nil {
t.Errorf("Expected nil got error: %v", err)
}
w, err := app.Process(req)
if err != nil {
t.Errorf("Expected nil got error: %v", err)
}
if w.FulfillmentText != "Hello, world!" {
t.Errorf("Expected 'Hello, world!' got %s", w.FulfillmentText)
}
}
func TestAppVerifyRequest(t *testing.T) {
app := NewApp()
app.VerifyRequest(func(r *http.Request) error {
return errors.New("Bad request")
})
if err := app.verifyRequest(nil); err == nil {
t.Errorf("Expected error got: %v", err)
}
}
func TestAppRequest(t *testing.T) {
app := NewApp()
app.OnIntent(func(w ResponseWriter, r *Request) error {
if r.IntentName() != "Welcome" {
t.Errorf("Expected 'Welcome' got %s", r.IntentName())
}
if r.Action() != "location" {
t.Errorf("Expected 'location' got %s", r.Action())
}
if r.Parameters()["hello"] != "world" {
t.Errorf("Expected 'world' got %s", r.Parameters()["hello"])
}
if r.Inputs()[0].RawInputs[0].Query != "Talk to my test app" {
t.Errorf("Expected 'Talk to my test app' got %s", r.Inputs()[0].RawInputs[0].Query)
}
return nil
})
req, err := NewRequest(createRequest(t))
if err != nil {
t.Errorf("Expected nil got error: %v", err)
}
if _, err := app.Process(req); err != nil {
t.Errorf("Expected nil got error: %v", err)
}
}