Files
discord.js/packages/rest/__tests__/RequestManager.test.ts
DD 278396e815 feat(REST): dynamic rate limit offsets (#10099)
* feat(REST): dynamic rate limit offsets

* chore: update tests

* chore: better doc comment

Co-authored-by: Jiralite <33201955+Jiralite@users.noreply.github.com>

* fix: don't overlook globalReset

Co-authored-by: ckohen <chaikohen@gmail.com>

---------

Co-authored-by: Jiralite <33201955+Jiralite@users.noreply.github.com>
Co-authored-by: ckohen <chaikohen@gmail.com>
Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
2024-01-30 09:29:42 +00:00

42 lines
1.1 KiB
TypeScript

import { MockAgent, setGlobalDispatcher, type Interceptable } from 'undici';
import { beforeEach, afterEach, test, expect } from 'vitest';
import { REST } from '../src/index.js';
import { normalizeRateLimitOffset } from '../src/lib/utils/utils.js';
import { genPath } from './util.js';
const api = new REST();
let mockAgent: MockAgent;
let mockPool: Interceptable;
beforeEach(() => {
mockAgent = new MockAgent();
mockAgent.disableNetConnect();
setGlobalDispatcher(mockAgent);
mockPool = mockAgent.get('https://discord.com');
});
afterEach(async () => {
await mockAgent.close();
});
test('no token', async () => {
mockPool
.intercept({
path: genPath('/simpleGet'),
method: 'GET',
})
.reply(200, 'Well this is awkward...');
const promise = api.get('/simpleGet');
await expect(promise).rejects.toThrowError('Expected token to be set for this request, but none was present');
await expect(promise).rejects.toBeInstanceOf(Error);
});
test('negative offset', () => {
const badREST = new REST({ offset: -5_000 });
expect(normalizeRateLimitOffset(badREST.options.offset, 'hehe :3')).toEqual(0);
});