generated from nhcarrigan/template
### 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>
100 lines
2.4 KiB
TypeScript
100 lines
2.4 KiB
TypeScript
/**
|
|
* @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(),
|
|
};
|
|
});
|
|
});
|
|
});
|