From 4588b4dd6cf7c061c40d6412ea677e3760da2aa1 Mon Sep 17 00:00:00 2001 From: Naomi Plasterer Date: Thu, 7 Nov 2024 20:50:00 -0800 Subject: [PATCH] fix up all the tests --- Sources/XMTPTestHelpers/TestHelpers.swift | 34 --- Tests/XMTPTests/ConversationsTests.swift | 245 ---------------------- Tests/XMTPTests/GroupTests.swift | 102 +++++++++ 3 files changed, 102 insertions(+), 279 deletions(-) delete mode 100644 Tests/XMTPTests/ConversationsTests.swift diff --git a/Sources/XMTPTestHelpers/TestHelpers.swift b/Sources/XMTPTestHelpers/TestHelpers.swift index e9cb1d83..d32a1462 100644 --- a/Sources/XMTPTestHelpers/TestHelpers.swift +++ b/Sources/XMTPTestHelpers/TestHelpers.swift @@ -61,40 +61,6 @@ } } - public struct FakeSCWWallet: SigningKey { - public var walletAddress: String - private var internalSignature: String - - public init() throws { - // Simulate a wallet address (could be derived from a hash of some internal data) - self.walletAddress = UUID().uuidString // Using UUID for uniqueness in this fake example - self.internalSignature = Data(repeating: 0x01, count: 64).toHex // Fake internal signature - } - - public var address: String { - walletAddress - } - - public var type: WalletType { - WalletType.SCW - } - - public var chainId: Int64? { - 1 - } - - public static func generate() throws -> FakeSCWWallet { - return try FakeSCWWallet() - } - - public func signSCW(message: String) async throws -> Data { - // swiftlint:disable force_unwrapping - let digest = SHA256.hash(data: message.data(using: .utf8)!) - // swiftlint:enable force_unwrapping - return Data(digest) - } - } - @available(iOS 15, *) public struct Fixtures { public var alix: PrivateKey! diff --git a/Tests/XMTPTests/ConversationsTests.swift b/Tests/XMTPTests/ConversationsTests.swift deleted file mode 100644 index dc893a7b..00000000 --- a/Tests/XMTPTests/ConversationsTests.swift +++ /dev/null @@ -1,245 +0,0 @@ -import LibXMTP -import XCTest -import XMTPTestHelpers - -@testable import XMTPiOS -@available(iOS 16, *) -class ConversationsTests: XCTestCase { - func testsCanCreateGroup() async throws { - let fixtures = try await fixtures() - let group = try await fixtures.boClient.conversations.newGroup(with: [ - fixtures.caro.address - ]) - let members = try await group.members.map(\.inboxId).sorted() - XCTAssertEqual( - [fixtures.caroClient.inboxID, fixtures.boClient.inboxID] - .sorted(), members) - - await assertThrowsAsyncError( - try await fixtures.boClient.conversations.newGroup(with: [ - PrivateKey().address - ]) - ) - } - - func testCanCreateDm() async throws { - let fixtures = try await fixtures() - - let dm = try await fixtures.boClient.conversations.findOrCreateDm( - with: fixtures.alix.walletAddress) - let members = try await dm.members - XCTAssertEqual(members.count, 2) - - let sameDm = try await fixtures.boClient.findDm( - address: fixtures.alix.walletAddress) - XCTAssertEqual(sameDm?.id, dm.id) - - try await fixtures.alixClient.conversations.sync() - let caroDm = try await fixtures.alixClient.findDm( - address: fixtures.boClient.address) - XCTAssertEqual(caroDm?.id, dm.id) - - await assertThrowsAsyncError( - try await fixtures.boClient.conversations.findOrCreateDm( - with: PrivateKey().address) - ) - } - - func testCanFindConversationByTopic() async throws { - let fixtures = try await fixtures() - - let group = try await fixtures.boClient.conversations.newGroup(with: [ - fixtures.caro.walletAddress - ]) - let dm = try await fixtures.boClient.conversations.findOrCreateDm( - with: fixtures.caro.walletAddress) - - let sameDm = try fixtures.boClient.findConversationByTopic( - topic: dm.topic) - let sameGroup = try fixtures.boClient.findConversationByTopic( - topic: group.topic) - - XCTAssertEqual(group.id, try sameGroup?.id) - XCTAssertEqual(dm.id, try sameDm?.id) - } - - func testCanListConversations() async throws { - let fixtures = try await fixtures() - - let dm = try await fixtures.boClient.conversations.findOrCreateDm( - with: fixtures.caro.walletAddress) - let group = try await fixtures.boClient.conversations.newGroup(with: [ - fixtures.caro.walletAddress - ]) - - let convoCount = try await fixtures.boClient.conversations - .list().count - let dmCount = try await fixtures.boClient.conversations.listDms().count - let groupCount = try await fixtures.boClient.conversations.listGroups() - .count - XCTAssertEqual(convoCount, 2) - XCTAssertEqual(dmCount, 1) - XCTAssertEqual(groupCount, 1) - - try await fixtures.caroClient.conversations.sync() - let convoCount2 = try await fixtures.caroClient.conversations.list() - .count - let groupCount2 = try await fixtures.caroClient.conversations - .listGroups().count - XCTAssertEqual(convoCount2, 1) - XCTAssertEqual(groupCount2, 1) - } - - func testCanListConversationsFiltered() async throws { - let fixtures = try await fixtures() - - let dm = try await fixtures.boClient.conversations.findOrCreateDm( - with: fixtures.caro.walletAddress) - let group = try await fixtures.boClient.conversations.newGroup(with: [ - fixtures.caro.walletAddress - ]) - - let convoCount = try await fixtures.boClient.conversations - .list().count - let convoCountConsent = try await fixtures.boClient.conversations - .list(consentState: .allowed).count - - XCTAssertEqual(convoCount, 2) - XCTAssertEqual(convoCountConsent, 2) - - try await group.updateConsentState(state: .denied) - - let convoCountAllowed = try await fixtures.boClient.conversations - .list(consentState: .allowed).count - let convoCountDenied = try await fixtures.boClient.conversations - .list(consentState: .denied).count - - XCTAssertEqual(convoCountAllowed, 1) - XCTAssertEqual(convoCountDenied, 1) - } - - func testCanListConversationsOrder() async throws { - let fixtures = try await fixtures() - - let dm = try await fixtures.boClient.conversations.findOrCreateDm( - with: fixtures.caro.walletAddress) - let group1 = try await fixtures.boClient.conversations.newGroup( - with: [fixtures.caro.walletAddress]) - let group2 = try await fixtures.boClient.conversations.newGroup( - with: [fixtures.caro.walletAddress]) - - _ = try await dm.send(content: "Howdy") - _ = try await group2.send(content: "Howdy") - _ = try await fixtures.boClient.conversations.syncAllConversations() - - let conversations = try await fixtures.boClient.conversations - .list() - let conversationsOrdered = try await fixtures.boClient.conversations - .list(order: .lastMessage) - - XCTAssertEqual(conversations.count, 3) - XCTAssertEqual(conversationsOrdered.count, 3) - - XCTAssertEqual( - try conversations.map { try $0.id }, [dm.id, group1.id, group2.id]) - XCTAssertEqual( - try conversationsOrdered.map { try $0.id }, - [group2.id, dm.id, group1.id]) - } - - func testsCanSendMessages() async throws { - let fixtures = try await fixtures() - let group = try await fixtures.boClient.conversations.newGroup(with: [ - fixtures.caro.address - ]) - try await group.send(content: "howdy") - let messageId = try await group.send(content: "gm") - try await group.sync() - - let groupMessages = try await group.messages() - XCTAssertEqual(groupMessages.first?.body, "gm") - XCTAssertEqual(groupMessages.first?.id, messageId) - XCTAssertEqual(groupMessages.first?.deliveryStatus, .published) - XCTAssertEqual(groupMessages.count, 3) - - try await fixtures.caroClient.conversations.sync() - let sameGroup = try await fixtures.caroClient.conversations.listGroups() - .last - try await sameGroup?.sync() - - let sameGroupMessages = try await sameGroup?.messages() - XCTAssertEqual(sameGroupMessages?.count, 2) - XCTAssertEqual(sameGroupMessages?.first?.body, "gm") - } - - func testsCanSendMessagesToDm() async throws { - let fixtures = try await fixtures() - let dm = try await fixtures.boClient.conversations.findOrCreateDm( - with: fixtures.caro.address) - try await dm.send(content: "howdy") - let messageId = try await dm.send(content: "gm") - try await dm.sync() - - let dmMessages = try await dm.messages() - XCTAssertEqual(dmMessages.first?.body, "gm") - XCTAssertEqual(dmMessages.first?.id, messageId) - XCTAssertEqual(dmMessages.first?.deliveryStatus, .published) - XCTAssertEqual(dmMessages.count, 3) - - try await fixtures.caroClient.conversations.sync() - let sameDm = try await fixtures.caroClient.findDm( - address: fixtures.boClient.address) - try await sameDm?.sync() - - let sameDmMessages = try await sameDm?.messages() - XCTAssertEqual(sameDmMessages?.count, 2) - XCTAssertEqual(sameDmMessages?.first?.body, "gm") - } - - func testCanStreamAllMessages() async throws { - let fixtures = try await fixtures() - - let expectation1 = XCTestExpectation(description: "got a conversation") - expectation1.expectedFulfillmentCount = 2 - let convo = try await fixtures.boClient.conversations.findOrCreateDm( - with: fixtures.caro.address) - let group = try await fixtures.caroClient.conversations.newGroup( - with: [fixtures.bo.address]) - try await fixtures.boClient.conversations.sync() - Task(priority: .userInitiated) { - for try await _ in await fixtures.boClient.conversations - .streamAllMessages() - { - expectation1.fulfill() - } - } - - _ = try await group.send(content: "hi") - _ = try await convo.send(content: "hi") - - await fulfillment(of: [expectation1], timeout: 3) - } - - func testCanStreamGroupsAndConversations() async throws { - let fixtures = try await fixtures() - - let expectation1 = XCTestExpectation(description: "got a conversation") - expectation1.expectedFulfillmentCount = 2 - - Task(priority: .userInitiated) { - for try await _ in await fixtures.boClient.conversations - .stream() - { - expectation1.fulfill() - } - } - - _ = try await fixtures.caroClient.conversations.newGroup(with: [ - fixtures.bo.address - ]) - _ = try await fixtures.boClient.conversations.findOrCreateDm( - with: fixtures.caro.address) - - await fulfillment(of: [expectation1], timeout: 3) - } -} diff --git a/Tests/XMTPTests/GroupTests.swift b/Tests/XMTPTests/GroupTests.swift index 5c0143e7..c4050a9a 100644 --- a/Tests/XMTPTests/GroupTests.swift +++ b/Tests/XMTPTests/GroupTests.swift @@ -176,6 +176,108 @@ class GroupTests: XCTestCase { XCTAssertEqual(1, alixGroupCount) XCTAssertEqual(1, boGroupCount) } + + func testCanFindConversationByTopic() async throws { + let fixtures = try await fixtures() + + let group = try await fixtures.boClient.conversations.newGroup(with: [ + fixtures.caro.walletAddress + ]) + let dm = try await fixtures.boClient.conversations.findOrCreateDm( + with: fixtures.caro.walletAddress) + + let sameDm = try fixtures.boClient.findConversationByTopic( + topic: dm.topic) + let sameGroup = try fixtures.boClient.findConversationByTopic( + topic: group.topic) + + XCTAssertEqual(group.id, try sameGroup?.id) + XCTAssertEqual(dm.id, try sameDm?.id) + } + + func testCanListConversations() async throws { + let fixtures = try await fixtures() + + let dm = try await fixtures.boClient.conversations.findOrCreateDm( + with: fixtures.caro.walletAddress) + let group = try await fixtures.boClient.conversations.newGroup(with: [ + fixtures.caro.walletAddress + ]) + + let convoCount = try await fixtures.boClient.conversations + .list().count + let dmCount = try await fixtures.boClient.conversations.listDms().count + let groupCount = try await fixtures.boClient.conversations.listGroups() + .count + XCTAssertEqual(convoCount, 2) + XCTAssertEqual(dmCount, 1) + XCTAssertEqual(groupCount, 1) + + try await fixtures.caroClient.conversations.sync() + let convoCount2 = try await fixtures.caroClient.conversations.list() + .count + let groupCount2 = try await fixtures.caroClient.conversations + .listGroups().count + XCTAssertEqual(convoCount2, 2) + XCTAssertEqual(groupCount2, 1) + } + + func testCanListConversationsFiltered() async throws { + let fixtures = try await fixtures() + + let dm = try await fixtures.boClient.conversations.findOrCreateDm( + with: fixtures.caro.walletAddress) + let group = try await fixtures.boClient.conversations.newGroup(with: [ + fixtures.caro.walletAddress + ]) + + let convoCount = try await fixtures.boClient.conversations + .list().count + let convoCountConsent = try await fixtures.boClient.conversations + .list(consentState: .allowed).count + + XCTAssertEqual(convoCount, 2) + XCTAssertEqual(convoCountConsent, 2) + + try await group.updateConsentState(state: .denied) + + let convoCountAllowed = try await fixtures.boClient.conversations + .list(consentState: .allowed).count + let convoCountDenied = try await fixtures.boClient.conversations + .list(consentState: .denied).count + + XCTAssertEqual(convoCountAllowed, 1) + XCTAssertEqual(convoCountDenied, 1) + } + + func testCanListConversationsOrder() async throws { + let fixtures = try await fixtures() + + let dm = try await fixtures.boClient.conversations.findOrCreateDm( + with: fixtures.caro.walletAddress) + let group1 = try await fixtures.boClient.conversations.newGroup( + with: [fixtures.caro.walletAddress]) + let group2 = try await fixtures.boClient.conversations.newGroup( + with: [fixtures.caro.walletAddress]) + + _ = try await dm.send(content: "Howdy") + _ = try await group2.send(content: "Howdy") + _ = try await fixtures.boClient.conversations.syncAllConversations() + + let conversations = try await fixtures.boClient.conversations + .list() + let conversationsOrdered = try await fixtures.boClient.conversations + .list(order: .lastMessage) + + XCTAssertEqual(conversations.count, 3) + XCTAssertEqual(conversationsOrdered.count, 3) + + XCTAssertEqual( + try conversations.map { try $0.id }, [dm.id, group1.id, group2.id]) + XCTAssertEqual( + try conversationsOrdered.map { try $0.id }, + [group2.id, dm.id, group1.id]) + } func testCanListGroupsAndConversations() async throws { let fixtures = try await fixtures()