mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-09 16:13:31 +01:00
test: fix type errors (#11325)
* test: fix type errors * chore: use MockedFunction --------- Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
This commit is contained in:
@@ -15,7 +15,7 @@ const mockRedisClient = {
|
|||||||
test('pubsub with custom encoding', async () => {
|
test('pubsub with custom encoding', async () => {
|
||||||
const encode = vi.fn((data) => data);
|
const encode = vi.fn((data) => data);
|
||||||
|
|
||||||
const broker = new PubSubRedisBroker(mockRedisClient, { encode, group: 'group' });
|
const broker = new PubSubRedisBroker(mockRedisClient, { encode, name: 'yeet', group: 'group' });
|
||||||
await broker.publish('test', 'test');
|
await broker.publish('test', 'test');
|
||||||
expect(encode).toHaveBeenCalledWith('test');
|
expect(encode).toHaveBeenCalledWith('test');
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
/* eslint-disable id-length */
|
/* eslint-disable id-length */
|
||||||
/* eslint-disable promise/prefer-await-to-then */
|
/* eslint-disable promise/prefer-await-to-then */
|
||||||
// @ts-nocheck
|
|
||||||
import { performance } from 'node:perf_hooks';
|
import { performance } from 'node:perf_hooks';
|
||||||
import { MockAgent, setGlobalDispatcher } from 'undici';
|
import { MockAgent, setGlobalDispatcher } from 'undici';
|
||||||
import type { Interceptable, MockInterceptor } from 'undici/types/mock-interceptor';
|
import type { Interceptable, MockInterceptor } from 'undici/types/mock-interceptor';
|
||||||
@@ -137,7 +136,7 @@ test('Handle unexpected 429', async () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
expect(await unexpectedLimit).toStrictEqual({ test: true });
|
expect(await unexpectedLimit).toStrictEqual({ test: true });
|
||||||
expect(performance.now()).toBeGreaterThanOrEqual(previous + 1_000);
|
expect(firstResolvedTime!).toBeGreaterThanOrEqual(previous + 1_000);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('server responding too slow', async () => {
|
test('server responding too slow', async () => {
|
||||||
|
|||||||
@@ -1,17 +1,15 @@
|
|||||||
import { Buffer, File as NativeFile } from 'node:buffer';
|
import { Buffer, File } from 'node:buffer';
|
||||||
import { URLSearchParams } from 'node:url';
|
import { URLSearchParams } from 'node:url';
|
||||||
import { DiscordSnowflake } from '@sapphire/snowflake';
|
import { DiscordSnowflake } from '@sapphire/snowflake';
|
||||||
import type { Snowflake } from 'discord-api-types/v10';
|
import type { Snowflake } from 'discord-api-types/v10';
|
||||||
import { Routes } from 'discord-api-types/v10';
|
import { Routes } from 'discord-api-types/v10';
|
||||||
import { type FormData, fetch } from 'undici';
|
import { type FormData, fetch } from 'undici';
|
||||||
import { File as UndiciFile, MockAgent, setGlobalDispatcher } from 'undici';
|
import { MockAgent, setGlobalDispatcher } from 'undici';
|
||||||
import type { Interceptable, MockInterceptor } from 'undici/types/mock-interceptor.js';
|
import type { Interceptable, MockInterceptor } from 'undici/types/mock-interceptor.js';
|
||||||
import { beforeEach, afterEach, test, expect, vitest } from 'vitest';
|
import { beforeEach, afterEach, test, expect, vitest } from 'vitest';
|
||||||
import { REST } from '../src/index.js';
|
import { REST } from '../src/index.js';
|
||||||
import { genPath } from './util.js';
|
import { genPath } from './util.js';
|
||||||
|
|
||||||
const File = NativeFile ?? UndiciFile;
|
|
||||||
|
|
||||||
const newSnowflake: Snowflake = DiscordSnowflake.generate().toString();
|
const newSnowflake: Snowflake = DiscordSnowflake.generate().toString();
|
||||||
|
|
||||||
const api = new REST().setToken('A-Very-Fake-Token');
|
const api = new REST().setToken('A-Very-Fake-Token');
|
||||||
|
|||||||
@@ -82,7 +82,7 @@ test('resolveBody', async () => {
|
|||||||
const fd = new globalThis.FormData();
|
const fd = new globalThis.FormData();
|
||||||
fd.append('key', 'value');
|
fd.append('key', 'value');
|
||||||
|
|
||||||
const resolved = await resolveBody(fd);
|
const resolved = await resolveBody(fd as UndiciFormData);
|
||||||
|
|
||||||
expect(resolved).toBeInstanceOf(UndiciFormData);
|
expect(resolved).toBeInstanceOf(UndiciFormData);
|
||||||
expect([...(resolved as UndiciFormData).entries()]).toStrictEqual([['key', 'value']]);
|
expect([...(resolved as UndiciFormData).entries()]).toStrictEqual([['key', 'value']]);
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ import { Buffer } from 'node:buffer';
|
|||||||
import { once } from 'node:events';
|
import { once } from 'node:events';
|
||||||
import process from 'node:process';
|
import process from 'node:process';
|
||||||
import { Readable } from 'node:stream';
|
import { Readable } from 'node:stream';
|
||||||
import { describe, test, expect, vitest, type Mock, beforeEach, afterEach } from 'vitest';
|
import { describe, test, expect, vitest, type MockedFunction, beforeEach, afterEach } from 'vitest';
|
||||||
import { addAudioPlayer, deleteAudioPlayer } from '../src/DataStore';
|
import { addAudioPlayer, deleteAudioPlayer } from '../src/DataStore';
|
||||||
import { VoiceConnection, VoiceConnectionStatus } from '../src/VoiceConnection';
|
import { VoiceConnection, VoiceConnectionStatus } from '../src/VoiceConnection';
|
||||||
import type { AudioPlayer } from '../src/audio/AudioPlayer';
|
import type { AudioPlayer } from '../src/audio/AudioPlayer';
|
||||||
@@ -252,7 +252,9 @@ describe('State transitions', () => {
|
|||||||
expect(connection.dispatchAudio).toHaveBeenCalledTimes(6);
|
expect(connection.dispatchAudio).toHaveBeenCalledTimes(6);
|
||||||
await wait();
|
await wait();
|
||||||
player['_stepPrepare']();
|
player['_stepPrepare']();
|
||||||
const prepareAudioPacket = connection.prepareAudioPacket as unknown as Mock<typeof connection.prepareAudioPacket>;
|
const prepareAudioPacket = connection.prepareAudioPacket as unknown as MockedFunction<
|
||||||
|
typeof connection.prepareAudioPacket
|
||||||
|
>;
|
||||||
expect(prepareAudioPacket).toHaveBeenCalledTimes(6);
|
expect(prepareAudioPacket).toHaveBeenCalledTimes(6);
|
||||||
expect(prepareAudioPacket.mock.calls[5]![0]).toEqual(silence().next().value);
|
expect(prepareAudioPacket.mock.calls[5]![0]).toEqual(silence().next().value);
|
||||||
|
|
||||||
@@ -307,7 +309,9 @@ describe('State transitions', () => {
|
|||||||
|
|
||||||
await wait();
|
await wait();
|
||||||
expect(player.checkPlayable()).toEqual(false);
|
expect(player.checkPlayable()).toEqual(false);
|
||||||
const prepareAudioPacket = connection.prepareAudioPacket as unknown as Mock<typeof connection.prepareAudioPacket>;
|
const prepareAudioPacket = connection.prepareAudioPacket as unknown as MockedFunction<
|
||||||
|
typeof connection.prepareAudioPacket
|
||||||
|
>;
|
||||||
expect(prepareAudioPacket).toHaveBeenCalledTimes(5);
|
expect(prepareAudioPacket).toHaveBeenCalledTimes(5);
|
||||||
|
|
||||||
expect(player.state.status).toEqual(AudioPlayerStatus.Idle);
|
expect(player.state.status).toEqual(AudioPlayerStatus.Idle);
|
||||||
@@ -338,7 +342,9 @@ describe('State transitions', () => {
|
|||||||
expect(addAudioPlayer).toBeCalledTimes(1);
|
expect(addAudioPlayer).toBeCalledTimes(1);
|
||||||
expect(player.checkPlayable()).toEqual(true);
|
expect(player.checkPlayable()).toEqual(true);
|
||||||
|
|
||||||
const prepareAudioPacket = connection.prepareAudioPacket as unknown as Mock<typeof connection.prepareAudioPacket>;
|
const prepareAudioPacket = connection.prepareAudioPacket as unknown as MockedFunction<
|
||||||
|
typeof connection.prepareAudioPacket
|
||||||
|
>;
|
||||||
|
|
||||||
// Run through a few packet cycles
|
// Run through a few packet cycles
|
||||||
for (let index = 1; index <= 5; index++) {
|
for (let index = 1; index <= 5; index++) {
|
||||||
|
|||||||
@@ -1,4 +1,3 @@
|
|||||||
// @ts-nocheck
|
|
||||||
import { describe, test, expect } from 'vitest';
|
import { describe, test, expect } from 'vitest';
|
||||||
import { findPipeline, StreamType, TransformerType, type Edge } from '../src/audio/TransformerGraph';
|
import { findPipeline, StreamType, TransformerType, type Edge } from '../src/audio/TransformerGraph';
|
||||||
|
|
||||||
@@ -10,12 +9,12 @@ const noConstraint = () => true;
|
|||||||
* @param pipeline - The pipeline of edges returned by findPipeline(...)
|
* @param pipeline - The pipeline of edges returned by findPipeline(...)
|
||||||
*/
|
*/
|
||||||
function reducePath(pipeline: Edge[]) {
|
function reducePath(pipeline: Edge[]) {
|
||||||
const streams = [pipeline[0].from.type];
|
const streams = [pipeline[0]!.from.type];
|
||||||
for (const edge of pipeline.slice(1)) {
|
for (const edge of pipeline.slice(1)) {
|
||||||
streams.push(edge.from.type);
|
streams.push(edge.from.type);
|
||||||
}
|
}
|
||||||
|
|
||||||
streams.push(pipeline[pipeline.length - 1].to.type);
|
streams.push(pipeline[pipeline.length - 1]!.to.type);
|
||||||
return streams;
|
return streams;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
/* eslint-disable @typescript-eslint/unbound-method */
|
/* eslint-disable @typescript-eslint/unbound-method */
|
||||||
/* eslint-disable @typescript-eslint/dot-notation */
|
/* eslint-disable @typescript-eslint/dot-notation */
|
||||||
// @ts-nocheck
|
|
||||||
import { EventEmitter } from 'node:events';
|
import { EventEmitter } from 'node:events';
|
||||||
|
import type { Mocked } from 'vitest';
|
||||||
import { vitest, describe, test, expect, beforeEach } from 'vitest';
|
import { vitest, describe, test, expect, beforeEach } from 'vitest';
|
||||||
import * as _DataStore from '../src/DataStore';
|
import * as _DataStore from '../src/DataStore';
|
||||||
import {
|
import {
|
||||||
@@ -25,16 +25,16 @@ vitest.mock('../src/networking/Networking', async (importOriginal) => {
|
|||||||
// eslint-disable-next-line @typescript-eslint/consistent-type-imports
|
// eslint-disable-next-line @typescript-eslint/consistent-type-imports
|
||||||
const actual = await importOriginal<typeof import('../src/networking/Networking')>();
|
const actual = await importOriginal<typeof import('../src/networking/Networking')>();
|
||||||
const Networking = actual.Networking;
|
const Networking = actual.Networking;
|
||||||
Networking.prototype.createWebSocket = vitest.fn();
|
Networking.prototype['createWebSocket'] = vitest.fn();
|
||||||
return {
|
return {
|
||||||
...actual,
|
...actual,
|
||||||
Networking,
|
Networking,
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
const DataStore = _DataStore as unknown as vitest.Mocked<typeof _DataStore>;
|
const DataStore = _DataStore as unknown as Mocked<typeof _DataStore>;
|
||||||
const AudioPlayer = _AudioPlayer as unknown as vitest.Mocked<typeof _AudioPlayer>;
|
const AudioPlayer = _AudioPlayer as unknown as Mocked<typeof _AudioPlayer>;
|
||||||
const PlayerSubscription = _PlayerSubscription as unknown as vitest.Mock<_PlayerSubscription>;
|
const PlayerSubscription = _PlayerSubscription as unknown as Mocked<typeof _PlayerSubscription>;
|
||||||
|
|
||||||
const _NetworkingClass = Networking.Networking;
|
const _NetworkingClass = Networking.Networking;
|
||||||
vitest.spyOn(Networking, 'Networking').mockImplementation((...args) => new _NetworkingClass(...args));
|
vitest.spyOn(Networking, 'Networking').mockImplementation((...args) => new _NetworkingClass(...args));
|
||||||
@@ -133,9 +133,10 @@ describe('createVoiceConnection', () => {
|
|||||||
|
|
||||||
const stateSetter = vitest.spyOn(existingVoiceConnection, 'state', 'set');
|
const stateSetter = vitest.spyOn(existingVoiceConnection, 'state', 'set');
|
||||||
|
|
||||||
// @ts-expect-error: We're testing
|
|
||||||
DataStore.getVoiceConnection.mockImplementation((guildId, group = 'default') =>
|
DataStore.getVoiceConnection.mockImplementation((guildId, group = 'default') =>
|
||||||
guildId === existingJoinConfig.guildId && group === existingJoinConfig.group ? existingVoiceConnection : null,
|
guildId === existingJoinConfig.guildId && group === existingJoinConfig.group
|
||||||
|
? existingVoiceConnection
|
||||||
|
: undefined,
|
||||||
);
|
);
|
||||||
|
|
||||||
const newAdapter = createFakeAdapter();
|
const newAdapter = createFakeAdapter();
|
||||||
@@ -172,9 +173,10 @@ describe('createVoiceConnection', () => {
|
|||||||
|
|
||||||
const rejoinSpy = vitest.spyOn(existingVoiceConnection, 'rejoin');
|
const rejoinSpy = vitest.spyOn(existingVoiceConnection, 'rejoin');
|
||||||
|
|
||||||
// @ts-expect-error: We're testing
|
|
||||||
DataStore.getVoiceConnection.mockImplementation((guildId, group = 'default') =>
|
DataStore.getVoiceConnection.mockImplementation((guildId, group = 'default') =>
|
||||||
guildId === existingJoinConfig.guildId && group === existingJoinConfig.group ? existingVoiceConnection : null,
|
guildId === existingJoinConfig.guildId && group === existingJoinConfig.group
|
||||||
|
? existingVoiceConnection
|
||||||
|
: undefined,
|
||||||
);
|
);
|
||||||
|
|
||||||
const newAdapter = createFakeAdapter();
|
const newAdapter = createFakeAdapter();
|
||||||
@@ -204,9 +206,10 @@ describe('createVoiceConnection', () => {
|
|||||||
adapterCreator: existingAdapter.creator,
|
adapterCreator: existingAdapter.creator,
|
||||||
});
|
});
|
||||||
|
|
||||||
// @ts-expect-error: We're testing
|
|
||||||
DataStore.getVoiceConnection.mockImplementation((guildId, group = 'default') =>
|
DataStore.getVoiceConnection.mockImplementation((guildId, group = 'default') =>
|
||||||
guildId === existingJoinConfig.guildId && group === existingJoinConfig.group ? existingVoiceConnection : null,
|
guildId === existingJoinConfig.guildId && group === existingJoinConfig.group
|
||||||
|
? existingVoiceConnection
|
||||||
|
: undefined,
|
||||||
);
|
);
|
||||||
|
|
||||||
const newAdapter = createFakeAdapter();
|
const newAdapter = createFakeAdapter();
|
||||||
@@ -444,7 +447,7 @@ describe('VoiceConnection#onNetworkingStateChange', () => {
|
|||||||
voiceConnection['_state'] = {
|
voiceConnection['_state'] = {
|
||||||
...(voiceConnection.state as VoiceConnectionSignallingState),
|
...(voiceConnection.state as VoiceConnectionSignallingState),
|
||||||
status: VoiceConnectionStatus.Connecting,
|
status: VoiceConnectionStatus.Connecting,
|
||||||
networking: new Networking.Networking({} as any, false),
|
networking: new Networking.Networking({} as any, {}),
|
||||||
};
|
};
|
||||||
|
|
||||||
voiceConnection['onNetworkingStateChange'](
|
voiceConnection['onNetworkingStateChange'](
|
||||||
@@ -462,7 +465,7 @@ describe('VoiceConnection#onNetworkingStateChange', () => {
|
|||||||
voiceConnection['_state'] = {
|
voiceConnection['_state'] = {
|
||||||
...(voiceConnection.state as VoiceConnectionSignallingState),
|
...(voiceConnection.state as VoiceConnectionSignallingState),
|
||||||
status: VoiceConnectionStatus.Connecting,
|
status: VoiceConnectionStatus.Connecting,
|
||||||
networking: new Networking.Networking({} as any, false),
|
networking: new Networking.Networking({} as any, {}),
|
||||||
};
|
};
|
||||||
|
|
||||||
voiceConnection['onNetworkingStateChange'](
|
voiceConnection['onNetworkingStateChange'](
|
||||||
@@ -492,7 +495,7 @@ describe('VoiceConnection#destroy', () => {
|
|||||||
voiceConnection.destroy();
|
voiceConnection.destroy();
|
||||||
expect(DataStore.getVoiceConnection).toHaveReturnedWith(voiceConnection);
|
expect(DataStore.getVoiceConnection).toHaveReturnedWith(voiceConnection);
|
||||||
expect(DataStore.untrackVoiceConnection).toHaveBeenCalledWith(voiceConnection);
|
expect(DataStore.untrackVoiceConnection).toHaveBeenCalledWith(voiceConnection);
|
||||||
expect(DataStore.createJoinVoiceChannelPayload.mock.calls[0][0]).toMatchObject({
|
expect(DataStore.createJoinVoiceChannelPayload.mock.calls[0]?.[0]).toMatchObject({
|
||||||
channelId: null,
|
channelId: null,
|
||||||
guildId: joinConfig.guildId,
|
guildId: joinConfig.guildId,
|
||||||
});
|
});
|
||||||
@@ -518,7 +521,7 @@ describe('VoiceConnection#disconnect', () => {
|
|||||||
voiceConnection.state = {
|
voiceConnection.state = {
|
||||||
status: VoiceConnectionStatus.Ready,
|
status: VoiceConnectionStatus.Ready,
|
||||||
adapter,
|
adapter,
|
||||||
networking: new Networking.Networking({} as any, false),
|
networking: new Networking.Networking({} as any, {}),
|
||||||
};
|
};
|
||||||
const leavePayload = Symbol('dummy');
|
const leavePayload = Symbol('dummy');
|
||||||
DataStore.createJoinVoiceChannelPayload.mockImplementation(() => leavePayload as any);
|
DataStore.createJoinVoiceChannelPayload.mockImplementation(() => leavePayload as any);
|
||||||
@@ -542,7 +545,7 @@ describe('VoiceConnection#disconnect', () => {
|
|||||||
voiceConnection.state = {
|
voiceConnection.state = {
|
||||||
status: VoiceConnectionStatus.Ready,
|
status: VoiceConnectionStatus.Ready,
|
||||||
adapter,
|
adapter,
|
||||||
networking: new Networking.Networking({} as any, false),
|
networking: new Networking.Networking({} as any, {}),
|
||||||
};
|
};
|
||||||
adapter.sendPayload.mockImplementation(() => false);
|
adapter.sendPayload.mockImplementation(() => false);
|
||||||
expect(voiceConnection.disconnect()).toEqual(false);
|
expect(voiceConnection.disconnect()).toEqual(false);
|
||||||
@@ -676,7 +679,7 @@ describe('VoiceConnection#onSubscriptionRemoved', () => {
|
|||||||
// Arrange
|
// Arrange
|
||||||
const ws = new EventEmitter() as any;
|
const ws = new EventEmitter() as any;
|
||||||
|
|
||||||
const oldNetworking = new Networking.Networking({} as any, false);
|
const oldNetworking = new Networking.Networking({} as any, {});
|
||||||
oldNetworking.state = {
|
oldNetworking.state = {
|
||||||
code: Networking.NetworkingStatusCode.Ready,
|
code: Networking.NetworkingStatusCode.Ready,
|
||||||
connectionData: {} as any,
|
connectionData: {} as any,
|
||||||
@@ -685,7 +688,7 @@ describe('VoiceConnection#onSubscriptionRemoved', () => {
|
|||||||
ws,
|
ws,
|
||||||
};
|
};
|
||||||
|
|
||||||
const newNetworking = new Networking.Networking({} as any, false);
|
const newNetworking = new Networking.Networking({} as any, {});
|
||||||
newNetworking.state = {
|
newNetworking.state = {
|
||||||
...oldNetworking.state,
|
...oldNetworking.state,
|
||||||
udp: new EventEmitter() as any,
|
udp: new EventEmitter() as any,
|
||||||
@@ -706,7 +709,7 @@ describe('VoiceConnection#onSubscriptionRemoved', () => {
|
|||||||
// Arrange
|
// Arrange
|
||||||
const udp = new EventEmitter() as any;
|
const udp = new EventEmitter() as any;
|
||||||
|
|
||||||
const oldNetworking = new Networking.Networking({} as any, false);
|
const oldNetworking = new Networking.Networking({} as any, {});
|
||||||
oldNetworking.state = {
|
oldNetworking.state = {
|
||||||
code: Networking.NetworkingStatusCode.Ready,
|
code: Networking.NetworkingStatusCode.Ready,
|
||||||
connectionData: {} as any,
|
connectionData: {} as any,
|
||||||
@@ -715,7 +718,7 @@ describe('VoiceConnection#onSubscriptionRemoved', () => {
|
|||||||
ws: new EventEmitter() as any,
|
ws: new EventEmitter() as any,
|
||||||
};
|
};
|
||||||
|
|
||||||
const newNetworking = new Networking.Networking({} as any, false);
|
const newNetworking = new Networking.Networking({} as any, {});
|
||||||
newNetworking.state = {
|
newNetworking.state = {
|
||||||
...oldNetworking.state,
|
...oldNetworking.state,
|
||||||
ws: new EventEmitter() as any,
|
ws: new EventEmitter() as any,
|
||||||
@@ -735,7 +738,7 @@ describe('VoiceConnection#onSubscriptionRemoved', () => {
|
|||||||
test('Applies initial listeners', () => {
|
test('Applies initial listeners', () => {
|
||||||
// Arrange
|
// Arrange
|
||||||
|
|
||||||
const newNetworking = new Networking.Networking({} as any, false);
|
const newNetworking = new Networking.Networking({} as any, {});
|
||||||
newNetworking.state = {
|
newNetworking.state = {
|
||||||
code: Networking.NetworkingStatusCode.Ready,
|
code: Networking.NetworkingStatusCode.Ready,
|
||||||
connectionData: {} as any,
|
connectionData: {} as any,
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
/* eslint-disable id-length */
|
/* eslint-disable id-length */
|
||||||
/* eslint-disable @typescript-eslint/dot-notation */
|
/* eslint-disable @typescript-eslint/dot-notation */
|
||||||
// @ts-nocheck
|
|
||||||
import { Buffer } from 'node:buffer';
|
import { Buffer } from 'node:buffer';
|
||||||
import { once } from 'node:events';
|
import { once } from 'node:events';
|
||||||
import process from 'node:process';
|
import process from 'node:process';
|
||||||
@@ -15,7 +14,6 @@ import {
|
|||||||
} from '../__mocks__/rtp';
|
} from '../__mocks__/rtp';
|
||||||
import { VoiceConnection, VoiceConnectionStatus } from '../src/VoiceConnection';
|
import { VoiceConnection, VoiceConnectionStatus } from '../src/VoiceConnection';
|
||||||
import { VoiceReceiver } from '../src/receive/VoiceReceiver';
|
import { VoiceReceiver } from '../src/receive/VoiceReceiver';
|
||||||
import { methods } from '../src/util/Secretbox';
|
|
||||||
|
|
||||||
vitest.mock('../src/VoiceConnection', async (importOriginal) => {
|
vitest.mock('../src/VoiceConnection', async (importOriginal) => {
|
||||||
// eslint-disable-next-line @typescript-eslint/consistent-type-imports
|
// eslint-disable-next-line @typescript-eslint/consistent-type-imports
|
||||||
@@ -33,18 +31,8 @@ async function nextTick() {
|
|||||||
return new Promise((resolve) => process.nextTick(resolve));
|
return new Promise((resolve) => process.nextTick(resolve));
|
||||||
}
|
}
|
||||||
|
|
||||||
function* rangeIter(start: number, end: number) {
|
|
||||||
for (let i = start; i <= end; i++) {
|
|
||||||
yield i;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function range(start: number, end: number) {
|
|
||||||
return Buffer.from([...rangeIter(start, end)]);
|
|
||||||
}
|
|
||||||
|
|
||||||
describe('VoiceReceiver', () => {
|
describe('VoiceReceiver', () => {
|
||||||
let voiceConnection: _VoiceConnection;
|
let voiceConnection: VoiceConnection;
|
||||||
let receiver: VoiceReceiver;
|
let receiver: VoiceReceiver;
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
@@ -64,7 +52,7 @@ describe('VoiceReceiver', () => {
|
|||||||
['RTP Packet Desktop', RTP_PACKET_DESKTOP],
|
['RTP Packet Desktop', RTP_PACKET_DESKTOP],
|
||||||
['RTP Packet Chrome', RTP_PACKET_CHROME],
|
['RTP Packet Chrome', RTP_PACKET_CHROME],
|
||||||
['RTP Packet Android', RTP_PACKET_ANDROID],
|
['RTP Packet Android', RTP_PACKET_ANDROID],
|
||||||
])('onUdpMessage: decrypt from %s', async (testName, RTP_PACKET) => {
|
])('onUdpMessage: decrypt from %s', async (_testName, RTP_PACKET) => {
|
||||||
receiver['decrypt'] = vitest.fn().mockImplementationOnce(() => RTP_PACKET.decrypted);
|
receiver['decrypt'] = vitest.fn().mockImplementationOnce(() => RTP_PACKET.decrypted);
|
||||||
|
|
||||||
const spy = vitest.spyOn(receiver.ssrcMap, 'get');
|
const spy = vitest.spyOn(receiver.ssrcMap, 'get');
|
||||||
@@ -144,8 +132,6 @@ describe('VoiceReceiver', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
describe('decrypt', () => {
|
describe('decrypt', () => {
|
||||||
const secretKey = new Uint8Array([1, 2, 3, 4]);
|
|
||||||
|
|
||||||
test('decrypt: aead_xchacha20_poly1305_rtpsize', () => {
|
test('decrypt: aead_xchacha20_poly1305_rtpsize', () => {
|
||||||
const nonceSpace = Buffer.alloc(24);
|
const nonceSpace = Buffer.alloc(24);
|
||||||
|
|
||||||
|
|||||||
@@ -1,21 +1,21 @@
|
|||||||
// @ts-nocheck
|
|
||||||
import { Buffer } from 'node:buffer';
|
import { Buffer } from 'node:buffer';
|
||||||
import { createSocket as _createSocket } from 'node:dgram';
|
import { createSocket as _createSocket } from 'node:dgram';
|
||||||
import { EventEmitter } from 'node:events';
|
import { EventEmitter } from 'node:events';
|
||||||
|
import type { MockedFunction } from 'vitest';
|
||||||
import { describe, test, expect, vitest, beforeEach, afterEach } from 'vitest';
|
import { describe, test, expect, vitest, beforeEach, afterEach } from 'vitest';
|
||||||
import { VoiceUDPSocket } from '../src/networking/VoiceUDPSocket';
|
import { VoiceUDPSocket } from '../src/networking/VoiceUDPSocket';
|
||||||
|
|
||||||
vitest.mock('node:dgram');
|
vitest.mock('node:dgram');
|
||||||
vitest.useFakeTimers();
|
vitest.useFakeTimers();
|
||||||
|
|
||||||
const createSocket = _createSocket as unknown as vitest.Mock<typeof _createSocket>;
|
const createSocket = _createSocket as unknown as MockedFunction<typeof _createSocket>;
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
createSocket.mockReset();
|
createSocket.mockReset();
|
||||||
});
|
});
|
||||||
|
|
||||||
class FakeSocket extends EventEmitter {
|
class FakeSocket extends EventEmitter {
|
||||||
public send(buffer: Buffer, port: number, address: string) {}
|
public send(_buffer: Buffer, _port: number, _address: string) {}
|
||||||
|
|
||||||
public close() {
|
public close() {
|
||||||
this.emit('close');
|
this.emit('close');
|
||||||
@@ -49,10 +49,10 @@ describe('VoiceUDPSocket#performIPDiscovery', () => {
|
|||||||
*/
|
*/
|
||||||
test('Resolves and cleans up with a successful flow', async () => {
|
test('Resolves and cleans up with a successful flow', async () => {
|
||||||
const fake = new FakeSocket();
|
const fake = new FakeSocket();
|
||||||
fake.send = vitest.fn().mockImplementation((buffer: Buffer, port: number, address: string) => {
|
fake.send = vitest.fn().mockImplementation((_buffer: Buffer, _port: number, _address: string) => {
|
||||||
fake.emit('message', VALID_RESPONSE);
|
fake.emit('message', VALID_RESPONSE);
|
||||||
});
|
});
|
||||||
createSocket.mockImplementation((type) => fake as any);
|
createSocket.mockImplementation((_type) => fake as any);
|
||||||
socket = new VoiceUDPSocket({ ip: '1.2.3.4', port: 25_565 });
|
socket = new VoiceUDPSocket({ ip: '1.2.3.4', port: 25_565 });
|
||||||
|
|
||||||
expect(createSocket).toHaveBeenCalledWith('udp4');
|
expect(createSocket).toHaveBeenCalledWith('udp4');
|
||||||
@@ -72,7 +72,7 @@ describe('VoiceUDPSocket#performIPDiscovery', () => {
|
|||||||
test('Waits for a valid response in an unexpected flow', async () => {
|
test('Waits for a valid response in an unexpected flow', async () => {
|
||||||
const fake = new FakeSocket();
|
const fake = new FakeSocket();
|
||||||
const fakeResponse = Buffer.from([1, 2, 3, 4, 5]);
|
const fakeResponse = Buffer.from([1, 2, 3, 4, 5]);
|
||||||
fake.send = vitest.fn().mockImplementation(async (buffer: Buffer, port: number, address: string) => {
|
fake.send = vitest.fn().mockImplementation(async (_buffer: Buffer, _port: number, _address: string) => {
|
||||||
fake.emit('message', fakeResponse);
|
fake.emit('message', fakeResponse);
|
||||||
await wait();
|
await wait();
|
||||||
fake.emit('message', VALID_RESPONSE);
|
fake.emit('message', VALID_RESPONSE);
|
||||||
@@ -92,7 +92,7 @@ describe('VoiceUDPSocket#performIPDiscovery', () => {
|
|||||||
|
|
||||||
test('Rejects if socket closes before IP discovery can be completed', async () => {
|
test('Rejects if socket closes before IP discovery can be completed', async () => {
|
||||||
const fake = new FakeSocket();
|
const fake = new FakeSocket();
|
||||||
fake.send = vitest.fn().mockImplementation(async (buffer: Buffer, port: number, address: string) => {
|
fake.send = vitest.fn().mockImplementation(async (_buffer: Buffer, _port: number, _address: string) => {
|
||||||
await wait();
|
await wait();
|
||||||
fake.close();
|
fake.close();
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -1,17 +1,16 @@
|
|||||||
// @ts-nocheck
|
|
||||||
import { Buffer } from 'node:buffer';
|
import { Buffer } from 'node:buffer';
|
||||||
import EventEmitter, { once } from 'node:events';
|
import EventEmitter, { once } from 'node:events';
|
||||||
import process from 'node:process';
|
import process from 'node:process';
|
||||||
import { Readable } from 'node:stream';
|
import { Readable } from 'node:stream';
|
||||||
import { opus as _opus } from 'prism-media';
|
import { opus as _opus } from 'prism-media';
|
||||||
import { describe, test, expect, vitest, type Mock, beforeAll, beforeEach } from 'vitest';
|
import { describe, test, expect, vitest, type Mock, beforeAll, beforeEach, type Mocked } from 'vitest';
|
||||||
import { StreamType } from '../src/audio/index';
|
import { StreamType } from '../src/audio/index';
|
||||||
import { demuxProbe } from '../src/util/demuxProbe';
|
import { demuxProbe } from '../src/util/demuxProbe';
|
||||||
|
|
||||||
vitest.mock('prism-media');
|
vitest.mock('prism-media');
|
||||||
|
|
||||||
const WebmDemuxer = _opus.WebmDemuxer as unknown as Mock<_opus.WebmDemuxer>;
|
const WebmDemuxer = _opus.WebmDemuxer as unknown as Mocked<typeof _opus.WebmDemuxer>;
|
||||||
const OggDemuxer = _opus.OggDemuxer as unknown as Mock<_opus.OggDemuxer>;
|
const OggDemuxer = _opus.OggDemuxer as unknown as Mocked<typeof _opus.OggDemuxer>;
|
||||||
|
|
||||||
async function nextTick() {
|
async function nextTick() {
|
||||||
// eslint-disable-next-line no-promise-executor-return
|
// eslint-disable-next-line no-promise-executor-return
|
||||||
@@ -52,12 +51,12 @@ describe('demuxProbe', () => {
|
|||||||
const oggWrite: Mock<(buffer: Buffer) => void> = vitest.fn();
|
const oggWrite: Mock<(buffer: Buffer) => void> = vitest.fn();
|
||||||
|
|
||||||
beforeAll(() => {
|
beforeAll(() => {
|
||||||
WebmDemuxer.prototype = {
|
(WebmDemuxer as any).prototype = {
|
||||||
...WebmDemuxer,
|
...WebmDemuxer,
|
||||||
...EventEmitter.prototype,
|
...EventEmitter.prototype,
|
||||||
write: webmWrite,
|
write: webmWrite,
|
||||||
};
|
};
|
||||||
OggDemuxer.prototype = {
|
(OggDemuxer as any).prototype = {
|
||||||
...OggDemuxer,
|
...OggDemuxer,
|
||||||
...EventEmitter.prototype,
|
...EventEmitter.prototype,
|
||||||
write: oggWrite,
|
write: oggWrite,
|
||||||
@@ -78,9 +77,9 @@ describe('demuxProbe', () => {
|
|||||||
|
|
||||||
test('Detects WebM', async () => {
|
test('Detects WebM', async () => {
|
||||||
const stream = Readable.from(gen(10), { objectMode: false });
|
const stream = Readable.from(gen(10), { objectMode: false });
|
||||||
webmWrite.mockImplementation(function mock(data: Buffer) {
|
webmWrite.mockImplementation(function mock(this: EventEmitter, data: Buffer) {
|
||||||
if (data[0] === 5) this.emit('head', validHead);
|
if (data[0] === 5) this.emit('head', validHead);
|
||||||
} as any);
|
});
|
||||||
const probe = await demuxProbe(stream);
|
const probe = await demuxProbe(stream);
|
||||||
expect(probe.type).toEqual(StreamType.WebmOpus);
|
expect(probe.type).toEqual(StreamType.WebmOpus);
|
||||||
await expect(collectStream(probe.stream)).resolves.toEqual(range(10));
|
await expect(collectStream(probe.stream)).resolves.toEqual(range(10));
|
||||||
@@ -88,9 +87,9 @@ describe('demuxProbe', () => {
|
|||||||
|
|
||||||
test('Detects Ogg', async () => {
|
test('Detects Ogg', async () => {
|
||||||
const stream = Readable.from(gen(10), { objectMode: false });
|
const stream = Readable.from(gen(10), { objectMode: false });
|
||||||
oggWrite.mockImplementation(function mock(data: Buffer) {
|
oggWrite.mockImplementation(function mock(this: EventEmitter, data: Buffer) {
|
||||||
if (data[0] === 5) this.emit('head', validHead);
|
if (data[0] === 5) this.emit('head', validHead);
|
||||||
} as any);
|
});
|
||||||
const probe = await demuxProbe(stream);
|
const probe = await demuxProbe(stream);
|
||||||
expect(probe.type).toEqual(StreamType.OggOpus);
|
expect(probe.type).toEqual(StreamType.OggOpus);
|
||||||
await expect(collectStream(probe.stream)).resolves.toEqual(range(10));
|
await expect(collectStream(probe.stream)).resolves.toEqual(range(10));
|
||||||
@@ -98,9 +97,9 @@ describe('demuxProbe', () => {
|
|||||||
|
|
||||||
test('Rejects invalid OpusHead', async () => {
|
test('Rejects invalid OpusHead', async () => {
|
||||||
const stream = Readable.from(gen(10), { objectMode: false });
|
const stream = Readable.from(gen(10), { objectMode: false });
|
||||||
oggWrite.mockImplementation(function mock(data: Buffer) {
|
oggWrite.mockImplementation(function mock(this: EventEmitter, data: Buffer) {
|
||||||
if (data[0] === 5) this.emit('head', invalidHead);
|
if (data[0] === 5) this.emit('head', invalidHead);
|
||||||
} as any);
|
});
|
||||||
const probe = await demuxProbe(stream);
|
const probe = await demuxProbe(stream);
|
||||||
expect(probe.type).toEqual(StreamType.Arbitrary);
|
expect(probe.type).toEqual(StreamType.Arbitrary);
|
||||||
await expect(collectStream(probe.stream)).resolves.toEqual(range(10));
|
await expect(collectStream(probe.stream)).resolves.toEqual(range(10));
|
||||||
|
|||||||
@@ -1,4 +1,3 @@
|
|||||||
// @ts-nocheck
|
|
||||||
import { describe, test, expect, vitest, beforeAll, beforeEach } from 'vitest';
|
import { describe, test, expect, vitest, beforeAll, beforeEach } from 'vitest';
|
||||||
import * as VoiceConnection from '../src/VoiceConnection';
|
import * as VoiceConnection from '../src/VoiceConnection';
|
||||||
import { joinVoiceChannel } from '../src/joinVoiceChannel';
|
import { joinVoiceChannel } from '../src/joinVoiceChannel';
|
||||||
@@ -21,7 +20,7 @@ describe('joinVoiceChannel', () => {
|
|||||||
guildId: '456',
|
guildId: '456',
|
||||||
adapterCreator,
|
adapterCreator,
|
||||||
});
|
});
|
||||||
expect(createVoiceConnection.mock.calls[0][0]).toMatchObject({
|
expect(createVoiceConnection.mock.calls[0]?.[0]).toMatchObject({
|
||||||
channelId: '123',
|
channelId: '123',
|
||||||
guildId: '456',
|
guildId: '456',
|
||||||
group: 'default',
|
group: 'default',
|
||||||
@@ -35,7 +34,7 @@ describe('joinVoiceChannel', () => {
|
|||||||
group: 'abc',
|
group: 'abc',
|
||||||
adapterCreator,
|
adapterCreator,
|
||||||
});
|
});
|
||||||
expect(createVoiceConnection.mock.calls[0][0]).toMatchObject({
|
expect(createVoiceConnection.mock.calls[0]?.[0]).toMatchObject({
|
||||||
channelId: '123',
|
channelId: '123',
|
||||||
guildId: '456',
|
guildId: '456',
|
||||||
group: 'abc',
|
group: 'abc',
|
||||||
|
|||||||
@@ -1,6 +1,4 @@
|
|||||||
/* eslint-disable @typescript-eslint/consistent-type-imports */
|
import { test, vi, expect } from 'vitest';
|
||||||
// @ts-nocheck
|
|
||||||
import { beforeEach, test, vi, expect } from 'vitest';
|
|
||||||
import {
|
import {
|
||||||
managerToFetchingStrategyOptions,
|
managerToFetchingStrategyOptions,
|
||||||
WorkerContextFetchingStrategy,
|
WorkerContextFetchingStrategy,
|
||||||
@@ -9,17 +7,20 @@ import {
|
|||||||
WorkerReceivePayloadOp,
|
WorkerReceivePayloadOp,
|
||||||
type WorkerReceivePayload,
|
type WorkerReceivePayload,
|
||||||
type WorkerSendPayload,
|
type WorkerSendPayload,
|
||||||
|
type SessionInfo,
|
||||||
} from '../../src/index.js';
|
} from '../../src/index.js';
|
||||||
import { mockGatewayInformation } from '../gateway.mock.js';
|
import { mockGatewayInformation } from '../gateway.mock.js';
|
||||||
|
|
||||||
const session = {
|
const session: SessionInfo = {
|
||||||
shardId: 0,
|
shardId: 0,
|
||||||
shardCount: 1,
|
shardCount: 1,
|
||||||
sequence: 123,
|
sequence: 123,
|
||||||
sessionId: 'abc',
|
sessionId: 'abc',
|
||||||
|
resumeURL: 'wss://resume.url/',
|
||||||
};
|
};
|
||||||
|
|
||||||
vi.mock('node:worker_threads', async () => {
|
vi.mock('node:worker_threads', async () => {
|
||||||
|
// eslint-disable-next-line @typescript-eslint/consistent-type-imports
|
||||||
const { EventEmitter }: typeof import('node:events') = await vi.importActual('node:events');
|
const { EventEmitter }: typeof import('node:events') = await vi.importActual('node:events');
|
||||||
class MockParentPort extends EventEmitter {
|
class MockParentPort extends EventEmitter {
|
||||||
public postMessage(message: WorkerReceivePayload) {
|
public postMessage(message: WorkerReceivePayload) {
|
||||||
|
|||||||
Reference in New Issue
Block a user