Skip to content

Commit

Permalink
test: refactor local clients
Browse files Browse the repository at this point in the history
  • Loading branch information
neekolas committed May 27, 2022
1 parent 8cbdd15 commit a819cee
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 31 deletions.
22 changes: 9 additions & 13 deletions test/Client.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import assert from 'assert'
import { pollFor, newWallet, dumpStream } from './helpers'
import {
pollFor,
newWallet,
dumpStream,
newLocalDockerClient,
newDevClient,
} from './helpers'
import { publishUserContact, sleep } from '../src/utils'
import Client, { KeyStoreType } from '../src/Client'
import { TestKeyCodec, ContentTypeTestKey } from './ContentTypeTestKey'
Expand All @@ -13,16 +19,6 @@ import {
PrivateKeyBundle,
} from '../src'

const newLocalDockerClient = (): Promise<Client> =>
Client.create(newWallet(), {
bootstrapAddrs: [
'/ip4/127.0.0.1/tcp/9001/ws/p2p/16Uiu2HAmNCxLZCkXNbpVPBpSSnHj9iq4HZQj7fxRzw2kj1kKSHHA',
],
})

const newTestnetClient = (): Promise<Client> =>
Client.create(newWallet(), { env: 'dev' })

describe('Client', () => {
const tests = [
{
Expand All @@ -32,8 +28,8 @@ describe('Client', () => {
]
if (process.env.CI || process.env.TESTNET) {
tests.push({
name: 'testnet',
newClient: newTestnetClient,
name: 'dev',
newClient: newDevClient,
})
}
tests.forEach((testCase) => {
Expand Down
12 changes: 3 additions & 9 deletions test/conversations/Conversation.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
import { Client } from '../../src'
import { Wallet } from 'ethers'
import { sleep } from '../../src/utils'

const opts = {
bootstrapAddrs: [
'/ip4/127.0.0.1/tcp/9001/ws/p2p/16Uiu2HAmNCxLZCkXNbpVPBpSSnHj9iq4HZQj7fxRzw2kj1kKSHHA',
],
}
import { newLocalDockerClient } from '../helpers'

jest.setTimeout(20000)

Expand All @@ -15,8 +9,8 @@ describe('conversations', () => {
let bob: Client

beforeEach(async () => {
alice = await Client.create(Wallet.createRandom(), opts)
bob = await Client.create(Wallet.createRandom(), opts)
alice = await newLocalDockerClient()
bob = await newLocalDockerClient()
})

afterEach(async () => {
Expand Down
12 changes: 3 additions & 9 deletions test/conversations/Conversations.test.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,16 @@
import { newLocalDockerClient } from './../helpers'
import { Client } from '../../src'
import { Wallet } from 'ethers'
import { sleep } from '../../src/utils'

const opts = {
bootstrapAddrs: [
'/ip4/127.0.0.1/tcp/9001/ws/p2p/16Uiu2HAmNCxLZCkXNbpVPBpSSnHj9iq4HZQj7fxRzw2kj1kKSHHA',
],
}

jest.setTimeout(20000)

describe('conversations', () => {
let alice: Client
let bob: Client

beforeEach(async () => {
alice = await Client.create(Wallet.createRandom(), opts)
bob = await Client.create(Wallet.createRandom(), opts)
alice = await newLocalDockerClient()
bob = await newLocalDockerClient()
await sleep(100)
})

Expand Down
12 changes: 12 additions & 0 deletions test/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@ import {
ContentCodec,
ContentTypeId,
TextCodec,
Client,
} from '../src'
import Stream from '../src/Stream'
import { promiseWithTimeout } from '../src/utils'

const LOCAL_DOCKER_MULTIADDR =
'/ip4/127.0.0.1/tcp/9001/ws/p2p/16Uiu2HAmNCxLZCkXNbpVPBpSSnHj9iq4HZQj7fxRzw2kj1kKSHHA'

export const sleep = (ms: number): Promise<void> =>
new Promise((resolve) => setTimeout(resolve, ms))

Expand Down Expand Up @@ -89,3 +93,11 @@ export class CodecRegistry {
return this._codecs.get(key)
}
}

export const newLocalDockerClient = (): Promise<Client> =>
Client.create(newWallet(), {
bootstrapAddrs: [LOCAL_DOCKER_MULTIADDR],
})

export const newDevClient = (): Promise<Client> =>
Client.create(newWallet(), { env: 'dev' })

0 comments on commit a819cee

Please sign in to comment.