Skip to content

Commit e3ab192

Browse files
authored
feat: add mock stream pair (#2069)
Adds a `streamPair` convenience function to the interface mocks that returns two `Stream` objects where the duplex streams read/write to/from the other.
1 parent 72319fe commit e3ab192

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed

packages/interface-compliance-tests/src/mocks/connection.ts

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,13 @@ export function mockConnection (maConn: MultiaddrConnection, opts: MockConnectio
170170
return connection
171171
}
172172

173-
export function mockStream (stream: Duplex<AsyncGenerator<Uint8ArrayList>, Source<Uint8ArrayList | Uint8Array>, Promise<void>>): Stream {
173+
export interface StreamInit {
174+
direction?: Direction
175+
protocol?: string
176+
id?: string
177+
}
178+
179+
export function mockStream (stream: Duplex<AsyncGenerator<Uint8ArrayList>, Source<Uint8ArrayList | Uint8Array>, Promise<void>>, init: StreamInit = {}): Stream {
174180
return {
175181
...stream,
176182
close: async () => {},
@@ -186,10 +192,31 @@ export function mockStream (stream: Duplex<AsyncGenerator<Uint8ArrayList>, Sourc
186192
id: `stream-${Date.now()}`,
187193
status: 'open',
188194
readStatus: 'ready',
189-
writeStatus: 'ready'
195+
writeStatus: 'ready',
196+
...init
190197
}
191198
}
192199

200+
export interface StreamPairInit {
201+
duplex: Duplex<AsyncGenerator<Uint8ArrayList>, Source<Uint8ArrayList | Uint8Array>, Promise<void>>
202+
init?: StreamInit
203+
}
204+
205+
export function streamPair (a: StreamPairInit, b: StreamPairInit, init: StreamInit = {}): [Stream, Stream] {
206+
return [
207+
mockStream(a.duplex, {
208+
direction: 'outbound',
209+
...init,
210+
...(a.init ?? {})
211+
}),
212+
mockStream(b.duplex, {
213+
direction: 'inbound',
214+
...init,
215+
...(b.init ?? {})
216+
})
217+
]
218+
}
219+
193220
export interface Peer {
194221
peerId: PeerId
195222
registrar: Registrar

packages/interface-compliance-tests/src/mocks/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
export { mockConnectionEncrypter } from './connection-encrypter.js'
22
export { mockConnectionGater } from './connection-gater.js'
33
export { mockConnectionManager, mockNetwork } from './connection-manager.js'
4-
export { mockConnection, mockStream, connectionPair } from './connection.js'
4+
export { mockConnection, mockStream, streamPair, connectionPair } from './connection.js'
55
export { mockMultiaddrConnection, mockMultiaddrConnPair } from './multiaddr-connection.js'
66
export { mockMuxer } from './muxer.js'
77
export { mockRegistrar } from './registrar.js'

0 commit comments

Comments
 (0)