From 2b8edda0b61254d2a6ea5be8faa14cc0ea96c96c Mon Sep 17 00:00:00 2001 From: Amy Yan Date: Tue, 13 Aug 2024 12:38:10 +1000 Subject: [PATCH] fix: removed es6 class mock in `WebSocketStream.test.ts` in favor of utility function --- tests/WebSocketStream.test.ts | 172 ++++++++++++++++------------------ 1 file changed, 83 insertions(+), 89 deletions(-) diff --git a/tests/WebSocketStream.test.ts b/tests/WebSocketStream.test.ts index 120ade7e..548f82a8 100644 --- a/tests/WebSocketStream.test.ts +++ b/tests/WebSocketStream.test.ts @@ -1,9 +1,9 @@ import type { StreamId } from '#message/index.js'; +import type WebSocketConnection from '#WebSocketConnection.js'; import Logger, { formatting, LogLevel, StreamHandler } from '@matrixai/logger'; import { fc, test } from '@fast-check/jest'; import * as messageTestUtils from './message/utils.js'; import WebSocketStream from '#WebSocketStream.js'; -import WebSocketConnection from '#WebSocketConnection.js'; import * as events from '#events.js'; import * as utils from '#utils.js'; import * as messageUtils from '#message/utils.js'; @@ -28,114 +28,108 @@ const logger2 = new Logger('stream 2', LogLevel.WARN, [ let streamIdCounter = 0n; -jest.mock('#WebSocketConnection.js', () => { - return jest.fn().mockImplementation((streamOptions: StreamOptions = {}) => { - const instance = new EventTarget() as EventTarget & { - peerConnection: WebSocketConnection | undefined; - connectTo: (connection: WebSocketConnection) => void; - send: (data: Uint8Array) => Promise; - newStream: () => Promise; - streamMap: Map; - }; - instance.peerConnection = undefined; - instance.connectTo = (peerConnection: any) => { - instance.peerConnection = peerConnection; - peerConnection.peerConnection = instance; - }; - instance.streamMap = new Map(); - instance.newStream = async () => { - const stream = new WebSocketStream({ - initiated: 'local', - streamId: streamIdCounter as StreamId, +function createMockedWebSocketConnection( + streamOptions: StreamOptions = {}, +): WebSocketConnection { + const instance = new EventTarget() as EventTarget & { + peerConnection: WebSocketConnection | undefined; + connectTo: (connection: WebSocketConnection) => void; + send: (data: Uint8Array) => Promise; + newStream: () => Promise; + streamMap: Map; + }; + instance.peerConnection = undefined; + instance.connectTo = (peerConnection: any) => { + instance.peerConnection = peerConnection; + peerConnection.peerConnection = instance; + }; + instance.streamMap = new Map(); + instance.newStream = async () => { + const stream = new WebSocketStream({ + initiated: 'local', + streamId: streamIdCounter as StreamId, + bufferSize: STREAM_BUFFER_SIZE, + connection: instance as any, + logger: logger1, + ...streamOptions, + }); + stream.addEventListener( + events.EventWebSocketStreamSend.name, + async (evt: any) => { + await instance.send(evt.msg); + }, + ); + stream.addEventListener( + events.EventWebSocketStreamStopped.name, + () => { + instance.streamMap.delete(stream.streamId); + }, + { once: true }, + ); + instance.streamMap.set(stream.streamId, stream); + await stream.start(); + streamIdCounter++; + return stream; + }; + instance.send = async (array: Uint8Array | Array) => { + let data: Uint8Array; + if (ArrayBuffer.isView(array)) { + data = array; + } else { + data = messageUtils.concatUInt8Array(...array); + } + const { data: streamId, remainder } = messageUtils.parseStreamId(data); + // @ts-ignore: protected property + let stream = instance.peerConnection!.streamMap.get(streamId); + if (stream == null) { + if ( + !(remainder.at(0) === 0 && remainder.at(1) === StreamMessageType.Ack) + ) { + return; + } + stream = new WebSocketStream({ + initiated: 'peer', + streamId, bufferSize: STREAM_BUFFER_SIZE, - connection: instance as any, - logger: logger1, + connection: instance.peerConnection!, + logger: logger2, ...streamOptions, }); stream.addEventListener( events.EventWebSocketStreamSend.name, async (evt: any) => { - await instance.send(evt.msg); + // @ts-ignore: protected property + await instance.peerConnection!.send(evt.msg); }, ); stream.addEventListener( events.EventWebSocketStreamStopped.name, () => { - instance.streamMap.delete(stream.streamId); + // @ts-ignore: protected property + instance.peerConnection!.streamMap.delete(streamId); }, { once: true }, ); - instance.streamMap.set(stream.streamId, stream); - await stream.start(); - streamIdCounter++; - return stream; - }; - instance.send = async (array: Uint8Array | Array) => { - let data: Uint8Array; - if (ArrayBuffer.isView(array)) { - data = array; - } else { - data = messageUtils.concatUInt8Array(...array); - } - const { data: streamId, remainder } = messageUtils.parseStreamId(data); // @ts-ignore: protected property - let stream = instance.peerConnection!.streamMap.get(streamId); - if (stream == null) { - if ( - !(remainder.at(0) === 0 && remainder.at(1) === StreamMessageType.Ack) - ) { - return; - } - stream = new WebSocketStream({ - initiated: 'peer', - streamId, - bufferSize: STREAM_BUFFER_SIZE, - connection: instance.peerConnection!, - logger: logger2, - ...streamOptions, - }); - stream.addEventListener( - events.EventWebSocketStreamSend.name, - async (evt: any) => { - // @ts-ignore: protected property - await instance.peerConnection!.send(evt.msg); - }, - ); - stream.addEventListener( - events.EventWebSocketStreamStopped.name, - () => { - // @ts-ignore: protected property - instance.peerConnection!.streamMap.delete(streamId); - }, - { once: true }, - ); - // @ts-ignore: protected property - instance.peerConnection!.streamMap.set(stream.streamId, stream); - await stream.start(); - instance.peerConnection!.dispatchEvent( - new events.EventWebSocketConnectionStream({ - detail: stream, - }), - ); - } - await stream.streamRecv(remainder); - }; - return instance; - }); -}); - -const connectionMock = jest.mocked(WebSocketConnection, { shallow: true }); + instance.peerConnection!.streamMap.set(stream.streamId, stream); + await stream.start(); + instance.peerConnection!.dispatchEvent( + new events.EventWebSocketConnectionStream({ + detail: stream, + }), + ); + } + await stream.streamRecv(remainder); + }; + return instance as unknown as WebSocketConnection; +} describe(WebSocketStream.name, () => { - beforeEach(async () => { - connectionMock.mockClear(); - }); - async function createConnectionPair( streamOptions: StreamOptions = {}, ): Promise<[WebSocketConnection, WebSocketConnection]> { - const connection1 = new (WebSocketConnection as any)(streamOptions); - const connection2 = new (WebSocketConnection as any)(streamOptions); + const connection1 = createMockedWebSocketConnection(streamOptions); + const connection2 = createMockedWebSocketConnection(streamOptions); (connection1 as any).connectTo(connection2); return [connection1, connection2]; }