Skip to content

Commit 5888663

Browse files
test: fix type errors (#11325)
* test: fix type errors * chore: use MockedFunction --------- Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
1 parent 8d33188 commit 5888663

File tree

12 files changed

+68
-78
lines changed

12 files changed

+68
-78
lines changed

packages/brokers/__tests__/index.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const mockRedisClient = {
1515
test('pubsub with custom encoding', async () => {
1616
const encode = vi.fn((data) => data);
1717

18-
const broker = new PubSubRedisBroker(mockRedisClient, { encode, group: 'group' });
18+
const broker = new PubSubRedisBroker(mockRedisClient, { encode, name: 'yeet', group: 'group' });
1919
await broker.publish('test', 'test');
2020
expect(encode).toHaveBeenCalledWith('test');
2121
});

packages/rest/__tests__/BurstHandler.test.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
/* eslint-disable id-length */
22
/* eslint-disable promise/prefer-await-to-then */
3-
// @ts-nocheck
43
import { performance } from 'node:perf_hooks';
54
import { MockAgent, setGlobalDispatcher } from 'undici';
65
import type { Interceptable, MockInterceptor } from 'undici/types/mock-interceptor';
@@ -137,7 +136,7 @@ test('Handle unexpected 429', async () => {
137136
});
138137

139138
expect(await unexpectedLimit).toStrictEqual({ test: true });
140-
expect(performance.now()).toBeGreaterThanOrEqual(previous + 1_000);
139+
expect(firstResolvedTime!).toBeGreaterThanOrEqual(previous + 1_000);
141140
});
142141

