Skip to content

Commit

Permalink
beefing up all the tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nplasterer committed Oct 24, 2024
1 parent b8be6d2 commit 585010d
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 22 deletions.
2 changes: 1 addition & 1 deletion Sources/XMTPiOS/Conversations.swift
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ public actor Conversations {
}
}

private func streamConversations() -> AsyncThrowingStream<Conversation, Error> {
public func streamConversations() -> AsyncThrowingStream<Conversation, Error> {
AsyncThrowingStream { continuation in
if (client.hasV2Client) {
continuation.finish(throwing: ConversationError.v2NotSupported("Only supported with V3 only clients use stream instead"))
Expand Down
58 changes: 38 additions & 20 deletions Tests/XMTPTests/GroupTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
}

Expand Down Expand Up @@ -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()
Expand All @@ -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

Expand Down Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand Down Expand Up @@ -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) {
Expand All @@ -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)
}
Expand All @@ -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) {
Expand All @@ -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)
}
Expand All @@ -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() {
Expand All @@ -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)
}
Expand All @@ -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() {
Expand All @@ -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)
}
Expand Down
80 changes: 80 additions & 0 deletions Tests/XMTPTests/V3ClientTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion XMTP.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 585010d

Please sign in to comment.