-
Notifications
You must be signed in to change notification settings - Fork 31
/
message_test.go
148 lines (114 loc) · 4.2 KB
/
message_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
package stream_chat
import (
"context"
"testing"
"time"
"github.com/stretchr/testify/require"
)
func TestClient_TranslateMessage(t *testing.T) {
c := initClient(t)
u := randomUser(t, c)
ch := initChannel(t, c, u.ID)
ctx := context.Background()
msg := &Message{Text: "test message"}
messageResp, err := ch.SendMessage(ctx, msg, u.ID)
require.NoError(t, err)
translated, err := c.TranslateMessage(ctx, messageResp.Message.ID, "es")
require.NoError(t, err)
require.Equal(t, "mensaje de prueba", translated.Message.I18n["es_text"])
}
func TestClient_SendMessage_Pending(t *testing.T) {
c := initClient(t)
user := randomUser(t, c)
ctx := context.Background()
ch := initChannel(t, c, user.ID)
resp1, err := c.CreateChannel(ctx, ch.Type, ch.ID, user.ID, nil)
require.NoError(t, err)
msg := &Message{Text: "test pending message"}
metadata := map[string]string{"my": "metadata"}
messageResp, err := resp1.Channel.SendMessage(ctx, msg, user.ID, MessagePending, MessagePendingMessageMetadata(metadata))
require.NoError(t, err)
require.Equal(t, metadata, messageResp.PendingMessageMetadata)
gotMsg, err := c.GetMessage(ctx, messageResp.Message.ID)
require.NoError(t, err)
require.Equal(t, metadata, gotMsg.PendingMessageMetadata)
_, err = c.CommitMessage(ctx, messageResp.Message.ID)
require.NoError(t, err)
}
func TestClient_SendMessage_SkipEnrichURL(t *testing.T) {
c := initClient(t)
user := randomUser(t, c)
ctx := context.Background()
ch := initChannel(t, c, user.ID)
resp1, err := c.CreateChannel(ctx, ch.Type, ch.ID, user.ID, nil)
require.NoError(t, err)
msg := &Message{Text: "test message with link to https://getstream.io"}
messageResp, err := resp1.Channel.SendMessage(ctx, msg, user.ID, MessageSkipEnrichURL)
require.NoError(t, err)
require.Empty(t, messageResp.Message.Attachments)
time.Sleep(3 * time.Second)
gotMsg, err := c.GetMessage(ctx, messageResp.Message.ID)
require.NoError(t, err)
require.Empty(t, gotMsg.Message.Attachments)
}
func TestClient_PinMessage(t *testing.T) {
c := initClient(t)
userA := randomUser(t, c)
userB := randomUser(t, c)
ctx := context.Background()
ch := initChannel(t, c, userA.ID, userB.ID)
resp1, err := c.CreateChannel(ctx, ch.Type, ch.ID, userA.ID, nil)
require.NoError(t, err)
msg := &Message{Text: "test message"}
messageResp, err := resp1.Channel.SendMessage(ctx, msg, userB.ID)
require.NoError(t, err)
msgWithOptions := &Message{Text: "test message"}
quotedMsgResp, err := resp1.Channel.SendMessage(ctx, msgWithOptions, userB.ID, func(msg *messageRequest) {
msg.Message.QuotedMessageID = messageResp.Message.ID
})
require.NoError(t, err)
require.Equal(t, messageResp.Message.ID, quotedMsgResp.Message.QuotedMessageID)
messageResp, err = c.PinMessage(ctx, messageResp.Message.ID, userA.ID, nil)
require.NoError(t, err)
msg = messageResp.Message
require.NotZero(t, msg.PinnedAt)
require.NotZero(t, msg.PinnedBy)
require.Equal(t, userA.ID, msg.PinnedBy.ID)
messageResp, err = c.UnPinMessage(ctx, msg.ID, userA.ID)
require.NoError(t, err)
msg = messageResp.Message
require.Zero(t, msg.PinnedAt)
require.Zero(t, msg.PinnedBy)
expireAt := time.Now().Add(3 * time.Second)
messageResp, err = c.PinMessage(ctx, msg.ID, userA.ID, &expireAt)
require.NoError(t, err)
msg = messageResp.Message
require.NotZero(t, msg.PinnedAt)
require.NotZero(t, msg.PinnedBy)
require.Equal(t, userA.ID, msg.PinnedBy.ID)
time.Sleep(3 * time.Second)
messageResp, err = c.GetMessage(ctx, msg.ID)
require.NoError(t, err)
msg = messageResp.Message
require.Zero(t, msg.PinnedAt)
require.Zero(t, msg.PinnedBy)
}
func TestClient_SendMessage_KeepChannelHidden(t *testing.T) {
c := initClient(t)
user := randomUser(t, c)
ctx := context.Background()
ch := initChannel(t, c, user.ID)
resp, err := c.CreateChannel(ctx, ch.Type, ch.ID, user.ID, nil)
require.NoError(t, err)
_, err = resp.Channel.Hide(ctx, user.ID)
require.NoError(t, err)
msg := &Message{Text: "test message"}
_, err = resp.Channel.SendMessage(ctx, msg, user.ID, KeepChannelHidden)
require.NoError(t, err)
result, err := c.QueryChannels(ctx, &QueryOption{
Filter: map[string]interface{}{"cid": resp.Channel.CID},
UserID: user.ID,
})
require.NoError(t, err)
require.Empty(t, result.Channels)
}