forked from go-telegram-bot-api/telegram-bot-api
-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathconfigs_test.go
More file actions
1983 lines (1841 loc) · 54 KB
/
Copy pathconfigs_test.go
File metadata and controls
1983 lines (1841 loc) · 54 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
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package tgbotapi
import (
"encoding/json"
"fmt"
"strings"
"testing"
)
func TestAnswerGuestQueryConfigParams(t *testing.T) {
result := NewInlineQueryResultArticle("guest-result", "Answer", "Hello")
config := NewAnswerGuestQuery("guest-query", result)
params, err := config.params()
if err != nil {
t.Fatalf("params failed: %v", err)
}
if params["guest_query_id"] != "guest-query" {
t.Fatalf("guest_query_id mismatch: %#v", params)
}
if !strings.Contains(params["result"], `"type":"article"`) || !strings.Contains(params["result"], `"id":"guest-result"`) {
t.Fatalf("result payload mismatch: %#v", params)
}
}
func TestAnswerChatJoinRequestQueryConfigParams(t *testing.T) {
config := NewAnswerChatJoinRequestQuery("join-query", "approve")
params, err := config.params()
if err != nil {
t.Fatalf("params failed: %v", err)
}
if params["chat_join_request_query_id"] != "join-query" {
t.Fatalf("chat_join_request_query_id mismatch: %#v", params)
}
if params["result"] != "approve" {
t.Fatalf("result mismatch: %#v", params)
}
}
func TestSendChatJoinRequestWebAppConfigParams(t *testing.T) {
config := NewSendChatJoinRequestWebApp("join-query", "https://example.com/app")
params, err := config.params()
if err != nil {
t.Fatalf("params failed: %v", err)
}
if params["chat_join_request_query_id"] != "join-query" {
t.Fatalf("chat_join_request_query_id mismatch: %#v", params)
}
if params["web_app_url"] != "https://example.com/app" {
t.Fatalf("web_app_url mismatch: %#v", params)
}
}
func TestSendRichMessageConfigParams(t *testing.T) {
config := NewSendRichMessage(123, NewInputRichMessageHTML("<p>Hello</p>"))
config.MessageThreadID = 456
config.DirectMessagesTopicID = 789
config.AllowPaidBroadcast = true
config.SuggestedPostParameters = &SuggestedPostParameters{Price: &SuggestedPostPrice{Currency: "XTR", Amount: 1}}
params, err := config.params()
if err != nil {
t.Fatalf("params failed: %v", err)
}
if params["chat_id"] != "123" {
t.Fatalf("chat_id mismatch: %#v", params)
}
if params["message_thread_id"] != "456" || params["direct_messages_topic_id"] != "789" {
t.Fatalf("thread params mismatch: %#v", params)
}
if params["allow_paid_broadcast"] != "true" {
t.Fatalf("allow_paid_broadcast mismatch: %#v", params)
}
if !strings.Contains(params["rich_message"], `"html":"\u003cp\u003eHello\u003c/p\u003e"`) {
t.Fatalf("rich_message mismatch: %#v", params)
}
if !strings.Contains(params["suggested_post_parameters"], `"price":{"currency":"XTR","amount":1}`) {
t.Fatalf("suggested_post_parameters mismatch: %#v", params)
}
}
func TestSendRichMessageDraftConfigParams(t *testing.T) {
config := NewSendRichMessageDraft(123, 456, NewInputRichMessageMarkdown("**Hello**"))
config.MessageThreadID = 789
params, err := config.params()
if err != nil {
t.Fatalf("params failed: %v", err)
}
if params["chat_id"] != "123" {
t.Fatalf("chat_id mismatch: %#v", params)
}
if params["draft_id"] != "456" || params["message_thread_id"] != "789" {
t.Fatalf("draft params mismatch: %#v", params)
}
if !strings.Contains(params["rich_message"], `"markdown":"**Hello**"`) {
t.Fatalf("rich_message mismatch: %#v", params)
}
}
func TestInputMediaLinkSerialization(t *testing.T) {
option := NewPollOptionWithMedia("docs", NewInputMediaLink("https://core.telegram.org/bots/api"))
config := SendPollConfig{
BaseChat: BaseChat{
ChatConfig: ChatConfig{ChatID: 1},
},
Question: "q",
Options: []InputPollOption{option},
}
params, err := config.params()
if err != nil {
t.Fatalf("params failed: %v", err)
}
if !strings.Contains(params["options"], `"media":{"type":"link","url":"https://core.telegram.org/bots/api"}`) {
t.Fatalf("options media mismatch: %#v", params["options"])
}
if files := config.files(); len(files) != 0 {
t.Fatalf("unexpected files for link media: %+v", files)
}
}
func TestSendLivePhotoConfigUploadSerialization(t *testing.T) {
config := NewLivePhoto(1, FilePath("tests/video.mp4"), FilePath("tests/image.jpg"))
config.Caption = "live"
config.HasSpoiler = true
params, err := config.params()
if err != nil {
t.Fatalf("params failed: %v", err)
}
if params["chat_id"] != "1" || params["caption"] != "live" || params["has_spoiler"] != "true" {
t.Fatalf("params mismatch: %#v", params)
}
files := config.files()
if len(files) != 2 || files[0].Name != "live_photo" || files[1].Name != "photo" {
t.Fatalf("unexpected files payload: %+v", files)
}
}
func TestVideoAndAnimationConfigDimensionParams(t *testing.T) {
tests := []struct {
name string
params func() (Params, error)
expectedWidth string
expectedHeight string
}{
{
name: "video",
params: func() (Params, error) {
config := NewVideo(1, FileID("video-file-id"))
config.Width = 1920
config.Height = 1080
return config.params()
},
expectedWidth: "1920",
expectedHeight: "1080",
},
{
name: "animation",
params: func() (Params, error) {
config := NewAnimation(1, FileID("animation-file-id"))
config.Width = 640
config.Height = 360
return config.params()
},
expectedWidth: "640",
expectedHeight: "360",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
params, err := tt.params()
if err != nil {
t.Fatalf("params failed: %v", err)
}
if params["width"] != tt.expectedWidth || params["height"] != tt.expectedHeight {
t.Fatalf("dimension params mismatch: %#v", params)
}
})
}
}
func TestSendPollConfigBotAPI10MediaSerialization(t *testing.T) {
optionMedia := &InputMediaSticker{
Type: "sticker",
Media: FilePath("tests/image.jpg"),
Emoji: ":)",
}
explanationMedia := &InputMediaLivePhoto{
BaseInputMedia: BaseInputMedia{
Type: "live_photo",
Media: FilePath("tests/video.mp4"),
},
Photo: FilePath("tests/image.jpg"),
}
pollMedia := &InputMediaLocation{
Type: "location",
Latitude: 10.5,
Longitude: 20.25,
}
config := SendPollConfig{
BaseChat: BaseChat{
ChatConfig: ChatConfig{ChatID: 1},
},
Question: "q",
Options: []InputPollOption{NewPollOptionWithMedia("a", optionMedia), NewPollOption("b")},
ExplanationMedia: explanationMedia,
Media: pollMedia,
MembersOnly: true,
CountryCodes: []string{"US", "PL"},
}
params, err := config.params()
if err != nil {
t.Fatalf("params failed: %v", err)
}
if !strings.Contains(params["options"], `"media":{"type":"sticker","media":"attach://poll-option-0-0","emoji":":)"}`) {
t.Fatalf("options media mismatch: %#v", params["options"])
}
if !strings.Contains(params["explanation_media"], `"media":"attach://explanation-media-0"`) ||
!strings.Contains(params["explanation_media"], `"photo":"attach://explanation-media-0-photo"`) {
t.Fatalf("explanation_media mismatch: %#v", params["explanation_media"])
}
if !strings.Contains(params["media"], `"type":"location"`) {
t.Fatalf("poll media mismatch: %#v", params["media"])
}
if params["members_only"] != "true" || params["country_codes"] != `["US","PL"]` {
t.Fatalf("poll limit params mismatch: %#v", params)
}
files := config.files()
if len(files) != 3 {
t.Fatalf("unexpected files count: %+v", files)
}
if optionMedia.Media != FilePath("tests/image.jpg") || explanationMedia.Media != FilePath("tests/video.mp4") {
t.Fatalf("original media was mutated")
}
}
func TestPaidMediaLivePhotoSerialization(t *testing.T) {
livePhoto := NewInputMediaLivePhoto(FilePath("tests/video.mp4"), FilePath("tests/image.jpg"))
paid := NewInputPaidMediaLivePhoto(&livePhoto)
config := NewPaidMedia(1, 10, &paid)
params, err := config.params()
if err != nil {
t.Fatalf("params failed: %v", err)
}
if !strings.Contains(params["media"], `"type":"live_photo"`) ||
!strings.Contains(params["media"], `"media":"attach://file-0"`) ||
!strings.Contains(params["media"], `"photo":"attach://file-0-photo"`) {
t.Fatalf("paid media payload mismatch: %#v", params["media"])
}
files := config.files()
if len(files) != 2 || files[0].Name != "file-0" || files[1].Name != "file-0-photo" {
t.Fatalf("unexpected files payload: %+v", files)
}
}
func TestPaidMediaConfigSerializesMediaArray(t *testing.T) {
photo := NewInputMediaPhoto(FileID("paid-photo-id"))
paid := NewInputPaidMediaPhoto(&photo)
config := NewPaidMedia(1, 10, &paid)
params, err := config.params()
if err != nil {
t.Fatalf("params failed: %v", err)
}
var media []map[string]any
if err := json.Unmarshal([]byte(params["media"]), &media); err != nil {
t.Fatalf("media must be a JSON array, got %q: %v", params["media"], err)
}
if len(media) != 1 || media[0]["type"] != "photo" || media[0]["media"] != "paid-photo-id" {
t.Fatalf("unexpected paid media payload: %#v", media)
}
}
func TestPaidMediaGroupConfigSerializesMultipleMedia(t *testing.T) {
photo := NewInputMediaPhoto(FileID("paid-photo-id"))
paidPhoto := NewInputPaidMediaPhoto(&photo)
video := NewInputMediaVideo(FileID("paid-video-id"))
paidVideo := NewInputPaidMediaVideo(&video)
config := NewPaidMediaGroup(1, 10, paidPhoto, paidVideo)
params, err := config.params()
if err != nil {
t.Fatalf("params failed: %v", err)
}
var media []map[string]any
if err := json.Unmarshal([]byte(params["media"]), &media); err != nil {
t.Fatalf("media must be a JSON array, got %q: %v", params["media"], err)
}
if len(media) != 2 || media[0]["type"] != "photo" || media[1]["type"] != "video" {
t.Fatalf("unexpected paid media payload: %#v", media)
}
}
func TestPaidMediaVideoMetadataSurvivesUploadPreparation(t *testing.T) {
video := NewInputMediaVideo(FilePath("tests/video.mp4"))
paid := NewInputPaidMediaVideo(&video)
paid.Cover = "cover-file-id"
paid.StartTimestamp = 12
prepared := prepareInputMediaForParams([]InputMedia{&paid})
if len(prepared) != 1 {
t.Fatalf("unexpected prepared media: %+v", prepared)
}
params := Params{}
if err := params.AddInterface("media", prepared[0]); err != nil {
t.Fatalf("marshal prepared media: %v", err)
}
if !strings.Contains(params["media"], `"cover":"cover-file-id"`) || !strings.Contains(params["media"], `"start_timestamp":12`) {
t.Fatalf("paid media metadata was not preserved: %s", params["media"])
}
}
func TestManagedBotAccessSettingsConfigFalseParam(t *testing.T) {
config := NewSetManagedBotAccessSettings(42, false, 1, 2)
params, err := config.params()
if err != nil {
t.Fatalf("params failed: %v", err)
}
if params["user_id"] != "42" || params["is_access_restricted"] != "false" || params["added_user_ids"] != "[1,2]" {
t.Fatalf("managed access params mismatch: %#v", params)
}
}
func TestDeleteReactionConfigsAndReturnBotsParams(t *testing.T) {
deleteReaction := NewDeleteMessageReaction(1, 2)
deleteReaction.UserID = 3
params, err := deleteReaction.params()
if err != nil {
t.Fatalf("delete reaction params failed: %v", err)
}
if params["chat_id"] != "1" || params["message_id"] != "2" || params["user_id"] != "3" {
t.Fatalf("delete reaction params mismatch: %#v", params)
}
deleteAll := NewDeleteAllMessageReactions(1)
deleteAll.ActorChatID = 4
params, err = deleteAll.params()
if err != nil {
t.Fatalf("delete all reactions params failed: %v", err)
}
if params["chat_id"] != "1" || params["actor_chat_id"] != "4" {
t.Fatalf("delete all reactions params mismatch: %#v", params)
}
admins := NewChatAdministrators(1)
admins.ReturnBots = true
params, err = admins.params()
if err != nil {
t.Fatalf("chat administrators params failed: %v", err)
}
if params["return_bots"] != "true" {
t.Fatalf("return_bots mismatch: %#v", params)
}
}
func TestSendPollConfigCloseDate64BitParam(t *testing.T) {
config := SendPollConfig{
BaseChat: BaseChat{
ChatConfig: ChatConfig{ChatID: 1},
},
Question: "q",
Options: []InputPollOption{{Text: "a"}},
CloseDate: 2208988800,
}
params, err := config.params()
if err != nil {
t.Fatalf("params failed: %v", err)
}
if params["close_date"] != "2208988800" {
t.Fatalf("close_date mismatch: %s", params["close_date"])
}
}
func TestSendPollConfigBotAPI96Params(t *testing.T) {
allowsRevoting := false
config := SendPollConfig{
BaseChat: BaseChat{
ChatConfig: ChatConfig{ChatID: 1},
},
Question: "q",
Options: []InputPollOption{{Text: "a"}, {Text: "b"}, {Text: "c"}},
Type: "quiz",
AllowsRevoting: &allowsRevoting,
ShuffleOptions: true,
AllowAddingOptions: true,
HideResultsUntilCloses: true,
CorrectOptionIDs: []int{0, 2},
Description: "desc",
DescriptionEntities: []MessageEntity{{Type: "bold", Offset: 0, Length: 4}},
}
params, err := config.params()
if err != nil {
t.Fatalf("params failed: %v", err)
}
if params["allows_revoting"] != "false" {
t.Fatalf("allows_revoting mismatch: %#v", params)
}
if params["shuffle_options"] != "true" {
t.Fatalf("shuffle_options mismatch: %#v", params)
}
if params["allow_adding_options"] != "true" {
t.Fatalf("allow_adding_options mismatch: %#v", params)
}
if params["hide_results_until_closes"] != "true" {
t.Fatalf("hide_results_until_closes mismatch: %#v", params)
}
if params["correct_option_ids"] != "[0,2]" {
t.Fatalf("correct_option_ids mismatch: %#v", params)
}
if _, ok := params["correct_option_id"]; ok {
t.Fatalf("unexpected legacy correct_option_id key: %#v", params)
}
if params["description"] != "desc" {
t.Fatalf("description mismatch: %#v", params)
}
if params["description_entities"] != `[{"type":"bold","offset":0,"length":4}]` {
t.Fatalf("description_entities mismatch: %#v", params)
}
}
func TestSendPollConfigAllowsRevotingOmittedWhenNil(t *testing.T) {
config := SendPollConfig{
BaseChat: BaseChat{
ChatConfig: ChatConfig{ChatID: 1},
},
Question: "q",
Options: []InputPollOption{{Text: "a"}, {Text: "b"}},
}
params, err := config.params()
if err != nil {
t.Fatalf("params failed: %v", err)
}
if _, ok := params["allows_revoting"]; ok {
t.Fatalf("unexpected allows_revoting param: %#v", params)
}
if _, ok := params["correct_option_ids"]; ok {
t.Fatalf("unexpected correct_option_ids param: %#v", params)
}
}
func TestSendPollConfigLegacyCorrectOptionIDCompat(t *testing.T) {
config := SendPollConfig{
BaseChat: BaseChat{
ChatConfig: ChatConfig{ChatID: 1},
},
Question: "q",
Options: []InputPollOption{{Text: "a"}, {Text: "b"}},
Type: "quiz",
CorrectOptionID: 0,
}
params, err := config.params()
if err != nil {
t.Fatalf("params failed: %v", err)
}
if params["correct_option_ids"] != "[0]" {
t.Fatalf("correct_option_ids mismatch: %#v", params)
}
if _, ok := params["correct_option_id"]; ok {
t.Fatalf("unexpected legacy correct_option_id key: %#v", params)
}
}
func TestSavePreparedKeyboardButtonConfigParams(t *testing.T) {
config := SavePreparedKeyboardButtonConfig{
UserID: 42,
Button: KeyboardButton{
Text: "Create bot",
RequestManagedBot: &KeyboardButtonRequestManagedBot{
RequestID: 7,
SuggestedName: "Demo",
SuggestedUsername: "demo_helper_bot",
},
},
}
params, err := config.params()
if err != nil {
t.Fatalf("params failed: %v", err)
}
if params["user_id"] != "42" {
t.Fatalf("user_id mismatch: %#v", params)
}
if !strings.Contains(params["button"], `"request_managed_bot":{"request_id":7`) {
t.Fatalf("button payload mismatch: %#v", params)
}
}
func TestSetChatMemberTagConfigTagParam(t *testing.T) {
config := SetChatMemberTagConfig{
ChatMemberConfig: ChatMemberConfig{
ChatConfig: ChatConfig{ChatID: 1},
UserID: 777,
},
Tag: "text tag param",
}
params, err := config.params()
if err != nil {
t.Fatalf("params failed: %v", err)
}
if params["tag"] != "text tag param" {
t.Fatalf("tag mismatch: %s", params["tag"])
}
}
func TestBanChatSenderChatConfigUntilDate64BitParam(t *testing.T) {
config := BanChatSenderChatConfig{
ChatConfig: ChatConfig{ChatID: 1},
SenderChatID: 2,
UntilDate: 2208988800,
}
params, err := config.params()
if err != nil {
t.Fatalf("params failed: %v", err)
}
if params["until_date"] != "2208988800" {
t.Fatalf("until_date mismatch: %s", params["until_date"])
}
}
func TestCreateChatInviteLinkConfigExpireDate64BitParam(t *testing.T) {
config := CreateChatInviteLinkConfig{
ChatConfig: ChatConfig{ChatID: 1},
Name: "name",
ExpireDate: 2208988800,
}
params, err := config.params()
if err != nil {
t.Fatalf("params failed: %v", err)
}
if params["expire_date"] != "2208988800" {
t.Fatalf("expire_date mismatch: %s", params["expire_date"])
}
}
func TestEditChatInviteLinkConfigExpireDate64BitParam(t *testing.T) {
config := EditChatInviteLinkConfig{
ChatConfig: ChatConfig{ChatID: 1},
InviteLink: "https://t.me/+abc",
ExpireDate: 2208988800,
}
params, err := config.params()
if err != nil {
t.Fatalf("params failed: %v", err)
}
if params["expire_date"] != "2208988800" {
t.Fatalf("expire_date mismatch: %s", params["expire_date"])
}
}
func TestPrepareInputMediaForParams(t *testing.T) {
tests := []struct {
name string
inputMedia []InputMedia
expectedMediaPaths []string
expectedThumbPaths []string
}{
{
name: "photo that needs upload",
inputMedia: []InputMedia{
&InputMediaPhoto{
BaseInputMedia: BaseInputMedia{
Type: "photo",
Media: FilePath("tests/image.jpg"),
},
},
},
expectedMediaPaths: []string{"attach://file-0"},
expectedThumbPaths: []string{""},
},
{
name: "video with thumbnail that need upload",
inputMedia: []InputMedia{
&InputMediaVideo{
BaseInputMedia: BaseInputMedia{
Type: "video",
Media: FilePath("tests/video.mp4"),
},
Thumb: FilePath("tests/image.jpg"),
},
},
expectedMediaPaths: []string{"attach://file-0"},
expectedThumbPaths: []string{"attach://file-0-thumb"},
},
{
name: "multiple media items",
inputMedia: []InputMedia{
&InputMediaPhoto{
BaseInputMedia: BaseInputMedia{
Type: "photo",
Media: FilePath("tests/image.jpg"),
},
},
&InputMediaVideo{
BaseInputMedia: BaseInputMedia{
Type: "video",
Media: FileURL("https://example.com/video.mp4"), // URL doesn't need upload
},
},
&InputMediaDocument{
BaseInputMedia: BaseInputMedia{
Type: "document",
Media: FilePath("tests/audio.mp3"),
},
Thumb: FilePath("tests/image.jpg"),
},
},
expectedMediaPaths: []string{"attach://file-0", "https://example.com/video.mp4", "attach://file-2"},
expectedThumbPaths: []string{"", "", "attach://file-2-thumb"},
},
{
name: "items that don't need upload",
inputMedia: []InputMedia{
&InputMediaPhoto{
BaseInputMedia: BaseInputMedia{
Type: "photo",
Media: FileID("photo123"),
},
},
&InputMediaVideo{
BaseInputMedia: BaseInputMedia{
Type: "video",
Media: FileURL("https://example.com/video.mp4"),
},
Thumb: FileID("thumb123"),
},
},
expectedMediaPaths: []string{"photo123", "https://example.com/video.mp4"},
expectedThumbPaths: []string{"", "thumb123"},
},
{
name: "paid media",
inputMedia: []InputMedia{
&InputPaidMedia{
Type: "video",
Media: &InputMediaVideo{
BaseInputMedia: BaseInputMedia{
Type: "video",
Media: FilePath("tests/video.mp4"),
},
},
Thumb: FilePath("tests/image.jpg"),
},
},
expectedMediaPaths: []string{"attach://file-0"},
expectedThumbPaths: []string{"attach://file-0-thumb"},
},
{
name: "complex nested media with metadata",
inputMedia: []InputMedia{
&InputPaidMedia{
Type: "video",
Media: &InputMediaVideo{
BaseInputMedia: BaseInputMedia{
Type: "video",
Media: FilePath("tests/complex_video.mp4"),
Caption: "Complex video with metadata",
},
Width: 1920,
Height: 1080,
Duration: 60,
SupportsStreaming: true,
Thumb: FilePath("tests/inner_thumb.jpg"),
},
Thumb: FilePath("tests/outer_thumb.jpg"),
Width: 1920,
Height: 1080,
Duration: 60,
SupportsStreaming: true,
},
},
expectedMediaPaths: []string{"attach://file-0"},
expectedThumbPaths: []string{"attach://file-0-thumb"},
},
{
name: "animation with caption entities and spoiler",
inputMedia: []InputMedia{
&InputMediaAnimation{
BaseInputMedia: BaseInputMedia{
Type: "animation",
Media: FilePath("tests/animation.gif"),
Caption: "Animation with *formatting*",
CaptionEntities: []MessageEntity{
{
Type: "bold",
Offset: 14,
Length: 11,
},
},
ParseMode: "Markdown",
HasSpoiler: true,
},
Thumb: FilePath("tests/anim_thumb.jpg"),
},
},
expectedMediaPaths: []string{"attach://file-0"},
expectedThumbPaths: []string{"attach://file-0-thumb"},
},
{
name: "audio with all media types",
inputMedia: []InputMedia{
&InputMediaAudio{
BaseInputMedia: BaseInputMedia{
Type: "audio",
Media: FilePath("tests/audio.mp3"),
},
Thumb: FilePath("tests/audio_thumb.jpg"),
Duration: 180,
Performer: "Test Artist",
Title: "Test Track",
},
},
expectedMediaPaths: []string{"attach://file-0"},
expectedThumbPaths: []string{"attach://file-0-thumb"},
},
{
name: "empty media array",
inputMedia: []InputMedia{
// Empty slice
},
expectedMediaPaths: []string{},
expectedThumbPaths: []string{},
},
{
name: "all media types with mixed sources",
inputMedia: []InputMedia{
&InputMediaPhoto{
BaseInputMedia: BaseInputMedia{
Type: "photo",
Media: FilePath("tests/photo.jpg"),
},
},
&InputMediaVideo{
BaseInputMedia: BaseInputMedia{
Type: "video",
Media: FileID("existing_video"),
},
Thumb: FilePath("tests/video_thumb.jpg"),
},
&InputMediaAnimation{
BaseInputMedia: BaseInputMedia{
Type: "animation",
Media: FileURL("https://example.com/animation.gif"),
},
},
&InputMediaAudio{
BaseInputMedia: BaseInputMedia{
Type: "audio",
Media: FilePath("tests/audio.mp3"),
},
},
&InputMediaDocument{
BaseInputMedia: BaseInputMedia{
Type: "document",
Media: FileID("existing_document"),
},
},
&InputPaidMedia{
Type: "photo",
Media: &InputMediaPhoto{
BaseInputMedia: BaseInputMedia{
Type: "photo",
Media: FilePath("tests/paid_photo.jpg"),
},
},
},
},
expectedMediaPaths: []string{
"attach://file-0",
"existing_video",
"https://example.com/animation.gif",
"attach://file-3",
"existing_document",
"attach://file-5",
},
expectedThumbPaths: []string{
"",
"attach://file-1-thumb",
"",
"",
"",
"",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := prepareInputMediaForParams(tt.inputMedia)
if len(result) != len(tt.inputMedia) {
t.Errorf("Expected result length %d, got %d", len(tt.inputMedia), len(result))
}
for i, media := range result {
if i >= len(tt.expectedMediaPaths) {
break // Safety check for empty media array test
}
mediaPath := media.getMedia().SendData()
if mediaPath != tt.expectedMediaPaths[i] {
t.Errorf("Media path at index %d: expected %s, got %s", i, tt.expectedMediaPaths[i], mediaPath)
}
thumb := media.getThumb()
var thumbPath string
if thumb != nil {
thumbPath = thumb.SendData()
}
expectedThumb := tt.expectedThumbPaths[i]
if thumbPath != expectedThumb {
t.Errorf("Thumb path at index %d: expected %s, got %s", i, expectedThumb, thumbPath)
}
}
if len(tt.inputMedia) > 0 && &result[0] == &tt.inputMedia[0] {
t.Error("Result should be a deep copy, not a reference to the original slice")
}
})
}
}
func TestPrepareInputMediaForFiles(t *testing.T) {
tests := []struct {
name string
inputMedia []InputMedia
expectedFiles []string // Just the file names we expect
}{
{
name: "basic media mix",
inputMedia: []InputMedia{
&InputMediaPhoto{
BaseInputMedia: BaseInputMedia{
Type: "photo",
Media: FilePath("tests/image.jpg"),
},
},
&InputMediaVideo{
BaseInputMedia: BaseInputMedia{
Type: "video",
Media: FileURL("https://example.com/video.mp4"), // This doesn't need upload
},
},
&InputMediaVideo{
BaseInputMedia: BaseInputMedia{
Type: "video",
Media: FilePath("tests/video.mp4"),
},
Thumb: FilePath("tests/image.jpg"),
},
&InputPaidMedia{
Type: "video",
Media: &InputMediaVideo{
BaseInputMedia: BaseInputMedia{
Type: "video",
Media: FilePath("tests/video.mp4"),
},
},
Thumb: FilePath("tests/image.jpg"),
},
},
expectedFiles: []string{
"file-0", "file-2", "file-2-thumb", "file-3", "file-3-thumb",
},
},
{
name: "empty media array",
inputMedia: []InputMedia{
// Empty slice
},
expectedFiles: []string{},
},
{
name: "only remote media (nothing to upload)",
inputMedia: []InputMedia{
&InputMediaPhoto{
BaseInputMedia: BaseInputMedia{
Type: "photo",
Media: FileID("existing_photo"),
},
},
&InputMediaVideo{
BaseInputMedia: BaseInputMedia{
Type: "video",
Media: FileURL("https://example.com/video.mp4"),
},
Thumb: FileID("existing_thumb"),
},
},
expectedFiles: []string{},
},
{
name: "complex nested media with both local and remote files",
inputMedia: []InputMedia{
&InputMediaPhoto{
BaseInputMedia: BaseInputMedia{
Type: "photo",
Media: FileID("existing_photo"),
},
},
&InputPaidMedia{
Type: "video",
Media: &InputMediaVideo{
BaseInputMedia: BaseInputMedia{
Type: "video",
Media: FilePath("tests/video.mp4"),
},
Thumb: FileID("existing_thumb"), // Remote thumb
},
Thumb: FilePath("tests/outer_thumb.jpg"), // Local thumb
},
&InputPaidMedia{
Type: "photo",
Media: &InputMediaPhoto{
BaseInputMedia: BaseInputMedia{
Type: "photo",
Media: FileURL("https://example.com/photo.jpg"), // Remote media
},
},
Thumb: FilePath("tests/thumb.jpg"), // Local thumb
},
},
expectedFiles: []string{
"file-1", "file-1-thumb", "file-2-thumb",
},
},
{
name: "all media types needing upload",
inputMedia: []InputMedia{
&InputMediaPhoto{
BaseInputMedia: BaseInputMedia{
Type: "photo",
Media: FilePath("tests/photo.jpg"),
},
},
&InputMediaVideo{
BaseInputMedia: BaseInputMedia{
Type: "video",
Media: FilePath("tests/video.mp4"),
},
Thumb: FilePath("tests/video_thumb.jpg"),
},
&InputMediaAnimation{
BaseInputMedia: BaseInputMedia{
Type: "animation",
Media: FilePath("tests/animation.gif"),
},
Thumb: FilePath("tests/animation_thumb.jpg"),
},
&InputMediaAudio{
BaseInputMedia: BaseInputMedia{
Type: "audio",
Media: FilePath("tests/audio.mp3"),
},
Thumb: FilePath("tests/audio_thumb.jpg"),
},
&InputMediaDocument{
BaseInputMedia: BaseInputMedia{
Type: "document",
Media: FilePath("tests/document.pdf"),
},
Thumb: FilePath("tests/document_thumb.jpg"),
},