refactor(WebSocketManager)!: remove deprecated rest option (#10998)

BREAKING CHANGE: The `rest` option in the `WebSocketManager` constructor has been removed. Pass a `fetchGatewayInformation` function instead.
This commit is contained in:
Almeida
2025-07-27 11:49:29 +01:00
committed by GitHub
parent 2e9bfba5f4
commit 08a61ca8e3
9 changed files with 98 additions and 281 deletions

View File

@@ -1,7 +1,5 @@
/* eslint-disable @typescript-eslint/consistent-type-imports */
// @ts-nocheck
import { REST } from '@discordjs/rest';
import { MockAgent, type Interceptable } from 'undici';
import { beforeEach, test, vi, expect } from 'vitest';
import {
managerToFetchingStrategyOptions,
@@ -12,15 +10,7 @@ import {
type WorkerReceivePayload,
type WorkerSendPayload,
} from '../../src/index.js';
let mockAgent: MockAgent;
let mockPool: Interceptable;
beforeEach(() => {
mockAgent = new MockAgent();
mockAgent.disableNetConnect();
mockPool = mockAgent.get('https://discord.com');
});
import { mockGatewayInformation } from '../gateway.mock.js';
const session = {
shardId: 0,
@@ -52,32 +42,13 @@ vi.mock('node:worker_threads', async () => {
});
test('session info', async () => {
const rest = new REST().setAgent(mockAgent).setToken('A-Very-Fake-Token');
const manager = new WebSocketManager({ token: 'A-Very-Fake-Token', intents: 0, rest });
mockPool
.intercept({
path: '/api/v10/gateway/bot',
method: 'GET',
})
.reply(() => ({
data: {
shards: 1,
session_start_limit: {
max_concurrency: 3,
reset_after: 60,
remaining: 3,
total: 3,
},
url: 'wss://gateway.discord.gg',
},
statusCode: 200,
responseOptions: {
headers: {
'content-type': 'application/json',
},
},
}));
const manager = new WebSocketManager({
token: 'A-Very-Fake-Token',
intents: 0,
async fetchGatewayInformation() {
return mockGatewayInformation;
},
});
const strategy = new WorkerContextFetchingStrategy(await managerToFetchingStrategyOptions(manager));

View File

@@ -1,10 +1,8 @@
/* eslint-disable id-length */
import { setImmediate } from 'node:timers';
import { REST } from '@discordjs/rest';
import type { RESTGetAPIGatewayBotResult, GatewayDispatchPayload, GatewaySendPayload } from 'discord-api-types/v10';
import { GatewayDispatchEvents, GatewayOpcodes, Routes } from 'discord-api-types/v10';
import { MockAgent, type Interceptable } from 'undici';
import { beforeEach, test, vi, expect, afterEach } from 'vitest';
import type { GatewayDispatchPayload, GatewaySendPayload } from 'discord-api-types/v10';
import { GatewayDispatchEvents, GatewayOpcodes } from 'discord-api-types/v10';
import { test, vi, expect, afterEach } from 'vitest';
import {
WebSocketManager,
WorkerSendPayloadOp,
@@ -15,9 +13,7 @@ import {
type WorkerSendPayload,
type SessionInfo,
} from '../../src/index.js';
let mockAgent: MockAgent;
let mockPool: Interceptable;
import { mockGatewayInformation } from '../gateway.mock.js';
const mockConstructor = vi.fn();
const mockSend = vi.fn();
@@ -135,12 +131,6 @@ vi.mock('node:worker_threads', async () => {
};
});
beforeEach(() => {
mockAgent = new MockAgent();
mockAgent.disableNetConnect();
mockPool = mockAgent.get('https://discord.com');
});
afterEach(() => {
mockConstructor.mockClear();
mockSend.mockClear();
@@ -148,15 +138,13 @@ afterEach(() => {
});
test('spawn, connect, send a message, session info, and destroy', async () => {
const rest = new REST().setAgent(mockAgent).setToken('A-Very-Fake-Token');
const mockRetrieveSessionInfo = vi.fn();
const mockUpdateSessionInfo = vi.fn();
const manager = new WebSocketManager({
token: 'A-Very-Fake-Token',
intents: 0,
async fetchGatewayInformation() {
return rest.get(Routes.gatewayBot()) as Promise<RESTGetAPIGatewayBotResult>;
return mockGatewayInformation;
},
shardIds: [0, 1],
retrieveSessionInfo: mockRetrieveSessionInfo,
@@ -166,30 +154,6 @@ test('spawn, connect, send a message, session info, and destroy', async () => {
const managerEmitSpy = vi.spyOn(manager, 'emit');
mockPool
.intercept({
path: '/api/v10/gateway/bot',
method: 'GET',
})
.reply(() => ({
data: {
shards: 1,
session_start_limit: {
max_concurrency: 3,
reset_after: 60,
remaining: 3,
total: 3,
},
url: 'wss://gateway.discord.gg',
},
statusCode: 200,
responseOptions: {
headers: {
'content-type': 'application/json',
},
},
}));
await manager.connect();
expect(mockConstructor).toHaveBeenCalledWith(
expect.stringContaining('defaultWorker.js'),