Skip to content

Commit

Permalink
Added test for streaming group msg from self
Browse files Browse the repository at this point in the history
  • Loading branch information
cameronvoell committed Feb 28, 2024
1 parent 30da179 commit 08870c1
Showing 1 changed file with 57 additions and 21 deletions.
78 changes: 57 additions & 21 deletions example/src/tests/groupTests.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import RNFS from 'react-native-fs'
import { DecodedMessage } from 'xmtp-react-native-sdk/lib/DecodedMessage'

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

Check warning on line 4 in example/src/tests/groupTests.ts

View workflow job for this annotation

GitHub Actions / lint

Replace `·Test,·assert,·createClients,·delayToPropogate,·isIos·` with `⏎··Test,⏎··assert,⏎··createClients,⏎··delayToPropogate,⏎··isIos,⏎`
import {
Client,
Conversation,
Expand Down Expand Up @@ -686,44 +686,80 @@ test('canMessage', async () => {

test('can stream group messages', async () => {
// Create three MLS enabled Clients
const aliceClient = await Client.createRandom({
env: 'local',
enableAlphaMls: true,
})
const bobClient = await Client.createRandom({
env: 'local',
enableAlphaMls: true,
})
const camClient = await Client.createRandom({
env: 'local',
enableAlphaMls: true,
})
const [alix, bo, caro] = await createClients(3)

// Alice creates a group
const aliceGroup = await aliceClient.conversations.newGroup([
bobClient.address,
camClient.address,
const alixGroup = await alix.conversations.newGroup([
bo.address,
caro.address,
])

// Record message stream for this group
const groupMessages: DecodedMessage[] = []
const cancelGroupMessageStream = await aliceGroup.streamGroupMessages(
const cancelGroupMessageStream = await alixGroup.streamGroupMessages(
async (message) => {
groupMessages.push(message)
}
)

// Bob's num groups == 1
const bobGroup = (await bobClient.conversations.listGroups())[0]

const boGroup = (await bo.conversations.listGroups())[0]
for (let i = 0; i < 5; i++) {
await bobGroup.send({ text: `Message ${i}` })
await boGroup.send({ text: `Message ${i}` })
await delayToPropogate()
}

if (groupMessages.length !== 5) {
throw Error('Unexpected group messages count ' + groupMessages.length)
}
for (let i = 0; i < 5; i++) {
if (groupMessages[i].content() !== `Message ${i}`) {
throw Error(
'Unexpected group message content ' + groupMessages[i].content()
)
}
}

cancelGroupMessageStream()
for (let i = 0; i < 5; i++) {
await boGroup.send({ text: `Message ${i}` })
}

if (groupMessages.length !== 5) {
throw Error('Unexpected convo messages count ' + groupMessages.length)
}

return true
})

test('can stream group messages from self', async () => {
// Create three MLS enabled Clients
const [alix, bo, caro] = await createClients(3)

// Alice creates a group
const alixGroup = await alix.conversations.newGroup([
bo.address,
caro.address,
])

// Record message stream for this group
const groupMessages: DecodedMessage[] = []
const cancelGroupMessageStream = await alixGroup.streamGroupMessages(
async (message) => {
groupMessages.push(message)
}
)

for (let i = 0; i < 5; i++) {
await alixGroup.send({ text: `Message ${i}` })
await delayToPropogate(200)
}

await delayToPropogate(1000)

if (groupMessages.length !== 5) {
throw Error('Unexpected group messages count ' + groupMessages.length)
}
for (let i = 0; i < 5; i++) {
if (groupMessages[i].content() !== `Message ${i}`) {
throw Error(
Expand All @@ -734,7 +770,7 @@ test('can stream group messages', async () => {

cancelGroupMessageStream()
for (let i = 0; i < 5; i++) {
await bobGroup.send({ text: `Message ${i}` })
await alixGroup.send({ text: `Message ${i}` })
}

if (groupMessages.length !== 5) {
Expand Down

0 comments on commit 08870c1

Please sign in to comment.