mirror of
https://github.com/discordeno/discordeno.git
synced 2026-06-03 17:30:07 +00:00
fixed tests
This commit is contained in:
@@ -19,6 +19,12 @@ export async function getMessages(
|
||||
throw new Error(bot.constants.Errors.INVALID_GET_MESSAGES_LIMIT);
|
||||
}
|
||||
|
||||
if (options) {
|
||||
if (bot.utils.hasProperty(options, "around")) options.around = (options.around as bigint).toString();
|
||||
if (bot.utils.hasProperty(options, "before")) options.before = (options.before as bigint).toString();
|
||||
if (bot.utils.hasProperty(options, "after")) options.after = (options.after as bigint).toString();
|
||||
}
|
||||
|
||||
const result = await bot.rest.runMethod<Message[]>(
|
||||
bot.rest,
|
||||
"get",
|
||||
|
||||
@@ -22,13 +22,9 @@ export async function processGlobalQueue(rest: RestManager) {
|
||||
break;
|
||||
}
|
||||
|
||||
const request = rest.globalQueue[0];
|
||||
const request = rest.globalQueue.shift();
|
||||
// REMOVES ANY POTENTIAL INVALID CONFLICTS
|
||||
if (!request) {
|
||||
console.log("removing invalid request");
|
||||
rest.globalQueue.shift();
|
||||
continue;
|
||||
}
|
||||
if (!request) continue;
|
||||
|
||||
// CHECK RATELIMITS FOR 429 REPEATS
|
||||
// IF THIS URL IS STILL RATE LIMITED, TRY AGAIN
|
||||
@@ -37,21 +33,14 @@ export async function processGlobalQueue(rest: RestManager) {
|
||||
const bucketResetIn = request.payload.bucketId ? rest.checkRateLimits(rest, request.payload.bucketId) : false;
|
||||
|
||||
if (urlResetIn || bucketResetIn) {
|
||||
console.log("rate limited shifting 1", rest.globalQueue.length);
|
||||
const rateLimitedRequest = rest.globalQueue.shift();
|
||||
console.log("rate limited shifting 2", rest.globalQueue.length);
|
||||
if (rateLimitedRequest) {
|
||||
// ONLY ADD TIMEOUT IF ANOTHER QUEUE IS NOT PENDING
|
||||
setTimeout(() => {
|
||||
rest.debug(`[REST - processGlobalQueue] rate limited, running setTimeout.`);
|
||||
// THIS REST IS RATE LIMITED, SO PUSH BACK TO START
|
||||
console.log("rate limited unshifting 1", rest.globalQueue.length);
|
||||
rest.globalQueue.unshift(rateLimitedRequest);
|
||||
console.log("rate limited unshifting 2", rest.globalQueue.length);
|
||||
// START QUEUE IF NOT STARTED
|
||||
rest.processGlobalQueue(rest);
|
||||
}, urlResetIn || (bucketResetIn as number));
|
||||
}
|
||||
// ONLY ADD TIMEOUT IF ANOTHER QUEUE IS NOT PENDING
|
||||
setTimeout(() => {
|
||||
rest.debug(`[REST - processGlobalQueue] rate limited, running setTimeout.`);
|
||||
// THIS REST IS RATE LIMITED, SO PUSH BACK TO START
|
||||
rest.globalQueue.unshift(request);
|
||||
// START QUEUE IF NOT STARTED
|
||||
rest.processGlobalQueue(rest);
|
||||
}, urlResetIn || (bucketResetIn as number));
|
||||
|
||||
continue;
|
||||
}
|
||||
@@ -99,14 +88,10 @@ export async function processGlobalQueue(rest: RestManager) {
|
||||
// If NOT rate limited remove from queue
|
||||
if (response.status !== 429) {
|
||||
request.request.reject(new Error(`[${response.status}] ${error}`));
|
||||
console.log("error but no rate limit shifting");
|
||||
rest.globalQueue.shift();
|
||||
} else {
|
||||
if (request.payload.retryCount++ >= rest.maxRetryCount) {
|
||||
rest.debug(`[REST - RetriesMaxed] ${JSON.stringify(request.payload)}`);
|
||||
// REMOVE ITEM FROM QUEUE TO PREVENT RETRY
|
||||
console.log("removing from queue max retries");
|
||||
rest.globalQueue.shift();
|
||||
request.request.reject(
|
||||
new Error(`[${response.status}] The request was rate limited and it maxed out the retries limit.`)
|
||||
);
|
||||
@@ -115,7 +100,7 @@ export async function processGlobalQueue(rest: RestManager) {
|
||||
|
||||
// WAS RATE LIMITED. PUSH TO END OF GLOBAL QUEUE, SO WE DON'T BLOCK OTHER REQUESTS.
|
||||
console.log("delaying request, rate limit 1", rest.globalQueue.length);
|
||||
rest.globalQueue.push(rest.globalQueue.shift()!);
|
||||
rest.globalQueue.push(request);
|
||||
console.log("delaying request, rate limit 2", rest.globalQueue.length);
|
||||
}
|
||||
|
||||
@@ -125,18 +110,12 @@ export async function processGlobalQueue(rest: RestManager) {
|
||||
// SOMETIMES DISCORD RETURNS AN EMPTY 204 RESPONSE THAT CAN'T BE MADE TO JSON
|
||||
if (response.status === 204) {
|
||||
rest.debug(`[REST - FetchSuccess] URL: ${request.urlToUse} | ${JSON.stringify(request.payload)}`);
|
||||
// REMOVE FROM QUEUE
|
||||
console.log("204 response removing from queue");
|
||||
rest.globalQueue.shift();
|
||||
request.request.respond({ status: 204 });
|
||||
} else {
|
||||
// CONVERT THE RESPONSE TO JSON
|
||||
const json = await response.json();
|
||||
|
||||
rest.debug(`[REST - fetchSuccess] ${JSON.stringify(request.payload)}`);
|
||||
// REMOVE FROM QUEUE
|
||||
console.log("request success removing from queue");
|
||||
rest.globalQueue.shift();
|
||||
request.request.respond({
|
||||
status: 200,
|
||||
body: JSON.stringify(json),
|
||||
@@ -146,9 +125,6 @@ export async function processGlobalQueue(rest: RestManager) {
|
||||
// SOMETHING WENT WRONG, LOG AND RESPOND WITH ERROR
|
||||
rest.debug(`[REST - fetchFailed] Payload: ${JSON.stringify(request.payload)} | Error: ${error}`);
|
||||
request.request.reject(error);
|
||||
// REMOVE FROM QUEUE
|
||||
console.log("big error removing from queue");
|
||||
rest.globalQueue.shift();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -2,6 +2,8 @@ import { Bot } from "../../../src/bot.ts";
|
||||
import { assertEquals, assertExists } from "../../deps.ts";
|
||||
import { delayUntil } from "../../utils.ts";
|
||||
|
||||
const reactionCounters = new Map<bigint, number>();
|
||||
|
||||
export async function addReactionTest(
|
||||
bot: Bot,
|
||||
guildId: bigint,
|
||||
@@ -59,18 +61,17 @@ export async function addReactionTest(
|
||||
}
|
||||
}
|
||||
|
||||
let reactions = 0;
|
||||
reactionCounters.set(message.id, 0);
|
||||
|
||||
bot.events.reactionAdd = function (bot, payload) {
|
||||
if (payload.messageId !== message.id) return;
|
||||
|
||||
reactions++;
|
||||
const current = reactionCounters.get(payload.messageId) || 0;
|
||||
reactionCounters.set(payload.messageId, current + 1);
|
||||
};
|
||||
|
||||
if (options.single) await bot.helpers.addReaction(message.channelId, message.id, emojiId);
|
||||
else await bot.helpers.addReactions(message.channelId, message.id, emojiIds, options.ordered);
|
||||
|
||||
await delayUntil(10000, () => reactions === (options.single ? 1 : emojiIds.length));
|
||||
await delayUntil(10000, () => reactionCounters.get(message.id) === (options.single ? 1 : emojiIds.length));
|
||||
|
||||
assertEquals(reactions, options.single ? 1 : emojiIds.length);
|
||||
assertEquals(reactionCounters.get(message.id), options.single ? 1 : emojiIds.length);
|
||||
}
|
||||
|
||||
30
tests/mod.ts
30
tests/mod.ts
@@ -92,135 +92,105 @@ Deno.test("[Bot] - Starting Tests", async (t) => {
|
||||
t.step({
|
||||
name: "[message] send message with text",
|
||||
fn: async (t) => {
|
||||
console.log('start', 1)
|
||||
await sendMessageWithTextTest(bot, channel.id, t);
|
||||
console.log('pass', 1)
|
||||
},
|
||||
...sanitizeMode,
|
||||
}),
|
||||
t.step({
|
||||
name: "[message] send message with embeds",
|
||||
fn: async (t) => {
|
||||
console.log('start', 2)
|
||||
await sendMessageWithEmbedsTest(bot, channel.id, t);
|
||||
console.log('pass', 2)
|
||||
},
|
||||
...sanitizeMode,
|
||||
}),
|
||||
// t.step({
|
||||
// name: "[message] send message with components",
|
||||
// fn: async (t) => {
|
||||
// console.log('start', 2.5)
|
||||
// await sendMessageWithComponents(bot, channel.id, t);
|
||||
// console.log('pass', 2.5)
|
||||
// },
|
||||
// ...sanitizeMode,
|
||||
// }),
|
||||
t.step({
|
||||
name: "[message] delete message without a reason",
|
||||
fn: async (t) => {
|
||||
console.log('start', 3)
|
||||
await deleteMessageWithoutReasonTest(bot, channel.id, t);
|
||||
console.log('pass', 3)
|
||||
},
|
||||
...sanitizeMode,
|
||||
}),
|
||||
t.step({
|
||||
name: "[message] delete message with a reason",
|
||||
fn: async (t) => {
|
||||
console.log('start', 4)
|
||||
await deleteMessageWithReasonTest(bot, channel.id, t);
|
||||
console.log('pass', 4)
|
||||
},
|
||||
...sanitizeMode,
|
||||
}),
|
||||
t.step({
|
||||
name: "[message] delete messages without a reason",
|
||||
fn: async (t) => {
|
||||
console.log('start', 5)
|
||||
await deleteMessagesWithoutReasonTest(bot, channel.id, t);
|
||||
console.log('pass', 5)
|
||||
},
|
||||
...sanitizeMode,
|
||||
}),
|
||||
t.step({
|
||||
name: "[message] delete messages with a reason",
|
||||
fn: async (t) => {
|
||||
console.log('start', 6)
|
||||
await deleteMessagesWithReasonTest(bot, channel.id, t);
|
||||
console.log('pass', 6)
|
||||
},
|
||||
...sanitizeMode,
|
||||
}),
|
||||
t.step({
|
||||
name: "[message] fetch a message",
|
||||
fn: async (t) => {
|
||||
console.log('start', 6.1)
|
||||
await getMessageTest(bot, channel.id, t);
|
||||
console.log('pass', 6.1)
|
||||
},
|
||||
...sanitizeMode,
|
||||
}),
|
||||
t.step({
|
||||
name: "[message] fetch messages",
|
||||
fn: async (t) => {
|
||||
console.log('start', 6.2)
|
||||
await getMessagesTest(bot, channel.id, t);
|
||||
console.log('pass', 6.2)
|
||||
},
|
||||
...sanitizeMode,
|
||||
}),
|
||||
t.step({
|
||||
name: "[message] add a reaction",
|
||||
fn: async (t) => {
|
||||
console.log('start', 7)
|
||||
await addReactionTest(bot, guild.id, channel.id, { custom: false, single: true, ordered: false }, t);
|
||||
console.log('pass', 7)
|
||||
},
|
||||
...sanitizeMode,
|
||||
}),
|
||||
t.step({
|
||||
name: "[message] add a custom reaction",
|
||||
fn: async (t) => {
|
||||
console.log('start', 8)
|
||||
await addReactionTest(bot, guild.id, channel.id, { custom: true, single: true, ordered: false }, t);
|
||||
console.log('pass', 8)
|
||||
},
|
||||
...sanitizeMode,
|
||||
}),
|
||||
t.step({
|
||||
name: "[message] add multiple reactions",
|
||||
fn: async (t) => {
|
||||
console.log('start', 9)
|
||||
await addReactionTest(bot, guild.id, channel.id, { custom: false, single: false, ordered: false }, t);
|
||||
console.log('pass', 9)
|
||||
},
|
||||
...sanitizeMode,
|
||||
}),
|
||||
t.step({
|
||||
name: "[message] add multiple custom reactions",
|
||||
fn: async (t) => {
|
||||
console.log('start', 10)
|
||||
await addReactionTest(bot, guild.id, channel.id, { custom: true, single: false, ordered: false }, t);
|
||||
console.log('pass', 10)
|
||||
},
|
||||
...sanitizeMode,
|
||||
}),
|
||||
t.step({
|
||||
name: "[message] add multiple reactions in order",
|
||||
fn: async (t) => {
|
||||
console.log('start', 11)
|
||||
await addReactionTest(bot, guild.id, channel.id, { custom: false, single: false, ordered: true }, t);
|
||||
console.log('pass', 11)
|
||||
},
|
||||
...sanitizeMode,
|
||||
}),
|
||||
t.step({
|
||||
name: "[message] add multiple custom reactions in order",
|
||||
fn: async (t) => {
|
||||
console.log('start', 12)
|
||||
await addReactionTest(bot, guild.id, channel.id, { custom: true, single: false, ordered: true }, t);
|
||||
console.log('pass', 12)
|
||||
},
|
||||
...sanitizeMode,
|
||||
}),
|
||||
|
||||
Reference in New Issue
Block a user