rig-task-bot/test/commands/assign.spec.ts

252 lines
6.8 KiB
TypeScript
Raw Normal View History

2024-09-30 01:41:25 +00:00
/**
* @copyright nhcarrigan
* @license Naomi's Public License
* @author Naomi Carrigan
*/
import {
ApplicationCommandOptionType,
InteractionContextType,
} from "discord.js";
import { describe, it, expect, vi } from "vitest";
import { assign } from "../../src/commands/assign.js";
import { errorHandler } from "../../src/utils/errorHandler.js";
vi.mock("../../src/utils/errorHandler.ts", () => {
return {
errorHandler: vi.fn(),
};
});
describe("assign command", () => {
it("should have the correct data", () => {
expect.assertions(16);
expect(assign.data.name, "did not have the correct name").toBe("assign");
expect(assign.data.name.length, "name is too long").toBeLessThanOrEqual(32);
expect(assign.data.name, "name has invalid characters").toMatch(
/^[-_\p{L}\p{N}\p{sc=Deva}\p{sc=Thai}]{1,32}$/u,
);
expect(
assign.data.description,
"did not have the correct description",
).toBe("Add or remove someone as a task assignee.");
expect(
assign.data.description.length,
"description is too long",
).toBeLessThanOrEqual(100);
expect(
assign.data.contexts,
"did not have the correct context",
).toStrictEqual([ InteractionContextType.Guild ]);
expect(assign.data.options, "should have 2 options").toHaveLength(2);
expect(
assign.data.options[0].toJSON().name,
"should have the correct name",
).toBe("task");
expect(
assign.data.options[0].toJSON().description,
"should have the correct description",
).toBe("The task number.");
expect(
assign.data.options[0].toJSON().required,
"should be required",
).toBeTruthy();
expect(
assign.data.options[0].toJSON()["min_value"],
"should have a min value of 1",
).toBe(1);
expect(
assign.data.options[0].toJSON().type,
"should be a number option",
).toBe(ApplicationCommandOptionType.Integer);
expect(
assign.data.options[1].toJSON().name,
"should have the correct name",
).toBe("assignee");
expect(
assign.data.options[1].toJSON().description,
"should have the correct description",
).toBe("The user to (un)assign.");
expect(
assign.data.options[1].toJSON().required,
"should be required",
).toBeTruthy();
expect(
assign.data.options[1].toJSON().type,
"should be a user option",
).toBe(ApplicationCommandOptionType.User);
});
it("should execute correctly when adding assign", async() => {
expect.assertions(4);
const mockBot = {
database: {
tasks: {
findFirst: vi.fn().mockResolvedValue({
assignees: [],
numericalId: 1,
}),
update: vi.fn().mockResolvedValue({}),
},
},
} as never;
const mockInteraction = {
deferReply: vi.fn(),
editReply: vi.fn(),
options: {
getInteger: vi.fn().mockReturnValue(1),
getUser: vi.fn().mockReturnValue({ id: "123" }),
},
} as never;
await assign.run(mockBot, mockInteraction);
expect(
mockInteraction.deferReply,
"should defer the reply",
).toHaveBeenCalledWith({ ephemeral: true });
expect(
mockBot.database.tasks.findFirst,
"should call findFirst",
).toHaveBeenCalledWith({
where: {
numericalId: 1,
},
});
expect(
mockBot.database.tasks.update,
"should call update",
).toHaveBeenCalledWith({
data: {
assignees: {
push: "123",
},
},
where: {
numericalId: 1,
},
});
expect(
mockInteraction.editReply,
"should call editReply",
).toHaveBeenCalledWith({
content: "User <@123> assigned to task 1.",
});
});
it("should execute correctly when removing assign", async() => {
expect.assertions(4);
const mockBot = {
database: {
tasks: {
findFirst: vi.fn().mockResolvedValue({
assignees: [ "123", "456" ],
numericalId: 1,
}),
update: vi.fn().mockResolvedValue({}),
},
},
} as never;
const mockInteraction = {
deferReply: vi.fn(),
editReply: vi.fn(),
options: {
getInteger: vi.fn().mockReturnValue(1),
getUser: vi.fn().mockReturnValue({ id: "123" }),
},
} as never;
await assign.run(mockBot, mockInteraction);
expect(
mockInteraction.deferReply,
"should defer the reply",
).toHaveBeenCalledWith({ ephemeral: true });
expect(
mockBot.database.tasks.findFirst,
"should call findFirst",
).toHaveBeenCalledWith({
where: {
numericalId: 1,
},
});
expect(
mockBot.database.tasks.update,
"should call update",
).toHaveBeenCalledWith({
data: {
assignees: [ "456" ],
},
where: {
numericalId: 1,
},
});
expect(
mockInteraction.editReply,
"should call editReply",
).toHaveBeenCalledWith({
content: "User <@123> unassigned from task 1.",
});
});
it("should execute correctly when task not found", async() => {
expect.assertions(4);
vi.resetAllMocks();
const mockBot = {
database: {
tasks: {
findFirst: vi.fn().mockResolvedValue(null),
update: vi.fn().mockResolvedValue({}),
},
},
} as never;
const mockInteraction = {
deferReply: vi.fn(),
editReply: vi.fn(),
options: {
getInteger: vi.fn().mockReturnValue(1),
getUser: vi.fn().mockReturnValue({ id: "123" }),
},
} as never;
await assign.run(mockBot, mockInteraction);
expect(
mockInteraction.deferReply,
"should defer the reply",
).toHaveBeenCalledWith({ ephemeral: true });
expect(
mockBot.database.tasks.findFirst,
"should call findFirst",
).toHaveBeenCalledWith({
where: {
numericalId: 1,
},
});
expect(
mockBot.database.tasks.update,
"should not call update",
).not.toHaveBeenCalled();
expect(
mockInteraction.editReply,
"should call editReply",
).toHaveBeenCalledWith({
content: "Task 1 not found.",
});
});
it("should handle errors correctly", async() => {
expect.assertions(1);
await assign.run(
{} as never,
{ editReply: vi.fn(), replied: false, reply: vi.fn() } as never,
);
expect(errorHandler, "should call error handler").toHaveBeenCalledTimes(1);
});
it("should handle errors with interaction.reply correctly", async() => {
expect.assertions(1);
vi.resetAllMocks();
await assign.run(
{} as never,
{ editReply: vi.fn(), replied: true, reply: vi.fn() } as never,
);
expect(errorHandler, "should call error handler").toHaveBeenCalledTimes(1);
});
});