Skip to content

Commit

Permalink
test: replace assert with expect
Browse files Browse the repository at this point in the history
  • Loading branch information
rygine committed Feb 26, 2024
1 parent 022ab75 commit 3a27fa4
Show file tree
Hide file tree
Showing 15 changed files with 142 additions and 150 deletions.
5 changes: 2 additions & 3 deletions test/BackupClient.test.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
import assert from 'assert'
import { BackupType } from '../src/message-backup/BackupClient'
import { newLocalHostClient, newDevClient } from './helpers'

describe('Backup configuration', () => {
it('Uses XMTP backup for localhost', async function () {
const c = await newLocalHostClient()
assert.equal(c.backupType, BackupType.xmtpTopicStore)
expect(c.backupType).toEqual(BackupType.xmtpTopicStore)
})
if (process.env.CI || process.env.TESTNET) {
it('Uses no backup for dev', async function () {
const c = await newDevClient()
assert.equal(c.backupType, BackupType.none)
expect(c.backupType).toEqual(BackupType.none)
})
}
})
19 changes: 9 additions & 10 deletions test/Client.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import assert from 'assert'
import {
newWallet,
newLocalHostClient,
Expand Down Expand Up @@ -29,7 +28,7 @@ import { createWalletClient, http } from 'viem'
import { privateKeyToAccount } from 'viem/accounts'
import { mainnet } from 'viem/chains'
import { generatePrivateKey } from 'viem/accounts'
import { vi } from 'vitest'
import { vi, assert } from 'vitest'

type TestCase = {
name: string
Expand Down Expand Up @@ -83,9 +82,9 @@ describe('Client', () => {

it('user contacts published', async () => {
const alicePublic = await alice.getUserContact(alice.address)
assert.deepEqual(alice.publicKeyBundle, alicePublic)
expect(alicePublic).toEqual(alice.publicKeyBundle)
const bobPublic = await bob.getUserContact(bob.address)
assert.deepEqual(bob.publicKeyBundle, bobPublic)
expect(bobPublic).toEqual(bob.publicKeyBundle)
})

it('user contacts are filtered to valid contacts', async () => {
Expand All @@ -98,18 +97,18 @@ describe('Client', () => {
},
])
const alicePublic = await alice.getUserContact(alice.address)
assert.deepEqual(alice.publicKeyBundle, alicePublic)
expect(alicePublic).toEqual(alice.publicKeyBundle)
})

it('Check address can be sent to', async () => {
const can_mesg_a = await alice.canMessage('NOT AN ADDRESS')
assert.equal(can_mesg_a, false)
expect(can_mesg_a).toBe(false)

const can_mesg_b = await alice.canMessage(bob.address)
assert.equal(can_mesg_b, true)
expect(can_mesg_b).toBe(true)

const lower = await alice.canMessage(bob.address.toLowerCase())
assert.equal(lower, true)
expect(lower).toBe(true)
})
})
})
Expand Down Expand Up @@ -200,7 +199,7 @@ describe('encodeContent', () => {
const payload = await c.encodeContent(uncompressed, {
compression: Compression.COMPRESSION_DEFLATE,
})
assert.deepEqual(Uint8Array.from(payload), compressed)
expect(Uint8Array.from(payload)).toEqual(compressed)
})
})

Expand Down Expand Up @@ -371,7 +370,7 @@ describe('ClientOptions', () => {
} catch (e) {
return
}
fail()
assert.fail()
})

