UnitNCRoomsManagerTest.swift 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. //
  2. // SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
  3. // SPDX-License-Identifier: GPL-3.0-or-later
  4. //
  5. import XCTest
  6. @testable import NextcloudTalk
  7. final class UnitNCRoomsManagerTest: TestBaseRealm {
  8. func testOfflineMessageFailure() throws {
  9. let activeAccount = NCDatabaseManager.sharedInstance().activeAccount()
  10. let roomToken = "offToken"
  11. addRoom(withToken: roomToken)
  12. // Create 2 messages which are in different sections
  13. let oldOfflineMessage = NCChatMessage()
  14. oldOfflineMessage.internalId = "internal1"
  15. oldOfflineMessage.accountId = activeAccount.accountId
  16. oldOfflineMessage.actorDisplayName = activeAccount.userDisplayName
  17. oldOfflineMessage.actorId = activeAccount.userId
  18. oldOfflineMessage.actorType = "users"
  19. oldOfflineMessage.token = roomToken
  20. oldOfflineMessage.message = "Message 1"
  21. oldOfflineMessage.isOfflineMessage = true
  22. oldOfflineMessage.sendingFailed = false
  23. // 12h is the threshold, set it to 13 hours
  24. oldOfflineMessage.timestamp = Int(Date().timeIntervalSince1970) - (60 * 60 * 13)
  25. try? realm.transaction {
  26. realm.add(oldOfflineMessage)
  27. }
  28. XCTAssertEqual(NCChatMessage.allObjects().count, 1)
  29. let exp = expectation(description: "\(#function)\(#line)")
  30. expectation(forNotification: .NCChatControllerDidSendChatMessage, object: NCRoomsManager.sharedInstance())
  31. NCRoomsManager.sharedInstance().resendOfflineMessages(forToken: roomToken) {
  32. exp.fulfill()
  33. }
  34. waitForExpectations(timeout: TestConstants.timeoutShort, handler: nil)
  35. let realmMessage = NCChatMessage.allObjects().firstObject()!
  36. XCTAssertTrue(realmMessage.sendingFailed)
  37. XCTAssertFalse(realmMessage.isOfflineMessage)
  38. }
  39. func testRoomsForAccount() throws {
  40. let nonFavOld = addRoom(withToken: "NonFavOld") { room in
  41. room.lastActivity = 100
  42. }
  43. let nonFavNew = addRoom(withToken: "NonFavNew") { room in
  44. room.lastActivity = 1000
  45. }
  46. let favOld = addRoom(withToken: "FavOld") { room in
  47. room.lastActivity = 100
  48. room.isFavorite = true
  49. }
  50. let favNew = addRoom(withToken: "FavNew") { room in
  51. room.lastActivity = 1000
  52. room.isFavorite = true
  53. }
  54. // Add an unrelated room, which should not be returned
  55. addRoom(withToken: "Unrelated", withAccountId: "foo")
  56. let activeAccount = NCDatabaseManager.sharedInstance().activeAccount()
  57. let rooms = NCRoomsManager.sharedInstance().roomsForAccountId(activeAccount.accountId, withRealm: nil)
  58. let expectedOrder = [favNew, favOld, nonFavNew, nonFavOld]
  59. XCTAssertEqual(rooms.count, 4)
  60. // Check if the order is correct
  61. for (index, element) in rooms.enumerated() {
  62. XCTAssertEqual(expectedOrder[index].token, element.token)
  63. }
  64. }
  65. }