From 585010d8392be40038656afa5eb54f2e3a2b5e4d Mon Sep 17 00:00:00 2001 From: Naomi Plasterer Date: Thu, 24 Oct 2024 12:02:46 -0700 Subject: [PATCH] beefing up all the tests --- Sources/XMTPiOS/Conversations.swift | 2 +- Tests/XMTPTests/GroupTests.swift | 58 +++++++++++++-------- Tests/XMTPTests/V3ClientTests.swift | 80 +++++++++++++++++++++++++++++ XMTP.podspec | 2 +- 4 files changed, 120 insertions(+), 22 deletions(-) diff --git a/Sources/XMTPiOS/Conversations.swift b/Sources/XMTPiOS/Conversations.swift index 19f7376b..bbca3a91 100644 --- a/Sources/XMTPiOS/Conversations.swift +++ b/Sources/XMTPiOS/Conversations.swift @@ -296,7 +296,7 @@ public actor Conversations { } } - private func streamConversations() -> AsyncThrowingStream { + public func streamConversations() -> AsyncThrowingStream { AsyncThrowingStream { continuation in if (client.hasV2Client) { continuation.finish(throwing: ConversationError.v2NotSupported("Only supported with V3 only clients use stream instead")) diff --git a/Tests/XMTPTests/GroupTests.swift b/Tests/XMTPTests/GroupTests.swift index cfdeb760..c23f2872 100644 --- a/Tests/XMTPTests/GroupTests.swift +++ b/Tests/XMTPTests/GroupTests.swift @@ -39,51 +39,52 @@ class GroupTests: XCTestCase { var alice: PrivateKey! var bob: PrivateKey! var fred: PrivateKey! + var davonV3: PrivateKey! var aliceClient: Client! var bobClient: Client! var fredClient: Client! + var davonV3Client: Client! } func localFixtures() async throws -> LocalFixtures { let key = try Crypto.secureRandomBytes(count: 32) + let options = ClientOptions.init( + api: .init(env: .local, isSecure: false), + codecs: [GroupUpdatedCodec()], + enableV3: true, + encryptionKey: key + ) let alice = try PrivateKey.generate() let aliceClient = try await Client.create( account: alice, - options: .init( - api: .init(env: .local, isSecure: false), - codecs: [GroupUpdatedCodec()], - enableV3: true, - encryptionKey: key - ) + options: options ) let bob = try PrivateKey.generate() let bobClient = try await Client.create( account: bob, - options: .init( - api: .init(env: .local, isSecure: false), - codecs: [GroupUpdatedCodec()], - enableV3: true, - encryptionKey: key - ) + options: options ) let fred = try PrivateKey.generate() let fredClient = try await Client.create( account: fred, - options: .init( - api: .init(env: .local, isSecure: false), - codecs: [GroupUpdatedCodec()], - enableV3: true, - encryptionKey: key - ) + options: options + ) + + let davonV3 = try PrivateKey.generate() + let davonV3Client = try await Client.createV3( + account: davonV3, + options: options ) return .init( alice: alice, bob: bob, fred: fred, + davonV3: davonV3, aliceClient: aliceClient, bobClient: bobClient, - fredClient: fredClient + fredClient: fredClient, + davonV3Client: davonV3Client ) } @@ -195,7 +196,10 @@ class GroupTests: XCTestCase { func testCanListGroups() async throws { let fixtures = try await localFixtures() _ = try await fixtures.aliceClient.conversations.newGroup(with: [fixtures.bob.address]) - + _ = try await fixtures.davonV3Client.conversations.findOrCreateDm(with: fixtures.bob.walletAddress) + _ = try await fixtures.davonV3Client.conversations.findOrCreateDm(with: fixtures.alice.walletAddress) + + try await fixtures.aliceClient.conversations.sync() let aliceGroupCount = try await fixtures.aliceClient.conversations.groups().count try await fixtures.bobClient.conversations.sync() @@ -209,6 +213,8 @@ class GroupTests: XCTestCase { let fixtures = try await localFixtures() _ = try await fixtures.aliceClient.conversations.newGroup(with: [fixtures.bob.address]) _ = try await fixtures.aliceClient.conversations.newConversation(with: fixtures.bob.address) + _ = try await fixtures.davonV3Client.conversations.findOrCreateDm(with: fixtures.bob.walletAddress) + _ = try await fixtures.davonV3Client.conversations.findOrCreateDm(with: fixtures.alice.walletAddress) let aliceGroupCount = try await fixtures.aliceClient.conversations.list(includeGroups: true).count @@ -557,6 +563,7 @@ class GroupTests: XCTestCase { } _ = try await fixtures.bobClient.conversations.newGroup(with: [fixtures.alice.address]) + _ = try await fixtures.davonV3Client.conversations.findOrCreateDm(with: fixtures.alice.address) await fulfillment(of: [expectation1], timeout: 3) } @@ -575,6 +582,7 @@ class GroupTests: XCTestCase { _ = try await fixtures.bobClient.conversations.newGroup(with: [fixtures.alice.address]) _ = try await fixtures.bobClient.conversations.newConversation(with: fixtures.alice.address) + _ = try await fixtures.davonV3Client.conversations.findOrCreateDm(with: fixtures.alice.address) await fulfillment(of: [expectation1], timeout: 3) } @@ -659,6 +667,8 @@ class GroupTests: XCTestCase { expectation1.expectedFulfillmentCount = 2 let convo = try await fixtures.bobClient.conversations.newConversation(with: fixtures.alice.address) let group = try await fixtures.bobClient.conversations.newGroup(with: [fixtures.alice.address]) + let dm = try await fixtures.davonV3Client.conversations.findOrCreateDm(with: fixtures.alice.address) + try await fixtures.aliceClient.conversations.sync() Task(priority: .userInitiated) { for try await _ in try await fixtures.aliceClient.conversations.streamAllMessages(includeGroups: true) { @@ -668,6 +678,7 @@ class GroupTests: XCTestCase { _ = try await group.send(content: "hi") _ = try await convo.send(content: "hi") + _ = try await dm.send(content: "hi") await fulfillment(of: [expectation1], timeout: 3) } @@ -680,6 +691,7 @@ class GroupTests: XCTestCase { expectation1.expectedFulfillmentCount = 2 let convo = try await fixtures.bobClient.conversations.newConversation(with: fixtures.alice.address) let group = try await fixtures.bobClient.conversations.newGroup(with: [fixtures.alice.address]) + let dm = try await fixtures.davonV3Client.conversations.findOrCreateDm(with: fixtures.alice.address) try await fixtures.aliceClient.conversations.sync() Task(priority: .userInitiated) { for try await _ in await fixtures.aliceClient.conversations.streamAllDecryptedMessages(includeGroups: true) { @@ -690,6 +702,7 @@ class GroupTests: XCTestCase { _ = try await group.send(content: "hi") _ = try await group.send(content: membershipChange, options: SendOptions(contentType: ContentTypeGroupUpdated)) _ = try await convo.send(content: "hi") + _ = try await dm.send(content: "hi") await fulfillment(of: [expectation1], timeout: 3) } @@ -700,6 +713,7 @@ class GroupTests: XCTestCase { let expectation1 = XCTestExpectation(description: "got a conversation") let group = try await fixtures.bobClient.conversations.newGroup(with: [fixtures.alice.address]) + let dm = try await fixtures.davonV3Client.conversations.findOrCreateDm(with: fixtures.alice.address) try await fixtures.aliceClient.conversations.sync() Task(priority: .userInitiated) { for try await _ in await fixtures.aliceClient.conversations.streamAllGroupMessages() { @@ -708,6 +722,7 @@ class GroupTests: XCTestCase { } _ = try await group.send(content: "hi") + _ = try await dm.send(content: "hi") await fulfillment(of: [expectation1], timeout: 3) } @@ -717,6 +732,8 @@ class GroupTests: XCTestCase { let expectation1 = XCTestExpectation(description: "got a conversation") let group = try await fixtures.bobClient.conversations.newGroup(with: [fixtures.alice.address]) + let dm = try await fixtures.davonV3Client.conversations.findOrCreateDm(with: fixtures.alice.address) + try await fixtures.aliceClient.conversations.sync() Task(priority: .userInitiated) { for try await _ in await fixtures.aliceClient.conversations.streamAllGroupDecryptedMessages() { @@ -725,6 +742,7 @@ class GroupTests: XCTestCase { } _ = try await group.send(content: "hi") + _ = try await dm.send(content: "hi") await fulfillment(of: [expectation1], timeout: 3) } diff --git a/Tests/XMTPTests/V3ClientTests.swift b/Tests/XMTPTests/V3ClientTests.swift index 49c203da..78bc22db 100644 --- a/Tests/XMTPTests/V3ClientTests.swift +++ b/Tests/XMTPTests/V3ClientTests.swift @@ -188,6 +188,29 @@ class V3ClientTests: XCTestCase { XCTAssertEqual(sameGroupMessages?.first?.body, "gm") } + func testsCanSendMessagesToDm() async throws { + let fixtures = try await localFixtures() + let dm = try await fixtures.boV3Client.conversations.findOrCreateDm(with: fixtures.caroV2V3.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.caroV2V3Client.conversations.sync() + let sameDm = try await fixtures.caroV2V3Client.findDm(address: fixtures.boV3Client.address) + try await sameDm?.sync() + + let sameDmMessages = try await sameDm?.messages() + XCTAssertEqual(sameDmMessages?.count, 2) + XCTAssertEqual(sameDmMessages?.first?.body, "gm") + } + func testGroupConsent() async throws { let fixtures = try await localFixtures() let group = try await fixtures.boV3Client.conversations.newGroup(with: [fixtures.caroV2V3.address]) @@ -243,7 +266,64 @@ class V3ClientTests: XCTestCase { XCTAssert(isAddressAllowed) XCTAssert(!isAddressDenied) } + + func testCanStreamAllMessagesFromV3Users() async throws { + let fixtures = try await localFixtures() + + let expectation1 = XCTestExpectation(description: "got a conversation") + expectation1.expectedFulfillmentCount = 2 + let convo = try await fixtures.boV3Client.conversations.findOrCreateDm(with: fixtures.caroV2V3.address) + let group = try await fixtures.caroV2V3Client.conversations.newGroup(with: [fixtures.boV3.address]) + try await fixtures.caroV2V3Client.conversations.sync() + Task(priority: .userInitiated) { + for try await _ in await fixtures.boV3Client.conversations.streamAllConversationMessages() { + expectation1.fulfill() + } + } + + _ = try await group.send(content: "hi") + _ = try await convo.send(content: "hi") + + await fulfillment(of: [expectation1], timeout: 3) + } + + func testCanStreamAllDecryptedMessagesFromV3Users() async throws { + let fixtures = try await localFixtures() + + let expectation1 = XCTestExpectation(description: "got a conversation") + expectation1.expectedFulfillmentCount = 2 + let convo = try await fixtures.boV3Client.conversations.findOrCreateDm(with: fixtures.caroV2V3.address) + let group = try await fixtures.caroV2V3Client.conversations.newGroup(with: [fixtures.boV3.address]) + try await fixtures.boV3Client.conversations.sync() + Task(priority: .userInitiated) { + for try await _ in await fixtures.boV3Client.conversations.streamAllDecryptedConversationMessages() { + expectation1.fulfill() + } + } + + _ = try await group.send(content: "hi") + _ = try await convo.send(content: "hi") + + await fulfillment(of: [expectation1], timeout: 3) + } + + func testCanStreamGroupsAndConversationsFromV3Users() async throws { + let fixtures = try await localFixtures() + let expectation1 = XCTestExpectation(description: "got a conversation") + expectation1.expectedFulfillmentCount = 2 + + Task(priority: .userInitiated) { + for try await _ in await fixtures.boV3Client.conversations.streamConversations() { + expectation1.fulfill() + } + } + + _ = try await fixtures.caroV2V3Client.conversations.newGroup(with: [fixtures.boV3.address]) + _ = try await fixtures.boV3Client.conversations.findOrCreateDm(with: fixtures.caroV2V3.address) + + await fulfillment(of: [expectation1], timeout: 3) + } func testCanStreamAllMessagesFromV2andV3Users() async throws { let fixtures = try await localFixtures() diff --git a/XMTP.podspec b/XMTP.podspec index e3f0e969..505cf860 100644 --- a/XMTP.podspec +++ b/XMTP.podspec @@ -16,7 +16,7 @@ Pod::Spec.new do |spec| # spec.name = "XMTP" - spec.version = "0.15.2" + spec.version = "0.16.0" spec.summary = "XMTP SDK Cocoapod" # This description is used to generate tags and improve search results.