-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmessage_test.go
More file actions
350 lines (308 loc) · 7.7 KB
/
message_test.go
File metadata and controls
350 lines (308 loc) · 7.7 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
package samhook
import (
"reflect"
"strings"
"testing"
"github.com/bytedance/sonic"
)
func TestMessage_JSONSerialization(t *testing.T) {
tests := []struct {
name string
message Message
want string
}{
{
name: "基本訊息",
message: Message{
Text: "Hello",
Username: "bot",
},
want: `{"text":"Hello","username":"bot"}`,
},
{
name: "完整訊息",
message: Message{
Text: "Hello",
Username: "bot",
IconEmoji: ":robot_face:",
Channel: "#general",
},
want: `{"text":"Hello","username":"bot","icon_emoji":":robot_face:","channel":"#general"}`,
},
{
name: "空訊息",
message: Message{},
want: `{}`,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := sonic.Marshal(tt.message)
if err != nil {
t.Fatalf("sonic.Marshal() error = %v", err)
}
// 解析 JSON 進行比較(忽略順序)
var gotMap, wantMap map[string]interface{}
if err := sonic.Unmarshal(got, &gotMap); err != nil {
t.Fatalf("failed to unmarshal got: %v", err)
}
if err := sonic.Unmarshal([]byte(tt.want), &wantMap); err != nil {
t.Fatalf("failed to unmarshal want: %v", err)
}
if !reflect.DeepEqual(gotMap, wantMap) {
t.Errorf("sonic.Marshal() = %s, want %s", got, tt.want)
}
})
}
}
func TestMessage_OmitEmpty(t *testing.T) {
msg := Message{
Text: "Hello",
// 其他欄位為空
}
data, err := sonic.Marshal(msg)
if err != nil {
t.Fatalf("sonic.Marshal() error = %v", err)
}
jsonStr := string(data)
// 驗證空欄位被省略
if strings.Contains(jsonStr, "username") {
t.Error("empty username should be omitted")
}
if strings.Contains(jsonStr, "icon_url") {
t.Error("empty icon_url should be omitted")
}
if strings.Contains(jsonStr, "attachments") {
t.Error("empty attachments should be omitted")
}
// 驗證非空欄位被包含
if !strings.Contains(jsonStr, "text") {
t.Error("non-empty text should be included")
}
}
func TestMessage_JSONTags(t *testing.T) {
msg := Message{
Text: "test",
Username: "bot",
IconURL: "https://example.com/icon.png",
IconEmoji: ":emoji:",
Channel: "#channel",
}
data, err := sonic.Marshal(msg)
if err != nil {
t.Fatalf("sonic.Marshal() error = %v", err)
}
jsonStr := string(data)
// 驗證 JSON 標籤與 Slack API 規範一致
expectedTags := []string{
`"text"`,
`"username"`,
`"icon_url"`, // 不是 iconUrl
`"icon_emoji"`, // 不是 iconEmoji
`"channel"`,
}
for _, tag := range expectedTags {
if !strings.Contains(jsonStr, tag) {
t.Errorf("expected JSON tag %s not found in %s", tag, jsonStr)
}
}
}
func TestMessage_RoundTrip(t *testing.T) {
original := Message{
Text: "Test message",
Username: "test-bot",
IconEmoji: ":robot_face:",
Channel: "#test",
Attachments: []Attachment{
{
Color: Good,
Title: "Test",
},
},
}
// 序列化
data, err := sonic.Marshal(original)
if err != nil {
t.Fatalf("sonic.Marshal() error = %v", err)
}
// 反序列化
var unmarshaled Message
if err := sonic.Unmarshal(data, &unmarshaled); err != nil {
t.Fatalf("sonic.Unmarshal() error = %v", err)
}
// 比較
if unmarshaled.Text != original.Text {
t.Errorf("Text: got %q, want %q", unmarshaled.Text, original.Text)
}
if unmarshaled.Username != original.Username {
t.Errorf("Username: got %q, want %q", unmarshaled.Username, original.Username)
}
if len(unmarshaled.Attachments) != len(original.Attachments) {
t.Errorf("Attachments length: got %d, want %d",
len(unmarshaled.Attachments), len(original.Attachments))
}
}
func TestAttachment_JSONSerialization(t *testing.T) {
attachment := Attachment{
Color: Good,
Title: "Test Title",
TitleLink: "https://example.com",
Text: "Test text",
Fallback: "Fallback text",
AuthorName: "Test Author",
Fields: []Field{
{Title: "Field 1", Value: "Value 1", Short: true},
{Title: "Field 2", Value: "Value 2", Short: false},
},
}
data, err := sonic.Marshal(attachment)
if err != nil {
t.Fatalf("sonic.Marshal() error = %v", err)
}
// 驗證關鍵欄位存在
jsonStr := string(data)
expectedFields := []string{
`"color"`,
`"title"`,
`"text"`,
`"fallback"`,
`"fields"`,
}
for _, field := range expectedFields {
if !strings.Contains(jsonStr, field) {
t.Errorf("expected field %s not found", field)
}
}
// 驗證 JSON 標籤正確性(使用下劃線)
if !strings.Contains(jsonStr, `"author_name"`) {
t.Error("expected author_name tag (with underscore)")
}
if !strings.Contains(jsonStr, `"title_link"`) {
t.Error("expected title_link tag (with underscore)")
}
}
func TestAttachment_OmitEmpty(t *testing.T) {
attachment := Attachment{
Color: Good,
Title: "Test",
// 其他欄位為空
}
data, err := sonic.Marshal(attachment)
if err != nil {
t.Fatalf("sonic.Marshal() error = %v", err)
}
jsonStr := string(data)
// 空欄位應該被省略
if strings.Contains(jsonStr, "pretext") {
t.Error("empty pretext should be omitted")
}
if strings.Contains(jsonStr, "fields") {
t.Error("empty fields should be omitted")
}
}
func TestField_JSONSerialization(t *testing.T) {
tests := []struct {
name string
field Field
want string
}{
{
name: "短欄位",
field: Field{
Title: "Title",
Value: "Value",
Short: true,
},
want: `{"title":"Title","value":"Value","short":true}`,
},
{
name: "長欄位",
field: Field{
Title: "Title",
Value: "Value",
Short: false,
},
// 注意:false 值在 omitempty 時會被省略,所以期望結果不包含 short
want: `{"title":"Title","value":"Value"}`,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := sonic.Marshal(tt.field)
if err != nil {
t.Fatalf("sonic.Marshal() error = %v", err)
}
var gotMap, wantMap map[string]interface{}
sonic.Unmarshal(got, &gotMap)
sonic.Unmarshal([]byte(tt.want), &wantMap)
if !reflect.DeepEqual(gotMap, wantMap) {
t.Errorf("sonic.Marshal() = %s, want %s", got, tt.want)
}
})
}
}
func TestField_BooleanSerialization(t *testing.T) {
field := Field{
Title: "Test",
Value: "Value",
Short: true,
}
data, err := sonic.Marshal(field)
if err != nil {
t.Fatalf("sonic.Marshal() error = %v", err)
}
// 驗證布林值正確序列化為 true/false(不是 1/0)
jsonStr := string(data)
if !strings.Contains(jsonStr, `"short":true`) {
t.Error("boolean should be serialized as true, not 1")
}
}
func TestMessage_WithAttachments(t *testing.T) {
msg := Message{
Text: "Test",
Attachments: []Attachment{
{
Color: Good,
Title: "Attachment 1",
Fields: []Field{
{Title: "Field 1", Value: "Value 1", Short: true},
},
},
{
Color: Warning,
Title: "Attachment 2",
},
},
}
data, err := sonic.Marshal(msg)
if err != nil {
t.Fatalf("sonic.Marshal() error = %v", err)
}
// 驗證嵌套結構正確序列化
jsonStr := string(data)
if !strings.Contains(jsonStr, `"attachments"`) {
t.Error("attachments should be included")
}
if !strings.Contains(jsonStr, `"fields"`) {
t.Error("nested fields should be included")
}
// 驗證可以反序列化
var unmarshaled Message
if err := sonic.Unmarshal(data, &unmarshaled); err != nil {
t.Fatalf("sonic.Unmarshal() error = %v", err)
}
if len(unmarshaled.Attachments) != 2 {
t.Errorf("expected 2 attachments, got %d", len(unmarshaled.Attachments))
}
}
func TestColorConstants(t *testing.T) {
if Warning != "#FFBB00" {
t.Errorf("Warning color incorrect: expected #FFBB00, got %s", Warning)
}
if Danger != "#FF0000" {
t.Errorf("Danger color incorrect: expected #FF0000, got %s", Danger)
}
if Good != "#00FF00" {
t.Errorf("Good color incorrect: expected #00FF00, got %s", Good)
}
}