Skip to content

Commit

Permalink
fix: add tests for filtering and consent state
Browse files Browse the repository at this point in the history
  • Loading branch information
nplasterer committed Nov 15, 2024
1 parent fbd2da7 commit b83f934
Show file tree
Hide file tree
Showing 3 changed files with 239 additions and 4 deletions.
119 changes: 117 additions & 2 deletions example/src/tests/conversationTests.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { Test, assert, createClients, delayToPropogate } from './test-utils'
import RNFS from 'react-native-fs'

import { Test, assert, createClients, delayToPropogate } from './test-utils'
import {
Client,
ConsentListEntry,
Conversation,
ConversationId,
ConversationVersion,
Expand Down Expand Up @@ -99,6 +101,63 @@ test('can find a dm by address', async () => {
return true
})

test('can filter conversations by consent', async () => {
const [alixClient, boClient, caroClient] = await createClients(3)

const boGroup1 = await boClient.conversations.newGroup([alixClient.address])
const otherGroup = await alixClient.conversations.newGroup([boClient.address])
const boDm1 = await boClient.conversations.findOrCreateDm(alixClient.address)
await caroClient.conversations.findOrCreateDm(boClient.address)
await boClient.conversations.sync
const boDm2 = await boClient.conversations.findDmByInboxId(caroClient.inboxId)
const boGroup2 = await boClient.conversations.findGroup(otherGroup.id)

const boConvos = await boClient.conversations.list()
const boConvosFilteredAllowed = await boClient.conversations.list(
{},
undefined,
undefined,
'allowed'
)
const boConvosFilteredUnknown = await boClient.conversations.list(
{},
undefined,
undefined,
'unknown'
)

assert(
boConvos.length === 4,
`Conversation length should be 4 but was ${boConvos.length}`
)

assert(
boConvosFilteredAllowed
.map((conversation: any) => conversation.id)
.toString() === [boGroup1.id, boDm1.id].toString(),
`Conversation allowed should be ${[
boGroup1.id,
boDm1.id,
].toString()} but was ${boConvosFilteredAllowed
.map((convo: any) => convo.id)
.toString()}`
)

assert(
boConvosFilteredUnknown
.map((conversation: any) => conversation.id)
.toString() === [boGroup2?.id, boDm2?.id].toString(),
`Conversation unknown filter should be ${[
boGroup2?.id,
boDm2?.id,
].toString()} but was ${boConvosFilteredUnknown
.map((convo: any) => convo.id)
.toString()}`
)

return true
})

test('can list conversations with params', async () => {
const [alixClient, boClient, caroClient] = await createClients(3)

Expand Down Expand Up @@ -494,23 +553,79 @@ test('can streamAllMessages from multiple clients - swapped', async () => {
})

test('can sync consent', async () => {
const [bo] = await createClients(1)
const keyBytes = new Uint8Array([
233, 120, 198, 96, 154, 65, 132, 17, 132, 96, 250, 40, 103, 35, 125, 64,
166, 83, 208, 224, 254, 44, 205, 227, 175, 49, 234, 129, 74, 252, 135, 145,
])
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const dbDirPath = `${RNFS.DocumentDirectoryPath}/xmtp_db`
const dbDirPath2 = `${RNFS.DocumentDirectoryPath}/xmtp_db2`
const directoryExists = await RNFS.exists(dbDirPath)
if (!directoryExists) {
await RNFS.mkdir(dbDirPath)
}
const directoryExists2 = await RNFS.exists(dbDirPath2)
if (!directoryExists2) {
await RNFS.mkdir(dbDirPath2)
}
const alix = await Client.createRandom({
env: 'local',
appVersion: 'Testing/0.0.0',
dbEncryptionKey: keyBytes,
dbDirectory: dbDirPath,
})


// Create DM conversation
const dm = await alix.conversations.findOrCreateDm(bo.address)
await dm.updateConsent('denied')
const consentState = await dm.consentState()
assert(consentState === 'denied', `Expected 'denied', got ${consentState}`)

await bo.conversations.sync()
const boDm = await bo.conversations.findConversation(dm.id)

const alix2 = await Client.createRandom({
env: 'local',
appVersion: 'Testing/0.0.0',
dbEncryptionKey: keyBytes,
dbDirectory: dbDirPath2,
})

const state = await alix2.inboxState(true)
assert(
state.installations.length === 2,
`Expected 2 installations, got ${state.installations.length}`
)

// Sync conversations
await bo.conversations.sync()
if (boDm) await boDm.sync()
await alix.conversations.sync()
await alix2.conversations.sync()
await alix2.preferences.syncConsent()
await alix.conversations.syncAllConversations()

await delayToPropogate(2000)
await alix2.conversations.syncAllConversations()
await delayToPropogate(2000)

const dm2 = await alix2.conversations.findConversation(dm.id)
const consentState2 = await dm2?.consentState()
assert(consentState2 === 'denied', `Expected 'denied', got ${consentState2}`)

await alix2.preferences.setConsentState(
new ConsentListEntry(dm2!.id, 'conversation_id', 'allowed')
)

const convoState = await alix2.preferences.conversationConsentState(dm2!.id)
assert(convoState === 'allowed', `Expected 'allowed', got ${convoState}`)

const updatedConsentState = await dm2?.consentState()
assert(
updatedConsentState === 'allowed',
`Expected 'allowed', got ${updatedConsentState}`
)

return true
})
69 changes: 67 additions & 2 deletions example/src/tests/dmTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,61 @@ function test(name: string, perform: () => Promise<boolean>) {
})
}

test('can filter dms by consent', async () => {
const [alixClient, boClient, caroClient] = await createClients(3)

await boClient.conversations.newGroup([alixClient.address])
const otherGroup = await alixClient.conversations.newGroup([boClient.address])
const boDm1 = await boClient.conversations.findOrCreateDm(alixClient.address)
await caroClient.conversations.findOrCreateDm(boClient.address)
await boClient.conversations.sync
const boDm2 = await boClient.conversations.findDmByInboxId(caroClient.inboxId)
await boClient.conversations.findGroup(otherGroup.id)

const boConvos = await boClient.conversations.listDms()
const boConvosFilteredAllowed = await boClient.conversations.listDms(
{},
undefined,
undefined,
'allowed'
)
const boConvosFilteredUnknown = await boClient.conversations.listDms(
{},
undefined,
undefined,
'unknown'
)

assert(
boConvos.length === 2,
`Conversation length should be 2 but was ${boConvos.length}`
)

assert(
boConvosFilteredAllowed
.map((conversation: any) => conversation.id)
.toString() === [boDm1.id].toString(),
`Conversation allowed should be ${[
boDm1.id,
].toString()} but was ${boConvosFilteredAllowed
.map((convo: any) => convo.id)
.toString()}`
)

assert(
boConvosFilteredUnknown
.map((conversation: any) => conversation.id)
.toString() === [boDm2?.id].toString(),
`Conversation unknown filter should be ${[
boDm2?.id,
].toString()} but was ${boConvosFilteredUnknown
.map((convo: any) => convo.id)
.toString()}`
)

return true
})

test('can list dms with params', async () => {
const [alixClient, boClient, caroClient] = await createClients(3)

Expand Down Expand Up @@ -37,14 +92,24 @@ test('can list dms with params', async () => {
boConvosOrderCreated
.map((conversation: any) => conversation.id)
.toString() === [boDm1.id, boDm2.id].toString(),
`Conversation created at order should be ${[boDm1.id, boDm2.id].toString()} but was ${boConvosOrderCreated.map((convo: any) => convo.id).toString()}`
`Conversation created at order should be ${[
boDm1.id,
boDm2.id,
].toString()} but was ${boConvosOrderCreated
.map((convo: any) => convo.id)
.toString()}`
)

assert(
boConvosOrderLastMessage
.map((conversation: any) => conversation.id)
.toString() === [boDm1.id, boDm2.id].toString(),
`Conversation last message order should be ${[boDm1.id, boDm2.id].toString()} but was ${boConvosOrderLastMessage.map((convo: any) => convo.id).toString()}`
`Conversation last message order should be ${[
boDm1.id,
boDm2.id,
].toString()} but was ${boConvosOrderLastMessage
.map((convo: any) => convo.id)
.toString()}`
)

const messages = await boConvosOrderLastMessage[0].messages()
Expand Down
55 changes: 55 additions & 0 deletions example/src/tests/groupTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -671,6 +671,61 @@ test('can stream groups', async () => {
return true
})

test('can filter groups by consent', async () => {
const [alixClient, boClient, caroClient] = await createClients(3)

const boGroup1 = await boClient.conversations.newGroup([alixClient.address])
const otherGroup = await alixClient.conversations.newGroup([boClient.address])
await boClient.conversations.findOrCreateDm(alixClient.address)
await caroClient.conversations.findOrCreateDm(boClient.address)
await boClient.conversations.sync
await boClient.conversations.findDmByInboxId(caroClient.inboxId)
const boGroup2 = await boClient.conversations.findGroup(otherGroup.id)

const boConvos = await boClient.conversations.listGroups()
const boConvosFilteredAllowed = await boClient.conversations.listGroups(
{},
undefined,
undefined,
'allowed'
)
const boConvosFilteredUnknown = await boClient.conversations.listGroups(
{},
undefined,
undefined,
'unknown'
)

assert(
boConvos.length === 2,
`Conversation length should be 2 but was ${boConvos.length}`
)

assert(
boConvosFilteredAllowed
.map((conversation: any) => conversation.id)
.toString() === [boGroup1.id].toString(),
`Conversation allowed should be ${[
boGroup1.id,
].toString()} but was ${boConvosFilteredAllowed
.map((convo: any) => convo.id)
.toString()}`
)

assert(
boConvosFilteredUnknown
.map((conversation: any) => conversation.id)
.toString() === [boGroup2?.id].toString(),
`Conversation unknown filter should be ${[
boGroup2?.id,
].toString()} but was ${boConvosFilteredUnknown
.map((convo: any) => convo.id)
.toString()}`
)

return true
})

test('can list groups with params', async () => {
const [alixClient, boClient] = await createClients(2)

Expand Down

0 comments on commit b83f934

Please sign in to comment.