-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmessages_test.go
More file actions
317 lines (283 loc) · 9.08 KB
/
messages_test.go
File metadata and controls
317 lines (283 loc) · 9.08 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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
package maxigo
import (
"context"
"errors"
"io"
"net/http"
"strings"
"testing"
)
func TestSendMessage(t *testing.T) {
t.Run("success", func(t *testing.T) {
c, _ := testClient(t, func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
t.Errorf("method = %q, want POST", r.Method)
}
if r.URL.Path != "/messages" {
t.Errorf("path = %q, want /messages", r.URL.Path)
}
if r.URL.Query().Get("chat_id") != "100" {
t.Errorf("chat_id = %q, want 100", r.URL.Query().Get("chat_id"))
}
var body NewMessageBody
readJSON(t, r, &body)
if !body.Text.Set || body.Text.Value != "Hello!" {
t.Errorf("text = %v, want %q", body.Text, "Hello!")
}
mid := "msg-123"
text := "Hello!"
writeJSON(t, w, sendMessageResult{
Message: Message{
Timestamp: 1234567890,
Body: MessageBody{
MID: mid,
Text: &text,
},
},
})
})
msg, err := c.SendMessage(context.Background(), 100, &NewMessageBody{Text: Some("Hello!")})
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if msg.Body.MID != "msg-123" {
t.Errorf("MID = %q, want %q", msg.Body.MID, "msg-123")
}
})
}
func TestSendMessageToUser(t *testing.T) {
c, _ := testClient(t, func(w http.ResponseWriter, r *http.Request) {
if r.URL.Query().Get("user_id") != "42" {
t.Errorf("user_id = %q, want 42", r.URL.Query().Get("user_id"))
}
writeJSON(t, w, sendMessageResult{
Message: Message{Timestamp: 1},
})
})
_, err := c.SendMessageToUser(context.Background(), 42, &NewMessageBody{Text: Some("hi")})
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
}
func TestSendMessageDisableLinkPreview(t *testing.T) {
c, _ := testClient(t, func(w http.ResponseWriter, r *http.Request) {
if r.URL.Query().Get("disable_link_preview") != "true" {
t.Errorf("disable_link_preview = %q, want true", r.URL.Query().Get("disable_link_preview"))
}
writeJSON(t, w, sendMessageResult{
Message: Message{Timestamp: 1},
})
})
_, err := c.SendMessage(context.Background(), 100, &NewMessageBody{
Text: Some("https://example.com"),
DisableLinkPreview: true,
})
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
}
func TestEditMessage(t *testing.T) {
c, _ := testClient(t, func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPut {
t.Errorf("method = %q, want PUT", r.Method)
}
if r.URL.Query().Get("message_id") != "mid-1" {
t.Errorf("message_id = %q, want mid-1", r.URL.Query().Get("message_id"))
}
writeJSON(t, w, SimpleQueryResult{Success: true})
})
result, err := c.EditMessage(context.Background(), "mid-1", &NewMessageBody{Text: Some("edited")})
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if !result.Success {
t.Error("Success should be true")
}
}
func TestDeleteMessage(t *testing.T) {
c, _ := testClient(t, func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodDelete {
t.Errorf("method = %q, want DELETE", r.Method)
}
if r.URL.Query().Get("message_id") != "mid-2" {
t.Errorf("message_id = %q, want mid-2", r.URL.Query().Get("message_id"))
}
writeJSON(t, w, SimpleQueryResult{Success: true})
})
result, err := c.DeleteMessage(context.Background(), "mid-2")
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if !result.Success {
t.Error("Success should be true")
}
}
func TestGetMessages(t *testing.T) {
c, _ := testClient(t, func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
t.Errorf("method = %q, want GET", r.Method)
}
if r.URL.Query().Get("chat_id") != "200" {
t.Errorf("chat_id = %q, want 200", r.URL.Query().Get("chat_id"))
}
if r.URL.Query().Get("count") != "10" {
t.Errorf("count = %q, want 10", r.URL.Query().Get("count"))
}
text := "Hello"
writeJSON(t, w, MessageList{
Messages: []Message{
{Timestamp: 1, Body: MessageBody{MID: "m1", Text: &text}},
},
})
})
result, err := c.GetMessages(context.Background(), GetMessagesOpts{ChatID: 200, Count: 10})
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if len(result.Messages) != 1 {
t.Fatalf("len(Messages) = %d, want 1", len(result.Messages))
}
if result.Messages[0].Body.MID != "m1" {
t.Errorf("MID = %q, want %q", result.Messages[0].Body.MID, "m1")
}
}
func TestGetMessagesWithIDs(t *testing.T) {
c, _ := testClient(t, func(w http.ResponseWriter, r *http.Request) {
ids := r.URL.Query().Get("message_ids")
if ids != "mid-1,mid-2,mid-3" {
t.Errorf("message_ids = %q, want %q", ids, "mid-1,mid-2,mid-3")
}
writeJSON(t, w, MessageList{Messages: []Message{}})
})
_, err := c.GetMessages(context.Background(), GetMessagesOpts{MessageIDs: []string{"mid-1", "mid-2", "mid-3"}})
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
}
func TestGetMessagesWithTimeRange(t *testing.T) {
c, _ := testClient(t, func(w http.ResponseWriter, r *http.Request) {
if r.URL.Query().Get("from") != "1000" {
t.Errorf("from = %q, want 1000", r.URL.Query().Get("from"))
}
if r.URL.Query().Get("to") != "500" {
t.Errorf("to = %q, want 500", r.URL.Query().Get("to"))
}
writeJSON(t, w, MessageList{Messages: []Message{}})
})
_, err := c.GetMessages(context.Background(), GetMessagesOpts{ChatID: 100, From: 1000, To: 500})
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
}
func TestGetMessageByID(t *testing.T) {
c, _ := testClient(t, func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/messages/mid-99" {
t.Errorf("path = %q, want /messages/mid-99", r.URL.Path)
}
writeJSON(t, w, Message{
Timestamp: 1, Body: MessageBody{MID: "mid-99"},
})
})
msg, err := c.GetMessageByID(context.Background(), "mid-99")
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if msg.Body.MID != "mid-99" {
t.Errorf("MID = %q, want %q", msg.Body.MID, "mid-99")
}
}
func TestAnswerCallbackAttachments(t *testing.T) {
// See: https://dev.max.ru/docs-api/methods/POST/answers
// "attachments: AttachmentRequest[] Nullable — if empty, all attachments will be removed"
t.Run("nil attachments omitted from JSON", func(t *testing.T) {
// When Attachments is nil (not set), the field must be omitted
// so the server keeps existing attachments.
c, _ := testClient(t, func(w http.ResponseWriter, r *http.Request) {
raw := mustReadBody(t, r)
if strings.Contains(raw, "attachments") {
t.Errorf("attachments should be omitted when nil, got: %s", raw)
}
writeJSON(t, w, SimpleQueryResult{Success: true})
})
_, err := c.AnswerCallback(context.Background(), "cb-1", &CallbackAnswer{
Message: &NewMessageBody{
Text: Some("Keep buttons"),
},
})
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
})
t.Run("empty attachments sent as empty array", func(t *testing.T) {
// When Attachments is an empty slice, the JSON must contain
// "attachments":[] so the server removes all attachments (e.g. inline keyboard).
c, _ := testClient(t, func(w http.ResponseWriter, r *http.Request) {
raw := mustReadBody(t, r)
if !strings.Contains(raw, `"attachments":[]`) {
t.Errorf("expected \"attachments\":[] in JSON body, got: %s", raw)
}
writeJSON(t, w, SimpleQueryResult{Success: true})
})
_, err := c.AnswerCallback(context.Background(), "cb-1", &CallbackAnswer{
Message: &NewMessageBody{
Text: Some("Remove buttons"),
Attachments: []AttachmentRequest{},
},
})
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
})
}
// mustReadBody reads and returns the raw request body. Test helper.
func mustReadBody(t *testing.T, r *http.Request) string {
t.Helper()
data, err := io.ReadAll(r.Body)
if err != nil {
t.Fatalf("failed to read body: %v", err)
}
return string(data)
}
func TestAnswerCallback(t *testing.T) {
t.Run("success", func(t *testing.T) {
c, _ := testClient(t, func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
t.Errorf("method = %q, want POST", r.Method)
}
if r.URL.Path != "/answers" {
t.Errorf("path = %q, want /answers", r.URL.Path)
}
if r.URL.Query().Get("callback_id") != "cb-1" {
t.Errorf("callback_id = %q, want cb-1", r.URL.Query().Get("callback_id"))
}
var answer CallbackAnswer
readJSON(t, r, &answer)
if !answer.Notification.Set || answer.Notification.Value != "Done!" {
t.Errorf("Notification = %v, want Done!", answer.Notification)
}
writeJSON(t, w, SimpleQueryResult{Success: true})
})
result, err := c.AnswerCallback(context.Background(), "cb-1", &CallbackAnswer{
Notification: Some("Done!"),
})
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if !result.Success {
t.Error("Success should be true")
}
})
t.Run("error", func(t *testing.T) {
c, _ := testClient(t, func(w http.ResponseWriter, r *http.Request) {
writeError(t, w, http.StatusMethodNotAllowed, `{"code":"method.not.allowed","message":"method not allowed"}`)
})
_, err := c.AnswerCallback(context.Background(), "cb-1", &CallbackAnswer{})
var e *Error
if !errors.As(err, &e) {
t.Fatalf("expected *Error, got %T", err)
}
if e.StatusCode != http.StatusMethodNotAllowed {
t.Errorf("StatusCode = %d, want 405", e.StatusCode)
}
})
}