-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathConversation.cs
More file actions
270 lines (250 loc) · 9.3 KB
/
Conversation.cs
File metadata and controls
270 lines (250 loc) · 9.3 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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Linq;
using LeanCloud;
using LeanCloud.Realtime;
using static NUnit.Framework.TestContext;
namespace Realtime.Test {
public class Conversation : BaseTest {
private LCIMClient c1;
private LCIMClient c2;
private LCIMClient lean;
private LCIMConversation conversation;
[SetUp]
public override async Task SetUp() {
await base.SetUp();
c1 = new LCIMClient(Guid.NewGuid().ToString());
await c1.Open();
c2 = new LCIMClient(Guid.NewGuid().ToString());
await c2.Open();
lean = new LCIMClient("lean");
await lean.Open();
conversation = await c1.CreateConversation(new string[] { "lean", "cloud" });
}
[TearDown]
public override async Task TearDown() {
await c1.Close();
await c2.Close();
await lean.Close();
await base.TearDown();
}
[Test]
[Order(0)]
public async Task AddMember() {
TaskCompletionSource<object> tcs = new TaskCompletionSource<object>();
c2.OnInvited = (conv, initBy) => {
WriteLine($"{c2.Id} is invited by {initBy}");
tcs.SetResult(null);
};
await conversation.AddMembers(new string[] { c2.Id });
Assert.AreEqual(conversation.MemberIds.Count, 4);
await tcs.Task;
}
[Test]
[Order(1)]
public async Task MuteMembers() {
TaskCompletionSource<object> tcs = new TaskCompletionSource<object>();
bool f1 = false, f2 = false;
c1.OnMembersMuted = (conv, mutedMembers, initBy) => {
Assert.True(mutedMembers.Contains(lean.Id));
Assert.True(conversation.MutedMemberIds.Contains(lean.Id));
f1 = true;
if (f1 && f2) {
tcs.TrySetResult(null);
}
};
lean.OnMuted = (conv, initBy) => {
WriteLine($"{lean.Id} is muted by {initBy}");
f2 = true;
if (f1 && f2) {
tcs.TrySetResult(null);
}
};
try {
await conversation.MuteMembers(new string[] { "lean" });
Assert.True(conversation.MutedMemberIds.Contains("lean"));
} catch (LCException e) {
if (e.Code == 4325) {
tcs.TrySetResult(null);
} else {
throw e;
}
}
await tcs.Task;
}
[Test]
[Order(2)]
public async Task UnmuteMembers() {
TaskCompletionSource<object> tcs = new TaskCompletionSource<object>();
bool f1 = false, f2 = false;
c1.OnMembersUnmuted = (conv, unmutedMembers, initBy) => {
Assert.True(unmutedMembers.Contains(lean.Id));
Assert.False(conversation.MutedMemberIds.Contains(lean.Id));
f1 = true;
if (f1 && f2) {
tcs.SetResult(null);
}
};
lean.OnUnmuted = (conv, initBy) => {
WriteLine($"{lean.Id} is unmuted by {initBy}");
f2 = true;
if (f1 && f2) {
tcs.SetResult(null);
}
};
try {
await conversation.UnmuteMembers(new string[] { "lean" });
Assert.False(conversation.MutedMemberIds.Contains("lean"));
} catch (LCException e) {
if (e.Code == 4325) {
tcs.TrySetResult(null);
} else {
throw e;
}
}
await tcs.Task;
}
[Test]
[Order(3)]
public async Task BlockMembers() {
TaskCompletionSource<object> tcs = new TaskCompletionSource<object>();
bool f1 = false, f2 = false;
c1.OnMembersBlocked = (conv, blockedMembers, initBy) => {
Assert.True(blockedMembers.Contains(lean.Id));
f1 = true;
if (f1 && f2) {
tcs.SetResult(null);
}
};
lean.OnBlocked = (conv, initBy) => {
WriteLine($"{lean.Id} is blocked by {initBy}");
f2 = true;
if (f1 && f2) {
tcs.TrySetResult(null);
}
};
try {
await conversation.BlockMembers(new string[] { "lean" });
LCIMPageResult result = await conversation.QueryBlockedMembers();
Assert.True(result.Results.Contains("lean"));
} catch (LCException e) {
if (e.Code == 4544) {
tcs.TrySetResult(null);
} else {
throw e;
}
}
await tcs.Task;
}
[Test]
[Order(4)]
public async Task UnblockMembers() {
TaskCompletionSource<object> tcs = new TaskCompletionSource<object>();
bool f1 = false, f2 = false;
c1.OnMembersUnblocked = (conv, blockedMembers, initBy) => {
Assert.True(blockedMembers.Contains(lean.Id));
f1 = true;
if (f1 && f2) {
tcs.SetResult(null);
}
};
lean.OnUnblocked = (conv, initBy) => {
WriteLine($"{lean.Id} is unblocked by {initBy}");
f2 = true;
if (f1 && f2) {
tcs.SetResult(null);
}
};
try {
await conversation.UnblockMembers(new string[] { "lean" });
LCIMPageResult result = await conversation.QueryBlockedMembers();
Assert.False(result.Results.Contains("lean"));
} catch (LCException e) {
if (e.Code == 4544) {
tcs.TrySetResult(null);
} else {
throw e;
}
}
await tcs.Task;
}
[Test]
[Order(5)]
public async Task UpdateRole() {
TaskCompletionSource<object> tcs = new TaskCompletionSource<object>();
c1.OnMemberInfoUpdated = (conv, member, role, initBy) => {
WriteLine($"{member} is {role} by {initBy}");
tcs.TrySetResult(null);
};
try {
await conversation.UpdateMemberRole("cloud", LCIMConversationMemberInfo.Manager);
LCIMConversationMemberInfo memberInfo = await conversation.GetMemberInfo("cloud");
Assert.True(memberInfo.IsManager);
Assert.AreEqual(memberInfo.ConversationId, conversation.Id);
Assert.AreEqual(memberInfo.MemberId, "cloud");
Assert.False(memberInfo.IsOwner);
} catch (LCException e) {
if (e.Code == 4325) {
tcs.TrySetResult(null);
} else {
throw e;
}
}
await tcs.Task;
}
[Test]
[Order(6)]
public async Task RemoveMember() {
TaskCompletionSource<object> tcs = new TaskCompletionSource<object>();
c2.OnKicked = (conv, initBy) => {
WriteLine($"{c2.Id} is kicked by {initBy}");
tcs.SetResult(null);
};
await conversation.RemoveMembers(new string[] { c2.Id });
Assert.AreEqual(conversation.MemberIds.Count, 3);
await tcs.Task;
}
[Test]
[Order(7)]
[Timeout(60000)]
public async Task UpdateInfo() {
TaskCompletionSource<object> tcs = new TaskCompletionSource<object>();
lean.OnConversationInfoUpdated = (conv, attrs, initBy) => {
WriteLine(attrs);
Assert.AreEqual(conv.Name, "leancloud");
Assert.AreEqual(conv["k1"], "v1");
Assert.AreEqual(conv["k2"], "v2");
Assert.AreEqual(attrs["k1"], "v1");
Assert.AreEqual(attrs["k2"], "v2");
tcs.TrySetResult(null);
};
await Task.Delay(5000);
await conversation.UpdateInfo(new Dictionary<string, object> {
{ "name", "leancloud" },
{ "k1", "v1" },
{ "k2", "v2" }
});
Assert.AreEqual(conversation.Name, "leancloud");
Assert.AreEqual(conversation["k1"], "v1");
Assert.AreEqual(conversation["k2"], "v2");
//await tcs.Task;
}
[Test]
[Timeout(20000)]
[Order(8)]
public async Task MuteConversation() {
await conversation.Mute();
await conversation.Unmute();
}
[Test]
[Order(9)]
public async Task QueryCustomAttrsConversations() {
LCIMConversationQuery query = lean.GetQuery();
var convs = await query.Find();
int count = convs.Where(conv => conv["k1"] != null).Count();
Assert.Greater(count, 0);
}
}
}