143142
test('server responding too slow', async () => {

packages/rest/__tests__/REST.test.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
1-
import { Buffer, File as NativeFile } from 'node:buffer';
1+
import { Buffer, File } from 'node:buffer';
22
import { URLSearchParams } from 'node:url';
33
import { DiscordSnowflake } from '@sapphire/snowflake';
44
import type { Snowflake } from 'discord-api-types/v10';
55
import { Routes } from 'discord-api-types/v10';
66
import { type FormData, fetch } from 'undici';
7-
import { File as UndiciFile, MockAgent, setGlobalDispatcher } from 'undici';
7+
import { MockAgent, setGlobalDispatcher } from 'undici';
88
import type { Interceptable, MockInterceptor } from 'undici/types/mock-interceptor.js';
99
import { beforeEach, afterEach, test, expect, vitest } from 'vitest';
1010
import { REST } from '../src/index.js';
1111
import { genPath } from './util.js';
1212

13-
const File = NativeFile ?? UndiciFile;
14-
1513
const newSnowflake: Snowflake = DiscordSnowflake.generate().toString();
1614

1715
const api = new REST().setToken('A-Very-Fake-Token');

packages/rest/__tests__/UndiciRequest.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ test('resolveBody', async () => {
8282
const fd = new globalThis.FormData();
8383
fd.append('key', 'value');
8484

85-
const resolved = await resolveBody(fd);
85+
const resolved = await resolveBody(fd as UndiciFormData);
8686

8787
expect(resolved).toBeInstanceOf(UndiciFormData);
8888
expect([...(resolved as UndiciFormData).entries()]).toStrictEqual([['key', 'value']]);

packages/voice/__tests__/AudioPlayer.test.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { Buffer } from 'node:buffer';
55
import { once } from 'node:events';
66
import process from 'node:process';
77
import { Readable } from 'node:stream';
8-
import { describe, test, expect, vitest, type Mock, beforeEach, afterEach } from 'vitest';
8+
import { describe, test, expect, vitest, type MockedFunction, beforeEach, afterEach } from 'vitest';
99
import { addAudioPlayer, deleteAudioPlayer } from '../src/DataStore';
1010
import { VoiceConnection, VoiceConnectionStatus } from '../src/VoiceConnection';
1111
import type { AudioPlayer } from '../src/audio/AudioPlayer';
@@ -252,7 +252,9 @@ describe('State transitions', () => {
252252
expect(connection.dispatchAudio).toHaveBeenCalledTimes(6);
253253
await wait();
254254
player['_stepPrepare']();
255-
const prepareAudioPacket = connection.prepareAudioPacket as unknown as Mock<typeof connection.prepareAudioPacket>;
255+
const prepareAudioPacket = connection.prepareAudioPacket as unknown as MockedFunction<
256+
typeof connection.prepareAudioPacket
257+
>;
256258
expect(prepareAudioPacket).toHaveBeenCalledTimes(6);
257259
expect(prepareAudioPacket.mock.calls[5]![0]).toEqual(silence().next().value);
258260

@@ -307,7 +309,9 @@ describe('State transitions', () => {
307309

308310
await wait();
309311
expect(player.checkPlayable()).toEqual(false);
310-
const prepareAudioPacket = connection.prepareAudioPacket as unknown as Mock<typeof connection.prepareAudioPacket>;
312+
const prepareAudioPacket = connection.prepareAudioPacket as unknown as MockedFunction<
313+
typeof connection.prepareAudioPacket
314+
>;
311315
expect(prepareAudioPacket).toHaveBeenCalledTimes(5);
312316

313317
expect(player.state.status).toEqual(AudioPlayerStatus.Idle);
@@ -338,7 +342,9 @@ describe('State transitions', () => {
338342
expect(addAudioPlayer).toBeCalledTimes(1);
339343
expect(player.checkPlayable()).toEqual(true);
340344

341-
const prepareAudioPacket = connection.prepareAudioPacket as unknown as Mock<typeof connection.prepareAudioPacket>;
345+
const prepareAudioPacket = connection.prepareAudioPacket as unknown as MockedFunction<
346+
typeof connection.prepareAudioPacket
347+
>;
342348

343349
// Run through a few packet cycles
344350
for (let index = 1; index <= 5; index++) {

packages/voice/__tests__/TransformerGraph.test.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
// @ts-nocheck
21
import { describe, test, expect } from 'vitest';
32
import { findPipeline, StreamType, TransformerType, type Edge } from '../src/audio/TransformerGraph';
43

@@ -10,12 +9,12 @@ const noConstraint = () => true;
109
* @param pipeline - The pipeline of edges returned by findPipeline(...)
1110
*/
1211
function reducePath(pipeline: Edge[]) {
13-
const streams = [pipeline[0].from.type];
12+
const streams = [pipeline[0]!.from.type];
1413
for (const edge of pipeline.slice(1)) {
1514
streams.push(edge.from.type);
1615
}
1716

18-
streams.push(pipeline[pipeline.length - 1].to.type);
17+
streams.push(pipeline[pipeline.length - 1]!.to.type);
1918
return streams;
2019
}
2120

packages/voice/__tests__/VoiceConnection.test.ts

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* eslint-disable @typescript-eslint/unbound-method */
22
/* eslint-disable @typescript-eslint/dot-notation */
3-
// @ts-nocheck
43
import { EventEmitter } from 'node:events';
4+
import type { Mocked } from 'vitest';
55
import { vitest, describe, test, expect, beforeEach } from 'vitest';
66
import * as _DataStore from '../src/DataStore';
77
import {
@@ -25,16 +25,16 @@ vitest.mock('../src/networking/Networking', async (importOriginal) => {
2525
// eslint-disable-next-line @typescript-eslint/consistent-type-imports
2626
const actual = await importOriginal<typeof import('../src/networking/Networking')>();
2727
const Networking = actual.Networking;
28-
Networking.prototype.createWebSocket = vitest.fn();
28+
Networking.prototype['createWebSocket'] = vitest.fn();
2929
return {
3030
...actual,
3131
Networking,
3232
};
3333
});
3434

35-
const DataStore = _DataStore as unknown as vitest.Mocked<typeof _DataStore>;
36-
const AudioPlayer = _AudioPlayer as unknown as vitest.Mocked<typeof _AudioPlayer>;
37-
const PlayerSubscription = _PlayerSubscription as unknown as vitest.Mock<_PlayerSubscription>;
35+
const DataStore = _DataStore as unknown as Mocked<typeof _DataStore>;
36+
const AudioPlayer = _AudioPlayer as unknown as Mocked<typeof _AudioPlayer>;
37+
const PlayerSubscription = _PlayerSubscription as unknown as Mocked<typeof _PlayerSubscription>;
3838

3939
const _NetworkingClass = Networking.Networking;
4040
vitest.spyOn(Networking, 'Networking').mockImplementation((...args) => new _NetworkingClass(...args));
@@ -133,9 +133,10 @@ describe('createVoiceConnection', () => {
133133

134134
const stateSetter = vitest.spyOn(existingVoiceConnection, 'state', 'set');
135135

136-
// @ts-expect-error: We're testing
137136
DataStore.getVoiceConnection.mockImplementation((guildId, group = 'default') =>
138-
guildId === existingJoinConfig.guildId && group === existingJoinConfig.group ? existingVoiceConnection : null,
137+
guildId === existingJoinConfig.guildId && group === existingJoinConfig.group
138+
? existingVoiceConnection
139+
: undefined,
139140
);
140141

141142
const newAdapter = createFakeAdapter();
@@ -172,9 +173,10 @@ describe('createVoiceConnection', () => {
172173

173174
const rejoinSpy = vitest.spyOn(existingVoiceConnection, 'rejoin');
174175

175-
// @ts-expect-error: We're testing
176176
DataStore.getVoiceConnection.mockImplementation((guildId, group = 'default') =>
177-
guildId === existingJoinConfig.guildId && group === existingJoinConfig.group ? existingVoiceConnection : null,
177+
guildId === existingJoinConfig.guildId && group === existingJoinConfig.group
178+
? existingVoiceConnection
179+
: undefined,
178180
);
179181

180182
const newAdapter = createFakeAdapter();
@@ -204,9 +206,10 @@ describe('createVoiceConnection', () => {
204206
adapterCreator: existingAdapter.creator,
205207
});
206208

207-
// @ts-expect-error: We're testing
208209
DataStore.getVoiceConnection.mockImplementation((guildId, group = 'default') =>
209-
guildId === existingJoinConfig.guildId && group === existingJoinConfig.group ? existingVoiceConnection : null,
210+
guildId === existingJoinConfig.guildId && group === existingJoinConfig.group
211+
? existingVoiceConnection
212+
: undefined,
210213
);
211214

212215
const newAdapter = createFakeAdapter();
@@ -444,7 +447,7 @@ describe('VoiceConnection#onNetworkingStateChange', () => {
444447
voiceConnection['_state'] = {
445448
...(voiceConnection.state as VoiceConnectionSignallingState),
446449
status: VoiceConnectionStatus.Connecting,
447-
networking: new Networking.Networking({} as any, false),
450+
networking: new Networking.Networking({} as any, {}),
448451
};
449452

450453
voiceConnection['onNetworkingStateChange'](
@@ -462,7 +465,7 @@ describe('VoiceConnection#onNetworkingStateChange', () => {
462465
voiceConnection['_state'] = {
463466
...(voiceConnection.state as VoiceConnectionSignallingState),
464467
status: VoiceConnectionStatus.Connecting,
465-
networking: new Networking.Networking({} as any, false),
468+
networking: new Networking.Networking({} as any, {}),
466469
};
467470

468471
voiceConnection['onNetworkingStateChange'](
@@ -492,7 +495,7 @@ describe('VoiceConnection#destroy', () => {
492495
voiceConnection.destroy();
493496
expect(DataStore.getVoiceConnection).toHaveReturnedWith(voiceConnection);
494497
expect(DataStore.untrackVoiceConnection).toHaveBeenCalledWith(voiceConnection);
495-
expect(DataStore.createJoinVoiceChannelPayload.mock.calls[0][0]).toMatchObject({
498+
expect(DataStore.createJoinVoiceChannelPayload.mock.calls[0]?.[0]).toMatchObject({
496499
channelId: null,
497500
guildId: joinConfig.guildId,
498501
});
@@ -518,7 +521,7 @@ describe('VoiceConnection#disconnect', () => {
518521
voiceConnection.state = {
519522
status: VoiceConnectionStatus.Ready,
520523
adapter,
521-
networking: new Networking.Networking({} as any, false),
524+
networking: new Networking.Networking({} as any, {}),
522525
};
523526
const leavePayload = Symbol('dummy');
524527
DataStore.createJoinVoiceChannelPayload.mockImplementation(() => leavePayload as any);
@@ -542,7 +545,7 @@ describe('VoiceConnection#disconnect', () => {
542545
voiceConnection.state = {
543546
status: VoiceConnectionStatus.Ready,
544547
adapter,
545-
networking: new Networking.Networking({} as any, false),
548+
networking: new Networking.Networking({} as any, {}),
546549
};
547550
adapter.sendPayload.mockImplementation(() => false);
548551
expect(voiceConnection.disconnect()).toEqual(false);
@@ -676,7 +679,7 @@ describe('VoiceConnection#onSubscriptionRemoved', () => {
676679
// Arrange
677680
const ws = new EventEmitter() as any;
678681

679-
const oldNetworking = new Networking.Networking({} as any, false);
682+
const oldNetworking = new Networking.Networking({} as any, {});
680683
oldNetworking.state = {
681684
code: Networking.NetworkingStatusCode.Ready,
682685
connectionData: {} as any,
@@ -685,7 +688,7 @@ describe('VoiceConnection#onSubscriptionRemoved', () => {
685688
ws,
686689
};
687690

688-
const newNetworking = new Networking.Networking({} as any, false);
691+
const newNetworking = new Networking.Networking({} as any, {});
689692
newNetworking.state = {
690693
...oldNetworking.state,
691694
udp: new EventEmitter() as any,
@@ -706,7 +709,7 @@ describe('VoiceConnection#onSubscriptionRemoved', () => {
706709
// Arrange
707710
const udp = new EventEmitter() as any;
708711

709-
const oldNetworking = new Networking.Networking({} as any, false);
712+
const oldNetworking = new Networking.Networking({} as any, {});
710713
oldNetworking.state = {
711714
code: Networking.NetworkingStatusCode.Ready,
712715
connectionData: {} as any,
@@ -715,7 +718,7 @@ describe('VoiceConnection#onSubscriptionRemoved', () => {
715718
ws: new EventEmitter() as any,
716719
};
717720

718-
const newNetworking = new Networking.Networking({} as any, false);
721+
const newNetworking = new Networking.Networking({} as any, {});
719722
newNetworking.state = {
720723
...oldNetworking.state,
721724
ws: new EventEmitter() as any,
@@ -735,7 +738,7 @@ describe('VoiceConnection#onSubscriptionRemoved', () => {
735738
test('Applies initial listeners', () => {
736739
// Arrange
737740

738-
const newNetworking = new Networking.Networking({} as any, false);
741+
const newNetworking = new Networking.Networking({} as any, {});
739742
newNetworking.state = {
740743
code: Networking.NetworkingStatusCode.Ready,
741744
connectionData: {} as any,

packages/voice/__tests__/VoiceReceiver.test.ts

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
/* eslint-disable id-length */
22
/* eslint-disable @typescript-eslint/dot-notation */
3-
// @ts-nocheck
43
import { Buffer } from 'node:buffer';
54
import { once } from 'node:events';
65
import process from 'node:process';
@@ -15,7 +14,6 @@ import {
1514
} from '../__mocks__/rtp';
1615
import { VoiceConnection, VoiceConnectionStatus } from '../src/VoiceConnection';
1716
import { VoiceReceiver } from '../src/receive/VoiceReceiver';
18-
import { methods } from '../src/util/Secretbox';
1917

2018
vitest.mock('../src/VoiceConnection', async (importOriginal) => {
2119
// eslint-disable-next-line @typescript-eslint/consistent-type-imports
@@ -33,18 +31,8 @@ async function nextTick() {
3331
return new Promise((resolve) => process.nextTick(resolve));
3432
}
3533

36-
function* rangeIter(start: number, end: number) {
37-
for (let i = start; i <= end; i++) {
38-
yield i;
39-
}
40-
}
41-
42-
function range(start: number, end: number) {
43-
return Buffer.from([...rangeIter(start, end)]);
44-
}
45-
4634
describe('VoiceReceiver', () => {
47-
let voiceConnection: _VoiceConnection;
35+
let voiceConnection: VoiceConnection;
4836
let receiver: VoiceReceiver;
4937

5038
beforeEach(() => {
@@ -64,7 +52,7 @@ describe('VoiceReceiver', () => {
6452
['RTP Packet Desktop', RTP_PACKET_DESKTOP],
6553
['RTP Packet Chrome', RTP_PACKET_CHROME],
6654
['RTP Packet Android', RTP_PACKET_ANDROID],
67-
])('onUdpMessage: decrypt from %s', async (testName, RTP_PACKET) => {
55+
])('onUdpMessage: decrypt from %s', async (_testName, RTP_PACKET) => {
6856
receiver['decrypt'] = vitest.fn().mockImplementationOnce(() => RTP_PACKET.decrypted);
6957

7058
const spy = vitest.spyOn(receiver.ssrcMap, 'get');
@@ -144,8 +132,6 @@ describe('VoiceReceiver', () => {
144132
});
145133

146134
describe('decrypt', () => {
147-
const secretKey = new Uint8Array([1, 2, 3, 4]);
148-
149135
test('decrypt: aead_xchacha20_poly1305_rtpsize', () => {
150136
const nonceSpace = Buffer.alloc(24);
151137

packages/voice/__tests__/VoiceUDPSocket.test.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
1-
// @ts-nocheck
21
import { Buffer } from 'node:buffer';
32
import { createSocket as _createSocket } from 'node:dgram';
43
import { EventEmitter } from 'node:events';
4+
import type { MockedFunction } from 'vitest';
55
import { describe, test, expect, vitest, beforeEach, afterEach } from 'vitest';
66
import { VoiceUDPSocket } from '../src/networking/VoiceUDPSocket';
77

88
vitest.mock('node:dgram');
99
vitest.useFakeTimers();
1010

11-
const createSocket = _createSocket as unknown as vitest.Mock<typeof _createSocket>;
11+
const createSocket = _createSocket as unknown as MockedFunction<typeof _createSocket>;
1212

1313
beforeEach(() => {
1414
createSocket.mockReset();
1515
});
1616

1717
class FakeSocket extends EventEmitter {
18-
public send(buffer: Buffer, port: number, address: string) {}
18+
public send(_buffer: Buffer, _port: number, _address: string) {}
1919

2020
public close() {
2121
this.emit('close');
@@ -49,10 +49,10 @@ describe('VoiceUDPSocket#performIPDiscovery', () => {
4949
*/
5050
test('Resolves and cleans up with a successful flow', async () => {
5151
const fake = new FakeSocket();
52-
fake.send = vitest.fn().mockImplementation((buffer: Buffer, port: number, address: string) => {
52+
fake.send = vitest.fn().mockImplementation((_buffer: Buffer, _port: number, _address: string) => {
5353
fake.emit('message', VALID_RESPONSE);
5454
});
55-
createSocket.mockImplementation((type) => fake as any);
55+
createSocket.mockImplementation((_type) => fake as any);
5656
socket = new VoiceUDPSocket({ ip: '1.2.3.4', port: 25_565 });
5757

5858
expect(createSocket).toHaveBeenCalledWith('udp4');
@@ -72,7 +72,7 @@ describe('VoiceUDPSocket#performIPDiscovery', () => {
7272
test('Waits for a valid response in an unexpected flow', async () => {
7373
const fake = new FakeSocket();
7474
const fakeResponse = Buffer.from([1, 2, 3, 4, 5]);
75-
fake.send = vitest.fn().mockImplementation(async (buffer: Buffer, port: number, address: string) => {
75+
fake.send = vitest.fn().mockImplementation(async (_buffer: Buffer, _port: number, _address: string) => {
7676
fake.emit('message', fakeResponse);
7777
await wait();
7878
fake.emit('message', VALID_RESPONSE);
@@ -92,7 +92,7 @@ describe('VoiceUDPSocket#performIPDiscovery', () => {
9292

9393
test('Rejects if socket closes before IP discovery can be completed', async () => {
9494
const fake = new FakeSocket();
95-
fake.send = vitest.fn().mockImplementation(async (buffer: Buffer, port: number, address: string) => {
95+
fake.send = vitest.fn().mockImplementation(async (_buffer: Buffer, _port: number, _address: string) => {
9696
await wait();
9797
fake.close();
9898
});

0 commit comments

Comments
 (0)