test(voice): fix tests

This commit is contained in:
iCrawl
2022-01-11 21:53:08 +01:00
parent db25f529b2
commit 62c74b8333
19 changed files with 44 additions and 206 deletions

View File

@@ -1,170 +0,0 @@
/* eslint-disable @typescript-eslint/no-unsafe-return */
/* eslint-disable @typescript-eslint/no-empty-function */
/* eslint-disable @typescript-eslint/no-unused-vars */
import { createSocket as _createSocket } from 'node:dgram';
import EventEmitter, { once } from 'node:events';
import { VoiceUDPSocket } from '../VoiceUDPSocket';
jest.mock('node:dgram');
jest.useFakeTimers();
const createSocket = _createSocket as unknown as jest.Mock<typeof _createSocket>;
beforeEach(() => {
createSocket.mockReset();
});
class FakeSocket extends EventEmitter {
public send(buffer: Buffer, port: number, address: string) {}
public close() {
this.emit('close');
}
}
// ip = 91.90.123.93, port = 54148
const VALID_RESPONSE = Buffer.from([
0x0, 0x2, 0x0, 0x46, 0x0, 0x4, 0xeb, 0x23, 0x39, 0x31, 0x2e, 0x39, 0x30, 0x2e, 0x31, 0x32, 0x33, 0x2e, 0x39, 0x33,
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xd3, 0x84,
]);
function wait() {
return new Promise((resolve) => {
setImmediate(resolve);
jest.advanceTimersToNextTimer();
});
}
describe('VoiceUDPSocket#performIPDiscovery', () => {
let socket: VoiceUDPSocket;
afterEach(() => {
socket.destroy();
});
/*
Ensures that the UDP socket sends data and parses the response correctly
*/
test('Resolves and cleans up with a successful flow', async () => {
const fake = new FakeSocket();
fake.send = jest.fn().mockImplementation((buffer: Buffer, port: number, address: string) => {
fake.emit('message', VALID_RESPONSE);
});
createSocket.mockImplementation((type) => fake as any);
socket = new VoiceUDPSocket({ ip: '1.2.3.4', port: 25565 });
expect(createSocket).toHaveBeenCalledWith('udp4');
expect(fake.listenerCount('message')).toBe(1);
await expect(socket.performIPDiscovery(1234)).resolves.toEqual({
ip: '91.90.123.93',
port: 54148,
});
// Ensure clean up occurs
expect(fake.listenerCount('message')).toBe(1);
});
/*
In the case where an unrelated message is received before the IP discovery buffer,
the UDP socket should wait indefinitely until the correct buffer arrives.
*/
test('Waits for a valid response in an unexpected flow', async () => {
const fake = new FakeSocket();
const fakeResponse = Buffer.from([1, 2, 3, 4, 5]);
fake.send = jest.fn().mockImplementation(async (buffer: Buffer, port: number, address: string) => {
fake.emit('message', fakeResponse);
await wait();
fake.emit('message', VALID_RESPONSE);
});
createSocket.mockImplementation(() => fake as any);
socket = new VoiceUDPSocket({ ip: '1.2.3.4', port: 25565 });
expect(createSocket).toHaveBeenCalledWith('udp4');
expect(fake.listenerCount('message')).toBe(1);
await expect(socket.performIPDiscovery(1234)).resolves.toEqual({
ip: '91.90.123.93',
port: 54148,
});
// Ensure clean up occurs
expect(fake.listenerCount('message')).toBe(1);
});
test('Rejects if socket closes before IP discovery can be completed', async () => {
const fake = new FakeSocket();
fake.send = jest.fn().mockImplementation(async (buffer: Buffer, port: number, address: string) => {
await wait();
fake.close();
});
createSocket.mockImplementation(() => fake as any);
socket = new VoiceUDPSocket({ ip: '1.2.3.4', port: 25565 });
expect(createSocket).toHaveBeenCalledWith('udp4');
await expect(socket.performIPDiscovery(1234)).rejects.toThrowError();
});
test('Stays alive when messages are echoed back', async () => {
const fake = new FakeSocket();
fake.send = jest.fn().mockImplementation(async (buffer: Buffer) => {
await wait();
fake.emit('message', buffer);
});
createSocket.mockImplementation(() => fake as any);
socket = new VoiceUDPSocket({ ip: '1.2.3.4', port: 25565 });
let closed = false;
// @ts-expect-error
socket.on('close', () => (closed = true));
for (let i = 0; i < 30; i++) {
jest.advanceTimersToNextTimer();
await wait();
}
expect(closed).toBe(false);
});
test('Emits an error when no response received to keep alive messages', async () => {
const fake = new FakeSocket();
fake.send = jest.fn();
createSocket.mockImplementation(() => fake as any);
socket = new VoiceUDPSocket({ ip: '1.2.3.4', port: 25565 });
let closed = false;
// @ts-expect-error
socket.on('close', () => (closed = true));
for (let i = 0; i < 15; i++) {
jest.advanceTimersToNextTimer();
await wait();
}
expect(closed).toBe(true);
});
test('Recovers from intermittent responses', async () => {
const fake = new FakeSocket();
const fakeSend = jest.fn();
fake.send = fakeSend;
createSocket.mockImplementation(() => fake as any);
socket = new VoiceUDPSocket({ ip: '1.2.3.4', port: 25565 });
let closed = false;
// @ts-expect-error
socket.on('close', () => (closed = true));
for (let i = 0; i < 10; i++) {
jest.advanceTimersToNextTimer();
await wait();
}
fakeSend.mockImplementation(async (buffer: Buffer) => {
await wait();
fake.emit('message', buffer);
});
expect(closed).toBe(false);
for (let i = 0; i < 30; i++) {
jest.advanceTimersToNextTimer();
await wait();
}
expect(closed).toBe(false);
});
});

