|
| 1 | +package botapi |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/gotd/td/constant" |
| 8 | + "github.com/gotd/td/tg" |
| 9 | +) |
| 10 | + |
| 11 | +func TestSetMessageReaction(t *testing.T) { |
| 12 | + inv := newMockInvoker() |
| 13 | + inv.reply(tg.MessagesSendReactionRequestTypeID, okUpdates()) |
| 14 | + |
| 15 | + b := newMockBot(inv) |
| 16 | + |
| 17 | + reactions := []ReactionType{Emoji("👍")} |
| 18 | + if err := b.SetMessageReaction(context.Background(), userRef(10, 1), 42, reactions, WithBigReaction()); err != nil { |
| 19 | + t.Fatalf("SetMessageReaction: %v", err) |
| 20 | + } |
| 21 | + |
| 22 | + var req tg.MessagesSendReactionRequest |
| 23 | + |
| 24 | + inv.decode(t, tg.MessagesSendReactionRequestTypeID, &req) |
| 25 | + |
| 26 | + if req.MsgID != 42 || !req.Big { |
| 27 | + t.Fatalf("req = %#v", req) |
| 28 | + } |
| 29 | + |
| 30 | + if len(req.Reaction) != 1 { |
| 31 | + t.Fatalf("reactions = %#v", req.Reaction) |
| 32 | + } |
| 33 | + |
| 34 | + emoji, ok := req.Reaction[0].(*tg.ReactionEmoji) |
| 35 | + if !ok || emoji.Emoticon != "👍" { |
| 36 | + t.Fatalf("reaction = %#v", req.Reaction[0]) |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +func TestSetMessageReactionEmptyClears(t *testing.T) { |
| 41 | + inv := newMockInvoker() |
| 42 | + inv.reply(tg.MessagesSendReactionRequestTypeID, okUpdates()) |
| 43 | + |
| 44 | + if err := newMockBot(inv).SetMessageReaction(context.Background(), userRef(10, 1), 1, nil); err != nil { |
| 45 | + t.Fatalf("SetMessageReaction: %v", err) |
| 46 | + } |
| 47 | + |
| 48 | + var req tg.MessagesSendReactionRequest |
| 49 | + |
| 50 | + inv.decode(t, tg.MessagesSendReactionRequestTypeID, &req) |
| 51 | + |
| 52 | + if len(req.Reaction) != 0 { |
| 53 | + t.Fatalf("reactions = %#v, want empty", req.Reaction) |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +func TestReactionToTgCustomEmoji(t *testing.T) { |
| 58 | + got, err := reactionToTg(CustomEmoji("12345")) |
| 59 | + if err != nil { |
| 60 | + t.Fatalf("reactionToTg: %v", err) |
| 61 | + } |
| 62 | + |
| 63 | + custom, ok := got.(*tg.ReactionCustomEmoji) |
| 64 | + if !ok || custom.DocumentID != 12345 { |
| 65 | + t.Fatalf("reaction = %#v", got) |
| 66 | + } |
| 67 | + |
| 68 | + if _, err := reactionToTg(CustomEmoji("notanumber")); err == nil { |
| 69 | + t.Fatal("expected error for invalid custom_emoji_id") |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +func TestChatJoinRequests(t *testing.T) { |
| 74 | + for _, c := range []struct { |
| 75 | + name string |
| 76 | + call func(*Bot) error |
| 77 | + approved bool |
| 78 | + }{ |
| 79 | + {"approve", func(b *Bot) error { |
| 80 | + return b.ApproveChatJoinRequest(context.Background(), tdlibChannel(50), 99) |
| 81 | + }, true}, |
| 82 | + {"decline", func(b *Bot) error { |
| 83 | + return b.DeclineChatJoinRequest(context.Background(), tdlibChannel(50), 99) |
| 84 | + }, false}, |
| 85 | + } { |
| 86 | + t.Run(c.name, func(t *testing.T) { |
| 87 | + inv := newMockInvoker() |
| 88 | + inv.reply(tg.MessagesHideChatJoinRequestRequestTypeID, okUpdates()) |
| 89 | + |
| 90 | + if err := c.call(newMockBot(inv)); err != nil { |
| 91 | + t.Fatalf("call: %v", err) |
| 92 | + } |
| 93 | + |
| 94 | + var req tg.MessagesHideChatJoinRequestRequest |
| 95 | + |
| 96 | + inv.decode(t, tg.MessagesHideChatJoinRequestRequestTypeID, &req) |
| 97 | + |
| 98 | + if req.Approved != c.approved { |
| 99 | + t.Fatalf("approved = %v, want %v", req.Approved, c.approved) |
| 100 | + } |
| 101 | + }) |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +func TestBanUnbanChatSenderChat(t *testing.T) { |
| 106 | + var p constant.TDLibPeerID |
| 107 | + |
| 108 | + p.Channel(60) |
| 109 | + |
| 110 | + senderID := int64(p) |
| 111 | + |
| 112 | + for _, c := range []struct { |
| 113 | + name string |
| 114 | + call func(*Bot) error |
| 115 | + view bool |
| 116 | + }{ |
| 117 | + {"ban", func(b *Bot) error { |
| 118 | + return b.BanChatSenderChat(context.Background(), tdlibChannel(50), senderID) |
| 119 | + }, true}, |
| 120 | + {"unban", func(b *Bot) error { |
| 121 | + return b.UnbanChatSenderChat(context.Background(), tdlibChannel(50), senderID) |
| 122 | + }, false}, |
| 123 | + } { |
| 124 | + t.Run(c.name, func(t *testing.T) { |
| 125 | + inv := newMockInvoker() |
| 126 | + inv.reply(tg.ChannelsEditBannedRequestTypeID, okUpdates()) |
| 127 | + |
| 128 | + if err := c.call(newMockBot(inv)); err != nil { |
| 129 | + t.Fatalf("call: %v", err) |
| 130 | + } |
| 131 | + |
| 132 | + var req tg.ChannelsEditBannedRequest |
| 133 | + |
| 134 | + inv.decode(t, tg.ChannelsEditBannedRequestTypeID, &req) |
| 135 | + |
| 136 | + if _, ok := req.Participant.(*tg.InputPeerChannel); !ok { |
| 137 | + t.Fatalf("participant = %#v, want channel", req.Participant) |
| 138 | + } |
| 139 | + |
| 140 | + if req.BannedRights.ViewMessages != c.view { |
| 141 | + t.Fatalf("ViewMessages = %v, want %v", req.BannedRights.ViewMessages, c.view) |
| 142 | + } |
| 143 | + }) |
| 144 | + } |
| 145 | +} |
0 commit comments