-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix bulk deletion with very-large queues (#63)
* Reword responses to some informational commands * Don't claim we cleared the queue if we failed to clear the queue * Respect Discord's bulk-delete limits * v1.3.2
- Loading branch information
1 parent
ad11b20
commit 0a88d9e
Showing
8 changed files
with
117 additions
and
12 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import type Discord from "discord.js"; | ||
import { bulkDeleteMessagesWithIds } from "./deleteMessage.js"; | ||
|
||
const mockBulkDelete = jest.fn(); | ||
const mockSingleDelete = jest.fn(); | ||
|
||
describe("Bulk Message Delete", () => { | ||
let channel: Discord.TextChannel; | ||
|
||
beforeEach(() => { | ||
channel = ({ | ||
bulkDelete: mockBulkDelete, | ||
messages: { | ||
delete: mockSingleDelete | ||
} | ||
} as unknown) as Discord.TextChannel; | ||
|
||
mockBulkDelete.mockImplementation((ids: Array<string>) => { | ||
// Discord balks when we try <2 || >100 IDs | ||
if (ids.length < 2 || ids.length > 100) { | ||
return Promise.reject(new Error("Can't delete fewer than 2 or more than 100")); | ||
} | ||
return Promise.resolve(); | ||
}); | ||
}); | ||
|
||
test.each` | ||
count | multiples | singles | ||
${2} | ${1} | ${0} | ||
${3} | ${1} | ${0} | ||
${45} | ${1} | ${0} | ||
${99} | ${1} | ${0} | ||
${100} | ${1} | ${0} | ||
${101} | ${1} | ${1} | ||
${800} | ${8} | ${0} | ||
`( | ||
"Clears when the queue has $count items in it", | ||
async (params: { count: number; multiples: number; singles: number }) => { | ||
const { count, multiples, singles } = params; | ||
// Use an array of `count` instances of "a" | ||
const messageIds = new Array<string>(count).fill("a"); | ||
await expect(bulkDeleteMessagesWithIds(messageIds, channel)).resolves.toBeTrue(); | ||
|
||
expect(mockBulkDelete).toHaveBeenCalledTimes(multiples); | ||
expect(mockSingleDelete).toHaveBeenCalledTimes(singles); | ||
} | ||
); | ||
|
||
test("Deletes one message individually", async () => { | ||
const messageIds: Array<string> = ["a"]; | ||
await expect(bulkDeleteMessagesWithIds(messageIds, channel)).resolves.toBeTrue(); | ||
|
||
expect(mockBulkDelete).not.toHaveBeenCalled(); | ||
expect(mockSingleDelete).toHaveBeenCalledTimes(1); | ||
expect(mockSingleDelete).toHaveBeenCalledWith("a"); | ||
}); | ||
|
||
test("Does nothing when the queue has 0 items in it", async () => { | ||
const messageIds = new Array<string>(); | ||
await expect(bulkDeleteMessagesWithIds(messageIds, channel)).resolves.toBeTrue(); | ||
|
||
expect(mockBulkDelete).not.toHaveBeenCalled(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters