Skip to content

Commit

Permalink
Merge branch 'master' into rc
Browse files Browse the repository at this point in the history
  • Loading branch information
jonasgloning committed Feb 14, 2023
2 parents 145414d + 33a312d commit c8da7e6
Show file tree
Hide file tree
Showing 18 changed files with 2,769 additions and 1,417 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/node.js.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ jobs:
- run: npm ci
- run: npm run build
- run: npm run lint
- run: npm run coverage:lcov
- run: npm run coverage
- name: Publish code coverage to CodeClimate
uses: paambaati/codeclimate-action@v3.2.0
env:
Expand Down
6 changes: 0 additions & 6 deletions .mocharc.jsonc

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { expect } from 'chai';
import { describe, expect, it } from "@jest/globals";

import { Client } from '../../../../src/models/client';
import { HeartbeatHandler } from '../../../../src/messageHandler/handlers';

Expand All @@ -11,6 +12,6 @@ describe('Heartbeat handler', () => {

HeartbeatHandler(client);

expect(client.getLastPing()).to.be.closeTo(nowTime, 2);
expect(client.getLastPing()).toBeCloseTo(nowTime, 2);
});
});
117 changes: 117 additions & 0 deletions __test__/messageHandler/handlers/transmission/index.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
import { describe, expect, it } from "@jest/globals";

import { Client } from "../../../../src/models/client";
import { TransmissionHandler } from "../../../../src/messageHandler/handlers";
import { Realm } from "../../../../src/models/realm";
import { MessageType } from "../../../../src/enums";
import type WebSocket from "ws";

const createFakeSocket = (): WebSocket => {
/* eslint-disable @typescript-eslint/no-empty-function */
const sock = {
send: (): void => {},
close: (): void => {},
on: (): void => {},
};
/* eslint-enable @typescript-eslint/no-empty-function */

return sock as unknown as WebSocket;
};

describe("Transmission handler", () => {
it("should save message in queue when destination client not connected", () => {
const realm = new Realm();
const handleTransmission = TransmissionHandler({ realm });

const clientFrom = new Client({ id: "id1", token: "" });
const idTo = "id2";
realm.setClient(clientFrom, clientFrom.getId());

handleTransmission(clientFrom, {
type: MessageType.OFFER,
src: clientFrom.getId(),
dst: idTo,
});

expect(realm.getMessageQueueById(idTo)?.getMessages().length).toBe(1);
});

it("should not save LEAVE and EXPIRE messages in queue when destination client not connected", () => {
const realm = new Realm();
const handleTransmission = TransmissionHandler({ realm });

const clientFrom = new Client({ id: "id1", token: "" });
const idTo = "id2";
realm.setClient(clientFrom, clientFrom.getId());

handleTransmission(clientFrom, {
type: MessageType.LEAVE,
src: clientFrom.getId(),
dst: idTo,
});
handleTransmission(clientFrom, {
type: MessageType.EXPIRE,
src: clientFrom.getId(),
dst: idTo,
});

expect(realm.getMessageQueueById(idTo)).toBeUndefined();
});

it("should send message to destination client when destination client connected", () => {
const realm = new Realm();
const handleTransmission = TransmissionHandler({ realm });

const clientFrom = new Client({ id: "id1", token: "" });
const clientTo = new Client({ id: "id2", token: "" });
const socketTo = createFakeSocket();
clientTo.setSocket(socketTo);
realm.setClient(clientTo, clientTo.getId());

let sent = false;
socketTo.send = (): void => {
sent = true;
};

handleTransmission(clientFrom, {
type: MessageType.OFFER,
src: clientFrom.getId(),
dst: clientTo.getId(),
});

expect(sent).toBe(true);
});

it("should send LEAVE message to source client when sending to destination client failed", () => {
const realm = new Realm();
const handleTransmission = TransmissionHandler({ realm });

const clientFrom = new Client({ id: "id1", token: "" });
const clientTo = new Client({ id: "id2", token: "" });
const socketFrom = createFakeSocket();
const socketTo = createFakeSocket();
clientFrom.setSocket(socketFrom);
clientTo.setSocket(socketTo);
realm.setClient(clientFrom, clientFrom.getId());
realm.setClient(clientTo, clientTo.getId());

let sent = false;
socketFrom.send = (data: string): void => {
if (JSON.parse(data)?.type === MessageType.LEAVE) {
sent = true;
}
};

socketTo.send = (): void => {
throw Error();
};

handleTransmission(clientFrom, {
type: MessageType.OFFER,
src: clientFrom.getId(),
dst: clientTo.getId(),
});

expect(sent).toBe(true);
});
});
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { expect } from 'chai';
import { describe, expect, it } from "@jest/globals";

