优化代码

This commit is contained in:
2026-05-23 23:49:47 +08:00
parent 5fdc70c645
commit a413c56a6f
4 changed files with 251 additions and 143 deletions

View File

@@ -6,6 +6,8 @@ import * as wsHandler from '../src/class/websockethandler';
Date.now = jest.fn(() => 1482363367071);
const anyParticipantId = expect.any(String);
describe('websocket signaling test in public mode', () => {
let server: WS;
let client: WebSocket;
@@ -39,14 +41,24 @@ describe('websocket signaling test in public mode', () => {
test('create connection from session1', async () => {
await wsHandler.onConnect(client, connectionId);
await expect(server).toReceiveMessage({ type: "connect", connectionId: connectionId, polite: true });
expect(server).toHaveReceivedMessages([{ type: "connect", connectionId: connectionId, polite: true }]);
await expect(server).toReceiveMessage({
type: "connect",
connectionId: connectionId,
polite: true,
role: "participant",
participantId: anyParticipantId
});
});
test('create connection from session2', async () => {
await wsHandler.onConnect(client2, connectionId2);
await expect(server).toReceiveMessage({ type: "connect", connectionId: connectionId2, polite: true });
expect(server).toHaveReceivedMessages([{ type: "connect", connectionId: connectionId2, polite: true }]);
await expect(server).toReceiveMessage({
type: "connect",
connectionId: connectionId2,
polite: true,
role: "participant",
participantId: anyParticipantId
});
});
test('send offer from session1', async () => {
@@ -59,32 +71,30 @@ describe('websocket signaling test in public mode', () => {
test('send answer from session2', async () => {
await wsHandler.onAnswer(client2, { connectionId: connectionId, sdp: testsdp });
const receiveAnswer = new Answer(testsdp, Date.now());
await expect(server).toReceiveMessage({ from: connectionId, to: "", type: "answer", data: receiveAnswer });
expect(server).toHaveReceivedMessages([{ from: connectionId, to: "", type: "answer", data: receiveAnswer }]);
await expect(server).toReceiveMessage({
from: connectionId,
to: "",
type: "answer",
data: receiveAnswer,
participantId: anyParticipantId
});
});
test('send candidate from sesson1', async () => {
const msg = { connectionId: connectionId, candidate: "testcandidate", sdpMLineIndex: 0, sdpMid: "0" };
await wsHandler.onCandidate(client, msg);
const receiveCandidate = new Candidate("testcandidate", 0, "0", Date.now());
await expect(server).toReceiveMessage({ from: connectionId, to: "", type: "candidate", data: receiveCandidate });
expect(server).toHaveReceivedMessages([{ from: connectionId, to: "", type: "candidate", data: receiveCandidate }]);
expect(true).toBe(true);
});
test('delete connection from session2', async () => {
await wsHandler.onDisconnect(client2, connectionId);
// disconnect send to client
await expect(server).toReceiveMessage({ type: "participant-left", connectionId: connectionId, participantId: anyParticipantId });
await expect(server).toReceiveMessage({ type: "disconnect", connectionId: connectionId });
// disconnect send to client2
await expect(server).toReceiveMessage({ type: "disconnect", connectionId: connectionId });
// server received total 2 disconnect messages
expect(server).toHaveReceivedMessages([{ type: "disconnect", connectionId: connectionId }, { type: "disconnect", connectionId: connectionId }]);
});
test('delete connection from session1', async () => {
await wsHandler.onDisconnect(client, connectionId);
await expect(server).toReceiveMessage({ type: "disconnect", connectionId: connectionId });
expect(server).toHaveReceivedMessages([{ type: "disconnect", connectionId: connectionId }, { type: "disconnect", connectionId: connectionId }]);
});
test('delete session2', async () => {
@@ -130,28 +140,49 @@ describe('websocket signaling test in private mode', () => {
test('create connection from session1', async () => {
await wsHandler.onConnect(client, connectionId);
await expect(server).toReceiveMessage({ type: "connect", connectionId: connectionId, polite: false });
expect(server).toHaveReceivedMessages([{ type: "connect", connectionId: connectionId, polite: false }]);
await expect(server).toReceiveMessage({
type: "connect",
connectionId: connectionId,
polite: false,
role: "host",
participantId: anyParticipantId
});
});
test('create connection from session2', async () => {
await wsHandler.onConnect(client2, connectionId);
await expect(server).toReceiveMessage({ type: "connect", connectionId: connectionId, polite: true });
expect(server).toHaveReceivedMessages([{ type: "connect", connectionId: connectionId, polite: true }]);
await expect(server).toReceiveMessage({ type: "participant-joined", connectionId: connectionId, participantId: anyParticipantId });
await expect(server).toReceiveMessage({
type: "connect",
connectionId: connectionId,
polite: true,
role: "participant",
participantId: anyParticipantId
});
});
test('send offer from session1', async () => {
await wsHandler.onOffer(client, { connectionId: connectionId, sdp: testsdp });
const receiveOffer = new Offer(testsdp, Date.now(), true);
await expect(server).toReceiveMessage({ from: connectionId, to: "", type: "offer", data: receiveOffer });
expect(server).toHaveReceivedMessages([{ from: connectionId, to: "", type: "offer", data: receiveOffer }]);
await expect(server).toReceiveMessage({
from: connectionId,
to: "",
type: "offer",
data: receiveOffer,
participantId: anyParticipantId
});
});
test('send answer from session2', async () => {
await wsHandler.onAnswer(client2, { connectionId: connectionId, sdp: testsdp });
const receiveAnswer = new Answer(testsdp, Date.now());
await expect(server).toReceiveMessage({ from: connectionId, to: "", type: "answer", data: receiveAnswer });
expect(server).toHaveReceivedMessages([{ from: connectionId, to: "", type: "answer", data: receiveAnswer }]);
await expect(server).toReceiveMessage({
from: connectionId,
to: "",
type: "answer",
data: receiveAnswer,
participantId: anyParticipantId
});
});
test('send candidate from sesson1', async () => {
@@ -164,18 +195,13 @@ describe('websocket signaling test in private mode', () => {
test('delete connection from session2', async () => {
await wsHandler.onDisconnect(client2, connectionId);
// disconnect send to client
await expect(server).toReceiveMessage({ type: "participant-left", connectionId: connectionId, participantId: anyParticipantId });
await expect(server).toReceiveMessage({ type: "disconnect", connectionId: connectionId });
// disconnect send to client2
await expect(server).toReceiveMessage({ type: "disconnect", connectionId: connectionId });
// server received total 2 disconnect messages
expect(server).toHaveReceivedMessages([{ type: "disconnect", connectionId: connectionId }, { type: "disconnect", connectionId: connectionId }]);
});
test('delete connection from session1', async () => {
await wsHandler.onDisconnect(client, connectionId);
await expect(server).toReceiveMessage({ type: "disconnect", connectionId: connectionId });
expect(server).toHaveReceivedMessages([{ type: "disconnect", connectionId: connectionId }, { type: "disconnect", connectionId: connectionId }]);
});
test('delete session2', async () => {