-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
state_ready_test.go
80 lines (63 loc) · 2.04 KB
/
state_ready_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
package gateway
import (
"bytes"
"github.com/discordpkg/gateway/event/opcode"
"testing"
)
func NewReadyState(t *testing.T, options ...Option) *ReadyState {
// ensure it's properly setup via the client constructor
client := NewClientMust(t, options...)
client.ctx.SetState(&ReadyState{ctx: client.ctx})
return client.ctx.state.(*ReadyState)
}
func TestReadyState_String(t *testing.T) {
state := &ReadyState{ctx: nil}
got := state.String()
wants := "ready"
if got != wants {
t.Errorf("incorrect state name. Got %s, wants %s", got, wants)
}
}
func TestReadyState_Process(t *testing.T) {
options := append(commonOptions, []Option{}...)
t.Run("unexpected payload", func(t *testing.T) {
state := NewReadyState(t, options...)
// try using a hello payload
payload := &Payload{Op: 10, Data: []byte(`{"heartbeat_interval":45}`)}
buffer := &bytes.Buffer{}
if err := state.Process(payload, buffer); err == nil {
t.Fatal("should have failed")
}
if _, ok := state.ctx.state.(*ClosedState); !ok {
t.Error("state was not closed")
}
})
t.Run("invalid json payload", func(t *testing.T) {
state := NewReadyState(t, options...)
payload := &Payload{Op: opcode.Dispatch, Data: []byte(`{||||||}`)}
buffer := &bytes.Buffer{}
if err := state.Process(payload, buffer); err == nil {
t.Fatal("should have failed due to invalid json syntax")
}
if _, ok := state.ctx.state.(*ClosedState); !ok {
t.Fatal("state was not set to closed")
}
})
t.Run("ok", func(t *testing.T) {
state := NewReadyState(t, options...)
payload := &Payload{Op: 0, Data: []byte(`{"v":10, "session_id": "test", "resume_gateway_url": "test.com"}`)}
buffer := &bytes.Buffer{}
if err := state.Process(payload, buffer); err != nil {
t.Fatal("should properly handle the dispatch payload")
}
if _, ok := state.ctx.state.(*ConnectedState); !ok {
t.Fatal("state was not set to connected")
}
if state.ctx.SessionID == "" {
t.Error("forgot to save session id")
}
if state.ctx.ResumeGatewayURL == "" {
t.Error("forgot to save resume url")
}
})
}