Skip to content

Commit

Permalink
add a bunch of tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nplasterer committed Jan 17, 2025
1 parent c72c45a commit 2e855ae
Show file tree
Hide file tree
Showing 5 changed files with 158 additions and 29 deletions.
6 changes: 3 additions & 3 deletions Sources/XMTPiOS/Conversations.swift
Original file line number Diff line number Diff line change
Expand Up @@ -256,14 +256,14 @@ public actor Conversations {
return newDm
}

public func newConversationWithInboxIds(
public func newConversationWithInboxId(
with peerInboxId: String
) async throws -> Conversation {
let dm = try await findOrCreateDmWithInboxIds(with: peerInboxId)
let dm = try await findOrCreateDmWithInboxId(with: peerInboxId)
return Conversation.dm(dm)
}

public func findOrCreateDmWithInboxIds(with peerInboxId: String)
public func findOrCreateDmWithInboxId(with peerInboxId: String)
async throws -> Dm
{
if peerInboxId.lowercased() == client.inboxID.lowercased() {
Expand Down
18 changes: 12 additions & 6 deletions Tests/XMTPTests/ConversationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,20 +64,23 @@ class ConversationTests: XCTestCase {
let convoCount = try await fixtures.boClient.conversations
.list().count
let convoCountConsent = try await fixtures.boClient.conversations
.list(consentState: .allowed).count
.list(consentStates: [.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
.list(consentStates: [.allowed]).count
let convoCountDenied = try await fixtures.boClient.conversations
.list(consentState: .denied).count
.list(consentStates: [.denied]).count
let convoCountCombined = try await fixtures.boClient.conversations
.list(consentStates: [.denied, .allowed]).count

XCTAssertEqual(convoCountAllowed, 1)
XCTAssertEqual(convoCountDenied, 1)
XCTAssertEqual(convoCountCombined, 2)
}

func testCanSyncAllConversationsFiltered() async throws {
Expand All @@ -92,20 +95,23 @@ class ConversationTests: XCTestCase {
let convoCount = try await fixtures.boClient.conversations
.syncAllConversations()
let convoCountConsent = try await fixtures.boClient.conversations
.syncAllConversations(consentState: .allowed)
.syncAllConversations(consentStates: [.allowed])

XCTAssertEqual(convoCount, 3)
XCTAssertEqual(convoCountConsent, 3)

try await group.updateConsentState(state: .denied)

let convoCountAllowed = try await fixtures.boClient.conversations
.syncAllConversations(consentState: .allowed)
.syncAllConversations(consentStates: [.allowed])
let convoCountDenied = try await fixtures.boClient.conversations
.syncAllConversations(consentState: .denied)
.syncAllConversations(consentStates: [.denied])
let convoCountCombined = try await fixtures.boClient.conversations
.syncAllConversations(consentStates: [.denied, .allowed])

XCTAssertEqual(convoCountAllowed, 2)
XCTAssertEqual(convoCountDenied, 2)
XCTAssertEqual(convoCountCombined, 3)
}

func testCanListConversationsOrder() async throws {
Expand Down
49 changes: 35 additions & 14 deletions Tests/XMTPTests/DmTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,33 @@ import XMTPTestHelpers

@available(iOS 16, *)
class DmTests: XCTestCase {

func testCanFindDmByInboxId() async throws {
let fixtures = try await fixtures()

let dm = try await fixtures.boClient.conversations.findOrCreateDm(with: fixtures.caro.walletAddress)
let dm = try await fixtures.boClient.conversations.findOrCreateDm(
with: fixtures.caro.walletAddress)

let caroDm = try fixtures.boClient.findDmByInboxId(
inboxId: fixtures.caroClient.inboxID)
let alixDm = try fixtures.boClient.findDmByInboxId(
inboxId: fixtures.alixClient.inboxID)

let caroDm = try fixtures.boClient.findDmByInboxId(inboxId: fixtures.caroClient.inboxID)
let alixDm = try fixtures.boClient.findDmByInboxId(inboxId: fixtures.alixClient.inboxID)

XCTAssertNil(alixDm)
XCTAssertEqual(caroDm?.id, dm.id)
}

func testCanFindDmByAddress() async throws {
let fixtures = try await fixtures()

let dm = try await fixtures.boClient.conversations.findOrCreateDm(with: fixtures.caro.walletAddress)
let dm = try await fixtures.boClient.conversations.findOrCreateDm(
with: fixtures.caro.walletAddress)

let caroDm = try await fixtures.boClient.findDmByAddress(
address: fixtures.caroClient.address)
let alixDm = try await fixtures.boClient.findDmByAddress(
address: fixtures.alixClient.address)

let caroDm = try await fixtures.boClient.findDmByAddress(address: fixtures.caroClient.address)
let alixDm = try await fixtures.boClient.findDmByAddress(address: fixtures.alixClient.address)

XCTAssertNil(alixDm)
XCTAssertEqual(caroDm?.id, dm.id)
}
Expand All @@ -42,6 +48,18 @@ class DmTests: XCTestCase {
XCTAssertEqual(convo1.id, sameConvo1.id)
}

func testCanCreateADmWithInboxId() async throws {
let fixtures = try await fixtures()

let convo1 = try await fixtures.boClient.conversations
.findOrCreateDmWithInboxId(
with: fixtures.alixClient.inboxID)
try await fixtures.alixClient.conversations.sync()
let sameConvo1 = try await fixtures.alixClient.conversations
.findOrCreateDmWithInboxId(with: fixtures.boClient.inboxID)
XCTAssertEqual(convo1.id, sameConvo1.id)
}

func testCanListDmMembers() async throws {
let fixtures = try await fixtures()

Expand Down Expand Up @@ -87,7 +105,7 @@ class DmTests: XCTestCase {
XCTAssertEqual(dmState, .allowed)
XCTAssertEqual(try dm.consentState(), .allowed)
}

func testCanListDmsFiltered() async throws {
let fixtures = try await fixtures()

Expand All @@ -102,20 +120,23 @@ class DmTests: XCTestCase {
let convoCount = try await fixtures.boClient.conversations
.listDms().count
let convoCountConsent = try await fixtures.boClient.conversations
.listDms(consentState: .allowed).count
.listDms(consentStates: [.allowed]).count

XCTAssertEqual(convoCount, 2)
XCTAssertEqual(convoCountConsent, 2)

try await dm2.updateConsentState(state: .denied)

let convoCountAllowed = try await fixtures.boClient.conversations
.listDms(consentState: .allowed).count
.listDms(consentStates: [.allowed]).count
let convoCountDenied = try await fixtures.boClient.conversations
.listDms(consentState: .denied).count
.listDms(consentStates: [.denied]).count
let convoCountCombined = try await fixtures.boClient.conversations
.listDms(consentStates: [.denied, .allowed]).count

XCTAssertEqual(convoCountAllowed, 1)
XCTAssertEqual(convoCountDenied, 1)
XCTAssertEqual(convoCountCombined, 2)
}

func testCanListConversationsOrder() async throws {
Expand Down Expand Up @@ -184,7 +205,7 @@ class DmTests: XCTestCase {

await fulfillment(of: [expectation1], timeout: 3)
}

func testCanStreamDms() async throws {
let fixtures = try await fixtures()

Expand Down
49 changes: 47 additions & 2 deletions Tests/XMTPTests/GroupPermissionsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,8 @@ class GroupPermissionTests: XCTestCase {
updateGroupNamePolicy: PermissionOption.admin,
updateGroupDescriptionPolicy: PermissionOption.allow,
updateGroupImagePolicy: PermissionOption.admin,
updateGroupPinnedFrameUrlPolicy: PermissionOption.deny
updateGroupPinnedFrameUrlPolicy: PermissionOption.deny,
updateMessageExpirationPolicy: PermissionOption.allow
)
_ = try await fixtures.boClient.conversations
.newGroupCustomPermissions(
Expand Down Expand Up @@ -390,6 +391,49 @@ class GroupPermissionTests: XCTestCase {
alixPermissionSet.updateGroupPinnedFrameUrlPolicy
== PermissionOption.deny)
}

func testCanCreateGroupWithInboxIdCustomPermissions() async throws {
let fixtures = try await fixtures()
let permissionPolicySet = PermissionPolicySet(
addMemberPolicy: PermissionOption.admin,
removeMemberPolicy: PermissionOption.deny,
addAdminPolicy: PermissionOption.admin,
removeAdminPolicy: PermissionOption.superAdmin,
updateGroupNamePolicy: PermissionOption.admin,
updateGroupDescriptionPolicy: PermissionOption.allow,
updateGroupImagePolicy: PermissionOption.admin,
updateGroupPinnedFrameUrlPolicy: PermissionOption.deny,
updateMessageExpirationPolicy: PermissionOption.allow
)
_ = try await fixtures.boClient.conversations
.newGroupCustomPermissionsWithInboxIds(
with: [fixtures.alixClient.inboxID, fixtures.caroClient.inboxID],
permissionPolicySet: permissionPolicySet,
pinnedFrameUrl: "initial url"
)

try await fixtures.alixClient.conversations.sync()
let alixGroup = try await fixtures.alixClient.conversations
.listGroups().first!

let alixPermissionSet = try alixGroup.permissionPolicySet()
XCTAssert(alixPermissionSet.addMemberPolicy == PermissionOption.admin)
XCTAssert(
alixPermissionSet.removeMemberPolicy == PermissionOption.deny)
XCTAssert(alixPermissionSet.addAdminPolicy == PermissionOption.admin)
XCTAssert(
alixPermissionSet.removeAdminPolicy == PermissionOption.superAdmin)
XCTAssert(
alixPermissionSet.updateGroupNamePolicy == PermissionOption.admin)
XCTAssert(
alixPermissionSet.updateGroupDescriptionPolicy
== PermissionOption.allow)
XCTAssert(
alixPermissionSet.updateGroupImagePolicy == PermissionOption.admin)
XCTAssert(
alixPermissionSet.updateGroupPinnedFrameUrlPolicy
== PermissionOption.deny)
}

func testCreateGroupWithInvalidPermissionsFails() async throws {
let fixtures = try await fixtures()
Expand All @@ -402,7 +446,8 @@ class GroupPermissionTests: XCTestCase {
updateGroupNamePolicy: PermissionOption.admin,
updateGroupDescriptionPolicy: PermissionOption.allow,
updateGroupImagePolicy: PermissionOption.admin,
updateGroupPinnedFrameUrlPolicy: PermissionOption.deny
updateGroupPinnedFrameUrlPolicy: PermissionOption.deny,
updateMessageExpirationPolicy: PermissionOption.allow
)
await assertThrowsAsyncError(
try await fixtures.boClient.conversations
Expand Down
65 changes: 61 additions & 4 deletions Tests/XMTPTests/GroupTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,61 @@ class GroupTests: XCTestCase {
try alixGroup.isSuperAdmin(inboxId: fixtures.boClient.inboxID))
XCTAssert(
try !alixGroup.isSuperAdmin(inboxId: fixtures.alixClient.inboxID))
}

func testCanCreateAGroupWithInboxIdDefaultPermissions() async throws {
let fixtures = try await fixtures()
let boGroup = try await fixtures.boClient.conversations
.newGroupWithInboxIds(
with: [fixtures.alixClient.inboxID])
try await fixtures.alixClient.conversations.sync()
let alixGroup = try await fixtures.alixClient.conversations
.listGroups().first!
XCTAssert(!boGroup.id.isEmpty)
XCTAssert(!alixGroup.id.isEmpty)

try await alixGroup.addMembers(addresses: [fixtures.caro.address])
try await boGroup.sync()

var alixMembersCount = try await alixGroup.members.count
var boMembersCount = try await boGroup.members.count
XCTAssertEqual(alixMembersCount, 3)
XCTAssertEqual(boMembersCount, 3)

try await boGroup.addAdmin(inboxId: fixtures.alixClient.inboxID)

try await alixGroup.removeMembers(addresses: [fixtures.caro.address])
try await boGroup.sync()

alixMembersCount = try await alixGroup.members.count
boMembersCount = try await boGroup.members.count
XCTAssertEqual(alixMembersCount, 2)
XCTAssertEqual(boMembersCount, 2)

try await boGroup.addMembers(addresses: [fixtures.caro.address])
try await alixGroup.sync()

try await boGroup.removeAdmin(inboxId: fixtures.alixClient.inboxID)
try await alixGroup.sync()

alixMembersCount = try await alixGroup.members.count
boMembersCount = try await boGroup.members.count
XCTAssertEqual(alixMembersCount, 3)
XCTAssertEqual(boMembersCount, 3)

XCTAssertEqual(
try boGroup.permissionPolicySet().addMemberPolicy, .allow)
XCTAssertEqual(
try alixGroup.permissionPolicySet().addMemberPolicy, .allow)

XCTAssert(
try boGroup.isSuperAdmin(inboxId: fixtures.boClient.inboxID))
XCTAssert(
try !boGroup.isSuperAdmin(inboxId: fixtures.alixClient.inboxID))
XCTAssert(
try alixGroup.isSuperAdmin(inboxId: fixtures.boClient.inboxID))
XCTAssert(
try !alixGroup.isSuperAdmin(inboxId: fixtures.alixClient.inboxID))
}

func testCanCreateAGroupWithAdminPermissions() async throws {
Expand Down Expand Up @@ -175,7 +229,7 @@ class GroupTests: XCTestCase {
XCTAssertEqual(1, alixGroupCount)
XCTAssertEqual(1, boGroupCount)
}

func testCanListGroupsFiltered() async throws {
let fixtures = try await fixtures()

Expand All @@ -191,20 +245,23 @@ class GroupTests: XCTestCase {
let convoCount = try await fixtures.boClient.conversations
.listGroups().count
let convoCountConsent = try await fixtures.boClient.conversations
.listGroups(consentState: .allowed).count
.listGroups(consentStates: [.allowed]).count

XCTAssertEqual(convoCount, 2)
XCTAssertEqual(convoCountConsent, 2)

try await group.updateConsentState(state: .denied)

let convoCountAllowed = try await fixtures.boClient.conversations
.listGroups(consentState: .allowed).count
.listGroups(consentStates: [.allowed]).count
let convoCountDenied = try await fixtures.boClient.conversations
.listGroups(consentState: .denied).count
.listGroups(consentStates: [.denied]).count
let convoCountCombined = try await fixtures.boClient.conversations
.listGroups(consentStates: [.denied, .allowed]).count

XCTAssertEqual(convoCountAllowed, 1)
XCTAssertEqual(convoCountDenied, 1)
XCTAssertEqual(convoCountCombined, 2)
}

func testCanListGroupsOrder() async throws {
Expand Down

0 comments on commit 2e855ae

Please sign in to comment.