|
| 1 | +package stream_chat |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/stretchr/testify/assert" |
| 8 | + "github.com/stretchr/testify/require" |
| 9 | +) |
| 10 | + |
| 11 | +func TestClient_QueryThreads(t *testing.T) { |
| 12 | + c := initClient(t) |
| 13 | + ctx := context.Background() |
| 14 | + |
| 15 | + t.Run("basic query", func(t *testing.T) { |
| 16 | + membersID, ch, parentMsg, replyMsg := testThreadSetup(t, c, 3) |
| 17 | + |
| 18 | + limit := 10 |
| 19 | + query := &QueryThreadsRequest{ |
| 20 | + Filter: map[string]any{ |
| 21 | + "channel_cid": map[string]any{ |
| 22 | + "$eq": ch.CID, |
| 23 | + }, |
| 24 | + }, |
| 25 | + Sort: &SortParamRequestList{ |
| 26 | + { |
| 27 | + Field: "created_at", |
| 28 | + Direction: -1, |
| 29 | + }, |
| 30 | + }, |
| 31 | + PagerRequest: PagerRequest{ |
| 32 | + Limit: &limit, |
| 33 | + }, |
| 34 | + UserID: membersID[0], |
| 35 | + } |
| 36 | + |
| 37 | + resp, err := c.QueryThreads(ctx, query) |
| 38 | + require.NoError(t, err) |
| 39 | + require.NotNil(t, resp, "response should not be nil") |
| 40 | + require.NotEmpty(t, resp.Threads, "threads should not be empty") |
| 41 | + |
| 42 | + thread := resp.Threads[0] |
| 43 | + assertThreadData(t, thread, ch, parentMsg, replyMsg) |
| 44 | + assertThreadParticipants(t, thread, ch.CreatedBy.ID) |
| 45 | + |
| 46 | + assert.Empty(t, resp.PagerResponse) |
| 47 | + }) |
| 48 | + |
| 49 | + t.Run("with pagination", func(t *testing.T) { |
| 50 | + membersID, ch, parentMsg1, replyMsg1 := testThreadSetup(t, c, 3) |
| 51 | + limit := 1 |
| 52 | + |
| 53 | + // Create a second thread |
| 54 | + parentMsg2, err := ch.SendMessage(ctx, &Message{Text: "Parent message for thread 2"}, ch.CreatedBy.ID) |
| 55 | + require.NoError(t, err, "send second parent message") |
| 56 | + |
| 57 | + replyMsg2, err := ch.SendMessage(ctx, &Message{ |
| 58 | + Text: "Reply message 2", |
| 59 | + ParentID: parentMsg2.Message.ID, |
| 60 | + }, ch.CreatedBy.ID) |
| 61 | + require.NoError(t, err, "send second reply message") |
| 62 | + |
| 63 | + // First page query |
| 64 | + query := &QueryThreadsRequest{ |
| 65 | + Filter: map[string]any{ |
| 66 | + "channel_cid": map[string]any{ |
| 67 | + "$eq": ch.CID, |
| 68 | + }, |
| 69 | + }, |
| 70 | + Sort: &SortParamRequestList{ |
| 71 | + { |
| 72 | + Field: "created_at", |
| 73 | + Direction: 1, |
| 74 | + }, |
| 75 | + }, |
| 76 | + PagerRequest: PagerRequest{ |
| 77 | + Limit: &limit, |
| 78 | + }, |
| 79 | + UserID: membersID[0], |
| 80 | + } |
| 81 | + |
| 82 | + resp, err := c.QueryThreads(ctx, query) |
| 83 | + require.NoError(t, err) |
| 84 | + require.NotNil(t, resp, "response should not be nil") |
| 85 | + require.NotEmpty(t, resp.Threads, "threads should not be empty") |
| 86 | + |
| 87 | + thread := resp.Threads[0] |
| 88 | + assertThreadData(t, thread, ch, parentMsg1, replyMsg1) |
| 89 | + assertThreadParticipants(t, thread, ch.CreatedBy.ID) |
| 90 | + |
| 91 | + // Second page query |
| 92 | + query2 := &QueryThreadsRequest{ |
| 93 | + Filter: map[string]any{ |
| 94 | + "channel_cid": map[string]any{ |
| 95 | + "$eq": ch.CID, |
| 96 | + }, |
| 97 | + }, |
| 98 | + Sort: &SortParamRequestList{ |
| 99 | + { |
| 100 | + Field: "created_at", |
| 101 | + Direction: -1, |
| 102 | + }, |
| 103 | + }, |
| 104 | + PagerRequest: PagerRequest{ |
| 105 | + Limit: &limit, |
| 106 | + Next: resp.Next, |
| 107 | + }, |
| 108 | + UserID: membersID[0], |
| 109 | + } |
| 110 | + |
| 111 | + resp, err = c.QueryThreads(ctx, query2) |
| 112 | + require.NoError(t, err) |
| 113 | + require.NotNil(t, resp, "response should not be nil") |
| 114 | + require.NotEmpty(t, resp.Threads, "threads should not be empty") |
| 115 | + |
| 116 | + thread = resp.Threads[0] |
| 117 | + assertThreadData(t, thread, ch, parentMsg2, replyMsg2) |
| 118 | + assertThreadParticipants(t, thread, ch.CreatedBy.ID) |
| 119 | + }) |
| 120 | +} |
| 121 | + |
| 122 | +// testThreadSetup creates a channel with members and returns necessary test data |
| 123 | +func testThreadSetup(t *testing.T, c *Client, numMembers int) ([]string, *Channel, *MessageResponse, *MessageResponse) { |
| 124 | + membersID := randomUsersID(t, c, numMembers) |
| 125 | + ch := initChannel(t, c, membersID...) |
| 126 | + |
| 127 | + // Create a parent message |
| 128 | + parentMsg, err := ch.SendMessage(context.Background(), &Message{Text: "Parent message for thread"}, ch.CreatedBy.ID) |
| 129 | + require.NoError(t, err, "send parent message") |
| 130 | + |
| 131 | + // Create a thread by sending a reply |
| 132 | + replyMsg, err := ch.SendMessage(context.Background(), &Message{ |
| 133 | + Text: "Reply message", |
| 134 | + ParentID: parentMsg.Message.ID, |
| 135 | + }, ch.CreatedBy.ID) |
| 136 | + require.NoError(t, err, "send reply message") |
| 137 | + |
| 138 | + return membersID, ch, parentMsg, replyMsg |
| 139 | +} |
| 140 | + |
| 141 | +// assertThreadData validates common thread data fields |
| 142 | +func assertThreadData(t *testing.T, thread ThreadResponse, ch *Channel, parentMsg, replyMsg *MessageResponse) { |
| 143 | + assert.Equal(t, ch.CID, thread.ChannelCID, "channel CID should match") |
| 144 | + assert.Equal(t, parentMsg.Message.ID, thread.ParentMessageID, "parent message ID should match") |
| 145 | + assert.Equal(t, ch.CreatedBy.ID, thread.CreatedByUserID, "created by user ID should match") |
| 146 | + assert.Equal(t, 1, thread.ReplyCount, "reply count should be 1") |
| 147 | + assert.Equal(t, 1, thread.ParticipantCount, "participant count should be 1") |
| 148 | + assert.Equal(t, parentMsg.Message.Text, thread.Title, "title should not be empty") |
| 149 | + assert.Equal(t, replyMsg.Message.CreatedAt, thread.CreatedAt, "created at should not be zero") |
| 150 | + assert.Equal(t, replyMsg.Message.UpdatedAt, thread.UpdatedAt, "updated at should not be zero") |
| 151 | + assert.Nil(t, thread.DeletedAt, "deleted at should be nil") |
| 152 | +} |
| 153 | + |
| 154 | +// assertThreadParticipants validates thread participant data |
| 155 | +func assertThreadParticipants(t *testing.T, thread ThreadResponse, createdByID string) { |
| 156 | + require.Len(t, thread.Participants, 1, "should have one participant") |
| 157 | + assert.Equal(t, createdByID, thread.Participants[0].UserID, "participant user ID should match") |
| 158 | + assert.NotZero(t, thread.Participants[0].CreatedAt, "participant created at should not be zero") |
| 159 | + assert.NotZero(t, thread.Participants[0].LastReadAt, "participant last read at should not be zero") |
| 160 | +} |
0 commit comments