From 58f0c82f56843be118fa92f43ce9c8f73e2a1fce Mon Sep 17 00:00:00 2001 From: Naomi Plasterer Date: Fri, 20 Sep 2024 00:13:56 -0600 Subject: [PATCH] write tests for it --- Tests/XMTPTests/V3Client.swift | 68 -------------- Tests/XMTPTests/V3ClientTests.swift | 135 ++++++++++++++++++++++++++++ 2 files changed, 135 insertions(+), 68 deletions(-) delete mode 100644 Tests/XMTPTests/V3Client.swift create mode 100644 Tests/XMTPTests/V3ClientTests.swift diff --git a/Tests/XMTPTests/V3Client.swift b/Tests/XMTPTests/V3Client.swift deleted file mode 100644 index f0dadb4a..00000000 --- a/Tests/XMTPTests/V3Client.swift +++ /dev/null @@ -1,68 +0,0 @@ -// -// File.swift -// -// -// Created by Naomi Plasterer on 9/19/24. -// - -import CryptoKit -import XCTest -@testable import XMTPiOS -import LibXMTP -import XMTPTestHelpers - -@available(iOS 16, *) -class V3ClientTests: XCTestCase { - // Use these fixtures to talk to the local node - struct LocalFixtures { - var alice: PrivateKey! - var bob: PrivateKey! - var fred: PrivateKey! - var aliceClient: Client! - var bobClient: Client! - var fredClient: Client! - } - - func localFixtures() async throws -> LocalFixtures { - let key = try Crypto.secureRandomBytes(count: 32) - 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 - ) - ) - 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 - ) - ) - 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 - ) - ) - - return .init( - alice: alice, - bob: bob, - fred: fred, - aliceClient: aliceClient, - bobClient: bobClient, - fredClient: fredClient - ) - } -} diff --git a/Tests/XMTPTests/V3ClientTests.swift b/Tests/XMTPTests/V3ClientTests.swift new file mode 100644 index 00000000..1cbd5a6e --- /dev/null +++ b/Tests/XMTPTests/V3ClientTests.swift @@ -0,0 +1,135 @@ +// +// File.swift +// +// +// Created by Naomi Plasterer on 9/19/24. +// + +import CryptoKit +import XCTest +@testable import XMTPiOS +import LibXMTP +import XMTPTestHelpers + +@available(iOS 16, *) +class V3ClientTests: XCTestCase { + // Use these fixtures to talk to the local node + struct LocalFixtures { + var alixV2: PrivateKey! + var boV3: PrivateKey! + var caroV2V3: PrivateKey! + var alixV2Client: Client! + var boV3Client: Client! + var caroV2V3Client: Client! + } + + func localFixtures() async throws -> LocalFixtures { + let key = try Crypto.secureRandomBytes(count: 32) + let alixV2 = try PrivateKey.generate() + let alixV2Client = try await Client.create( + account: alixV2, + options: .init( + api: .init(env: .local, isSecure: false) + ) + ) + let boV3 = try PrivateKey.generate() + let boV3Client = try await Client.createOrBuild( + account: boV3, + options: .init( + api: .init(env: .local, isSecure: false), + enableV3: true, + encryptionKey: key + ) + ) + let caroV2V3 = try PrivateKey.generate() + let caroV2V3Client = try await Client.create( + account: caroV2V3, + options: .init( + api: .init(env: .local, isSecure: false), + enableV3: true, + encryptionKey: key + ) + ) + + return .init( + alixV2: alixV2, + boV3: boV3, + caroV2V3: caroV2V3, + alixV2Client: alixV2Client, + boV3Client: boV3Client, + caroV2V3Client: caroV2V3Client + ) + } + + func testsCanCreateGroup() async throws { + let fixtures = try await localFixtures() + let group = try await fixtures.boV3Client.conversations.newGroup(with: [fixtures.caroV2V3.address]) + let members = try group.members.map(\.inboxId).sorted() + XCTAssertEqual([fixtures.caroV2V3Client.inboxID, fixtures.boV3Client.inboxID].sorted(), members) + + await assertThrowsAsyncError( + try await fixtures.boV3Client.conversations.newGroup(with: [fixtures.alixV2.address]) + ) + } + + func testsCanSendMessages() async throws { + let fixtures = try await localFixtures() + let group = try await fixtures.boV3Client.conversations.newGroup(with: [fixtures.caroV2V3.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.caroV2V3Client.conversations.sync() + let sameGroup = try await fixtures.caroV2V3Client.conversations.groups().last + try await sameGroup?.sync() + + let sameGroupMessages = try await sameGroup?.messages() + XCTAssertEqual(sameGroupMessages?.count, 2) + XCTAssertEqual(sameGroupMessages?.first?.body, "gm") + } + + func testCanStreamAllMessagesFromV2andV3Users() async throws { + let fixtures = try await localFixtures() + + let expectation1 = XCTestExpectation(description: "got a conversation") + expectation1.expectedFulfillmentCount = 2 + let convo = try await fixtures.alixV2Client.conversations.newConversation(with: fixtures.caroV2V3.address) + let group = try await fixtures.boV3Client.conversations.newGroup(with: [fixtures.caroV2V3.address]) + try await fixtures.caroV2V3Client.conversations.sync() + Task(priority: .userInitiated) { + for try await _ in await fixtures.caroV2V3Client.conversations.streamAllMessages(includeGroups: true) { + expectation1.fulfill() + } + } + + _ = try await group.send(content: "hi") + _ = try await convo.send(content: "hi") + + await fulfillment(of: [expectation1], timeout: 3) + } + + func testCanStreamGroupsAndConversationsFromV2andV3Users() 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.caroV2V3Client.conversations.streamAll() { + expectation1.fulfill() + } + } + + _ = try await fixtures.boV3Client.conversations.newGroup(with: [fixtures.caroV2V3.address]) + _ = try await fixtures.alixV2Client.conversations.newConversation(with: fixtures.caroV2V3.address) + + await fulfillment(of: [expectation1], timeout: 3) + } +}