diff --git a/example/src/tests/createdAtTests.ts b/example/src/tests/createdAtTests.ts index 0502d3665..da66ebaec 100644 --- a/example/src/tests/createdAtTests.ts +++ b/example/src/tests/createdAtTests.ts @@ -1,10 +1,11 @@ -import { Test, assert, delayToPropogate, isIos } from './test-utils' import { - Client, - Conversation, - ConversationContainer, - Group, -} from '../../../src/index' + Test, + assert, + createClients, + delayToPropogate, + isIos, +} from './test-utils' +import { Conversation, ConversationContainer, Group } from '../../../src/index' export const createdAtTests: Test[] = [] @@ -14,57 +15,46 @@ function test(name: string, perform: () => Promise) { test('group createdAt matches listGroups', 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, + // alix creates a group + const alixGroup = await alix.conversations.newGroup([ + bo.address, + caro.address, ]) - // Bob creates a group - const bobGroup = await bobClient.conversations.newGroup([aliceClient.address]) + // bo creates a group + const boGroup = await bo.conversations.newGroup([alix.address]) // Fetch groups using listGroups method - await aliceClient.conversations.syncGroups() - const aliceGroups = await aliceClient.conversations.listGroups() + await alix.conversations.syncGroups() + const alixGroups = await alix.conversations.listGroups() // BUG - List returns in Reverse Chronological order on iOS // and Chronological order on Android const first = isIos() ? 1 : 0 const second = isIos() ? 0 : 1 - assert(aliceGroups.length === 2, 'Alice should have two groups') + assert(alixGroups.length === 2, 'Alix should have two groups') assert( - aliceGroups[first].id === aliceGroup.id, + alixGroups[first].id === alixGroup.id, 'First group returned from listGroups should be the first group created' ) assert( - aliceGroups[first].createdAt === aliceGroup.createdAt, - 'Alice group createdAt should match' + alixGroups[first].createdAt === alixGroup.createdAt, + 'Alix group createdAt should match' ) assert( - aliceGroups[second].id === bobGroup.id, - 'Bob group createdAt should match' + alixGroups[second].id === boGroup.id, + 'Bo group createdAt should match' ) // Below assertion fails on Android if (isIos()) { assert( - aliceGroups[second].createdAt === bobGroup.createdAt, + alixGroups[second].createdAt === boGroup.createdAt, 'Second group returned from listGroups shows ' + - aliceGroups[second].createdAt + + alixGroups[second].createdAt + ' but should be ' + - bobGroup.createdAt + boGroup.createdAt ) } return true @@ -72,66 +62,55 @@ test('group createdAt matches listGroups', async () => { test('group createdAt matches listAll', 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, + // alix creates a group + const alixGroup = await alix.conversations.newGroup([ + bo.address, + caro.address, ]) - // Bob creates a group - const bobGroup = await bobClient.conversations.newGroup([aliceClient.address]) + // bo creates a group + const boGroup = await bo.conversations.newGroup([alix.address]) // Fetch groups using listGroups method - await aliceClient.conversations.syncGroups() - const aliceGroups = await aliceClient.conversations.listAll() + await alix.conversations.syncGroups() + const alixGroups = await alix.conversations.listAll() - assert(aliceGroups.length === 2, 'Alice should have two groups') + assert(alixGroups.length === 2, 'alix should have two groups') // Returns reverse Chronological order on Android and iOS const first = 1 const second = 0 assert( - aliceGroups[first].topic === aliceGroup.id, + alixGroups[first].topic === alixGroup.id, 'First group returned from listGroups shows ' + - (aliceGroups[1] as Group).id + + (alixGroups[1] as Group).id + ' but should be ' + - aliceGroup.id + alixGroup.id ) assert( - aliceGroups[second].topic === bobGroup.id, + alixGroups[second].topic === boGroup.id, 'Second group returned from listGroups shows ' + - (aliceGroups[0] as Group).id + + (alixGroups[0] as Group).id + ' but should be ' + - bobGroup.id + boGroup.id ) assert( - aliceGroups[first].createdAt === aliceGroup.createdAt, - 'Alice group returned from listGroups shows createdAt ' + - aliceGroups[1].createdAt + + alixGroups[first].createdAt === alixGroup.createdAt, + 'alix group returned from listGroups shows createdAt ' + + alixGroups[1].createdAt + ' but should be ' + - aliceGroup.createdAt + alixGroup.createdAt ) // Below assertion fail on Android if (isIos()) { assert( - aliceGroups[second].createdAt === bobGroup.createdAt, - 'Bob group returned from listGroups shows createdAt ' + - aliceGroups[0].createdAt + + alixGroups[second].createdAt === boGroup.createdAt, + 'bo group returned from listGroups shows createdAt ' + + alixGroups[0].createdAt + ' but should be ' + - bobGroup.createdAt + boGroup.createdAt ) } return true @@ -139,25 +118,11 @@ test('group createdAt matches listAll', async () => { test('group createdAt matches streamGroups', async () => { // Create three MLS enabled Clients - const aliceClient = await Client.createRandom({ - env: 'local', - enableAlphaMls: true, - }) - await delayToPropogate() - const bobClient = await Client.createRandom({ - env: 'local', - enableAlphaMls: true, - }) - await delayToPropogate() - const camClient = await Client.createRandom({ - env: 'local', - enableAlphaMls: true, - }) - await delayToPropogate() + const [alix, bo, caro] = await createClients(3) // Start streaming groups const allGroups: Group[] = [] - const cancelStream = await aliceClient.conversations.streamGroups( + const cancelStream = await alix.conversations.streamGroups( async (group: Group) => { allGroups.push(group) } @@ -165,39 +130,39 @@ test('group createdAt matches streamGroups', async () => { await delayToPropogate() - // Alice creates a group - const bobGroup = await bobClient.conversations.newGroup([aliceClient.address]) + // alix creates a group + const boGroup = await bo.conversations.newGroup([alix.address]) await delayToPropogate() - // Bob creates a group - const camGroup = await camClient.conversations.newGroup([aliceClient.address]) + // bo creates a group + const caroGroup = await caro.conversations.newGroup([alix.address]) await delayToPropogate() - assert(allGroups.length === 2, 'Alice should have two groups') + assert(allGroups.length === 2, 'alix should have two groups') // Stream returns in chronological order assert( - allGroups[0].id === bobGroup.id, - 'first ' + allGroups[0].id + ' != ' + bobGroup.id + allGroups[0].id === boGroup.id, + 'first ' + allGroups[0].id + ' != ' + boGroup.id ) assert( - allGroups[1].id === camGroup.id, - 'second ' + allGroups[1].id + ' != ' + camGroup.id + allGroups[1].id === caroGroup.id, + 'second ' + allGroups[1].id + ' != ' + caroGroup.id ) // CreatedAt returned from stream matches createAt from create function // Assertion below fails on Android if (isIos()) { assert( - allGroups[0].createdAt === bobGroup.createdAt, - 'first ' + allGroups[0].createdAt + ' != ' + bobGroup.createdAt + allGroups[0].createdAt === boGroup.createdAt, + 'first ' + allGroups[0].createdAt + ' != ' + boGroup.createdAt ) assert( - allGroups[1].createdAt === camGroup.createdAt, - 'second ' + allGroups[1].createdAt + ' != ' + camGroup.createdAt + allGroups[1].createdAt === caroGroup.createdAt, + 'second ' + allGroups[1].createdAt + ' != ' + caroGroup.createdAt ) } @@ -208,25 +173,11 @@ test('group createdAt matches streamGroups', async () => { test('group createdAt matches streamAll', async () => { // Create three MLS enabled Clients - const aliceClient = await Client.createRandom({ - env: 'local', - enableAlphaMls: true, - }) - await delayToPropogate() - const bobClient = await Client.createRandom({ - env: 'local', - enableAlphaMls: true, - }) - await delayToPropogate() - const camClient = await Client.createRandom({ - env: 'local', - enableAlphaMls: true, - }) - await delayToPropogate() + const [alix, bo, caro] = await createClients(3) // Start streaming groups const allGroups: ConversationContainer[] = [] - const cancelStream = await aliceClient.conversations.streamAll( + const cancelStream = await alix.conversations.streamAll( async (group: ConversationContainer) => { allGroups.push(group) } @@ -234,38 +185,38 @@ test('group createdAt matches streamAll', async () => { await delayToPropogate() - // Alice creates a group - const bobGroup = await bobClient.conversations.newGroup([aliceClient.address]) + // alix creates a group + const boGroup = await bo.conversations.newGroup([alix.address]) await delayToPropogate() - // Bob creates a group - const camGroup = await camClient.conversations.newGroup([aliceClient.address]) + // bo creates a group + const caroGroup = await caro.conversations.newGroup([alix.address]) await delayToPropogate() - assert(allGroups.length === 2, 'Alice should have two groups') + assert(allGroups.length === 2, 'alix should have two groups') // Stream returns in chronological order assert( - allGroups[0].topic === bobGroup.topic, - 'first ' + allGroups[0].topic + ' != ' + bobGroup.topic + allGroups[0].topic === boGroup.topic, + 'first ' + allGroups[0].topic + ' != ' + boGroup.topic ) assert( - allGroups[1].topic === camGroup.topic, - 'second ' + allGroups[1].topic + ' != ' + camGroup.topic + allGroups[1].topic === caroGroup.topic, + 'second ' + allGroups[1].topic + ' != ' + caroGroup.topic ) // CreatedAt returned from stream matches createAt from create function // Assertion below fails on Android if (isIos()) { assert( - allGroups[0].createdAt === bobGroup.createdAt, - 'first ' + allGroups[0].createdAt + ' != ' + bobGroup.createdAt + allGroups[0].createdAt === boGroup.createdAt, + 'first ' + allGroups[0].createdAt + ' != ' + boGroup.createdAt ) assert( - allGroups[1].createdAt === camGroup.createdAt, - 'second ' + allGroups[1].createdAt + ' != ' + camGroup.createdAt + allGroups[1].createdAt === caroGroup.createdAt, + 'second ' + allGroups[1].createdAt + ' != ' + caroGroup.createdAt ) } @@ -276,35 +227,19 @@ test('group createdAt matches streamAll', async () => { test('conversation createdAt matches list', async () => { // Create three MLS enabled Clients - const aliceClient = await Client.createRandom({ - env: 'local', - enableAlphaMls: true, - }) - await delayToPropogate() - const bobClient = await Client.createRandom({ - env: 'local', - enableAlphaMls: true, - }) - await delayToPropogate() - const camClient = await Client.createRandom({ - env: 'local', - enableAlphaMls: true, - }) - await delayToPropogate() + const [alix, bo, caro] = await createClients(3) - // Alice creates a conversation - const aliceConversation = await aliceClient.conversations.newConversation( - bobClient.address - ) + // alix creates a conversation + const alixConversation = await alix.conversations.newConversation(bo.address) - // Bob creates a conversation - const camConversation = await camClient.conversations.newConversation( - aliceClient.address + // bo creates a conversation + const caroConversation = await caro.conversations.newConversation( + alix.address ) // Fetch conversations using list() method - const aliceConversations = await aliceClient.conversations.list() - assert(aliceConversations.length === 2, 'Alice should have two conversations') + const alixConversations = await alix.conversations.list() + assert(alixConversations.length === 2, 'alix should have two conversations') // BUG - List returns in Chronological order on iOS // and reverse Chronological order on Android @@ -312,22 +247,22 @@ test('conversation createdAt matches list', async () => { const second = isIos() ? 1 : 0 assert( - aliceConversations[first].topic === aliceConversation.topic, - aliceConversations[first].topic + ' != ' + aliceConversation.topic + alixConversations[first].topic === alixConversation.topic, + alixConversations[first].topic + ' != ' + alixConversation.topic ) assert( - aliceConversations[second].topic === camConversation.topic, - aliceConversations[second].topic + ' != ' + camConversation.topic + alixConversations[second].topic === caroConversation.topic, + alixConversations[second].topic + ' != ' + caroConversation.topic ) // CreatedAt returned from list matches createAt from create function assert( - aliceConversations[first].createdAt === aliceConversation.createdAt, - aliceConversations[first].createdAt + ' != ' + aliceConversation.createdAt + alixConversations[first].createdAt === alixConversation.createdAt, + alixConversations[first].createdAt + ' != ' + alixConversation.createdAt ) assert( - aliceConversations[second].createdAt === camConversation.createdAt, - aliceConversations[second].createdAt + ' != ' + camConversation.createdAt + alixConversations[second].createdAt === caroConversation.createdAt, + alixConversations[second].createdAt + ' != ' + caroConversation.createdAt ) return true @@ -335,35 +270,19 @@ test('conversation createdAt matches list', async () => { test('conversation createdAt matches listAll', async () => { // Create three MLS enabled Clients - const aliceClient = await Client.createRandom({ - env: 'local', - enableAlphaMls: true, - }) - await delayToPropogate() - const bobClient = await Client.createRandom({ - env: 'local', - enableAlphaMls: true, - }) - await delayToPropogate() - const camClient = await Client.createRandom({ - env: 'local', - enableAlphaMls: true, - }) - await delayToPropogate() + const [alix, bo, caro] = await createClients(3) - // Alice creates a group - const aliceConversation = await aliceClient.conversations.newConversation( - bobClient.address - ) + // alix creates a group + const alixConversation = await alix.conversations.newConversation(bo.address) - // Bob creates a group - const camConversation = await camClient.conversations.newConversation( - aliceClient.address + // bo creates a group + const caroConversation = await caro.conversations.newConversation( + alix.address ) // Fetch conversations using list() method - const aliceConversations = await aliceClient.conversations.listAll() - assert(aliceConversations.length === 2, 'Alice should have two conversations') + const alixConversations = await alix.conversations.listAll() + assert(alixConversations.length === 2, 'alix should have two conversations') // BUG - List returns in Chronological order on iOS // and reverse Chronological order on Android @@ -372,22 +291,22 @@ test('conversation createdAt matches listAll', async () => { // List returns in reverse Chronological order assert( - aliceConversations[first].topic === aliceConversation.topic, - aliceConversations[first].topic + ' != ' + aliceConversation.topic + alixConversations[first].topic === alixConversation.topic, + alixConversations[first].topic + ' != ' + alixConversation.topic ) assert( - aliceConversations[second].topic === camConversation.topic, - aliceConversations[second].topic + ' != ' + aliceConversation.topic + alixConversations[second].topic === caroConversation.topic, + alixConversations[second].topic + ' != ' + alixConversation.topic ) // CreatedAt returned from list matches createAt from create function assert( - aliceConversations[first].createdAt === aliceConversation.createdAt, - aliceConversations[first].createdAt + ' != ' + aliceConversation.createdAt + alixConversations[first].createdAt === alixConversation.createdAt, + alixConversations[first].createdAt + ' != ' + alixConversation.createdAt ) assert( - aliceConversations[second].createdAt === camConversation.createdAt, - aliceConversations[second].createdAt + ' != ' + camConversation.createdAt + alixConversations[second].createdAt === caroConversation.createdAt, + alixConversations[second].createdAt + ' != ' + caroConversation.createdAt ) return true @@ -395,74 +314,58 @@ test('conversation createdAt matches listAll', async () => { test('conversation createdAt matches stream', async () => { // Create three MLS enabled Clients - const aliceClient = await Client.createRandom({ - env: 'local', - enableAlphaMls: true, - }) - await delayToPropogate() - const bobClient = await Client.createRandom({ - env: 'local', - enableAlphaMls: true, - }) - await delayToPropogate() - const camClient = await Client.createRandom({ - env: 'local', - enableAlphaMls: true, - }) - await delayToPropogate() + const [alix, bo, caro] = await createClients(3) // Start streaming conversations const allConversations: Conversation[] = [] - await aliceClient.conversations.stream(async (conversation) => { + await alix.conversations.stream(async (conversation) => { allConversations.push(conversation) }) - // Alice creates a conversation - const aliceConversation = await aliceClient.conversations.newConversation( - bobClient.address - ) + // alix creates a conversation + const alixConversation = await alix.conversations.newConversation(bo.address) await delayToPropogate() - // Bob creates a conversation - const camConversation = await camClient.conversations.newConversation( - aliceClient.address + // bo creates a conversation + const caroConversation = await caro.conversations.newConversation( + alix.address ) await delayToPropogate() - assert(allConversations.length === 2, 'Alice should have two conversations') + assert(allConversations.length === 2, 'alix should have two conversations') // Stream returns in chronological order assert( - allConversations[0].topic === aliceConversation.topic, + allConversations[0].topic === alixConversation.topic, 'list()[1].topic: ' + allConversations[0].topic + ' != ' + - aliceConversation.topic + alixConversation.topic ) assert( - allConversations[1].topic === camConversation.topic, + allConversations[1].topic === caroConversation.topic, 'list()[0].topic: ' + allConversations[1].topic + ' != ' + - camConversation.topic + caroConversation.topic ) // CreatedAt returned from list matches createAt from create function assert( - allConversations[0].createdAt === aliceConversation.createdAt, + allConversations[0].createdAt === alixConversation.createdAt, 'list()[0].createdAt: ' + allConversations[0].createdAt + ' != ' + - aliceConversation.createdAt + alixConversation.createdAt ) assert( - allConversations[1].createdAt === camConversation.createdAt, + allConversations[1].createdAt === caroConversation.createdAt, 'list()[1].createdAt: ' + allConversations[1].createdAt + ' != ' + - camConversation.createdAt + caroConversation.createdAt ) return true @@ -470,76 +373,58 @@ test('conversation createdAt matches stream', async () => { test('conversation createdAt matches streamAll', async () => { // Create three MLS enabled Clients - const aliceClient = await Client.createRandom({ - env: 'local', - enableAlphaMls: true, - }) - await delayToPropogate() - const bobClient = await Client.createRandom({ - env: 'local', - enableAlphaMls: true, - }) - await delayToPropogate() - const camClient = await Client.createRandom({ - env: 'local', - enableAlphaMls: true, - }) - await delayToPropogate() + const [alix, bo, caro] = await createClients(3) // Start streaming conversations const allConversations: ConversationContainer[] = [] - const cancel = await aliceClient.conversations.streamAll( - async (conversation) => { - allConversations.push(conversation) - } - ) + const cancel = await alix.conversations.streamAll(async (conversation) => { + allConversations.push(conversation) + }) - // Alice creates a group - const aliceConversation = await aliceClient.conversations.newConversation( - bobClient.address - ) + // alix creates a group + const alixConversation = await alix.conversations.newConversation(bo.address) await delayToPropogate() - // Bob creates a group - const camConversation = await camClient.conversations.newConversation( - aliceClient.address + // bo creates a group + const caroConversation = await caro.conversations.newConversation( + alix.address ) await delayToPropogate() - assert(allConversations.length === 2, 'Alice should have two conversations') + assert(allConversations.length === 2, 'alix should have two conversations') // Stream returns in chronological order assert( - allConversations[0].topic === aliceConversation.topic, + allConversations[0].topic === alixConversation.topic, 'list()[1].topic: ' + allConversations[0].topic + ' != ' + - aliceConversation.topic + alixConversation.topic ) assert( - allConversations[1].topic === camConversation.topic, + allConversations[1].topic === caroConversation.topic, 'list()[0].topic: ' + allConversations[1].topic + ' != ' + - camConversation.topic + caroConversation.topic ) // CreatedAt returned from list matches createAt from create function assert( - allConversations[0].createdAt === aliceConversation.createdAt, + allConversations[0].createdAt === alixConversation.createdAt, 'list()[0].createdAt: ' + allConversations[0].createdAt + ' != ' + - aliceConversation.createdAt + alixConversation.createdAt ) assert( - allConversations[1].createdAt === camConversation.createdAt, + allConversations[1].createdAt === caroConversation.createdAt, 'list()[1].createdAt: ' + allConversations[1].createdAt + ' != ' + - camConversation.createdAt + caroConversation.createdAt ) cancel() diff --git a/example/src/tests/tests.ts b/example/src/tests/tests.ts index c996de5ad..9bb221b4a 100644 --- a/example/src/tests/tests.ts +++ b/example/src/tests/tests.ts @@ -8,7 +8,7 @@ import { PrivateKeyAccount } from 'viem' import { privateKeyToAccount } from 'viem/accounts' import { DecodedMessage } from 'xmtp-react-native-sdk/lib/DecodedMessage' -import { Test, assert, delayToPropogate } from './test-utils' +import { Test, assert, createClients, delayToPropogate } from './test-utils' import { Query, JSContentCodec, @@ -244,9 +244,9 @@ test('can load 1995 conversations from dev network "2k lens convos" account', as xmtpClient.address === '0x209fAEc92D9B072f3E03d6115002d6652ef563cd', 'Address: ' + xmtpClient.address ) - let start = Date.now() + const start = Date.now() const conversations = await xmtpClient.conversations.list() - let end = Date.now() + const end = Date.now() console.log( `Loaded ${conversations.length} conversations in ${end - start}ms` ) @@ -334,13 +334,20 @@ test('canMessage', async () => { throw new Error('should be able to message v2 client') } + 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 caro = await Client.createRandom({ env: 'local', enableAlphaMls: true, + dbEncryptionKey: keyBytes, }) const chux = await Client.createRandom({ env: 'local', enableAlphaMls: true, + dbEncryptionKey: keyBytes, }) const canMessageV3 = await caro.canGroupMessage([chux.address]) @@ -1320,12 +1327,8 @@ test('instantiate frames client correctly', async () => { // Skipping this test as it's not something supported right now test('can stream all conversation Messages from multiple clients', async () => { - const bo = await Client.createRandom({ env: 'local', enableAlphaMls: true }) - await delayToPropogate() - const alix = await Client.createRandom({ env: 'local', enableAlphaMls: true }) - await delayToPropogate() - const caro = await Client.createRandom({ env: 'local', enableAlphaMls: true }) - await delayToPropogate() + const [alix, bo, caro] = await createClients(3) + if (bo.address === alix.address) { throw Error('Bo and Alix should have different addresses') } @@ -1384,12 +1387,8 @@ test('can stream all conversation Messages from multiple clients', async () => { }) test('can stream all conversation Messages from multiple clients - swapped', async () => { - const bo = await Client.createRandom({ env: 'local', enableAlphaMls: true }) - await delayToPropogate() - const alix = await Client.createRandom({ env: 'local', enableAlphaMls: true }) - await delayToPropogate() - const caro = await Client.createRandom({ env: 'local', enableAlphaMls: true }) - await delayToPropogate() + const [alix, bo, caro] = await createClients(3) + if (bo.address === alix.address) { throw Error('Bo and Alix should have different addresses') } diff --git a/src/lib/XMTPPush.ts b/src/lib/XMTPPush.ts index 94fea5035..58680b7cc 100644 --- a/src/lib/XMTPPush.ts +++ b/src/lib/XMTPPush.ts @@ -1,7 +1,7 @@ import { Client } from './Client' import * as XMTPModule from '../index' -export default class XMTPPush { +export class XMTPPush { client: Client constructor(client: Client) {