generated from nhcarrigan/template
feat: first version of bot (#2)
### Explanation This should set up everything we need for our initial launch. Test coverage is at 100% to ensure nothing breaks. ### Issue _No response_ ### Attestations - [x] I have read and agree to the [Code of Conduct](https://docs.nhcarrigan.com/community/coc/) - [x] I have read and agree to the [Community Guidelines](https://docs.nhcarrigan.com/community/guide/). - [x] My contribution complies with the [Contributor Covenant](https://docs.nhcarrigan.com/dev/covenant/). ### Dependencies - [x] I have pinned the dependencies to a specific patch version. ### Style - [x] I have run the linter and resolved any errors. - [x] My pull request uses an appropriate title, matching the conventional commit standards. - [x] My scope of feat/fix/chore/etc. correctly matches the nature of changes in my pull request. ### Tests - [x] My contribution adds new code, and I have added tests to cover it. - [ ] My contribution modifies existing code, and I have updated the tests to reflect these changes. - [x] All new and existing tests pass locally with my changes. - [x] Code coverage remains at or above the configured threshold. ### Documentation Coming soon - I'm working on the infra for docs next ### Versioning Major - My pull request introduces a breaking change. Reviewed-on: https://codeberg.org/nhcarrigan/rig-task-bot/pulls/2 Co-authored-by: Naomi Carrigan <commits@nhcarrigan.com> Co-committed-by: Naomi Carrigan <commits@nhcarrigan.com>
This commit is contained in:
156
test/events/onInteractionCreate.spec.ts
Normal file
156
test/events/onInteractionCreate.spec.ts
Normal file
@ -0,0 +1,156 @@
|
||||
/**
|
||||
* @copyright nhcarrigan
|
||||
* @license Naomi's Public License
|
||||
* @author Naomi Carrigan
|
||||
*/
|
||||
|
||||
import { describe, it, expect, vi, beforeEach } from "vitest";
|
||||
import { onInteractionCreate } from "../../src/events/onInteractionCreate.js";
|
||||
import { errorHandler } from "../../src/utils/errorHandler.js";
|
||||
|
||||
vi.mock("../../src/utils/sendDebugLog.ts", () => {
|
||||
return {
|
||||
sendDebugLog: vi.fn(),
|
||||
};
|
||||
});
|
||||
|
||||
const mockBot = {
|
||||
env: {
|
||||
discordDebugWebhook: {
|
||||
send: vi.fn(),
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
describe("onInteractionCreate", () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
});
|
||||
|
||||
it("should respond with the correct message when command not in guild",
|
||||
async() => {
|
||||
expect.assertions(1);
|
||||
const mockInteraction = {
|
||||
commandName: "test",
|
||||
inCachedGuild: vi.fn().mockReturnValue(false),
|
||||
isChatInputCommand: vi.fn().mockReturnValue(true),
|
||||
reply: vi.fn(),
|
||||
};
|
||||
await onInteractionCreate(mockBot as never, mockInteraction as never);
|
||||
expect(mockInteraction.reply, "should reply with correct body").
|
||||
toHaveBeenCalledWith({
|
||||
content: "How did you run this out of a guild?",
|
||||
ephemeral: true,
|
||||
});
|
||||
});
|
||||
|
||||
it("should handle chat input commands", async() => {
|
||||
expect.assertions(1);
|
||||
const mockInteraction = {
|
||||
commandName: "test",
|
||||
inCachedGuild: vi.fn().mockReturnValue(true),
|
||||
isChatInputCommand: vi.fn().mockReturnValue(true),
|
||||
reply: vi.fn(),
|
||||
};
|
||||
await onInteractionCreate(mockBot as never, mockInteraction as never);
|
||||
expect(mockInteraction.reply, "should reply with correct body").
|
||||
toHaveBeenCalledWith({
|
||||
content: "Interaction test not found.",
|
||||
ephemeral: true,
|
||||
});
|
||||
});
|
||||
|
||||
it("should respond with the correct message when modal not in guild",
|
||||
async() => {
|
||||
expect.assertions(1);
|
||||
const mockInteraction = {
|
||||
customId: "test",
|
||||
inCachedGuild: vi.fn().mockReturnValue(false),
|
||||
isChatInputCommand: vi.fn().mockReturnValue(false),
|
||||
isModalSubmit: vi.fn().mockReturnValue(true),
|
||||
reply: vi.fn(),
|
||||
};
|
||||
await onInteractionCreate(mockBot as never, mockInteraction as never);
|
||||
expect(mockInteraction.reply, "should reply with correct body").
|
||||
toHaveBeenCalledWith({
|
||||
content: "How did you get a modal outside of a guild?",
|
||||
ephemeral: true,
|
||||
});
|
||||
});
|
||||
|
||||
it("should handle modal submit interactions", async() => {
|
||||
expect.assertions(1);
|
||||
const mockInteraction = {
|
||||
customId: "test",
|
||||
inCachedGuild: vi.fn().mockReturnValue(true),
|
||||
isChatInputCommand: vi.fn().mockReturnValue(false),
|
||||
isModalSubmit: vi.fn().mockReturnValue(true),
|
||||
reply: vi.fn(),
|
||||
};
|
||||
await onInteractionCreate(mockBot as never, mockInteraction as never);
|
||||
expect(mockInteraction.reply, "should reply with correct body").
|
||||
toHaveBeenCalledWith({
|
||||
content: `Modal test has no handler.`,
|
||||
ephemeral: true,
|
||||
});
|
||||
});
|
||||
|
||||
it("should throw an error if the interaction type is unknown", async() => {
|
||||
expect.assertions(1);
|
||||
const mockInteraction = {
|
||||
customId: "test",
|
||||
inCachedGuild: vi.fn().mockReturnValue(true),
|
||||
isAutocomplete: vi.fn().mockReturnValue(false),
|
||||
isChatInputCommand: vi.fn().mockReturnValue(false),
|
||||
isModalSubmit: vi.fn().mockReturnValue(false),
|
||||
reply: vi.fn(),
|
||||
};
|
||||
await onInteractionCreate(mockBot as never, mockInteraction as never);
|
||||
expect(errorHandler, "should call error handler").
|
||||
toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it("should call the error handler if an error is thrown", async() => {
|
||||
expect.assertions(1);
|
||||
const mockInteraction = {
|
||||
commandName: "test",
|
||||
editReply: vi.fn(),
|
||||
inCachedGuild: vi.fn().mockReturnValue(true),
|
||||
isAutocomplete: vi.fn().mockReturnValue(false),
|
||||
isChatInputCommand: vi.fn().mockImplementation(() => {
|
||||
throw new Error("Test error");
|
||||
}),
|
||||
replied: true,
|
||||
reply: vi.fn(),
|
||||
};
|
||||
vi.mock("../../src/utils/errorHandler.ts", () => {
|
||||
return {
|
||||
errorHandler: vi.fn(),
|
||||
};
|
||||
});
|
||||
await onInteractionCreate(mockBot as never, mockInteraction as never);
|
||||
expect(errorHandler, "should call error handler").
|
||||
toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it("should call the error handler if autocomplete is thrown", async() => {
|
||||
expect.assertions(1);
|
||||
const mockInteraction = {
|
||||
commandName: "test",
|
||||
inCachedGuild: vi.fn().mockReturnValue(true),
|
||||
isAutocomplete: vi.fn().mockReturnValue(true),
|
||||
isChatInputCommand: vi.fn().mockImplementation(() => {
|
||||
throw new Error("Test error");
|
||||
}),
|
||||
reply: vi.fn(),
|
||||
};
|
||||
vi.mock("../../src/utils/errorHandler.ts", () => {
|
||||
return {
|
||||
errorHandler: vi.fn(),
|
||||
};
|
||||
});
|
||||
await onInteractionCreate(mockBot as never, mockInteraction as never);
|
||||
expect(errorHandler, "should call error handler").
|
||||
toHaveBeenCalledTimes(1);
|
||||
});
|
||||
});
|
99
test/events/onReady.spec.ts
Normal file
99
test/events/onReady.spec.ts
Normal file
@ -0,0 +1,99 @@
|
||||
/**
|
||||
* @copyright nhcarrigan
|
||||
* @license Naomi's Public License
|
||||
* @author Naomi Carrigan
|
||||
*/
|
||||
|
||||
import { describe, it, expect, vi, beforeEach } from "vitest";
|
||||
import { onReady } from "../../src/events/onReady.js";
|
||||
import { displayCommandCurl } from "../../src/utils/displayCommandCurl.js";
|
||||
import { errorHandler } from "../../src/utils/errorHandler.js";
|
||||
import { sendDebugLog } from "../../src/utils/sendDebugLog.js";
|
||||
|
||||
vi.mock("../../src/utils/sendDebugLog.ts", () => {
|
||||
return {
|
||||
sendDebugLog: vi.fn(),
|
||||
};
|
||||
});
|
||||
|
||||
const mockBot = {
|
||||
discord: {
|
||||
user: {
|
||||
id: "123",
|
||||
},
|
||||
},
|
||||
env: {
|
||||
discordDebugWebhook: {
|
||||
send: vi.fn(),
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
describe("onReady", () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
});
|
||||
|
||||
it("should call sendDebugLog with the correct messages", async() => {
|
||||
expect.assertions(4);
|
||||
vi.mock("../../src/utils/errorHandler.ts", () => {
|
||||
return {
|
||||
errorHandler: vi.fn(),
|
||||
};
|
||||
});
|
||||
await onReady(mockBot as never);
|
||||
|
||||
expect(errorHandler, "should call error handler").not.toHaveBeenCalled();
|
||||
|
||||
expect(sendDebugLog, "should send debug message").toHaveBeenCalledTimes(2);
|
||||
expect(sendDebugLog, "should send debug message").toHaveBeenCalledWith(
|
||||
mockBot,
|
||||
{
|
||||
content: "Bot has authenticated to Discord.",
|
||||
},
|
||||
);
|
||||
expect(sendDebugLog, "should send CURL string").toHaveBeenCalledWith(
|
||||
mockBot,
|
||||
{
|
||||
files: [
|
||||
{
|
||||
attachment: Buffer.from(displayCommandCurl(mockBot as never)),
|
||||
name: "curl.sh",
|
||||
},
|
||||
],
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
it("should not throw an error", async() => {
|
||||
expect.assertions(1);
|
||||
await expect(
|
||||
onReady(mockBot as never),
|
||||
"should not error",
|
||||
).resolves.not.toThrow();
|
||||
});
|
||||
|
||||
it("should call the error handler if an error is thrown", async() => {
|
||||
expect.assertions(1);
|
||||
vi.mock("../../src/utils/errorHandler.ts", () => {
|
||||
return {
|
||||
errorHandler: vi.fn(),
|
||||
};
|
||||
});
|
||||
|
||||
vi.mock("../../src/utils/sendDebugLog.ts", () => {
|
||||
return {
|
||||
sendDebugLog: vi.fn().mockRejectedValue(new Error("Test error")),
|
||||
};
|
||||
});
|
||||
|
||||
await onReady({} as never);
|
||||
|
||||
expect(errorHandler, "should call error handler").toHaveBeenCalledTimes(1);
|
||||
vi.mock("../../src/utils/sendDebugLog.ts", () => {
|
||||
return {
|
||||
sendDebugLog: vi.fn(),
|
||||
};
|
||||
});
|
||||
});
|
||||
});
|
Reference in New Issue
Block a user