import { HandlersRegistry } from '../../src/messageHandler/handlersRegistry';
import { Handler } from '../../src/messageHandler/handler';
import type { Handler } from '../../src/messageHandler/handler';
import { MessageType } from '../../src/enums';

describe('HandlersRegistry', () => {
Expand All @@ -18,6 +19,6 @@ describe('HandlersRegistry', () => {

handlersRegistry.handle(undefined, { type: MessageType.OPEN, src: 'src', dst: 'dst' });

expect(handled).to.be.true;
expect(handled).toBe(true);
});
});
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { expect } from 'chai';
import { describe, expect, it } from "@jest/globals";

import { MessageQueue } from '../../src/models/messageQueue';
import { MessageType } from '../../src/enums';
import { IMessage } from '../../src/models/message';
import type { IMessage } from '../../src/models/message';
import { wait } from '../utils';

describe('MessageQueue', () => {
Expand All @@ -17,23 +18,23 @@ describe('MessageQueue', () => {
it('should add message to queue', () => {
const queue = new MessageQueue();
queue.addMessage(createTestMessage());
expect(queue.getMessages().length).to.eq(1);
expect(queue.getMessages().length).toBe(1);
});
});

describe('#readMessage', () => {
it('should return undefined for empty queue', () => {
const queue = new MessageQueue();
expect(queue.readMessage()).to.be.undefined;
expect(queue.readMessage()).toBeUndefined();
});

it('should return message if any exists in queue', () => {
const queue = new MessageQueue();
const message = createTestMessage();
queue.addMessage(message);

expect(queue.readMessage()).to.deep.eq(message);
expect(queue.readMessage()).to.be.undefined;
expect(queue.readMessage()).toEqual(message);
expect(queue.readMessage()).toBeUndefined();
});
});

Expand All @@ -42,7 +43,7 @@ describe('MessageQueue', () => {
const queue = new MessageQueue();
const lastReadAt = queue.getLastReadAt();
queue.readMessage();
expect(queue.getLastReadAt()).to.be.eq(lastReadAt);
expect(queue.getLastReadAt()).toBe(lastReadAt);
});

it('should be changed when read message', async () => {
Expand All @@ -52,11 +53,11 @@ describe('MessageQueue', () => {

await wait(10);

expect(queue.getLastReadAt()).to.be.eq(lastReadAt);
expect(queue.getLastReadAt()).toBe(lastReadAt);

queue.readMessage();

expect(queue.getLastReadAt()).to.be.gte(lastReadAt + 10);
expect(queue.getLastReadAt()).toBeGreaterThanOrEqual(lastReadAt + 10);
});
});
});
17 changes: 9 additions & 8 deletions test/models/realm.ts → __test__/models/realm.spec.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { expect } from 'chai';
import { describe, expect, it } from "@jest/globals";

import { Realm } from '../../src/models/realm';
import { Client } from '../../src/models/client';

describe('Realm', () => {
describe('#generateClientId', () => {
it('should generate a 36-character UUID, or return function value', () => {
const realm = new Realm();
expect(realm.generateClientId().length).to.eq(36);
expect(realm.generateClientId(() => 'abcd')).to.eq('abcd');
expect(realm.generateClientId().length).toBe(36);
expect(realm.generateClientId(() => 'abcd')).toBe('abcd');
});
});

Expand All @@ -17,7 +18,7 @@ describe('Realm', () => {
const client = new Client({ id: 'id', token: '' });

realm.setClient(client, 'id');
expect(realm.getClientsIds()).to.deep.eq(['id']);
expect(realm.getClientsIds()).toEqual(['id']);
});
});

Expand All @@ -29,7 +30,7 @@ describe('Realm', () => {
realm.setClient(client, 'id');
realm.removeClientById('id');

expect(realm.getClientById('id')).to.be.undefined;
expect(realm.getClientById('id')).toBeUndefined();
});
});

