@@ -1226,10 +1226,231 @@ extension LivestreamChannelController_Tests {
1226
1226
) ,
1227
1227
didReceiveEvent: event
1228
1228
)
1229
-
1229
+
1230
1230
// Then
1231
1231
XCTAssertEqual ( controller. channel? . name, " Updated Name " )
1232
1232
}
1233
+
1234
+ // MARK: - Member Update Events Tests
1235
+
1236
+ func test_didReceiveEvent_memberRelatedEvents_updateChannelFromDataStore( ) {
1237
+ let cid = controller. cid!
1238
+
1239
+ // Test data for all member-related events
1240
+ let memberEvents : [ ( event: Event , description: String ) ] = [
1241
+ (
1242
+ MemberAddedEvent (
1243
+ user: . mock( id: . unique) ,
1244
+ cid: cid,
1245
+ member: . mock( id: . unique) ,
1246
+ createdAt: . unique
1247
+ ) ,
1248
+ " MemberAddedEvent "
1249
+ ) ,
1250
+ (
1251
+ MemberRemovedEvent (
1252
+ user: . mock( id: . unique) ,
1253
+ cid: cid,
1254
+ createdAt: . unique
1255
+ ) ,
1256
+ " MemberRemovedEvent "
1257
+ ) ,
1258
+ (
1259
+ MemberUpdatedEvent (
1260
+ user: . mock( id: . unique) ,
1261
+ cid: cid,
1262
+ member: . mock( id: . unique) ,
1263
+ createdAt: . unique
1264
+ ) ,
1265
+ " MemberUpdatedEvent "
1266
+ ) ,
1267
+ (
1268
+ NotificationAddedToChannelEvent (
1269
+ channel: . mock( cid: cid) ,
1270
+ unreadCount: nil ,
1271
+ member: . mock( id: . unique) ,
1272
+ createdAt: . unique
1273
+ ) ,
1274
+ " NotificationAddedToChannelEvent "
1275
+ ) ,
1276
+ (
1277
+ NotificationRemovedFromChannelEvent (
1278
+ user: . mock( id: . unique) ,
1279
+ cid: cid,
1280
+ member: . mock( id: . unique) ,
1281
+ createdAt: . unique
1282
+ ) ,
1283
+ " NotificationRemovedFromChannelEvent "
1284
+ ) ,
1285
+ (
1286
+ NotificationInvitedEvent (
1287
+ user: . mock( id: . unique) ,
1288
+ cid: cid,
1289
+ member: . mock( id: . unique) ,
1290
+ createdAt: . unique
1291
+ ) ,
1292
+ " NotificationInvitedEvent "
1293
+ ) ,
1294
+ (
1295
+ NotificationInviteAcceptedEvent (
1296
+ user: . mock( id: . unique) ,
1297
+ channel: . mock( cid: cid) ,
1298
+ member: . mock( id: . unique) ,
1299
+ createdAt: . unique
1300
+ ) ,
1301
+ " NotificationInviteAcceptedEvent "
1302
+ ) ,
1303
+ (
1304
+ NotificationInviteRejectedEvent (
1305
+ user: . mock( id: . unique) ,
1306
+ channel: . mock( cid: cid) ,
1307
+ member: . mock( id: . unique) ,
1308
+ createdAt: . unique
1309
+ ) ,
1310
+ " NotificationInviteRejectedEvent "
1311
+ )
1312
+ ]
1313
+
1314
+ // Test each member-related event
1315
+ for (index, ( event, description) ) in memberEvents. enumerated ( ) {
1316
+ let initialMemberCount = 10 + index
1317
+ let updatedMemberCount = 20 + index
1318
+
1319
+ // Given - Set up initial channel in database
1320
+ let initialChannelPayload = ChannelPayload . dummy (
1321
+ channel: . dummy( cid: cid, memberCount: initialMemberCount)
1322
+ )
1323
+ try ! client. databaseContainer. writeSynchronously { session in
1324
+ try session. saveChannel ( payload: initialChannelPayload)
1325
+ }
1326
+
1327
+ // Load initial data
1328
+ let exp = expectation ( description: " sync completes " )
1329
+ controller. synchronize { _ in
1330
+ exp. fulfill ( )
1331
+ }
1332
+ client. mockAPIClient. test_simulateResponse ( . success( initialChannelPayload) )
1333
+
1334
+ waitForExpectations ( timeout: defaultTimeout)
1335
+ XCTAssertEqual ( controller. channel? . memberCount, initialMemberCount, " Initial setup failed for \( description) " )
1336
+
1337
+ // Update channel in database with new member count
1338
+ let updatedChannelPayload = ChannelPayload . dummy (
1339
+ channel: . dummy( cid: cid, memberCount: updatedMemberCount)
1340
+ )
1341
+ try ! client. databaseContainer. writeSynchronously { session in
1342
+ try session. saveChannel ( payload: updatedChannelPayload)
1343
+ }
1344
+
1345
+ // When
1346
+ controller. eventsController (
1347
+ EventsController ( notificationCenter: client. eventNotificationCenter) ,
1348
+ didReceiveEvent: event
1349
+ )
1350
+
1351
+ // Then
1352
+ XCTAssertEqual (
1353
+ controller. channel? . memberCount,
1354
+ updatedMemberCount,
1355
+ " \( description) should update channel from data store "
1356
+ )
1357
+ }
1358
+ }
1359
+
1360
+ func test_didReceiveEvent_channelTruncatedEvent_updatesChannelAndMessages( ) {
1361
+ // Given - Set up initial channel with messages
1362
+ let cid = controller. cid!
1363
+ let initialMessage1 = ChatMessage . mock ( id: " message1 " , cid: cid, text: " Message 1 " )
1364
+ let initialMessage2 = ChatMessage . mock ( id: " message2 " , cid: cid, text: " Message 2 " )
1365
+
1366
+ // Load initial messages
1367
+ controller. eventsController (
1368
+ EventsController ( notificationCenter: client. eventNotificationCenter) ,
1369
+ didReceiveEvent: MessageNewEvent (
1370
+ user: . mock( id: . unique) ,
1371
+ message: initialMessage1,
1372
+ channel: . mock( cid: cid) ,
1373
+ createdAt: . unique,
1374
+ watcherCount: nil ,
1375
+ unreadCount: nil
1376
+ )
1377
+ )
1378
+ controller. eventsController (
1379
+ EventsController ( notificationCenter: client. eventNotificationCenter) ,
1380
+ didReceiveEvent: MessageNewEvent (
1381
+ user: . mock( id: . unique) ,
1382
+ message: initialMessage2,
1383
+ channel: . mock( cid: cid) ,
1384
+ createdAt: . unique,
1385
+ watcherCount: nil ,
1386
+ unreadCount: nil
1387
+ )
1388
+ )
1389
+ XCTAssertEqual ( controller. messages. count, 2 )
1390
+
1391
+ // Create truncated channel and truncation message
1392
+ let truncatedChannel = ChatChannel . mock ( cid: cid, name: " Truncated Channel " )
1393
+ let truncationMessage = ChatMessage . mock ( id: " truncation " , cid: cid, text: " Channel was truncated " )
1394
+
1395
+ let event = ChannelTruncatedEvent (
1396
+ channel: truncatedChannel,
1397
+ user: . mock( id: . unique) ,
1398
+ message: truncationMessage,
1399
+ createdAt: . unique
1400
+ )
1401
+
1402
+ // When
1403
+ controller. eventsController (
1404
+ EventsController ( notificationCenter: client. eventNotificationCenter) ,
1405
+ didReceiveEvent: event
1406
+ )
1407
+
1408
+ // Then
1409
+ XCTAssertEqual ( controller. channel? . name, " Truncated Channel " )
1410
+ XCTAssertEqual ( controller. messages. count, 1 )
1411
+ XCTAssertEqual ( controller. messages. first? . id, " truncation " )
1412
+ XCTAssertEqual ( controller. messages. first? . text, " Channel was truncated " )
1413
+ }
1414
+
1415
+ func test_didReceiveEvent_channelTruncatedEventWithoutMessage_clearsMessages( ) {
1416
+ // Given - Set up initial channel with messages
1417
+ let cid = controller. cid!
1418
+ let initialMessage = ChatMessage . mock ( id: " message1 " , cid: cid, text: " Message 1 " )
1419
+
1420
+ // Load initial message
1421
+ controller. eventsController (
1422
+ EventsController ( notificationCenter: client. eventNotificationCenter) ,
1423
+ didReceiveEvent: MessageNewEvent (
1424
+ user: . mock( id: . unique) ,
1425
+ message: initialMessage,
1426
+ channel: . mock( cid: cid) ,
1427
+ createdAt: . unique,
1428
+ watcherCount: nil ,
1429
+ unreadCount: nil
1430
+ )
1431
+ )
1432
+ XCTAssertEqual ( controller. messages. count, 1 )
1433
+
1434
+ // Create truncated channel without truncation message
1435
+ let truncatedChannel = ChatChannel . mock ( cid: cid, name: " Truncated Channel " )
1436
+
1437
+ let event = ChannelTruncatedEvent (
1438
+ channel: truncatedChannel,
1439
+ user: . mock( id: . unique) ,
1440
+ message: nil , // No truncation message
1441
+ createdAt: . unique
1442
+ )
1443
+
1444
+ // When
1445
+ controller. eventsController (
1446
+ EventsController ( notificationCenter: client. eventNotificationCenter) ,
1447
+ didReceiveEvent: event
1448
+ )
1449
+
1450
+ // Then
1451
+ XCTAssertEqual ( controller. channel? . name, " Truncated Channel " )
1452
+ XCTAssertTrue ( controller. messages. isEmpty)
1453
+ }
1233
1454
1234
1455
func test_didReceiveEvent_differentChannelEvent_isIgnored( ) {
1235
1456
let otherChannelId = ChannelId . unique
0 commit comments