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:
2024-09-30 01:41:25 +00:00
committed by Naomi the Technomancer
parent da6fbfd45e
commit 296a50fedd
49 changed files with 4816 additions and 3 deletions

View File

@ -0,0 +1,33 @@
/**
* @copyright nhcarrigan
* @license Naomi's Public License
* @author Naomi Carrigan
*/
import { describe, it, expect } from "vitest";
import { displayCommandCurl } from "../../src/utils/displayCommandCurl.js";
const expectedCommandObject = `[{"options":[],"name":"create","description":"Create a new task.","contexts":[0],"type":1},{"options":[],"name":"update","description":"Update a task.","contexts":[0],"type":1},{"options":[{"min_value":1,"type":4,"name":"task","description":"The task number.","required":true},{"type":3,"choices":[{"name":"Low","value":"low"},{"name":"Medium","value":"medium"},{"name":"High","value":"high"},{"name":"Critical","value":"critical"},{"name":"None","value":"none"}],"name":"priority","description":"The priority level.","required":true}],"name":"priority","description":"Set the priority of a task.","contexts":[0],"type":1},{"options":[{"min_value":1,"type":4,"name":"task","description":"The task number.","required":true},{"type":3,"name":"tag","description":"The tag.","required":true}],"name":"tag","description":"Add or remove a tag from a task.","contexts":[0],"type":1},{"options":[{"min_value":1,"type":4,"name":"task","description":"The task number.","required":true},{"name":"assignee","description":"The user to (un)assign.","required":true,"type":6}],"name":"assign","description":"Add or remove someone as a task assignee.","contexts":[0],"type":1},{"options":[{"type":3,"choices":[{"name":"Low","value":"low"},{"name":"Medium","value":"medium"},{"name":"High","value":"high"},{"name":"Critical","value":"critical"},{"name":"None","value":"none"}],"name":"priority","description":"List tasks under this priority.","required":false},{"type":3,"name":"tag","description":"List tasks with this tag.","required":false},{"name":"assignee","description":"List tasks assigned to this user.","required":false,"type":6},{"name":"completed","description":"List completed tasks.","required":false,"type":5}],"name":"list","description":"List all tasks, with optional filters.","contexts":[0],"type":1},{"options":[{"min_value":1,"type":4,"name":"id","description":"The ID of the task to view.","required":true}],"name":"view","description":"View a task by its ID.","contexts":[0],"type":1},{"options":[{"min_value":1,"type":4,"name":"id","description":"The ID of the task to complete.","required":true}],"name":"complete","description":"Mark a task as completed.","contexts":[0],"type":1},{"options":[{"min_value":1,"type":4,"name":"id","description":"The ID of the task to delete.","required":true}],"name":"delete","description":"Mark a task as deleted. WARNING: This will scrub all PII from the task and CANNOT be undone.","contexts":[0],"type":1}]`;
describe("display command curl", () => {
it("should return the expected string", () => {
expect.assertions(1);
const string
= displayCommandCurl({ discord: { user: { id: "123" } } } as never);
expect(string, "did not return valid curl string").toBe(`curl -X PUT -H "Authorization: Bot {TOKEN}" -H "Content-Type: application/json" --data '${expectedCommandObject}' https://discord.com/api/v10/applications/123/commands`);
});
it("should handle the fallback ID", () => {
expect.assertions(1);
const string = displayCommandCurl({ discord: { user: {} } } as never);
expect(string, "did not return valid curl string").toBe(`curl -X PUT -H "Authorization: Bot {TOKEN}" -H "Content-Type: application/json" --data '${expectedCommandObject}' https://discord.com/api/v10/applications/{ID}/commands`);
});
it("should include all commands in the payload", () => {
expect.assertions(1);
const string
= displayCommandCurl({ discord: { user: { id: "123" } } } as never);
expect(string, "missing create command").toContain("\"name\":\"create\"");
});
});

View File

@ -0,0 +1,96 @@
/**
* @copyright nhcarrigan
* @license Naomi's Public License
* @author Naomi Carrigan
*/
import { EmbedBuilder } from "discord.js";
import { describe, it, expect, vi } from "vitest";
import { errorHandler } from "../../src/utils/errorHandler.ts";
import { sendDebugLog } from "../../src/utils/sendDebugLog.ts";
vi.mock("../../src/utils/sendDebugLog.ts", () => {
return {
sendDebugLog: vi.fn(),
};
});
const mockBot = {
env: {
discordDebugWebhook: {
send: vi.fn(),
},
},
};
describe("errorHandler", () => {
it("should call sendDebugLog", async() => {
expect.assertions(1);
await errorHandler(mockBot as never, "test", new Error("Test error"));
expect(sendDebugLog, "should send debug log").toHaveBeenCalledTimes(1);
});
it("should properly format the embed", async() => {
expect.assertions(1);
const error = new Error("Test error");
const id = await errorHandler(mockBot as never, "test", error);
expect(sendDebugLog, "should send debug log").toHaveBeenCalledWith(
mockBot,
{
embeds: [
new EmbedBuilder({
description: error.message,
fields: [
{
name: "Stack",
value: `\`\`\`\n${String(error.stack).slice(0, 1000)}`,
},
],
footer: {
// eslint-disable-next-line @typescript-eslint/naming-convention
icon_url: undefined,
text: `Error ID: ${id}`,
},
title: "Error: test",
}),
],
},
);
});
it("should handle non-error objects", async() => {
expect.assertions(1);
const id = await errorHandler(mockBot as never, "test", "Test error");
expect(sendDebugLog, "should send debug log").toHaveBeenCalledWith(
mockBot,
{
embeds: [
new EmbedBuilder({
description: "Test error",
footer: {
// eslint-disable-next-line @typescript-eslint/naming-convention
icon_url: undefined,
text: `Error ID: ${id}`,
},
title: "Error: test",
}),
],
},
);
});
it("should call the reply function if provided", async() => {
expect.assertions(1);
const replyFunction = vi.fn().mockName("reply");
const id = await errorHandler(
mockBot as never,
"test",
new Error("Test error"),
replyFunction,
);
expect(replyFunction, "should send error ID to user").toHaveBeenCalledWith({
content: `Oops! Something went wrong! Please reach out to us in our [support server](https://chat.nhcarrigan.com) and bring this error ID: ${id}`,
ephemeral: true,
});
});
});