Expand All @@ -39,12 +40,12 @@ describe('Realm', () => {
const client = new Client({ id: 'id', token: '' });

realm.setClient(client, 'id');
expect(realm.getClientsIds()).to.deep.eq(['id']);
expect(realm.getClientsIds()).toEqual(['id']);

expect(realm.getClientById('id')).to.eq(client);
expect(realm.getClientById('id')).toBe(client);

realm.removeClientById('id');
expect(realm.getClientsIds()).to.deep.eq([]);
expect(realm.getClientsIds()).toEqual([]);
});
});
});
13 changes: 7 additions & 6 deletions test/peerjs.ts → __test__/peerjs.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { expect } from 'chai';
import { describe, expect, it } from "@jest/globals";

import http from 'http';
import expectedJson from '../app.json';
import { spawn } from 'child_process';
Expand Down Expand Up @@ -27,7 +28,8 @@ async function makeRequest() {
}

describe('Check bin/peerjs', () => {
it('should return content of app.json file', async () => {
it('should return content of app.json file', async () => {
expect.assertions(1);
let resolver: () => void;
let rejecter: (err: unknown) => void;
const promise = new Promise<void>((resolve, reject) => {
Expand All @@ -36,18 +38,17 @@ describe('Check bin/peerjs', () => {
});

const ls = spawn('node', [path.join(__dirname, '../', 'dist/bin/peerjs.js'), '--port', PORT]);

ls.stdout.on('data', async (data: string) => {
ls.stdout.on('data', async (data: string) => {
if (!data.includes('Started')) return;

try {
const resp = await makeRequest();
expect(resp).to.deep.eq(expectedJson);
expect(resp).toEqual(expectedJson);
resolver();
} catch (error) {
rejecter(error);
} finally {
ls.kill('SIGINT');
ls.kill('SIGKILL');
}
});

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { expect } from 'chai';
import { describe, expect, it } from "@jest/globals";

import { Client } from '../../../src/models/client';
import { Realm } from '../../../src/models/realm';
import { CheckBrokenConnections } from '../../../src/services/checkBrokenConnections';
Expand All @@ -16,7 +17,7 @@ describe('CheckBrokenConnections', () => {

await wait(checkBrokenConnections.checkInterval * 2 + 30);

expect(realm.getClientById('id')).to.be.undefined;
expect(realm.getClientById('id')).toBeUndefined();

checkBrokenConnections.stop();
});
Expand All @@ -37,7 +38,7 @@ describe('CheckBrokenConnections', () => {

await wait(checkBrokenConnections.checkInterval * 2 + 10);

expect(realm.getClientById('id')).to.be.undefined;
expect(realm.getClientById('id')).toBeUndefined();

checkBrokenConnections.stop();
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { expect } from 'chai';
import { describe, expect, it } from "@jest/globals";

import { Client } from '../../../src/models/client';
import { Realm } from '../../../src/models/realm';
import { IMessage } from '../../../src/models/message';
import type { IMessage } from '../../../src/models/message';
import { MessagesExpire } from '../../../src/services/messagesExpire';
import { MessageHandler } from '../../../src/messageHandler';
import { MessageType } from '../../../src/enums';
Expand Down Expand Up @@ -33,11 +34,11 @@ describe('MessagesExpire', () => {

await wait(checkInterval * 2);

expect(realm.getMessageQueueById(client.getId())?.getMessages().length).to.be.eq(1);
expect(realm.getMessageQueueById(client.getId())?.getMessages().length).toBe(1);

await wait(expireTimeout);

expect(realm.getMessageQueueById(client.getId())).to.be.undefined;
expect(realm.getMessageQueueById(client.getId())).toBeUndefined();

messagesExpire.stopMessagesExpiration();
});
Expand All @@ -59,8 +60,8 @@ describe('MessagesExpire', () => {
let handledCount = 0;

messageHandler.handle = (client, message): boolean => {
expect(client).to.be.undefined;
expect(message.type).to.be.eq(MessageType.EXPIRE);
expect(client).toBeUndefined();
expect(message.type).toBe(MessageType.EXPIRE);

handledCount++;

Expand All @@ -72,7 +73,7 @@ describe('MessagesExpire', () => {
await wait(checkInterval * 2);
await wait(expireTimeout);

expect(handledCount).to.be.eq(2);
expect(handledCount).toBe(2);

messagesExpire.stopMessagesExpiration();
});
Expand Down
Loading

0 comments on commit c8da7e6

Please sign in to comment.