uwcsc / codeybot

Discord CodeyBot used for mock interviewing, suggestions and other features.
19 stars 18 forks source link

[FEATURE] Add unit tests for the bot #251

Closed mcpenguin closed 1 year ago

mcpenguin commented 2 years ago

Describe the problem and the impact.

We currently have no unit/integration tests for the bot. Thus, it is hard to notice if we have broken something else in the code when we add new features or refactor existing changes. CodeyBot is big enough and has enough features that I think it warrants a unit test framework.

Describe your proposed solution(s).

Add a unit/integration test framework to the bot.

mcpenguin commented 2 years ago

I'm looking at https://www.testim.io/blog/typescript-unit-testing-101/.

This is also useful probably: https://github.com/discordjs/discord.js/discussions/6179#discussioncomment-1044220

mcpenguin commented 2 years ago

Unit tests for the business logic of each command (ie executeCommand in the commandDetails classes) should not be too hard to implement; we might just need to mock any database calls.

The harder thing to implement would be integration tests, since Sapphire and discord.js might not be as easy to mock. From the Sapphire server:

For integration testing the real problem is mocking DiscordJS / the discord API tbh.

For unit testing you could simply create fake message / interaction objects with only those properties you require (I.e. create builders), pass them through a run function and assert the result.

You'll still have to mock out some of the DJS stuff so you can spy on the results and you'll have to construct your instance of Sapphire client to ensure pieces are loaded at some setup phase but other than that it should be doable. Mocking/spies is therefore where the most work resides.