-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmessage_test.go
More file actions
746 lines (708 loc) · 17.8 KB
/
message_test.go
File metadata and controls
746 lines (708 loc) · 17.8 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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
package feishubot
import (
"encoding/json"
"testing"
"github.com/google/go-cmp/cmp"
)
func TestNewTextMessage(t *testing.T) {
tests := []struct {
name string
text string
want *Message
wantErr bool
}{
{
name: "valid text message",
text: "Hello, world!",
want: &Message{
MsgType: MsgTypeText,
Content: map[string]interface{}{
"text": "Hello, world!",
},
},
wantErr: false,
},
{
name: "empty text message",
text: "",
want: &Message{
MsgType: MsgTypeText,
Content: map[string]interface{}{
"text": "",
},
},
wantErr: false,
},
{
name: "text with @ mention",
text: `<at user_id="ou_xxx">Tom</at> new update`,
want: &Message{
MsgType: MsgTypeText,
Content: map[string]interface{}{
"text": `<at user_id="ou_xxx">Tom</at> new update`,
},
},
wantErr: false,
},
{
name: "text with @ all",
text: `<at user_id="all">所有人</at> announcement`,
want: &Message{
MsgType: MsgTypeText,
Content: map[string]interface{}{
"text": `<at user_id="all">所有人</at> announcement`,
},
},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := NewTextMessage(tt.text)
if !cmp.Equal(got, tt.want) {
t.Errorf("NewTextMessage() diff = %v", cmp.Diff(tt.want, got))
}
if got.MsgType != tt.want.MsgType {
t.Errorf("NewTextMessage() MsgType = %v, want %v", got.MsgType, tt.want.MsgType)
}
if got.Content["text"] != tt.want.Content["text"] {
t.Errorf("NewTextMessage() Content[text] = %v, want %v", got.Content["text"], tt.want.Content["text"])
}
})
}
}
func TestNewPostMessage(t *testing.T) {
tests := []struct {
name string
lang Language
content *PostContent
want *Message
wantErr bool
}{
{
name: "valid post message with title",
lang: LanguageZhCN,
content: NewPostContent(
"Project Update",
NewParagraph(NewTextElement("Project has been updated successfully.")),
),
want: &Message{
MsgType: MsgTypePost,
Content: map[string]interface{}{
"post": map[string]interface{}{
"zh_cn": map[string]interface{}{
"title": "Project Update",
"content": [][]map[string]interface{}{
{
{"tag": "text", "text": "Project has been updated successfully."},
},
},
},
},
},
},
wantErr: false,
},
{
name: "valid post message without title",
lang: LanguageEnUS,
content: NewPostContent(
"",
NewParagraph(NewTextElement("Simple post content")),
),
want: &Message{
MsgType: MsgTypePost,
Content: map[string]interface{}{
"post": map[string]interface{}{
"en_us": map[string]interface{}{
"title": "",
"content": [][]map[string]interface{}{
{
{"tag": "text", "text": "Simple post content"},
},
},
},
},
},
},
wantErr: false,
},
{
name: "post message with link",
lang: LanguageZhCN,
content: NewPostContent(
"Title",
NewParagraph(
NewTextElement("Project has been updated: "),
NewLinkElement("View", "http://www.example.com/"),
),
),
want: &Message{
MsgType: MsgTypePost,
Content: map[string]interface{}{
"post": map[string]interface{}{
"zh_cn": map[string]interface{}{
"title": "Title",
"content": [][]map[string]interface{}{
{
{"tag": "text", "text": "Project has been updated: "},
{"tag": "a", "text": "View", "href": "http://www.example.com/"},
},
},
},
},
},
},
wantErr: false,
},
{
name: "post message with @ mention",
lang: LanguageZhCN,
content: NewPostContent(
"Notify",
NewParagraph(
NewTextElement("Notification for "),
NewAtElement("ou_xxx", "Tom"),
),
),
want: &Message{
MsgType: MsgTypePost,
Content: map[string]interface{}{
"post": map[string]interface{}{
"zh_cn": map[string]interface{}{
"title": "Notify",
"content": [][]map[string]interface{}{
{
{"tag": "text", "text": "Notification for "},
{"tag": "at", "user_id": "ou_xxx", "user_name": "Tom"},
},
},
},
},
},
},
wantErr: false,
},
{
name: "multi-language post message",
lang: LanguageZhCN,
content: NewPostContent(
"Title",
NewParagraph(NewTextElement("Content")),
),
want: &Message{
MsgType: MsgTypePost,
Content: map[string]interface{}{
"post": map[string]interface{}{
"zh_cn": map[string]interface{}{
"title": "Title",
"content": [][]map[string]interface{}{
{{"tag": "text", "text": "Content"}},
},
},
"en_us": map[string]interface{}{
"title": "Title",
"content": [][]map[string]interface{}{
{{"tag": "text", "text": "Content"}},
},
},
},
},
},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var got *Message
if tt.name == "multi-language post message" {
content := NewPostContent("Title", NewParagraph(NewTextElement("Content")))
got = NewPostMessageMultiLanguage(
NewPostLanguageContent(LanguageZhCN, content),
NewPostLanguageContent(LanguageEnUS, content),
)
} else {
got = NewPostMessage(tt.lang, tt.content)
}
if got.MsgType != tt.want.MsgType {
t.Errorf("NewPostMessage() MsgType = %v, want %v", got.MsgType, tt.want.MsgType)
}
// Compare nested structures
gotBytes, _ := json.Marshal(got.Content)
wantBytes, _ := json.Marshal(tt.want.Content)
if string(gotBytes) != string(wantBytes) {
t.Errorf("NewPostMessage() Content = %s, want %s", gotBytes, wantBytes)
}
})
}
}
func TestNewImageMessage(t *testing.T) {
tests := []struct {
name string
imageKey string
want *Message
wantErr bool
}{
{
name: "valid image message",
imageKey: "img_ecffc3b9-8f14-400f-a014-05eca1a4310g",
want: &Message{
MsgType: MsgTypeImage,
Content: map[string]interface{}{
"image_key": "img_ecffc3b9-8f14-400f-a014-05eca1a4310g",
},
},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := NewImageMessage(tt.imageKey)
if !cmp.Equal(got, tt.want) {
t.Errorf("NewImageMessage() diff = %v", cmp.Diff(tt.want, got))
}
})
}
}
func TestNewShareChatMessage(t *testing.T) {
tests := []struct {
name string
shareChatID string
want *Message
wantErr bool
}{
{
name: "valid share chat message",
shareChatID: "oc_f5b1a7eb27ae2****339ff",
want: &Message{
MsgType: MsgTypeShareChat,
Content: map[string]interface{}{
"share_chat_id": "oc_f5b1a7eb27ae2****339ff",
},
},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := NewShareChatMessage(tt.shareChatID)
if !cmp.Equal(got, tt.want) {
t.Errorf("NewShareChatMessage() diff = %v", cmp.Diff(tt.want, got))
}
})
}
}
func TestNewInteractiveMessage(t *testing.T) {
tests := []struct {
name string
card map[string]interface{}
want *Message
}{
{
name: "interactive message from Card",
card: map[string]interface{}{
"schema": "2.0",
"body": map[string]interface{}{
"elements": []map[string]interface{}{
{"tag": "markdown", "content": "Hello!"},
},
},
"header": map[string]interface{}{
"title": map[string]interface{}{"tag": "plain_text", "content": "Card Title"},
"template": "blue",
},
},
want: &Message{
MsgType: MsgTypeInteractive,
Card: map[string]interface{}{
"schema": "2.0",
"body": map[string]interface{}{
"elements": []map[string]interface{}{
{"tag": "markdown", "content": "Hello!"},
},
},
"header": map[string]interface{}{
"title": map[string]interface{}{"tag": "plain_text", "content": "Card Title"},
"template": "blue",
},
},
},
},
{
name: "interactive message with nil card",
card: nil,
want: &Message{MsgType: MsgTypeInteractive, Card: nil},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := NewInteractiveMessageFromMap(tt.card)
if got.MsgType != tt.want.MsgType {
t.Errorf("NewInteractiveMessage() MsgType = %v, want %v", got.MsgType, tt.want.MsgType)
}
if !cmp.Equal(got.Card, tt.want.Card) {
t.Errorf("NewInteractiveMessage() Card diff = %v", cmp.Diff(tt.want.Card, got.Card))
}
})
}
}
func TestPostElements(t *testing.T) {
tests := []struct {
name string
elem Element
want []string
}{
{
name: "text element",
elem: NewTextElement("Hello"),
want: []string{`"tag":"text"`, `"text":"Hello"`},
},
{
name: "link element",
elem: NewLinkElement("View", "https://example.com"),
want: []string{`"tag":"a"`, `"text":"View"`, `"href":"https://example.com"`},
},
{
name: "at element",
elem: NewAtElement("ou_xxx", "Tom"),
want: []string{`"tag":"at"`, `"user_id":"ou_xxx"`, `"user_name":"Tom"`},
},
{
name: "image element",
elem: NewImageElement("img_key_123"),
want: []string{`"tag":"img"`, `"image_key":"img_key_123"`},
},
{
name: "emoticon element",
elem: NewEmoticonElement("emoji_key_123"),
want: []string{`"tag":"emotion"`, `"emoji_key":"emoji_key_123"`},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
data, _ := json.Marshal(tt.elem)
got := string(data)
for _, wantContains := range tt.want {
if !containsString(got, wantContains) {
t.Errorf("Element = %v, missing %q", got, wantContains)
}
}
})
}
}
func TestCardElements(t *testing.T) {
tests := []struct {
name string
elem CardElement
want []string
}{
{
name: "markdown element",
elem: NewMarkdownElement("Hello **World**"),
want: []string{`"tag":"markdown"`, `"content":"Hello **World**"`},
},
{
name: "button element",
elem: NewButtonElement("Click Me", "primary", "https://example.com"),
want: []string{`"tag":"button"`, `"tag":"plain_text"`, `"content":"Click Me"`, `"type":"primary"`, `"url":"https://example.com"`},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
data, _ := json.Marshal(tt.elem)
got := string(data)
for _, wantContains := range tt.want {
if !containsString(got, wantContains) {
t.Errorf("CardElement = %v, missing %q", got, wantContains)
}
}
})
}
}
func TestCardBuilder(t *testing.T) {
tests := []struct {
name string
card *Card
want []string
}{
{
name: "simple card",
card: NewCard("2.0").
SetBody(&CardBody{
Elements: []CardElement{
NewMarkdownElement("Hello, World!"),
},
}),
want: []string{`"schema":"2.0"`, `"tag":"markdown"`, `"content":"Hello, World!"`},
},
{
name: "card with header",
card: NewCard("2.0").
SetHeader(&CardHeader{
Title: NewCardTitle("Title"),
Template: "blue",
}).
SetBody(&CardBody{
Elements: []CardElement{
NewMarkdownElement("Content"),
},
}),
want: []string{`"schema":"2.0"`, `"tag":"plain_text"`, `"content":"Title"`, `"template":"blue"`, `"content":"Content"`},
},
{
name: "card with config",
card: NewCard("2.0").
SetConfig(map[string]interface{}{
"wide_screen_mode": true,
}).
SetBody(&CardBody{
Elements: []CardElement{
NewMarkdownElement("Content"),
},
}),
want: []string{`"schema":"2.0"`, `"wide_screen_mode":true`, `"content":"Content"`},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := tt.card.ToMap()
data, _ := json.Marshal(got)
gotStr := string(data)
for _, wantContains := range tt.want {
if !containsString(gotStr, wantContains) {
t.Errorf("Card.ToMap() got %v, missing %q", gotStr, wantContains)
}
}
})
}
}
func TestMessageJSONSerialization(t *testing.T) {
tests := []struct {
name string
message *Message
contains []string
}{
{
name: "text message serialization",
message: NewTextMessage("test message"),
contains: []string{`"msg_type":"text"`, `"text":"test message"`},
},
{
name: "post message serialization",
message: NewPostMessage(
LanguageZhCN,
NewPostContent("Title", NewParagraph(NewTextElement("Content"))),
),
contains: []string{`"msg_type":"post"`, `"title":"Title"`, `"tag":"text"`, `"text":"Content"`},
},
{
name: "post message with link serialization",
message: NewPostMessage(
LanguageZhCN,
NewPostContent(
"Update",
NewParagraph(
NewTextElement("View: "),
NewLinkElement("Details", "https://example.com"),
),
),
),
contains: []string{`"msg_type":"post"`, `"tag":"a"`, `"href":"https://example.com"`},
},
{
name: "image message serialization",
message: NewImageMessage("img_key_123"),
contains: []string{`"msg_type":"image"`, `"image_key":"img_key_123"`},
},
{
name: "share chat message serialization",
message: NewShareChatMessage("oc_12345"),
contains: []string{`"msg_type":"share_chat"`, `"share_chat_id":"oc_12345"`},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
data, err := json.Marshal(tt.message)
if err != nil {
t.Fatalf("json.Marshal() error = %v", err)
}
jsonStr := string(data)
for _, wantContains := range tt.contains {
if !containsString(jsonStr, wantContains) {
t.Errorf("json.Marshal() result does not contain %q\ngot: %s", wantContains, jsonStr)
}
}
})
}
}
func TestMessageWithSignature(t *testing.T) {
tests := []struct {
name string
message *Message
timestamp int64
sign string
contains []string
}{
{
name: "message with timestamp and sign",
message: NewTextMessage("test"),
timestamp: 1599360473,
sign: "abc123",
contains: []string{`"timestamp":1599360473`, `"sign":"abc123"`},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tt.message.Timestamp = tt.timestamp
tt.message.Sign = tt.sign
data, err := json.Marshal(tt.message)
if err != nil {
t.Fatalf("json.Marshal() error = %v", err)
}
jsonStr := string(data)
for _, wantContains := range tt.contains {
if !containsString(jsonStr, wantContains) {
t.Errorf("json.Marshal() result does not contain %q\ngot: %s", wantContains, jsonStr)
}
}
})
}
}
// Helper function to check if a string contains a substring
func containsString(s, substr string) bool {
for i := 0; i <= len(s)-len(substr); i++ {
if i+len(substr) > len(s) {
return false
}
if s[i:i+len(substr)] == substr {
return true
}
}
return false
}
func TestMsgTypeString(t *testing.T) {
tests := []struct {
name string
t MsgType
want string
}{
{"text type", MsgTypeText, "text"},
{"post type", MsgTypePost, "post"},
{"image type", MsgTypeImage, "image"},
{"share_chat type", MsgTypeShareChat, "share_chat"},
{"interactive type", MsgTypeInteractive, "interactive"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := string(tt.t)
if got != tt.want {
t.Errorf("MsgType.String() = %v, want %v", got, tt.want)
}
})
}
}
func TestLanguageString(t *testing.T) {
tests := []struct {
name string
l Language
want string
}{
{"Simplified Chinese", LanguageZhCN, "zh_cn"},
{"English", LanguageEnUS, "en_us"},
{"Japanese", LanguageJa, "ja"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := string(tt.l)
if got != tt.want {
t.Errorf("Language.String() = %v, want %v", got, tt.want)
}
})
}
}
func TestComplexPostMessage(t *testing.T) {
// Test a complex post message with multiple paragraphs and element types
message := NewPostMessage(
LanguageZhCN,
NewPostContent(
"Daily Report",
// First paragraph: text + link + @mention
NewParagraph(
NewTextElement("Project update: "),
NewLinkElement("View Details", "https://example.com"),
NewTextElement(" - "),
NewAtElement("ou_xxx", "Tom"),
),
// Second paragraph: image + text
NewParagraph(
NewImageElement("img_key_123"),
NewTextElement(" Screenshot attached"),
),
// Third paragraph: text only
NewParagraph(
NewTextElement("Please review by EOD."),
),
),
)
data, err := json.Marshal(message.Content)
if err != nil {
t.Fatalf("json.Marshal() error = %v", err)
}
jsonStr := string(data)
// Verify content structure
requiredStrings := []string{
`"title":"Daily Report"`,
`"tag":"text"`,
`"tag":"a"`,
`"tag":"at"`,
`"tag":"img"`,
`"user_id":"ou_xxx"`,
`"href":"https://example.com"`,
`"image_key":"img_key_123"`,
}
for _, s := range requiredStrings {
if !containsString(jsonStr, s) {
t.Errorf("Complex post message missing %q\ngot: %s", s, jsonStr)
}
}
}
func TestInteractiveCardMessage(t *testing.T) {
// Test building an interactive card with header, body, and buttons
card := NewCard("2.0").
SetConfig(map[string]interface{}{
"wide_screen_mode": true,
}).
SetHeader(&CardHeader{
Title: NewCardTitle("Task Alert"),
Template: "red",
}).
SetBody(&CardBody{
Direction: "vertical",
Padding: "12px 12px 12px 12px",
Elements: []CardElement{
NewMarkdownElement("**High Priority Task**\n\nPlease complete this task by end of day."),
NewButtonElement("View Details", "primary", "https://example.com/task/123"),
NewButtonElement("Dismiss", "default", "https://example.com/dismiss"),
},
})
message := NewInteractiveMessage(card)
data, err := json.Marshal(message.Card)
if err != nil {
t.Fatalf("json.Marshal() error = %v", err)
}
jsonStr := string(data)
requiredStrings := []string{
`"schema":"2.0"`,
`"wide_screen_mode":true`,
`"template":"red"`,
`"tag":"markdown"`,
`"tag":"button"`,
`"type":"primary"`,
`"type":"default"`,
}
for _, s := range requiredStrings {
if !containsString(jsonStr, s) {
t.Errorf("Interactive card message missing %q\ngot: %s", s, jsonStr)
}
}
}