Skip to content

Commit c8da7e6

Browse files
committed
Merge branch 'master' into rc
2 parents 145414d + 33a312d commit c8da7e6

File tree

18 files changed

+2769
-1417
lines changed

18 files changed

+2769
-1417
lines changed

.github/workflows/node.js.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ jobs:
2929
- run: npm ci
3030
- run: npm run build
3131
- run: npm run lint
32-
- run: npm run coverage:lcov
32+
- run: npm run coverage
3333
- name: Publish code coverage to CodeClimate
3434
uses: paambaati/[email protected]
3535
env:

.mocharc.jsonc

Lines changed: 0 additions & 6 deletions
This file was deleted.

test/messageHandler/handlers/heartbeat/index.ts renamed to __test__/messageHandler/handlers/heartbeat/index.spec.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { expect } from 'chai';
1+
import { describe, expect, it } from "@jest/globals";
2+
23
import { Client } from '../../../../src/models/client';
34
import { HeartbeatHandler } from '../../../../src/messageHandler/handlers';
45

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

1213
HeartbeatHandler(client);
1314

14-
expect(client.getLastPing()).to.be.closeTo(nowTime, 2);
15+
expect(client.getLastPing()).toBeCloseTo(nowTime, 2);
1516
});
1617
});
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
import { describe, expect, it } from "@jest/globals";
2+
3+
import { Client } from "../../../../src/models/client";
4+
import { TransmissionHandler } from "../../../../src/messageHandler/handlers";
5+
import { Realm } from "../../../../src/models/realm";
6+
import { MessageType } from "../../../../src/enums";
7+
import type WebSocket from "ws";
8+
9+
const createFakeSocket = (): WebSocket => {
10+
/* eslint-disable @typescript-eslint/no-empty-function */
11+
const sock = {
12+
send: (): void => {},
13+
close: (): void => {},
14+
on: (): void => {},
15+
};
16+
/* eslint-enable @typescript-eslint/no-empty-function */
17+
18+
return sock as unknown as WebSocket;
19+
};
20+
21+
describe("Transmission handler", () => {
22+
it("should save message in queue when destination client not connected", () => {
23+
const realm = new Realm();
24+
const handleTransmission = TransmissionHandler({ realm });
25+
26+
const clientFrom = new Client({ id: "id1", token: "" });
27+
const idTo = "id2";
28+
realm.setClient(clientFrom, clientFrom.getId());
29+
30+
handleTransmission(clientFrom, {
31+
type: MessageType.OFFER,
32+
src: clientFrom.getId(),
33+
dst: idTo,
34+
});
35+
36+
expect(realm.getMessageQueueById(idTo)?.getMessages().length).toBe(1);
37+
});
38+
39+
it("should not save LEAVE and EXPIRE messages in queue when destination client not connected", () => {
40+
const realm = new Realm();
41+
const handleTransmission = TransmissionHandler({ realm });
42+
43+
const clientFrom = new Client({ id: "id1", token: "" });
44+
const idTo = "id2";
45+
realm.setClient(clientFrom, clientFrom.getId());
46+
47+
handleTransmission(clientFrom, {
48+
type: MessageType.LEAVE,
49+
src: clientFrom.getId(),
50+
dst: idTo,
51+
});
52+
handleTransmission(clientFrom, {
53+
type: MessageType.EXPIRE,
54+
src: clientFrom.getId(),
55+
dst: idTo,
56+
});
57+
58+
expect(realm.getMessageQueueById(idTo)).toBeUndefined();
59+
});
60+
61+
it("should send message to destination client when destination client connected", () => {
62+
const realm = new Realm();
63+
const handleTransmission = TransmissionHandler({ realm });
64+
65+
const clientFrom = new Client({ id: "id1", token: "" });
66+
const clientTo = new Client({ id: "id2", token: "" });
67+
const socketTo = createFakeSocket();
68+
clientTo.setSocket(socketTo);
69+
realm.setClient(clientTo, clientTo.getId());
70+
71+
let sent = false;
72+
socketTo.send = (): void => {
73+
sent = true;
74+
};
75+
76+
handleTransmission(clientFrom, {
77+
type: MessageType.OFFER,
78+
src: clientFrom.getId(),
79+
dst: clientTo.getId(),
80+
});
81+
82+
expect(sent).toBe(true);
83+
});
84+
85+
it("should send LEAVE message to source client when sending to destination client failed", () => {
86+
const realm = new Realm();
87+
const handleTransmission = TransmissionHandler({ realm });
88+
89+
const clientFrom = new Client({ id: "id1", token: "" });
90+
const clientTo = new Client({ id: "id2", token: "" });
91+
const socketFrom = createFakeSocket();
92+
const socketTo = createFakeSocket();
93+
clientFrom.setSocket(socketFrom);
94+
clientTo.setSocket(socketTo);
95+
realm.setClient(clientFrom, clientFrom.getId());
96+
realm.setClient(clientTo, clientTo.getId());
97+
98+
let sent = false;
99+
socketFrom.send = (data: string): void => {
100+
if (JSON.parse(data)?.type === MessageType.LEAVE) {
101+
sent = true;
102+
}
103+
};
104+
105+
socketTo.send = (): void => {
106+
throw Error();
107+
};
108+
109+
handleTransmission(clientFrom, {
110+
type: MessageType.OFFER,
111+
src: clientFrom.getId(),
112+
dst: clientTo.getId(),
113+
});
114+
115+
expect(sent).toBe(true);
116+
});
117+
});

