Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consent stream and sync tests #549

Merged
merged 1 commit into from
Nov 26, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
157 changes: 157 additions & 0 deletions example/src/tests/conversationTests.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import RNFS from 'react-native-fs'

Check warning on line 1 in example/src/tests/conversationTests.ts

View workflow job for this annotation

GitHub Actions / lint

There should be at least one empty line between import groups
import { Test, assert, createClients, delayToPropogate } from './test-utils'
import {

Check warning on line 3 in example/src/tests/conversationTests.ts

View workflow job for this annotation

GitHub Actions / lint

There should be at least one empty line between import groups
Client,
ConsentRecord,
Conversation,
ConversationId,
ConversationVersion,
} from '../../../src/index'
import { Wallet } from 'ethers'

Check warning on line 10 in example/src/tests/conversationTests.ts

View workflow job for this annotation

GitHub Actions / lint

`ethers` import should occur before import of `react-native-fs`

export const conversationTests: Test[] = []
let counter = 1
Expand Down Expand Up @@ -547,3 +551,156 @@

return true
})

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 alixWallet = Wallet.createRandom()

const alix = await Client.create(alixWallet, {
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.create(alixWallet, {
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 ConsentRecord(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
})

test('can stream 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,
])
const dbDirPath = `${RNFS.DocumentDirectoryPath}/xmtp_db`
const dbDirPath2 = `${RNFS.DocumentDirectoryPath}/xmtp_db2`

// Ensure the directories exist
if (!(await RNFS.exists(dbDirPath))) {
await RNFS.mkdir(dbDirPath)
}
if (!(await RNFS.exists(dbDirPath2))) {
await RNFS.mkdir(dbDirPath2)
}

const alixWallet = Wallet.createRandom()

const alix = await Client.create(alixWallet, {
env: 'local',
appVersion: 'Testing/0.0.0',
dbEncryptionKey: keyBytes,
dbDirectory: dbDirPath,
})

const alixGroup = await alix.conversations.newGroup([bo.address])

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

await alixGroup.send('Hello')
await alix.conversations.sync()
await alix2.conversations.sync()
await alix.conversations.syncAllConversations()
await alix2.conversations.syncAllConversations()

const alix2Group = await alix2.conversations.findConversation(alixGroup.id)

const consent = []
await alix.preferences.streamConsent(async (entry: ConsentRecord) => {
consent.push(entry)
})

await delayToPropogate()

await alix2Group!.updateConsent('denied')
const dm = await alix2.conversations.newConversation(bo.address)
await dm!.updateConsent('denied')

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

assert(
consent.length === 3,
`Expected 3 consent records, got ${consent.length}`
)
const updatedConsentState = await alixGroup.consentState()
assert(
updatedConsentState === 'denied',
`Expected 'denied', got ${updatedConsentState}`
)

alix.preferences.cancelStreamConsent()

return true
})
Loading