-
Notifications
You must be signed in to change notification settings - Fork 9
Tests to typescript #229
Tests to typescript #229
Changes from all commits
631fe92
11724d3
7fab45c
30a05da
68b3681
3335a8c
15b8200
91282e0
70de3b8
f9206c4
eeec191
e5fd145
9dcd9d2
c26f5ff
15f8d25
37c9538
eba7084
1a4299c
2d054ff
ecfadf4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,8 +2,10 @@ import { computeAddress } from '@ethersproject/transactions' | |
| import { Web3Provider } from '@ethersproject/providers' | ||
| import { hexlify } from '@ethersproject/bytes' | ||
| import { sha256 } from '@ethersproject/sha2' | ||
| import { StreamrClient } from '../StreamrClient' | ||
| import { EthereumConfig } from '../Config' | ||
|
|
||
| async function getUsername(client) { | ||
| async function getUsername(client: StreamrClient) { | ||
| const { options: { auth = {} } = {} } = client | ||
| if (auth.username) { return auth.username } | ||
|
|
||
|
|
@@ -15,7 +17,7 @@ async function getUsername(client) { | |
| ) | ||
| } | ||
|
|
||
| export async function getAddressFromOptions({ ethereum, privateKey } = {}) { | ||
| export async function getAddressFromOptions({ ethereum, privateKey }: { ethereum?: EthereumConfig, privateKey?: any} = {}) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should probably get something like this into type PrivateKeyAuth = {
privateKey: string
ethereum?: never,
apiKey?: never,
}
type ProviderAuth = {
ethereum: ExternalProvider | JsonRpcFetchFunc
privateKey?: never,
apiKey?: never,
}
/**
* deprecated: Please create an ethereum identity.
*/
type APIKeyAuth = {
apiKey: string
privateKey?: never,
ethereum?: never,
}
type AnonymousAuth = {
privateKey?: never,
apiKey?: never,
ethereum?: never,
}
type Authenticated = PrivateKeyAuth | ProviderAuth | APIKeyAuth
type AuthOptions = Authenticated | AnonymousAuthMaybe for another PR, your call.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good point. It would be great if e.g.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yeah it might, worth looking into |
||
| if (privateKey) { | ||
| return computeAddress(privateKey).toLowerCase() | ||
| } | ||
|
|
@@ -29,7 +31,7 @@ export async function getAddressFromOptions({ ethereum, privateKey } = {}) { | |
| throw new Error('Need either "privateKey" or "ethereum".') | ||
| } | ||
|
|
||
| export async function getUserId(client) { | ||
| export async function getUserId(client: StreamrClient) { | ||
| if (client.session.isUnauthenticated()) { | ||
| throw new Error('Need to be authenticated to getUserId.') | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,34 +5,39 @@ import { StreamrClient } from '../../src/StreamrClient' | |
| import Connection from '../../src/Connection' | ||
|
|
||
| import config from './config' | ||
| import { Stream } from '../../src/stream' | ||
| import { Subscriber, Subscription } from '../../src/subscribe' | ||
| import { MessageRef } from 'streamr-client-protocol/dist/src/protocol/message_layer' | ||
| import { StreamrClientOptions } from '../../src' | ||
|
|
||
| const MAX_MESSAGES = 10 | ||
|
|
||
| describeRepeats('GapFill with resends', () => { | ||
| let expectErrors = 0 // check no errors by default | ||
| let publishTestMessages | ||
| let publishTestMessages: ReturnType<typeof getPublishTestMessages> | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
| let onError = jest.fn() | ||
| let client | ||
| let stream | ||
| let subscriber | ||
| let client: StreamrClient | ||
| let stream: Stream | ||
| let subscriber: Subscriber | ||
|
|
||
| const createClient = (opts = {}) => { | ||
| const c = new StreamrClient({ | ||
| ...config.clientOptions, | ||
| ...opts, | ||
| auth: { | ||
| privateKey: fakePrivateKey(), | ||
| }, | ||
| autoConnect: false, | ||
| autoDisconnect: false, | ||
| // @ts-expect-error | ||
| maxRetries: 2, | ||
| ...config.clientOptions, | ||
| ...opts, | ||
| }) | ||
| c.onError = jest.fn() | ||
| c.on('error', onError) | ||
| return c | ||
| } | ||
|
|
||
| async function setupClient(opts) { | ||
| async function setupClient(opts: StreamrClientOptions) { | ||
| // eslint-disable-next-line require-atomic-updates | ||
| client = createClient(opts) | ||
| subscriber = client.subscriber | ||
|
|
@@ -57,11 +62,11 @@ describeRepeats('GapFill with resends', () => { | |
| if (!subscriber) { return } | ||
| expect(subscriber.count(stream.id)).toBe(0) | ||
| if (!client) { return } | ||
| expect(client.getSubscriptions(stream.id)).toEqual([]) | ||
| expect(client.getSubscriptions()).toEqual([]) | ||
| }) | ||
|
|
||
| afterEach(async () => { | ||
| await wait() | ||
| await wait(0) | ||
| // ensure no unexpected errors | ||
| expect(onError).toHaveBeenCalledTimes(expectErrors) | ||
| if (client) { | ||
|
|
@@ -70,7 +75,7 @@ describeRepeats('GapFill with resends', () => { | |
| }) | ||
|
|
||
| afterEach(async () => { | ||
| await wait() | ||
| await wait(0) | ||
| if (client) { | ||
| client.debug('disconnecting after test >>') | ||
| await client.disconnect() | ||
|
|
@@ -84,7 +89,7 @@ describeRepeats('GapFill with resends', () => { | |
| } | ||
| }) | ||
|
|
||
| let subs = [] | ||
| let subs: Subscription[] = [] | ||
|
|
||
| beforeEach(async () => { | ||
| const existingSubs = subs | ||
|
|
@@ -108,7 +113,7 @@ describeRepeats('GapFill with resends', () => { | |
| const { parse } = client.connection | ||
| let count = 0 | ||
| client.connection.parse = (...args) => { | ||
| const msg = parse.call(client.connection, ...args) | ||
| const msg: any = parse.call(client.connection, ...args) | ||
| if (!msg.streamMessage) { | ||
| return msg | ||
| } | ||
|
|
@@ -143,7 +148,7 @@ describeRepeats('GapFill with resends', () => { | |
| const { parse } = client.connection | ||
| let count = 0 | ||
| client.connection.parse = (...args) => { | ||
| const msg = parse.call(client.connection, ...args) | ||
| const msg: any = parse.call(client.connection, ...args) | ||
| if (!msg.streamMessage) { | ||
| return msg | ||
| } | ||
|
|
@@ -176,7 +181,7 @@ describeRepeats('GapFill with resends', () => { | |
| const { parse } = client.connection | ||
| let count = 0 | ||
| client.connection.parse = (...args) => { | ||
| const msg = parse.call(client.connection, ...args) | ||
| const msg: any = parse.call(client.connection, ...args) | ||
| if (!msg.streamMessage) { | ||
| return msg | ||
| } | ||
|
|
@@ -208,7 +213,7 @@ describeRepeats('GapFill with resends', () => { | |
| const { parse } = client.connection | ||
| let count = 0 | ||
| client.connection.parse = (...args) => { | ||
| const msg = parse.call(client.connection, ...args) | ||
| const msg: any = parse.call(client.connection, ...args) | ||
| if (!msg.streamMessage) { | ||
| return msg | ||
| } | ||
|
|
@@ -242,9 +247,9 @@ describeRepeats('GapFill with resends', () => { | |
| it('can fill gaps in resends even if gap cannot be filled', async () => { | ||
| const { parse } = client.connection | ||
| let count = 0 | ||
| let droppedMsgRef | ||
| let droppedMsgRef: MessageRef | ||
| client.connection.parse = (...args) => { | ||
| const msg = parse.call(client.connection, ...args) | ||
| const msg: any = parse.call(client.connection, ...args) | ||
| if (!msg.streamMessage) { | ||
| return msg | ||
| } | ||
|
|
@@ -277,7 +282,7 @@ describeRepeats('GapFill with resends', () => { | |
| received.push(m.getParsedContent()) | ||
| // should not need to explicitly end | ||
| } | ||
| expect(received).toEqual(published.filter((_value, index) => index !== 2)) | ||
| expect(received).toEqual(published.filter((_value: any, index: number) => index !== 2)) | ||
| expect(client.connection.getState()).toBe('connected') | ||
| }, 60000) | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,12 +5,12 @@ import { StreamrClient } from '../../src/StreamrClient' | |
| import { Defer } from '../../src/utils' | ||
|
|
||
| import config from './config' | ||
| import { Stream } from '../../src/stream' | ||
| import { Subscription } from '../../src' | ||
| import { PublishRequest } from 'streamr-client-protocol/dist/src/protocol/control_layer' | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. blegh, ideally shouldn't have For now you might have to grab it like this: But if we update protocol to 8.0.0 you should be able to do this as of streamr-dev/streamr-client-protocol-js#53: import { PublishRequest } from 'streamr-client-protocol' |
||
|
|
||
| const createClient = (opts = {}) => new StreamrClient({ | ||
| ...(config.clientOptions || { | ||
| url: config.websocketUrl, | ||
| restUrl: config.restUrl, | ||
| }), | ||
| ...config.clientOptions, | ||
| auth: { | ||
| privateKey: fakePrivateKey(), | ||
| }, | ||
|
|
@@ -22,10 +22,10 @@ const createClient = (opts = {}) => new StreamrClient({ | |
| const MAX_MESSAGES = 3 | ||
|
|
||
| describe('resend/reconnect', () => { | ||
| let client | ||
| let stream | ||
| let publishedMessages | ||
| let publishTestMessages | ||
| let client: StreamrClient | ||
| let stream: Stream | ||
| let publishedMessages: [message: any, request: PublishRequest][] | ||
| let publishTestMessages: ReturnType<typeof getPublishTestMessages> | ||
|
|
||
| beforeEach(async () => { | ||
| client = createClient() | ||
|
|
@@ -49,13 +49,13 @@ describe('resend/reconnect', () => { | |
|
|
||
| describe('reconnect with resend', () => { | ||
| let shouldDisconnect = false | ||
| let sub | ||
| let messages = [] | ||
| let sub: Subscription | ||
| let messages: any[] = [] | ||
| beforeEach(async () => { | ||
| const done = Defer() | ||
| messages = [] | ||
| sub = await client.subscribe({ | ||
| stream: stream.id, | ||
| streamId: stream.id, | ||
| resend: { | ||
| last: MAX_MESSAGES, | ||
| }, | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.