forked from go-lark/lark
-
Notifications
You must be signed in to change notification settings - Fork 0
/
msg_buf_test.go
147 lines (127 loc) · 4.95 KB
/
msg_buf_test.go
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
package lark
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestAttachText(t *testing.T) {
mb := NewMsgBuffer(MsgText)
msg := mb.Text("hello").Build()
assert.Equal(t, "hello", msg.Content.Text.Text)
}
func TestAttachImage(t *testing.T) {
mb := NewMsgBuffer(MsgImage)
msg := mb.Image("aaaaa").Build()
assert.Equal(t, "aaaaa", msg.Content.Image.ImageKey)
}
func TestMsgTextBinding(t *testing.T) {
mb := NewMsgBuffer(MsgText)
msg := mb.Text("hello, world").BindEmail(testUserEmail).Build()
assert.Equal(t, "hello, world", msg.Content.Text.Text)
assert.Equal(t, testUserEmail, msg.Email)
}
func TestBindingUserIDs(t *testing.T) {
mb := NewMsgBuffer(MsgText)
msgEmail := mb.BindEmail(testUserEmail).Build()
assert.Equal(t, testUserEmail, msgEmail.Email)
mb.Clear()
msgOpenChatID := mb.BindOpenChatID(testGroupChatID).Build()
assert.Equal(t, testGroupChatID, msgOpenChatID.ChatID)
mb.Clear()
msgUserID := mb.BindUserID("333444").Build()
assert.Equal(t, "333444", msgUserID.UserID)
mb.Clear()
msgReplyID := mb.BindReply("om_f779ffe0ffa3d1b94fc1ef5fcb6f1063").Build()
assert.Equal(t, "om_f779ffe0ffa3d1b94fc1ef5fcb6f1063", msgReplyID.RootID)
mb.ReplyInThread(true)
assert.False(t, msgReplyID.ReplyInThread)
}
func TestMsgShareChat(t *testing.T) {
mb := NewMsgBuffer(MsgShareCard)
msg := mb.ShareChat("6559399282837815565").Build()
assert.Equal(t, MsgShareCard, msg.MsgType)
assert.Equal(t, "6559399282837815565", msg.Content.ShareChat.ChatID)
}
func TestMsgShareUser(t *testing.T) {
mb := NewMsgBuffer(MsgShareUser)
msg := mb.ShareUser("334455").Build()
assert.Equal(t, MsgShareUser, msg.MsgType)
assert.Equal(t, "334455", msg.Content.ShareUser.UserID)
}
func TestMsgFile(t *testing.T) {
mb := NewMsgBuffer(MsgFile)
msg := mb.File("file_v2_71cafb2c-137f-4bb0-8381-ffd4971dbecg").Build()
assert.Equal(t, MsgFile, msg.MsgType)
assert.Equal(t, "file_v2_71cafb2c-137f-4bb0-8381-ffd4971dbecg", msg.Content.File.FileKey)
}
func TestMsgAudio(t *testing.T) {
mb := NewMsgBuffer(MsgAudio)
msg := mb.Audio("file_v2_71cafb2c-137f-4bb0-8381-ffd4971dbecg").Build()
assert.Equal(t, MsgAudio, msg.MsgType)
assert.Equal(t, "file_v2_71cafb2c-137f-4bb0-8381-ffd4971dbecg", msg.Content.Audio.FileKey)
}
func TestMsgMedia(t *testing.T) {
mb := NewMsgBuffer(MsgMedia)
msg := mb.Media("file_v2_b53cd6cc-5327-4968-8bf6-4528deb3068g", "img_v2_b276195a-9ae0-4fec-bbfe-f74b4d5a994g").Build()
assert.Equal(t, MsgMedia, msg.MsgType)
assert.Equal(t, "file_v2_b53cd6cc-5327-4968-8bf6-4528deb3068g", msg.Content.Media.FileKey)
assert.Equal(t, "img_v2_b276195a-9ae0-4fec-bbfe-f74b4d5a994g", msg.Content.Media.ImageKey)
}
func TestMsgSticker(t *testing.T) {
mb := NewMsgBuffer(MsgSticker)
msg := mb.Sticker("4ba009df-2453-47b3-a753-444b152217bg").Build()
assert.Equal(t, MsgSticker, msg.MsgType)
assert.Equal(t, "4ba009df-2453-47b3-a753-444b152217bg", msg.Content.Sticker.FileKey)
}
func TestMsgWithWrongType(t *testing.T) {
mb := NewMsgBuffer(MsgText)
mb.ShareChat("6559399282837815565")
assert.Equal(t, mb.Error().Error(), "`ShareChat` is only available to `share_chat`")
mb.ShareUser("334455")
assert.Equal(t, mb.Error().Error(), "`ShareUser` is only available to `share_user`")
mb.Image("aaa")
assert.Equal(t, mb.Error().Error(), "`Image` is only available to `image`")
mb.File("aaa")
assert.Equal(t, mb.Error().Error(), "`File` is only available to `file`")
mb.Audio("aaa")
assert.Equal(t, mb.Error().Error(), "`Audio` is only available to `audio`")
mb.Media("aaa", "bbb")
assert.Equal(t, mb.Error().Error(), "`Media` is only available to `media`")
mb.Sticker("aaa")
assert.Equal(t, mb.Error().Error(), "`Sticker` is only available to `sticker`")
mb.Post(nil)
assert.Equal(t, mb.Error().Error(), "`Post` is only available to `post`")
mb.Card("nil")
assert.Equal(t, mb.Error().Error(), "`Card` is only available to `interactive`")
mbp := NewMsgBuffer(MsgPost)
mbp.Text("hello")
assert.Equal(t, mbp.Error().Error(), "`Text` is only available to `text`")
}
func TestClearMessage(t *testing.T) {
mb := NewMsgBuffer(MsgText)
mb.Text("hello, world").Build()
assert.Equal(t, "hello, world", mb.message.Content.Text.Text)
mb.Clear()
assert.Equal(t, MsgText, mb.msgType)
assert.Empty(t, mb.message.Content)
mb.Text("attach again").Build()
assert.Equal(t, "attach again", mb.message.Content.Text.Text)
}
func TestWorkWithTextBuilder(t *testing.T) {
mb := NewMsgBuffer(MsgText)
mb.Text(NewTextBuilder().Textln("hello, world").Render()).Build()
assert.Equal(t, "hello, world\n", mb.message.Content.Text.Text)
}
func TestWithSign(t *testing.T) {
mb := NewMsgBuffer(MsgText)
assert.Empty(t, mb.message.Sign)
msg := mb.WithSign("xxx", 1661860880).Build()
assert.NotEmpty(t, mb.message.Sign)
assert.Equal(t, "QnWVTSBe6FmQDE0bG6X0mURbI+DnvVyu1h+j5dHOjrU=", msg.Sign)
}
func TestWithUUID(t *testing.T) {
mb := NewMsgBuffer(MsgText)
assert.Empty(t, mb.message.UUID)
msg := mb.WithUUID("abc-def-0000").Build()
assert.NotEmpty(t, mb.message.UUID)
assert.Equal(t, "abc-def-0000", msg.UUID)
}