test/messageHandler/handlersRegistry.ts renamed to __test__/messageHandler/handlersRegistry.spec.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
import { expect } from 'chai';
1+
import { describe, expect, it } from "@jest/globals";
2+
23
import { HandlersRegistry } from '../../src/messageHandler/handlersRegistry';
3-
import { Handler } from '../../src/messageHandler/handler';
4+
import type { Handler } from '../../src/messageHandler/handler';
45
import { MessageType } from '../../src/enums';
56

67
describe('HandlersRegistry', () => {
@@ -18,6 +19,6 @@ describe('HandlersRegistry', () => {
1819

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

21-
expect(handled).to.be.true;
22+
expect(handled).toBe(true);
2223
});
2324
});

test/models/messageQueue.ts renamed to __test__/models/messageQueue.spec.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
import { expect } from 'chai';
1+
import { describe, expect, it } from "@jest/globals";
2+
23
import { MessageQueue } from '../../src/models/messageQueue';
34
import { MessageType } from '../../src/enums';
4-
import { IMessage } from '../../src/models/message';
5+
import type { IMessage } from '../../src/models/message';
56
import { wait } from '../utils';
67

78
describe('MessageQueue', () => {
@@ -17,23 +18,23 @@ describe('MessageQueue', () => {
1718
it('should add message to queue', () => {
1819
const queue = new MessageQueue();
1920
queue.addMessage(createTestMessage());
20-
expect(queue.getMessages().length).to.eq(1);
21+
expect(queue.getMessages().length).toBe(1);
2122
});
2223
});
2324

2425
describe('#readMessage', () => {
2526
it('should return undefined for empty queue', () => {
2627
const queue = new MessageQueue();
27-
expect(queue.readMessage()).to.be.undefined;
28+
expect(queue.readMessage()).toBeUndefined();
2829
});
2930

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

35-
expect(queue.readMessage()).to.deep.eq(message);
36-
expect(queue.readMessage()).to.be.undefined;
36+
expect(queue.readMessage()).toEqual(message);
37+
expect(queue.readMessage()).toBeUndefined();
3738
});
3839
});
3940

@@ -42,7 +43,7 @@ describe('MessageQueue', () => {
4243
const queue = new MessageQueue();
4344
const lastReadAt = queue.getLastReadAt();
4445
queue.readMessage();
45-
expect(queue.getLastReadAt()).to.be.eq(lastReadAt);
46+
expect(queue.getLastReadAt()).toBe(lastReadAt);
4647
});
4748

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

5354
await wait(10);
5455

55-
expect(queue.getLastReadAt()).to.be.eq(lastReadAt);
56+
expect(queue.getLastReadAt()).toBe(lastReadAt);
5657

5758
queue.readMessage();
5859

59-
expect(queue.getLastReadAt()).to.be.gte(lastReadAt + 10);
60+
expect(queue.getLastReadAt()).toBeGreaterThanOrEqual(lastReadAt + 10);
6061
});
6162
});
6263
});
Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1-
import { expect } from 'chai';
1+
import { describe, expect, it } from "@jest/globals";
2+
23
import { Realm } from '../../src/models/realm';
34
import { Client } from '../../src/models/client';
45

56
describe('Realm', () => {
67
describe('#generateClientId', () => {
78
it('should generate a 36-character UUID, or return function value', () => {
89
const realm = new Realm();
9-
expect(realm.generateClientId().length).to.eq(36);
10-
expect(realm.generateClientId(() => 'abcd')).to.eq('abcd');
10+
expect(realm.generateClientId().length).toBe(36);
11+
expect(realm.generateClientId(() => 'abcd')).toBe('abcd');
1112
});
1213
});
1314

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

1920
realm.setClient(client, 'id');
20-
expect(realm.getClientsIds()).to.deep.eq(['id']);
21+
expect(realm.getClientsIds()).toEqual(['id']);
2122
});
2223
});
2324

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