View File

@@ -1,123 +0,0 @@
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
import { VoiceOpcodes } from 'discord-api-types/voice/v4';
import EventEmitter, { once } from 'node:events';
import WS from 'jest-websocket-mock';
import { VoiceWebSocket } from '../VoiceWebSocket';
beforeEach(() => {
WS.clean();
});
function onceIgnoreError<T extends EventEmitter>(target: T, event: string) {
return new Promise((resolve) => {
target.on(event, resolve);
});
}
function onceOrThrow<T extends EventEmitter>(target: T, event: string, after: number) {
return new Promise((resolve, reject) => {
target.on(event, resolve);
setTimeout(() => reject(new Error('Time up')), after);
});
}
describe('VoiceWebSocket: packet parsing', () => {
test('Parses and emits packets', async () => {
const endpoint = 'ws://localhost:1234';
const server = new WS(endpoint, { jsonProtocol: true });
const ws = new VoiceWebSocket(endpoint, false);
await server.connected;
const dummy = { value: 3 };
const rcv = once(ws, 'packet');
server.send(dummy);
await expect(rcv).resolves.toEqual([dummy]);
});
test('Recovers from invalid packets', async () => {
const endpoint = 'ws://localhost:1234';
const server = new WS(endpoint);
const ws = new VoiceWebSocket(endpoint, false);
await server.connected;
let rcv = once(ws, 'packet');
server.send('asdf');
await expect(rcv).rejects.toThrowError();
const dummy = { op: 1234 };
rcv = once(ws, 'packet');
server.send(JSON.stringify(dummy));
await expect(rcv).resolves.toEqual([dummy]);
});
});
describe('VoiceWebSocket: event propagation', () => {
test('open', async () => {
const endpoint = 'ws://localhost:1234';
const server = new WS(endpoint);
const ws = new VoiceWebSocket(endpoint, false);
const rcv = once(ws, 'open');
await server.connected;
await expect(rcv).resolves.toBeTruthy();
});
test('close (clean)', async () => {
const endpoint = 'ws://localhost:1234';
const server = new WS(endpoint);
const ws = new VoiceWebSocket(endpoint, false);
await server.connected;
const rcv = once(ws, 'close');
server.close();
await expect(rcv).resolves.toBeTruthy();
});
test('close (error)', async () => {
const endpoint = 'ws://localhost:1234';
const server = new WS(endpoint);
const ws = new VoiceWebSocket(endpoint, false);
await server.connected;
const rcvError = once(ws, 'error');
const rcvClose = onceIgnoreError(ws, 'close');
server.error();
await expect(rcvError).resolves.toBeTruthy();
await expect(rcvClose).resolves.toBeTruthy();
});
});
describe('VoiceWebSocket: heartbeating', () => {
test('Normal heartbeat flow', async () => {
const endpoint = 'ws://localhost:1234';
const server = new WS(endpoint, { jsonProtocol: true });
const ws = new VoiceWebSocket(endpoint, false);
await server.connected;
const rcv = onceOrThrow(ws, 'close', 750);
ws.setHeartbeatInterval(50);
for (let i = 0; i < 10; i++) {
const packet: any = await server.nextMessage;
expect(packet).toMatchObject({
op: VoiceOpcodes.Heartbeat,
});
server.send({
op: VoiceOpcodes.HeartbeatAck,
d: packet.d,
});
expect(ws.ping).toBeGreaterThanOrEqual(0);
}
ws.setHeartbeatInterval(-1);
await expect(rcv).rejects.toThrowError();
});
test('Closes when no ack is received', async () => {
const endpoint = 'ws://localhost:1234';
const server = new WS(endpoint, { jsonProtocol: true });
const ws = new VoiceWebSocket(endpoint, false);
// eslint-disable-next-line @typescript-eslint/no-empty-function
ws.on('error', () => {});
await server.connected;
const rcv = onceIgnoreError(ws, 'close');
ws.setHeartbeatInterval(50);
await expect(rcv).resolves.toBeTruthy();
expect(ws.ping).toBe(undefined);
expect(server.messages.length).toBe(3);
});
});