rig-task-bot/test/events/onInteractionCreate.spec.ts
Naomi Carrigan 296a50fedd 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>
2024-09-30 01:41:25 +00:00

157 lines
5.1 KiB
TypeScript

/**
* @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);
});
});