Skip to content

Commit

Permalink
fix: removed es6 class mock in WebSocketStream.test.ts in favor of …
Browse files Browse the repository at this point in the history
…utility function
  • Loading branch information
amydevs committed Aug 13, 2024
1 parent 76e55df commit 2b8edda
Showing 1 changed file with 83 additions and 89 deletions.
172 changes: 83 additions & 89 deletions tests/WebSocketStream.test.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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<void>;
newStream: () => Promise<WebSocketStream>;
streamMap: Map<StreamId, WebSocketStream>;
};
instance.peerConnection = undefined;
instance.connectTo = (peerConnection: any) => {
instance.peerConnection = peerConnection;
peerConnection.peerConnection = instance;
};
instance.streamMap = new Map<StreamId, WebSocketStream>();
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<void>;
newStream: () => Promise<WebSocketStream>;
streamMap: Map<StreamId, WebSocketStream>;
};
instance.peerConnection = undefined;
instance.connectTo = (peerConnection: any) => {
instance.peerConnection = peerConnection;
peerConnection.peerConnection = instance;
};
instance.streamMap = new Map<StreamId, WebSocketStream>();
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<Uint8Array>) => {
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<Uint8Array>) => {
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];
}
Expand Down

0 comments on commit 2b8edda

Please sign in to comment.