-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy paththread_test.go
160 lines (137 loc) · 5 KB
/
thread_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
148
149
150
151
152
153
154
155
156
157
158
159
160
package stream_chat
import (
"context"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestClient_QueryThreads(t *testing.T) {
c := initClient(t)
ctx := context.Background()
t.Run("basic query", func(t *testing.T) {
membersID, ch, parentMsg, replyMsg := testThreadSetup(t, c, 3)
limit := 10
query := &QueryThreadsRequest{
Filter: map[string]any{
"channel_cid": map[string]any{
"$eq": ch.CID,
},
},
Sort: &SortParamRequestList{
{
Field: "created_at",
Direction: -1,
},
},
PagerRequest: PagerRequest{
Limit: &limit,
},
UserID: membersID[0],
}
resp, err := c.QueryThreads(ctx, query)
require.NoError(t, err)
require.NotNil(t, resp, "response should not be nil")
require.NotEmpty(t, resp.Threads, "threads should not be empty")
thread := resp.Threads[0]
assertThreadData(t, thread, ch, parentMsg, replyMsg)
assertThreadParticipants(t, thread, ch.CreatedBy.ID)
assert.Empty(t, resp.PagerResponse)
})
t.Run("with pagination", func(t *testing.T) {
membersID, ch, parentMsg1, replyMsg1 := testThreadSetup(t, c, 3)
limit := 1
// Create a second thread
parentMsg2, err := ch.SendMessage(ctx, &Message{Text: "Parent message for thread 2"}, ch.CreatedBy.ID)
require.NoError(t, err, "send second parent message")
replyMsg2, err := ch.SendMessage(ctx, &Message{
Text: "Reply message 2",
ParentID: parentMsg2.Message.ID,
}, ch.CreatedBy.ID)
require.NoError(t, err, "send second reply message")
// First page query
query := &QueryThreadsRequest{
Filter: map[string]any{
"channel_cid": map[string]any{
"$eq": ch.CID,
},
},
Sort: &SortParamRequestList{
{
Field: "created_at",
Direction: 1,
},
},
PagerRequest: PagerRequest{
Limit: &limit,
},
UserID: membersID[0],
}
resp, err := c.QueryThreads(ctx, query)
require.NoError(t, err)
require.NotNil(t, resp, "response should not be nil")
require.NotEmpty(t, resp.Threads, "threads should not be empty")
thread := resp.Threads[0]
assertThreadData(t, thread, ch, parentMsg1, replyMsg1)
assertThreadParticipants(t, thread, ch.CreatedBy.ID)
// Second page query
query2 := &QueryThreadsRequest{
Filter: map[string]any{
"channel_cid": map[string]any{
"$eq": ch.CID,
},
},
Sort: &SortParamRequestList{
{
Field: "created_at",
Direction: -1,
},
},
PagerRequest: PagerRequest{
Limit: &limit,
Next: resp.Next,
},
UserID: membersID[0],
}
resp, err = c.QueryThreads(ctx, query2)
require.NoError(t, err)
require.NotNil(t, resp, "response should not be nil")
require.NotEmpty(t, resp.Threads, "threads should not be empty")
thread = resp.Threads[0]
assertThreadData(t, thread, ch, parentMsg2, replyMsg2)
assertThreadParticipants(t, thread, ch.CreatedBy.ID)
})
}
// testThreadSetup creates a channel with members and returns necessary test data
func testThreadSetup(t *testing.T, c *Client, numMembers int) ([]string, *Channel, *MessageResponse, *MessageResponse) {
membersID := randomUsersID(t, c, numMembers)
ch := initChannel(t, c, membersID...)
// Create a parent message
parentMsg, err := ch.SendMessage(context.Background(), &Message{Text: "Parent message for thread"}, ch.CreatedBy.ID)
require.NoError(t, err, "send parent message")
// Create a thread by sending a reply
replyMsg, err := ch.SendMessage(context.Background(), &Message{
Text: "Reply message",
ParentID: parentMsg.Message.ID,
}, ch.CreatedBy.ID)
require.NoError(t, err, "send reply message")
return membersID, ch, parentMsg, replyMsg
}
// assertThreadData validates common thread data fields
func assertThreadData(t *testing.T, thread ThreadResponse, ch *Channel, parentMsg, replyMsg *MessageResponse) {
assert.Equal(t, ch.CID, thread.ChannelCID, "channel CID should match")
assert.Equal(t, parentMsg.Message.ID, thread.ParentMessageID, "parent message ID should match")
assert.Equal(t, ch.CreatedBy.ID, thread.CreatedByUserID, "created by user ID should match")
assert.Equal(t, 1, thread.ReplyCount, "reply count should be 1")
assert.Equal(t, 1, thread.ParticipantCount, "participant count should be 1")
assert.Equal(t, parentMsg.Message.Text, thread.Title, "title should not be empty")
assert.Equal(t, replyMsg.Message.CreatedAt, thread.CreatedAt, "created at should not be zero")
assert.Equal(t, replyMsg.Message.UpdatedAt, thread.UpdatedAt, "updated at should not be zero")
assert.Nil(t, thread.DeletedAt, "deleted at should be nil")
}
// assertThreadParticipants validates thread participant data
func assertThreadParticipants(t *testing.T, thread ThreadResponse, createdByID string) {
require.Len(t, thread.Participants, 1, "should have one participant")
assert.Equal(t, createdByID, thread.Participants[0].UserID, "participant user ID should match")
assert.NotZero(t, thread.Participants[0].CreatedAt, "participant created at should not be zero")
assert.NotZero(t, thread.Participants[0].LastReadAt, "participant last read at should not be zero")
}