Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add mock stream pair #2069

Merged
merged 1 commit into from
Sep 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 29 additions & 2 deletions packages/interface-compliance-tests/src/mocks/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,13 @@ export function mockConnection (maConn: MultiaddrConnection, opts: MockConnectio
return connection
}

export function mockStream (stream: Duplex<AsyncGenerator<Uint8ArrayList>, Source<Uint8ArrayList | Uint8Array>, Promise<void>>): Stream {
export interface StreamInit {
direction?: Direction
protocol?: string
id?: string
}

export function mockStream (stream: Duplex<AsyncGenerator<Uint8ArrayList>, Source<Uint8ArrayList | Uint8Array>, Promise<void>>, init: StreamInit = {}): Stream {
return {
...stream,
close: async () => {},
Expand All @@ -186,10 +192,31 @@ export function mockStream (stream: Duplex<AsyncGenerator<Uint8ArrayList>, Sourc
id: `stream-${Date.now()}`,
status: 'open',
readStatus: 'ready',
writeStatus: 'ready'
writeStatus: 'ready',
...init
}
}

export interface StreamPairInit {
duplex: Duplex<AsyncGenerator<Uint8ArrayList>, Source<Uint8ArrayList | Uint8Array>, Promise<void>>
init?: StreamInit
}

export function streamPair (a: StreamPairInit, b: StreamPairInit, init: StreamInit = {}): [Stream, Stream] {
return [
mockStream(a.duplex, {
direction: 'outbound',
...init,
...(a.init ?? {})
}),
mockStream(b.duplex, {
direction: 'inbound',
...init,
...(b.init ?? {})
})
]
}

export interface Peer {
peerId: PeerId
registrar: Registrar
Expand Down
2 changes: 1 addition & 1 deletion packages/interface-compliance-tests/src/mocks/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export { mockConnectionEncrypter } from './connection-encrypter.js'
export { mockConnectionGater } from './connection-gater.js'
export { mockConnectionManager, mockNetwork } from './connection-manager.js'
export { mockConnection, mockStream, connectionPair } from './connection.js'
export { mockConnection, mockStream, streamPair, connectionPair } from './connection.js'
export { mockMultiaddrConnection, mockMultiaddrConnPair } from './multiaddr-connection.js'
export { mockMuxer } from './muxer.js'
export { mockRegistrar } from './registrar.js'
Expand Down
Loading