32-
expect(realm.getClientById('id')).to.be.undefined;
33+
expect(realm.getClientById('id')).toBeUndefined();
3334
});
3435
});
3536

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

4142
realm.setClient(client, 'id');
42-
expect(realm.getClientsIds()).to.deep.eq(['id']);
43+
expect(realm.getClientsIds()).toEqual(['id']);
4344

44-
expect(realm.getClientById('id')).to.eq(client);
45+
expect(realm.getClientById('id')).toBe(client);
4546

4647
realm.removeClientById('id');
47-
expect(realm.getClientsIds()).to.deep.eq([]);
48+
expect(realm.getClientsIds()).toEqual([]);
4849
});
4950
});
5051
});

test/peerjs.ts renamed to __test__/peerjs.spec.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { expect } from 'chai';
1+
import { describe, expect, it } from "@jest/globals";
2+
23
import http from 'http';
34
import expectedJson from '../app.json';
45
import { spawn } from 'child_process';
@@ -27,7 +28,8 @@ async function makeRequest() {
2728
}
2829

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

3840
const ls = spawn('node', [path.join(__dirname, '../', 'dist/bin/peerjs.js'), '--port', PORT]);
39-
40-
ls.stdout.on('data', async (data: string) => {
41+
ls.stdout.on('data', async (data: string) => {
4142
if (!data.includes('Started')) return;
4243

4344
try {
4445
const resp = await makeRequest();
45-
expect(resp).to.deep.eq(expectedJson);
46+
expect(resp).toEqual(expectedJson);
4647
resolver();
4748
} catch (error) {
4849
rejecter(error);
4950
} finally {
50-
ls.kill('SIGINT');
51+
ls.kill('SIGKILL');
5152
}
5253
});
5354

test/services/checkBrokenConnections/index.ts renamed to __test__/services/checkBrokenConnections/index.spec.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { expect } from 'chai';
1+
import { describe, expect, it } from "@jest/globals";
2+
23
import { Client } from '../../../src/models/client';
34
import { Realm } from '../../../src/models/realm';
45
import { CheckBrokenConnections } from '../../../src/services/checkBrokenConnections';
@@ -16,7 +17,7 @@ describe('CheckBrokenConnections', () => {
1617

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

19-
expect(realm.getClientById('id')).to.be.undefined;
20+
expect(realm.getClientById('id')).toBeUndefined();
2021

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

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

40-
expect(realm.getClientById('id')).to.be.undefined;
41+
expect(realm.getClientById('id')).toBeUndefined();
4142

4243
checkBrokenConnections.stop();
4344
});

test/services/messagesExpire/index.ts renamed to __test__/services/messagesExpire/index.spec.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
import { expect } from 'chai';
1+
import { describe, expect, it } from "@jest/globals";
2+
23
import { Client } from '../../../src/models/client';
34
import { Realm } from '../../../src/models/realm';
4-
import { IMessage } from '../../../src/models/message';
5+
import type { IMessage } from '../../../src/models/message';
56
import { MessagesExpire } from '../../../src/services/messagesExpire';
67
import { MessageHandler } from '../../../src/messageHandler';
78
import { MessageType } from '../../../src/enums';
@@ -33,11 +34,11 @@ describe('MessagesExpire', () => {
3334

3435
await wait(checkInterval * 2);
3536

36-
expect(realm.getMessageQueueById(client.getId())?.getMessages().length).to.be.eq(1);
37+
expect(realm.getMessageQueueById(client.getId())?.getMessages().length).toBe(1);
3738

3839
await wait(expireTimeout);
3940

40-
expect(realm.getMessageQueueById(client.getId())).to.be.undefined;
41+
expect(realm.getMessageQueueById(client.getId())).toBeUndefined();
4142

4243
messagesExpire.stopMessagesExpiration();
4344
});
@@ -59,8 +60,8 @@ describe('MessagesExpire', () => {
5960
let handledCount = 0;
6061

6162
messageHandler.handle = (client, message): boolean => {
62-
expect(client).to.be.undefined;
63-
expect(message.type).to.be.eq(MessageType.EXPIRE);
63+
expect(client).toBeUndefined();
64+
expect(message.type).toBe(MessageType.EXPIRE);
6465

6566
handledCount++;
6667

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

75-
expect(handledCount).to.be.eq(2);
76+
expect(handledCount).toBe(2);
7677

7778
messagesExpire.stopMessagesExpiration();
7879
});

0 commit comments

Comments
 (0)