-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoptions_test.go
More file actions
170 lines (149 loc) · 4.7 KB
/
options_test.go
File metadata and controls
170 lines (149 loc) · 4.7 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
package maxigobot
import (
"testing"
"time"
maxigo "github.com/maxigo-bot/maxigo-client"
)
func TestBuildSendConfig_defaults(t *testing.T) {
cfg := buildSendConfig(nil)
if cfg.ReplyTo != "" {
t.Error("ReplyTo should be empty by default")
}
if cfg.Notify != nil {
t.Error("Notify should be nil by default")
}
if cfg.Format != nil {
t.Error("Format should be nil by default")
}
}
func TestBuildSendConfig_allOptions(t *testing.T) {
cfg := buildSendConfig([]SendOption{
WithReplyTo("mid123"),
WithNotify(false),
WithFormat(maxigo.FormatMarkdown),
WithDisableLinkPreview(),
WithAttachments(maxigo.NewLocationAttachment(55.0, 37.0)),
})
if cfg.ReplyTo != "mid123" {
t.Errorf("ReplyTo = %q, want %q", cfg.ReplyTo, "mid123")
}
if cfg.Notify == nil || *cfg.Notify != false {
t.Error("Notify should be false")
}
if cfg.Format == nil || *cfg.Format != maxigo.FormatMarkdown {
t.Error("Format should be markdown")
}
if !cfg.DisableLinkPreview {
t.Error("DisableLinkPreview should be true")
}
if len(cfg.Attachments) != 1 {
t.Errorf("Attachments count = %d, want 1", len(cfg.Attachments))
}
}
func TestToMessageBody(t *testing.T) {
cfg := sendConfig{
ReplyTo: "mid456",
Format: ptr(maxigo.FormatHTML),
}
body := toMessageBody("hello", cfg)
if !body.Text.Set || body.Text.Value != "hello" {
t.Error("Text should be 'hello'")
}
if body.Link == nil {
t.Fatal("Link should not be nil")
}
if body.Link.Type != maxigo.LinkReply {
t.Errorf("Link.Type = %q, want %q", body.Link.Type, maxigo.LinkReply)
}
if body.Link.MID != "mid456" {
t.Errorf("Link.MID = %q, want %q", body.Link.MID, "mid456")
}
if !body.Format.Set || body.Format.Value != maxigo.FormatHTML {
t.Error("Format should be html")
}
}
func TestToMessageBody_noReply(t *testing.T) {
body := toMessageBody("test", sendConfig{})
if body.Link != nil {
t.Error("Link should be nil when no reply")
}
}
func TestWithKeyboard(t *testing.T) {
row := []maxigo.Button{
maxigo.NewCallbackButton("Yes", "yes"),
maxigo.NewCallbackButton("No", "no"),
}
cfg := buildSendConfig([]SendOption{WithKeyboard(row)})
if len(cfg.Attachments) != 1 {
t.Fatalf("Attachments count = %d, want 1", len(cfg.Attachments))
}
if cfg.Attachments[0].Type != "inline_keyboard" {
t.Errorf("Attachment type = %q, want %q", cfg.Attachments[0].Type, "inline_keyboard")
}
}
func TestWithKeyboard_multipleRows(t *testing.T) {
row1 := []maxigo.Button{maxigo.NewCallbackButton("A", "a")}
row2 := []maxigo.Button{maxigo.NewCallbackButton("B", "b")}
cfg := buildSendConfig([]SendOption{WithKeyboard(row1, row2)})
if len(cfg.Attachments) != 1 {
t.Fatalf("Attachments count = %d, want 1", len(cfg.Attachments))
}
kb, ok := cfg.Attachments[0].Payload.(maxigo.Keyboard)
if !ok {
t.Fatalf("Payload is not Keyboard, got %T", cfg.Attachments[0].Payload)
}
if len(kb.Buttons) != 2 {
t.Errorf("Keyboard rows = %d, want 2", len(kb.Buttons))
}
}
func TestWithKeyboard_combinedWithText(t *testing.T) {
row := []maxigo.Button{maxigo.NewCallbackButton("OK", "ok")}
cfg := buildSendConfig([]SendOption{WithKeyboard(row)})
body := toMessageBody("Choose:", cfg)
if !body.Text.Set || body.Text.Value != "Choose:" {
t.Error("Text should be 'Choose:'")
}
if len(body.Attachments) != 1 {
t.Fatalf("Attachments count = %d, want 1", len(body.Attachments))
}
}
func TestWithRateLimitIntervals(t *testing.T) {
b, err := New("token", WithRateLimitIntervals(1*time.Second, 3*time.Second))
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if len(b.retry.rateLimitIntervals) != 2 {
t.Errorf("rateLimitIntervals count = %d, want 2", len(b.retry.rateLimitIntervals))
}
if b.retry.rateLimitIntervals[0] != 1*time.Second {
t.Errorf("rateLimitIntervals[0] = %v, want 1s", b.retry.rateLimitIntervals[0])
}
}
func TestWithRateLimitIntervals_disable(t *testing.T) {
b, err := New("token", WithRateLimitIntervals())
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if len(b.retry.rateLimitIntervals) != 0 {
t.Errorf("rateLimitIntervals should be empty, got %d", len(b.retry.rateLimitIntervals))
}
}
func TestWithUploadRetryIntervals(t *testing.T) {
b, err := New("token", WithUploadRetryIntervals(500*time.Millisecond))
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if len(b.retry.uploadRetryIntervals) != 1 {
t.Errorf("uploadRetryIntervals count = %d, want 1", len(b.retry.uploadRetryIntervals))
}
}
func TestWithUploadRetryIntervals_disable(t *testing.T) {
b, err := New("token", WithUploadRetryIntervals())
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if len(b.retry.uploadRetryIntervals) != 0 {
t.Errorf("uploadRetryIntervals should be empty, got %d", len(b.retry.uploadRetryIntervals))
}
}
func ptr[T any](v T) *T { return &v }