-
Notifications
You must be signed in to change notification settings - Fork 31
/
message_history_test.go
90 lines (75 loc) · 2.8 KB
/
message_history_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
package stream_chat
import (
"context"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestMessageHistory(t *testing.T) {
client := initClient(t)
users := randomUsers(t, client, 2)
user1 := users[0]
user2 := users[1]
ch := initChannel(t, client, user1.ID)
ctx := context.Background()
initialText := "initial text"
customField := "custom_field"
initialCustomFieldValue := "custom value"
// send a message with initial text (user1)
response, err := ch.SendMessage(ctx, &Message{Text: initialText, ExtraData: map[string]interface{}{customField: initialCustomFieldValue}}, user1.ID)
require.NoError(t, err)
message := response.Message
updatedText1 := "updated text"
updatedCustomFieldValue := "updated custom value"
// update the message by user1
_, err = client.UpdateMessage(ctx, &Message{Text: updatedText1, ExtraData: map[string]interface{}{customField: updatedCustomFieldValue}, UserID: user1.ID}, message.ID)
assert.NoError(t, err)
updatedText2 := "updated text 2"
// update the message by user2
_, err = client.UpdateMessage(ctx, &Message{Text: updatedText2, UserID: user2.ID}, message.ID)
assert.NoError(t, err)
t.Run("test query", func(t *testing.T) {
req := QueryMessageHistoryRequest{
Filter: map[string]interface{}{
"message_id": message.ID,
},
}
messageHistoryResponse, err := client.QueryMessageHistory(ctx, req)
assert.NoError(t, err)
assert.NotNil(t, messageHistoryResponse)
history := messageHistoryResponse.MessageHistory
assert.Equal(t, 2, len(history))
firstUpdate := history[1]
assert.Equal(t, initialText, firstUpdate.Text)
assert.Equal(t, user1.ID, firstUpdate.MessageUpdatedByID)
assert.Equal(t, initialCustomFieldValue, firstUpdate.ExtraData[customField].(string))
secondUpdate := history[0]
assert.Equal(t, updatedText1, secondUpdate.Text)
assert.Equal(t, user1.ID, secondUpdate.MessageUpdatedByID)
assert.Equal(t, updatedCustomFieldValue, secondUpdate.ExtraData[customField].(string))
})
t.Run("test sorting", func(t *testing.T) {
sortedHistoryQueryRequest := QueryMessageHistoryRequest{
Filter: map[string]interface{}{
"message_id": message.ID,
},
Sort: []*SortOption{
{
Field: "message_updated_at",
Direction: 1,
},
},
}
sortedHistoryResponse, err := client.QueryMessageHistory(ctx, sortedHistoryQueryRequest)
assert.NoError(t, err)
assert.NotNil(t, sortedHistoryResponse)
sortedHistory := sortedHistoryResponse.MessageHistory
assert.Equal(t, 2, len(sortedHistory))
firstUpdate := sortedHistory[0]
assert.Equal(t, initialText, firstUpdate.Text)
assert.Equal(t, user1.ID, firstUpdate.MessageUpdatedByID)
secondUpdate := sortedHistory[1]
assert.Equal(t, updatedText1, secondUpdate.Text)
assert.Equal(t, user1.ID, secondUpdate.MessageUpdatedByID)
})
}