View File

@ -0,0 +1,63 @@
/**
* @copyright nhcarrigan
* @license Naomi's Public License
* @author Naomi Carrigan
*/
import { describe, it, expect, vi } from "vitest";
import { sendDebugLog } from "../../src/utils/sendDebugLog.ts";
const mockBot = {
discord: {
user: {
displayAvatarURL: vi.
fn().
mockReturnValue("https://cdn.nhcarrigan.com/nhcarrigan.png"),
username: "Tasks",
},
},
env: {
discordDebugWebhook: {
send: vi.fn(),
},
},
};
describe("send debug log", () => {
it("should send a message to the webhook", () => {
expect.assertions(2);
sendDebugLog(mockBot as never, { content: "Test message." });
expect(
mockBot.env.discordDebugWebhook.send,
"should send message",
).toHaveBeenCalledTimes(1);
expect(
mockBot.env.discordDebugWebhook.send,
"should send message",
).toHaveBeenCalledWith({
avatarURL: "https://cdn.nhcarrigan.com/nhcarrigan.png",
content: "Test message.",
username: "Tasks",
});
});
it("should fallback when no user", () => {
expect.assertions(2);
// @ts-expect-error - Testing fallback when user is undefined.
mockBot.discord.user = undefined;
vi.resetAllMocks();
sendDebugLog(mockBot as never, { content: "Test message." });
expect(
mockBot.env.discordDebugWebhook.send,
"should send message",
).toHaveBeenCalledTimes(1);
expect(
mockBot.env.discordDebugWebhook.send,
"should send message",
).toHaveBeenCalledWith({
avatarURL: "https://cdn.nhcarrigan.com/profile.png",
content: "Test message.",
username: "RIG Task Bot",
});
});
});

View File

@ -0,0 +1,63 @@
/**
* @copyright nhcarrigan
* @license Naomi's Public License
* @author Naomi Carrigan
*/
import { describe, it, expect, afterAll, beforeAll } from "vitest";
import { validateEnvironmentVariables }
from "../../src/utils/validateEnvironmentVariables.js";
describe("validate environment variables", () => {
beforeAll(() => {
delete process.env.DISCORD_TOKEN;
delete process.env.DISCORD_DEBUG_WEBHOOK;
});
afterAll(() => {
delete process.env.DISCORD_TOKEN;
delete process.env.DISCORD_DEBUG_WEBHOOK;
});
it("should throw when DISCORD_TOKEN is not set", () => {
expect.assertions(1);
expect(() => {
validateEnvironmentVariables();
},
"did not throw on missing DISCORD_TOKEN").
toThrow(new ReferenceError("DISCORD_TOKEN cannot be undefined."));
});
it("should throw when DISCORD_DEBUG_WEBHOOK is not set", () => {
expect.assertions(1);
process.env.DISCORD_TOKEN = "test";
expect(() => {
validateEnvironmentVariables();
}
, "did not throw on missing DISCORD_DEBUG_WEBHOOK").
toThrow(new ReferenceError("DISCORD_DEBUG_WEBHOOK cannot be undefined."));
});
it("should throw when MONGO_URI is not set", () => {
expect.assertions(1);
process.env.DISCORD_DEBUG_WEBHOOK
// eslint-disable-next-line stylistic/max-len
= "https://discord.com/api/webhooks/11111111111111111/AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
expect(() => {
validateEnvironmentVariables();
},
"did not throw on missing MONGO_URI").
toThrow(new ReferenceError("MONGO_URI cannot be undefined."));
});
it("should return the expected environment variables", () => {
expect.assertions(2);
process.env.MONGO_URI = "test";
const result = validateEnvironmentVariables();
expect(result.discordToken, "did not return correct token").toBe("test");
expect(result.discordDebugWebhook.url,
"did not correctly instantiate debug hook").
// eslint-disable-next-line stylistic/max-len
toBe("https://discord.com/api/webhooks/11111111111111111/AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA");
});
});