IntegrationChatTest.swift 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. //
  2. // SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
  3. // SPDX-License-Identifier: GPL-3.0-or-later
  4. //
  5. import XCTest
  6. import Foundation
  7. @testable import NextcloudTalk
  8. final class IntegrationChatTest: TestBase {
  9. func testSendMessage() throws {
  10. let activeAccount = NCDatabaseManager.sharedInstance().activeAccount()
  11. let roomName = "Integration Test Room 👍"
  12. let chatMessage = "Test Message 😀😆"
  13. var exp = expectation(description: "\(#function)\(#line)")
  14. var roomToken = ""
  15. // Create a room
  16. NCAPIController.sharedInstance().createRoom(forAccount: activeAccount, withInvite: nil, ofType: .public, andName: roomName) { room, error in
  17. XCTAssertNil(error)
  18. roomToken = room?.token ?? ""
  19. exp.fulfill()
  20. }
  21. waitForExpectations(timeout: TestConstants.timeoutLong, handler: nil)
  22. // Send a message
  23. exp = expectation(description: "\(#function)\(#line)")
  24. NCAPIController.sharedInstance().sendChatMessage(chatMessage, toRoom: roomToken, displayName: "", replyTo: 0, referenceId: "", silently: false, for: activeAccount) { error in
  25. XCTAssertNil(error)
  26. exp.fulfill()
  27. }
  28. waitForExpectations(timeout: TestConstants.timeoutLong, handler: nil)
  29. // Try to receive the sent message
  30. exp = expectation(description: "\(#function)\(#line)")
  31. NCAPIController.sharedInstance().receiveChatMessages(ofRoom: roomToken,
  32. fromLastMessageId: 0,
  33. history: true,
  34. includeLastMessage: true,
  35. timeout: false,
  36. lastCommonReadMessage: 0,
  37. setReadMarker: false,
  38. markNotificationsAsRead: false,
  39. for: activeAccount) { messages, _, _, error, errorCode in
  40. XCTAssertNil(error)
  41. XCTAssertEqual(errorCode, 0)
  42. for rawMessage in messages! {
  43. if let message = rawMessage as? NSDictionary {
  44. // swiftlint:disable:next force_cast
  45. if message.object(forKey: "message") as! String == chatMessage {
  46. exp.fulfill()
  47. return
  48. }
  49. }
  50. }
  51. }
  52. waitForExpectations(timeout: TestConstants.timeoutLong, handler: nil)
  53. }
  54. }