Repository navigation
Expand file tree
/
Copy pathjson_test.go
More file actions
189 lines (157 loc) · 4.32 KB
/
Copy pathjson_test.go
File metadata and controls
189 lines (157 loc) · 4.32 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
package stream_chat
import (
"encoding/json"
"math/rand"
"reflect"
"testing"
"github.com/stretchr/testify/require"
)
func randomExtraData(in interface{}) {
v := reflect.ValueOf(in).Elem()
if v.Kind() != reflect.Struct {
return
}
f := v.FieldByName("ExtraData")
f.Set(reflect.ValueOf(map[string]interface{}{
"mystring": randomString(10),
"mybool": rand.Float64() < 0.5,
"data": "custom",
"custom_data": "really_custom",
"extra": map[string]interface{}{
randomString(10): randomString(10),
},
"stream": randomString(10),
"my_score": float64(rand.Intn(100)),
}))
}
func testInvariantJSON(t *testing.T, in, in2 interface{}) {
t.Helper()
// put random
randomExtraData(in)
// marshal given
data, err := json.Marshal(in)
require.NoError(t, err)
// unmarshal again
require.NoError(t, json.Unmarshal(data, in2))
// ensure they are same
require.Equal(t, in, in2)
}
func TestJSON(t *testing.T) {
var c, c2 Channel
testInvariantJSON(t, &c, &c2)
var u, u2 User
testInvariantJSON(t, &u, &u2)
var e, e2 Event
testInvariantJSON(t, &e, &e2)
var m, m2 Message
testInvariantJSON(t, &m, &m2)
var mr, mr2 messageRequestMessage
testInvariantJSON(t, &mr, &mr2)
var a, a2 Attachment
testInvariantJSON(t, &a, &a2)
var r, r2 Reaction
testInvariantJSON(t, &r, &r2)
}
// TestFlattenExtraData tests the flattenExtraData function directly
func TestFlattenExtraData(t *testing.T) {
t.Run("Flatten nested extra_data", func(t *testing.T) {
m := map[string]interface{}{
"field1": "value1",
"extra_data": map[string]interface{}{
"custom_field": "custom_value",
"another_field": 123,
},
}
flattenExtraData(m)
// Fields should be flattened
require.Equal(t, "custom_value", m["custom_field"])
require.Equal(t, 123, m["another_field"])
require.Equal(t, "value1", m["field1"])
// The nested "extra_data" key should not exist
require.NotContains(t, m, "extra_data")
})
t.Run("No extra_data key", func(t *testing.T) {
m := map[string]interface{}{
"field1": "value1",
"field2": 123,
}
flattenExtraData(m)
// Map should be unchanged
require.Equal(t, "value1", m["field1"])
require.Equal(t, 123, m["field2"])
require.Len(t, m, 2)
})
t.Run("extra_data is not a map", func(t *testing.T) {
m := map[string]interface{}{
"field1": "value1",
"extra_data": "not_a_map",
}
flattenExtraData(m)
// extra_data should remain unchanged if it's not a map
require.Equal(t, "not_a_map", m["extra_data"])
require.Equal(t, "value1", m["field1"])
})
t.Run("Empty extra_data map", func(t *testing.T) {
m := map[string]interface{}{
"field1": "value1",
"extra_data": map[string]interface{}{},
}
flattenExtraData(m)
// Empty extra_data should be removed
require.NotContains(t, m, "extra_data")
require.Equal(t, "value1", m["field1"])
})
}
func TestExportUserResponse_UnmarshalJSON(t *testing.T) {
// This is the actual response format returned by the API.
// Previously, ExportUserResponse embedded *User directly, which caused
// User.UnmarshalJSON to consume the entire response body, losing the
// user, messages, and reactions data.
apiResponse := `{
"user": {
"id": "103415720",
"name": "Batman",
"language": "",
"role": "user",
"teams": [],
"created_at": "2025-05-06T19:41:07.894092Z",
"updated_at": "2025-05-06T20:15:52.812595Z",
"banned": false,
"online": false,
"last_active": "2026-03-10T14:09:24.664584Z",
"blocked_user_ids": [],
"shadow_banned": false,
"invisible": false
},
"messages": [
{
"id": "msg1",
"cid": "messaging:general",
"text": "Hello world",
"user": {"id": "103415720"},
"user_id": "103415720"
}
],
"reactions": [
{
"message_id": "msg1",
"user_id": "103415720",
"type": "like"
}
],
"duration": "117.63ms"
}`
var resp ExportUserResponse
err := json.Unmarshal([]byte(apiResponse), &resp)
require.NoError(t, err)
require.NotNil(t, resp.User)
require.Equal(t, "103415720", resp.User.ID)
require.Equal(t, "Batman", resp.User.Name)
require.Equal(t, "user", resp.User.Role)
require.Len(t, resp.Messages, 1)
require.Equal(t, "msg1", resp.Messages[0].ID)
require.Equal(t, "Hello world", resp.Messages[0].Text)
require.Len(t, resp.Reactions, 1)
require.Equal(t, "msg1", resp.Reactions[0].MessageID)
require.Equal(t, "like", resp.Reactions[0].Type)
}