-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathChatChannelIntegrationTest.java
More file actions
1382 lines (1178 loc) · 50.5 KB
/
Copy pathChatChannelIntegrationTest.java
File metadata and controls
1382 lines (1178 loc) · 50.5 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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package io.getstream;
import static org.junit.jupiter.api.Assertions.*;
import static org.junit.jupiter.api.Assumptions.abort;
import io.getstream.exceptions.StreamTransportException;
import io.getstream.models.*;
import java.util.*;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.MethodOrderer;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.junit.jupiter.api.TestMethodOrder;
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
class ChatChannelIntegrationTest extends ChatTestBase {
private final List<String> createdUserIds = new ArrayList<>();
private final List<String> createdChannelIds = new ArrayList<>();
@AfterAll
void cleanup() {
for (String channelId : createdChannelIds) {
try {
chat.deleteChannel(
"messaging", channelId, DeleteChannelRequest.builder().HardDelete(true).build())
.execute();
} catch (Exception ignored) {
}
}
if (!createdUserIds.isEmpty()) {
deleteUsersWithRetry(createdUserIds);
}
}
@Test
@Order(1)
void testCreateChannelWithID() throws Exception {
List<String> userIds = createTestUsers(1);
createdUserIds.addAll(userIds);
String creatorId = userIds.get(0);
String channelId = createTestChannel(creatorId);
createdChannelIds.add(channelId);
// Verify channel exists by querying
var resp =
chat.queryChannels(
QueryChannelsRequest.builder().filterConditions(Map.of("id", channelId)).build())
.execute();
assertNotNull(resp.getData());
assertFalse(resp.getData().getChannels().isEmpty(), "QueryChannels should return the channel");
assertEquals(channelId, resp.getData().getChannels().get(0).getChannel().getId());
assertEquals("messaging", resp.getData().getChannels().get(0).getChannel().getType());
}
@Test
@Order(2)
void testCreateChannelWithMembers() throws Exception {
List<String> userIds = createTestUsers(3);
createdUserIds.addAll(userIds);
String channelId = createTestChannelWithMembers(userIds.get(0), userIds);
createdChannelIds.add(channelId);
// Query the channel back and verify member count >= 3
var resp =
chat.queryChannels(
QueryChannelsRequest.builder().filterConditions(Map.of("id", channelId)).build())
.execute();
assertNotNull(resp.getData());
assertFalse(resp.getData().getChannels().isEmpty(), "QueryChannels should return the channel");
var channelState = resp.getData().getChannels().get(0);
assertNotNull(channelState.getMembers(), "Channel members should not be null");
assertTrue(
channelState.getMembers().size() >= 3,
"Channel should have at least 3 members, got: " + channelState.getMembers().size());
}
@Test
@Order(3)
void testCreateDistinctChannel() throws Exception {
List<String> userIds = createTestUsers(2);
createdUserIds.addAll(userIds);
String creatorId = userIds.get(0);
String memberId = userIds.get(1);
List<ChannelMemberRequest> members =
List.of(
ChannelMemberRequest.builder().userID(creatorId).build(),
ChannelMemberRequest.builder().userID(memberId).build());
// Create distinct channel (no explicit channel ID)
var resp1 =
chat.getOrCreateDistinctChannel(
"messaging",
GetOrCreateDistinctChannelRequest.builder()
.data(ChannelInput.builder().createdByID(creatorId).members(members).build())
.build())
.execute();
assertNotNull(resp1.getData());
assertNotNull(resp1.getData().getChannel(), "Channel should not be null");
String cid1 = resp1.getData().getChannel().getCid();
String channelId1 = resp1.getData().getChannel().getId();
assertNotNull(cid1, "CID should not be null");
createdChannelIds.add(channelId1);
// Call again with same members - should return same channel
var resp2 =
chat.getOrCreateDistinctChannel(
"messaging",
GetOrCreateDistinctChannelRequest.builder()
.data(ChannelInput.builder().createdByID(creatorId).members(members).build())
.build())
.execute();
assertNotNull(resp2.getData());
assertNotNull(resp2.getData().getChannel(), "Channel should not be null on second call");
String cid2 = resp2.getData().getChannel().getCid();
assertEquals(cid1, cid2, "Same members should return the same channel CID");
}
@Test
@Order(4)
void testQueryChannels() throws Exception {
List<String> userIds = createTestUsers(1);
createdUserIds.addAll(userIds);
String creatorId = userIds.get(0);
String channelId = createTestChannel(creatorId);
createdChannelIds.add(channelId);
// Query by both type and id
var resp =
chat.queryChannels(
QueryChannelsRequest.builder()
.filterConditions(Map.of("type", "messaging", "id", channelId))
.build())
.execute();
assertNotNull(resp.getData());
assertFalse(resp.getData().getChannels().isEmpty(), "QueryChannels should return the channel");
assertEquals(channelId, resp.getData().getChannels().get(0).getChannel().getId());
}
@Test
@Order(6)
void testPartialUpdateChannel() throws Exception {
List<String> userIds = createTestUsers(1);
createdUserIds.addAll(userIds);
String creatorId = userIds.get(0);
String channelId = createTestChannel(creatorId);
createdChannelIds.add(channelId);
// Set custom fields: color=red, description=A test channel
var setResp =
chat.updateChannelPartial(
"messaging",
channelId,
UpdateChannelPartialRequest.builder()
.set(Map.of("color", "red", "description", "A test channel"))
.build())
.execute();
assertNotNull(setResp.getData(), "PartialUpdate (set) response should not be null");
assertNotNull(setResp.getData().getChannel(), "Channel in response should not be null");
var custom = setResp.getData().getChannel().getCustom();
assertNotNull(custom, "Custom data should not be null after set");
assertEquals("red", custom.get("color"), "Custom field 'color' should be 'red'");
// Unset 'color' and verify it's removed
var unsetResp =
chat.updateChannelPartial(
"messaging",
channelId,
UpdateChannelPartialRequest.builder().unset(List.of("color")).build())
.execute();
assertNotNull(unsetResp.getData(), "PartialUpdate (unset) response should not be null");
assertNotNull(unsetResp.getData().getChannel(), "Channel in response should not be null");
var customAfterUnset = unsetResp.getData().getChannel().getCustom();
assertTrue(
customAfterUnset == null || !customAfterUnset.containsKey("color"),
"Custom field 'color' should be removed after unset");
}
@Test
@Order(9)
void testAddRemoveMembers() throws Exception {
List<String> userIds = createTestUsers(4);
createdUserIds.addAll(userIds);
String creatorId = userIds.get(0);
String memberId1 = userIds.get(1);
String memberId2 = userIds.get(2);
String memberId3 = userIds.get(3);
// Create channel with creator + member1
String channelId = createTestChannelWithMembers(creatorId, List.of(creatorId, memberId1));
createdChannelIds.add(channelId);
// Add member2 and member3
chat.updateChannel(
"messaging",
channelId,
UpdateChannelRequest.builder()
.addMembers(
List.of(
ChannelMemberRequest.builder().userID(memberId2).build(),
ChannelMemberRequest.builder().userID(memberId3).build()))
.build())
.execute();
// Verify members added (should have at least 4 members)
var resp1 =
chat.getOrCreateChannel("messaging", channelId, GetOrCreateChannelRequest.builder().build())
.execute();
assertNotNull(resp1.getData());
assertTrue(
resp1.getData().getMembers().size() >= 4,
"Channel should have at least 4 members after add, got: "
+ resp1.getData().getMembers().size());
// Remove member3
chat.updateChannel(
"messaging",
channelId,
UpdateChannelRequest.builder().removeMembers(List.of(memberId3)).build())
.execute();
// Verify member3 is removed
var resp2 =
chat.getOrCreateChannel("messaging", channelId, GetOrCreateChannelRequest.builder().build())
.execute();
assertNotNull(resp2.getData());
boolean memberFound =
resp2.getData().getMembers().stream().anyMatch(m -> memberId3.equals(m.getUserID()));
assertFalse(memberFound, "member3 should have been removed from the channel");
}
@Test
@Order(11)
void testInviteAcceptReject() throws Exception {
List<String> userIds = createTestUsers(3);
createdUserIds.addAll(userIds);
String creatorId = userIds.get(0);
String invitee1Id = userIds.get(1);
String invitee2Id = userIds.get(2);
// Create channel with creator as member and 2 users as invitees
String channelId = "test-inv-" + UUID.randomUUID().toString().substring(0, 12);
createdChannelIds.add(channelId);
chat.getOrCreateChannel(
"messaging",
channelId,
GetOrCreateChannelRequest.builder()
.data(
ChannelInput.builder()
.createdByID(creatorId)
.members(List.of(ChannelMemberRequest.builder().userID(creatorId).build()))
.invites(
List.of(
ChannelMemberRequest.builder().userID(invitee1Id).build(),
ChannelMemberRequest.builder().userID(invitee2Id).build()))
.build())
.build())
.execute();
// Accept invite for invitee1
var acceptResp =
chat.updateChannel(
"messaging",
channelId,
UpdateChannelRequest.builder().acceptInvite(true).userID(invitee1Id).build())
.execute();
assertNotNull(acceptResp.getData(), "AcceptInvite response should not be null");
// Reject invite for invitee2
var rejectResp =
chat.updateChannel(
"messaging",
channelId,
UpdateChannelRequest.builder().rejectInvite(true).userID(invitee2Id).build())
.execute();
assertNotNull(rejectResp.getData(), "RejectInvite response should not be null");
}
@Test
@Order(13)
void testTruncateChannel() throws Exception {
List<String> userIds = createTestUsers(1);
createdUserIds.addAll(userIds);
String creatorId = userIds.get(0);
String channelId = createTestChannel(creatorId);
createdChannelIds.add(channelId);
// Send 3 messages
sendTestMessage("messaging", channelId, creatorId, "msg1");
sendTestMessage("messaging", channelId, creatorId, "msg2");
sendTestMessage("messaging", channelId, creatorId, "msg3");
// Truncate the channel
var truncResp =
chat.truncateChannel("messaging", channelId, TruncateChannelRequest.builder().build())
.execute();
assertNotNull(truncResp.getData(), "TruncateChannel response should not be null");
assertNotNull(truncResp.getData().getDuration(), "Duration should not be null");
// Verify channel messages are now empty
var resp =
chat.getOrCreateChannel("messaging", channelId, GetOrCreateChannelRequest.builder().build())
.execute();
assertNotNull(resp.getData(), "GetOrCreateChannel response should not be null");
List<MessageResponse> messages = resp.getData().getMessages();
assertTrue(
messages == null || messages.isEmpty(),
"Channel messages should be empty after truncation, got: "
+ (messages != null ? messages.size() : 0));
}
@Test
@Order(12)
void testHideShowChannel() throws Exception {
List<String> userIds = createTestUsers(2);
createdUserIds.addAll(userIds);
String creatorId = userIds.get(0);
String memberId = userIds.get(1);
// Create channel with both users as members
String channelId = createTestChannelWithMembers(creatorId, userIds);
createdChannelIds.add(channelId);
// Hide the channel for memberId
var hideResp =
chat.hideChannel(
"messaging", channelId, HideChannelRequest.builder().userID(memberId).build())
.execute();
assertNotNull(hideResp.getData(), "HideChannel response should not be null");
assertNotNull(hideResp.getData().getDuration(), "Duration should not be null");
// Show the channel for memberId
var showResp =
chat.showChannel(
"messaging", channelId, ShowChannelRequest.builder().userID(memberId).build())
.execute();
assertNotNull(showResp.getData(), "ShowChannel response should not be null");
assertNotNull(showResp.getData().getDuration(), "Duration should not be null");
}
@Test
@Order(10)
void testQueryMembers() throws Exception {
List<String> userIds = createTestUsers(3);
createdUserIds.addAll(userIds);
String creatorId = userIds.get(0);
// Create channel with all 3 users as members
String channelId = createTestChannelWithMembers(creatorId, userIds);
createdChannelIds.add(channelId);
// Query members of the channel
var resp =
chat.queryMembers(
QueryMembersRequest.builder()
.Payload(
QueryMembersPayload.builder()
.type("messaging")
.id(channelId)
.filterConditions(Map.of())
.build())
.build())
.execute();
assertNotNull(resp.getData(), "QueryMembers response should not be null");
assertNotNull(resp.getData().getMembers(), "Members list should not be null");
assertTrue(
resp.getData().getMembers().size() >= 3,
"Channel should have at least 3 members, got: " + resp.getData().getMembers().size());
}
@Test
@Order(8)
void testHardDeleteChannels() throws Exception {
List<String> userIds = createTestUsers(1);
createdUserIds.addAll(userIds);
String creatorId = userIds.get(0);
// Create 2 channels but do NOT track them in createdChannelIds (we delete them explicitly)
String channelId1 = createTestChannel(creatorId);
String channelId2 = createTestChannel(creatorId);
List<String> cids = List.of("messaging:" + channelId1, "messaging:" + channelId2);
// Hard delete both channels via batch endpoint with retry for rate limiting
String taskId = null;
for (int i = 0; i < 10; i++) {
try {
var resp =
chat.deleteChannels(DeleteChannelsRequest.builder().cids(cids).hardDelete(true).build())
.execute();
assertNotNull(resp.getData(), "DeleteChannels response should not be null");
taskId = resp.getData().getTaskID();
break;
} catch (Exception e) {
if (!e.getMessage().contains("Too many requests")) throw e;
Thread.sleep((i + 1) * 3000L);
}
}
assertNotNull(taskId, "TaskID should not be null for hard delete");
assertFalse(taskId.isEmpty(), "TaskID should not be empty");
// Poll the async hard-delete task. A timeout here reflects shared-backend
// async-queue latency, not an SDK defect (the request succeeded and returned
// a task), so skip rather than fail; a genuine task failure throws
// StreamTaskException and still fails the test.
try {
client.waitForTask(taskId);
} catch (StreamTransportException e) {
abort(
"hard-delete task did not reach a terminal state within the poll window: "
+ e.getMessage());
}
}
@Test
@Order(15)
void testMarkReadUnread() throws Exception {
List<String> userIds = createTestUsers(2);
createdUserIds.addAll(userIds);
String creatorId = userIds.get(0);
String memberId = userIds.get(1);
String channelId = createTestChannelWithMembers(creatorId, userIds);
createdChannelIds.add(channelId);
// Send a message so there's something to mark read/unread
String messageId = sendTestMessage("messaging", channelId, creatorId, "hello mark read test");
// Mark the channel as read for memberId
var markReadResp =
chat.markRead("messaging", channelId, MarkReadRequest.builder().userID(memberId).build())
.execute();
assertNotNull(markReadResp.getData(), "MarkRead response should not be null");
assertNotNull(
markReadResp.getData().getDuration(), "Duration should not be null after markRead");
// Mark the channel as unread from that message for memberId
var markUnreadResp =
chat.markUnread(
"messaging",
channelId,
MarkUnreadRequest.builder().userID(memberId).messageID(messageId).build())
.execute();
assertNotNull(markUnreadResp.getData(), "MarkUnread response should not be null");
assertNotNull(
markUnreadResp.getData().getDuration(), "Duration should not be null after markUnread");
}
@Test
@Order(16)
void testMuteUnmuteChannel() throws Exception {
List<String> userIds = createTestUsers(2);
createdUserIds.addAll(userIds);
String creatorId = userIds.get(0);
String memberId = userIds.get(1);
String channelId = createTestChannelWithMembers(creatorId, userIds);
createdChannelIds.add(channelId);
String cid = "messaging:" + channelId;
// Mute the channel for memberId
var muteResp =
chat.muteChannel(
MuteChannelRequest.builder().channelCids(List.of(cid)).userID(memberId).build())
.execute();
assertNotNull(muteResp.getData(), "MuteChannel response should not be null");
assertNotNull(muteResp.getData().getDuration(), "Duration should not be null after mute");
// Verify muted via QueryChannels with muted=true filter
var mutedResp =
chat.queryChannels(
QueryChannelsRequest.builder()
.filterConditions(Map.of("muted", true, "cid", cid))
.userID(memberId)
.build())
.execute();
assertNotNull(mutedResp.getData(), "QueryChannels (muted=true) response should not be null");
assertFalse(
mutedResp.getData().getChannels().isEmpty(),
"Channel should appear in muted=true query after muting");
// Unmute the channel for memberId
var unmuteResp =
chat.unmuteChannel(
UnmuteChannelRequest.builder().channelCids(List.of(cid)).userID(memberId).build())
.execute();
assertNotNull(unmuteResp.getData(), "UnmuteChannel response should not be null");
// Verify unmuted via QueryChannels with muted=false filter
var unmutedResp =
chat.queryChannels(
QueryChannelsRequest.builder()
.filterConditions(Map.of("muted", false, "cid", cid))
.userID(memberId)
.build())
.execute();
assertNotNull(unmutedResp.getData(), "QueryChannels (muted=false) response should not be null");
assertFalse(
unmutedResp.getData().getChannels().isEmpty(),
"Channel should appear in muted=false query after unmuting");
}
@Test
@Order(17)
void testMemberPartialUpdate() throws Exception {
List<String> userIds = createTestUsers(2);
createdUserIds.addAll(userIds);
String creatorId = userIds.get(0);
String memberId = userIds.get(1);
String channelId = createTestChannelWithMembers(creatorId, userIds);
createdChannelIds.add(channelId);
// Set custom fields on the member
var setResp =
chat.updateMemberPartial(
"messaging",
channelId,
UpdateMemberPartialRequest.builder()
.UserID(memberId)
.set(Map.of("role_label", "vip", "score", 100))
.build())
.execute();
assertNotNull(setResp.getData(), "UpdateMemberPartial (set) response should not be null");
assertNotNull(setResp.getData().getChannelMember(), "ChannelMember should not be null");
var custom = setResp.getData().getChannelMember().getCustom();
assertNotNull(custom, "Custom data should not be null after set");
assertEquals("vip", custom.get("role_label"), "Custom field 'role_label' should be 'vip'");
assertNotNull(custom.get("score"), "Custom field 'score' should be present");
// Unset 'score' and verify it's removed
var unsetResp =
chat.updateMemberPartial(
"messaging",
channelId,
UpdateMemberPartialRequest.builder()
.UserID(memberId)
.unset(List.of("score"))
.build())
.execute();
assertNotNull(unsetResp.getData(), "UpdateMemberPartial (unset) response should not be null");
assertNotNull(
unsetResp.getData().getChannelMember(), "ChannelMember should not be null after unset");
var customAfterUnset = unsetResp.getData().getChannelMember().getCustom();
assertTrue(
customAfterUnset == null || !customAfterUnset.containsKey("score"),
"Custom field 'score' should be removed after unset");
}
@Test
@Order(18)
void testAssignRoles() throws Exception {
List<String> userIds = createTestUsers(2);
createdUserIds.addAll(userIds);
String creatorId = userIds.get(0);
String memberId = userIds.get(1);
String channelId = createTestChannelWithMembers(creatorId, userIds);
createdChannelIds.add(channelId);
// Assign channel_moderator role to memberId
chat.updateChannel(
"messaging",
channelId,
UpdateChannelRequest.builder()
.assignRoles(
List.of(
ChannelMemberRequest.builder()
.userID(memberId)
.channelRole("channel_moderator")
.build()))
.build())
.execute();
// Verify via QueryMembers that the role is set
var qResp =
chat.queryMembers(
QueryMembersRequest.builder()
.Payload(
QueryMembersPayload.builder()
.type("messaging")
.id(channelId)
.filterConditions(Map.of("user_id", memberId))
.build())
.build())
.execute();
assertNotNull(qResp.getData(), "QueryMembers response should not be null");
assertFalse(qResp.getData().getMembers().isEmpty(), "Members list should not be empty");
assertEquals(
"channel_moderator",
qResp.getData().getMembers().get(0).getChannelRole(),
"Member should have channel_moderator role");
}
@Test
@Order(20)
void testMarkUnreadWithThread() throws Exception {
List<String> userIds = createTestUsers(2);
createdUserIds.addAll(userIds);
String creatorId = userIds.get(0);
String memberId = userIds.get(1);
String channelId = createTestChannelWithMembers(creatorId, userIds);
createdChannelIds.add(channelId);
// Send parent message
String parentMsgId = sendTestMessage("messaging", channelId, creatorId, "parent message");
// Send thread reply to create a thread
chat.sendMessage(
"messaging",
channelId,
SendMessageRequest.builder()
.message(
MessageRequest.builder()
.text("thread reply")
.userID(memberId)
.parentID(parentMsgId)
.build())
.build())
.execute();
// Mark channel as read first
chat.markRead("messaging", channelId, MarkReadRequest.builder().userID(memberId).build())
.execute();
// Mark unread from thread (using the parent message ID as the thread ID)
var markUnreadResp =
chat.markUnread(
"messaging",
channelId,
MarkUnreadRequest.builder().userID(memberId).threadID(parentMsgId).build())
.execute();
assertNotNull(markUnreadResp.getData(), "MarkUnread (thread) response should not be null");
assertNotNull(
markUnreadResp.getData().getDuration(),
"Duration should not be null after markUnread with thread");
}
@Test
@Order(19)
void testAddDemoteModerators() throws Exception {
List<String> userIds = createTestUsers(2);
createdUserIds.addAll(userIds);
String creatorId = userIds.get(0);
String memberId = userIds.get(1);
String channelId = createTestChannelWithMembers(creatorId, userIds);
createdChannelIds.add(channelId);
// Add memberId as moderator
chat.updateChannel(
"messaging",
channelId,
UpdateChannelRequest.builder().addModerators(List.of(memberId)).build())
.execute();
// Verify via QueryMembers that the role is channel_moderator
var afterPromote =
chat.queryMembers(
QueryMembersRequest.builder()
.Payload(
QueryMembersPayload.builder()
.type("messaging")
.id(channelId)
.filterConditions(Map.of("user_id", memberId))
.build())
.build())
.execute();
assertNotNull(afterPromote.getData(), "QueryMembers response should not be null");
assertFalse(afterPromote.getData().getMembers().isEmpty(), "Members list should not be empty");
assertEquals(
"channel_moderator",
afterPromote.getData().getMembers().get(0).getChannelRole(),
"Member should have channel_moderator role after promotion");
// Demote memberId back to regular member
chat.updateChannel(
"messaging",
channelId,
UpdateChannelRequest.builder().demoteModerators(List.of(memberId)).build())
.execute();
// Verify role is back to channel_member
var afterDemote =
chat.queryMembers(
QueryMembersRequest.builder()
.Payload(
QueryMembersPayload.builder()
.type("messaging")
.id(channelId)
.filterConditions(Map.of("user_id", memberId))
.build())
.build())
.execute();
assertNotNull(afterDemote.getData(), "QueryMembers response should not be null after demote");
assertFalse(
afterDemote.getData().getMembers().isEmpty(),
"Members list should not be empty after demote");
assertEquals(
"channel_member",
afterDemote.getData().getMembers().get(0).getChannelRole(),
"Member should be back to channel_member role after demotion");
}
@Test
@Order(21)
void testTruncateWithOptions() throws Exception {
List<String> userIds = createTestUsers(2);
createdUserIds.addAll(userIds);
String creatorId = userIds.get(0);
String memberId = userIds.get(1);
String channelId = createTestChannelWithMembers(creatorId, userIds);
createdChannelIds.add(channelId);
// Send 2 messages
sendTestMessage("messaging", channelId, creatorId, "Truncate msg 1");
sendTestMessage("messaging", channelId, creatorId, "Truncate msg 2");
// Truncate with message, skip_push=true, hard_delete=true
var truncResp =
chat.truncateChannel(
"messaging",
channelId,
TruncateChannelRequest.builder()
.message(
MessageRequest.builder()
.text("Channel was truncated")
.userID(creatorId)
.build())
.skipPush(true)
.hardDelete(true)
.build())
.execute();
assertNotNull(truncResp.getData(), "TruncateWithOptions response should not be null");
assertNotNull(truncResp.getData().getDuration(), "Duration should not be null");
}
@Test
@Order(14)
void testFreezeUnfreezeChannel() throws Exception {
List<String> userIds = createTestUsers(1);
createdUserIds.addAll(userIds);
String creatorId = userIds.get(0);
String channelId = createTestChannel(creatorId);
createdChannelIds.add(channelId);
// Freeze the channel
var freezeResp =
chat.updateChannelPartial(
"messaging",
channelId,
UpdateChannelPartialRequest.builder().set(Map.of("frozen", true)).build())
.execute();
assertNotNull(freezeResp.getData(), "Freeze response should not be null");
assertNotNull(
freezeResp.getData().getChannel(), "Channel in freeze response should not be null");
Boolean frozenAfterFreeze = freezeResp.getData().getChannel().getFrozen();
assertNotNull(frozenAfterFreeze, "Frozen field should not be null after freeze");
assertTrue(frozenAfterFreeze, "Channel should be frozen after setting frozen=true");
// Unfreeze the channel
var unfreezeResp =
chat.updateChannelPartial(
"messaging",
channelId,
UpdateChannelPartialRequest.builder().set(Map.of("frozen", false)).build())
.execute();
assertNotNull(unfreezeResp.getData(), "Unfreeze response should not be null");
assertNotNull(
unfreezeResp.getData().getChannel(), "Channel in unfreeze response should not be null");
Boolean frozenAfterUnfreeze = unfreezeResp.getData().getChannel().getFrozen();
assertNotNull(frozenAfterUnfreeze, "Frozen field should not be null after unfreeze");
assertFalse(frozenAfterUnfreeze, "Channel should be unfrozen after setting frozen=false");
}
@Test
@Order(22)
void testPinUnpinChannel() throws Exception {
List<String> userIds = createTestUsers(2);
createdUserIds.addAll(userIds);
String creatorId = userIds.get(0);
String memberId = userIds.get(1);
String channelId = createTestChannelWithMembers(creatorId, userIds);
createdChannelIds.add(channelId);
String cid = "messaging:" + channelId;
// Pin the channel for memberId via UpdateMemberPartial
var pinResp =
chat.updateMemberPartial(
"messaging",
channelId,
UpdateMemberPartialRequest.builder()
.UserID(memberId)
.set(Map.of("pinned", true))
.build())
.execute();
assertNotNull(pinResp.getData(), "Pin response should not be null");
assertNotNull(
pinResp.getData().getChannelMember(), "ChannelMember should not be null after pin");
// Verify pinned via QueryChannels with pinned=true filter
var pinnedResp =
chat.queryChannels(
QueryChannelsRequest.builder()
.filterConditions(Map.of("pinned", true, "cid", cid))
.userID(memberId)
.build())
.execute();
assertNotNull(pinnedResp.getData(), "QueryChannels (pinned=true) response should not be null");
assertFalse(
pinnedResp.getData().getChannels().isEmpty(),
"Channel should appear in pinned=true query after pinning");
// Unpin the channel for memberId
var unpinResp =
chat.updateMemberPartial(
"messaging",
channelId,
UpdateMemberPartialRequest.builder()
.UserID(memberId)
.set(Map.of("pinned", false))
.build())
.execute();
assertNotNull(unpinResp.getData(), "Unpin response should not be null");
assertNotNull(
unpinResp.getData().getChannelMember(), "ChannelMember should not be null after unpin");
// Verify unpinned via QueryChannels with pinned=false filter
var unpinnedResp =
chat.queryChannels(
QueryChannelsRequest.builder()
.filterConditions(Map.of("pinned", false, "cid", cid))
.userID(memberId)
.build())
.execute();
assertNotNull(
unpinnedResp.getData(), "QueryChannels (pinned=false) response should not be null");
assertFalse(
unpinnedResp.getData().getChannels().isEmpty(),
"Channel should appear in pinned=false query after unpinning");
}
@Test
@Order(23)
void testArchiveUnarchiveChannel() throws Exception {
List<String> userIds = createTestUsers(2);
createdUserIds.addAll(userIds);
String creatorId = userIds.get(0);
String memberId = userIds.get(1);
String channelId = createTestChannelWithMembers(creatorId, userIds);
createdChannelIds.add(channelId);
String cid = "messaging:" + channelId;
// Archive the channel for memberId via UpdateMemberPartial
var archiveResp =
chat.updateMemberPartial(
"messaging",
channelId,
UpdateMemberPartialRequest.builder()
.UserID(memberId)
.set(Map.of("archived", true))
.build())
.execute();
assertNotNull(archiveResp.getData(), "Archive response should not be null");
assertNotNull(
archiveResp.getData().getChannelMember(), "ChannelMember should not be null after archive");
// Verify archived via QueryChannels with archived=true filter
var archivedResp =
chat.queryChannels(
QueryChannelsRequest.builder()
.filterConditions(Map.of("archived", true, "cid", cid))
.userID(memberId)
.build())
.execute();
assertNotNull(
archivedResp.getData(), "QueryChannels (archived=true) response should not be null");
assertFalse(
archivedResp.getData().getChannels().isEmpty(),
"Channel should appear in archived=true query after archiving");
// Unarchive the channel for memberId
var unarchiveResp =
chat.updateMemberPartial(
"messaging",
channelId,
UpdateMemberPartialRequest.builder()
.UserID(memberId)
.set(Map.of("archived", false))
.build())
.execute();
assertNotNull(unarchiveResp.getData(), "Unarchive response should not be null");
assertNotNull(
unarchiveResp.getData().getChannelMember(),
"ChannelMember should not be null after unarchive");
// Verify unarchived via QueryChannels with archived=false filter
var unarchivedResp =
chat.queryChannels(
QueryChannelsRequest.builder()
.filterConditions(Map.of("archived", false, "cid", cid))
.userID(memberId)
.build())
.execute();
assertNotNull(
unarchivedResp.getData(), "QueryChannels (archived=false) response should not be null");
assertFalse(
unarchivedResp.getData().getChannels().isEmpty(),
"Channel should appear in archived=false query after unarchiving");
}
@Test
@Order(24)
void testAddMembersWithRoles() throws Exception {
List<String> creatorIds = createTestUsers(1);
createdUserIds.addAll(creatorIds);
String creatorId = creatorIds.get(0);
String channelId = createTestChannel(creatorId);
createdChannelIds.add(channelId);
// Create 2 more users to add with specific roles
List<String> newMemberIds = createTestUsers(2);
createdUserIds.addAll(newMemberIds);
String moderatorId = newMemberIds.get(0);
String memberId = newMemberIds.get(1);
// Add members with specific channel roles
chat.updateChannel(
"messaging",
channelId,
UpdateChannelRequest.builder()
.addMembers(
List.of(
ChannelMemberRequest.builder()
.userID(moderatorId)
.channelRole("channel_moderator")
.build(),
ChannelMemberRequest.builder()
.userID(memberId)
.channelRole("channel_member")
.build()))
.build())
.execute();
// Verify roles via queryMembers with $in filter
var qResp =
chat.queryMembers(
QueryMembersRequest.builder()
.Payload(