Skip to content

Commit

Permalink
get the tests going
Browse files Browse the repository at this point in the history
  • Loading branch information
nplasterer committed Oct 26, 2024
1 parent 8aa5552 commit c97134e
Show file tree
Hide file tree
Showing 6 changed files with 171 additions and 65 deletions.
64 changes: 64 additions & 0 deletions example/src/tests/dmTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
Test,
assert,
createClients,

Check warning on line 8 in example/src/tests/dmTests.ts

View workflow job for this annotation

GitHub Actions / lint

'createClients' is defined but never used
createV3Clients,
delayToPropogate,

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

View workflow job for this annotation

GitHub Actions / lint

'delayToPropogate' is defined but never used
} from './test-utils'
import {
Expand All @@ -22,3 +23,66 @@ let counter = 1
function test(name: string, perform: () => Promise<boolean>) {
dmTests.push({ name: String(counter++) + '. ' + name, run: perform })
}

test('can find a conversations by id', async () => {
const [alixClient, boClient] = await createV3Clients(2)
const alixGroup = await alixClient.conversations.newGroup([boClient.address])
const alixDm = await alixClient.conversations.findOrCreateDm(boClient.address)

await boClient.conversations.syncConversations()
const boGroup = await boClient.conversations.findConversation(alixGroup.id)
const boDm = await boClient.conversations.findConversation(alixDm.id)

assert(
boGroup?.id === alixGroup.id,
`bo group id ${boGroup?.id} does not match alix group id ${alixGroup.id}`
)

assert(
boDm?.id === alixDm.id,
`bo dm id ${boDm?.id} does not match alix dm id ${alixDm.id}`
)

return true
})

test('can find a conversation by topic', async () => {
const [alixClient, boClient] = await createV3Clients(2)
const alixGroup = await alixClient.conversations.newGroup([boClient.address])
const alixDm = await alixClient.conversations.findOrCreateDm(boClient.address)

await boClient.conversations.syncConversations()
const boGroup = await boClient.conversations.findConversationByTopic(
alixGroup.topic
)
const boDm = await boClient.conversations.findConversationByTopic(
alixDm.topic
)

assert(
boGroup?.id === alixGroup.id,
`bo group topic ${boGroup?.id} does not match alix group topic ${alixGroup.id}`
)

assert(
boDm?.id === alixDm.id,
`bo dm topic ${boDm?.id} does not match alix dm topic ${alixDm.id}`
)

return true
})

test('can find a dm by address', async () => {
const [alixClient, boClient] = await createV3Clients(2)
const alixDm = await alixClient.conversations.findOrCreateDm(boClient.address)

await boClient.conversations.syncConversations()
const boDm = await boClient.conversations.findDm(alixClient.address)

assert(
boDm?.id === alixDm.id,
`bo dm id ${boDm?.id} does not match alix dm id ${alixDm.id}`
)

return true
})
83 changes: 78 additions & 5 deletions example/src/tests/groupPerformanceTests.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
/* eslint-disable @typescript-eslint/no-extra-non-null-assertion */
import { Client, Group } from 'xmtp-react-native-sdk'
import { Client, Conversation, Dm, Group } from 'xmtp-react-native-sdk'
import { DefaultContentTypes } from 'xmtp-react-native-sdk/lib/types/DefaultContentType'

import { Test, assert, createClients } from './test-utils'
import { Test, assert, createClients, createV3Clients } from './test-utils'

export const groupPerformanceTests: Test[] = []
let counter = 1
Expand Down Expand Up @@ -34,29 +35,101 @@ async function createGroups(
return groups
}

async function createDms(
client: Client,
peers: Client[],
numMessages: number
): Promise<Dm[]> {
const dms = []
for (let i = 0; i < peers.length; i++) {
const dm = await peers[i].conversations.findOrCreateDm(client.address)
dms.push(dm)
for (let i = 0; i < numMessages; i++) {
await dm.send({ text: `Alix message ${i}` })
}
}
return dms
}

async function createV2Convos(
client: Client,
peers: Client[],
numMessages: number
): Promise<Conversation<DefaultContentTypes>[]> {
const convos = []
for (let i = 0; i < peers.length; i++) {
const convo = await peers[i].conversations.newConversation(client.address)
convos.push(convo)
for (let i = 0; i < numMessages; i++) {
await convo.send({ text: `Alix message ${i}` })
}
}
return convos
}

let alixClient: Client
let boClient: Client
let davonV3Client: Client
let initialPeers: Client[]
let initialGroups: Group[]
// let initialDms: Dm[]
// let initialV2Convos: Conversation<DefaultContentTypes>[]

async function beforeAll(
groupSize: number = 1,
groupMessages: number = 1,
peersSize: number = 1
messages: number = 1,
peersSize: number = 1,
includeDms: boolean = false,
includeV2Convos: boolean = false
) {
;[alixClient] = await createClients(1)
;[davonV3Client] = await createV3Clients(1)

initialPeers = await createClients(peersSize)
const initialV3Peers = await createV3Clients(peersSize)
boClient = initialPeers[0]

initialGroups = await createGroups(
alixClient,
initialPeers,
groupSize,
groupMessages
messages
)

if (includeDms) {
await createDms(davonV3Client, initialV3Peers, messages)
}

if (includeV2Convos) {
await createV2Convos(alixClient, initialPeers, messages)
}
}

test('test compare V2 and V3 dms', async () => {
await beforeAll(0, 0, 50, true, true)
let start = Date.now()
let v2Convos = await alixClient.conversations.list()
let end = Date.now()
console.log(`Alix loaded ${v2Convos.length} v2Convos in ${end - start}ms`)

start = Date.now()
v2Convos = await alixClient.conversations.list()
end = Date.now()
console.log(`Alix 2nd loaded ${v2Convos.length} v2Convos in ${end - start}ms`)

start = Date.now()
await davonV3Client.conversations.syncConversations()
end = Date.now()
console.log(`Davon synced ${v2Convos.length} Dms in ${end - start}ms`)

start = Date.now()
const dms = await davonV3Client.conversations.listConversations()
end = Date.now()
console.log(`Davon loaded ${dms.length} Dms in ${end - start}ms`)

return true
})

test('testing large group listings with ordering', async () => {
await beforeAll(1000, 10, 10)

Expand Down
65 changes: 6 additions & 59 deletions example/src/tests/groupTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ import {
Group,
ConversationContainer,
ConversationVersion,
MessageDeliveryStatus,
GroupUpdatedContent,
GroupUpdatedCodec,
} from '../../../src/index'
import { getSigner } from '../../../src/lib/Signer'

export const groupTests: Test[] = []
let counter = 1
Expand Down Expand Up @@ -427,15 +427,6 @@ test('group message delivery status', async () => {
`the messages length should be 2 but was ${alixMessages.length}`
)

const alixMessagesFiltered: DecodedMessage[] = await alixGroup.messages({
deliveryStatus: MessageDeliveryStatus.PUBLISHED,
})

assert(
alixMessagesFiltered.length === 2,
`the messages length should be 2 but was ${alixMessagesFiltered.length}`
)

await alixGroup.sync()
const alixMessages2: DecodedMessage[] = await alixGroup.messages()

Expand Down Expand Up @@ -686,56 +677,12 @@ test('unpublished messages handling', async () => {
throw new Error(`Message count should be 1, but it is ${messageCount}`)
}

// Verify the count of published and unpublished messages
let messageCountPublished = (
await alixGroup.messages({
deliveryStatus: MessageDeliveryStatus.PUBLISHED,
})
).length
let messageCountUnpublished = (
await alixGroup.messages({
deliveryStatus: MessageDeliveryStatus.UNPUBLISHED,
})
).length
if (messageCountPublished !== 0) {
throw new Error(
`Published message count should be 0, but it is ${messageCountPublished}`
)
}
if (messageCountUnpublished !== 1) {
throw new Error(
`Unpublished message count should be 1, but it is ${messageCountUnpublished}`
)
}

// Publish the prepared message
await alixGroup.publishPreparedMessages()

// Sync the group after publishing the message
await alixGroup.sync()

// Verify the message counts again
messageCountPublished = (
await alixGroup.messages({
deliveryStatus: MessageDeliveryStatus.PUBLISHED,
})
).length
messageCountUnpublished = (
await alixGroup.messages({
deliveryStatus: MessageDeliveryStatus.UNPUBLISHED,
})
).length
messageCount = (await alixGroup.messages()).length
if (messageCountPublished !== 1) {
throw new Error(
`Published message count should be 1, but it is ${messageCountPublished}`
)
}
if (messageCountUnpublished !== 0) {
throw new Error(
`Unpublished message count should be 0, but it is ${messageCountUnpublished}`
)
}
if (messageCount !== 1) {
throw new Error(`Message count should be 1, but it is ${messageCount}`)
}
Expand Down Expand Up @@ -1332,7 +1279,7 @@ test('can stream group messages', async () => {

// Record message stream for this group
const groupMessages: DecodedMessage[] = []
const cancelGroupMessageStream = await alixGroup.streamGroupMessages(
const cancelGroupMessageStream = await alixGroup.streamMessages(
async (message) => {
groupMessages.push(message)
}
Expand Down Expand Up @@ -1778,10 +1725,10 @@ test('can stream all group Messages from multiple clients', async () => {
const alixGroup = await caro.conversations.newGroup([alix.address])
const boGroup = await caro.conversations.newGroup([bo.address])

await alixGroup.streamGroupMessages(async (message) => {
await alixGroup.streamMessages(async (message) => {
allAlixMessages.push(message)
})
await boGroup.streamGroupMessages(async (message) => {
await boGroup.streamMessages(async (message) => {
allBoMessages.push(message)
})

Expand Down Expand Up @@ -1825,10 +1772,10 @@ test('can stream all group Messages from multiple clients - swapped', async () =
const alixGroup = await caro.conversations.newGroup([alix.address])
const boGroup = await caro.conversations.newGroup([bo.address])

await boGroup.streamGroupMessages(async (message) => {
await boGroup.streamMessages(async (message) => {
allBoMessages.push(message)
})
await alixGroup.streamGroupMessages(async (message) => {
await alixGroup.streamMessages(async (message) => {
allAlixMessages.push(message)
})

Expand Down
19 changes: 19 additions & 0 deletions example/src/tests/test-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,25 @@ export async function createClients(numClients: number): Promise<Client[]> {
return clients
}

export async function createV3Clients(numClients: number): Promise<Client[]> {
const clients = []
for (let i = 0; i < numClients; i++) {
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 client = await Client.createRandomV3({
env: 'local',
enableV3: true,
dbEncryptionKey: keyBytes,
})
client.register(new GroupUpdatedCodec())
clients.push(client)
}
return clients
}

export async function createV3TestingClients(): Promise<Client[]> {
const clients = []
const keyBytes = new Uint8Array([
Expand Down
3 changes: 3 additions & 0 deletions example/src/tests/tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,9 @@ export function convertPrivateKeyAccountToSigner(
privateKeyAccount.signMessage({
message: typeof message === 'string' ? message : { raw: message },
}),
getChainId: () => undefined,
getBlockNumber: () => undefined,
walletType: () => 'EOA',
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/lib/Conversations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ export default class Conversations<
* @returns {Promise<ConversationContainer[]>} A Promise that resolves to an array of ConversationContainer objects.
*/
async listConversations(): Promise<ConversationContainer<ContentTypes>[]> {
return await XMTPModule.listConversations(this.client)
return await XMTPModule.listV3Conversations(this.client)
}

/**
Expand Down

0 comments on commit c97134e

Please sign in to comment.