it('allows you to use custom content types', async () => {
Expand Down
7 changes: 3 additions & 4 deletions test/Compression.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import assert from 'assert'
import { content as proto } from '@xmtp/proto'
import {
compress,
Expand All @@ -14,7 +13,7 @@ describe('Compression', function () {
// make sink smaller so that it has to grow a lot
let to = { bytes: new Uint8Array(3) }
await readStreamFromBytes(from, 23).pipeTo(writeStreamToBytes(to, 1000))
assert.deepEqual(from, to.bytes)
expect(from).toEqual(to.bytes)
})

it('will not write beyond limit', () => {
Expand All @@ -37,8 +36,8 @@ describe('Compression', function () {
compression: proto.Compression.COMPRESSION_DEFLATE,
}
await compress(content)
assert.deepEqual(content.content, compressed)
expect(content.content).toEqual(compressed)
await decompress(content, 1000)
assert.deepEqual(content.content, uncompressed)
expect(content.content).toEqual(uncompressed)
})
})
5 changes: 2 additions & 3 deletions test/ContactBundle.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import * as assert from 'assert'
import { decodeContactBundle, encodeContactBundle } from '../src/ContactBundle'
import { PublicKeyBundle, SignedPublicKeyBundle } from '../src'
import {
Expand All @@ -15,7 +14,7 @@ describe('ContactBundles', function () {
let bytes = encodeContactBundle(pub)
const cb = decodeContactBundle(bytes)
expect(cb).toBeInstanceOf(PublicKeyBundle)
assert.ok(pub.equals(cb as PublicKeyBundle))
expect(pub.equals(cb as PublicKeyBundle)).toBeTruthy()
})
it('roundtrip v2', async function () {
const wallet = newWallet()
Expand All @@ -24,6 +23,6 @@ describe('ContactBundles', function () {
let bytes = encodeContactBundle(pub)
const cb = decodeContactBundle(bytes)
expect(cb).toBeInstanceOf(SignedPublicKeyBundle)
assert.ok(pub.equals(cb as SignedPublicKeyBundle))
expect(pub.equals(cb as SignedPublicKeyBundle)).toBeTruthy()
})
})
31 changes: 16 additions & 15 deletions test/Contacts.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { privatePreferences } from '@xmtp/proto'
import Client from '../src/Client'
import { Contacts } from '../src/Contacts'
import { newWallet } from './helpers'
Expand Down Expand Up @@ -174,19 +175,19 @@ describe('Contacts', () => {
expect(bobClient.contacts.isDenied(carol.address)).toBe(false)
})

// it('should stream consent updates', async () => {
// const aliceStream = await aliceClient.contacts.streamConsentList()
// await aliceClient.conversations.newConversation(bob.address)

// let numActions = 0
// const actions: privatePreferences.PrivatePreferencesAction[] = []
// for await (const action of aliceStream) {
// numActions++
// expect(action.block).toBeUndefined()
// expect(action.allow?.walletAddresses).toEqual([bob.address])
// break
// }
// expect(numActions).toBe(1)
// await aliceStream.return()
// })
it('should stream consent updates', async () => {
const aliceStream = await aliceClient.contacts.streamConsentList()
await aliceClient.conversations.newConversation(bob.address)

let numActions = 0
const actions: privatePreferences.PrivatePreferencesAction[] = []
for await (const action of aliceStream) {
numActions++
expect(action.block).toBeUndefined()
expect(action.allow?.walletAddresses).toEqual([bob.address])
break
}
expect(numActions).toBe(1)
await aliceStream.return()
})
})
23 changes: 11 additions & 12 deletions test/Message.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { ConversationV1 } from './../src/conversations/Conversation'
import assert from 'assert'
import { newWallet } from './helpers'
import { MessageV1, DecodedMessage } from '../src/Message'
import { PrivateKeyBundleV1 } from '../src/crypto/PrivateKeyBundle'
Expand Down Expand Up @@ -28,8 +27,8 @@ describe('Message', function () {
it('fully encodes/decodes messages', async function () {
// Alice's key bundle
const alicePub = alice.getPublicKeyBundle()
assert.ok(alice.identityKey)
assert.deepEqual(alice.identityKey.publicKey, alicePub.identityKey)
expect(alice.identityKey).toBeTruthy()
expect(alice.identityKey.publicKey).toEqual(alicePub.identityKey)

const bobWalletAddress = bob
.getPublicKeyBundle()
Expand All @@ -53,23 +52,23 @@ describe('Message', function () {
new Date()
)

assert.equal(msg1.senderAddress, aliceWallet.address)
assert.equal(msg1.recipientAddress, bobWalletAddress)
expect(msg1.senderAddress).toEqual(aliceWallet.address)
expect(msg1.recipientAddress).toEqual(bobWalletAddress)
const decrypted = await msg1.decrypt(
aliceKeystore,
alice.getPublicKeyBundle()
)
assert.deepEqual(decrypted, content)
expect(decrypted).toEqual(content)

// Bob decodes message from Alice
const msg2 = await MessageV1.fromBytes(msg1.toBytes())
const msg2Decrypted = await msg2.decrypt(
bobKeystore,
bob.getPublicKeyBundle()
)
assert.deepEqual(msg2Decrypted, decrypted)
assert.equal(msg2.senderAddress, aliceWallet.address)
assert.equal(msg2.recipientAddress, bobWalletAddress)
expect(msg2Decrypted).toEqual(decrypted)
expect(msg2.senderAddress).toEqual(aliceWallet.address)
expect(msg2.recipientAddress).toEqual(bobWalletAddress)
})

it('undecodable returns with undefined decrypted value', async () => {
Expand All @@ -89,7 +88,7 @@ describe('Message', function () {
bob.getPublicKeyBundle(),
new Date()
)
assert.ok(!msg.error)
expect(!msg.error).toBeTruthy()
const eveResult = msg.decrypt(eveKeystore, eve.getPublicKeyBundle())
expect(eveResult).rejects.toThrow(KeystoreError)
})
Expand Down Expand Up @@ -143,8 +142,8 @@ describe('Message', function () {
alice.getPublicKeyBundle(),
new Date()
)
assert.equal(msg.id.length, 64)
assert.equal(msg.id, bytesToHex(await sha256(msg.toBytes())))
expect(msg.id.length).toEqual(64)
expect(msg.id).toEqual(bytesToHex(await sha256(msg.toBytes())))
})

describe('DecodedMessage', () => {
Expand Down
17 changes: 8 additions & 9 deletions test/codecs/Composite.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import assert from 'assert'
import { ContentTypeText } from '../../src'
import {
CompositeCodec,
Expand All @@ -18,9 +17,9 @@ describe('CompositeType', () => {
],
}
const encoded = codec.encode(content, codecs)
assert(encoded.type.sameAs(ContentTypeComposite))
expect(encoded.type.sameAs(ContentTypeComposite)).toBe(true)
const decoded = codec.decode(encoded, codecs)
assert.deepEqual(decoded, content)
expect(decoded).toEqual(content)
})
it('nested composite', async () => {
const content = {
Expand All @@ -45,22 +44,22 @@ describe('CompositeType', () => {
],
}
const encoded = codec.encode(content, codecs)
assert(encoded.type.sameAs(ContentTypeComposite))
expect(encoded.type.sameAs(ContentTypeComposite)).toBe(true)
const decoded = codec.decode(encoded, codecs)
assert.deepEqual(decoded, content)
expect(decoded).toEqual(content)
})

it('not quite composite decodes as single part composite', async () => {
const content = { type: ContentTypeText, content: 'one' }
const encoded = codec.encode(content, codecs)
assert(encoded.type.sameAs(ContentTypeComposite))
expect(encoded.type.sameAs(ContentTypeComposite)).toBe(true)
const decoded = codec.decode(encoded, codecs)
assert.deepEqual(decoded, { parts: [content] })
expect(decoded).toEqual({ parts: [content] })
})

it('definitely not a composite', () => {
const codec = codecs.codecFor(ContentTypeComposite)
assert(codec)
expect(() => codec.encode('definitely not a composite', codecs)).toThrow()
expect(codec).toBeTruthy()
expect(() => codec!.encode('definitely not a composite', codecs)).toThrow()
})
})
27 changes: 13 additions & 14 deletions test/codecs/Text.test.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,28 @@
import assert from 'assert'
import { CodecRegistry } from '../helpers'
import { ContentTypeText, Encoding } from '../../src/codecs/Text'

describe('ContentTypeText', () => {
const codecs = new CodecRegistry()
const codec = codecs.codecFor(ContentTypeText)
assert(codec)
expect(codec).toBeTruthy()

it('can encode/decode text', () => {
const text = 'Hey'
const ec = codec.encode(text, codecs)
assert(ec.type.sameAs(ContentTypeText))
assert.equal(ec.parameters.encoding, Encoding.utf8)
const text2 = codec.decode(ec, codecs)
assert.equal(text2, text)
const ec = codec!.encode(text, codecs)
expect(ec.type.sameAs(ContentTypeText)).toBe(true)
expect(ec.parameters.encoding).toEqual(Encoding.utf8)
const text2 = codec!.decode(ec, codecs)
expect(text2).toEqual(text)
})

it('defaults to utf-8', () => {
const text = 'Hey'
const ec = codec.encode(text, codecs)
assert(ec.type.sameAs(ContentTypeText))
assert.equal(ec.parameters.encoding, Encoding.utf8)
const ec = codec!.encode(text, codecs)
expect(ec.type.sameAs(ContentTypeText)).toBe(true)
expect(ec.parameters.encoding).toEqual(Encoding.utf8)
delete ec.parameters.encoding
const text2 = codec.decode(ec, codecs)
assert.equal(text2, text)
const text2 = codec!.decode(ec, codecs)
expect(text2).toEqual(text)
})

it('throws on non-string', () => {
Expand All @@ -42,7 +41,7 @@ describe('ContentTypeText', () => {
parameters: {},
content: {} as Uint8Array,
}
expect(() => codec.decode(ec, codecs)).toThrow()
expect(() => codec!.decode(ec, codecs)).toThrow()
})

it('throws on unknown encoding', () => {
Expand All @@ -51,7 +50,7 @@ describe('ContentTypeText', () => {
parameters: { encoding: 'UTF-16' },
content: new Uint8Array(0),
}
expect(() => codec.decode(ec, codecs)).toThrow(
expect(() => codec!.decode(ec, codecs)).toThrow(
'unrecognized encoding UTF-16'
)
})
Expand Down
10 changes: 5 additions & 5 deletions test/conversations/Conversation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { PrivateKey, SignedPublicKeyBundle } from '../../src/crypto'
import { ConversationV2 } from '../../src/conversations/Conversation'
import { ContentTypeTestKey, TestKeyCodec } from '../ContentTypeTestKey'
import { content as proto } from '@xmtp/proto'
import { vi } from 'vitest'
import { assert, vi } from 'vitest'

describe('conversation', () => {
let alice: Client<string>
Expand Down Expand Up @@ -180,7 +180,7 @@ describe('conversation', () => {
const stream = await aliceConversation.streamEphemeral()

if (!stream) {
fail('no stream')
assert.fail('no stream')
}

await sleep(100)
Expand Down Expand Up @@ -216,7 +216,7 @@ describe('conversation', () => {
const stream = await aliceConversation.streamEphemeral()

if (!stream) {
fail('no stream')
assert.fail('no stream')
}

await sleep(100)
Expand Down Expand Up @@ -526,7 +526,7 @@ describe('conversation', () => {
const ac = await alice.conversations.newConversation(bob.address)
expect(ac.conversationVersion).toBe('v2')
if (!(ac instanceof ConversationV2)) {
fail()
assert.fail()
}
const as = await ac.streamMessages()
await sleep(100)
Expand All @@ -536,7 +536,7 @@ describe('conversation', () => {
const bc = bcs[0]
expect(bc.conversationVersion).toBe('v2')
if (!(bc instanceof ConversationV2)) {
fail()
assert.fail()
}
expect(bc.topic).toBe(ac.topic)
const bs = await bc.streamMessages()
Expand Down
Loading

0 comments on commit 3a27fa4

Please sign in to comment.