Compare commits

...

53 commits

Author SHA1 Message Date
Ethan Lane 451c53f2cd WIP: Start of basentity tests
All checks were successful
Test / build (push) Successful in 16s
2024-06-29 09:26:17 +01:00
Ethan Lane 345a9efbc2 Create warn command tests 2024-06-29 09:16:13 +01:00
Ethan Lane 97bd3d675b WIP: Give warn command snapshots names
All checks were successful
Test / build (push) Successful in 15s
2024-06-28 18:24:17 +01:00
Ethan Lane 42ec4a99db WIP: Start of warn command tests
All checks were successful
Test / build (push) Successful in 16s
2024-06-28 18:21:28 +01:00
Ethan Lane 739bd0713d WIP: Migrate to yarn
Some checks failed
Test / build (push) Failing after 16s
2024-06-27 18:31:59 +01:00
Ethan Lane 16bc43c30e Merge branch 'develop' into feature/258-fix-tests 2024-06-27 17:51:43 +01:00
RenovateBot 7ccfa34562 Update dependency typescript to v5.5.2 (#440)
All checks were successful
Deploy To Stage / build (push) Successful in 7s
Deploy To Stage / deploy (push) Successful in 17s
This PR contains the following updates:

| Package | Type | Update | Change |
|---|---|---|---|
| [typescript](https://www.typescriptlang.org/) ([source](https://github.com/Microsoft/TypeScript)) | devDependencies | minor | [`5.4.5` -> `5.5.2`](https://renovatebot.com/diffs/npm/typescript/5.4.5/5.5.2) |

---

### Release Notes

<details>
<summary>Microsoft/TypeScript (typescript)</summary>

### [`v5.5.2`](https://github.com/microsoft/TypeScript/releases/tag/v5.5.2): TypeScript 5.5

[Compare Source](https://github.com/Microsoft/TypeScript/compare/v5.4.5...v5.5.2)

For release notes, check out the [release announcement](https://devblogs.microsoft.com/typescript/announcing-typescript-5-5/).

For the complete list of fixed issues, check out the

-   [fixed issues query for TypeScript v5.5.2 (Stable)](https://github.com/Microsoft/TypeScript/issues?utf8=%E2%9C%93\&q=is%3Aissue+milestone%3A%22TypeScript+5.5.2%22+is%3Aclosed+).
-   [fixed issues query for TypeScript v5.5.1 (RC)](https://github.com/Microsoft/TypeScript/issues?utf8=%E2%9C%93\&q=is%3Aissue+milestone%3A%22TypeScript+5.5.1%22+is%3Aclosed+).
-   [fixed issues query for TypeScript v5.5.0 (Beta)](https://github.com/Microsoft/TypeScript/issues?utf8=%E2%9C%93\&q=is%3Aissue+milestone%3A%22TypeScript+5.5.0%22+is%3Aclosed+).

Downloads are available on:

-   [npm](https://www.npmjs.com/package/typescript)
-   [NuGet package](https://www.nuget.org/packages/Microsoft.TypeScript.MSBuild)

</details>

---

### Configuration

📅 **Schedule**: Branch creation - At any time (no schedule defined), Automerge - At any time (no schedule defined).

🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update again.

---

 - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check this box

---

This PR has been generated by [Renovate Bot](https://github.com/renovatebot/renovate).
<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNy4wLjAiLCJ1cGRhdGVkSW5WZXIiOiIzNy4wLjAiLCJ0YXJnZXRCcmFuY2giOiJkZXZlbG9wIn0=-->

Reviewed-on: #440
Reviewed-by: Vylpes <ethan@vylpes.com>
Co-authored-by: Renovate Bot <renovate@vylpes.com>
Co-committed-by: Renovate Bot <renovate@vylpes.com>
2024-06-24 18:54:35 +01:00
RenovateBot f1c7cdc6d6 Update dependency np to v10.0.6 (#439)
All checks were successful
Deploy To Stage / build (push) Successful in 7s
Deploy To Stage / deploy (push) Successful in 17s
This PR contains the following updates:

| Package | Type | Update | Change |
|---|---|---|---|
| [np](https://github.com/sindresorhus/np) | devDependencies | patch | [`10.0.5` -> `10.0.6`](https://renovatebot.com/diffs/npm/np/10.0.5/10.0.6) |

---

### Release Notes

<details>
<summary>sindresorhus/np (np)</summary>

### [`v10.0.6`](https://github.com/sindresorhus/np/releases/tag/v10.0.6)

[Compare Source](https://github.com/sindresorhus/np/compare/v10.0.5...v10.0.6)

-   Fix usage with `pnpm` and `--any-branch` or `--branch` ([#&#8203;745](https://github.com/sindresorhus/np/issues/745))  [`d0c1493`](https://github.com/sindresorhus/np/commit/d0c1493)

</details>

---

### Configuration

📅 **Schedule**: Branch creation - At any time (no schedule defined), Automerge - At any time (no schedule defined).

🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update again.

---

 - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check this box

---

This PR has been generated by [Renovate Bot](https://github.com/renovatebot/renovate).
<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNy4wLjAiLCJ1cGRhdGVkSW5WZXIiOiIzNy4wLjAiLCJ0YXJnZXRCcmFuY2giOiJkZXZlbG9wIn0=-->

Reviewed-on: #439
Reviewed-by: Vylpes <ethan@vylpes.com>
Co-authored-by: Renovate Bot <renovate@vylpes.com>
Co-committed-by: Renovate Bot <renovate@vylpes.com>
2024-06-24 18:53:52 +01:00
RenovateBot 1371a30eee Update dependency ts-jest to v29.1.5 (#438)
All checks were successful
Deploy To Stage / build (push) Successful in 7s
Deploy To Stage / deploy (push) Successful in 16s
This PR contains the following updates:

| Package | Type | Update | Change |
|---|---|---|---|
| [ts-jest](https://kulshekhar.github.io/ts-jest) ([source](https://github.com/kulshekhar/ts-jest)) | dependencies | patch | [`29.1.2` -> `29.1.5`](https://renovatebot.com/diffs/npm/ts-jest/29.1.2/29.1.5) |

---

### Release Notes

<details>
<summary>kulshekhar/ts-jest (ts-jest)</summary>

### [`v29.1.5`](https://github.com/kulshekhar/ts-jest/blob/HEAD/CHANGELOG.md#2915-2024-06-16)

[Compare Source](https://github.com/kulshekhar/ts-jest/compare/v29.1.4...v29.1.5)

##### Bug Fixes

-   build(deps-dev): bump braces ([5560334](https://github.com/kulshekhar/ts-jest/commit/5560334)), ([59026b4](https://github.com/kulshekhar/ts-jest/commit/59026b4)), ([0d9e359](https://github.com/kulshekhar/ts-jest/commit/0d9e359))

### [`v29.1.4`](https://github.com/kulshekhar/ts-jest/blob/HEAD/CHANGELOG.md#2914-2024-05-28)

[Compare Source](https://github.com/kulshekhar/ts-jest/compare/v29.1.3...v29.1.4)

##### Bug Fixes

-   fix(transformer): allow transforming of .cts/.mts extensions. ([b8f6eaa](https://github.com/kulshekhar/ts-jest/commit/b8f6eaa)), closes [#&#8203;3996](https://github.com/kulshekhar/ts-jest/issues/3996)

##### Features

-   feat: make cli generate esm config based on `type: "module"` ([81a5f64](https://github.com/kulshekhar/ts-jest/commit/81a5f64)), closes [#&#8203;4210](https://github.com/kulshekhar/ts-jest/issues/4210) [#&#8203;4012](https://github.com/kulshekhar/ts-jest/issues/4012)

### [`v29.1.3`](https://github.com/kulshekhar/ts-jest/blob/HEAD/CHANGELOG.md#2913-2024-05-21)

[Compare Source](https://github.com/kulshekhar/ts-jest/compare/v29.1.2...v29.1.3)

##### Bug Fixes

-   add `@jest/transform` as an optional peer dependency ([0ba7f86](0ba7f861c3))
-   bring back Node 14 support ([eda56a7](eda56a7797))

##### Performance Improvements

-   remove ts resolved module cache file ([4c88da5](4c88da5899))

</details>

---

### Configuration

📅 **Schedule**: Branch creation - At any time (no schedule defined), Automerge - At any time (no schedule defined).

🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update again.

---

 - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check this box

---

This PR has been generated by [Renovate Bot](https://github.com/renovatebot/renovate).
<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNy4wLjAiLCJ1cGRhdGVkSW5WZXIiOiIzNy4wLjAiLCJ0YXJnZXRCcmFuY2giOiJkZXZlbG9wIn0=-->

Reviewed-on: #438
Reviewed-by: Vylpes <ethan@vylpes.com>
Co-authored-by: Renovate Bot <renovate@vylpes.com>
Co-committed-by: Renovate Bot <renovate@vylpes.com>
2024-06-24 18:53:05 +01:00
RenovateBot 34398e5657 Update actions/setup-node action to v4 (#437)
All checks were successful
Deploy To Stage / build (push) Successful in 7s
Deploy To Stage / deploy (push) Successful in 16s
This PR contains the following updates:

| Package | Type | Update | Change |
|---|---|---|---|
| [actions/setup-node](https://github.com/actions/setup-node) | action | major | `v1` -> `v4` |

---

### Release Notes

<details>
<summary>actions/setup-node (actions/setup-node)</summary>

### [`v4`](https://github.com/actions/setup-node/compare/v3...v4)

[Compare Source](https://github.com/actions/setup-node/compare/v3...v4)

### [`v3`](https://github.com/actions/setup-node/compare/v2...v3)

[Compare Source](https://github.com/actions/setup-node/compare/v2...v3)

### [`v2`](https://github.com/actions/setup-node/compare/v1...v2)

[Compare Source](https://github.com/actions/setup-node/compare/v1...v2)

</details>

---

### Configuration

📅 **Schedule**: Branch creation - At any time (no schedule defined), Automerge - At any time (no schedule defined).

🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update again.

---

 - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check this box

---

This PR has been generated by [Renovate Bot](https://github.com/renovatebot/renovate).
<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNy4wLjAiLCJ1cGRhdGVkSW5WZXIiOiIzNy4wLjAiLCJ0YXJnZXRCcmFuY2giOiJkZXZlbG9wIn0=-->

Reviewed-on: #437
Reviewed-by: Vylpes <ethan@vylpes.com>
Co-authored-by: Renovate Bot <renovate@vylpes.com>
Co-committed-by: Renovate Bot <renovate@vylpes.com>
2024-06-24 18:52:23 +01:00
RenovateBot cd0737cf3b Update dependency @discordjs/rest to v2.3.0 (#435)
All checks were successful
Deploy To Stage / build (push) Successful in 7s
Deploy To Stage / deploy (push) Successful in 15s
This PR contains the following updates:

| Package | Type | Update | Change |
|---|---|---|---|
| [@discordjs/rest](https://discord.js.org) ([source](https://github.com/discordjs/discord.js)) | dependencies | minor | [`2.2.0` -> `2.3.0`](https://renovatebot.com/diffs/npm/@discordjs%2frest/2.2.0/2.3.0) |

---

### Release Notes

<details>
<summary>discordjs/discord.js (@&#8203;discordjs/rest)</summary>

### [`v2.3.0`](https://github.com/discordjs/discord.js/blob/HEAD/packages/rest/CHANGELOG.md#discordjsrest230---2024-05-04)

[Compare Source](https://github.com/discordjs/discord.js/compare/@discordjs/rest@2.2.0...@discordjs/rest@2.3.0)

#### Bug Fixes

-   Anchor link for events ([0efd1be](0efd1bea46))

#### Documentation

-   Split docs.api.json into multiple json files ([597340f](597340f288))
-   Remove hyphen after `@returns` ([#&#8203;9989](https://github.com/discordjs/discord.js/issues/9989)) ([e9ff991](e9ff99101b))

#### Features

-   Local and preview detection ([79fbda3](79fbda3aac))
-   **REST:** Dynamic rate limit offsets ([#&#8203;10099](https://github.com/discordjs/discord.js/issues/10099)) ([278396e](278396e815))

#### Refactor

-   Docs ([#&#8203;10126](https://github.com/discordjs/discord.js/issues/10126)) ([18cce83](18cce83d80))
-   Use interfaces for AsyncEventEmitter event maps ([#&#8203;10044](https://github.com/discordjs/discord.js/issues/10044)) ([adfd9cd](adfd9cd3b3))

#### Styling

-   Fix up lint ([d869d9b](d869d9b3fe))

#### Testing

-   Skip flaky rest test ([#&#8203;10234](https://github.com/discordjs/discord.js/issues/10234)) ([dc8f149](dc8f14967c))

</details>

---

### Configuration

📅 **Schedule**: Branch creation - At any time (no schedule defined), Automerge - At any time (no schedule defined).

🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update again.

---

 - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check this box

---

This PR has been generated by [Renovate Bot](https://github.com/renovatebot/renovate).
<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNy4wLjAiLCJ1cGRhdGVkSW5WZXIiOiIzNy4wLjAiLCJ0YXJnZXRCcmFuY2giOiJkZXZlbG9wIn0=-->

Reviewed-on: #435
Reviewed-by: Vylpes <ethan@vylpes.com>
Co-authored-by: Renovate Bot <renovate@vylpes.com>
Co-committed-by: Renovate Bot <renovate@vylpes.com>
2024-06-24 18:51:01 +01:00
RenovateBot 5f22a31398 Update dependency @types/node to v20.14.8 (#434)
All checks were successful
Deploy To Stage / build (push) Successful in 8s
Deploy To Stage / deploy (push) Successful in 15s
This PR contains the following updates:

| Package | Type | Update | Change |
|---|---|---|---|
| [@types/node](https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/node) ([source](https://github.com/DefinitelyTyped/DefinitelyTyped)) | devDependencies | minor | [`20.12.8` -> `20.14.8`](https://renovatebot.com/diffs/npm/@types%2fnode/20.12.8/20.14.8) |

---

### Configuration

📅 **Schedule**: Branch creation - At any time (no schedule defined), Automerge - At any time (no schedule defined).

🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update again.

---

 - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check this box

---

This PR has been generated by [Renovate Bot](https://github.com/renovatebot/renovate).
<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNy4wLjAiLCJ1cGRhdGVkSW5WZXIiOiIzNy4wLjAiLCJ0YXJnZXRCcmFuY2giOiJkZXZlbG9wIn0=-->

Reviewed-on: #434
Reviewed-by: Vylpes <ethan@vylpes.com>
Co-authored-by: Renovate Bot <renovate@vylpes.com>
Co-committed-by: Renovate Bot <renovate@vylpes.com>
2024-06-24 18:50:03 +01:00
Ethan Lane 581c275adf WIP: Start of rules command tests
All checks were successful
Test / build (push) Successful in 17s
2024-05-18 10:14:57 +01:00
Ethan Lane 3e198308b9 Add poll command tests 2024-05-18 10:07:54 +01:00
Ethan Lane 39231ddc16 WIP: Start of poll command tests
All checks were successful
Test / build (push) Successful in 16s
2024-05-13 16:23:51 +01:00
Ethan Lane 91bc444e71 Add kick command tests 2024-05-13 15:58:14 +01:00
Ethan Lane d0a8db718e WIP: Start of kick command tests
All checks were successful
Test / build (push) Successful in 16s
2024-05-11 14:03:05 +01:00
Ethan Lane 2841ed5463 Create ignore command tests
All checks were successful
Test / build (push) Successful in 17s
2024-05-11 10:58:50 +01:00
Ethan Lane ac51eb7f40 Create disable command tests 2024-05-11 10:42:38 +01:00
Ethan Lane bd6858daa3 Create config command tests 2024-05-11 09:44:36 +01:00
Ethan Lane 2377ec165f WIP: Start of config tests
All checks were successful
Test / build (push) Successful in 16s
2024-05-07 18:13:41 +01:00
Ethan Lane 3d8b5a8347 WIP: Start of config command tests
All checks were successful
Test / build (push) Successful in 16s
2024-05-06 16:34:17 +01:00
Ethan Lane 3afc28e02b Merge branch 'develop' into feature/258-fix-tests 2024-05-06 16:13:21 +01:00
RenovateBot 932d3ac3d5 Update dependency jest-mock-extended to v3.0.7 (#432)
All checks were successful
Deploy To Stage / build (push) Successful in 7s
Deploy To Stage / deploy (push) Successful in 14s
This PR contains the following updates:

| Package | Type | Update | Change |
|---|---|---|---|
| [jest-mock-extended](https://github.com/marchaos/jest-mock-extended) | dependencies | patch | [`3.0.6` -> `3.0.7`](https://renovatebot.com/diffs/npm/jest-mock-extended/3.0.6/3.0.7) |

---

### Release Notes

<details>
<summary>marchaos/jest-mock-extended (jest-mock-extended)</summary>

### [`v3.0.7`](https://github.com/marchaos/jest-mock-extended/releases/tag/3.0.7)

[Compare Source](77a64ae832...3.0.7)

Bumped ts-essentials

</details>

---

### Configuration

📅 **Schedule**: Branch creation - At any time (no schedule defined), Automerge - At any time (no schedule defined).

🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update again.

---

 - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check this box

---

This PR has been generated by [Renovate Bot](https://github.com/renovatebot/renovate).
<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNy4wLjAiLCJ1cGRhdGVkSW5WZXIiOiIzNy4wLjAiLCJ0YXJnZXRCcmFuY2giOiJkZXZlbG9wIn0=-->

Reviewed-on: #432
Co-authored-by: Renovate Bot <renovate@vylpes.com>
Co-committed-by: Renovate Bot <renovate@vylpes.com>
2024-05-06 16:11:58 +01:00
RenovateBot 6412ad0ad4 Update dependency @types/node to v20.12.8 (#431)
All checks were successful
Deploy To Stage / build (push) Successful in 10s
Deploy To Stage / deploy (push) Successful in 15s
This PR contains the following updates:

| Package | Type | Update | Change |
|---|---|---|---|
| [@types/node](https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/node) ([source](https://github.com/DefinitelyTyped/DefinitelyTyped)) | devDependencies | patch | [`20.12.7` -> `20.12.8`](https://renovatebot.com/diffs/npm/@types%2fnode/20.12.7/20.12.8) |

---

### Configuration

📅 **Schedule**: Branch creation - At any time (no schedule defined), Automerge - At any time (no schedule defined).

🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update again.

---

 - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check this box

---

This PR has been generated by [Renovate Bot](https://github.com/renovatebot/renovate).
<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNy4wLjAiLCJ1cGRhdGVkSW5WZXIiOiIzNy4wLjAiLCJ0YXJnZXRCcmFuY2giOiJkZXZlbG9wIn0=-->

Reviewed-on: #431
Co-authored-by: Renovate Bot <renovate@vylpes.com>
Co-committed-by: Renovate Bot <renovate@vylpes.com>
2024-05-06 16:11:17 +01:00
Ethan Lane 166bd80113 WIP: Start of config tests
All checks were successful
Test / build (push) Successful in 16s
2024-05-06 10:54:00 +01:00
Ethan Lane fa7e35b4dd Create code command tests 2024-05-06 10:24:04 +01:00
Ethan Lane 7e625b232e Create clear command tests
All checks were successful
Test / build (push) Successful in 17s
2024-05-04 16:48:00 +01:00
Ethan Lane 6323398e48 Merge branch 'develop' into feature/258-fix-tests 2024-05-04 16:09:30 +01:00
Ethan Lane 60972e5782 Fix bunny tests
All checks were successful
Test / build (push) Successful in 16s
2024-05-01 18:36:31 +01:00
Ethan Lane 74cdd04d27 Merge branch 'develop' into feature/258-fix-tests 2024-05-01 18:23:06 +01:00
RenovateBot f1c513bd16 Update actions/checkout action to v4 (#430)
All checks were successful
Deploy To Stage / build (push) Successful in 7s
Deploy To Stage / deploy (push) Successful in 16s
This PR contains the following updates:

| Package | Type | Update | Change |
|---|---|---|---|
| [actions/checkout](https://github.com/actions/checkout) | action | major | `v2` -> `v4` |

---

### Release Notes

<details>
<summary>actions/checkout (actions/checkout)</summary>

### [`v4`](https://github.com/actions/checkout/blob/HEAD/CHANGELOG.md#v414)

[Compare Source](https://github.com/actions/checkout/compare/v3...v4)

-   Disable `extensions.worktreeConfig` when disabling `sparse-checkout` by [@&#8203;jww3](https://github.com/jww3) in https://github.com/actions/checkout/pull/1692
-   Add dependabot config by [@&#8203;cory-miller](https://github.com/cory-miller) in https://github.com/actions/checkout/pull/1688
-   Bump the minor-actions-dependencies group with 2 updates by [@&#8203;dependabot](https://github.com/dependabot) in https://github.com/actions/checkout/pull/1693
-   Bump word-wrap from 1.2.3 to 1.2.5 by [@&#8203;dependabot](https://github.com/dependabot) in https://github.com/actions/checkout/pull/1643

### [`v3`](https://github.com/actions/checkout/blob/HEAD/CHANGELOG.md#v360)

[Compare Source](https://github.com/actions/checkout/compare/v2...v3)

-   [Fix: Mark test scripts with Bash'isms to be run via Bash](https://github.com/actions/checkout/pull/1377)
-   [Add option to fetch tags even if fetch-depth > 0](https://github.com/actions/checkout/pull/579)

</details>

---

### Configuration

📅 **Schedule**: Branch creation - At any time (no schedule defined), Automerge - At any time (no schedule defined).

🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update again.

---

 - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check this box

---

This PR has been generated by [Renovate Bot](https://github.com/renovatebot/renovate).
<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNy4wLjAiLCJ1cGRhdGVkSW5WZXIiOiIzNy4wLjAiLCJ0YXJnZXRCcmFuY2giOiJkZXZlbG9wIn0=-->

Reviewed-on: #430
Co-authored-by: Renovate Bot <renovate@vylpes.com>
Co-committed-by: Renovate Bot <renovate@vylpes.com>
2024-04-29 17:51:22 +01:00
RenovateBot 942fea12bf Update dependency minimatch to v9.0.4 (#422)
All checks were successful
Deploy To Stage / build (push) Successful in 7s
Deploy To Stage / deploy (push) Successful in 15s
This PR contains the following updates:

| Package | Type | Update | Change |
|---|---|---|---|
| [minimatch](https://github.com/isaacs/minimatch) | dependencies | patch | [`9.0.3` -> `9.0.4`](https://renovatebot.com/diffs/npm/minimatch/9.0.3/9.0.4) |

---

### Release Notes

<details>
<summary>isaacs/minimatch (minimatch)</summary>

### [`v9.0.4`](https://github.com/isaacs/minimatch/compare/v9.0.3...v9.0.4)

[Compare Source](https://github.com/isaacs/minimatch/compare/v9.0.3...v9.0.4)

</details>

---

### Configuration

📅 **Schedule**: Branch creation - At any time (no schedule defined), Automerge - At any time (no schedule defined).

🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update again.

---

 - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check this box

---

This PR has been generated by [Renovate Bot](https://github.com/renovatebot/renovate).
<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNy4wLjAiLCJ1cGRhdGVkSW5WZXIiOiIzNy4wLjAiLCJ0YXJnZXRCcmFuY2giOiJkZXZlbG9wIn0=-->

Reviewed-on: #422
Co-authored-by: Renovate Bot <renovate@vylpes.com>
Co-committed-by: Renovate Bot <renovate@vylpes.com>
2024-04-29 17:50:11 +01:00
Ethan Lane ffff5e8c7f Add bunny command tests
All checks were successful
continuous-integration/drone/push Build is passing
2024-03-30 16:53:18 +00:00
Ethan Lane 5409ecaffc Create ban command tests
All checks were successful
continuous-integration/drone/push Build is passing
2024-03-28 19:02:14 +00:00
Ethan Lane e860a19bfe Create audits command tests
Some checks failed
continuous-integration/drone/push Build is failing
2024-03-27 16:49:39 +00:00
Ethan Lane e7fde3a2ea Add role command tests 2024-03-27 13:57:36 +00:00
Ethan Lane d5a5282371 Create config tests 2024-03-27 12:51:51 +00:00
Ethan Lane 00f6a60a43 Add entry command tests 2024-03-27 12:27:20 +00:00
Ethan Lane 946422b2ac Create remove lobby tests 2024-03-27 11:51:26 +00:00
Ethan Lane 5e30962053 Create lobby command tests 2024-03-27 11:31:32 +00:00
Ethan Lane f770e3f4fc Add list lobby tests 2024-03-27 11:01:18 +00:00
Ethan Lane 53a084b3b1 Create add lobby tests 2024-03-27 10:40:42 +00:00
Ethan Lane ca64ede1ad WIP: Start of lobbyadd tests
All checks were successful
continuous-integration/drone/push Build is passing
2024-03-25 18:11:38 +00:00
Ethan Lane fc975f4602 Create util tests 2024-03-25 17:59:34 +00:00
Ethan Lane 39b2a495a8 Merge branch 'develop' into feature/258-fix-tests 2024-03-25 17:35:23 +00:00
Ethan Lane 6d2b323338 WIP: util tests
Some checks failed
continuous-integration/drone/push Build is failing
2024-03-25 17:31:45 +00:00
Ethan Lane 88d229b709 Create events tests
All checks were successful
continuous-integration/drone/push Build is passing
2024-03-22 17:40:36 +00:00
Ethan Lane 9c13ee6099 Add client tests
All checks were successful
continuous-integration/drone/push Build is passing
2024-03-20 18:30:58 +00:00
Ethan Lane 0f0dbee7de Merge branch 'develop' into feature/258-fix-tests
All checks were successful
continuous-integration/drone/push Build is passing
2024-03-18 18:47:50 +00:00
Ethan Lane 4e9ec89108 Add verify and button tests
All checks were successful
continuous-integration/drone/push Build is passing
2024-02-05 15:38:24 +00:00
Ethan Lane 69ef94dbb5 Add placeholder tests
All checks were successful
continuous-integration/drone/push Build is passing
2024-02-03 19:29:28 +00:00
Ethan Lane ea6bac2af1 Re-enable tests 2024-02-01 18:59:46 +00:00
84 changed files with 11629 additions and 16060 deletions

View file

@ -54,11 +54,10 @@ steps:
- yarn install --frozen-lockfile
- yarn build
# - name: test
# image: node
# commands:
# - yarn install --frozen-lockfile
# - yarn test
- name: test
image: node
commands:
- yarn test
trigger:
branch:

View file

@ -7,7 +7,7 @@
# any secret values.
BOT_TOKEN=
BOT_VER=3.2.1
BOT_VER=3.3.0
BOT_AUTHOR=Vylpes
BOT_OWNERID=147392775707426816
BOT_CLIENTID=682942374040961060

View file

@ -12,14 +12,14 @@ jobs:
runs-on: node
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4
- name: Use Node.js
uses: actions/setup-node@v1
uses: actions/setup-node@v4
with:
node-version: 18.x
- run: npm ci
- run: npm run build
- run: npm test
- run: yarn install --frozen-lockfile
- run: yarn build
- run: yarn test
- name: "Copy files over to location"
run: cp -r . ${{ secrets.PROD_REPO_PATH }}
@ -63,5 +63,5 @@ jobs:
&& (pm2 delete vylbot_prod || true) \
&& docker compose up -d \
&& sleep 10 \
&& npm run db:up \
&& pm2 start --name vylbot_prod dist/vylbot.js
&& yarn db:up \
&& pm2 start --name vylbot_prod dist/vylbot.js

View file

@ -12,14 +12,14 @@ jobs:
runs-on: node
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4
- name: Use Node.js
uses: actions/setup-node@v1
uses: actions/setup-node@v4
with:
node-version: 18.x
- run: npm ci
- run: npm run build
- run: npm test
- run: yarn install --frozen-lockfile
- run: yarn build
- run: yarn test
- name: "Copy files over to location"
run: cp -r . ${{ secrets.STAGE_REPO_PATH }}
@ -63,5 +63,5 @@ jobs:
&& (pm2 delete vylbot_stage || true) \
&& docker compose up -d \
&& sleep 10 \
&& npm run db:up \
&& pm2 start --name vylbot_stage dist/vylbot.js
&& yarn db:up \
&& pm2 start --name vylbot_stage dist/vylbot.js

View file

@ -14,11 +14,11 @@ jobs:
runs-on: node
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4
- name: Use Node.js
uses: actions/setup-node@v1
uses: actions/setup-node@v4
with:
node-version: 18.x
- run: npm ci
- run: npm run build
- run: npm test
- run: yarn install --frozen-lockfile
- run: yarn build
- run: yarn test

8815
package-lock.json generated

File diff suppressed because it is too large Load diff

View file

@ -8,7 +8,7 @@
"clean": "rm -rf node_modules/ dist/",
"build": "tsc",
"start": "node ./dist/vylbot",
"test": "echo true",
"test": "jest",
"db:up": "typeorm migration:run -d dist/database/dataSources/appDataSource.js",
"db:down": "typeorm migration:revert -d dist/database/dataSources/appDataSource.js",
"db:create": "typeorm migration:create ./src/database/migrations",
@ -35,7 +35,7 @@
"emoji-regex": "^10.0.0",
"jest": "^29.0.0",
"jest-mock-extended": "^3.0.0",
"minimatch": "9.0.3",
"minimatch": "9.0.4",
"mysql": "^2.18.1",
"random-bunny": "^2.1.6",
"ts-jest": "^29.0.0",

View file

@ -1,6 +1,5 @@
import { Client, Partials } from "discord.js";
import * as dotenv from "dotenv";
import { createConnection } from "typeorm";
import { EventType } from "../constants/EventType";
import ICommandItem from "../contracts/ICommandItem";
import IEventItem from "../contracts/IEventItem";
@ -14,32 +13,20 @@ import { ButtonEvent } from "../type/buttonEvent";
import CacheHelper from "../helpers/CacheHelper";
export class CoreClient extends Client {
private static _commandItems: ICommandItem[];
private static _eventItems: IEventItem[];
private static _buttonEvents: ButtonEventItem[];
public static commandItems: ICommandItem[];
public static eventItems: IEventItem[];
public static buttonEvents: ButtonEventItem[];
private _events: Events;
private _util: Util;
public static get commandItems(): ICommandItem[] {
return this._commandItems;
}
public static get eventItems(): IEventItem[] {
return this._eventItems;
}
public static get buttonEvents(): ButtonEventItem[] {
return this._buttonEvents;
}
constructor(intents: number[], partials: Partials[]) {
super({ intents: intents, partials: partials });
dotenv.config();
CoreClient._commandItems = [];
CoreClient._eventItems = [];
CoreClient._buttonEvents = [];
CoreClient.commandItems = [];
CoreClient.eventItems = [];
CoreClient.buttonEvents = [];
this._events = new Events();
this._util = new Util();
@ -51,9 +38,12 @@ export class CoreClient extends Client {
return;
}
await AppDataSource.initialize()
.then(() => console.log("Data Source Initialized"))
.catch((err) => console.error("Error Initialising Data Source", err));
try {
await AppDataSource.initialize();
} catch (err) {
console.error("Error Initialising Data Source", err);
return;
}
super.on("interactionCreate", this._events.onInteractionCreate);
super.on("ready", this._events.onReady);
@ -64,7 +54,7 @@ export class CoreClient extends Client {
await CacheHelper.UpdateServerCache(guild);
});
this._util.loadEvents(this, CoreClient._eventItems);
this._util.loadEvents(this, CoreClient.eventItems);
this._util.loadSlashCommands(this);
}
@ -75,7 +65,7 @@ export class CoreClient extends Client {
ServerId: serverId,
};
CoreClient._commandItems.push(item);
CoreClient.commandItems.push(item);
}
public static RegisterEvent(eventType: EventType, func: Function) {
@ -84,7 +74,7 @@ export class CoreClient extends Client {
ExecutionFunction: func,
};
CoreClient._eventItems.push(item);
CoreClient.eventItems.push(item);
}
public static RegisterButtonEvent(buttonId: string, event: ButtonEvent) {
@ -93,6 +83,6 @@ export class CoreClient extends Client {
Event: event,
};
CoreClient._buttonEvents.push(item);
CoreClient.buttonEvents.push(item);
}
}

View file

@ -8,10 +8,10 @@ export class Events {
if (interaction.isChatInputCommand()) {
ChatInputCommand.onChatInput(interaction);
}
if (interaction.isButton()) {
} else if (interaction.isButton()) {
Button.onButtonClicked(interaction);
} else {
console.error("Received interaction unable to interact with, ignoring");
}
}

View file

@ -53,6 +53,6 @@ export default class AddRole extends Command {
const entity = new eLobby(channel.channel.id, role.role.id, cooldown.value as number, gameName.value as string);
await entity.Save(eLobby, entity);
await interaction.reply(`Added \`${channel.name}\` as a new lobby channel with a cooldown of \`${cooldown.value} minutes \` and will ping \`${role.name}\` on use`);
await interaction.reply(`Added \`${channel.name}\` as a new lobby channel with a cooldown of \`${cooldown.value} minutes\` and will ping \`${role.name}\` on use`);
}
}

View file

@ -1,4 +1,4 @@
import { CacheType, CommandInteraction, EmbedBuilder, GuildBasedChannel, PermissionsBitField, SlashCommandBuilder } from "discord.js";
import { CacheType, CommandInteraction, EmbedBuilder, PermissionsBitField, SlashCommandBuilder } from "discord.js";
import { Command } from "../../../type/command";
import { default as eLobby } from "../../../database/entities/501231711271780357/Lobby";
import EmbedColours from "../../../constants/EmbedColours";

View file

@ -72,20 +72,18 @@ export default class Role extends Command {
const userRole = roleManager.cache.find(x => x.name == requestedRole.role!.name);
const assignRole = interaction.guild.roles.cache.find(x => x.id == requestedRole.role!.id);
if (!assignRole) return;
if (!assignRole.editable) {
if (!assignRole!.editable) {
await interaction.reply('Insufficient permissions. Please contact a moderator.');
return;
}
if (!userRole) {
await roleManager.add(assignRole);
await interaction.reply(`Gave role: \`${assignRole.name}\``);
await roleManager.add(assignRole!);
await interaction.reply(`Gave role: \`${assignRole!.name}\``);
} else {
await roleManager.remove(assignRole);
await interaction.reply(`Removed role: \`${assignRole.name}\``);
await roleManager.remove(assignRole!);
await interaction.reply(`Removed role: \`${assignRole!.name}\``);
}
}

View file

@ -60,8 +60,7 @@ export default class Audits extends Command {
{ name: 'Mute', value: AuditType.Mute.toString() },
{ name: 'Kick', value: AuditType.Kick.toString() },
{ name: 'Ban', value: AuditType.Ban.toString() },
)
.setRequired(true))
))
.addStringOption(option =>
option
.setName('reason')
@ -93,7 +92,7 @@ export default class Audits extends Command {
private async SendAuditForUser(interaction: CommandInteraction) {
if (!interaction.guildId) return;
const user = interaction.options.getUser('target');
const user = interaction.options.get('target', true).user!;
if (!user) {
await interaction.reply("User not found.");
@ -191,7 +190,7 @@ export default class Audits extends Command {
private async AddAudit(interaction: CommandInteraction) {
if (!interaction.guildId) return;
const user = interaction.options.getUser('target');
const user = interaction.options.get('target', true).user!;
const auditType = interaction.options.get('type');
const reasonInput = interaction.options.get('reason');
@ -209,4 +208,4 @@ export default class Audits extends Command {
await interaction.reply(`Created new audit with ID \`${audit.AuditId}\``);
}
}
}

View file

@ -26,6 +26,7 @@ export default class Bunny extends Command {
];
const random = Math.floor(Math.random() * subreddits.length);
const selectedSubreddit = subreddits[random];
const result = await randomBunny(selectedSubreddit, 'hot');

View file

@ -61,7 +61,7 @@ export default class Config extends Command {
]);
if (!server) {
await interaction.reply('Server not setup. Please use the setup command,');
await interaction.reply('Server not setup. Please use the setup command.');
return;
}
@ -70,13 +70,13 @@ export default class Config extends Command {
await this.SendHelpText(interaction);
break;
case 'reset':
await this.ResetValue(interaction);
await this.ResetValue(interaction, server);
break;
case 'get':
await this.GetValue(interaction);
await this.GetValue(interaction, server);
break;
case 'set':
await this.SetValue(interaction);
await this.SetValue(interaction, server);
break;
default:
await interaction.reply('Subcommand not found.');
@ -94,9 +94,7 @@ export default class Config extends Command {
await interaction.reply({ embeds: [ embed ]});
}
private async GetValue(interaction: CommandInteraction) {
if (!interaction.guildId) return;
private async GetValue(interaction: CommandInteraction, server: Server) {
const key = interaction.options.get('key');
if (!key || !key.value) {
@ -104,15 +102,6 @@ export default class Config extends Command {
return;
}
const server = await Server.FetchOneById<Server>(Server, interaction.guildId, [
"Settings",
]);
if (!server) {
await interaction.reply('Server not found.');
return;
}
const setting = server.Settings.filter(x => x.Key == key.value)[0];
if (setting) {
@ -128,7 +117,7 @@ export default class Config extends Command {
}
}
private async ResetValue(interaction: CommandInteraction) {
private async ResetValue(interaction: CommandInteraction, server: Server) {
if (!interaction.guildId) return;
const key = interaction.options.get('key');
@ -138,15 +127,6 @@ export default class Config extends Command {
return;
}
const server = await Server.FetchOneById<Server>(Server, interaction.guildId, [
"Settings",
]);
if (!server) {
await interaction.reply('Server not found.');
return;
}
const setting = server.Settings.filter(x => x.Key == key.value)[0];
if (!setting) {
@ -159,34 +139,18 @@ export default class Config extends Command {
await interaction.reply('The setting has been reset to the default.');
}
private async SetValue(interaction: CommandInteraction) {
if (!interaction.guildId) return;
const key = interaction.options.get('key');
const value = interaction.options.get('value');
if (!key || !key.value || !value || !value.value) {
await interaction.reply('Fields are required.');
return;
}
const server = await Server.FetchOneById<Server>(Server, interaction.guildId, [
"Settings",
]);
if (!server) {
await interaction.reply('Server not found.');
return;
}
private async SetValue(interaction: CommandInteraction, server: Server) {
const key = interaction.options.get('key', true);
const value = interaction.options.get('value', true);
const setting = server.Settings.filter(x => x.Key == key.value)[0];
if (setting) {
setting.UpdateBasicDetails(key.value.toString(), value.value.toString());
setting.UpdateBasicDetails(key.value!.toString(), value.value!.toString());
await setting.Save(Setting, setting);
} else {
const newSetting = new Setting(key.value.toString(), value.value.toString());
const newSetting = new Setting(key.value!.toString(), value.value!.toString());
await newSetting.Save(Setting, newSetting);

View file

@ -48,17 +48,12 @@ export default class Disable extends Command {
private async Add(interaction: CommandInteraction) {
if (!interaction.guildId) return;
const commandName = interaction.options.get('name');
if (!commandName || !commandName.value) {
await interaction.reply('Fields are required.');
return;
}
const commandName = interaction.options.get('name', true);
const disabledCommandsString = await SettingsHelper.GetSetting("commands.disabled", interaction.guildId);
const disabledCommands = disabledCommandsString != "" ? disabledCommandsString?.split(",") : [];
const disabledCommands = disabledCommandsString != undefined ? disabledCommandsString?.split(",") : [];
disabledCommands?.push(commandName.value.toString());
disabledCommands?.push(commandName.value!.toString());
await SettingsHelper.SetSetting("commands.disabled", interaction.guildId, disabledCommands!.join(","));
@ -68,23 +63,18 @@ export default class Disable extends Command {
private async Remove(interaction: CommandInteraction) {
if (!interaction.guildId) return;
const commandName = interaction.options.get('name');
if (!commandName || !commandName.value) {
await interaction.reply('Fields are required.');
return;
}
const commandName = interaction.options.get('name', true);
const disabledCommandsString = await SettingsHelper.GetSetting("commands.disabled", interaction.guildId);
const disabledCommands = disabledCommandsString != "" ? disabledCommandsString?.split(",") : [];
const disabledCommands = disabledCommandsString != undefined ? disabledCommandsString?.split(",") : [];
const disabledCommandsInstance = disabledCommands?.findIndex(x => x == commandName.value!.toString());
if (disabledCommandsInstance! > -1) {
disabledCommands?.splice(disabledCommandsInstance!, 1);
if (disabledCommandsInstance > -1) {
disabledCommands?.splice(disabledCommandsInstance, 1);
}
await SettingsHelper.SetSetting("commands.disabled", interaction.guildId, disabledCommands!.join(","));
await SettingsHelper.SetSetting("commands.disabled", interaction.guildId, disabledCommands.join(","));
await interaction.reply(`Enabled command ${commandName.value}`);
}

View file

@ -13,23 +13,16 @@ export default class Ignore extends Command {
}
public override async execute(interaction: CommandInteraction) {
if (!interaction.guildId) return;
const isChannelIgnored = await IgnoredChannel.IsChannelIgnored(interaction.guildId);
const isChannelIgnored = await IgnoredChannel.IsChannelIgnored(interaction.channelId);
if (isChannelIgnored) {
const entity = await IgnoredChannel.FetchOneById(IgnoredChannel, interaction.guildId);
const entity = await IgnoredChannel.FetchOneById(IgnoredChannel, interaction.channelId);
if (!entity) {
await interaction.reply('Unable to find channel.');
return;
}
await IgnoredChannel.Remove(IgnoredChannel, entity);
await IgnoredChannel.Remove(IgnoredChannel, entity!);
await interaction.reply('This channel will start being logged again.');
} else {
const entity = new IgnoredChannel(interaction.guildId);
const entity = new IgnoredChannel(interaction.channelId);
await entity.Save(IgnoredChannel, entity);

View file

@ -29,22 +29,17 @@ export default class Kick extends Command {
if (!interaction.guildId) return;
if (!interaction.guild) return;
const targetUser = interaction.options.get('target');
const targetUser = interaction.options.get('target', true);
const reasonInput = interaction.options.get('reason');
if (!targetUser || !targetUser.user || !targetUser.member) {
await interaction.reply("User not found.");
return;
}
const member = targetUser.member as GuildMember;
const reason = reasonInput && reasonInput.value ? reasonInput.value.toString() : "*none*";
const logEmbed = new EmbedBuilder()
.setColor(EmbedColours.Ok)
.setTitle("Member Kicked")
.setDescription(`<@${targetUser.user.id}> \`${targetUser.user.tag}\``)
.setThumbnail(targetUser.user.avatarURL())
.setDescription(`<@${targetUser.user!.id}> \`${targetUser.user!.tag}\``)
.setThumbnail(targetUser.user!.avatarURL())
.addFields([
{
name: "Moderator",
@ -62,19 +57,19 @@ export default class Kick extends Command {
}
await member.kick();
await interaction.reply(`\`${targetUser.user.tag}\` has been kicked.`);
await interaction.reply(`\`${targetUser.user!.tag}\` has been kicked.`);
const channelName = await SettingsHelper.GetSetting('channels.logs.mod', interaction.guildId);
if (!channelName) return;
if (channelName) {
const channel = interaction.guild.channels.cache.find(x => x.name == channelName) as TextChannel;
const channel = interaction.guild.channels.cache.find(x => x.name == channelName) as TextChannel;
if (channel) {
await channel.send({ embeds: [ logEmbed ]});
if (channel) {
await channel.send({ embeds: [ logEmbed ]});
}
}
const audit = new Audit(targetUser.user.id, AuditType.Kick, reason, interaction.user.id, interaction.guildId);
const audit = new Audit(targetUser.user!.id, AuditType.Kick, reason, interaction.user.id, interaction.guildId);
await audit.Save(Audit, audit);
}
}

View file

@ -40,15 +40,13 @@ export default class Poll extends Command {
}
public override async execute(interaction: CommandInteraction) {
const title = interaction.options.get('title');
const option1 = interaction.options.get('option1');
const option2 = interaction.options.get('option2');
const title = interaction.options.get('title', true);
const option1 = interaction.options.get('option1', true);
const option2 = interaction.options.get('option2', true);
const option3 = interaction.options.get('option3');
const option4 = interaction.options.get('option4');
const option5 = interaction.options.get('option5');
if (!title || !option1 || !option2) return;
const description = [
option1.value as string,
option2.value as string,
@ -58,15 +56,7 @@ export default class Poll extends Command {
]
.filter(x => x != null);
const arrayOfNumbers = [
':one:',
':two:',
':three:',
':four:',
':five:',
];
const reactionEmojis = ["1⃣", "2⃣", "3⃣", "4⃣", "5⃣", "6⃣", "7⃣", "8⃣", "9⃣"];
const reactionEmojis = ["1⃣", "2⃣", "3⃣", "4⃣", "5⃣"];
description.forEach((value, index) => {
description[index] = `${reactionEmojis[index]} ${description[index]}`;
@ -82,10 +72,11 @@ export default class Poll extends Command {
});
const message = await interaction.reply({ embeds: [ embed ]});
const messageResponse = await interaction.reply({ embeds: [ embed ]});
const message = await messageResponse.fetch();
description.forEach(async (value, index) => {
await (await message.fetch()).react(reactionEmojis[index]);
await message.react(reactionEmojis[index]);
});
}
}

View file

@ -27,21 +27,16 @@ export default class Warn extends Command {
public override async execute(interaction: CommandInteraction) {
if (!interaction.guild || !interaction.guildId) return;
const targetUser = interaction.options.get('target');
const targetUser = interaction.options.get('target', true).user!;
const reasonInput = interaction.options.get('reason');
if (!targetUser || !targetUser.user || !targetUser.member) {
await interaction.reply('Fields are required.');
return;
}
const reason = reasonInput && reasonInput.value ? reasonInput.value.toString() : "*none*";
const logEmbed = new EmbedBuilder()
.setColor(EmbedColours.Ok)
.setTitle("Member Warned")
.setDescription(`<@${targetUser.user.id}> \`${targetUser.user.tag}\``)
.setThumbnail(targetUser.user.avatarURL())
.setDescription(`<@${targetUser.id}> \`${targetUser.tag}\``)
.setThumbnail(targetUser.avatarURL())
.addFields([
{
name: "Moderator",
@ -55,17 +50,17 @@ export default class Warn extends Command {
const channelName = await SettingsHelper.GetSetting('channels.logs.mod', interaction.guildId);
if (!channelName) return;
if (channelName) {
const channel = interaction.guild.channels.cache.find(x => x.name == channelName) as TextChannel;
const channel = interaction.guild.channels.cache.find(x => x.name == channelName) as TextChannel;
if (channel) {
await channel.send({ embeds: [ logEmbed ]});
if (channel) {
await channel.send({ embeds: [ logEmbed ]});
}
}
const audit = new Audit(targetUser.user.id, AuditType.Warn, reason, interaction.user.id, interaction.guildId);
const audit = new Audit(targetUser.id, AuditType.Warn, reason, interaction.user.id, interaction.guildId);
await audit.Save(Audit, audit);
await interaction.reply('Successfully warned user.');
}
}
}

View file

@ -46,14 +46,4 @@ export default class SettingsHelper {
await server.Save(Server, server);
}
}
public static async GetServerPrefix(serverId: string): Promise<string> {
const setting = await this.GetSetting("bot.prefix", serverId);
if (!setting) {
return "v!";
}
return setting;
}
}

View file

@ -0,0 +1,155 @@
import { ButtonInteraction, GuildMember, Role } from "discord.js";
import Verify from "../../src/buttonEvents/verify";
import SettingsHelper from "../../src/helpers/SettingsHelper";
jest.mock("../../src/helpers/SettingsHelper");
describe("execute", () => {
let role: Role;
let member: GuildMember;
let interaction: ButtonInteraction;
let entity: Verify;
beforeEach(() => {
entity = new Verify();
role = {} as Role;
member = {
manageable: true,
roles: {
add: jest.fn(),
}
} as unknown as GuildMember;
interaction = {
reply: jest.fn(),
guildId: 'guildId',
guild: {
roles: {
cache: {
find: jest.fn().mockReturnValue(role),
}
},
members: {
cache: {
find: jest.fn().mockReturnValue(member),
}
}
}
} as unknown as ButtonInteraction;
SettingsHelper.GetSetting = jest.fn().mockResolvedValue("roleName");
});
test("EXPECT verification role to be given", async () => {
await entity.execute(interaction);
expect(interaction.reply).toHaveBeenCalledTimes(1);
expect(interaction.reply).toHaveBeenCalledWith({
content: "Given role",
ephemeral: true,
});
expect(SettingsHelper.GetSetting).toHaveBeenCalledWith("verification.role", "guildId");
expect(interaction.guild?.roles.cache.find).toHaveBeenCalledTimes(1);
expect(interaction.guild?.members.cache.find).toHaveBeenCalledTimes(1);
expect(member.roles.add).toHaveBeenCalledWith(role);
});
test("GIVEN interaction.guildId is null, EXPECT nothing to happen", async () => {
interaction.guildId = null;
await entity.execute(interaction);
expect(interaction.reply).not.toHaveBeenCalled();
expect(SettingsHelper.GetSetting).not.toHaveBeenCalled();
expect(interaction.guild?.roles.cache.find).not.toHaveBeenCalled();
expect(interaction.guild?.members.cache.find).not.toHaveBeenCalled();
expect(member.roles.add).not.toHaveBeenCalled();
});
test("GIVEN interaction.guild is null, EXPECT nothing to happen", async () => {
interaction = {
reply: jest.fn(),
guildId: 'guildId',
guild: null,
} as unknown as ButtonInteraction;
await entity.execute(interaction);
expect(interaction.reply).not.toHaveBeenCalled();
expect(SettingsHelper.GetSetting).not.toHaveBeenCalled();
expect(member.roles.add).not.toHaveBeenCalled();
});
test("GIVEN verification.role setting is not found, EXPECT nothing to happen", async () => {
SettingsHelper.GetSetting = jest.fn().mockResolvedValue(undefined);
await entity.execute(interaction);
expect(SettingsHelper.GetSetting).toHaveBeenCalledWith("verification.role", "guildId");
expect(interaction.reply).not.toHaveBeenCalled();
expect(interaction.guild?.roles.cache.find).not.toHaveBeenCalled();
expect(interaction.guild?.members.cache.find).not.toHaveBeenCalled();
expect(member.roles.add).not.toHaveBeenCalled();
});
test("GIVEN role can not be found, EXPECT error", async () => {
interaction.guild!.roles.cache.find = jest.fn().mockReturnValue(undefined);
await entity.execute(interaction);
expect(interaction.reply).toHaveBeenCalledTimes(1);
expect(interaction.reply).toHaveBeenCalledWith({
content: "Unable to find the role, roleName",
ephemeral: true,
});
expect(interaction.guild?.members.cache.find).not.toHaveBeenCalled();
expect(member.roles.add).not.toHaveBeenCalled();
});
test("GIVEN member can not be found, EXPECT error", async () => {
interaction.guild!.members.cache.find = jest.fn().mockReturnValue(undefined);
await entity.execute(interaction);
expect(interaction.reply).toHaveBeenCalledTimes(1);
expect(interaction.reply).toHaveBeenCalledWith({
content: "Unable to give role to user",
ephemeral: true,
});
expect(interaction.guild?.roles.cache.find).toHaveBeenCalledTimes(1);
expect(interaction.guild?.members.cache.find).toHaveBeenCalledTimes(1);
expect(member.roles.add).not.toHaveBeenCalled();
});
test("GIVEN member is not manageable, EXPECT error", async () => {
member = {
manageable: false,
roles: {
add: jest.fn(),
}
} as unknown as GuildMember;
interaction.guild!.members.cache.find = jest.fn().mockReturnValue(member);
await entity.execute(interaction);
expect(interaction.reply).toHaveBeenCalledTimes(1);
expect(interaction.reply).toHaveBeenCalledWith({
content: "Unable to give role to user",
ephemeral: true,
});
expect(interaction.guild?.roles.cache.find).toHaveBeenCalledTimes(1);
expect(interaction.guild?.members.cache.find).toHaveBeenCalledTimes(1);
expect(member.roles.add).not.toHaveBeenCalled();
});
});

View file

@ -1,153 +1,129 @@
import { mock } from "jest-mock-extended";
const connectionMock = mock<Connection>();
const qbuilderMock = mock<SelectQueryBuilder<any>>();
let repositoryMock = mock<Repository<any>>();
let settingMock = mock<Setting>();
jest.mock('typeorm', () => {
qbuilderMock.where.mockReturnThis();
qbuilderMock.select.mockReturnThis();
repositoryMock.createQueryBuilder.mockReturnValue(qbuilderMock);
repositoryMock.findOne.mockImplementation(async () => {
return settingMock;
});
connectionMock.getRepository.mockReturnValue(repositoryMock);
return {
getConnection: () => connectionMock,
createConnection: () => connectionMock,
BaseEntity: class Mock {},
ObjectType: () => {},
Entity: () => {},
InputType: () => {},
Index: () => {},
PrimaryColumn: () => {},
Column: () => {},
CreateDateColumn: () => {},
UpdateDateColumn: () => {},
OneToMany: () => {},
ManyToOne: () => {},
}
});
jest.mock("discord.js");
jest.mock("dotenv");
jest.mock("../../src/client/events");
jest.mock("../../src/client/util");
jest.mock("../../src/constants/DefaultValues");
import { Partials, Client } from "discord.js";
import { CoreClient } from "../../src/client/client";
import { Client } from "discord.js";
import * as dotenv from "dotenv";
import { Events } from "../../src/client/events";
import AppDataSource from "../../src/database/dataSources/appDataSource";
import { Util } from "../../src/client/util";
import { Command } from "../../src/type/command";
import { Event } from "../../src/type/event";
import DefaultValues from "../../src/constants/DefaultValues";
import { Connection, Repository, SelectQueryBuilder } from "typeorm";
import Setting from "../../src/entity/Setting";
import { EventType } from "../../src/constants/EventType";
import { ButtonEvent } from "../../src/type/buttonEvent";
jest.mock("dotenv");
beforeEach(() => {
jest.resetAllMocks();
jest.resetModules();
})
jest.clearAllMocks();
describe('Constructor', () => {
test('Expect Successful Initialisation', () => {
const coreClient = new CoreClient();
expect(coreClient).toBeInstanceOf(Client);
expect(dotenv.config).toBeCalledTimes(1);
expect(Events).toBeCalledTimes(1);
expect(Util).toBeCalledTimes(1);
expect(DefaultValues.useDevPrefix).toBe(false);
});
process.env = {};
});
test('Given devmode parameter is true, Expect devmode prefix to be true', () => {
const coreClient = new CoreClient(true);
expect(coreClient).toBeInstanceOf(Client);
expect(dotenv.config).toBeCalledTimes(1);
expect(Events).toBeCalledTimes(1);
expect(Util).toBeCalledTimes(1);
expect(DefaultValues.useDevPrefix).toBe(true);
describe('constructor', () => {
test("EXPECT properties to be set", () => {
const client = new CoreClient([1, 2, 3], [Partials.User]);
expect(dotenv.config).toHaveBeenCalledTimes(1);
expect(client).toBeDefined();
expect(CoreClient.commandItems).toEqual([]);
expect(CoreClient.eventItems).toEqual([]);
expect(CoreClient.buttonEvents).toEqual([]);
expect(client).toHaveProperty("_events");
expect(client).toHaveProperty("_util");
});
});
describe('Start', () => {
test('Given Env Is Valid, Expect Successful Start', async () => {
process.env = {
BOT_TOKEN: "TOKEN",
};
const coreClient = new CoreClient();
describe('start', () => {
test("EXPECT bot to start", async () => {
process.env.BOT_TOKEN = "token";
console.error = jest.fn();
AppDataSource.initialize = jest.fn().mockResolvedValue(undefined);
await coreClient.start();
expect(coreClient.on).toBeCalledWith("message", expect.any(Function));
expect(coreClient.on).toBeCalledWith("ready", expect.any(Function));
const client = new CoreClient([1, 2, 3], [Partials.User]);
const loadSlashCommands = jest.spyOn(Util.prototype, "loadSlashCommands").mockImplementation();
const loadEvents = jest.spyOn(Util.prototype, "loadEvents").mockImplementation();
Client.prototype.login = jest.fn();
Client.prototype.on = jest.fn();
await client.start();
expect(AppDataSource.initialize).toHaveBeenCalledTimes(1);
expect(console.error).not.toHaveBeenCalled();
expect(client.login).toHaveBeenCalledTimes(1);
expect(client.login).toHaveBeenCalledWith("token");
expect(client.on).toHaveBeenCalledTimes(2);
expect(client.on).toHaveBeenCalledWith("interactionCreate", expect.any(Function));
expect(client.on).toHaveBeenCalledWith("ready", expect.any(Function))
expect(loadSlashCommands).toHaveBeenCalledTimes(1);
expect(loadSlashCommands).toHaveBeenCalledWith(client);
expect(loadEvents).toHaveBeenCalledTimes(1);
expect(loadEvents).toHaveBeenCalledWith(client, []);
});
test('Given BOT_TOKEN Is Null, Expect Failure', async () => {
process.env = {};
const consoleError = jest.fn();
test("GIVEN BOT_TOKEN is not in env var, EXPECT error", async () => {
console.error = jest.fn();
console.error = consoleError;
const coreClient = new CoreClient();
const client = new CoreClient([1, 2, 3], [ Partials.User ]);
await coreClient.start();
await client.start();
expect(consoleError).toBeCalledWith("BOT_TOKEN is not defined in .env");
expect(coreClient.on).not.toBeCalled();
expect(coreClient.login).not.toBeCalled();
expect(console.error).toHaveBeenCalledWith("BOT_TOKEN is not defined in .env");
});
test('Given BOT_TOKEN Is Empty, Expect Failure', async () => {
process.env = {
BOT_TOKEN: '',
}
const consoleError = jest.fn();
console.error = consoleError;
const coreClient = new CoreClient();
test("GIVEN database connection can not be initialised, EXPECT error", async () => {
process.env.BOT_TOKEN = "token";
console.log = jest.fn();
console.error = jest.fn();
AppDataSource.initialize = jest.fn().mockRejectedValue("error");
await coreClient.start();
const client = new CoreClient([1, 2, 3], [Partials.User]);
expect(consoleError).toBeCalledWith("BOT_TOKEN is not defined in .env");
expect(coreClient.on).not.toBeCalled();
expect(coreClient.login).not.toBeCalled();
await client.start();
expect(console.log).not.toHaveBeenCalled();
expect(console.error).toHaveBeenCalledWith("Error Initialising Data Source", "error");
});
});
describe('RegisterCommand', () => {
test('Expect command added to register', () => {
const cmd = mock<Command>();
const client = new CoreClient();
client.RegisterCommand("test", cmd);
test("EXPECT command to be added to list", () => {
CoreClient.RegisterCommand("name", {} as Command, "serverId");
expect(client.commandItems.length).toBe(1);
expect(client.commandItems[0].Name).toBe("test");
expect(client.commandItems[0].Command).toBe(cmd);
expect(CoreClient.commandItems).toStrictEqual([
{
Name: "name",
Command: {},
ServerId: "serverId",
}
])
});
});
describe('RegisterEvent', () => {
test('Expect event added to register', () => {
const evt = mock<Event>();
const client = new CoreClient();
client.RegisterEvent(evt);
describe("RegisterEvent", () => {
test("EXPECT event to be added to list", () => {
CoreClient.RegisterEvent(EventType.ChannelCreate, () => {});
expect(client.eventItems.length).toBe(1);
expect(client.eventItems[0].Event).toBe(evt);
expect(CoreClient.eventItems).toStrictEqual([
{
EventType: EventType.ChannelCreate,
ExecutionFunction: expect.any(Function),
}
]);
});
});
describe("RegisterButtonEvent", () => {
test("EXPECT button event to be added to list", () => {
CoreClient.RegisterButtonEvent("buttonId", {} as ButtonEvent);
expect(CoreClient.buttonEvents).toStrictEqual([
{
ButtonId: "buttonId",
Event: {},
}
])
});
});

View file

@ -1,241 +1,89 @@
import { Interaction } from "discord.js";
import { Events } from "../../src/client/events";
import { Message } from "discord.js";
import { Util } from "../../src/client/util";
import ICommandItem from "../../src/contracts/ICommandItem";
import { Command } from "../../src/type/command";
import { mock } from "jest-mock-extended";
import ChatInputCommand from "../../src/client/interactionCreate/chatInputCommand";
import Button from "../../src/client/interactionCreate/button";
jest.mock("../../src/client/util");
describe('onInteractionCreate', () => {
test("GIVEN the interaction is a chat input command, EXPECT chat input command handler to be executed", async () => {
const interaction = {
isChatInputCommand: jest.fn().mockReturnValue(true),
guildId: "123",
} as unknown as Interaction;
beforeEach(() => {
Util.prototype.loadCommand = jest.fn();
});
ChatInputCommand.onChatInput = jest.fn();
describe('OnMessage', () => {
test('Given Message Is Valid Expect Message Sent', async () => {
process.env = {
BOT_TOKEN: 'TOKEN',
BOT_PREFIX: '!',
FOLDERS_COMMANDS: 'commands',
FOLDERS_EVENTS: 'events',
};
Util.prototype.loadCommand = jest.fn().mockReturnValue({ valid: true });
const message = {
guild: {},
author: {
bot: false,
},
content: "!test first",
} as unknown as Message;
const cmd = mock<Command>();
const commandItem: ICommandItem = {
Name: "test",
Command: cmd
};
const commands: ICommandItem[] = [ commandItem ];
const events = new Events();
const result = await events.onMessage(message, commands);
expect(result.valid).toBeTruthy();
expect(result.context?.prefix).toBe('!');
expect(result.context?.name).toBe('test');
expect(result.context?.args.length).toBe(1);
expect(result.context?.args[0]).toBe('first');
expect(result.context?.message).toBe(message);
await events.onInteractionCreate(interaction);
expect(interaction.isChatInputCommand).toHaveBeenCalledTimes(1);
expect(ChatInputCommand.onChatInput).toHaveBeenCalledTimes(1);
expect(ChatInputCommand.onChatInput).toHaveBeenCalledWith(interaction);
});
test('Given Guild Is Null, Expect Failed Result', async () => {
process.env = {
BOT_TOKEN: 'TOKEN',
BOT_PREFIX: '!',
FOLDERS_COMMANDS: 'commands',
FOLDERS_EVENTS: 'events',
}
Util.prototype.loadCommand = jest.fn().mockReturnValue({ valid: true });
const message = {
guild: null,
author: {
bot: false,
},
content: "!test first",
} as unknown as Message;
const cmd = mock<Command>();
test("GIVEN the interaction is a button, EXPECT button handler to be executed", async () => {
const interaction = {
isChatInputCommand: jest.fn().mockReturnValue(false),
isButton: jest.fn().mockReturnValue(true),
guildId: "123",
} as unknown as Interaction;
const commandItem: ICommandItem = {
Name: "test",
Command: cmd
};
Button.onButtonClicked = jest.fn();
const commands: ICommandItem[] = [ commandItem ];
const events = new Events();
const result = await events.onMessage(message, commands);
expect(result.valid).toBeFalsy();
expect(result.message).toBe("Message was not sent in a guild, ignoring.");
await events.onInteractionCreate(interaction);
expect(interaction.isButton).toHaveBeenCalledTimes(1);
expect(Button.onButtonClicked).toHaveBeenCalledTimes(1);
expect(Button.onButtonClicked).toHaveBeenCalledWith(interaction);
});
test('Given Author Is A Bot, Expect Failed Result', async () => {
process.env = {
BOT_TOKEN: 'TOKEN',
BOT_PREFIX: '!',
FOLDERS_COMMANDS: 'commands',
FOLDERS_EVENTS: 'events',
}
Util.prototype.loadCommand = jest.fn().mockReturnValue({ valid: true });
const message = {
guild: {},
author: {
bot: true,
},
content: "!test first",
} as unknown as Message;
const cmd = mock<Command>();
test("GIVEN the interaction is not a chat input command or button, EXPECT error", async () => {
const interaction = {
isChatInputCommand: jest.fn().mockReturnValue(false),
isButton: jest.fn().mockReturnValue(false),
guildId: "123",
} as unknown as Interaction;
const commandItem: ICommandItem = {
Name: "test",
Command: cmd
};
console.error = jest.fn();
const commands: ICommandItem[] = [ commandItem ];
const events = new Events();
const result = await events.onMessage(message, commands);
expect(result.valid).toBeFalsy();
expect(result.message).toBe("Message was sent by a bot, ignoring.");
await events.onInteractionCreate(interaction);
expect(interaction.isChatInputCommand).toHaveBeenCalledTimes(1);
expect(interaction.isButton).toHaveBeenCalledTimes(1);
expect(console.error).toHaveBeenCalledWith("Received interaction unable to interact with, ignoring");
});
test('Given Message Content Was Not A Command, Expect Failed Result', async () => {
process.env = {
BOT_TOKEN: 'TOKEN',
BOT_PREFIX: '!',
FOLDERS_COMMANDS: 'commands',
FOLDERS_EVENTS: 'events',
}
Util.prototype.loadCommand = jest.fn().mockReturnValue({ valid: true });
const message = {
guild: {},
author: {
bot: false,
},
content: "This is a standard message",
} as unknown as Message;
const cmd = mock<Command>();
test("GIVEN interaction.guildId is null, EXPECT nothing to happen", async () => {
const interaction = {
isChatInputCommand: jest.fn(),
isButton: jest.fn(),
guildId: null,
} as unknown as Interaction;
const commandItem: ICommandItem = {
Name: "test",
Command: cmd
};
ChatInputCommand.onChatInput = jest.fn();
Button.onButtonClicked = jest.fn();
const commands: ICommandItem[] = [ commandItem ];
const events = new Events();
const result = await events.onMessage(message, commands);
expect(result.valid).toBeFalsy();
expect(result.message).toBe("Message was not a command, ignoring.");
});
test('Given Message Had No Command Name, Expect Failed Result', async () => {
process.env = {
BOT_TOKEN: 'TOKEN',
BOT_PREFIX: '!',
FOLDERS_COMMANDS: 'commands',
FOLDERS_EVENTS: 'events',
}
Util.prototype.loadCommand = jest.fn().mockReturnValue({ valid: true });
const message = {
guild: {},
author: {
bot: false,
},
content: "!",
} as unknown as Message;
await events.onInteractionCreate(interaction);
const cmd = mock<Command>();
const commandItem: ICommandItem = {
Name: "test",
Command: cmd
};
const commands: ICommandItem[] = [ commandItem ];
const events = new Events();
const result = await events.onMessage(message, commands);
expect(result.valid).toBeFalsy();
expect(result.message).toBe("Command name was not found");
});
test('Given Command Failed To Execute, Expect Failed Result', async () => {
process.env = {
BOT_TOKEN: 'TOKEN',
BOT_PREFIX: '!',
FOLDERS_COMMANDS: 'commands',
FOLDERS_EVENTS: 'events',
}
Util.prototype.loadCommand = jest.fn().mockReturnValue({ valid: false, message: "Command failed" });
const message = {
guild: {},
author: {
bot: false,
},
content: "!test first",
} as unknown as Message;
const cmd = mock<Command>();
const commandItem: ICommandItem = {
Name: "test",
Command: cmd
};
const commands: ICommandItem[] = [ commandItem ];
const events = new Events();
const result = await events.onMessage(message, commands);
expect(result.valid).toBeFalsy();
expect(result.message).toBe("Command failed");
expect(interaction.isChatInputCommand).not.toHaveBeenCalled();
expect(interaction.isButton).not.toHaveBeenCalled();
expect(ChatInputCommand.onChatInput).not.toHaveBeenCalled();
expect(Button.onButtonClicked).not.toHaveBeenCalled();
});
});
describe('OnReady', () => {
test('Expect Console Log', () => {
describe("onReady", () => {
test("EXPECT console to be logged", () => {
console.log = jest.fn();
const events = new Events();
events.onReady();
expect(console.log).toBeCalledWith("Ready");
expect(console.log).toHaveBeenCalledWith("Ready");
});
});
});

View file

@ -0,0 +1,70 @@
import Button from "../../../src/client/interactionCreate/button";
import { CoreClient } from "../../../src/client/client";
import ButtonEventItem from "../../../src/contracts/ButtonEventItem";
import { ButtonInteraction } from "discord.js";
import { ButtonEvent } from "../../../src/type/buttonEvent";
describe('onButtonClicked', () => {
let item: ButtonEventItem;
let interaction: ButtonInteraction;
beforeEach(() => {
item = {
ButtonId: "buttonId",
Event: {
execute: jest.fn(),
} as unknown as ButtonEvent,
} as unknown as ButtonEventItem;
interaction = {
reply: jest.fn(),
isButton: true,
customId: "buttonId test",
} as unknown as ButtonInteraction;
CoreClient.buttonEvents = [ item ];
});
test("EXPECT button event to be executed", async () => {
await Button.onButtonClicked(interaction);
expect(item.Event.execute).toHaveBeenCalledTimes(1);
expect(item.Event.execute).toHaveBeenCalledWith(interaction);
expect(interaction.reply).not.toHaveBeenCalled();
});
test("GIVEN interaction is not a button, EXEPCT nothing to happen", async () => {
interaction = {
reply: jest.fn(),
isButton: false,
} as unknown as ButtonInteraction;
await Button.onButtonClicked(interaction);
expect(item.Event.execute).not.toHaveBeenCalled();
expect(interaction.reply).not.toHaveBeenCalled();
});
test("GIVEN no button event is registered, EXPECT error", async () => {
CoreClient.buttonEvents = [];
await Button.onButtonClicked(interaction);
expect(interaction.reply).toHaveBeenCalledTimes(1);
expect(interaction.reply).toHaveBeenCalledWith("Event not found.");
expect(item.Event.execute).not.toHaveBeenCalled();
});
test("GIVEN button event is not registered, EXPECT error", async () => {
interaction.customId = "anotherButtonId test";
await Button.onButtonClicked(interaction);
expect(interaction.reply).toHaveBeenCalledTimes(1);
expect(interaction.reply).toHaveBeenCalledWith("Event not found.");
expect(item.Event.execute).not.toHaveBeenCalled();
});
});

View file

@ -0,0 +1,168 @@
import { ChatInputCommandInteraction } from 'discord.js';
import ChatInputComamnd from '../../../src/client/interactionCreate/chatInputCommand';
import { CoreClient } from '../../../src/client/client';
import ICommandItem from '../../../src/contracts/ICommandItem';
beforeEach(() => {
CoreClient.commandItems = [];
});
describe('onChatInput', () => {
test("GIVEN command is registered globally AND command is found, execute the global command", async () => {
// Arrange
const interaction = {
reply: jest.fn(),
isChatInputCommand: jest.fn().mockReturnValue(true),
commandName: "test",
} as unknown as ChatInputCommandInteraction;
const testCommand: ICommandItem = {
Name: "test",
Command: {
CommandBuilder: jest.fn(),
execute: jest.fn(),
},
};
const testServerCommand: ICommandItem = {
Name: "test",
Command: {
CommandBuilder: jest.fn(),
execute: jest.fn(),
},
ServerId: "123",
};
CoreClient.commandItems = [ testCommand, testServerCommand ];
// Act
await ChatInputComamnd.onChatInput(interaction);
// Assert
expect(interaction.isChatInputCommand).toHaveBeenCalledTimes(1);
expect(interaction.reply).not.toHaveBeenCalled();
expect(testCommand.Command.execute).toHaveBeenCalledTimes(1);
expect(testCommand.Command.execute).toHaveBeenCalledWith(interaction);
expect(testServerCommand.Command.execute).not.toHaveBeenCalled();
});
test("GIVEN command is registered to a single server, execute the server command", async () => {
// Arrange
const interaction = {
reply: jest.fn(),
isChatInputCommand: jest.fn().mockReturnValue(true),
commandName: "test",
guildId: "123",
} as unknown as ChatInputCommandInteraction;
const testCommand: ICommandItem = {
Name: "test",
Command: {
CommandBuilder: jest.fn(),
execute: jest.fn(),
},
};
const testServerCommand: ICommandItem = {
Name: "test",
Command: {
CommandBuilder: jest.fn(),
execute: jest.fn(),
},
ServerId: "123",
};
CoreClient.commandItems = [ testCommand, testServerCommand ];
// Act
await ChatInputComamnd.onChatInput(interaction);
// Assert
expect(interaction.isChatInputCommand).toHaveBeenCalledTimes(1);
expect(interaction.reply).not.toHaveBeenCalled();
expect(testServerCommand.Command.execute).toHaveBeenCalledTimes(1);
expect(testServerCommand.Command.execute).toHaveBeenCalledWith(interaction);
expect(testCommand.Command.execute).not.toHaveBeenCalled();
});
test("GIVEN interaction is not a chat input command, EXPECT nothing to happen", async () => {
// Arrange
const interaction = {
reply: jest.fn(),
isChatInputCommand: jest.fn().mockReturnValue(false),
commandName: "test",
guildId: "123",
} as unknown as ChatInputCommandInteraction;
const testCommand: ICommandItem = {
Name: "test",
Command: {
CommandBuilder: jest.fn(),
execute: jest.fn(),
},
};
const testServerCommand: ICommandItem = {
Name: "test",
Command: {
CommandBuilder: jest.fn(),
execute: jest.fn(),
},
ServerId: "123",
};
CoreClient.commandItems = [ testCommand, testServerCommand ];
// Act
await ChatInputComamnd.onChatInput(interaction);
// Assert
expect(interaction.isChatInputCommand).toHaveBeenCalledTimes(1);
expect(interaction.reply).not.toHaveBeenCalled();
expect(testServerCommand.Command.execute).not.toHaveBeenCalled();
expect(testCommand.Command.execute).not.toHaveBeenCalled();
});
test("GIVEN command is registered globally AND command is not found, EXPECT error", async () => {
// Arrange
const interaction = {
reply: jest.fn(),
isChatInputCommand: jest.fn().mockReturnValue(true),
commandName: "other",
guildId: "123",
} as unknown as ChatInputCommandInteraction;
const testCommand: ICommandItem = {
Name: "test",
Command: {
CommandBuilder: jest.fn(),
execute: jest.fn(),
},
};
const testServerCommand: ICommandItem = {
Name: "test",
Command: {
CommandBuilder: jest.fn(),
execute: jest.fn(),
},
ServerId: "123",
};
CoreClient.commandItems = [ testCommand, testServerCommand ];
// Act
await ChatInputComamnd.onChatInput(interaction);
// Assert
expect(interaction.isChatInputCommand).toHaveBeenCalledTimes(1);
expect(interaction.reply).toHaveBeenCalledTimes(1);
expect(interaction.reply).toHaveBeenCalledWith("Command not found.");
expect(testServerCommand.Command.execute).not.toHaveBeenCalled();
expect(testCommand.Command.execute).not.toHaveBeenCalled();
});
});

View file

@ -1,370 +1,333 @@
import discordjs, { Client, REST, Routes, SlashCommandBuilder } from "discord.js";
import { Util } from "../../src/client/util";
import { Client, Message } from "discord.js";
import fs from "fs";
import { mock } from "jest-mock-extended";
import { CoreClient } from "../../src/client/client";
import { Command } from "../../src/type/command";
import ICommandItem from "../../src/contracts/ICommandItem";
import IEventItem from "../../src/contracts/IEventItem";
import { Event } from "../../src/type/event";
jest.mock("fs");
import { EventType } from "../../src/constants/EventType";
beforeEach(() => {
fs.existsSync = jest.fn();
jest.resetAllMocks();
jest.resetModules();
process.env = {};
});
describe('LoadCommand', () => {
test('Given Successful Exection, Expect Successful Result', async () => {
process.env = {
BOT_TOKEN: 'TOKEN',
BOT_PREFIX: '!',
FOLDERS_COMMANDS: 'commands',
FOLDERS_EVENTS: 'events',
}
const message = {
member: {
roles: {
cache: {
find: jest.fn().mockReturnValue(true),
}
},
},
reply: jest.fn(),
} as unknown as Message;
describe("loadSlashCommands", () => {
test("EXPECT slash commands to be loaded to the discord API", () => {
process.env.BOT_TOKEN = "token";
process.env.BOT_CLIENTID = "clientid";
const cmd = mock<Command>();
const client = {
guilds: {
cache: {
has: jest.fn().mockReturnValue(true),
}
}
} as unknown as Client;
const commandItem: ICommandItem = {
Name: "test",
Command: cmd
};
const setTokenPutMock = jest.fn();
const setTokenMock = jest.fn().mockReturnValue({
put: setTokenPutMock,
});
const commands: ICommandItem[] = [ commandItem ];
const util = new Util();
const result = await util.loadCommand("test", [ "first" ], message, commands);
expect(result.valid).toBeTruthy();
expect(cmd.execute).toBeCalled();
});
test('Given Member Is Null, Expect Failed Result', async () => {
process.env = {
BOT_TOKEN: 'TOKEN',
BOT_PREFIX: '!',
FOLDERS_COMMANDS: 'commands',
FOLDERS_EVENTS: 'events',
}
const message = {
member: null
} as unknown as Message;
REST.prototype.setToken = setTokenMock;
Routes.applicationCommands = jest.fn().mockReturnValue("command url");
Routes.applicationGuildCommands = jest.fn().mockReturnValue("guild command url");
const cmd = mock<Command>();
CoreClient.commandItems = [
{
Name: "global",
Command: {
CommandBuilder: {
name: "global"
} as SlashCommandBuilder,
} as Command,
},
{
Name: "server-specific",
Command: {
CommandBuilder: {
name: "server",
} as SlashCommandBuilder,
} as Command,
ServerId: "123",
}
];
const commandItem: ICommandItem = {
Name: "test",
Command: cmd
};
const util = new Util();
util.loadSlashCommands(client);
const commands: ICommandItem[] = [ commandItem ];
const util = new Util();
const result = await util.loadCommand("test", [ "first" ], message, commands);
expect(result.valid).toBeFalsy();
expect(result.message).toBe("Member is not part of message");
expect(cmd.execute).not.toBeCalled();
});
test('Given User Does Have Role, Expect Successful Result', async () => {
process.env = {
BOT_TOKEN: 'TOKEN',
BOT_PREFIX: '!',
FOLDERS_COMMANDS: 'commands',
FOLDERS_EVENTS: 'events',
}
const message = {
member: {
roles: {
cache: {
find: jest.fn().mockReturnValue(true),
}
},
},
reply: jest.fn(),
} as unknown as Message;
expect(setTokenMock).toHaveBeenCalledWith("token");
const cmd = mock<Command>();
expect(setTokenPutMock).toHaveBeenCalledTimes(2);
expect(setTokenPutMock).toHaveBeenCalledWith("command url", { body: [ CoreClient.commandItems[0].Command.CommandBuilder ] });
expect(setTokenPutMock).toHaveBeenCalledWith("guild command url", { body: [ CoreClient.commandItems[1].Command.CommandBuilder ]});
const commandItem: ICommandItem = {
Name: "test",
Command: cmd
};
expect(client.guilds.cache.has).toHaveBeenCalledTimes(1);
expect(client.guilds.cache.has).toHaveBeenCalledWith("123");
const commands: ICommandItem[] = [ commandItem ];
const util = new Util();
const result = await util.loadCommand("test", [ "first" ], message, commands);
expect(result.valid).toBeTruthy();
expect(cmd.execute).toBeCalled();
});
test('Given User Does Not Have Role, Expect Failed Result', async () => {
process.env = {
BOT_TOKEN: 'TOKEN',
BOT_PREFIX: '!',
FOLDERS_COMMANDS: 'commands',
FOLDERS_EVENTS: 'events',
}
const message = {
member: {
roles: {
cache: {
find: jest.fn().mockReturnValue(false),
}
},
},
reply: jest.fn(),
} as unknown as Message;
expect(Routes.applicationCommands).toHaveBeenCalledTimes(1);
expect(Routes.applicationCommands).toHaveBeenCalledWith("clientid");
const cmd = mock<Command>();
cmd._roles = [ "Moderator" ];
expect(Routes.applicationGuildCommands).toHaveBeenCalledTimes(1);
expect(Routes.applicationGuildCommands).toHaveBeenCalledWith("clientid", "123");
});
const commandItem: ICommandItem = {
Name: "test",
Command: cmd
};
test("GIVEN bot is not in a guild for a server command, EXPECT this to be ignored", () => {
process.env.BOT_TOKEN = "token";
process.env.BOT_CLIENTID = "clientid";
const commands: ICommandItem[] = [ commandItem ];
const util = new Util();
const result = await util.loadCommand("test", [ "first" ], message, commands);
expect(result.valid).toBeFalsy();
expect(result.message).toBe("You require the `Moderator` role to run this command");
expect(cmd.execute).not.toBeCalled();
});
const client = {
guilds: {
cache: {
has: jest.fn().mockReturnValue(false),
}
}
} as unknown as Client;
test('Given command is set to disabled, Expect command to not fire', async () => {
process.env = {
BOT_TOKEN: 'TOKEN',
BOT_PREFIX: '!',
FOLDERS_COMMANDS: 'commands',
FOLDERS_EVENTS: 'events',
COMMANDS_DISABLED: 'test',
COMMANDS_DISABLED_MESSAGE: 'disabled',
}
const message = {
member: {
roles: {
cache: {
find: jest.fn().mockReturnValue(true),
}
},
},
reply: jest.fn(),
} as unknown as Message;
const setTokenPutMock = jest.fn();
const setTokenMock = jest.fn().mockReturnValue({
put: setTokenPutMock,
});
const messageReply = jest.spyOn(message, 'reply');
REST.prototype.setToken = setTokenMock;
Routes.applicationCommands = jest.fn().mockReturnValue("command url");
Routes.applicationGuildCommands = jest.fn().mockReturnValue("guild command url");
const cmd = mock<Command>();
CoreClient.commandItems = [
{
Name: "server-specific",
Command: {
CommandBuilder: {
name: "server",
} as SlashCommandBuilder,
} as Command,
ServerId: "123",
}
];
const commandItem: ICommandItem = {
Name: "test",
Command: cmd
};
const util = new Util();
util.loadSlashCommands(client);
const commands: ICommandItem[] = [ commandItem ];
const util = new Util();
const result = await util.loadCommand("test", [ "first" ], message, commands);
expect(result.valid).toBeFalsy();
expect(result.message).toBe("Command is disabled");
expect(messageReply).toBeCalledWith("disabled");
expect(cmd.execute).not.toBeCalled();
});
test('Given command COMMANDS_DISABLED_MESSAGE is empty, Expect default message sent', async () => {
process.env = {
BOT_TOKEN: 'TOKEN',
BOT_PREFIX: '!',
FOLDERS_COMMANDS: 'commands',
FOLDERS_EVENTS: 'events',
COMMANDS_DISABLED: 'test',
}
const message = {
member: {
roles: {
cache: {
find: jest.fn().mockReturnValue(true),
}
},
},
reply: jest.fn(),
} as unknown as Message;
const messageReply = jest.spyOn(message, 'reply');
const cmd = mock<Command>();
const commandItem: ICommandItem = {
Name: "test",
Command: cmd
};
const commands: ICommandItem[] = [ commandItem ];
const util = new Util();
const result = await util.loadCommand("test", [ "first" ], message, commands);
expect(result.valid).toBeFalsy();
expect(result.message).toBe("Command is disabled");
expect(messageReply).toBeCalledWith("This command is disabled.");
expect(cmd.execute).not.toBeCalled();
});
test('Given a different command is disabled, Expect command to still fire', async () => {
process.env = {
BOT_TOKEN: 'TOKEN',
BOT_PREFIX: '!',
FOLDERS_COMMANDS: 'commands',
FOLDERS_EVENTS: 'events',
COMMANDS_DISABLED: 'other',
}
const message = {
member: {
roles: {
cache: {
find: jest.fn().mockReturnValue(true),
}
},
},
reply: jest.fn(),
} as unknown as Message;
const cmd = mock<Command>();
const otherCmd = mock<Command>();
const commandItem: ICommandItem = {
Name: "test",
Command: cmd
};
const otherCommandItem: ICommandItem = {
Name: "other",
Command: otherCmd,
}
const commands: ICommandItem[] = [ commandItem, otherCommandItem ];
const util = new Util();
const result = await util.loadCommand("test", [ "first" ], message, commands);
expect(result.valid).toBeTruthy();
expect(cmd.execute).toBeCalled();
expect(otherCmd.execute).not.toBeCalled();
});
test('Given command is not found in register, expect command not found error', async () => {
process.env = {
BOT_TOKEN: 'TOKEN',
BOT_PREFIX: '!',
FOLDERS_COMMANDS: 'commands',
FOLDERS_EVENTS: 'events',
}
const message = {
member: {
roles: {
cache: {
find: jest.fn().mockReturnValue(true),
}
},
},
reply: jest.fn(),
} as unknown as Message;
const commands: ICommandItem[] = [];
const util = new Util();
const result = await util.loadCommand("test", [ "first" ], message, commands);
expect(result.valid).toBeFalsy();
expect(result.message).toBe('Command not found');
expect(message.reply).toBeCalledWith('Command not found');
});
expect(Routes.applicationGuildCommands).not.toHaveBeenCalled();
});
});
describe('LoadEvents', () => {
test('Given Events Are Loaded, Expect Successful Result', () => {
process.env = {
BOT_TOKEN: 'TOKEN',
BOT_PREFIX: '!',
FOLDERS_COMMANDS: 'commands',
FOLDERS_EVENTS: 'events',
}
const client = {
on: jest.fn(),
} as unknown as Client;
describe("loadEvents", () => {
test("GIVEN event type is channelCreate, EXPECT event function to be executed", () => {
const client = {
on: jest.fn(),
} as unknown as Client;
const events: IEventItem[] = [
{
EventType: EventType.ChannelCreate,
ExecutionFunction: jest.fn(),
}
];
const evt = mock<Event>();
const util = new Util();
util.loadEvents(client, events);
const eventItem: IEventItem = {
Event: evt
};
expect(client.on).toHaveBeenCalledTimes(1);
expect(client.on).toHaveBeenCalledWith("channelCreate", expect.any(Function));
});
const eventItems: IEventItem[] = [ eventItem ];
const util = new Util();
const result = util.loadEvents(client, eventItems);
const clientOn = jest.spyOn(client, 'on');
expect(result.valid).toBeTruthy();
expect(clientOn).toBeCalledTimes(13);
});
test('Given No Events Found, Expect Successful Result', () => {
process.env = {
BOT_TOKEN: 'TOKEN',
BOT_PREFIX: '!',
FOLDERS_COMMANDS: 'commands',
FOLDERS_EVENTS: 'events',
}
const client = {
on: jest.fn(),
} as unknown as Client;
test("GIVEN event type is channelDelete, EXPECT event function to be executed", () => {
const client = {
on: jest.fn(),
} as unknown as Client;
const events: IEventItem[] = [
{
EventType: EventType.ChannelDelete,
ExecutionFunction: jest.fn(),
}
];
const eventItems: IEventItem[] = [];
const util = new Util();
const result = util.loadEvents(client, eventItems);
const clientOn = jest.spyOn(client, 'on');
expect(result.valid).toBeTruthy();
expect(clientOn).toBeCalledTimes(0);
});
});
const util = new Util();
util.loadEvents(client, events);
expect(client.on).toHaveBeenCalledTimes(1);
expect(client.on).toHaveBeenCalledWith("channelDelete", expect.any(Function));
});
test("GIVEN event type is channelUpdate, EXPECT event function to be executed", () => {
const client = {
on: jest.fn(),
} as unknown as Client;
const events: IEventItem[] = [
{
EventType: EventType.ChannelUpdate,
ExecutionFunction: jest.fn(),
}
];
const util = new Util();
util.loadEvents(client, events);
expect(client.on).toHaveBeenCalledTimes(1);
expect(client.on).toHaveBeenCalledWith("channelUpdate", expect.any(Function));
});
test("GIVEN event type is guildBanAdd, EXPECT event function to be executed", () => {
const client = {
on: jest.fn(),
} as unknown as Client;
const events: IEventItem[] = [
{
EventType: EventType.GuildBanAdd,
ExecutionFunction: jest.fn(),
}
];
const util = new Util();
util.loadEvents(client, events);
expect(client.on).toHaveBeenCalledTimes(1);
expect(client.on).toHaveBeenCalledWith("guildBanAdd", expect.any(Function));
});
test("GIVEN event type is guildBanRemove, EXPECT event function to be executed", () => {
const client = {
on: jest.fn(),
} as unknown as Client;
const events: IEventItem[] = [
{
EventType: EventType.GuildBanRemove,
ExecutionFunction: jest.fn(),
}
];
const util = new Util();
util.loadEvents(client, events);
expect(client.on).toHaveBeenCalledTimes(1);
expect(client.on).toHaveBeenCalledWith("guildBanRemove", expect.any(Function));
});
test("GIVEN event type is guildCreate, EXPECT event function to be executed", () => {
const client = {
on: jest.fn(),
} as unknown as Client;
const events: IEventItem[] = [
{
EventType: EventType.GuildCreate,
ExecutionFunction: jest.fn(),
}
];
const util = new Util();
util.loadEvents(client, events);
expect(client.on).toHaveBeenCalledTimes(1);
expect(client.on).toHaveBeenCalledWith("guildCreate", expect.any(Function));
});
test("GIVEN event type is guildMemberAdd, EXPECT event function to be executed", () => {
const client = {
on: jest.fn(),
} as unknown as Client;
const events: IEventItem[] = [
{
EventType: EventType.GuildMemberAdd,
ExecutionFunction: jest.fn(),
}
];
const util = new Util();
util.loadEvents(client, events);
expect(client.on).toHaveBeenCalledTimes(1);
expect(client.on).toHaveBeenCalledWith("guildMemberAdd", expect.any(Function));
});
test("GIVEN event type is guildMemberRemove, EXPECT event function to be executed", () => {
const client = {
on: jest.fn(),
} as unknown as Client;
const events: IEventItem[] = [
{
EventType: EventType.GuildMemberRemove,
ExecutionFunction: jest.fn(),
}
];
const util = new Util();
util.loadEvents(client, events);
expect(client.on).toHaveBeenCalledTimes(1);
expect(client.on).toHaveBeenCalledWith("guildMemberRemove", expect.any(Function));
});
test("GIVEN event type is messageCreate, EXPECT event function to be executed", () => {
const client = {
on: jest.fn(),
} as unknown as Client;
const events: IEventItem[] = [
{
EventType: EventType.MessageCreate,
ExecutionFunction: jest.fn(),
}
];
const util = new Util();
util.loadEvents(client, events);
expect(client.on).toHaveBeenCalledTimes(1);
expect(client.on).toHaveBeenCalledWith("messageCreate", expect.any(Function));
});
test("GIVEN event type is messageDelete, EXPECT event function to be executed", () => {
const client = {
on: jest.fn(),
} as unknown as Client;
const events: IEventItem[] = [
{
EventType: EventType.MessageDelete,
ExecutionFunction: jest.fn(),
}
];
const util = new Util();
util.loadEvents(client, events);
expect(client.on).toHaveBeenCalledTimes(1);
expect(client.on).toHaveBeenCalledWith("messageDelete", expect.any(Function));
});
test("GIVEN event type is messageUpdate, EXPECT event function to be executed", () => {
const client = {
on: jest.fn(),
} as unknown as Client;
const events: IEventItem[] = [
{
EventType: EventType.MessageUpdate,
ExecutionFunction: jest.fn(),
}
];
const util = new Util();
util.loadEvents(client, events);
expect(client.on).toHaveBeenCalledTimes(1);
expect(client.on).toHaveBeenCalledWith("messageUpdate", expect.any(Function));
});
test("GIVEN event type is not implemented, EXPECT error", () => {
const client = {
on: jest.fn(),
} as unknown as Client;
const events: IEventItem[] = [
{
EventType: EventType.Ready,
ExecutionFunction: jest.fn(),
}
];
console.error = jest.fn();
const util = new Util();
util.loadEvents(client, events);
expect(console.error).toHaveBeenCalledWith("Event not implemented.");
});
});

View file

@ -0,0 +1,391 @@
import { CommandInteraction, PermissionsBitField, SlashCommandBuilder, SlashCommandChannelOption, SlashCommandNumberOption, SlashCommandRoleOption, SlashCommandStringOption } from "discord.js";
import Add from "../../../../src/commands/501231711271780357/Lobby/add";
import Lobby from "../../../../src/database/entities/501231711271780357/Lobby";
describe('constuctor', () => {
test("EXPECT properties to be set", () => {
const add = new Add();
expect(add.CommandBuilder).toBeDefined();
const commandbuilder = add.CommandBuilder as SlashCommandBuilder;
expect(commandbuilder.name).toBe("addlobby");
expect(commandbuilder.description).toBe("Add lobby channel");
expect(commandbuilder.default_member_permissions).toBe(PermissionsBitField.Flags.ModerateMembers.toString());
expect(commandbuilder.options.length).toBe(4);
const channelOption = commandbuilder.options[0] as SlashCommandChannelOption;
expect(channelOption.name).toBe("channel");
expect(channelOption.description).toBe("The channel");
expect(channelOption.required).toBe(true);
const roleOption = commandbuilder.options[1] as SlashCommandRoleOption;
expect(roleOption.name).toBe("role");
expect(roleOption.description).toBe("The role to ping on request");
expect(roleOption.required).toBe(true);
const cooldownOption = commandbuilder.options[2] as SlashCommandNumberOption;
expect(cooldownOption.name).toBe("cooldown");
expect(cooldownOption.description).toBe("The cooldown in minutes");
expect(cooldownOption.required).toBe(true);
const nameOption = commandbuilder.options[3] as SlashCommandStringOption;
expect(nameOption.name).toBe("name");
expect(nameOption.description).toBe("The game name");
expect(nameOption.required).toBe(true);
});
});
describe('execute', () => {
test("EXPECT channel to be added to the lobby database table", async () => {
const channel = {
channel: {
id: "channelId",
},
name: "channelName",
};
const role = {
role: {},
name: "roleName",
};
const cooldown = {
value: "5",
};
const gameName = {
value: "test",
};
const interaction = {
options: {
get: jest.fn()
.mockReturnValueOnce(channel)
.mockReturnValueOnce(role)
.mockReturnValueOnce(cooldown)
.mockReturnValueOnce(gameName),
},
reply: jest.fn(),
} as unknown as CommandInteraction;
Lobby.FetchOneByChannelId = jest.fn().mockReturnValue(null);
Lobby.prototype.Save = jest.fn();
const add = new Add();
await add.execute(interaction);
expect(interaction.options.get).toHaveBeenCalledTimes(4);
expect(interaction.options.get).toHaveBeenCalledWith("channel");
expect(interaction.options.get).toHaveBeenCalledWith("role");
expect(interaction.options.get).toHaveBeenCalledWith("cooldown");
expect(interaction.options.get).toHaveBeenCalledWith("name");
expect(Lobby.FetchOneByChannelId).toHaveBeenCalledTimes(1);
expect(Lobby.FetchOneByChannelId).toHaveBeenCalledWith("channelId");
expect(Lobby.prototype.Save).toHaveBeenCalledTimes(1);
expect(interaction.reply).toHaveBeenCalledTimes(1);
expect(interaction.reply).toHaveBeenCalledWith("Added `channelName` as a new lobby channel with a cooldown of `5 minutes` and will ping `roleName` on use");
});
test("GIVEN channel is null, EXPECT error", async () => {
const channel = null;
const role = {
role: {},
name: "roleName",
};
const cooldown = {
value: "5",
};
const gameName = {
value: "test",
};
const interaction = {
options: {
get: jest.fn()
.mockReturnValueOnce(channel)
.mockReturnValueOnce(role)
.mockReturnValueOnce(cooldown)
.mockReturnValueOnce(gameName),
},
reply: jest.fn(),
} as unknown as CommandInteraction;
const add = new Add();
await add.execute(interaction);
expect(interaction.reply).toHaveBeenCalledTimes(1);
expect(interaction.reply).toHaveBeenCalledWith("Fields are required.");
});
test("GIVEN channel.channel is undefined, EXPECT error", async () => {
const channel = {
channel: undefined,
};
const role = {
role: {},
name: "roleName",
};
const cooldown = {
value: "5",
};
const gameName = {
value: "test",
};
const interaction = {
options: {
get: jest.fn()
.mockReturnValueOnce(channel)
.mockReturnValueOnce(role)
.mockReturnValueOnce(cooldown)
.mockReturnValueOnce(gameName),
},
reply: jest.fn(),
} as unknown as CommandInteraction;
const add = new Add();
await add.execute(interaction);
expect(interaction.reply).toHaveBeenCalledTimes(1);
expect(interaction.reply).toHaveBeenCalledWith("Fields are required.");
});
test("GIVEN role is null, EXPECT error", async () => {
const channel = {
channel: {},
};
const role = null;
const cooldown = {
value: "5",
};
const gameName = {
value: "test",
};
const interaction = {
options: {
get: jest.fn()
.mockReturnValueOnce(channel)
.mockReturnValueOnce(role)
.mockReturnValueOnce(cooldown)
.mockReturnValueOnce(gameName),
},
reply: jest.fn(),
} as unknown as CommandInteraction;
const add = new Add();
await add.execute(interaction);
expect(interaction.reply).toHaveBeenCalledTimes(1);
expect(interaction.reply).toHaveBeenCalledWith("Fields are required.");
});
test("GIVEN role.role is undefined, EXPECT error", async () => {
const channel = {
channel: {},
};
const role = {
role: undefined
};
const cooldown = {
value: "5",
};
const gameName = {
value: "test",
};
const interaction = {
options: {
get: jest.fn()
.mockReturnValueOnce(channel)
.mockReturnValueOnce(role)
.mockReturnValueOnce(cooldown)
.mockReturnValueOnce(gameName),
},
reply: jest.fn(),
} as unknown as CommandInteraction;
const add = new Add();
await add.execute(interaction);
expect(interaction.reply).toHaveBeenCalledTimes(1);
expect(interaction.reply).toHaveBeenCalledWith("Fields are required.");
});
test("GIVEN cooldown is null, EXPECT error", async () => {
const channel = {
channel: {},
};
const role = {
role: {},
name: "roleName",
};
const cooldown = null;
const gameName = {
value: "test",
};
const interaction = {
options: {
get: jest.fn()
.mockReturnValueOnce(channel)
.mockReturnValueOnce(role)
.mockReturnValueOnce(cooldown)
.mockReturnValueOnce(gameName),
},
reply: jest.fn(),
} as unknown as CommandInteraction;
const add = new Add();
await add.execute(interaction);
expect(interaction.reply).toHaveBeenCalledTimes(1);
expect(interaction.reply).toHaveBeenCalledWith("Fields are required.");
});
test("GIVEN cooldown.value is undefined, EXPECT error", async () => {
const channel = {
channel: {},
};
const role = {
role: {},
name: "roleName",
};
const cooldown = {
value: undefined,
};
const gameName = {
value: "test",
};
const interaction = {
options: {
get: jest.fn()
.mockReturnValueOnce(channel)
.mockReturnValueOnce(role)
.mockReturnValueOnce(cooldown)
.mockReturnValueOnce(gameName),
},
reply: jest.fn(),
} as unknown as CommandInteraction;
const add = new Add();
await add.execute(interaction);
expect(interaction.reply).toHaveBeenCalledTimes(1);
expect(interaction.reply).toHaveBeenCalledWith("Fields are required.");
});
test("GIVEN gameName is null, EXPECT error", async () => {
const channel = {
channel: {},
};
const role = {
role: {},
name: "roleName",
};
const cooldown = {
value: "5",
};
const gameName = null;
const interaction = {
options: {
get: jest.fn()
.mockReturnValueOnce(channel)
.mockReturnValueOnce(role)
.mockReturnValueOnce(cooldown)
.mockReturnValueOnce(gameName),
},
reply: jest.fn(),
} as unknown as CommandInteraction;
const add = new Add();
await add.execute(interaction);
expect(interaction.reply).toHaveBeenCalledTimes(1);
expect(interaction.reply).toHaveBeenCalledWith("Fields are required.");
});
test("GIVEN gameName.value is undefined, EXPECT error", async () => {
const channel = {
channel: {},
};
const role = {
role: {},
name: "roleName",
};
const cooldown = {
value: "5",
};
const gameName = {
value: undefined,
};
const interaction = {
options: {
get: jest.fn()
.mockReturnValueOnce(channel)
.mockReturnValueOnce(role)
.mockReturnValueOnce(cooldown)
.mockReturnValueOnce(gameName),
},
reply: jest.fn(),
} as unknown as CommandInteraction;
const add = new Add();
await add.execute(interaction);
expect(interaction.reply).toHaveBeenCalledTimes(1);
expect(interaction.reply).toHaveBeenCalledWith("Fields are required.");
});
test("GIVEN channel has already been set up in the database, EXPECT error", async () => {
const channel = {
channel: {
id: "channelId",
},
name: "channelName",
};
const role = {
role: {},
name: "roleName",
};
const cooldown = {
value: "5",
};
const gameName = {
value: "test",
};
const interaction = {
options: {
get: jest.fn()
.mockReturnValueOnce(channel)
.mockReturnValueOnce(role)
.mockReturnValueOnce(cooldown)
.mockReturnValueOnce(gameName),
},
reply: jest.fn(),
} as unknown as CommandInteraction;
Lobby.FetchOneByChannelId = jest.fn().mockReturnValue({});
Lobby.prototype.Save = jest.fn();
const add = new Add();
await add.execute(interaction);
expect(interaction.reply).toHaveBeenCalledTimes(1);
expect(interaction.reply).toHaveBeenCalledWith("This channel has already been setup.");
expect(Lobby.FetchOneByChannelId).toHaveBeenCalledTimes(1);
expect(Lobby.prototype.Save).not.toHaveBeenCalled();
});
});

View file

@ -0,0 +1,82 @@
import { APIEmbedField, CommandInteraction, EmbedBuilder, PermissionsBitField, SlashCommandBuilder } from "discord.js";
import List from "../../../../src/commands/501231711271780357/Lobby/list";
import Lobby from "../../../../src/database/entities/501231711271780357/Lobby";
import EmbedColours from "../../../../src/constants/EmbedColours";
describe('constructor', () => {
test("EXPECT properties to be set", () => {
const list = new List();
expect(list.CommandBuilder).toBeDefined();
const commandBulder = list.CommandBuilder as SlashCommandBuilder;
expect(commandBulder.name).toBe("listlobby");
expect(commandBulder.description).toBe("Lists all channels set up as lobbies");
expect(commandBulder.default_member_permissions).toBe(PermissionsBitField.Flags.ModerateMembers.toString());
});
});
describe("execute", () => {
test('EXPECT list of lobby channels to be sent', async () => {
let repliedWith;
const interaction = {
guild: {
channels: {
cache: {
map: jest.fn().mockReturnValue([{
id: "channelId",
}]),
}
}
},
reply: jest.fn().mockImplementation((options) => {
repliedWith = options;
}),
} as unknown as CommandInteraction;
Lobby.FetchOneByChannelId = jest.fn().mockReturnValue({
Name: "lobbyName",
LastUsed: "lastUsed",
});
const list = new List();
await list.execute(interaction);
expect(Lobby.FetchOneByChannelId).toHaveBeenCalledTimes(1);
expect(Lobby.FetchOneByChannelId).toHaveBeenCalledWith("channelId");
expect(interaction.reply).toHaveBeenCalledTimes(1);
expect(repliedWith).toBeDefined();
expect(repliedWith!.embeds).toBeDefined();
expect(repliedWith!.embeds.length).toBe(1);
const repliedEmbed: EmbedBuilder = repliedWith!.embeds[0];
expect(repliedEmbed.data.color).toBe(EmbedColours.Ok);
expect(repliedEmbed.data.title).toBe("Lobbies");
expect(repliedEmbed.data.description).toBe("Channels: 1");
expect(repliedEmbed.data.fields).toBeDefined();
expect(repliedEmbed.data.fields!.length).toBe(1);
const repliedEmbedField1: APIEmbedField = repliedEmbed.data.fields![0];
expect(repliedEmbedField1.name).toBe("# lobbyName");
expect(repliedEmbedField1.value).toBe("Last Used: lastUsed");
});
test("GIVEN interaction.guild is null, EXPECT error", async () => {
const interaction = {
guild: null,
reply: jest.fn(),
} as unknown as CommandInteraction;
const list = new List();
await list.execute(interaction);
expect(interaction.reply).toHaveBeenCalledTimes(1);
expect(interaction.reply).toHaveBeenCalledWith("Guild not found.");
});
});

View file

@ -0,0 +1,112 @@
import { CommandInteraction, SlashCommandBuilder } from "discord.js";
import Command from "../../../../src/commands/501231711271780357/Lobby/lobby";
import Lobby from "../../../../src/database/entities/501231711271780357/Lobby";
describe('constructor', () => {
test("EXPECT properties to be set", () => {
const lobby = new Command();
expect(lobby.CommandBuilder).toBeDefined();
const commandBuilder = lobby.CommandBuilder as SlashCommandBuilder;
expect(commandBuilder.name).toBe("lobby");
expect(commandBuilder.description).toBe("Attempt to organise a lobby");
});
});
describe("execute", () => {
test("EXPECT lobby command to announce a lobby setup", async () => {
const interaction = {
user: "user",
channelId: "channelId",
reply: jest.fn(),
} as unknown as CommandInteraction;
const markAsUsed = jest.fn();
const lobbySave = jest.fn();
Lobby.FetchOneByChannelId = jest.fn().mockResolvedValue({
Cooldown: 5,
LastUsed: new Date(2 * 60 * 1000),
Name: "gameName",
RoleId: "roleId",
MarkAsUsed: markAsUsed,
Save: lobbySave,
});
Date.now = jest.fn().mockReturnValue(10 * 60 * 1000);
const lobby = new Command();
await lobby.execute(interaction);
expect(Lobby.FetchOneByChannelId).toHaveBeenCalledTimes(1);
expect(Lobby.FetchOneByChannelId).toHaveBeenCalledWith("channelId");
expect(Date.now).toHaveBeenCalledTimes(1);
expect(markAsUsed).toHaveBeenCalledTimes(1);
expect(lobbySave).toHaveBeenCalledTimes(1);
expect(interaction.reply).toHaveBeenCalledTimes(1);
expect(interaction.reply).toHaveBeenCalledWith("user would like to organise a lobby of **gameName**! <@&roleId>");
});
test("GIVEN interaction.channelId is null, EXPECT nothing to happen", async () => {
const interaction = {
channelId: null,
reply: jest.fn(),
} as unknown as CommandInteraction;
const lobby = new Command();
await lobby.execute(interaction);
expect(interaction.reply).not.toHaveBeenCalled();
});
test("GIVEN channel is not setup in the database as a lobby, EXPECT error", async () => {
const interaction = {
channelId: "channelId",
reply: jest.fn(),
} as unknown as CommandInteraction;
Lobby.FetchOneByChannelId = jest.fn().mockResolvedValue(null);
const lobby = new Command();
await lobby.execute(interaction);
expect(Lobby.FetchOneByChannelId).toHaveBeenCalledTimes(1);
expect(interaction.reply).toHaveBeenCalledTimes(1);
expect(interaction.reply).toHaveBeenCalledWith("This channel is disabled from using the lobby command.");
});
test("GIVEN lobby command was last used within the cooldown, EXPECT error", async () => {
const interaction = {
user: "user",
channelId: "channelId",
reply: jest.fn(),
} as unknown as CommandInteraction;
const markAsUsed = jest.fn();
const lobbySave = jest.fn();
Lobby.FetchOneByChannelId = jest.fn().mockResolvedValue({
Cooldown: 5,
LastUsed: new Date(2 * 60 * 1000),
Name: "gameName",
RoleId: "roleId",
MarkAsUsed: markAsUsed,
Save: lobbySave,
});
Date.now = jest.fn().mockReturnValue(5 * 60 * 1000);
const lobby = new Command();
await lobby.execute(interaction);
expect(interaction.reply).toHaveBeenCalledTimes(1);
expect(interaction.reply).toHaveBeenCalledWith("Requesting a lobby for this game is on cooldown! Please try again in **2 minutes**.");
});
});

View file

@ -0,0 +1,116 @@
import { CommandInteraction, PermissionsBitField, SlashCommandBuilder, SlashCommandChannelOption } from "discord.js";
import Remove from "../../../../src/commands/501231711271780357/Lobby/remove";
import Lobby from "../../../../src/database/entities/501231711271780357/Lobby";
import BaseEntity from "../../../../src/contracts/BaseEntity";
describe('constructor', () => {
test("EXPECT properties to be set", () => {
const remove = new Remove();
expect(remove.CommandBuilder).toBeDefined();
const commandBuilder = remove.CommandBuilder as SlashCommandBuilder;
expect(commandBuilder.name).toBe("removelobby");
expect(commandBuilder.description).toBe("Remove a lobby channel");
expect(commandBuilder.default_member_permissions).toBe(PermissionsBitField.Flags.ModerateMembers.toString());
expect(commandBuilder.options.length).toBe(1);
const channelOption = commandBuilder.options[0] as SlashCommandChannelOption;
expect(channelOption.name).toBe("channel");
expect(channelOption.description).toBe("The channel");
expect(channelOption.required).toBe(true);
});
});
describe("execute", () => {
test("EXPECT channel to be removed from database", async () => {
const channel = {
channel: {
id: "channelId",
},
};
const interaction = {
options: {
get: jest.fn().mockReturnValue(channel),
},
reply: jest.fn(),
} as unknown as CommandInteraction;
Lobby.FetchOneByChannelId = jest.fn().mockResolvedValue({});
BaseEntity.Remove = jest.fn();
const remove = new Remove();
await remove.execute(interaction);
expect(Lobby.FetchOneByChannelId).toHaveBeenCalledTimes(1);
expect(Lobby.FetchOneByChannelId).toHaveBeenCalledWith("channelId");
expect(BaseEntity.Remove).toHaveBeenCalledTimes(1);
expect(interaction.options.get).toHaveBeenCalledTimes(1);
expect(interaction.options.get).toHaveBeenCalledWith("channel");
expect(interaction.reply).toHaveBeenCalledTimes(1);
expect(interaction.reply).toHaveBeenCalledWith("Removed <#channelId> from the list of lobby channels");
});
test("GIVEN channel is null, EXPECT error", async () => {
const interaction = {
options: {
get: jest.fn().mockReturnValue(null),
},
reply: jest.fn(),
} as unknown as CommandInteraction;
const remove = new Remove();
await remove.execute(interaction);
expect(interaction.reply).toHaveBeenCalledTimes(1);
expect(interaction.reply).toHaveBeenCalledWith("Channel is required.");
});
test("GIVEN channel.channel is undefined, EXPECT error", async () => {
const channel = {
channel: undefined,
};
const interaction = {
options: {
get: jest.fn().mockReturnValue(channel),
},
reply: jest.fn(),
} as unknown as CommandInteraction;
const remove = new Remove();
await remove.execute(interaction);
expect(interaction.reply).toHaveBeenCalledTimes(1);
expect(interaction.reply).toHaveBeenCalledWith("Channel is required.");
});
test("GIVEN channel is not set up as a lobby, EXPECT error", async () => {
const channel = {
channel: {
id: "channelId",
},
};
const interaction = {
options: {
get: jest.fn().mockReturnValue(channel),
},
reply: jest.fn(),
} as unknown as CommandInteraction;
Lobby.FetchOneByChannelId = jest.fn().mockResolvedValue(null);
const remove = new Remove();
await remove.execute(interaction);
expect(interaction.reply).toHaveBeenCalledTimes(1);
expect(interaction.reply).toHaveBeenCalledWith("Channel not found.");
});
});

View file

@ -0,0 +1,111 @@
import { CommandInteraction, EmbedBuilder, PermissionsBitField, SlashCommandBuilder } from "discord.js";
import Entry from "../../../src/commands/501231711271780357/entry";
import SettingsHelper from "../../../src/helpers/SettingsHelper";
import EmbedColours from "../../../src/constants/EmbedColours";
describe('constructor', () => {
test("EXPECT properties to be set", () => {
const entry = new Entry();
expect(entry.CommandBuilder).toBeDefined();
const commandBuilder = entry.CommandBuilder as SlashCommandBuilder;
expect(commandBuilder.name).toBe("entry");
expect(commandBuilder.description).toBe("Sends the entry embed");
expect(commandBuilder.default_member_permissions).toBe(PermissionsBitField.Flags.ModerateMembers.toString());
});
});
describe("execute", () => {
test("EXPECT entry embed to be sent", async () => {
let sentWith;
const interaction = {
guildId: "guildId",
channel: {
send: jest.fn().mockImplementation((options) => {
sentWith = options;
}),
},
} as unknown as CommandInteraction;
SettingsHelper.GetSetting = jest.fn().mockResolvedValue("channelId");
const entry = new Entry();
await entry.execute(interaction);
expect(SettingsHelper.GetSetting).toHaveBeenCalledTimes(1);
expect(SettingsHelper.GetSetting).toHaveBeenCalledWith("channels.rules", "guildId");
expect(interaction.channel!.send).toHaveBeenCalledTimes(1);
expect(sentWith).toBeDefined();
expect(sentWith!.embeds).toBeDefined();
expect(sentWith!.embeds.length).toBe(1);
const embed = sentWith!.embeds[0] as EmbedBuilder;
expect(embed.data.color).toBe(EmbedColours.Ok);
expect(embed.data.title).toBe("Welcome");
expect(embed.data.description).toBe("Welcome to the server! Please make sure to read the rules in the <#channelId> channel and type the code found there in here to proceed to the main part of the server.");
});
test("GIVEN interaction.guildId is null, EXPECT error", async () => {
const interaction = {
guildId: null,
} as unknown as CommandInteraction;
SettingsHelper.GetSetting = jest.fn();
const entry = new Entry();
await entry.execute(interaction);
expect(SettingsHelper.GetSetting).not.toHaveBeenCalled();
});
test("GIVEN interaction.channel is null, EXPECT error", async () => {
const interaction = {
guildId: "guildId",
channel: null,
} as unknown as CommandInteraction;
SettingsHelper.GetSetting = jest.fn();
const entry = new Entry();
await entry.execute(interaction);
expect(SettingsHelper.GetSetting).not.toHaveBeenCalled();
});
test("GIVEN channels.rules setting is not set, EXPECT channel id to be defaulted", async () => {
let sentWith;
const interaction = {
guildId: "guildId",
channel: {
send: jest.fn().mockImplementation((options) => {
sentWith = options;
}),
},
} as unknown as CommandInteraction;
SettingsHelper.GetSetting = jest.fn().mockResolvedValue(undefined);
const entry = new Entry();
await entry.execute(interaction);
expect(SettingsHelper.GetSetting).toHaveBeenCalledTimes(1);
expect(SettingsHelper.GetSetting).toHaveBeenCalledWith("channels.rules", "guildId");
expect(interaction.channel!.send).toHaveBeenCalledTimes(1);
expect(sentWith).toBeDefined();
expect(sentWith!.embeds).toBeDefined();
expect(sentWith!.embeds.length).toBe(1);
const embed = sentWith!.embeds[0] as EmbedBuilder;
expect(embed.data.color).toBe(EmbedColours.Ok);
expect(embed.data.title).toBe("Welcome");
expect(embed.data.description).toBe("Welcome to the server! Please make sure to read the rules in the <#rules> channel and type the code found there in here to proceed to the main part of the server.");
});
});

View file

@ -0,0 +1,227 @@
import { CommandInteraction, PermissionsBitField, SlashCommandBuilder, SlashCommandRoleOption } from "discord.js";
import Config from "../../../src/commands/Role/config";
import Role from "../../../src/database/entities/Role";
import Server from "../../../src/database/entities/Server";
describe('constructor', () => {
test('EXPECT properties to be set', () => {
const config = new Config();
expect(config.CommandBuilder).toBeDefined();
const commandBuilder = config.CommandBuilder as SlashCommandBuilder;
expect(commandBuilder.name).toBe("configrole");
expect(commandBuilder.description).toBe("Toggle your roles");
expect(commandBuilder.default_member_permissions).toBe(PermissionsBitField.Flags.ManageRoles.toString());
expect(commandBuilder.options.length).toBe(1);
const roleOption = commandBuilder.options[0] as SlashCommandRoleOption;
expect(roleOption.name).toBe("role");
expect(roleOption.description).toBe("The role name");
expect(roleOption.required).toBe(true);
});
});
describe("execute", () => {
test("GIVEN role is marked as assignable, EXPECT role to be removed", async () => {
const role = {
role: {
id: "roleId",
},
};
const interaction = {
guildId: "guildId",
guild: {},
member: {},
options: {
get: jest.fn().mockReturnValue(role),
},
reply: jest.fn(),
} as unknown as CommandInteraction;
Role.FetchOneByRoleId = jest.fn().mockResolvedValue({});
Role.Remove = jest.fn();
const config = new Config();
await config.execute(interaction);
expect(Role.FetchOneByRoleId).toHaveBeenCalledTimes(1);
expect(Role.FetchOneByRoleId).toHaveBeenCalledWith("roleId");
expect(Role.Remove).toHaveBeenCalledTimes(1);
expect(interaction.options.get).toHaveBeenCalledTimes(1);
expect(interaction.options.get).toHaveBeenCalledWith("role");
expect(interaction.reply).toHaveBeenCalledTimes(1);
expect(interaction.reply).toHaveBeenCalledWith("Removed role from configuration.");
});
test("GIVEN role is not marked as assignable, EXPECT role to be added", async () => {
let newRole: Role | undefined;
const role = {
role: {
id: "roleId",
},
};
const interaction = {
guildId: "guildId",
guild: {},
member: {},
options: {
get: jest.fn().mockReturnValue(role),
},
reply: jest.fn(),
} as unknown as CommandInteraction;
Role.FetchOneByRoleId = jest.fn().mockResolvedValue(null);
Role.prototype.SetServer = jest.fn();
Role.prototype.Save = jest.fn().mockImplementation((_, role) => {
newRole = role;
});
Server.FetchOneById = jest.fn().mockResolvedValue({});
const config = new Config();
await config.execute(interaction);
expect(Role.FetchOneByRoleId).toHaveBeenCalledTimes(1);
expect(Role.FetchOneByRoleId).toHaveBeenCalledWith("roleId");
expect(Role.prototype.SetServer).toHaveBeenCalledTimes(1);
expect(Role.prototype.Save).toHaveBeenCalledTimes(1);
expect(newRole).toBeDefined();
expect(newRole!.RoleId).toBe("roleId");
expect(interaction.options.get).toHaveBeenCalledTimes(1);
expect(interaction.options.get).toHaveBeenCalledWith("role");
expect(interaction.reply).toHaveBeenCalledTimes(1);
expect(interaction.reply).toHaveBeenCalledWith("Added role to configuration.");
});
test("GIVEN interaction.guildId is null, EXPECT error", async () => {
const interaction = {
guildId: null,
options: {
get: jest.fn(),
}
} as unknown as CommandInteraction;
const config = new Config();
await config.execute(interaction);
expect(interaction.options.get).not.toHaveBeenCalled();
});
test("GIVEN interaction.guild is null, EXPECT error", async () => {
const interaction = {
guildId: "guildId",
guild: null,
options: {
get: jest.fn(),
}
} as unknown as CommandInteraction;
const config = new Config();
await config.execute(interaction);
expect(interaction.options.get).not.toHaveBeenCalled();
});
test("GIVEN interaction.member is null, EXPECT error", async () => {
const interaction = {
guildId: "guildId",
guild: {},
member: null,
options: {
get: jest.fn(),
}
} as unknown as CommandInteraction;
const config = new Config();
await config.execute(interaction);
expect(interaction.options.get).not.toHaveBeenCalled();
});
test("GIVEN role is null, EXPECT error", async () => {
const interaction = {
guildId: "guildId",
guild: {},
member: {},
options: {
get: jest.fn().mockReturnValue(null),
},
reply: jest.fn(),
} as unknown as CommandInteraction;
const config = new Config();
await config.execute(interaction);
expect(interaction.reply).toHaveBeenCalledTimes(1);
expect(interaction.reply).toHaveBeenCalledWith("Fields are required.");
});
test("GIVEN role.role is undefined, EXPECT error", async () => {
const role = {
role: null,
}
const interaction = {
guildId: "guildId",
guild: {},
member: {},
options: {
get: jest.fn().mockReturnValue(role),
},
reply: jest.fn(),
} as unknown as CommandInteraction;
const config = new Config();
await config.execute(interaction);
expect(interaction.reply).toHaveBeenCalledTimes(1);
expect(interaction.reply).toHaveBeenCalledWith("Fields are required.");
});
test("GIVEN server is not configured in the database, EXPECT error", async () => {
let newRole: Role | undefined;
const role = {
role: {
id: "roleId",
},
};
const interaction = {
guildId: "guildId",
guild: {},
member: {},
options: {
get: jest.fn().mockReturnValue(role),
},
reply: jest.fn(),
} as unknown as CommandInteraction;
Role.FetchOneByRoleId = jest.fn().mockResolvedValue(null);
Role.prototype.SetServer = jest.fn();
Role.prototype.Save = jest.fn().mockImplementation((_, role) => {
newRole = role;
});
Server.FetchOneById = jest.fn().mockResolvedValue(null);
const config = new Config();
await config.execute(interaction);
expect(interaction.reply).toHaveBeenCalledTimes(1);
expect(interaction.reply).toHaveBeenCalledWith("This server has not been setup.");
});
});

View file

@ -0,0 +1,475 @@
import { CommandInteraction, GuildMemberRoleManager, SlashCommandBuilder, SlashCommandRoleOption, SlashCommandSubcommandBuilder } from "discord.js";
import Command from "../../../src/commands/Role/role";
import Role from "../../../src/database/entities/Role";
import { EmbedBuilder } from "@discordjs/builders";
import EmbedColours from "../../../src/constants/EmbedColours";
describe('Constructor', () => {
test('EXPECT properties are set', () => {
const command = new Command();
expect(command.CommandBuilder).toBeDefined();
const commandBuilder = command.CommandBuilder as SlashCommandBuilder;
expect(commandBuilder.name).toBe("role");
expect(commandBuilder.description).toBe("Toggle your roles");
expect(commandBuilder.options.length).toBe(2);
const toggleSubcommand = commandBuilder.options[0] as SlashCommandSubcommandBuilder;
expect(toggleSubcommand.name).toBe("toggle");
expect(toggleSubcommand.description).toBe("Toggle your role");
expect(toggleSubcommand.options.length).toBe(1);
const toggleRoleOption = toggleSubcommand.options[0] as SlashCommandRoleOption;
expect(toggleRoleOption.name).toBe("role");
expect(toggleRoleOption.description).toBe("The role name");
expect(toggleRoleOption.required).toBe(true);
const listSubcommand = commandBuilder.options[1] as SlashCommandSubcommandBuilder;
expect(listSubcommand.name).toBe("list");
expect(listSubcommand.description).toBe("List togglable roles");
});
});
describe('Execute', () => {
test("GIVEN interaction is not a chat input command, EXPECT nothing to happen", async () => {
const interaction = {
isChatInputCommand: jest.fn().mockReturnValue(false),
reply: jest.fn(),
} as unknown as CommandInteraction;
const command = new Command();
await command.execute(interaction);
expect(interaction.reply).not.toHaveBeenCalled();
});
test("GIVEN an invalid subcommand is given, EXPECT not found error", async () => {
const interaction = {
isChatInputCommand: jest.fn().mockReturnValue(true),
options: {
getSubcommand: jest.fn().mockReturnValue("other"),
},
reply: jest.fn(),
} as unknown as CommandInteraction;
const command = new Command();
await command.execute(interaction);
expect(interaction.reply).toHaveBeenCalledTimes(1);
expect(interaction.reply).toHaveBeenCalledWith("Subcommand not found.");
});
});
describe("toggle", () => {
test("GIVEN user has the role, EXPECT role to be removed", async () => {
const role = {
id: "roleId",
name: "roleName",
editable: true,
};
const requestedRole = {
role: {
name: "roleName",
},
};
const userRole = {};
const interaction = {
isChatInputCommand: jest.fn().mockReturnValue(true),
guildId: "guildId",
guild: {
roles: {
cache: {
find: jest.fn().mockReturnValue(role),
}
}
},
member: {
roles: {
cache: {
find: jest.fn().mockReturnValue(userRole),
},
remove: jest.fn(),
},
},
options: {
get: jest.fn().mockReturnValue(requestedRole),
getSubcommand: jest.fn().mockReturnValue("toggle"),
},
reply: jest.fn(),
} as unknown as CommandInteraction;
Role.FetchAllByServerId = jest.fn().mockResolvedValue([{}]);
const command = new Command();
await command.execute(interaction);
expect(Role.FetchAllByServerId).toHaveBeenCalledTimes(1);
expect(Role.FetchAllByServerId).toHaveBeenCalledWith("guildId");
expect(interaction.guild?.roles.cache.find).toHaveBeenCalledTimes(2);
expect(interaction.options.get).toHaveBeenCalledTimes(1);
expect(interaction.options.get).toHaveBeenCalledWith("role");
const roleManager = interaction.member!.roles as GuildMemberRoleManager;
expect(roleManager.cache.find).toHaveBeenCalledTimes(1);
expect(roleManager.remove).toHaveBeenCalledTimes(1);
expect(interaction.reply).toHaveBeenCalledTimes(1);
expect(interaction.reply).toHaveBeenCalledWith("Removed role: `roleName`");
});
test("GIVEN user does not have the role, EXPECT role to be added", async () => {
const role = {
id: "roleId",
name: "roleName",
editable: true,
};
const requestedRole = {
role: {
name: "roleName",
},
};
const interaction = {
isChatInputCommand: jest.fn().mockReturnValue(true),
guildId: "guildId",
guild: {
roles: {
cache: {
find: jest.fn().mockReturnValue(role),
}
}
},
member: {
roles: {
cache: {
find: jest.fn().mockReturnValue(undefined),
},
add: jest.fn(),
},
},
options: {
get: jest.fn().mockReturnValue(requestedRole),
getSubcommand: jest.fn().mockReturnValue("toggle"),
},
reply: jest.fn(),
} as unknown as CommandInteraction;
Role.FetchAllByServerId = jest.fn().mockResolvedValue([{}]);
const command = new Command();
await command.execute(interaction);
expect(Role.FetchAllByServerId).toHaveBeenCalledTimes(1);
expect(Role.FetchAllByServerId).toHaveBeenCalledWith("guildId");
expect(interaction.guild?.roles.cache.find).toHaveBeenCalledTimes(2);
expect(interaction.options.get).toHaveBeenCalledTimes(1);
expect(interaction.options.get).toHaveBeenCalledWith("role");
const roleManager = interaction.member!.roles as GuildMemberRoleManager;
expect(roleManager.cache.find).toHaveBeenCalledTimes(1);
expect(roleManager.add).toHaveBeenCalledTimes(1);
expect(interaction.reply).toHaveBeenCalledTimes(1);
expect(interaction.reply).toHaveBeenCalledWith("Gave role: `roleName`");
})
test("GIVEN interaction.guild is null, EXPECT nothing to happen", async () => {
const role = {
id: "roleId",
name: "roleName",
editable: true,
};
const requestedRole = {
role: {
name: "roleName",
},
};
const userRole = {};
const interaction = {
isChatInputCommand: jest.fn().mockReturnValue(true),
guildId: "guildId",
guild: null,
member: {
roles: {
cache: {
find: jest.fn().mockReturnValue(userRole),
},
add: jest.fn(),
remove: jest.fn(),
},
},
options: {
get: jest.fn().mockReturnValue(requestedRole),
getSubcommand: jest.fn().mockReturnValue("toggle"),
},
reply: jest.fn(),
} as unknown as CommandInteraction;
Role.FetchAllByServerId = jest.fn().mockResolvedValue([{}]);
const command = new Command();
await command.execute(interaction);
expect(interaction.reply).not.toHaveBeenCalled();
const roleManager = interaction.member!.roles as GuildMemberRoleManager;
expect(roleManager.add).not.toHaveBeenCalled();
expect(roleManager.remove).not.toHaveBeenCalled();
});
test("GIVEN interaction.member is null, EXPECT nothing to happen", async () => {
const role = {
id: "roleId",
name: "roleName",
editable: true,
};
const requestedRole = {
role: {
name: "roleName",
},
};
const userRole = {};
const interaction = {
isChatInputCommand: jest.fn().mockReturnValue(true),
guildId: "guildId",
guild: null,
member: {
roles: {
cache: {
find: jest.fn().mockReturnValue(userRole),
},
add: jest.fn(),
remove: jest.fn(),
},
},
options: {
get: jest.fn().mockReturnValue(requestedRole),
getSubcommand: jest.fn().mockReturnValue("toggle"),
},
reply: jest.fn(),
} as unknown as CommandInteraction;
Role.FetchAllByServerId = jest.fn().mockResolvedValue([{}]);
const command = new Command();
await command.execute(interaction);
expect(interaction.reply).not.toHaveBeenCalled();
const roleManager = interaction.member!.roles as GuildMemberRoleManager;
expect(roleManager.add).not.toHaveBeenCalled();
expect(roleManager.remove).not.toHaveBeenCalled();
});
test("GIVEN requestedRole is null, EXPECT invalid error", async () => {
const role = {
id: "roleId",
name: "roleName",
editable: true,
};
const userRole = {};
const interaction = {
isChatInputCommand: jest.fn().mockReturnValue(true),
guildId: "guildId",
guild: {
roles: {
cache: {
find: jest.fn().mockReturnValue(role),
}
}
},
member: {
roles: {
cache: {
find: jest.fn().mockReturnValue(userRole),
},
remove: jest.fn(),
},
},
options: {
get: jest.fn().mockReturnValue(null),
getSubcommand: jest.fn().mockReturnValue("toggle"),
},
reply: jest.fn(),
} as unknown as CommandInteraction;
Role.FetchAllByServerId = jest.fn().mockResolvedValue([{}]);
const command = new Command();
await command.execute(interaction);
expect(interaction.reply).toHaveBeenCalledTimes(1);
expect(interaction.reply).toHaveBeenCalledWith("Fields are required.");
});
test("GIVEN requestedRole.role is undefined, EXPECT invalid error", async () => {
const role = {
id: "roleId",
name: "roleName",
editable: true,
};
const userRole = {};
const requestedRole = {
role: undefined,
};
const interaction = {
isChatInputCommand: jest.fn().mockReturnValue(true),
guildId: "guildId",
guild: {
roles: {
cache: {
find: jest.fn().mockReturnValue(role),
}
}
},
member: {
roles: {
cache: {
find: jest.fn().mockReturnValue(userRole),
},
remove: jest.fn(),
},
},
options: {
get: jest.fn().mockReturnValue(requestedRole),
getSubcommand: jest.fn().mockReturnValue("toggle"),
},
reply: jest.fn(),
} as unknown as CommandInteraction;
Role.FetchAllByServerId = jest.fn().mockResolvedValue([{}]);
const command = new Command();
await command.execute(interaction);
expect(interaction.reply).toHaveBeenCalledTimes(1);
expect(interaction.reply).toHaveBeenCalledWith("Fields are required.");
});
test("GIVEN assignRole is not editable, EXPECT insufficient permissions error", async () => {
const role = {
id: "roleId",
name: "roleName",
editable: false,
};
const userRole = {};
const requestedRole = {
role: {
name: "roleName",
},
};
const interaction = {
isChatInputCommand: jest.fn().mockReturnValue(true),
guildId: "guildId",
guild: {
roles: {
cache: {
find: jest.fn().mockReturnValue(role),
}
}
},
member: {
roles: {
cache: {
find: jest.fn().mockReturnValue(userRole),
},
remove: jest.fn(),
},
},
options: {
get: jest.fn().mockReturnValue(requestedRole),
getSubcommand: jest.fn().mockReturnValue("toggle"),
},
reply: jest.fn(),
} as unknown as CommandInteraction;
Role.FetchAllByServerId = jest.fn().mockResolvedValue([{}]);
const command = new Command();
await command.execute(interaction);
expect(interaction.reply).toHaveBeenCalledTimes(1);
expect(interaction.reply).toHaveBeenCalledWith("Insufficient permissions. Please contact a moderator.");
});
});
describe("list", () => {
test("EXPECT role list to be sent", async () => {
let repliedWith;
const role = {
name: "roleName",
};
const interaction = {
guildId: "guildId",
guild: {
roles: {
cache: {
find: jest.fn().mockReturnValue(role),
}
}
},
isChatInputCommand: jest.fn().mockReturnValue(true),
options: {
getSubcommand: jest.fn().mockReturnValue("list"),
},
reply: jest.fn().mockImplementation((options) => {
repliedWith = options;
}),
} as unknown as CommandInteraction;
Role.FetchAllByServerId = jest.fn().mockResolvedValue([role]);
const command = new Command();
await command.execute(interaction);
expect(Role.FetchAllByServerId).toHaveBeenCalledTimes(1);
expect(Role.FetchAllByServerId).toHaveBeenCalledWith("guildId");
expect(interaction.guild?.roles.cache.find).toHaveBeenCalledTimes(1);
expect(interaction.reply).toHaveBeenCalledTimes(1);
expect(repliedWith).toBeDefined();
expect(repliedWith!.embeds).toBeDefined();
expect(repliedWith!.embeds.length).toBe(1);
const embed = repliedWith!.embeds[0] as EmbedBuilder;
expect(embed.data.color).toBe(EmbedColours.Ok);
expect(embed.data.title).toBe("Roles");
expect(embed.data.description).toBe("Roles: 1\n\nroleName");
});
});

View file

@ -0,0 +1,32 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`Constructor EXPECT properties to be set 1`] = `
{
"default_member_permissions": "8",
"default_permission": undefined,
"description": "Rules-related commands",
"description_localizations": undefined,
"dm_permission": undefined,
"name": "rules",
"name_localizations": undefined,
"nsfw": undefined,
"options": [
{
"description": "Send the rules embeds for this server",
"description_localizations": undefined,
"name": "embeds",
"name_localizations": undefined,
"options": [],
"type": 1,
},
{
"description": "Send the server verification embed button",
"description_localizations": undefined,
"name": "access",
"name_localizations": undefined,
"options": [],
"type": 1,
},
],
}
`;

View file

@ -0,0 +1,38 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`Execute EXPECT user to be warned: savedAudit 1`] = `
{
"AuditId": Any<String>,
"AuditType": 1,
"Id": Any<String>,
"ModeratorId": "moderatorId",
"Reason": "Test reason",
"ServerId": "guildId",
"UserId": "userId",
"WhenCreated": Any<Date>,
"WhenUpdated": Any<Date>,
}
`;
exports[`Execute EXPECT user to be warned: sentEmbeds 1`] = `
[
{
"color": 3166394,
"description": "<@userId> \`userTag\`",
"fields": [
{
"name": "Moderator",
"value": "<@moderatorId>",
},
{
"name": "Reason",
"value": "Test reason",
},
],
"thumbnail": {
"url": "https://google.com/avatar.png",
},
"title": "Member Warned",
},
]
`;

View file

@ -1,8 +1,6 @@
import { Message } from "discord.js";
import { mock } from "jest-mock-extended";
import { ActionRowBuilder, ButtonBuilder, ButtonStyle, CommandInteraction, EmbedBuilder, InteractionReplyOptions,SlashCommandBuilder } from "discord.js";
import About from "../../src/commands/about";
import { ICommandContext } from "../../src/contracts/ICommandContext";
import PublicEmbed from "../../src/helpers/embeds/PublicEmbed";
import EmbedColours from "../../src/constants/EmbedColours";
beforeEach(() => {
process.env = {};
@ -10,143 +8,169 @@ beforeEach(() => {
describe('Constructor', () => {
test('Expect values set', () => {
const about = new About();
const command = new About();
expect(about._category).toBe("General");
expect(command.CommandBuilder).toBeDefined();
const commandBuilder = command.CommandBuilder as SlashCommandBuilder;
expect(commandBuilder.name).toBe("about");
expect(commandBuilder.description).toBe("About VylBot");
});
});
describe('Execute', () => {
test('Expect embed to be made and sent to the current channel', async () => {
process.env = {
BOT_VER: "BOT_VER",
BOT_AUTHOR: "BOT_AUTHOR",
BOT_DATE: "BOT_DATE"
};
test('GIVEN ABOUT_FUNDING and ABOUT_REPO are NOT present in env var, EXPECT embed to be sent without buttons', async () => {
// Setup
let replyOptions: InteractionReplyOptions | undefined;
const message = mock<Message>();
message.channel.send = jest.fn();
const interaction = {
reply: jest.fn((options: InteractionReplyOptions) => {
replyOptions = options;
}),
} as unknown as CommandInteraction;
const context: ICommandContext = {
name: "about",
args: [],
message: message
};
// Execution
process.env.BOT_VER = "VERSION";
process.env.BOT_AUTHOR = "AUTHOR";
const about = new About();
const command = new About();
const result = await about.execute(context);
await command.execute(interaction);
expect(message.channel.send).toBeCalledTimes(1);
// Assertion
expect(interaction.reply).toHaveBeenCalledTimes(1);
expect(replyOptions).toBeDefined();
expect(replyOptions?.embeds).toBeDefined();
expect(replyOptions?.embeds?.length).toBe(1);
expect(replyOptions?.components).toBeDefined();
expect(replyOptions?.components?.length).toBe(0);
const repliedEmbed = replyOptions?.embeds![0] as EmbedBuilder;
expect(repliedEmbed.data.color).toBe(EmbedColours.Ok);
expect(repliedEmbed.data.title).toBe("About");
expect(repliedEmbed.data.description).toBe("Discord Bot made by Vylpes");
expect(repliedEmbed.data.fields?.length).toBe(2);
const repliedEmbedVersionField = repliedEmbed.data.fields![0];
expect(repliedEmbedVersionField.name).toBe("Version");
expect(repliedEmbedVersionField.value).toBe("VERSION");
expect(repliedEmbedVersionField.inline).toBeTruthy();
const repliedEmbedAuthorField = repliedEmbed.data.fields![1];
expect(repliedEmbedAuthorField.name).toBe("Author");
expect(repliedEmbedAuthorField.value).toBe("AUTHOR");
expect(repliedEmbedAuthorField.inline).toBeTruthy();
});
test('Expect embed send to have values', async () => {
process.env = {
BOT_VER: "BOT_VER",
BOT_AUTHOR: "BOT_AUTHOR",
BOT_DATE: "BOT_DATE"
};
test('GIVEN ABOUT_FUNDING is present in env var, EXPECT funding button to be sent', async () => {
// Setup
let replyOptions: InteractionReplyOptions | undefined;
const message = mock<Message>();
message.channel.send = jest.fn();
const interaction = {
reply: jest.fn((options: InteractionReplyOptions) => {
replyOptions = options;
}),
} as unknown as CommandInteraction;
const context: ICommandContext = {
name: "about",
args: [],
message: message
};
// Execution
process.env.BOT_VER = "VERSION";
process.env.BOT_AUTHOR = "AUTHOR";
process.env.ABOUT_FUNDING = "https://ko-fi.com/vylpes";
const about = new About();
const command = new About();
const result = await about.execute(context);
await command.execute(interaction);
expect(result.embeds.length).toBe(1);
// Assertion
expect(replyOptions?.components?.length).toBe(1);
const embed = result.embeds[0];
const repliedRow = replyOptions?.components![0] as ActionRowBuilder<ButtonBuilder>;
expect(embed.title).toBe('About');
expect(embed.description).toBe('');
expect(embed.fields.length).toBe(3);
expect(repliedRow.components?.length).toBe(1);
const repliedRowFundingComponent = repliedRow.components![0] as any;
expect(repliedRowFundingComponent.data.url).toBe("https://ko-fi.com/vylpes");
expect(repliedRowFundingComponent.data.label).toBe("Funding");
expect(repliedRowFundingComponent.data.style).toBe(ButtonStyle.Link);
});
test('Expect version field to have values', async () => {
process.env = {
BOT_VER: "BOT_VER",
BOT_AUTHOR: "BOT_AUTHOR",
BOT_DATE: "BOT_DATE"
};
test('GIVEN ABOUT_REPO is present in env var, EXPECT funding button to be sent', async () => {
// Setup
let replyOptions: InteractionReplyOptions | undefined;
const message = mock<Message>();
message.channel.send = jest.fn();
const interaction = {
reply: jest.fn((options: InteractionReplyOptions) => {
replyOptions = options;
}),
} as unknown as CommandInteraction;
const context: ICommandContext = {
name: "about",
args: [],
message: message
};
// Execution
process.env.BOT_VER = "VERSION";
process.env.BOT_AUTHOR = "AUTHOR";
process.env.ABOUT_REPO = "https://gitea.vylpes.xyz/rabbitlabs/vylbot-app";
const about = new About();
const command = new About();
const result = await about.execute(context);
await command.execute(interaction);
const embed = result.embeds[0];
const field = embed.fields[0];
// Assertion
expect(replyOptions?.components?.length).toBe(1);
expect(field.name).toBe('Version');
expect(field.value).toBe('BOT_VER');
const repliedRow = replyOptions?.components![0] as ActionRowBuilder<ButtonBuilder>;
expect(repliedRow.components?.length).toBe(1);
const repliedRowRepoComponent = repliedRow.components![0] as any;
expect(repliedRowRepoComponent.data.url).toBe("https://gitea.vylpes.xyz/rabbitlabs/vylbot-app");
expect(repliedRowRepoComponent.data.label).toBe("Repo");
expect(repliedRowRepoComponent.data.style).toBe(ButtonStyle.Link);
});
test('Expect author field to have values', async () => {
process.env = {
BOT_VER: "BOT_VER",
BOT_AUTHOR: "BOT_AUTHOR",
BOT_DATE: "BOT_DATE"
};
test('GIVEN ABOUT_REPO AND ABOUT_FUNDING is present in env var, EXPECT funding button to be sent', async () => {
// Setup
let replyOptions: InteractionReplyOptions | undefined;
const message = mock<Message>();
message.channel.send = jest.fn();
const interaction = {
reply: jest.fn((options: InteractionReplyOptions) => {
replyOptions = options;
}),
} as unknown as CommandInteraction;
const context: ICommandContext = {
name: "about",
args: [],
message: message
};
// Execution
process.env.BOT_VER = "VERSION";
process.env.BOT_AUTHOR = "AUTHOR";
process.env.ABOUT_REPO = "https://gitea.vylpes.xyz/rabbitlabs/vylbot-app";
process.env.ABOUT_FUNDING = "https://ko-fi.com/vylpes";
const about = new About();
const command = new About();
const result = await about.execute(context);
await command.execute(interaction);
const embed = result.embeds[0];
const field = embed.fields[1];
// Assertion
expect(replyOptions?.components?.length).toBe(1);
expect(field.name).toBe('Author');
expect(field.value).toBe('BOT_AUTHOR');
});
const repliedRow = replyOptions?.components![0] as ActionRowBuilder<ButtonBuilder>;
test('Expect version field to have values', async () => {
process.env = {
BOT_VER: "BOT_VER",
BOT_AUTHOR: "BOT_AUTHOR",
BOT_DATE: "BOT_DATE"
};
const message = mock<Message>();
message.channel.send = jest.fn();
expect(repliedRow.components?.length).toBe(2);
const context: ICommandContext = {
name: "about",
args: [],
message: message
};
const repliedRowRepoComponent = repliedRow.components![0] as any;
const about = new About();
expect(repliedRowRepoComponent.data.url).toBe("https://gitea.vylpes.xyz/rabbitlabs/vylbot-app");
expect(repliedRowRepoComponent.data.label).toBe("Repo");
expect(repliedRowRepoComponent.data.style).toBe(ButtonStyle.Link);
const result = await about.execute(context);
const repliedRowFundingComponent = repliedRow.components![1] as any;
const embed = result.embeds[0];
const field = embed.fields[2];
expect(field.name).toBe('Date');
expect(field.value).toBe('BOT_DATE');
expect(repliedRowFundingComponent.data.url).toBe("https://ko-fi.com/vylpes");
expect(repliedRowFundingComponent.data.label).toBe("Funding");
expect(repliedRowFundingComponent.data.style).toBe(ButtonStyle.Link);
});
});

View file

@ -0,0 +1,792 @@
import { CommandInteraction, EmbedBuilder, PermissionsBitField, SlashCommandBuilder, SlashCommandStringOption, SlashCommandSubcommandBuilder, SlashCommandUserOption } from "discord.js";
import Audits from "../../src/commands/audits";
import Audit from "../../src/database/entities/Audit";
import { AuditType } from "../../src/constants/AuditType";
import AuditTools from "../../src/helpers/AuditTools";
import EmbedColours from "../../src/constants/EmbedColours";
import StringTools from "../../src/helpers/StringTools";
describe("constructor", () => {
test("EXPECT properties to be set", () => {
const audits = new Audits();
expect(audits.CommandBuilder).toBeDefined();
const commandBuilder = audits.CommandBuilder as SlashCommandBuilder;
expect(commandBuilder.name).toBe("audits");
expect(commandBuilder.description).toBe("View audits of a particular user in the server");
expect(commandBuilder.default_member_permissions).toBe(PermissionsBitField.Flags.ModerateMembers.toString());
expect(commandBuilder.options.length).toBe(4);
const userSubcommand = commandBuilder.options[0] as SlashCommandSubcommandBuilder;
expect(userSubcommand.name).toBe("user");
expect(userSubcommand.description).toBe("View all audits done against a user");
expect(userSubcommand.options.length).toBe(1);
const userSubcommandUserOption = userSubcommand.options[0] as SlashCommandUserOption;
expect(userSubcommandUserOption.name).toBe("target");
expect(userSubcommandUserOption.description).toBe("The user");
expect(userSubcommandUserOption.required).toBe(true);
const viewSubcommand = commandBuilder.options[1] as SlashCommandSubcommandBuilder;
expect(viewSubcommand.name).toBe("view");
expect(viewSubcommand.description).toBe("View a particular audit");
expect(viewSubcommand.options.length).toBe(1);
const viewSubcommandAuditIdOption = viewSubcommand.options[0] as SlashCommandStringOption;
expect(viewSubcommandAuditIdOption.name).toBe("auditid");
expect(viewSubcommandAuditIdOption.description).toBe("The audit id in caps");
expect(viewSubcommandAuditIdOption.required).toBe(true);
const clearSubcommand = commandBuilder.options[2] as SlashCommandSubcommandBuilder;
expect(clearSubcommand.name).toBe("clear");
expect(clearSubcommand.description).toBe("Clears an audit from a user");
expect(clearSubcommand.options.length).toBe(1);
const clearSubcommandAuditIdOption = clearSubcommand.options[0] as SlashCommandStringOption;
expect(clearSubcommandAuditIdOption.name).toBe("auditid");
expect(clearSubcommandAuditIdOption.description).toBe("The audit id in caps");
expect(clearSubcommandAuditIdOption.required).toBe(true);
const addSubcommand = commandBuilder.options[3] as SlashCommandSubcommandBuilder;
expect(addSubcommand.name).toBe("add");
expect(addSubcommand.description).toBe("Manually add an audit");
expect(addSubcommand.options.length).toBe(3);
const addSubcommandUserOption = addSubcommand.options[0] as SlashCommandUserOption;
expect(addSubcommandUserOption.name).toBe("target");
expect(addSubcommandUserOption.description).toBe("The user");
expect(addSubcommandUserOption.required).toBe(true);
const addSubcommandTypeOption = addSubcommand.options[1] as SlashCommandStringOption;
expect(addSubcommandTypeOption.name).toBe("type");
expect(addSubcommandTypeOption.description).toBe("The type of audit");
expect(addSubcommandTypeOption.required).toBe(true);
expect(addSubcommandTypeOption.choices).toBeDefined();
expect(addSubcommandTypeOption.choices!.length).toBe(5);
const addSubcommandReasonOption = addSubcommand.options[2] as SlashCommandStringOption;
expect(addSubcommandReasonOption.name).toBe("reason");
expect(addSubcommandReasonOption.description).toBe("The reason");
});
});
describe('execute', () => {
test("GIVEN interaction is not a chat input command, EXPECT nothing to happen", async () => {
const interaction = {
isChatInputCommand: jest.fn().mockReturnValue(false),
reply: jest.fn(),
} as unknown as CommandInteraction;
const audits = new Audits();
await audits.execute(interaction);
expect(interaction.isChatInputCommand).toHaveBeenCalledTimes(1);
expect(interaction.reply).not.toHaveBeenCalled();
});
test("GIVEN subcommand is invalid, EXPECT error", async () => {
const interaction = {
isChatInputCommand: jest.fn().mockReturnValue(true),
options: {
getSubcommand: jest.fn().mockReturnValue("invalid"),
},
reply: jest.fn(),
} as unknown as CommandInteraction;
const audits = new Audits();
await audits.execute(interaction);
expect(interaction.reply).toHaveBeenCalledTimes(1);
expect(interaction.reply).toHaveBeenCalledWith("Subcommand doesn't exist.");
});
});
describe("user", () => {
test("EXPECT audits for user to be sent", async () => {
let repliedWith;
const user = {
id: "userId",
};
const audit = {
AuditId: "auditId",
AuditType: AuditType.Warn,
WhenCreated: new Date(5 * 60 * 1000),
};
const interaction = {
isChatInputCommand: jest.fn().mockReturnValue(true),
options: {
getSubcommand: jest.fn().mockReturnValue("user"),
get: jest.fn().mockReturnValue({
user: user
}),
},
guildId: "guildId",
reply: jest.fn().mockImplementation((options) => {
repliedWith = options;
})
} as unknown as CommandInteraction;
Audit.FetchAuditsByUserId = jest.fn().mockReturnValue([ audit ]);
AuditTools.TypeToFriendlyText = jest.fn().mockReturnValue("Warn");
const audits = new Audits();
await audits.execute(interaction);
expect(Audit.FetchAuditsByUserId).toHaveBeenCalledTimes(1);
expect(Audit.FetchAuditsByUserId).toHaveBeenCalledWith("userId", "guildId");
expect(AuditTools.TypeToFriendlyText).toHaveBeenCalledTimes(1);
expect(AuditTools.TypeToFriendlyText).toHaveBeenCalledWith(AuditType.Warn);
expect(interaction.options.get).toHaveBeenCalledTimes(1);
expect(interaction.options.get).toHaveBeenCalledWith("target", true);
expect(interaction.reply).toHaveBeenCalledTimes(1);
expect(repliedWith).toBeDefined();
expect(repliedWith!.embeds).toBeDefined();
expect(repliedWith!.embeds.length).toBe(1);
const embed = repliedWith!.embeds[0] as EmbedBuilder;
expect(embed.data.color).toBe(EmbedColours.Ok);
expect(embed.data.title).toBe("Audits");
expect(embed.data.description).toBe("Audits: 1");
expect(embed.data.fields).toBeDefined();
expect(embed.data.fields?.length).toBe(1);
const embedField = embed.data.fields![0];
expect(embedField.name).toBe("auditId // Warn");
expect(embedField.value).toBe(new Date(5 * 60 * 1000).toString());
});
test("GIVEN interaction.guildId is null, EXPECT nothing to happen", async () => {
const interaction = {
isChatInputCommand: jest.fn().mockReturnValue(true),
options: {
getSubcommand: jest.fn().mockReturnValue("user"),
},
guildId: null,
reply: jest.fn(),
} as unknown as CommandInteraction;
const audits = new Audits();
await audits.execute(interaction);
expect(interaction.reply).not.toHaveBeenCalled();
});
test("GIVEN user is null, EXPECT error", async () => {
const interaction = {
isChatInputCommand: jest.fn().mockReturnValue(true),
options: {
getSubcommand: jest.fn().mockReturnValue("user"),
get: jest.fn().mockReturnValue({
user: null
}),
},
guildId: "guildId",
reply: jest.fn(),
} as unknown as CommandInteraction;
const audits = new Audits();
await audits.execute(interaction);
expect(interaction.options.get).toHaveBeenCalledTimes(1);
expect(interaction.reply).toHaveBeenCalledTimes(1);
expect(interaction.reply).toHaveBeenCalledWith("User not found.");
});
test("GIVEN audits null, EXPECT no audits to be displayed", async () => {
const interaction = {
isChatInputCommand: jest.fn().mockReturnValue(true),
options: {
getSubcommand: jest.fn().mockReturnValue("user"),
get: jest.fn().mockReturnValue({
user: {},
}),
},
guildId: "guildId",
reply: jest.fn(),
} as unknown as CommandInteraction;
Audit.FetchAuditsByUserId = jest.fn().mockResolvedValue(null);
const audits = new Audits();
await audits.execute(interaction);
expect(Audit.FetchAuditsByUserId).toHaveBeenCalledTimes(1);
expect(interaction.options.get).toHaveBeenCalledTimes(1);
expect(interaction.reply).toHaveBeenCalledTimes(1);
expect(interaction.reply).toHaveBeenCalledWith("There are no audits for this user.");
});
test("GIVEN audits length is 0, EXPECT no audits to be displayed", async () => {
const interaction = {
isChatInputCommand: jest.fn().mockReturnValue(true),
options: {
getSubcommand: jest.fn().mockReturnValue("user"),
get: jest.fn().mockReturnValue({
user: {},
}),
},
guildId: "guildId",
reply: jest.fn(),
} as unknown as CommandInteraction;
Audit.FetchAuditsByUserId = jest.fn().mockResolvedValue([]);
const audits = new Audits();
await audits.execute(interaction);
expect(Audit.FetchAuditsByUserId).toHaveBeenCalledTimes(1);
expect(interaction.options.get).toHaveBeenCalledTimes(1);
expect(interaction.reply).toHaveBeenCalledTimes(1);
expect(interaction.reply).toHaveBeenCalledWith("There are no audits for this user.");
});
});
describe("view", () => {
test("EXPECT specific audit defaults to be sent", async () => {
let repliedWith;
const auditOption = {
value: "auditId",
};
const audit = {
Reason: "Test reason",
AuditType: AuditType.Warn,
ModeratorId: "moderatorId",
AuditId: "auditId",
};
const interaction = {
guildId: "guildId",
isChatInputCommand: jest.fn().mockReturnValue(true),
options: {
getSubcommand: jest.fn().mockReturnValue("view"),
get: jest.fn().mockReturnValue(auditOption),
},
reply: jest.fn().mockImplementation((options) => {
repliedWith = options;
}),
} as unknown as CommandInteraction;
Audit.FetchAuditByAuditId = jest.fn().mockResolvedValue(audit);
AuditTools.TypeToFriendlyText = jest.fn().mockReturnValue("Warn");
const audits = new Audits();
await audits.execute(interaction);
expect(Audit.FetchAuditByAuditId).toHaveBeenCalledTimes(1);
expect(Audit.FetchAuditByAuditId).toHaveBeenCalledWith("AUDITID", "guildId");
expect(interaction.reply).toHaveBeenCalledTimes(1);
expect(repliedWith).toBeDefined();
expect(repliedWith!.embeds).toBeDefined();
expect(repliedWith!.embeds.length).toBe(1);
const embed = repliedWith!.embeds[0] as EmbedBuilder;
expect(embed.data.color).toBe(EmbedColours.Ok);
expect(embed.data.title).toBe("Audit");
expect(embed.data.description).toBe("AUDITID");
expect(embed.data.fields).toBeDefined();
expect(embed.data.fields!.length).toBe(3);
const embedReasonField = embed.data.fields![0];
expect(embedReasonField.name).toBe("Reason");
expect(embedReasonField.value).toBe("Test reason");
expect(embedReasonField.inline).toBe(true);
const embedTypeField = embed.data.fields![1];
expect(embedTypeField.name).toBe("Type");
expect(embedTypeField.value).toBe("Warn");
expect(embedTypeField.inline).toBe(true);
const embedModeratorField = embed.data.fields![2];
expect(embedModeratorField.name).toBe("Moderator");
expect(embedModeratorField.value).toBe("<@moderatorId>");
expect(embedModeratorField.inline).toBe(true);
});
test("GIVEN interaction.guildId is null, expect nothing to happen", async () => {
const interaction = {
isChatInputCommand: jest.fn().mockReturnValue(true),
options: {
getSubcommand: jest.fn().mockReturnValue("view"),
},
guildId: null,
reply: jest.fn(),
} as unknown as CommandInteraction;
const audits = new Audits();
await audits.execute(interaction);
expect(interaction.reply).not.toHaveBeenCalled();
});
test("GIVEN auditId is null, EXPECT error", async () => {
const interaction = {
guildId: "guildId",
isChatInputCommand: jest.fn().mockReturnValue(true),
options: {
getSubcommand: jest.fn().mockReturnValue("view"),
get: jest.fn().mockReturnValue(null),
},
reply: jest.fn(),
} as unknown as CommandInteraction;
const audits = new Audits();
await audits.execute(interaction);
expect(interaction.options.get).toHaveBeenCalledTimes(1);
expect(interaction.options.get).toHaveBeenCalledWith("auditid");
expect(interaction.reply).toHaveBeenCalledTimes(1);
expect(interaction.reply).toHaveBeenCalledWith("AuditId not found.");
});
test("GIVEN auditId.value is undefined, EXPECT error", async () => {
const audit = {
value: undefined,
}
const interaction = {
guildId: "guildId",
isChatInputCommand: jest.fn().mockReturnValue(true),
options: {
getSubcommand: jest.fn().mockReturnValue("view"),
get: jest.fn().mockReturnValue(audit),
},
reply: jest.fn(),
} as unknown as CommandInteraction;
const audits = new Audits();
await audits.execute(interaction);
expect(interaction.options.get).toHaveBeenCalledTimes(1);
expect(interaction.options.get).toHaveBeenCalledWith("auditid");
expect(interaction.reply).toHaveBeenCalledTimes(1);
expect(interaction.reply).toHaveBeenCalledWith("AuditId not found.");
});
test("GIVEN audit is not in database, EXPECT error", async () => {
const audit = {
value: "auditId",
}
const interaction = {
guildId: "guildId",
isChatInputCommand: jest.fn().mockReturnValue(true),
options: {
getSubcommand: jest.fn().mockReturnValue("view"),
get: jest.fn().mockReturnValue(audit),
},
reply: jest.fn(),
} as unknown as CommandInteraction;
Audit.FetchAuditByAuditId = jest.fn().mockResolvedValue(null);
const audits = new Audits();
await audits.execute(interaction);
expect(Audit.FetchAuditByAuditId).toHaveBeenCalledTimes(1);
expect(interaction.options.get).toHaveBeenCalledTimes(1);
expect(interaction.options.get).toHaveBeenCalledWith("auditid");
expect(interaction.reply).toHaveBeenCalledTimes(1);
expect(interaction.reply).toHaveBeenCalledWith("Audit not found.");
});
test("GIVEN audit.Reason was not supplied, EXPECT reason to be defaulted", async () => {
let repliedWith;
const auditOption = {
value: "auditId",
};
const audit = {
Reason: undefined,
AuditType: AuditType.Warn,
ModeratorId: "moderatorId",
AuditId: "auditId",
};
const interaction = {
guildId: "guildId",
isChatInputCommand: jest.fn().mockReturnValue(true),
options: {
getSubcommand: jest.fn().mockReturnValue("view"),
get: jest.fn().mockReturnValue(auditOption),
},
reply: jest.fn().mockImplementation((options) => {
repliedWith = options;
}),
} as unknown as CommandInteraction;
Audit.FetchAuditByAuditId = jest.fn().mockResolvedValue(audit);
AuditTools.TypeToFriendlyText = jest.fn().mockReturnValue("Warn");
const audits = new Audits();
await audits.execute(interaction);
const embed = repliedWith!.embeds[0] as EmbedBuilder;
const embedReasonField = embed.data.fields![0];
expect(embedReasonField.name).toBe("Reason");
expect(embedReasonField.value).toBe("*none*");
expect(embedReasonField.inline).toBe(true);
});
});
describe("clear", () => {
test("EXPECT audit to be cleared", async () => {
const auditOption = {
value: "auditId",
}
const audit = {};
const interaction = {
isChatInputCommand: jest.fn().mockReturnValue(true),
options: {
getSubcommand: jest.fn().mockReturnValue("clear"),
get: jest.fn().mockReturnValue(auditOption),
},
guildId: "guildId",
reply: jest.fn(),
} as unknown as CommandInteraction;
Audit.FetchAuditByAuditId = jest.fn().mockReturnValue(audit);
Audit.Remove = jest.fn();
const audits = new Audits();
await audits.execute(interaction);
expect(Audit.FetchAuditByAuditId).toHaveBeenCalledTimes(1);
expect(Audit.FetchAuditByAuditId).toHaveBeenCalledWith("AUDITID", "guildId");
expect(Audit.Remove).toHaveBeenCalledTimes(1);
expect(interaction.options.get).toHaveBeenCalledTimes(1);
expect(interaction.options.get).toHaveBeenCalledWith("auditid");
expect(interaction.reply).toHaveBeenCalledTimes(1);
expect(interaction.reply).toHaveBeenCalledWith("Audit cleared.");
});
test("GIVEN interaction.guildId is null, EXPECT nothing to happen", async () => {
const interaction = {
isChatInputCommand: jest.fn().mockReturnValue(true),
options: {
getSubcommand: jest.fn().mockReturnValue("clear"),
},
reply: jest.fn(),
} as unknown as CommandInteraction;
const audits = new Audits();
await audits.execute(interaction);
expect(interaction.reply).not.toHaveBeenCalled();
});
test("GIVEN auditId is null, EXPECT error", async () => {
const interaction = {
guildId: "guildId",
isChatInputCommand: jest.fn().mockReturnValue(true),
options: {
getSubcommand: jest.fn().mockReturnValue("clear"),
get: jest.fn().mockReturnValue(null),
},
reply: jest.fn(),
} as unknown as CommandInteraction;
const audits = new Audits();
await audits.execute(interaction);
expect(interaction.reply).toHaveBeenCalledTimes(1);
expect(interaction.reply).toHaveBeenCalledWith("AuditId not found.");
});
test("GIVEN auditId.value is undefined, EXPECT error", async () => {
const auditOption = {
value: undefined,
}
const interaction = {
guildId: "guildId",
isChatInputCommand: jest.fn().mockReturnValue(true),
options: {
getSubcommand: jest.fn().mockReturnValue("clear"),
get: jest.fn().mockReturnValue(auditOption),
},
reply: jest.fn(),
} as unknown as CommandInteraction;
const audits = new Audits();
await audits.execute(interaction);
expect(interaction.reply).toHaveBeenCalledTimes(1);
expect(interaction.reply).toHaveBeenCalledWith("AuditId not found.");
});
test("GIVEN audit is not found, EXPECT error", async () => {
const auditOption = {
value: "auditId",
}
const interaction = {
guildId: "guildId",
isChatInputCommand: jest.fn().mockReturnValue(true),
options: {
getSubcommand: jest.fn().mockReturnValue("clear"),
get: jest.fn().mockReturnValue(auditOption),
},
reply: jest.fn(),
} as unknown as CommandInteraction;
Audit.FetchAuditByAuditId = jest.fn().mockReturnValue(null);
const audits = new Audits();
await audits.execute(interaction);
expect(Audit.FetchAuditByAuditId).toHaveBeenCalledTimes(1);
expect(interaction.reply).toHaveBeenCalledTimes(1);
expect(interaction.reply).toHaveBeenCalledWith("Audit not found.");
});
});
describe("add", () => {
test("EXPECT audit to be added", async () => {
const user = {
id: "userId",
};
const type = {
value: AuditType.Warn,
};
const reason = {};
const interaction = {
guildId: "guildId",
isChatInputCommand: jest.fn().mockReturnValue(true),
options: {
getSubcommand: jest.fn().mockReturnValue("add"),
get: jest.fn()
.mockReturnValueOnce({ user })
.mockReturnValueOnce(type)
.mockReturnValue(reason),
},
user: {
id: "userId",
},
reply: jest.fn(),
} as unknown as CommandInteraction;
Audit.prototype.Save = jest.fn();
StringTools.RandomString = jest.fn().mockReturnValue("AUDITID");
const audits = new Audits();
await audits.execute(interaction);
expect(Audit.prototype.Save).toHaveBeenCalledTimes(1);
expect(interaction.reply).toHaveBeenCalledTimes(1);
expect(interaction.reply).toHaveBeenCalledWith("Created new audit with ID `AUDITID`");
});
test("GIVEN interaction.guildId is null, EXPECT nothing to happen", async () => {
const interaction = {
guildId: null,
isChatInputCommand: jest.fn().mockReturnValue(true),
options: {
getSubcommand: jest.fn().mockReturnValue("add"),
},
reply: jest.fn(),
} as unknown as CommandInteraction;
const audits = new Audits();
await audits.execute(interaction);
expect(interaction.reply).not.toHaveBeenCalled();
});
test("GIVEN user is null, EXPECT error", async () => {
const interaction = {
guildId: "guildId",
isChatInputCommand: jest.fn().mockReturnValue(true),
options: {
getSubcommand: jest.fn().mockReturnValue("add"),
get: jest.fn()
.mockReturnValueOnce({ user: null })
.mockReturnValue({}),
},
reply: jest.fn(),
} as unknown as CommandInteraction;
const audits = new Audits();
await audits.execute(interaction);
expect(interaction.reply).toHaveBeenCalledTimes(1);
expect(interaction.reply).toHaveBeenCalledWith("Invalid input.");
});
test("GIVEN auditType is null, EXPECT error", async () => {
const interaction = {
guildId: "guildId",
isChatInputCommand: jest.fn().mockReturnValue(true),
options: {
getSubcommand: jest.fn().mockReturnValue("add"),
get: jest.fn()
.mockReturnValueOnce({ user: null })
.mockReturnValueOnce(null)
.mockReturnValue({}),
},
reply: jest.fn(),
} as unknown as CommandInteraction;
const audits = new Audits();
await audits.execute(interaction);
expect(interaction.reply).toHaveBeenCalledTimes(1);
expect(interaction.reply).toHaveBeenCalledWith("Invalid input.");
});
test("GIVEN auditType.value is undefined, EXPECT error", async () => {
const interaction = {
guildId: "guildId",
isChatInputCommand: jest.fn().mockReturnValue(true),
options: {
getSubcommand: jest.fn().mockReturnValue("add"),
get: jest.fn()
.mockReturnValueOnce({ user: null })
.mockReturnValueOnce({
value: undefined
})
.mockReturnValue({}),
},
reply: jest.fn(),
} as unknown as CommandInteraction;
const audits = new Audits();
await audits.execute(interaction);
expect(interaction.reply).toHaveBeenCalledTimes(1);
expect(interaction.reply).toHaveBeenCalledWith("Invalid input.");
});
test("GIVEN reasonInput is null, EXPECT reason to be empty", async ()=> {
let savedAudit: Audit | undefined;
const user = {
id: "userId",
};
const type = {
value: AuditType.Warn,
};
const interaction = {
guildId: "guildId",
isChatInputCommand: jest.fn().mockReturnValue(true),
options: {
getSubcommand: jest.fn().mockReturnValue("add"),
get: jest.fn()
.mockReturnValueOnce({ user })
.mockReturnValueOnce(type)
.mockReturnValue(null),
},
user: {
id: "userId",
},
reply: jest.fn(),
} as unknown as CommandInteraction;
Audit.prototype.Save = jest.fn().mockImplementation((_, audit: Audit) => {
savedAudit = audit;
});
StringTools.RandomString = jest.fn().mockReturnValue("AUDITID");
const audits = new Audits();
await audits.execute(interaction);
expect(Audit.prototype.Save).toHaveBeenCalledTimes(1);
expect(savedAudit).toBeDefined();
expect(savedAudit!.Reason).toBe("");
});
test("GIVEN reasonType.value is undefined, EXPECT reason to be empty", async () => {
let savedAudit: Audit | undefined;
const user = {
id: "userId",
};
const type = {
value: AuditType.Warn,
};
const reason = {
value: undefined,
};
const interaction = {
guildId: "guildId",
isChatInputCommand: jest.fn().mockReturnValue(true),
options: {
getSubcommand: jest.fn().mockReturnValue("add"),
get: jest.fn()
.mockReturnValueOnce({ user })
.mockReturnValueOnce(type)
.mockReturnValue(reason),
},
user: {
id: "userId",
},
reply: jest.fn(),
} as unknown as CommandInteraction;
Audit.prototype.Save = jest.fn().mockImplementation((_, audit: Audit) => {
savedAudit = audit;
});
StringTools.RandomString = jest.fn().mockReturnValue("AUDITID");
const audits = new Audits();
await audits.execute(interaction);
expect(Audit.prototype.Save).toHaveBeenCalledTimes(1);
expect(savedAudit).toBeDefined();
expect(savedAudit!.Reason).toBe("");
});
});

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,125 @@
import { CommandInteraction, SlashCommandBuilder } from "discord.js";
import Bunny from "../../src/commands/bunny";
import * as randomBunny from "random-bunny";
import { EmbedBuilder } from "@discordjs/builders";
import EmbedColours from "../../src/constants/EmbedColours";
beforeEach(() => {
jest.resetAllMocks();
jest.resetModules();
})
describe('constructor', () => {
test("EXPECT properties to be set", () => {
const bunny = new Bunny();
expect(bunny.CommandBuilder).toBeDefined();
const commandBuilder = bunny.CommandBuilder as SlashCommandBuilder;
expect(commandBuilder.name).toBe("bunny");
expect(commandBuilder.description).toBe("Get a random picture of a rabbit.");
});
});
describe('execute', () => {
test("EXPECT embed to be sent", async () => {
let repliedWith;
const interaction = {
isChatInputCommand: jest.fn().mockReturnValue(true),
editReply: jest.fn().mockImplementation((options) => {
repliedWith = options;
}),
deferReply: jest.fn(),
} as unknown as CommandInteraction;
jest.spyOn(randomBunny, "default").mockResolvedValue({
IsSuccess: true,
Result: {
Title: "title",
Permalink: "/r/permalink",
Url: "https://permalink.com/url.png",
Ups: 1,
Archived: false,
Downs: 1,
Hidden: false,
Subreddit: "rabbits",
SubredditSubscribers: 1,
},
Error: undefined,
Query: {
subreddit: "rabbits",
sortBy: "hot",
},
});
Math.floor = jest.fn().mockReturnValue(0);
const bunny = new Bunny();
await bunny.execute(interaction);
expect(randomBunny.default).toHaveBeenCalledTimes(1);
expect(randomBunny.default).toHaveBeenCalledWith("rabbits", "hot");
expect(interaction.editReply).toHaveBeenCalledTimes(1);
expect(interaction.deferReply).toHaveBeenCalledTimes(1);
expect(repliedWith).toBeDefined();
expect(repliedWith!.embeds).toBeDefined();
expect(repliedWith!.embeds.length).toBe(1);
const embed = repliedWith!.embeds[0] as EmbedBuilder;
expect(embed.data.color).toBe(EmbedColours.Ok);
expect(embed.data.title).toBe("title");
expect(embed.data.description).toBe("/r/permalink");
expect(embed.data.image).toBeDefined();
expect(embed.data.image!.url).toBe("https://permalink.com/url.png");
expect(embed.data.url).toBe("https://reddit.com/r/permalink");
expect(embed.data.footer).toBeDefined();
expect(embed.data.footer!.text).toBe("r/rabbits · 1 upvotes");
});
test("GIVEN interaction is not a chat input command, EXPECT nothing to happen", async () => {
const interaction = {
isChatInputCommand: jest.fn().mockReturnValue(false),
reply: jest.fn(),
} as unknown as CommandInteraction;
const bunny = new Bunny();
await bunny.execute(interaction);
expect(interaction.reply).not.toHaveBeenCalled();
});
test("GIVEN a result from random-bunny is failure, EXPECT error", async () => {
const interaction = {
isChatInputCommand: jest.fn().mockReturnValue(true),
editReply: jest.fn(),
deferReply: jest.fn(),
} as unknown as CommandInteraction;
jest.spyOn(randomBunny, "default").mockResolvedValue({
IsSuccess: false,
Result: undefined,
Error: undefined,
Query: {
subreddit: "rabbits",
sortBy: "hot",
},
});
Math.floor = jest.fn().mockReturnValue(0);
const bunny = new Bunny();
await bunny.execute(interaction);
expect(randomBunny.default).toHaveBeenCalledTimes(1);
expect(interaction.editReply).toHaveBeenCalledTimes(1);
expect(interaction.editReply).toHaveBeenCalledWith("There was an error running this command.");
expect(interaction.deferReply).toHaveBeenCalledTimes(1);
});
});

View file

@ -1,178 +1,216 @@
import { Message } from "discord.js";
import Clear from "../../src/commands/clear";
import { ICommandContext } from "../../src/contracts/ICommandContext";
import { ChatInputCommandInteraction, PermissionsBitField, SlashCommandBuilder, SlashCommandNumberOption, TextChannel } from "discord.js";
import Command from "../../src/commands/clear";
beforeEach(() => {
process.env = {};
});
describe('Constructor', () => {
test('Expect values to be set', () => {
process.env = {
ROLES_MODERATOR: "Moderator"
};
test("EXPECT values to be set", () => {
const command = new Command();
const clear = new Clear();
expect(command.CommandBuilder).toBeDefined();
expect(clear._category).toBe('Moderation');
expect(clear._roles.length).toBe(1);
expect(clear._roles[0]).toBe('Moderator');
const commandBuilder = command.CommandBuilder as SlashCommandBuilder;
expect(commandBuilder.name).toBe("clear");
expect(commandBuilder.description).toBe("Clears the channel of messages");
expect(commandBuilder.default_member_permissions).toBe(PermissionsBitField.Flags.ManageMessages.toString());
expect(commandBuilder.options.length).toBe(1);
const commandBuilderCountOption = commandBuilder.options[0] as SlashCommandNumberOption;
expect(commandBuilderCountOption.name).toBe("count");
expect(commandBuilderCountOption.description).toBe("The amount to delete");
expect(commandBuilderCountOption.required).toBe(true);
expect(commandBuilderCountOption.min_value).toBe(1);
expect(commandBuilderCountOption.max_value).toBe(100);
});
});
describe('Execute', () => {
test('Given valid arguments, expect messages to be cleared', async () => {
const messageChannelSend = jest.fn();
const messageChannelBulkDelete = jest.fn();
test("GIVEN input is valid, EXPECT messages to be cleared", async () => {
// Arrange
const channel = {
manageable: true,
bulkDelete: jest.fn(),
} as unknown as TextChannel;
const message = {
channel: {
send: messageChannelSend,
bulkDelete: messageChannelBulkDelete
}
} as unknown as Message;
const interaction = {
isChatInputCommand: jest.fn().mockReturnValue(true),
channel: channel,
options: {
getNumber: jest.fn().mockReturnValue(50),
},
reply: jest.fn(),
} as unknown as ChatInputCommandInteraction;
const context: ICommandContext = {
name: 'clear',
args: ['5'],
message: message
};
// Act
const command = new Command();
await command.execute(interaction);
const clear = new Clear();
const result = await clear.execute(context);
// Assert
expect(interaction.isChatInputCommand).toHaveBeenCalledTimes(1);
expect(messageChannelSend).toBeCalledTimes(1);
expect(messageChannelBulkDelete).toBeCalledWith(5);
expect(result.embeds.length).toBe(1);
expect(interaction.options.getNumber).toHaveBeenCalledTimes(1);
expect(interaction.options.getNumber).toHaveBeenCalledWith("count");
// PublicEmbed
const publicEmbed = result.embeds[0];
expect(channel.bulkDelete).toHaveBeenCalledTimes(1);
expect(channel.bulkDelete).toHaveBeenCalledWith(50);
expect(publicEmbed.title).toBe('');
expect(publicEmbed.description).toBe('5 message(s) were removed');
expect(interaction.reply).toHaveBeenCalledTimes(1);
expect(interaction.reply).toHaveBeenCalledWith("50 message(s) were removed.");
});
test('Given argument is not given, expect error embed to be sent', async () => {
const messageChannelSend = jest.fn();
const messageChannelBulkDelete = jest.fn();
test("GIVEN interaction is NOT a chat input command, EXPECT nothing to happen", async () => {
// Arrange
const channel = {
manageable: true,
bulkDelete: jest.fn(),
} as unknown as TextChannel;
const message = {
channel: {
send: messageChannelSend,
bulkDelete: messageChannelBulkDelete
}
} as unknown as Message;
const interaction = {
isChatInputCommand: jest.fn().mockReturnValue(false),
channel: channel,
options: {
getNumber: jest.fn().mockReturnValue(50),
},
reply: jest.fn(),
} as unknown as ChatInputCommandInteraction;
const context: ICommandContext = {
name: 'clear',
args: [],
message: message
};
// Act
const command = new Command();
await command.execute(interaction);
const clear = new Clear();
const result = await clear.execute(context);
expect(messageChannelSend).toBeCalledTimes(1);
expect(messageChannelBulkDelete).not.toBeCalled();
expect(result.embeds.length).toBe(1);
// ErrorEmbed
const errorEmbed = result.embeds[0];
expect(errorEmbed.title).toBeNull();
expect(errorEmbed.description).toBe('Please specify an amount between 1 and 100');
// Assert
expect(interaction.reply).not.toHaveBeenCalled();
expect(channel.bulkDelete).not.toHaveBeenCalled();
});
test('Given argument is not a number, expect error embed to be sent', async () => {
const messageChannelSend = jest.fn();
const messageChannelBulkDelete = jest.fn();
test("GIVEN interaction.channel is null, EXPECT nothing to happen", async () => {
// Arrange
const interaction = {
isChatInputCommand: jest.fn().mockReturnValue(true),
channel: null,
options: {
getNumber: jest.fn().mockReturnValue(50),
},
reply: jest.fn(),
} as unknown as ChatInputCommandInteraction;
const message = {
channel: {
send: messageChannelSend,
bulkDelete: messageChannelBulkDelete
}
} as unknown as Message;
// Act
const command = new Command();
await command.execute(interaction);
const context: ICommandContext = {
name: 'clear',
args: ['A'],
message: message
};
const clear = new Clear();
const result = await clear.execute(context);
expect(messageChannelSend).toBeCalledTimes(1);
expect(messageChannelBulkDelete).not.toBeCalled();
expect(result.embeds.length).toBe(1);
// ErrorEmbed
const errorEmbed = result.embeds[0];
expect(errorEmbed.title).toBeNull();
expect(errorEmbed.description).toBe('Please specify an amount between 1 and 100');
// Assert
expect(interaction.reply).not.toHaveBeenCalled();
});
test('Given argument is less than 1, expect error embed to be sent', async () => {
const messageChannelSend = jest.fn();
const messageChannelBulkDelete = jest.fn();
test("GIVEN totalClear input is NOT supplied, EXPECT invalid error", async () => {
// Arrange
const channel = {
manageable: true,
bulkDelete: jest.fn(),
} as unknown as TextChannel;
const message = {
channel: {
send: messageChannelSend,
bulkDelete: messageChannelBulkDelete
}
} as unknown as Message;
const interaction = {
isChatInputCommand: jest.fn().mockReturnValue(true),
channel: channel,
options: {
getNumber: jest.fn().mockReturnValue(null),
},
reply: jest.fn(),
} as unknown as ChatInputCommandInteraction;
const context: ICommandContext = {
name: 'clear',
args: ['0'],
message: message
};
// Act
const command = new Command();
await command.execute(interaction);
const clear = new Clear();
const result = await clear.execute(context);
// Assert
expect(interaction.reply).toHaveBeenCalledTimes(1);
expect(interaction.reply).toHaveBeenCalledWith("Please specify an amount between 1 and 100.");
expect(messageChannelSend).toBeCalledTimes(1);
expect(messageChannelBulkDelete).not.toBeCalled();
expect(result.embeds.length).toBe(1);
// ErrorEmbed
const errorEmbed = result.embeds[0];
expect(errorEmbed.title).toBeNull();
expect(errorEmbed.description).toBe('Please specify an amount between 1 and 100');
expect(channel.bulkDelete).not.toHaveBeenCalled();
});
test('Given argument is more than 100, expect error embed to be sent', async () => {
const messageChannelSend = jest.fn();
const messageChannelBulkDelete = jest.fn();
test("GIVEN totalClear is less than or equal to 0, EXPECT invalid error", async () => {
// Arrange
const channel = {
manageable: true,
bulkDelete: jest.fn(),
} as unknown as TextChannel;
const message = {
channel: {
send: messageChannelSend,
bulkDelete: messageChannelBulkDelete
}
} as unknown as Message;
const interaction = {
isChatInputCommand: jest.fn().mockReturnValue(true),
channel: channel,
options: {
getNumber: jest.fn().mockReturnValue(0),
},
reply: jest.fn(),
} as unknown as ChatInputCommandInteraction;
const context: ICommandContext = {
name: 'clear',
args: ['101'],
message: message
};
// Act
const command = new Command();
await command.execute(interaction);
const clear = new Clear();
const result = await clear.execute(context);
// Assert
expect(interaction.reply).toHaveBeenCalledTimes(1);
expect(interaction.reply).toHaveBeenCalledWith("Please specify an amount between 1 and 100.");
expect(messageChannelSend).toBeCalledTimes(1);
expect(messageChannelBulkDelete).not.toBeCalled();
expect(result.embeds.length).toBe(1);
expect(channel.bulkDelete).not.toHaveBeenCalled();
});
// ErrorEmbed
const errorEmbed = result.embeds[0];
test("GIVEN totalClear is greater than 100, EXPECT invalid error", async () => {
// Arrange
const channel = {
manageable: true,
bulkDelete: jest.fn(),
} as unknown as TextChannel;
expect(errorEmbed.title).toBeNull();
expect(errorEmbed.description).toBe('Please specify an amount between 1 and 100');
const interaction = {
isChatInputCommand: jest.fn().mockReturnValue(true),
channel: channel,
options: {
getNumber: jest.fn().mockReturnValue(101),
},
reply: jest.fn(),
} as unknown as ChatInputCommandInteraction;
// Act
const command = new Command();
await command.execute(interaction);
// Assert
expect(interaction.reply).toHaveBeenCalledTimes(1);
expect(interaction.reply).toHaveBeenCalledWith("Please specify an amount between 1 and 100.");
expect(channel.bulkDelete).not.toHaveBeenCalled();
});
test("GIVEN channel is NOT manageable, EXPECT insufficient permissions error", async () => {
// Arrange
const channel = {
manageable: false,
bulkDelete: jest.fn(),
} as unknown as TextChannel;
const interaction = {
isChatInputCommand: jest.fn().mockReturnValue(true),
channel: channel,
options: {
getNumber: jest.fn().mockReturnValue(50),
},
reply: jest.fn(),
} as unknown as ChatInputCommandInteraction;
// Act
const command = new Command();
await command.execute(interaction);
// Assert
expect(interaction.reply).toHaveBeenCalledTimes(1);
expect(interaction.reply).toHaveBeenCalledWith("Insufficient permissions. Please contact a moderator.");
expect(channel.bulkDelete).not.toHaveBeenCalled();
});
});

261
tests/commands/code.test.ts Normal file
View file

@ -0,0 +1,261 @@
import { APIEmbed, APIVersion, ChatInputCommandInteraction, CommandInteraction, SlashCommandBuilder, SlashCommandSubcommandBuilder } from "discord.js";
import Command from "../../src/commands/code";
import StringTools from "../../src/helpers/StringTools";
import SettingsHelper from "../../src/helpers/SettingsHelper";
describe('constructor', () => {
test("EXPECT properties to be set", () => {
const command = new Command();
expect(command.CommandBuilder).toBeDefined();
const commandBuilder = command.CommandBuilder as SlashCommandBuilder;
expect(commandBuilder.name).toBe("code");
expect(commandBuilder.description).toBe("Manage the verification code of the server");
expect(commandBuilder.options.length).toBe(2);
const commandBuilderRandomiseSubcommand = commandBuilder.options[0] as SlashCommandSubcommandBuilder;
expect(commandBuilderRandomiseSubcommand.name).toBe("randomise");
expect(commandBuilderRandomiseSubcommand.description).toBe("Regenerates the verification code for this server");
const commandBuilderEmbedSubcommand = commandBuilder.options[1] as SlashCommandSubcommandBuilder;
expect(commandBuilderEmbedSubcommand.name).toBe("embed");
expect(commandBuilderEmbedSubcommand.description).toBe("Sends the embed with the current code to the current channel");
});
});
describe("execute", () => {
test("GIVEN interaction is not a chat input command, EXPECT nothing to happen", async () => {
// Assert
const interaction = {
isChatInputCommand: jest.fn().mockReturnValue(false),
reply: jest.fn(),
} as unknown as CommandInteraction;
// Act
const command = new Command();
await command.execute(interaction);
// Assert
expect(interaction.isChatInputCommand).toHaveBeenCalledTimes(1);
expect(interaction.reply).not.toHaveBeenCalled();
});
});
describe("randomise", () => {
test("EXPECT entry code to be randomised", async () => {
// Arrange
const interaction = {
isChatInputCommand: jest.fn().mockReturnValue(true),
options: {
getSubcommand: jest.fn().mockReturnValue("randomise"),
},
reply: jest.fn(),
guildId: "guildId",
} as unknown as ChatInputCommandInteraction;
StringTools.RandomString = jest.fn().mockReturnValue("12345");
SettingsHelper.SetSetting = jest.fn();
// Act
const command = new Command();
await command.execute(interaction);
// Assert
expect(interaction.isChatInputCommand).toHaveBeenCalledTimes(1);
expect(interaction.options.getSubcommand).toHaveBeenCalledTimes(1);
expect(StringTools.RandomString).toHaveBeenCalledTimes(1);
expect(StringTools.RandomString).toHaveBeenCalledWith(5);
expect(SettingsHelper.SetSetting).toHaveBeenCalledTimes(1);
expect(SettingsHelper.SetSetting).toHaveBeenCalledWith("verification.code", "guildId", "12345");
expect(interaction.reply).toHaveBeenCalledTimes(1);
expect(interaction.reply).toHaveBeenCalledWith("Entry code has been set to `12345`");
});
test("GIVEN interaction.guildId is null, EXPECT nothing to happen", async () => {
// Arrange
const interaction = {
isChatInputCommand: jest.fn().mockReturnValue(true),
options: {
getSubcommand: jest.fn().mockReturnValue("randomise"),
},
reply: jest.fn(),
guildId: null,
} as unknown as ChatInputCommandInteraction;
StringTools.RandomString = jest.fn().mockReturnValue("12345");
SettingsHelper.SetSetting = jest.fn();
// Act
const command = new Command();
await command.execute(interaction);
// Assert
expect(interaction.reply).not.toHaveBeenCalled();
expect(SettingsHelper.SetSetting).not.toHaveBeenCalled();
expect(StringTools.RandomString).not.toHaveBeenCalled();
});
});
describe("embed", () => {
test("EXPECT embed to be sent", async () => {
let repliedWithEmbed: any;
// Arrange
const interaction = {
isChatInputCommand: jest.fn().mockReturnValue(true),
options: {
getSubcommand: jest.fn().mockReturnValue("embed"),
},
reply: jest.fn(),
guildId: "guildId",
channel: {
send: jest.fn().mockImplementation((options: any) => {
repliedWithEmbed = options.embeds[0];
}),
},
} as unknown as ChatInputCommandInteraction;
SettingsHelper.GetSetting = jest.fn().mockResolvedValue("12345");
// Act
const command = new Command();
await command.execute(interaction);
// Assert
expect(interaction.isChatInputCommand).toHaveBeenCalledTimes(1);
expect(interaction.options.getSubcommand).toHaveBeenCalledTimes(1);
expect(SettingsHelper.GetSetting).toHaveBeenCalledTimes(1);
expect(SettingsHelper.GetSetting).toHaveBeenCalledWith("verification.code", "guildId");
expect(interaction.channel!.send).toHaveBeenCalledTimes(1);
expect(repliedWithEmbed).toBeDefined();
expect(repliedWithEmbed.data.title).toBe("Entry Code");
expect(repliedWithEmbed.data.description).toBe("12345");
});
test("GIVEN interaction.guildId is null, EXPECT nothing to happen", async () => {
let repliedWithEmbed: any;
// Arrange
const interaction = {
isChatInputCommand: jest.fn().mockReturnValue(true),
options: {
getSubcommand: jest.fn().mockReturnValue("embed"),
},
reply: jest.fn(),
guildId: null,
channel: {
send: jest.fn().mockImplementation((options: any) => {
repliedWithEmbed = options.embeds[0];
}),
},
} as unknown as ChatInputCommandInteraction;
SettingsHelper.GetSetting = jest.fn().mockResolvedValue("12345");
// Act
const command = new Command();
await command.execute(interaction);
// Assert
expect(interaction.channel!.send).not.toHaveBeenCalled();
expect(SettingsHelper.GetSetting).not.toHaveBeenCalled();
expect(repliedWithEmbed).toBeUndefined();
});
test("GIVEN interaction.channel is null, EXPECT nothing to happen", async () => {
// Arrange
const interaction = {
isChatInputCommand: jest.fn().mockReturnValue(true),
options: {
getSubcommand: jest.fn().mockReturnValue("embed"),
},
reply: jest.fn(),
guildId: "guildId",
channel: null,
} as unknown as ChatInputCommandInteraction;
SettingsHelper.GetSetting = jest.fn().mockResolvedValue("12345");
// Act
const command = new Command();
await command.execute(interaction);
// Assert
expect(SettingsHelper.GetSetting).not.toHaveBeenCalled();
});
test("GIVEN verification.code setting is undefined, EXPECT error", async () => {
let repliedWithEmbed: any;
// Arrange
const interaction = {
isChatInputCommand: jest.fn().mockReturnValue(true),
options: {
getSubcommand: jest.fn().mockReturnValue("embed"),
},
reply: jest.fn(),
guildId: "guildId",
channel: {
send: jest.fn().mockImplementation((options: any) => {
repliedWithEmbed = options.embeds[0];
}),
},
} as unknown as ChatInputCommandInteraction;
SettingsHelper.GetSetting = jest.fn().mockResolvedValue(undefined);
// Act
const command = new Command();
await command.execute(interaction);
// Assert
expect(interaction.reply).toHaveBeenCalledTimes(1);
expect(interaction.reply).toHaveBeenCalledWith("There is no code for this server setup.");
expect(interaction.channel! .send).not.toHaveBeenCalled();
});
test("GIVEN verification.code setting is empty, EXPECT error", async () => {
let repliedWithEmbed: any;
// Arrange
const interaction = {
isChatInputCommand: jest.fn().mockReturnValue(true),
options: {
getSubcommand: jest.fn().mockReturnValue("embed"),
},
reply: jest.fn(),
guildId: "guildId",
channel: {
send: jest.fn().mockImplementation((options: any) => {
repliedWithEmbed = options.embeds[0];
}),
},
} as unknown as ChatInputCommandInteraction;
SettingsHelper.GetSetting = jest.fn().mockResolvedValue("");
// Act
const command = new Command();
await command.execute(interaction);
// Assert
expect(interaction.reply).toHaveBeenCalledTimes(1);
expect(interaction.reply).toHaveBeenCalledWith("There is no code for this server setup.");
expect(interaction.channel!.send).not.toHaveBeenCalled();
});
});

View file

@ -0,0 +1,612 @@
import { ChatInputCommandInteraction, CommandInteraction, PermissionsBitField, SlashCommandBuilder, SlashCommandStringOption, SlashCommandSubcommandBuilder } from "discord.js";
import Command from "../../src/commands/config";
import Server from "../../src/database/entities/Server";
import fs from "fs";
import EmbedColours from "../../src/constants/EmbedColours";
import Setting from "../../src/database/entities/Setting";
import DefaultValues from "../../src/constants/DefaultValues";
beforeEach(() => {
process.cwd = jest.fn().mockReturnValue("/cwd");
});
describe("constructor", () => {
test("EXPECT properties to be set", () => {
const command = new Command();
expect(command.CommandBuilder).toBeDefined();
const commandBuilder = command.CommandBuilder as SlashCommandBuilder;
expect(commandBuilder.name).toBe("config");
expect(commandBuilder.description).toBe("Configure the current server");
expect(commandBuilder.default_member_permissions).toBe(PermissionsBitField.Flags.Administrator.toString());
expect(commandBuilder.options.length).toBe(4);
const commandBuilderResetSubcommand = commandBuilder.options[0] as SlashCommandSubcommandBuilder;
expect(commandBuilderResetSubcommand.name).toBe("reset");
expect(commandBuilderResetSubcommand.description).toBe("Reset a setting to the default");
expect(commandBuilderResetSubcommand.options.length).toBe(1);
const commandBuilderResetSubcommandKeyOption = commandBuilderResetSubcommand.options[0] as SlashCommandStringOption;
expect(commandBuilderResetSubcommandKeyOption.name).toBe("key");
expect(commandBuilderResetSubcommandKeyOption.description).toBe("The key");
expect(commandBuilderResetSubcommandKeyOption.required).toBe(true);
const commandBuilderGetSubcommand = commandBuilder.options[1] as SlashCommandSubcommandBuilder;
expect(commandBuilderGetSubcommand.name).toBe("get");
expect(commandBuilderGetSubcommand.description).toBe("Gets a setting for the server");
expect(commandBuilderGetSubcommand.options.length).toBe(1);
const commandBuilderGetSubcommandKeyOption = commandBuilderGetSubcommand.options[0] as SlashCommandStringOption;
expect(commandBuilderGetSubcommandKeyOption.name).toBe("key");
expect(commandBuilderGetSubcommandKeyOption.description).toBe("The key");
expect(commandBuilderGetSubcommandKeyOption.required).toBe(true);
const commandBuilderSetSubcommand = commandBuilder.options[2] as SlashCommandSubcommandBuilder;
expect(commandBuilderSetSubcommand.name).toBe("set");
expect(commandBuilderSetSubcommand.description).toBe("Sets a setting to a specified value");
expect(commandBuilderSetSubcommand.options.length).toBe(2);
const commandBuilderSetSubcommandKeyOption = commandBuilderSetSubcommand.options[0] as SlashCommandStringOption;
expect(commandBuilderSetSubcommandKeyOption.name).toBe("key");
expect(commandBuilderSetSubcommandKeyOption.description).toBe("The key");
expect(commandBuilderSetSubcommandKeyOption.required).toBe(true);
const commandBuilderSetSubcommandValueOption = commandBuilderSetSubcommand.options[1] as SlashCommandStringOption;
expect(commandBuilderSetSubcommandValueOption.name).toBe("value");
expect(commandBuilderSetSubcommandValueOption.description).toBe("The value");
expect(commandBuilderSetSubcommandValueOption.required).toBe(true);
const commandBuilderListSubcommand = commandBuilder.options[3] as SlashCommandSubcommandBuilder;
expect(commandBuilderListSubcommand.name).toBe("list");
expect(commandBuilderListSubcommand.description).toBe("Lists all settings");
});
});
describe("execute", () => {
test("GIVEN interaction is not a chat input command, EXPECT nothing to happen", async () => {
// Arrange
const interaction = {
isChatInputCommand: jest.fn().mockReturnValue(false),
options: {
getSubcommand: jest.fn(),
},
} as unknown as ChatInputCommandInteraction;
// Act
const command = new Command();
await command.execute(interaction);
// Assert
expect(interaction.isChatInputCommand).toHaveBeenCalledTimes(1);
expect(interaction.options.getSubcommand).not.toHaveBeenCalled();
});
test("GIVEN interaction.guildId is null, EXPECT nothing to happen", async () => {
// Arrange
const interaction = {
isChatInputCommand: jest.fn().mockReturnValue(true),
options: {
getSubcommand: jest.fn(),
},
guildId: null,
} as unknown as ChatInputCommandInteraction;
// Act
const command = new Command();
await command.execute(interaction);
// Assert
expect(interaction.isChatInputCommand).toHaveBeenCalledTimes(1);
expect(interaction.options.getSubcommand).not.toHaveBeenCalled();
});
test("GIVEN server is not set up in the database, EXPECT error", async () => {
// Arrange
const interaction = {
isChatInputCommand: jest.fn().mockReturnValue(true),
options: {
getSubcommand: jest.fn(),
},
guildId: "guildId",
reply: jest.fn(),
} as unknown as ChatInputCommandInteraction;
Server.FetchOneById = jest.fn().mockResolvedValue(null);
// Act
const command = new Command();
await command.execute(interaction);
// Assert
expect(Server.FetchOneById).toHaveBeenCalledTimes(1);
expect(Server.FetchOneById).toHaveBeenCalledWith(Server, "guildId", [ "Settings" ]);
expect(interaction.reply).toHaveBeenCalledTimes(1);
expect(interaction.reply).toHaveBeenCalledWith("Server not setup. Please use the setup command.");
expect(interaction.options.getSubcommand).not.toHaveBeenCalled();
});
test("GIVEN subcommand is invalid, EXPECT error", async () => {
// Arrange
const interaction = {
isChatInputCommand: jest.fn().mockReturnValue(true),
options: {
getSubcommand: jest.fn().mockReturnValue("invalid"),
},
guildId: "guildId",
reply: jest.fn(),
} as unknown as ChatInputCommandInteraction;
Server.FetchOneById = jest.fn().mockResolvedValue({});
// Act
const command = new Command();
await command.execute(interaction);
// Assert
expect(interaction.options.getSubcommand).toHaveBeenCalledTimes(1);
expect(interaction.reply).toHaveBeenCalledTimes(1);
expect(interaction.reply).toHaveBeenCalledWith("Subcommand not found.");
});
});
describe("list", () => {
test("EXPECT help text to be sent", async () => {
let repliedWith: any;
// Assert
const interaction = {
isChatInputCommand: jest.fn().mockReturnValue(true),
guildId: "guildId",
options: {
getSubcommand: jest.fn().mockReturnValue("list"),
},
reply: jest.fn().mockImplementation((options: any) => {
repliedWith = options.embeds[0];
}),
} as unknown as CommandInteraction;
Server.FetchOneById = jest.fn().mockResolvedValue({});
const readFileSyncMock = jest.spyOn(fs, "readFileSync").mockReturnValue("Example config text");
// Act
const command = new Command();
await command.execute(interaction);
// Assert
expect(readFileSyncMock).toHaveBeenCalledTimes(1);
expect(readFileSyncMock).toHaveBeenCalledWith("/cwd/data/usage/config.txt");
expect(repliedWith).toBeDefined();
expect(repliedWith.data.color).toBe(EmbedColours.Ok);
expect(repliedWith.data.title).toBe("Config");
expect(repliedWith.data.description).toBe("Example config text");
});
});
describe("reset", () => {
test("EXPECT setting value to be set to default", async () => {
// Assert
const interaction = {
isChatInputCommand: jest.fn().mockReturnValue(true),
guildId: "guildId",
options: {
getSubcommand: jest.fn().mockReturnValue("reset"),
get: jest.fn().mockReturnValue({
value: "test.key",
}),
},
reply: jest.fn(),
} as unknown as CommandInteraction;
Server.FetchOneById = jest.fn().mockResolvedValue({
Settings: [
{
Key: "test.key",
Value: "12345",
},
],
});
Setting.Remove = jest.fn();
// Act
const command = new Command();
await command.execute(interaction);
// Assert
expect(interaction.options.get).toHaveBeenCalledTimes(1);
expect(interaction.options.get).toHaveBeenCalledWith("key");
expect(Server.FetchOneById).toHaveBeenCalledTimes(1);
expect(Server.FetchOneById).toHaveBeenCalledWith(Server, "guildId", [ "Settings" ]);
expect(Setting.Remove).toHaveBeenCalledTimes(1);
expect(Setting.Remove).toHaveBeenCalledWith(Setting, {
Key: "test.key",
Value: "12345",
});
expect(interaction.reply).toHaveBeenCalledTimes(1);
expect(interaction.reply).toHaveBeenCalledWith("The setting has been reset to the default.");
});
test("GIVEN key is null, EXPECT error", async () => {
// Assert
const interaction = {
isChatInputCommand: jest.fn().mockReturnValue(true),
guildId: "guildId",
options: {
getSubcommand: jest.fn().mockReturnValue("reset"),
get: jest.fn().mockReturnValue({
value: undefined,
}),
},
reply: jest.fn(),
} as unknown as CommandInteraction;
Server.FetchOneById = jest.fn().mockResolvedValue({
Settings: [
{
Key: "test.key",
Value: "12345",
},
],
});
Setting.Remove = jest.fn();
// Act
const command = new Command();
await command.execute(interaction);
// Assert
expect(interaction.reply).toHaveBeenCalledTimes(1);
expect(interaction.reply).toHaveBeenCalledWith("Fields are required.");
});
test("GIVEN key.value is undefined, EXPECT error", async () => {
// Assert
const interaction = {
isChatInputCommand: jest.fn().mockReturnValue(true),
guildId: "guildId",
options: {
getSubcommand: jest.fn().mockReturnValue("reset"),
get: jest.fn().mockReturnValue(null),
},
reply: jest.fn(),
} as unknown as CommandInteraction;
Server.FetchOneById = jest.fn().mockResolvedValue({
Settings: [
{
Key: "test.key",
Value: "12345",
},
],
});
Setting.Remove = jest.fn();
// Act
const command = new Command();
await command.execute(interaction);
// Assert
expect(interaction.reply).toHaveBeenCalledTimes(1);
expect(interaction.reply).toHaveBeenCalledWith("Fields are required.");
});
test("GIVEN setting is not found, EXPECT error", async () => {
// Assert
const interaction = {
isChatInputCommand: jest.fn().mockReturnValue(true),
guildId: "guildId",
options: {
getSubcommand: jest.fn().mockReturnValue("reset"),
get: jest.fn().mockReturnValue({
value: "test.key",
}),
},
reply: jest.fn(),
} as unknown as CommandInteraction;
Server.FetchOneById = jest.fn().mockResolvedValue({
Settings: [],
});
Setting.Remove = jest.fn();
// Act
const command = new Command();
await command.execute(interaction);
// Assert
expect(interaction.reply).toHaveBeenCalledTimes(1);
expect(interaction.reply).toHaveBeenCalledWith("Setting not found.");
});
});
describe("get", () => {
test("EXPECT setting value to be sent", async () => {
// Assert
const interaction = {
isChatInputCommand: jest.fn().mockReturnValue(true),
guildId: "guildId",
options: {
getSubcommand: jest.fn().mockReturnValue("get"),
get: jest.fn().mockReturnValue({
value: "test.key",
}),
},
reply: jest.fn(),
} as unknown as CommandInteraction;
Server.FetchOneById = jest.fn().mockResolvedValue({
Settings: [
{
Key: "test.key",
Value: "12345",
},
],
});
Setting.Remove = jest.fn();
// Act
const command = new Command();
await command.execute(interaction);
// Assert
expect(interaction.reply).toHaveBeenCalledTimes(1);
expect(interaction.reply).toHaveBeenCalledWith("`test.key`: `12345`");
expect(interaction.options.get).toHaveBeenCalledTimes(1);
expect(interaction.options.get).toHaveBeenCalledWith("key");
});
test("GIVEN key is null, EXPECT error", async () => {
// Assert
const interaction = {
isChatInputCommand: jest.fn().mockReturnValue(true),
guildId: "guildId",
options: {
getSubcommand: jest.fn().mockReturnValue("get"),
get: jest.fn().mockReturnValue(null),
},
reply: jest.fn(),
} as unknown as CommandInteraction;
Server.FetchOneById = jest.fn().mockResolvedValue({
Settings: [
{
Key: "test.key",
Value: "12345",
},
],
});
Setting.Remove = jest.fn();
// Act
const command = new Command();
await command.execute(interaction);
// Assert
expect(interaction.reply).toHaveBeenCalledTimes(1);
expect(interaction.reply).toHaveBeenCalledWith("Fields are required.");
});
test("GIVEN key.value is undefined, EXPECT error", async () => {
// Assert
const interaction = {
isChatInputCommand: jest.fn().mockReturnValue(true),
guildId: "guildId",
options: {
getSubcommand: jest.fn().mockReturnValue("get"),
get: jest.fn().mockReturnValue({
value: undefined,
}),
},
reply: jest.fn(),
} as unknown as CommandInteraction;
Server.FetchOneById = jest.fn().mockResolvedValue({
Settings: [
{
Key: "test.key",
Value: "12345",
},
],
});
Setting.Remove = jest.fn();
// Act
const command = new Command();
await command.execute(interaction);
// Assert
expect(interaction.reply).toHaveBeenCalledTimes(1);
expect(interaction.reply).toHaveBeenCalledWith("Fields are required.");
});
test("GIVEN setting can not be found AND a default value is found, EXPECT default to be shown", async () => {
// Assert
const interaction = {
isChatInputCommand: jest.fn().mockReturnValue(true),
guildId: "guildId",
options: {
getSubcommand: jest.fn().mockReturnValue("get"),
get: jest.fn().mockReturnValue({
value: "test.key",
}),
},
reply: jest.fn(),
} as unknown as CommandInteraction;
Server.FetchOneById = jest.fn().mockResolvedValue({
Settings: [],
});
DefaultValues.GetValue = jest.fn().mockReturnValue("Test");
Setting.Remove = jest.fn();
// Act
const command = new Command();
await command.execute(interaction);
// Assert
expect(interaction.reply).toHaveBeenCalledTimes(1);
expect(interaction.reply).toHaveBeenCalledWith("`test.key`: `Test` <DEFAULT>");
expect(DefaultValues.GetValue).toHaveBeenCalledTimes(1);
expect(DefaultValues.GetValue).toHaveBeenCalledWith("test.key");
});
test("GIVEN setting can not be found AND a default value is not found, EXPECT none to be shown", async () => {
// Assert
const interaction = {
isChatInputCommand: jest.fn().mockReturnValue(true),
guildId: "guildId",
options: {
getSubcommand: jest.fn().mockReturnValue("get"),
get: jest.fn().mockReturnValue({
value: "test.key",
}),
},
reply: jest.fn(),
} as unknown as CommandInteraction;
Server.FetchOneById = jest.fn().mockResolvedValue({
Settings: [],
});
DefaultValues.GetValue = jest.fn().mockReturnValue(undefined);
Setting.Remove = jest.fn();
// Act
const command = new Command();
await command.execute(interaction);
// Assert
expect(interaction.reply).toHaveBeenCalledTimes(1);
expect(interaction.reply).toHaveBeenCalledWith("`test.key`: <NONE>");
expect(DefaultValues.GetValue).toHaveBeenCalledTimes(1);
expect(DefaultValues.GetValue).toHaveBeenCalledWith("test.key");
});
});
describe("set", () => {
test("GIVEN setting already is set, EXPECT setting to be updated", async () => {
// Assert
const interaction = {
isChatInputCommand: jest.fn().mockReturnValue(true),
guildId: "guildId",
options: {
getSubcommand: jest.fn().mockReturnValue("set"),
get: jest.fn().mockReturnValueOnce({
value: "test.key",
}).mockReturnValue({
value: "54321",
}),
},
reply: jest.fn(),
} as unknown as CommandInteraction;
const server = {
Settings: [
{
Key: "test.key",
Value: "12345",
UpdateBasicDetails: jest.fn(),
Save: jest.fn(),
},
],
} as unknown as Server;
Server.FetchOneById = jest.fn().mockResolvedValue(server);
// Act
const command = new Command();
await command.execute(interaction);
// Assert
expect(interaction.reply).toHaveBeenCalledTimes(1);
expect(interaction.reply).toHaveBeenCalledWith("Setting has been set.");
expect(server.Settings[0].UpdateBasicDetails).toHaveBeenCalledTimes(1);
expect(server.Settings[0].UpdateBasicDetails).toHaveBeenCalledWith("test.key", "54321");
expect(server.Settings[0].Save).toHaveBeenCalledTimes(1);
expect(server.Settings[0].Save).toHaveBeenCalledWith(Setting, server.Settings[0]);
});
test("GIVEN setting is not set, EXPECT setting to be added", async () => {
let savedSetting: Setting | undefined;
// Assert
const interaction = {
isChatInputCommand: jest.fn().mockReturnValue(true),
guildId: "guildId",
options: {
getSubcommand: jest.fn().mockReturnValue("set"),
get: jest.fn().mockReturnValueOnce({
value: "test.key",
}).mockReturnValue({
value: "54321",
}),
},
reply: jest.fn(),
} as unknown as CommandInteraction;
const server = {
Settings: [],
AddSettingToServer: jest.fn(),
Save: jest.fn(),
} as unknown as Server;
Server.FetchOneById = jest.fn().mockResolvedValue(server);
Setting.prototype.Save = jest.fn().mockImplementation((_, setting: Setting) => {
savedSetting = setting;
});
// Act
const command = new Command();
await command.execute(interaction);
// Assert
expect(Setting.prototype.Save).toHaveBeenCalledTimes(1);
expect(Setting.prototype.Save).toHaveBeenCalledWith(Setting, expect.any(Setting));
expect(server.AddSettingToServer).toHaveBeenCalledTimes(1);
expect(server.AddSettingToServer).toHaveBeenCalledWith(expect.any(Setting));
expect(server.Save).toHaveBeenCalledTimes(1);
expect(server.Save).toHaveBeenCalledWith(Server, server);
expect(interaction.reply).toHaveBeenCalledTimes(1);
expect(interaction.reply).toHaveBeenCalledWith("Setting has been set.");
expect(savedSetting).toBeDefined();
expect(savedSetting!.Key).toBe("test.key");
expect(savedSetting!.Value).toBe("54321");
});
});

View file

@ -0,0 +1,285 @@
import { ChatInputCommandInteraction, PermissionsBitField, SlashCommandBuilder, SlashCommandStringOption, SlashCommandSubcommandBuilder } from "discord.js";
import Command from "../../src/commands/disable";
import SettingsHelper from "../../src/helpers/SettingsHelper";
describe('constructor', () => {
test('EXPECT properties to be set', () => {
const command = new Command();
expect(command.CommandBuilder).toBeDefined();
const commandBuilder = command.CommandBuilder as SlashCommandBuilder;
expect(commandBuilder.name).toBe("disable");
expect(commandBuilder.description).toBe("Disables a command");
expect(commandBuilder.default_member_permissions).toBe(PermissionsBitField.Flags.Administrator.toString());
expect(commandBuilder.options.length).toBe(2);
const commandBuilderAddSubcommand = commandBuilder.options[0] as SlashCommandSubcommandBuilder;
expect(commandBuilderAddSubcommand.name).toBe("add");
expect(commandBuilderAddSubcommand.description).toBe("Disables a command for the server");
expect(commandBuilderAddSubcommand.options.length).toBe(1);
const commandBuilderAddSubcommandNameOption = commandBuilderAddSubcommand.options[0] as SlashCommandStringOption;
expect(commandBuilderAddSubcommandNameOption.name).toBe("name");
expect(commandBuilderAddSubcommandNameOption.description).toBe("The name of the command");
expect(commandBuilderAddSubcommandNameOption.required).toBe(true);
const commandBuilderRemoveSubcommand = commandBuilder.options[1] as SlashCommandSubcommandBuilder;
expect(commandBuilderRemoveSubcommand.name).toBe("remove");
expect(commandBuilderRemoveSubcommand.description).toBe("Enables a command for the server");
expect(commandBuilderRemoveSubcommand.options.length).toBe(1);
const commandBuilderRemoveSubcommandNameOption = commandBuilderRemoveSubcommand.options[0] as SlashCommandStringOption;
expect(commandBuilderRemoveSubcommandNameOption.name).toBe("name");
expect(commandBuilderRemoveSubcommandNameOption.description).toBe("The name of the command");
expect(commandBuilderRemoveSubcommandNameOption.required).toBe(true);
});
});
describe('execute', () => {
test("GIVEN interaction is not a chat input command, EXPECT nothing to happen", async () => {
// Arrange
const interaction = {
isChatInputCommand: jest.fn().mockReturnValue(false),
reply: jest.fn(),
} as unknown as ChatInputCommandInteraction;
// Act
const command = new Command();
await command.execute(interaction);
// Assert
expect(interaction.isChatInputCommand).toHaveBeenCalledTimes(1);
expect(interaction.reply).not.toHaveBeenCalled();
});
test("GIVEN subcommand is invalid, EXPECT error", async () => {
// Arrange
const interaction = {
isChatInputCommand: jest.fn().mockReturnValue(true),
options: {
getSubcommand: jest.fn().mockReturnValue("invalid"),
},
reply: jest.fn(),
} as unknown as ChatInputCommandInteraction;
// Act
const command = new Command();
await command.execute(interaction);
// Assert
expect(interaction.options.getSubcommand).toHaveBeenCalledTimes(1);
expect(interaction.reply).toHaveBeenCalledTimes(1);
expect(interaction.reply).toHaveBeenCalledWith("Subcommand not found.");
});
});
describe('add', () => {
test("EXPECT command to be added to disabled list", async () => {
// Arrange
const interaction = {
guildId: "guildId",
isChatInputCommand: jest.fn().mockReturnValue(true),
options: {
getSubcommand: jest.fn().mockReturnValue("add"),
get: jest.fn().mockReturnValue({
value: "testCommand",
}),
},
reply: jest.fn(),
} as unknown as ChatInputCommandInteraction;
SettingsHelper.GetSetting = jest.fn().mockResolvedValue("otherCommand");
SettingsHelper.SetSetting = jest.fn();
// Act
const command = new Command();
await command.execute(interaction);
// Assert
expect(interaction.options.get).toHaveBeenCalledTimes(1);
expect(interaction.options.get).toHaveBeenCalledWith("name", true);
expect(SettingsHelper.GetSetting).toHaveBeenCalledTimes(1);
expect(SettingsHelper.GetSetting).toHaveBeenCalledWith("commands.disabled", "guildId");
expect(SettingsHelper.SetSetting).toHaveBeenCalledTimes(1);
expect(SettingsHelper.SetSetting).toHaveBeenCalledWith("commands.disabled", "guildId", "otherCommand,testCommand");
expect(interaction.reply).toHaveBeenCalledTimes(1);
expect(interaction.reply).toHaveBeenCalledWith("Disabled command testCommand");
});
test("GIVEN interaction.guildId is null, EXPECT nothing to happen", async () => {
// Arrange
const interaction = {
guildId: null,
isChatInputCommand: jest.fn().mockReturnValue(true),
options: {
getSubcommand: jest.fn().mockReturnValue("add"),
get: jest.fn().mockReturnValue({
value: "testCommand",
}),
},
reply: jest.fn(),
} as unknown as ChatInputCommandInteraction;
SettingsHelper.GetSetting = jest.fn().mockResolvedValue("otherCommand");
SettingsHelper.SetSetting = jest.fn();
// Act
const command = new Command();
await command.execute(interaction);
// Assert
expect(interaction.reply).not.toHaveBeenCalled();
expect(SettingsHelper.SetSetting).not.toHaveBeenCalled();
});
test("GIVEN disabledCommandsString is undefined, EXPECT empty disabledCommands array to be used", async () => {
// Arrange
const interaction = {
guildId: "guildId",
isChatInputCommand: jest.fn().mockReturnValue(true),
options: {
getSubcommand: jest.fn().mockReturnValue("add"),
get: jest.fn().mockReturnValue({
value: "testCommand",
}),
},
reply: jest.fn(),
} as unknown as ChatInputCommandInteraction;
SettingsHelper.GetSetting = jest.fn().mockResolvedValue(undefined);
SettingsHelper.SetSetting = jest.fn();
// Act
const command = new Command();
await command.execute(interaction);
// Assert
expect(SettingsHelper.SetSetting).toHaveBeenCalledTimes(1);
expect(SettingsHelper.SetSetting).toHaveBeenCalledWith("commands.disabled", "guildId", "testCommand");
});
});
describe("remove", () => {
test("EXPECT command to be removed from disabled list", async () => {
// Arrange
const interaction = {
guildId: "guildId",
isChatInputCommand: jest.fn().mockReturnValue(true),
options: {
getSubcommand: jest.fn().mockReturnValue("remove"),
get: jest.fn().mockReturnValue({
value: "testCommand",
}),
},
reply: jest.fn(),
} as unknown as ChatInputCommandInteraction;
SettingsHelper.GetSetting = jest.fn().mockResolvedValue("otherCommand,testCommand");
SettingsHelper.SetSetting = jest.fn();
// Act
const command = new Command();
await command.execute(interaction);
// Assert
expect(interaction.options.get).toHaveBeenCalledTimes(1);
expect(interaction.options.get).toHaveBeenCalledWith("name", true);
expect(SettingsHelper.GetSetting).toHaveBeenCalledTimes(1);
expect(SettingsHelper.GetSetting).toHaveBeenCalledWith("commands.disabled", "guildId");
expect(SettingsHelper.SetSetting).toHaveBeenCalledTimes(1);
expect(SettingsHelper.SetSetting).toHaveBeenCalledWith("commands.disabled", "guildId", "otherCommand");
expect(interaction.reply).toHaveBeenCalledTimes(1);
expect(interaction.reply).toHaveBeenCalledWith("Enabled command testCommand");
});
test("GIVEN interaction.guildId is null, EXPECT nothing to happen", async () => {
// Arrange
const interaction = {
guildId: null,
isChatInputCommand: jest.fn().mockReturnValue(true),
options: {
getSubcommand: jest.fn().mockReturnValue("remove"),
get: jest.fn().mockReturnValue({
value: "testCommand",
}),
},
reply: jest.fn(),
} as unknown as ChatInputCommandInteraction;
SettingsHelper.GetSetting = jest.fn().mockResolvedValue("otherCommand,testCommand");
SettingsHelper.SetSetting = jest.fn();
// Act
const command = new Command();
await command.execute(interaction);
// Assert
expect(interaction.reply).not.toHaveBeenCalled();
expect(SettingsHelper.SetSetting).not.toHaveBeenCalled();
});
test("GIVEN disabledCommandsString is undefined, EXPECT empty disabledCommands array to be used", async () => {
// Arrange
const interaction = {
guildId: "guildId",
isChatInputCommand: jest.fn().mockReturnValue(true),
options: {
getSubcommand: jest.fn().mockReturnValue("remove"),
get: jest.fn().mockReturnValue({
value: "testCommand",
}),
},
reply: jest.fn(),
} as unknown as ChatInputCommandInteraction;
SettingsHelper.GetSetting = jest.fn().mockResolvedValue(undefined);
SettingsHelper.SetSetting = jest.fn();
// Act
const command = new Command();
await command.execute(interaction);
// Assert
expect(SettingsHelper.SetSetting).toHaveBeenCalledTimes(1);
expect(SettingsHelper.SetSetting).toHaveBeenCalledWith("commands.disabled", "guildId", "");
});
test("GIVEN instance of commandName is not found in disabledCommands array, EXPECT it not to try to remove it", async () => {
// Arrange
const interaction = {
guildId: "guildId",
isChatInputCommand: jest.fn().mockReturnValue(true),
options: {
getSubcommand: jest.fn().mockReturnValue("remove"),
get: jest.fn().mockReturnValue({
value: "testCommand",
}),
},
reply: jest.fn(),
} as unknown as ChatInputCommandInteraction;
SettingsHelper.GetSetting = jest.fn().mockResolvedValue("otherCommand");
SettingsHelper.SetSetting = jest.fn();
// Act
const command = new Command();
await command.execute(interaction);
// Assert
expect(SettingsHelper.SetSetting).toHaveBeenCalledTimes(1);
expect(SettingsHelper.SetSetting).toHaveBeenCalledWith("commands.disabled", "guildId", "otherCommand");
});
});

View file

@ -1,136 +0,0 @@
import { Message } from "discord.js";
import Evaluate from "../../src/commands/eval";
import { ICommandContext } from "../../src/contracts/ICommandContext";
beforeEach(() => {
process.env = {};
});
describe('Constructor', () => {
test('Expect values to be set', () => {
const evaluate = new Evaluate();
expect(evaluate._category).toBe('Owner');
});
});
describe('Execute', () => {
test('Given user has permission, expect eval statement ran', () => {
process.env = {
BOT_OWNERID: 'OWNERID'
};
console.log = jest.fn();
global.eval = jest.fn()
.mockReturnValue('General Kenobi');
const messageChannelSend = jest.fn();
const message = {
author: {
id: 'OWNERID'
},
channel: {
send: messageChannelSend
}
} as unknown as Message;
const context: ICommandContext = {
name: 'eval',
args: ['echo', 'Hello', 'there'],
message: message
};
const evaluate = new Evaluate();
const result = evaluate.execute(context);
expect(console.log).toBeCalledWith('Eval Statement: echo Hello there');
expect(global.eval).toBeCalledWith('echo Hello there');
expect(result.embeds.length).toBe(1);
// PublicEmbed
const publicEmbed = result.embeds[0];
expect(publicEmbed.title).toBe('');
expect(publicEmbed.description).toBe('General Kenobi');
});
test('Given user does not have permission, expect nothing to occur', () => {
process.env = {
BOT_OWNERID: 'DIFFERENT'
};
console.log = jest.fn();
global.eval = jest.fn()
.mockReturnValue('General Kenobi');
const messageChannelSend = jest.fn();
const message = {
author: {
id: 'OWNERID'
},
channel: {
send: messageChannelSend
}
} as unknown as Message;
const context: ICommandContext = {
name: 'eval',
args: ['echo', 'Hello', 'there'],
message: message
};
const evaluate = new Evaluate();
const result = evaluate.execute(context);
expect(console.log).not.toBeCalled();
expect(global.eval).not.toBeCalled();
expect(result.embeds.length).toBe(0);
});
test('Given eval failed, expect error embed to be sent', () => {
process.env = {
BOT_OWNERID: 'OWNERID'
};
console.log = jest.fn();
global.eval = jest.fn()
.mockImplementation(() => {
throw new Error('Error message');
});
const messageChannelSend = jest.fn();
const message = {
author: {
id: 'OWNERID'
},
channel: {
send: messageChannelSend
}
} as unknown as Message;
const context: ICommandContext = {
name: 'eval',
args: ['echo', 'Hello', 'there'],
message: message
};
const evaluate = new Evaluate();
const result = evaluate.execute(context);
expect(console.log).toBeCalledWith('Eval Statement: echo Hello there');
expect(global.eval).toBeCalledWith('echo Hello there');
expect(result.embeds.length).toBe(1);
// ErrorEmbed
const errorEmbed = result.embeds[0];
expect(errorEmbed.title).toBeNull();
expect(errorEmbed.description).toBe('Error: Error message');
});
});

View file

@ -1,267 +0,0 @@
import Help, { ICommandData } from "../../src/commands/help";
import { Message } from "discord.js";
import { ICommandContext } from "../../src/contracts/ICommandContext";
const oldCwd = process.cwd();
describe('Constructor', () => {
test('Expect properties to be set', () => {
const help = new Help();
expect(help._category).toBe('General');
});
});
describe('Execute', () => {
test('Given no arguments were given, expect SendAll to be executed', () => {
const message = {} as unknown as Message;
const context: ICommandContext = {
name: 'help',
args: [],
message: message
};
const help = new Help();
help.SendAll = jest.fn();
help.SendSingle = jest.fn();
help.execute(context);
expect(help.SendAll).toBeCalled();
expect(help.SendSingle).not.toBeCalled();
});
test('Given an argument was given, expect SendSingle to be executed', () => {
const message = {} as unknown as Message;
const context: ICommandContext = {
name: 'help',
args: ['about'],
message: message
};
const help = new Help();
help.SendAll = jest.fn();
help.SendSingle = jest.fn();
help.execute(context);
expect(help.SendAll).not.toBeCalled();
expect(help.SendSingle).toBeCalled();
});
});
describe('SendAll', () => {
test('Expect embed with all commands to be sent', () => {
const messageChannelSend = jest.fn();
const message = {
channel: {
send: messageChannelSend
}
} as unknown as Message;
const context: ICommandContext = {
name: 'help',
args: [],
message: message
};
const help = new Help();
const commandData0: ICommandData = {
Exists: true,
Name: 'about',
Category: 'general',
Roles: []
};
const commandData1: ICommandData = {
Exists: true,
Name: 'role',
Category: 'general',
Roles: []
};
help.GetAllCommandData = jest.fn()
.mockReturnValue([commandData0, commandData1]);
const result = help.SendAll(context);
expect(help.GetAllCommandData).toBeCalled();
expect(messageChannelSend).toBeCalled();
expect(result.embeds.length).toBe(1);
// PublicEmbed
const publicEmbed = result.embeds[0];
expect(publicEmbed.fields.length).toBe(1);
// PublicEmbed -> GeneralCategory Field
const publicEmbedFieldGeneral = publicEmbed.fields[0];
expect(publicEmbedFieldGeneral.name).toBe('General');
expect(publicEmbedFieldGeneral.value).toBe('about, role');
});
});
describe('SendSingle', () => {
test('Given command exists, expect embed to be sent with command fields', () => {
const messageChannelSend = jest.fn();
const message = {
channel: {
send: messageChannelSend
}
} as unknown as Message;
const context: ICommandContext = {
name: 'help',
args: ['about'],
message: message
};
const commandData: ICommandData = {
Exists: true,
Name: 'about',
Category: 'general',
Roles: ['role1', 'role2']
};
const help = new Help();
help.GetCommandData = jest.fn()
.mockReturnValue(commandData);
const result = help.SendSingle(context);
expect(help.GetCommandData).toBeCalledWith('about');
expect(messageChannelSend).toBeCalled();
expect(result.embeds.length).toBe(1);
// PublicEmbed
const publicEmbed = result.embeds[0];
expect(publicEmbed.title).toBe('About');
expect(publicEmbed.description).toBe('');
expect(publicEmbed.fields.length).toBe(2);
// PublicEmbed -> Category Field
const fieldCategory = publicEmbed.fields[0];
expect(fieldCategory.name).toBe('Category');
expect(fieldCategory.value).toBe('General');
// PublicEmbed -> RequiredRoles Field
const fieldRoles = publicEmbed.fields[1];
expect(fieldRoles.name).toBe('Required Roles');
expect(fieldRoles.value).toBe('Role1, Role2');
});
test('Given command does not exist, expect error embed to be sent', () => {
const messageChannelSend = jest.fn();
const message = {
channel: {
send: messageChannelSend
}
} as unknown as Message;
const context: ICommandContext = {
name: 'help',
args: ['about'],
message: message
};
const commandData: ICommandData = {
Exists: false
};
const help = new Help();
help.GetCommandData = jest.fn()
.mockReturnValue(commandData);
const result = help.SendSingle(context);
expect(help.GetCommandData).toBeCalledWith('about');
expect(messageChannelSend).toBeCalled();
expect(result.embeds.length).toBe(1);
// ErrorEmbed
const errorEmbed = result.embeds[0];
expect(errorEmbed.description).toBe('Command does not exist');
});
});
describe('GetAllCommandData', () => {
test('Expect array of command data to be returned', () => {
process.env = {
FOLDERS_COMMANDS: "commands"
};
process.cwd = jest.fn()
.mockReturnValue(`${oldCwd}/tests/_mocks`);
const help = new Help();
const result = help.GetAllCommandData();
expect(result.length).toBe(1);
// Mock Command
const mockCommand = result[0];
expect(mockCommand.Exists).toBeTruthy();
expect(mockCommand.Name).toBe("mockCmd");
expect(mockCommand.Category).toBe("General");
expect(mockCommand.Roles!.length).toBe(1);
expect(mockCommand.Roles![0]).toBe("Moderator");
});
});
describe('GetCommandData', () => {
test('Given command exists, expect data to be returned', () => {
process.env = {
FOLDERS_COMMANDS: "commands"
};
process.cwd = jest.fn()
.mockReturnValue(`${oldCwd}/tests/_mocks`);
const help = new Help();
const result = help.GetCommandData('mockCmd');
expect(result.Exists).toBeTruthy();
expect(result.Name).toBe("mockCmd");
expect(result.Category).toBe("General");
expect(result.Roles!.length).toBe(1);
expect(result.Roles![0]).toBe("Moderator");
});
test('Given command does not exist, expect exists false to be returned', () => {
process.env = {
FOLDERS_COMMANDS: "commands"
};
const oldCwd = process.cwd();
process.cwd = jest.fn()
.mockReturnValue(`${oldCwd}/tests/_mocks`);
const help = new Help();
const result = help.GetCommandData('none');
expect(result.Exists).toBeFalsy();
});
});

View file

@ -0,0 +1,79 @@
import { CommandInteraction, PermissionsBitField, SlashCommandBuilder } from "discord.js";
import Command from "../../src/commands/ignore";
import IgnoredChannel from "../../src/database/entities/IgnoredChannel";
describe('constructor', () => {
test("EXPECT properties to be set", () => {
const command = new Command();
expect(command.CommandBuilder).toBeDefined();
const commandBuilder = command.CommandBuilder as SlashCommandBuilder;
expect(commandBuilder.name).toBe("ignore");
expect(commandBuilder.description).toBe("Ignore events in this channel");
expect(commandBuilder.default_member_permissions).toBe(PermissionsBitField.Flags.Administrator.toString());
});
});
describe("execute", () => {
test("GIVEN channel is currently ignored, EXPECT channel to be removed from list", async () => {
// Arrange
const interaction = {
guildId: "guildId",
channelId: "channelId",
reply: jest.fn(),
} as unknown as CommandInteraction;
IgnoredChannel.IsChannelIgnored = jest.fn().mockResolvedValue(true);
IgnoredChannel.FetchOneById = jest.fn().mockResolvedValue({});
IgnoredChannel.Remove = jest.fn();
// Act
const command = new Command();
await command.execute(interaction);
// Assert
expect(IgnoredChannel.IsChannelIgnored).toHaveBeenCalledTimes(1);
expect(IgnoredChannel.IsChannelIgnored).toHaveBeenCalledWith("channelId");
expect(IgnoredChannel.FetchOneById).toHaveBeenCalledTimes(1);
expect(IgnoredChannel.FetchOneById).toHaveBeenCalledWith(IgnoredChannel, "channelId");
expect(IgnoredChannel.Remove).toHaveBeenCalledTimes(1);
expect(IgnoredChannel.Remove).toHaveBeenCalledWith(IgnoredChannel, {});
expect(interaction.reply).toHaveBeenCalledTimes(1);
expect(interaction.reply).toHaveBeenCalledWith("This channel will start being logged again.");
});
test("GIVEN channel is not currently ignored, EXPECT channel to be added to list", async () => {
let savedChannel: IgnoredChannel | undefined;
// Arrange
const interaction = {
guildId: "guildId",
channelId: "channelId",
reply: jest.fn(),
} as unknown as CommandInteraction;
IgnoredChannel.IsChannelIgnored = jest.fn().mockResolvedValue(false);
IgnoredChannel.prototype.Save = jest.fn().mockImplementation((_, channel: IgnoredChannel) => {
savedChannel = channel;
});
// Act
const command = new Command();
await command.execute(interaction);
// Assert
expect(IgnoredChannel.prototype.Save).toHaveBeenCalledTimes(1);
expect(IgnoredChannel.prototype.Save).toHaveBeenCalledWith(IgnoredChannel, expect.any(IgnoredChannel));
expect(interaction.reply).toHaveBeenCalledTimes(1);
expect(interaction.reply).toHaveBeenCalledWith("This channel will now be ignored from logging.");
expect(savedChannel).toBeDefined();
expect(savedChannel!.Id).toBe("channelId");
});
});

File diff suppressed because it is too large Load diff

View file

@ -1,815 +0,0 @@
import { mock } from "jest-mock-extended";
import { GuildMember, Message, Role, TextChannel, User } from "discord.js";
import Mute from "../../src/commands/mute";
import { ICommandContext } from "../../src/contracts/ICommandContext";
beforeEach(() => {
process.env = {};
});
describe('Constructor', () => {
test('Expect properties to be set', () => {
process.env = {
ROLES_MODERATOR: 'Moderator'
};
const mute = new Mute();
expect(mute._category).toBe("Moderation");
expect(mute._roles.length).toBe(1);
expect(mute._roles[0]).toBe('Moderator');
});
});
describe('Execute', () => {
test('Given user has permission, expect user to be given muted role', async () => {
process.env = {
CHANNELS_LOGS_MOD: 'mod-logs',
ROLES_MUTED: 'Muted'
};
const user = {
displayAvatarURL: jest.fn(),
tag: 'USERTAG'
} as unknown as User;
const messageAuthor = {
tag: 'AUTHORTAG'
} as unknown as User;
const member = {
manageable: true,
roles: {
add: jest.fn()
}
} as unknown as GuildMember;
const role = {
name: 'Muted'
} as unknown as Role;
const logChannel = {
name: 'mod-logs',
send: jest.fn()
} as unknown as TextChannel;
const messageMentionsUsersFirst = jest.fn()
.mockReturnValue(user);
const messageGuildMember = jest.fn()
.mockReturnValue(member);
const messageGuildRolesCacheFind = jest.fn()
.mockImplementation((callback): Role | undefined => {
const result = callback(role);
if (!result) {
return undefined;
}
return role;
});
const messageChannelSend = jest.fn();
const messageGuildChannelsCacheFind = jest.fn()
.mockImplementation((callback): TextChannel | undefined => {
const result = callback(logChannel);
if (!result) {
return undefined;
}
return logChannel;
});
const message = {
mentions: {
users: {
first: messageMentionsUsersFirst
}
},
guild: {
member: messageGuildMember,
available: true,
roles: {
cache: {
find: messageGuildRolesCacheFind
}
},
channels: {
cache: {
find: messageGuildChannelsCacheFind
}
}
},
channel: {
send: messageChannelSend
},
author: messageAuthor
} as unknown as Message;
const context: ICommandContext = {
name: 'mute',
args: ['USER', 'Test', 'Reason'],
message: message
};
const mute = new Mute();
const result = await mute.execute(context);
expect(messageMentionsUsersFirst).toBeCalledTimes(1);
expect(messageGuildMember).toBeCalledWith(user);
expect(messageGuildRolesCacheFind).toBeCalledTimes(1);
expect(messageGuildChannelsCacheFind).toBeCalledTimes(1);
expect(messageChannelSend).toBeCalledTimes(1);
expect(member.roles.add).toBeCalledWith(role, 'Moderator: AUTHORTAG, Reason: Test Reason');
expect(result.embeds.length).toBe(2);
// Log Embed
const logEmbed = result.embeds[0];
expect(logEmbed.title).toBe('Member Muted');
expect(logEmbed.fields.length).toBe(3);
// Log Embed -> User Field
const logEmbedUserField = logEmbed.fields[0];
expect(logEmbedUserField.name).toBe('User');
expect(logEmbedUserField.value).toBe('[object Object] `USERTAG`');
expect(logEmbedUserField.inline).toBeTruthy();
// Log Embed -> Moderator Field
const logEmbedModeratorField = logEmbed.fields[1];
expect(logEmbedModeratorField.name).toBe('Moderator');
expect(logEmbedModeratorField.value).toBe('[object Object] `AUTHORTAG`');
// Log Embed -> Reason Field
const logEmbedFieldReason = logEmbed.fields[2];
expect(logEmbedFieldReason.name).toBe('Reason');
expect(logEmbedFieldReason.value).toBe('Test Reason');
// Public Embed
const publicEmbed = result.embeds[1];
expect(publicEmbed.title).toBe('');
expect(publicEmbed.description).toBe('[object Object] has been muted');
});
test('Given moderator did not supply a reason, expect default reason used', async () => {
process.env = {
CHANNELS_LOGS_MOD: 'mod-logs',
ROLES_MUTED: 'Muted'
};
const user = {
displayAvatarURL: jest.fn(),
tag: 'USERTAG'
} as unknown as User;
const messageAuthor = {
tag: 'AUTHORTAG'
} as unknown as User;
const member = {
manageable: true,
roles: {
add: jest.fn()
}
} as unknown as GuildMember;
const role = {
name: 'Muted'
} as unknown as Role;
const logChannel = {
name: 'mod-logs',
send: jest.fn()
} as unknown as TextChannel;
const messageMentionsUsersFirst = jest.fn()
.mockReturnValue(user);
const messageGuildMember = jest.fn()
.mockReturnValue(member);
const messageGuildRolesCacheFind = jest.fn()
.mockImplementation((callback): Role | undefined => {
const result = callback(role);
if (!result) {
return undefined;
}
return role;
});
const messageChannelSend = jest.fn();
const messageGuildChannelsCacheFind = jest.fn()
.mockImplementation((callback): TextChannel | undefined => {
const result = callback(logChannel);
if (!result) {
return undefined;
}
return logChannel;
});
const message = {
mentions: {
users: {
first: messageMentionsUsersFirst
}
},
guild: {
member: messageGuildMember,
available: true,
roles: {
cache: {
find: messageGuildRolesCacheFind
}
},
channels: {
cache: {
find: messageGuildChannelsCacheFind
}
}
},
channel: {
send: messageChannelSend
},
author: messageAuthor
} as unknown as Message;
const context: ICommandContext = {
name: 'mute',
args: ['USER'],
message: message
};
const mute = new Mute();
const result = await mute.execute(context);
expect(messageMentionsUsersFirst).toBeCalledTimes(1);
expect(messageGuildMember).toBeCalledWith(user);
expect(messageGuildRolesCacheFind).toBeCalledTimes(1);
expect(messageGuildChannelsCacheFind).toBeCalledTimes(1);
expect(messageChannelSend).toBeCalledTimes(1);
expect(member.roles.add).toBeCalledWith(role, 'Moderator: AUTHORTAG, Reason: *none*');
expect(result.embeds.length).toBe(2);
// Log Embed
const logEmbed = result.embeds[0];
expect(logEmbed.title).toBe('Member Muted');
expect(logEmbed.fields.length).toBe(3);
// Log Embed -> User Field
const logEmbedUserField = logEmbed.fields[0];
expect(logEmbedUserField.name).toBe('User');
expect(logEmbedUserField.value).toBe('[object Object] `USERTAG`');
expect(logEmbedUserField.inline).toBeTruthy();
// Log Embed -> Moderator Field
const logEmbedModeratorField = logEmbed.fields[1];
expect(logEmbedModeratorField.name).toBe('Moderator');
expect(logEmbedModeratorField.value).toBe('[object Object] `AUTHORTAG`');
// Log Embed -> Reason Field
const logEmbedFieldReason = logEmbed.fields[2];
expect(logEmbedFieldReason.name).toBe('Reason');
expect(logEmbedFieldReason.value).toBe('*none*');
// Public Embed
const publicEmbed = result.embeds[1];
expect(publicEmbed.title).toBe('');
expect(publicEmbed.description).toBe('[object Object] has been muted');
});
test('Given user did not mention a user, expect user not to exist', async () => {
process.env = {
CHANNELS_LOGS_MOD: 'mod-logs',
ROLES_MUTED: 'Muted'
};
const user = {
displayAvatarURL: jest.fn(),
tag: 'USERTAG'
} as unknown as User;
const messageAuthor = {
tag: 'AUTHORTAG'
} as unknown as User;
const member = {
manageable: true,
roles: {
add: jest.fn()
}
} as unknown as GuildMember;
const role = {
name: 'Muted'
} as unknown as Role;
const logChannel = {
name: 'mod-logs',
send: jest.fn()
} as unknown as TextChannel;
const messageMentionsUsersFirst = jest.fn()
.mockReturnValue(null);
const messageGuildMember = jest.fn()
.mockReturnValue(member);
const messageGuildRolesCacheFind = jest.fn()
.mockImplementation((callback): Role | undefined => {
const result = callback(role);
if (!result) {
return undefined;
}
return role;
});
const messageChannelSend = jest.fn();
const messageGuildChannelsCacheFind = jest.fn()
.mockImplementation((callback): TextChannel | undefined => {
const result = callback(logChannel);
if (!result) {
return undefined;
}
return logChannel;
});
const message = {
mentions: {
users: {
first: messageMentionsUsersFirst
}
},
guild: {
member: messageGuildMember,
available: true,
roles: {
cache: {
find: messageGuildRolesCacheFind
}
},
channels: {
cache: {
find: messageGuildChannelsCacheFind
}
}
},
channel: {
send: messageChannelSend
},
author: messageAuthor
} as unknown as Message;
const context: ICommandContext = {
name: 'mute',
args: ['USER', 'Test', 'Reason'],
message: message
};
const mute = new Mute();
const result = await mute.execute(context);
expect(messageMentionsUsersFirst).toBeCalledTimes(1);
expect(messageGuildMember).not.toBeCalled();
expect(messageGuildRolesCacheFind).not.toBeCalled();
expect(messageGuildChannelsCacheFind).not.toBeCalled();
expect(messageChannelSend).toBeCalledTimes(1);
expect(result.embeds.length).toBe(1);
// Error Embed
const errorEmbed = result.embeds[0];
expect(errorEmbed.description).toBe('User does not exist');
});
test('Given member can not be found from user, expect user to not be in server', async () => {
process.env = {
CHANNELS_LOGS_MOD: 'mod-logs',
ROLES_MUTED: 'Muted'
};
const user = {
displayAvatarURL: jest.fn(),
tag: 'USERTAG'
} as unknown as User;
const messageAuthor = {
tag: 'AUTHORTAG'
} as unknown as User;
const member = {
manageable: true,
roles: {
add: jest.fn()
}
} as unknown as GuildMember;
const role = {
name: 'Muted'
} as unknown as Role;
const logChannel = {
name: 'mod-logs',
send: jest.fn()
} as unknown as TextChannel;
const messageMentionsUsersFirst = jest.fn()
.mockReturnValue(user);
const messageGuildMember = jest.fn()
.mockReturnValue(null);
const messageGuildRolesCacheFind = jest.fn()
.mockImplementation((callback): Role | undefined => {
const result = callback(role);
if (!result) {
return undefined;
}
return role;
});
const messageChannelSend = jest.fn();
const messageGuildChannelsCacheFind = jest.fn()
.mockImplementation((callback): TextChannel | undefined => {
const result = callback(logChannel);
if (!result) {
return undefined;
}
return logChannel;
});
const message = {
mentions: {
users: {
first: messageMentionsUsersFirst
}
},
guild: {
member: messageGuildMember,
available: true,
roles: {
cache: {
find: messageGuildRolesCacheFind
}
},
channels: {
cache: {
find: messageGuildChannelsCacheFind
}
}
},
channel: {
send: messageChannelSend
},
author: messageAuthor
} as unknown as Message;
const context: ICommandContext = {
name: 'mute',
args: ['USER', 'Test', 'Reason'],
message: message
};
const mute = new Mute();
const result = await mute.execute(context);
expect(messageMentionsUsersFirst).toBeCalledTimes(1);
expect(messageGuildMember).toBeCalledWith(user);
expect(messageGuildRolesCacheFind).not.toBeCalled();
expect(messageGuildChannelsCacheFind).not.toBeCalled();
expect(messageChannelSend).toBeCalledTimes(1);
expect(result.embeds.length).toBe(1);
// Error Embed
const errorEmbed = result.embeds[0];
expect(errorEmbed.description).toBe('User is not in this server');
});
test('Given guild is unavailable, expect execution to stop', async () => {
process.env = {
CHANNELS_LOGS_MOD: 'mod-logs',
ROLES_MUTED: 'Muted'
};
const user = {
displayAvatarURL: jest.fn(),
tag: 'USERTAG'
} as unknown as User;
const messageAuthor = {
tag: 'AUTHORTAG'
} as unknown as User;
const member = {
manageable: true,
roles: {
add: jest.fn()
}
} as unknown as GuildMember;
const role = {
name: 'Muted'
} as unknown as Role;
const logChannel = {
name: 'mod-logs',
send: jest.fn()
} as unknown as TextChannel;
const messageMentionsUsersFirst = jest.fn()
.mockReturnValue(user);
const messageGuildMember = jest.fn()
.mockReturnValue(member);
const messageGuildRolesCacheFind = jest.fn()
.mockImplementation((callback): Role | undefined => {
const result = callback(role);
if (!result) {
return undefined;
}
return role;
});
const messageChannelSend = jest.fn();
const messageGuildChannelsCacheFind = jest.fn()
.mockImplementation((callback): TextChannel | undefined => {
const result = callback(logChannel);
if (!result) {
return undefined;
}
return logChannel;
});
const message = {
mentions: {
users: {
first: messageMentionsUsersFirst
}
},
guild: {
member: messageGuildMember,
available: false,
roles: {
cache: {
find: messageGuildRolesCacheFind
}
},
channels: {
cache: {
find: messageGuildChannelsCacheFind
}
}
},
channel: {
send: messageChannelSend
},
author: messageAuthor
} as unknown as Message;
const context: ICommandContext = {
name: 'mute',
args: ['USER', 'Test', 'Reason'],
message: message
};
const mute = new Mute();
const result = await mute.execute(context);
expect(messageMentionsUsersFirst).toBeCalledTimes(1);
expect(messageGuildMember).toBeCalledWith(user);
expect(messageGuildRolesCacheFind).not.toBeCalled();
expect(messageGuildChannelsCacheFind).not.toBeCalled();
expect(messageChannelSend).not.toBeCalled();
expect(result.embeds.length).toBe(0);
});
test('Given client can not manage user, expect insufficient permissions', async () => {
process.env = {
CHANNELS_LOGS_MOD: 'mod-logs',
ROLES_MUTED: 'Muted'
};
const user = {
displayAvatarURL: jest.fn(),
tag: 'USERTAG'
} as unknown as User;
const messageAuthor = {
tag: 'AUTHORTAG'
} as unknown as User;
const member = {
manageable: false,
roles: {
add: jest.fn()
}
} as unknown as GuildMember;
const role = {
name: 'Muted'
} as unknown as Role;
const logChannel = {
name: 'mod-logs',
send: jest.fn()
} as unknown as TextChannel;
const messageMentionsUsersFirst = jest.fn()
.mockReturnValue(user);
const messageGuildMember = jest.fn()
.mockReturnValue(member);
const messageGuildRolesCacheFind = jest.fn()
.mockImplementation((callback): Role | undefined => {
const result = callback(role);
if (!result) {
return undefined;
}
return role;
});
const messageChannelSend = jest.fn();
const messageGuildChannelsCacheFind = jest.fn()
.mockImplementation((callback): TextChannel | undefined => {
const result = callback(logChannel);
if (!result) {
return undefined;
}
return logChannel;
});
const message = {
mentions: {
users: {
first: messageMentionsUsersFirst
}
},
guild: {
member: messageGuildMember,
available: true,
roles: {
cache: {
find: messageGuildRolesCacheFind
}
},
channels: {
cache: {
find: messageGuildChannelsCacheFind
}
}
},
channel: {
send: messageChannelSend
},
author: messageAuthor
} as unknown as Message;
const context: ICommandContext = {
name: 'mute',
args: ['USER', 'Test', 'Reason'],
message: message
};
const mute = new Mute();
const result = await mute.execute(context);
expect(messageMentionsUsersFirst).toBeCalledTimes(1);
expect(messageGuildMember).toBeCalledWith(user);
expect(messageGuildRolesCacheFind).not.toBeCalled();
expect(messageGuildChannelsCacheFind).not.toBeCalled();
expect(messageChannelSend).toBeCalledTimes(1);
expect(result.embeds.length).toBe(1);
// Error Embed
const errorEmbed = result.embeds[0];
expect(errorEmbed.description).toBe('Unable to do this action, am I missing permissions?');
});
test('Given muted role can not be found, expect role not found', async () => {
process.env = {
CHANNELS_LOGS_MOD: 'mod-logs',
ROLES_MUTED: 'Muted'
};
const user = {
displayAvatarURL: jest.fn(),
tag: 'USERTAG'
} as unknown as User;
const messageAuthor = {
tag: 'AUTHORTAG'
} as unknown as User;
const member = {
manageable: true,
roles: {
add: jest.fn()
}
} as unknown as GuildMember;
const role = {
name: 'Muted'
} as unknown as Role;
const logChannel = {
name: 'mod-logs',
send: jest.fn()
} as unknown as TextChannel;
const messageMentionsUsersFirst = jest.fn()
.mockReturnValue(user);
const messageGuildMember = jest.fn()
.mockReturnValue(member);
const messageGuildRolesCacheFind = jest.fn()
.mockReturnValue(undefined);
const messageChannelSend = jest.fn();
const messageGuildChannelsCacheFind = jest.fn()
.mockImplementation((callback): TextChannel | undefined => {
const result = callback(logChannel);
if (!result) {
return undefined;
}
return logChannel;
});
const message = {
mentions: {
users: {
first: messageMentionsUsersFirst
}
},
guild: {
member: messageGuildMember,
available: true,
roles: {
cache: {
find: messageGuildRolesCacheFind
}
},
channels: {
cache: {
find: messageGuildChannelsCacheFind
}
}
},
channel: {
send: messageChannelSend
},
author: messageAuthor
} as unknown as Message;
const context: ICommandContext = {
name: 'mute',
args: ['USER', 'Test', 'Reason'],
message: message
};
const mute = new Mute();
const result = await mute.execute(context);
expect(messageMentionsUsersFirst).toBeCalledTimes(1);
expect(messageGuildMember).toBeCalledWith(user);
expect(messageGuildRolesCacheFind).toBeCalledTimes(1);
expect(messageGuildChannelsCacheFind).not.toBeCalled();
expect(messageChannelSend).toBeCalledTimes(1);
expect(result.embeds.length).toBe(1);
// Error Embed
const errorEmbed = result.embeds[0];
expect(errorEmbed.description).toBe('Unable to find role');
});
});

View file

@ -1,262 +1,242 @@
import { Message, MessageEmbed } from "discord.js";
import Poll from "../../src/commands/poll";
import { ICommandContext } from "../../src/contracts/ICommandContext";
import { CommandInteraction, EmbedBuilder, InteractionResponse, Message, SlashCommandBuilder, SlashCommandStringOption } from "discord.js";
import Command from "../../src/commands/poll";
import EmbedColours from "../../src/constants/EmbedColours";
describe('Constructor', () => {
test('Expect properties to be set', () => {
const poll = new Poll();
test('EXPECT properties to be set', () => {
const command = new Command();
expect(poll._category).toBe('General');
expect(command.CommandBuilder).toBeDefined();
const commandBuilder = command.CommandBuilder as SlashCommandBuilder;
expect(commandBuilder.name).toBe("poll");
expect(commandBuilder.description).toBe("Run a poll, automatically adding reaction emojis as options");
expect(commandBuilder.options.length).toBe(6);
const commandBuilderTitleOption = commandBuilder.options[0] as SlashCommandStringOption;
expect(commandBuilderTitleOption.name).toBe("title");
expect(commandBuilderTitleOption.description).toBe("Title of the poll");
expect(commandBuilderTitleOption.required).toBe(true);
const commandBuilderOption1Option = commandBuilder.options[1] as SlashCommandStringOption;
expect(commandBuilderOption1Option.name).toBe("option1");
expect(commandBuilderOption1Option.description).toBe("Option 1");
expect(commandBuilderOption1Option.required).toBe(true);
const commandBuilderOption2Option = commandBuilder.options[2] as SlashCommandStringOption;
expect(commandBuilderOption2Option.name).toBe("option2");
expect(commandBuilderOption2Option.description).toBe("Option 2");
expect(commandBuilderOption2Option.required).toBe(true);
const commandBuilderOption3Option = commandBuilder.options[3] as SlashCommandStringOption;
expect(commandBuilderOption3Option.name).toBe("option3");
expect(commandBuilderOption3Option.description).toBe("Option 3");
const commandBuilderOption4Option = commandBuilder.options[4] as SlashCommandStringOption;
expect(commandBuilderOption4Option.name).toBe("option4");
expect(commandBuilderOption4Option.description).toBe("Option 4");
const commandBuilderOption5Option = commandBuilder.options[5] as SlashCommandStringOption;
expect(commandBuilderOption5Option.name).toBe("option5");
expect(commandBuilderOption5Option.description).toBe("Option 5");
});
});
describe('Execute', () => {
test('Given input is valid, expect poll to be generated', async () => {
const returnMessageReact = jest.fn();
const returnMessage = {
react: returnMessageReact
} as unknown as Message;
const messageChannelSend = jest.fn()
.mockReturnValue(returnMessage);
const messageDelete = jest.fn();
test("EXPECT a poll to be created", async () => {
let sentEmbed: EmbedBuilder | undefined;
// Arrange
const message = {
channel: {
send: messageChannelSend
react: jest.fn(),
} as unknown as Message<boolean>;
const response = {
fetch: jest.fn().mockResolvedValue(message),
} as unknown as InteractionResponse<boolean>;
const interaction = {
options: {
get: jest.fn().mockReturnValueOnce({ value: "Title" })
.mockReturnValueOnce({ value: "Option 1" })
.mockReturnValueOnce({ value: "Option 2" })
.mockReturnValueOnce({ value: "Option 3" })
.mockReturnValueOnce({ value: "Option 4" })
.mockReturnValue({ value: "Option 5" }),
},
delete: messageDelete,
deletable: true
} as unknown as Message;
reply: jest.fn().mockImplementation((options: any) => {
sentEmbed = options.embeds[0];
const context: ICommandContext = {
name: 'poll',
args: ['Test', 'title;', 'one;', 'two'],
message: message
};
return response;
}),
user: {
username: "username",
avatarURL: jest.fn().mockReturnValue("https://avatarurl.com/user.png"),
},
} as unknown as CommandInteraction;
const poll = new Poll();
// Act
const command = new Command();
await command.execute(interaction);
const result = await poll.execute(context);
// Assert
expect(interaction.options.get).toHaveBeenCalledTimes(6);
expect(interaction.options.get).toHaveBeenCalledWith("title", true);
expect(interaction.options.get).toHaveBeenCalledWith("option1", true);
expect(interaction.options.get).toHaveBeenCalledWith("option2", true);
expect(interaction.options.get).toHaveBeenCalledWith("option3");
expect(interaction.options.get).toHaveBeenCalledWith("option4");
expect(interaction.options.get).toHaveBeenCalledWith("option5");
expect(messageChannelSend).toBeCalledTimes(1);
expect(messageDelete).toBeCalledTimes(1);
expect(returnMessageReact).toBeCalledTimes(2);
expect(interaction.reply).toHaveBeenCalledTimes(1);
expect(result.embeds.length).toBe(1);
expect(sentEmbed).toBeDefined();
expect(sentEmbed!.data.color).toBe(EmbedColours.Ok);
expect(sentEmbed!.data.title).toBe("Title");
expect(sentEmbed!.data.description).toBe("1⃣ Option 1\n2⃣ Option 2\n3⃣ Option 3\n4⃣ Option 4\n5⃣ Option 5");
expect(sentEmbed!.data.footer).toBeDefined();
expect(sentEmbed!.data.footer!.text).toBe("Poll by username");
expect(sentEmbed!.data.footer!.icon_url).toBe("https://avatarurl.com/user.png");
// Embed
const embed = result.embeds[0];
expect(interaction.user.avatarURL).toHaveBeenCalledTimes(1);
expect(embed.title).toBe('Test title');
expect(embed.description).toBe(':one: one\n:two: two');
expect(response.fetch).toHaveBeenCalledTimes(1);
expect(message.react).toHaveBeenCalledTimes(5);
expect(message.react).toHaveBeenCalledWith("1⃣");
expect(message.react).toHaveBeenCalledWith("2⃣");
expect(message.react).toHaveBeenCalledWith("3⃣");
expect(message.react).toHaveBeenCalledWith("4⃣");
expect(message.react).toHaveBeenCalledWith("5⃣");
});
test('Given message is not deletable by client, expect it not to attempt deletion', async () => {
const returnMessageReact = jest.fn();
const returnMessage = {
react: returnMessageReact
} as unknown as Message;
const messageChannelSend = jest.fn()
.mockReturnValue(returnMessage);
const messageDelete = jest.fn();
test("GIVEN option3 is not supplied, EXPECT a 2 option poll to be created", async () => {
let sentEmbed: EmbedBuilder | undefined;
// Arrange
const message = {
channel: {
send: messageChannelSend
react: jest.fn(),
} as unknown as Message<boolean>;
const response = {
fetch: jest.fn().mockResolvedValue(message),
} as unknown as InteractionResponse<boolean>;
const interaction = {
options: {
get: jest.fn().mockReturnValueOnce({ value: "Title" })
.mockReturnValueOnce({ value: "Option 1" })
.mockReturnValueOnce({ value: "Option 2" }),
},
delete: messageDelete,
deletable: false
} as unknown as Message;
reply: jest.fn().mockImplementation((options: any) => {
sentEmbed = options.embeds[0];
const context: ICommandContext = {
name: 'poll',
args: ['Test', 'title;', 'one;', 'two'],
message: message
};
return response;
}),
user: {
username: "username",
avatarURL: jest.fn().mockReturnValue("https://avatarurl.com/user.png"),
},
} as unknown as CommandInteraction;
const poll = new Poll();
// Act
const command = new Command();
await command.execute(interaction);
const result = await poll.execute(context);
// Assert
expect(sentEmbed).toBeDefined();
expect(sentEmbed!.data.description).toBe("1⃣ Option 1\n2⃣ Option 2");
expect(messageChannelSend).toBeCalledTimes(1);
expect(messageDelete).not.toBeCalled();
expect(returnMessageReact).toBeCalledTimes(2);
expect(result.embeds.length).toBe(1);
// Embed
const embed = result.embeds[0];
expect(embed.title).toBe('Test title');
expect(embed.description).toBe(':one: one\n:two: two');
expect(message.react).toHaveBeenCalledTimes(2);
});
test('Given no arguments, expect error embed', async () => {
const returnMessageReact = jest.fn();
const returnMessage = {
react: returnMessageReact
} as unknown as Message;
const messageChannelSend = jest.fn()
.mockReturnValue(returnMessage);
const messageDelete = jest.fn();
test("GIVEN option4 is not supplied, EXPECT a 3 option poll to be created", async () => {
let sentEmbed: EmbedBuilder | undefined;
// Arrange
const message = {
channel: {
send: messageChannelSend
react: jest.fn(),
} as unknown as Message<boolean>;
const response = {
fetch: jest.fn().mockResolvedValue(message),
} as unknown as InteractionResponse<boolean>;
const interaction = {
options: {
get: jest.fn().mockReturnValueOnce({ value: "Title" })
.mockReturnValueOnce({ value: "Option 1" })
.mockReturnValueOnce({ value: "Option 2" })
.mockReturnValueOnce({ value: "Option 3" }),
},
delete: messageDelete,
deletable: true
} as unknown as Message;
reply: jest.fn().mockImplementation((options: any) => {
sentEmbed = options.embeds[0];
const context: ICommandContext = {
name: 'poll',
args: [],
message: message
};
return response;
}),
user: {
username: "username",
avatarURL: jest.fn().mockReturnValue("https://avatarurl.com/user.png"),
},
} as unknown as CommandInteraction;
const poll = new Poll();
// Act
const command = new Command();
await command.execute(interaction);
const result = await poll.execute(context);
// Assert
expect(sentEmbed).toBeDefined();
expect(sentEmbed!.data.description).toBe("1⃣ Option 1\n2⃣ Option 2\n3⃣ Option 3");
expect(messageChannelSend).toBeCalledTimes(1);
expect(messageDelete).not.toBeCalled();
expect(returnMessageReact).not.toBeCalled();
expect(result.embeds.length).toBe(1);
// Error Embed
const errorEmbed = result.embeds[0];
expect(errorEmbed.description).toBe('Usage: <title>;<option 1>;<option 2>... (separate options with semicolons), maximum of 9 options');
expect(message.react).toHaveBeenCalledTimes(3);
});
test('Given only 1 option, expect error embed', async () => {
const returnMessageReact = jest.fn();
const returnMessage = {
react: returnMessageReact
} as unknown as Message;
const messageChannelSend = jest.fn()
.mockReturnValue(returnMessage);
const messageDelete = jest.fn();
test("GIVEN option5 is not supplied, EXPECT a 4 option poll to be created", async () => {
let sentEmbed: EmbedBuilder | undefined;
// Arrange
const message = {
channel: {
send: messageChannelSend
react: jest.fn(),
} as unknown as Message<boolean>;
const response = {
fetch: jest.fn().mockResolvedValue(message),
} as unknown as InteractionResponse<boolean>;
const interaction = {
options: {
get: jest.fn().mockReturnValueOnce({ value: "Title" })
.mockReturnValueOnce({ value: "Option 1" })
.mockReturnValueOnce({ value: "Option 2" })
.mockReturnValueOnce({ value: "Option 3" })
.mockReturnValueOnce({ value: "Option 4" }),
},
delete: messageDelete,
deletable: true
} as unknown as Message;
reply: jest.fn().mockImplementation((options: any) => {
sentEmbed = options.embeds[0];
const context: ICommandContext = {
name: 'poll',
args: ['Test', 'title;', 'one'],
message: message
};
const poll = new Poll();
const result = await poll.execute(context);
expect(messageChannelSend).toBeCalledTimes(1);
expect(messageDelete).not.toBeCalled();
expect(returnMessageReact).not.toBeCalled();
expect(result.embeds.length).toBe(1);
// Error Embed
const errorEmbed = result.embeds[0];
expect(errorEmbed.description).toBe('Usage: <title>;<option 1>;<option 2>... (separate options with semicolons), maximum of 9 options');
});
test('Given 9 options, expect poll to be generated', async () => {
const returnMessageReact = jest.fn();
const returnMessage = {
react: returnMessageReact
} as unknown as Message;
const messageChannelSend = jest.fn()
.mockReturnValue(returnMessage);
const messageDelete = jest.fn();
const message = {
channel: {
send: messageChannelSend
return response;
}),
user: {
username: "username",
avatarURL: jest.fn().mockReturnValue("https://avatarurl.com/user.png"),
},
delete: messageDelete,
deletable: true
} as unknown as Message;
} as unknown as CommandInteraction;
const context: ICommandContext = {
name: 'poll',
args: ['Test', 'title;', 'one;', 'two;', 'three;', 'four;', 'five;', 'six;', 'seven;', 'eight;', 'nine'],
message: message
};
// Act
const command = new Command();
await command.execute(interaction);
const poll = new Poll();
// Assert
expect(sentEmbed).toBeDefined();
expect(sentEmbed!.data.description).toBe("1⃣ Option 1\n2⃣ Option 2\n3⃣ Option 3\n4⃣ Option 4");
const result = await poll.execute(context);
expect(messageChannelSend).toBeCalledTimes(1);
expect(messageDelete).toBeCalledTimes(1);
expect(returnMessageReact).toBeCalledTimes(9);
expect(result.embeds.length).toBe(1);
// Embed
const embed = result.embeds[0];
expect(embed.title).toBe('Test title');
expect(embed.description).toBe(':one: one\n:two: two\n:three: three\n:four: four\n:five: five\n:six: six\n:seven: seven\n:eight: eight\n:nine: nine');
});
test('Given 10 options, expect error embed', async () => {
const returnMessageReact = jest.fn();
const returnMessage = {
react: returnMessageReact
} as unknown as Message;
const messageChannelSend = jest.fn()
.mockReturnValue(returnMessage);
const messageDelete = jest.fn();
const message = {
channel: {
send: messageChannelSend
},
delete: messageDelete,
deletable: true
} as unknown as Message;
const context: ICommandContext = {
name: 'poll',
args: ['Test', 'title;', 'one;', 'two;', 'three;', 'four;', 'five;', 'six;', 'seven;', 'eight;', 'nine;', 'ten'],
message: message
};
const poll = new Poll();
const result = await poll.execute(context);
expect(messageChannelSend).toBeCalledTimes(1);
expect(messageDelete).not.toBeCalled();
expect(returnMessageReact).not.toBeCalled();
expect(result.embeds.length).toBe(1);
// Error Embed
const errorEmbed = result.embeds[0];
expect(errorEmbed.description).toBe('Usage: <title>;<option 1>;<option 2>... (separate options with semicolons), maximum of 9 options');
expect(message.react).toHaveBeenCalledTimes(4);
});
});

View file

@ -1,411 +0,0 @@
import { GuildMemberRoleManager, Message, Role as DiscordRole } from "discord.js";
import { mock } from "jest-mock-extended";
import Role from "../../src/commands/role";
import { ICommandContext } from "../../src/contracts/ICommandContext";
beforeEach(() => {
process.env = {};
});
describe('Constructor', () => {
test('Expect properties are set', () => {
const role = new Role();
expect(role._category).toBe("General");
});
});
describe('Execute', () => {
test('Given no arguments were given, expect SendRolesList to be executed', async () => {
process.env = {
COMMANDS_ROLE_ROLES: 'One,Two'
};
const message = {} as unknown as Message;
const context: ICommandContext = {
name: 'role',
args: [],
message: message
};
const role = new Role();
role.SendRolesList = jest.fn();
role.ToggleRole = jest.fn();
await role.execute(context);
expect(role.SendRolesList).toBeCalledWith(context, ['One', 'Two']);
expect(role.ToggleRole).not.toBeCalled();
});
test('Given an argument was given, expect ToggleRole to be executed', async () => {
process.env = {
COMMANDS_ROLE_ROLES: 'One,Two'
};
const message = {} as unknown as Message;
const context: ICommandContext = {
name: 'role',
args: ['One'],
message: message
};
const role = new Role();
role.SendRolesList = jest.fn();
role.ToggleRole = jest.fn();
await role.execute(context);
expect(role.SendRolesList).not.toBeCalled();
expect(role.ToggleRole).toBeCalledWith(context, ['One', 'Two']);
});
});
describe('SendRolesList', () => {
test('Expect embed with roles to be sent to the current channel', () => {
process.env = {
BOT_PREFIX: '!'
};
const messageChannelSend = jest.fn();
const message = {
channel: {
send: messageChannelSend
}
} as unknown as Message;
const context: ICommandContext = {
name: 'role',
args: [],
message: message
};
const roles = ['One', 'Two'];
const role = new Role();
const result = role.SendRolesList(context, roles);
expect(messageChannelSend).toBeCalledTimes(1);
expect(result.embeds.length).toBe(1);
// Embed
const embed = result.embeds[0];
expect(embed.title).toBe('Roles');
expect(embed.description).toBe('Do !role <role> to get the role!\nOne\nTwo');
});
});
describe('ToggleRole', () => {
test('Given role name is a valid role AND user does not have the role, expect role to be added', async () => {
const discordRole = {} as unknown as DiscordRole;
const messageMemberRolesCacheFind = jest.fn()
.mockReturnValue(undefined);
const messageGuildRolesCacheFind = jest.fn()
.mockReturnValue(discordRole);
const messageChannelSend = jest.fn();
const message = {
member: {
roles: {
cache: {
find: messageMemberRolesCacheFind
}
}
},
guild: {
roles: {
cache: {
find: messageGuildRolesCacheFind
}
}
},
channel: {
send: messageChannelSend
}
} as unknown as Message;
const context: ICommandContext = {
name: 'role',
args: ['One'],
message: message
};
const roles = ['One', 'Two'];
const role = new Role();
role.AddRole = jest.fn();
role.RemoveRole = jest.fn();
const result = await role.ToggleRole(context, roles);
expect(messageMemberRolesCacheFind).toBeCalledTimes(1);
expect(messageGuildRolesCacheFind).toBeCalledTimes(1);
expect(messageChannelSend).not.toBeCalled();
expect(role.AddRole).toBeCalledWith(context, discordRole);
expect(role.RemoveRole).not.toBeCalled();
expect(result.embeds.length).toBe(0);
});
test('Given role name is a valid role AND user has the role, expect role to be removed', async () => {
const discordRole = {} as unknown as DiscordRole;
const messageMemberRolesCacheFind = jest.fn()
.mockReturnValue(discordRole);
const messageGuildRolesCacheFind = jest.fn()
.mockReturnValue(discordRole);
const messageChannelSend = jest.fn();
const message = {
member: {
roles: {
cache: {
find: messageMemberRolesCacheFind
}
}
},
guild: {
roles: {
cache: {
find: messageGuildRolesCacheFind
}
}
},
channel: {
send: messageChannelSend
}
} as unknown as Message;
const context: ICommandContext = {
name: 'role',
args: ['One'],
message: message
};
const roles = ['One', 'Two'];
const role = new Role();
role.AddRole = jest.fn();
role.RemoveRole = jest.fn();
const result = await role.ToggleRole(context, roles);
expect(messageMemberRolesCacheFind).toBeCalledTimes(1);
expect(messageGuildRolesCacheFind).toBeCalledTimes(1);
expect(messageChannelSend).not.toBeCalled();
expect(role.AddRole).not.toBeCalled();
expect(role.RemoveRole).toBeCalledWith(context, discordRole);
expect(result.embeds.length).toBe(0);
});
test('Given role requested is not in the roles array, expect role not assignable error', async () => {
const discordRole = {} as unknown as DiscordRole;
const messageMemberRolesCacheFind = jest.fn()
.mockReturnValue(undefined);
const messageGuildRolesCacheFind = jest.fn()
.mockReturnValue(discordRole);
const messageChannelSend = jest.fn();
const message = {
member: {
roles: {
cache: {
find: messageMemberRolesCacheFind
}
}
},
guild: {
roles: {
cache: {
find: messageGuildRolesCacheFind
}
}
},
channel: {
send: messageChannelSend
}
} as unknown as Message;
const context: ICommandContext = {
name: 'role',
args: ['Three'],
message: message
};
const roles = ['One', 'Two'];
const role = new Role();
role.AddRole = jest.fn();
role.RemoveRole = jest.fn();
const result = await role.ToggleRole(context, roles);
expect(messageMemberRolesCacheFind).not.toBeCalled();
expect(messageGuildRolesCacheFind).not.toBeCalled();
expect(messageChannelSend).toBeCalledTimes(1);
expect(role.AddRole).not.toBeCalled();
expect(role.RemoveRole).not.toBeCalled();
expect(result.embeds.length).toBe(1);
// Error Embed
const errorEmbed = result.embeds[0];
expect(errorEmbed.description).toBe("This role isn't marked as assignable, to see a list of assignable roles, run this command without any parameters");
});
test('Given the role is not in the guild, expect error', async () => {
const discordRole = {} as unknown as DiscordRole;
const messageMemberRolesCacheFind = jest.fn()
.mockReturnValue(undefined);
const messageGuildRolesCacheFind = jest.fn()
.mockReturnValue(undefined);
const messageChannelSend = jest.fn();
const message = {
member: {
roles: {
cache: {
find: messageMemberRolesCacheFind
}
}
},
guild: {
roles: {
cache: {
find: messageGuildRolesCacheFind
}
}
},
channel: {
send: messageChannelSend
}
} as unknown as Message;
const context: ICommandContext = {
name: 'role',
args: ['One'],
message: message
};
const roles = ['One', 'Two'];
const role = new Role();
role.AddRole = jest.fn();
role.RemoveRole = jest.fn();
const result = await role.ToggleRole(context, roles);
expect(messageMemberRolesCacheFind).not.toBeCalled();
expect(messageGuildRolesCacheFind).toBeCalledTimes(1);
expect(messageChannelSend).toBeCalledTimes(1);
expect(role.AddRole).not.toBeCalled();
expect(role.RemoveRole).not.toBeCalled();
expect(result.embeds.length).toBe(1);
// Error Embed
const errorEmbed = result.embeds[0];
expect(errorEmbed.description).toBe("The current server doesn't have this role. Please contact the server's moderators");
});
});
describe('AddRole', () => {
test('Expect role to be added to user', async () => {
const messageChannelSend = jest.fn();
const guildMemberRoleManager = mock<GuildMemberRoleManager>();
const message = {
member: {
roles: guildMemberRoleManager
},
channel: {
send: messageChannelSend
}
} as unknown as Message;
const context: ICommandContext = {
name: 'role',
args: ['One'],
message: message
};
const discordRole = {
name: 'One'
} as unknown as DiscordRole;
const role = new Role();
const result = await role.AddRole(context, discordRole);
expect(guildMemberRoleManager.add).toBeCalledWith(discordRole, "Toggled with role command");
expect(messageChannelSend).toBeCalled();
expect(result.embeds.length).toBe(1);
// Embed
const embed = result.embeds[0];
expect(embed.title).toBe('');
expect(embed.description).toBe('Gave role: One');
});
});
describe('RemoveRole', () => {
test('Expect role to be removed from user', async () => {
const messageChannelSend = jest.fn();
const guildMemberRoleManager = mock<GuildMemberRoleManager>();
const message = {
member: {
roles: guildMemberRoleManager
},
channel: {
send: messageChannelSend
}
} as unknown as Message;
const context: ICommandContext = {
name: 'role',
args: ['One'],
message: message
};
const discordRole = {
name: 'One'
} as unknown as DiscordRole;
const role = new Role();
const result = await role.RemoveRole(context, discordRole);
expect(guildMemberRoleManager.remove).toBeCalledWith(discordRole, "Toggled with role command");
expect(messageChannelSend).toBeCalled();
expect(result.embeds.length).toBe(1);
// Embed
const embed = result.embeds[0];
expect(embed.title).toBe('');
expect(embed.description).toBe('Removed role: One');
});
});

View file

@ -1,106 +1,25 @@
import { Message } from "discord.js";
import Rules from "../../src/commands/rules";
import { ICommandContext } from "../../src/contracts/ICommandContext";
const oldCwd = process.cwd();
import Command from "../../src/commands/rules";
beforeEach(() => {
process.env = {};
});
describe('Constructor', () => {
test('Expect properties to be set', () => {
process.env = {
ROLES_MODERATOR: "Moderator"
};
test('EXPECT properties to be set', () => {
const command = new Command();
const rules = new Rules();
expect(rules._category).toBe("Admin");
expect(rules._roles.length).toBe(1);
expect(rules._roles[0]).toBe("Moderator");
expect(command.CommandBuilder).toMatchSnapshot();
});
});
describe('Execute', () => {
test('Given rules exist, expect rules to be sent to current channel', () => {
process.env = {
COMMANDS_RULES_FILE: 'rules/rules.json'
};
test.todo("GIVEN interaction is not a chat input command, EXPECT nothing to happen");
process.cwd = jest.fn()
.mockReturnValue(`${oldCwd}/tests/_mocks`);
test.todo("GIVEN invalid subcommand is given, EXPECT invalid error");
});
const messageChannelSend = jest.fn();
describe("access", () => {
test.todo("GIVEN interaction.guildId is null, EXPECT nothing to happen");
const message = {
channel: {
send: messageChannelSend
}
} as unknown as Message;
const context: ICommandContext = {
name: 'rules',
args: [],
message: message
};
const rules = new Rules();
const result = rules.execute(context);
expect(messageChannelSend).toBeCalledTimes(2);
expect(result.embeds.length).toBe(2);
// Header Embed
const embedHeader = result.embeds[0];
expect(embedHeader.title).toBe("");
expect(embedHeader.description).toBe("");
expect(embedHeader.image?.url).toBe("IMAGEURL");
expect(embedHeader.footer?.text).toBe("");
// Main Embed
const embedMain = result.embeds[1];
expect(embedMain.title).toBe("TITLE 1");
expect(embedMain.description).toBe("DESCRIPTION 1A\nDESCRIPTION 1B");
expect(embedMain.image?.url).toBe("");
expect(embedMain.footer?.text).toBe("FOOTER 1");
});
test('Given rules file does not exist, expect does not exist error', () => {
process.env = {
COMMANDS_RULES_FILE: 'rules/none.json'
};
process.cwd = jest.fn()
.mockReturnValue(`${oldCwd}/tests/_mocks`);
const messageChannelSend = jest.fn();
const message = {
channel: {
send: messageChannelSend
}
} as unknown as Message;
const context: ICommandContext = {
name: 'rules',
args: [],
message: message
};
const rules = new Rules();
const result = rules.execute(context);
expect(messageChannelSend).toBeCalledTimes(1);
expect(result.embeds.length).toBe(1);
// Error Embed
const errorEmbed = result.embeds[0];
expect(errorEmbed.description).toBe("Rules file doesn't exist");
});
test.todo("GIVEN rules.access.label setting is not supplied, EXPECT label to be defaulted");
});

View file

@ -0,0 +1,11 @@
describe("constructor", () => {
test.todo("EXPECT properties to be set");
});
describe('execute', () => {
test.todo("EXPECT server to be setup in database");
test.todo("GIVEN interaction.guildId is null, EXPECT nothing to happen");
test.todo("GIVEN server is found in the database, EXPECT error");
});

View file

@ -1,4 +1,4 @@
import { APIEmbed, CacheType, CommandInteraction, CommandInteractionOption, DMChannel, Embed, EmbedBuilder, EmbedField, Guild, GuildChannel, GuildMember, InteractionReplyOptions, JSONEncodable, Message, MessageCreateOptions, MessagePayload, SlashCommandBuilder, TextChannel, User } from "discord.js";
import { APIEmbed, CacheType, CommandInteraction, CommandInteractionOption, DMChannel, EmbedBuilder, EmbedField, Guild, GuildMember, InteractionReplyOptions, JSONEncodable, Message, MessageCreateOptions, SlashCommandBuilder, TextChannel, User } from "discord.js";
import { mock } from "jest-mock-extended";
import Timeout from "../../src/commands/timeout";
import SettingsHelper from "../../src/helpers/SettingsHelper";
@ -44,8 +44,8 @@ describe('execute', () => {
const timeoutFunc = jest.fn();
let dmChannelSentEmbeds: (APIEmbed | JSONEncodable<APIEmbed>)[] | undefined;
let logsChannelSentEmbeds: (APIEmbed | JSONEncodable<APIEmbed>)[] | undefined;
let dmChannelSentEmbeds: readonly (APIEmbed | JSONEncodable<APIEmbed>)[] | undefined;
let logsChannelSentEmbeds: readonly (APIEmbed | JSONEncodable<APIEmbed>)[] | undefined;
const dmChannel = {
send: jest.fn().mockImplementation((options: MessageCreateOptions) => {
@ -58,6 +58,7 @@ describe('execute', () => {
id: 'userId',
tag: 'userTag',
createDM: jest.fn().mockResolvedValue(dmChannel),
avatarURL: jest.fn().mockReturnValue("https://google.com/test.png"),
} as unknown as User,
member: {
manageable: true,
@ -106,7 +107,7 @@ describe('execute', () => {
await command.execute(interaction);
// EXPECT user to be timed out
expect(timeoutFunc).toBeCalledWith(1000, 'Test reason');
expect(timeoutFunc).toHaveBeenCalledWith(1000, 'Test reason');
// EXPECT embeds to be sent
expect(embeds).toBeDefined();
@ -116,6 +117,7 @@ describe('execute', () => {
const resultEmbed = embeds![0] as EmbedBuilder;
expect(resultEmbed.data.description).toBe('<@userId> has been timed out');
expect(resultEmbed.data.fields).toBeDefined();
expect(resultEmbed.data.fields!.length).toBe(1);
@ -154,6 +156,7 @@ describe('execute', () => {
expect(logsChannelSentEmbed.color).toBe(EmbedColours.Ok);
expect(logsChannelSentEmbed.title).toBe("Member Timed Out");
expect(logsChannelSentEmbed.thumbnail.url).toBe("https://google.com/test.png");
expect(logsChannelSentEmbed.description).toBe("<@userId> `userTag`");
expect(logsChannelSentEmbed.fields?.length).toBe(4);
@ -415,7 +418,8 @@ describe('execute', () => {
user: {
id: 'userId',
tag: 'userTag',
} as User,
avatarURL: jest.fn().mockReturnValue("https://google.com/test.png"),
} as unknown as User,
member: {
manageable: false,
} as GuildMember
@ -482,8 +486,9 @@ describe('execute', () => {
sentEmbeds.push(options.embeds![0] as EmbedBuilder);
return mock<Message<false>>();
})
}),
}) as unknown as DMChannel,
avatarURL: jest.fn().mockReturnValue("https://google.com/test.png"),
} as unknown as User,
member: {
manageable: true,
@ -567,6 +572,7 @@ describe('execute', () => {
return mock<Message<false>>();
})
}) as unknown as DMChannel,
avatarURL: jest.fn().mockReturnValue("https://google.com/test.png"),
} as unknown as User,
member: {
manageable: true,
@ -641,6 +647,7 @@ describe('execute', () => {
return mock<Message<false>>();
})
}) as unknown as DMChannel,
avatarURL: jest.fn().mockReturnValue("https://google.com/test.png"),
} as unknown as User,
member: {
manageable: true,
@ -690,8 +697,8 @@ describe('execute', () => {
const timeoutFunc = jest.fn();
let dmChannelSentEmbeds: (APIEmbed | JSONEncodable<APIEmbed>)[] | undefined;
let logsChannelSentEmbeds: (APIEmbed | JSONEncodable<APIEmbed>)[] | undefined;
let dmChannelSentEmbeds: readonly (APIEmbed | JSONEncodable<APIEmbed>)[] | undefined;
let logsChannelSentEmbeds: readonly (APIEmbed | JSONEncodable<APIEmbed>)[] | undefined;
const dmChannel = {
send: jest.fn().mockImplementation((options: MessageCreateOptions) => {
@ -704,6 +711,7 @@ describe('execute', () => {
id: 'userId',
tag: 'userTag',
createDM: jest.fn().mockRejectedValue(undefined),
avatarURL: jest.fn().mockReturnValue("https://google.com/test.png"),
} as unknown as User,
member: {
manageable: true,
@ -763,4 +771,4 @@ describe('execute', () => {
expect(resultEmbedDMField.name).toBe("DM Sent");
expect(resultEmbedDMField.value).toBe("false");
});
});
});

View file

@ -1,813 +0,0 @@
import { GuildMember, Message, Role, TextChannel, User } from "discord.js";
import Unmute from "../../src/commands/unmute";
import { ICommandContext } from "../../src/contracts/ICommandContext";
beforeEach(() => {
process.env = {};
});
describe('Constructor', () => {
test('Expect properties to be set', () => {
process.env = {
ROLES_MODERATOR: 'Moderator'
};
const mute = new Unmute();
expect(mute._category).toBe("Moderation");
expect(mute._roles.length).toBe(1);
expect(mute._roles[0]).toBe('Moderator');
});
});
describe('Execute', () => {
test('Given user has permission, expect user to be given muted role', async () => {
process.env = {
CHANNELS_LOGS_MOD: 'mod-logs',
ROLES_MUTED: 'Muted'
};
const user = {
displayAvatarURL: jest.fn(),
tag: 'USERTAG'
} as unknown as User;
const messageAuthor = {
tag: 'AUTHORTAG'
} as unknown as User;
const member = {
manageable: true,
roles: {
remove: jest.fn()
}
} as unknown as GuildMember;
const role = {
name: 'Muted'
} as unknown as Role;
const logChannel = {
name: 'mod-logs',
send: jest.fn()
} as unknown as TextChannel;
const messageMentionsUsersFirst = jest.fn()
.mockReturnValue(user);
const messageGuildMember = jest.fn()
.mockReturnValue(member);
const messageGuildRolesCacheFind = jest.fn()
.mockImplementation((callback): Role | undefined => {
const result = callback(role);
if (!result) {
return undefined;
}
return role;
});
const messageChannelSend = jest.fn();
const messageGuildChannelsCacheFind = jest.fn()
.mockImplementation((callback): TextChannel | undefined => {
const result = callback(logChannel);
if (!result) {
return undefined;
}
return logChannel;
});
const message = {
mentions: {
users: {
first: messageMentionsUsersFirst
}
},
guild: {
member: messageGuildMember,
available: true,
roles: {
cache: {
find: messageGuildRolesCacheFind
}
},
channels: {
cache: {
find: messageGuildChannelsCacheFind
}
}
},
channel: {
send: messageChannelSend
},
author: messageAuthor
} as unknown as Message;
const context: ICommandContext = {
name: 'mute',
args: ['USER', 'Test', 'Reason'],
message: message
};
const mute = new Unmute();
const result = await mute.execute(context);
expect(messageMentionsUsersFirst).toBeCalledTimes(1);
expect(messageGuildMember).toBeCalledWith(user);
expect(messageGuildRolesCacheFind).toBeCalledTimes(1);
expect(messageGuildChannelsCacheFind).toBeCalledTimes(1);
expect(messageChannelSend).toBeCalledTimes(1);
expect(member.roles.remove).toBeCalledWith(role, 'Moderator: AUTHORTAG, Reason: Test Reason');
expect(result.embeds.length).toBe(2);
// Log Embed
const logEmbed = result.embeds[0];
expect(logEmbed.title).toBe('Member Unmuted');
expect(logEmbed.fields.length).toBe(3);
// Log Embed -> User Field
const logEmbedUserField = logEmbed.fields[0];
expect(logEmbedUserField.name).toBe('User');
expect(logEmbedUserField.value).toBe('[object Object] `USERTAG`');
expect(logEmbedUserField.inline).toBeTruthy();
// Log Embed -> Moderator Field
const logEmbedModeratorField = logEmbed.fields[1];
expect(logEmbedModeratorField.name).toBe('Moderator');
expect(logEmbedModeratorField.value).toBe('[object Object] `AUTHORTAG`');
// Log Embed -> Reason Field
const logEmbedFieldReason = logEmbed.fields[2];
expect(logEmbedFieldReason.name).toBe('Reason');
expect(logEmbedFieldReason.value).toBe('Test Reason');
// Public Embed
const publicEmbed = result.embeds[1];
expect(publicEmbed.title).toBe('');
expect(publicEmbed.description).toBe('[object Object] has been unmuted');
});
test('Given moderator did not supply a reason, expect default reason is used', async () => {
process.env = {
CHANNELS_LOGS_MOD: 'mod-logs',
ROLES_MUTED: 'Muted'
};
const user = {
displayAvatarURL: jest.fn(),
tag: 'USERTAG'
} as unknown as User;
const messageAuthor = {
tag: 'AUTHORTAG'
} as unknown as User;
const member = {
manageable: true,
roles: {
remove: jest.fn()
}
} as unknown as GuildMember;
const role = {
name: 'Muted'
} as unknown as Role;
const logChannel = {
name: 'mod-logs',
send: jest.fn()
} as unknown as TextChannel;
const messageMentionsUsersFirst = jest.fn()
.mockReturnValue(user);
const messageGuildMember = jest.fn()
.mockReturnValue(member);
const messageGuildRolesCacheFind = jest.fn()
.mockImplementation((callback): Role | undefined => {
const result = callback(role);
if (!result) {
return undefined;
}
return role;
});
const messageChannelSend = jest.fn();
const messageGuildChannelsCacheFind = jest.fn()
.mockImplementation((callback): TextChannel | undefined => {
const result = callback(logChannel);
if (!result) {
return undefined;
}
return logChannel;
});
const message = {
mentions: {
users: {
first: messageMentionsUsersFirst
}
},
guild: {
member: messageGuildMember,
available: true,
roles: {
cache: {
find: messageGuildRolesCacheFind
}
},
channels: {
cache: {
find: messageGuildChannelsCacheFind
}
}
},
channel: {
send: messageChannelSend
},
author: messageAuthor
} as unknown as Message;
const context: ICommandContext = {
name: 'mute',
args: ['USER'],
message: message
};
const mute = new Unmute();
const result = await mute.execute(context);
expect(messageMentionsUsersFirst).toBeCalledTimes(1);
expect(messageGuildMember).toBeCalledWith(user);
expect(messageGuildRolesCacheFind).toBeCalledTimes(1);
expect(messageGuildChannelsCacheFind).toBeCalledTimes(1);
expect(messageChannelSend).toBeCalledTimes(1);
expect(member.roles.remove).toBeCalledWith(role, 'Moderator: AUTHORTAG, Reason: *none*');
expect(result.embeds.length).toBe(2);
// Log Embed
const logEmbed = result.embeds[0];
expect(logEmbed.title).toBe('Member Unmuted');
expect(logEmbed.fields.length).toBe(3);
// Log Embed -> User Field
const logEmbedUserField = logEmbed.fields[0];
expect(logEmbedUserField.name).toBe('User');
expect(logEmbedUserField.value).toBe('[object Object] `USERTAG`');
expect(logEmbedUserField.inline).toBeTruthy();
// Log Embed -> Moderator Field
const logEmbedModeratorField = logEmbed.fields[1];
expect(logEmbedModeratorField.name).toBe('Moderator');
expect(logEmbedModeratorField.value).toBe('[object Object] `AUTHORTAG`');
// Log Embed -> Reason Field
const logEmbedFieldReason = logEmbed.fields[2];
expect(logEmbedFieldReason.name).toBe('Reason');
expect(logEmbedFieldReason.value).toBe('*none*');
// Public Embed
const publicEmbed = result.embeds[1];
expect(publicEmbed.title).toBe('');
expect(publicEmbed.description).toBe('[object Object] has been unmuted');
});
test('Given user did not mention a user, expect user not to exist', async () => {
process.env = {
CHANNELS_LOGS_MOD: 'mod-logs',
ROLES_MUTED: 'Muted'
};
const user = {
displayAvatarURL: jest.fn(),
tag: 'USERTAG'
} as unknown as User;
const messageAuthor = {
tag: 'AUTHORTAG'
} as unknown as User;
const member = {
manageable: true,
roles: {
remove: jest.fn()
}
} as unknown as GuildMember;
const role = {
name: 'Muted'
} as unknown as Role;
const logChannel = {
name: 'mod-logs',
send: jest.fn()
} as unknown as TextChannel;
const messageMentionsUsersFirst = jest.fn()
.mockReturnValue(null);
const messageGuildMember = jest.fn()
.mockReturnValue(member);
const messageGuildRolesCacheFind = jest.fn()
.mockImplementation((callback): Role | undefined => {
const result = callback(role);
if (!result) {
return undefined;
}
return role;
});
const messageChannelSend = jest.fn();
const messageGuildChannelsCacheFind = jest.fn()
.mockImplementation((callback): TextChannel | undefined => {
const result = callback(logChannel);
if (!result) {
return undefined;
}
return logChannel;
});
const message = {
mentions: {
users: {
first: messageMentionsUsersFirst
}
},
guild: {
member: messageGuildMember,
available: true,
roles: {
cache: {
find: messageGuildRolesCacheFind
}
},
channels: {
cache: {
find: messageGuildChannelsCacheFind
}
}
},
channel: {
send: messageChannelSend
},
author: messageAuthor
} as unknown as Message;
const context: ICommandContext = {
name: 'mute',
args: ['USER', 'Test', 'Reason'],
message: message
};
const mute = new Unmute();
const result = await mute.execute(context);
expect(messageMentionsUsersFirst).toBeCalledTimes(1);
expect(messageGuildMember).not.toBeCalled();
expect(messageGuildRolesCacheFind).not.toBeCalled();
expect(messageGuildChannelsCacheFind).not.toBeCalled();
expect(messageChannelSend).toBeCalledTimes(1);
expect(result.embeds.length).toBe(1);
// Error Embed
const errorEmbed = result.embeds[0];
expect(errorEmbed.description).toBe('User does not exist');
});
test('Given member can not be found from user, expect user to not be in server', async () => {
process.env = {
CHANNELS_LOGS_MOD: 'mod-logs',
ROLES_MUTED: 'Muted'
};
const user = {
displayAvatarURL: jest.fn(),
tag: 'USERTAG'
} as unknown as User;
const messageAuthor = {
tag: 'AUTHORTAG'
} as unknown as User;
const member = {
manageable: true,
roles: {
remove: jest.fn()
}
} as unknown as GuildMember;
const role = {
name: 'Muted'
} as unknown as Role;
const logChannel = {
name: 'mod-logs',
send: jest.fn()
} as unknown as TextChannel;
const messageMentionsUsersFirst = jest.fn()
.mockReturnValue(user);
const messageGuildMember = jest.fn()
.mockReturnValue(null);
const messageGuildRolesCacheFind = jest.fn()
.mockImplementation((callback): Role | undefined => {
const result = callback(role);
if (!result) {
return undefined;
}
return role;
});
const messageChannelSend = jest.fn();
const messageGuildChannelsCacheFind = jest.fn()
.mockImplementation((callback): TextChannel | undefined => {
const result = callback(logChannel);
if (!result) {
return undefined;
}
return logChannel;
});
const message = {
mentions: {
users: {
first: messageMentionsUsersFirst
}
},
guild: {
member: messageGuildMember,
available: true,
roles: {
cache: {
find: messageGuildRolesCacheFind
}
},
channels: {
cache: {
find: messageGuildChannelsCacheFind
}
}
},
channel: {
send: messageChannelSend
},
author: messageAuthor
} as unknown as Message;
const context: ICommandContext = {
name: 'mute',
args: ['USER', 'Test', 'Reason'],
message: message
};
const mute = new Unmute();
const result = await mute.execute(context);
expect(messageMentionsUsersFirst).toBeCalledTimes(1);
expect(messageGuildMember).toBeCalledWith(user);
expect(messageGuildRolesCacheFind).not.toBeCalled();
expect(messageGuildChannelsCacheFind).not.toBeCalled();
expect(messageChannelSend).toBeCalledTimes(1);
expect(result.embeds.length).toBe(1);
// Error Embed
const errorEmbed = result.embeds[0];
expect(errorEmbed.description).toBe('User is not in this server');
});
test('Given guild is unavailable, expect execution to stop', async () => {
process.env = {
CHANNELS_LOGS_MOD: 'mod-logs',
ROLES_MUTED: 'Muted'
};
const user = {
displayAvatarURL: jest.fn(),
tag: 'USERTAG'
} as unknown as User;
const messageAuthor = {
tag: 'AUTHORTAG'
} as unknown as User;
const member = {
manageable: true,
roles: {
remove: jest.fn()
}
} as unknown as GuildMember;
const role = {
name: 'Muted'
} as unknown as Role;
const logChannel = {
name: 'mod-logs',
send: jest.fn()
} as unknown as TextChannel;
const messageMentionsUsersFirst = jest.fn()
.mockReturnValue(user);
const messageGuildMember = jest.fn()
.mockReturnValue(member);
const messageGuildRolesCacheFind = jest.fn()
.mockImplementation((callback): Role | undefined => {
const result = callback(role);
if (!result) {
return undefined;
}
return role;
});
const messageChannelSend = jest.fn();
const messageGuildChannelsCacheFind = jest.fn()
.mockImplementation((callback): TextChannel | undefined => {
const result = callback(logChannel);
if (!result) {
return undefined;
}
return logChannel;
});
const message = {
mentions: {
users: {
first: messageMentionsUsersFirst
}
},
guild: {
member: messageGuildMember,
available: false,
roles: {
cache: {
find: messageGuildRolesCacheFind
}
},
channels: {
cache: {
find: messageGuildChannelsCacheFind
}
}
},
channel: {
send: messageChannelSend
},
author: messageAuthor
} as unknown as Message;
const context: ICommandContext = {
name: 'mute',
args: ['USER', 'Test', 'Reason'],
message: message
};
const mute = new Unmute();
const result = await mute.execute(context);
expect(messageMentionsUsersFirst).toBeCalledTimes(1);
expect(messageGuildMember).toBeCalledWith(user);
expect(messageGuildRolesCacheFind).not.toBeCalled();
expect(messageGuildChannelsCacheFind).not.toBeCalled();
expect(messageChannelSend).not.toBeCalled();
expect(result.embeds.length).toBe(0);
});
test('Given client can not manage user, expect insufficient permissions', async () => {
process.env = {
CHANNELS_LOGS_MOD: 'mod-logs',
ROLES_MUTED: 'Muted'
};
const user = {
displayAvatarURL: jest.fn(),
tag: 'USERTAG'
} as unknown as User;
const messageAuthor = {
tag: 'AUTHORTAG'
} as unknown as User;
const member = {
manageable: false,
roles: {
remove: jest.fn()
}
} as unknown as GuildMember;
const role = {
name: 'Muted'
} as unknown as Role;
const logChannel = {
name: 'mod-logs',
send: jest.fn()
} as unknown as TextChannel;
const messageMentionsUsersFirst = jest.fn()
.mockReturnValue(user);
const messageGuildMember = jest.fn()
.mockReturnValue(member);
const messageGuildRolesCacheFind = jest.fn()
.mockImplementation((callback): Role | undefined => {
const result = callback(role);
if (!result) {
return undefined;
}
return role;
});
const messageChannelSend = jest.fn();
const messageGuildChannelsCacheFind = jest.fn()
.mockImplementation((callback): TextChannel | undefined => {
const result = callback(logChannel);
if (!result) {
return undefined;
}
return logChannel;
});
const message = {
mentions: {
users: {
first: messageMentionsUsersFirst
}
},
guild: {
member: messageGuildMember,
available: true,
roles: {
cache: {
find: messageGuildRolesCacheFind
}
},
channels: {
cache: {
find: messageGuildChannelsCacheFind
}
}
},
channel: {
send: messageChannelSend
},
author: messageAuthor
} as unknown as Message;
const context: ICommandContext = {
name: 'mute',
args: ['USER', 'Test', 'Reason'],
message: message
};
const mute = new Unmute();
const result = await mute.execute(context);
expect(messageMentionsUsersFirst).toBeCalledTimes(1);
expect(messageGuildMember).toBeCalledWith(user);
expect(messageGuildRolesCacheFind).not.toBeCalled();
expect(messageGuildChannelsCacheFind).not.toBeCalled();
expect(messageChannelSend).toBeCalledTimes(1);
expect(result.embeds.length).toBe(1);
// Error Embed
const errorEmbed = result.embeds[0];
expect(errorEmbed.description).toBe('Unable to do this action, am I missing permissions?');
});
test('Given muted role can not be found, expect role not found', async () => {
process.env = {
CHANNELS_LOGS_MOD: 'mod-logs',
ROLES_MUTED: 'Muted'
};
const user = {
displayAvatarURL: jest.fn(),
tag: 'USERTAG'
} as unknown as User;
const messageAuthor = {
tag: 'AUTHORTAG'
} as unknown as User;
const member = {
manageable: true,
roles: {
remove: jest.fn()
}
} as unknown as GuildMember;
const role = {
name: 'Muted'
} as unknown as Role;
const logChannel = {
name: 'mod-logs',
send: jest.fn()
} as unknown as TextChannel;
const messageMentionsUsersFirst = jest.fn()
.mockReturnValue(user);
const messageGuildMember = jest.fn()
.mockReturnValue(member);
const messageGuildRolesCacheFind = jest.fn()
.mockReturnValue(undefined);
const messageChannelSend = jest.fn();
const messageGuildChannelsCacheFind = jest.fn()
.mockImplementation((callback): TextChannel | undefined => {
const result = callback(logChannel);
if (!result) {
return undefined;
}
return logChannel;
});
const message = {
mentions: {
users: {
first: messageMentionsUsersFirst
}
},
guild: {
member: messageGuildMember,
available: true,
roles: {
cache: {
find: messageGuildRolesCacheFind
}
},
channels: {
cache: {
find: messageGuildChannelsCacheFind
}
}
},
channel: {
send: messageChannelSend
},
author: messageAuthor
} as unknown as Message;
const context: ICommandContext = {
name: 'mute',
args: ['USER', 'Test', 'Reason'],
message: message
};
const mute = new Unmute();
const result = await mute.execute(context);
expect(messageMentionsUsersFirst).toBeCalledTimes(1);
expect(messageGuildMember).toBeCalledWith(user);
expect(messageGuildRolesCacheFind).toBeCalledTimes(1);
expect(messageGuildChannelsCacheFind).not.toBeCalled();
expect(messageChannelSend).toBeCalledTimes(1);
expect(result.embeds.length).toBe(1);
// Error Embed
const errorEmbed = result.embeds[0];
expect(errorEmbed.description).toBe('Unable to find role');
});
});

View file

@ -1,485 +1,494 @@
import { GuildMember, Message, TextChannel, User } from "discord.js";
import {APIEmbed, APIEmbedField, CommandInteraction, EmbedBuilder, PermissionsBitField, SlashCommandBuilder, SlashCommandStringOption, SlashCommandUserOption, TextChannel} from "discord.js";
import Warn from "../../src/commands/warn";
import { ICommandContext } from "../../src/contracts/ICommandContext";
import SettingsHelper from "../../src/helpers/SettingsHelper";
import EmbedColours from "../../src/constants/EmbedColours";
import Audit from "../../src/database/entities/Audit";
import {AuditType} from "../../src/constants/AuditType";
beforeEach(() => {
process.env = {};
});
describe('Constructor', () => {
test('Expect values to be set', () => {
process.env.ROLES_MODERATOR = 'Moderator';
test('EXPECT values to be set', () => {
const command = new Warn();
const warn = new Warn();
expect(command.CommandBuilder).toBeDefined();
expect(warn._category).toBe('Moderation');
expect(warn._roles.length).toBe(1);
expect(warn._roles[0]).toBe('Moderator');
const commandBuilder = command.CommandBuilder as SlashCommandBuilder;
expect(commandBuilder.name).toBe("warn");
expect(commandBuilder.description).toBe("Warns a member in the server with an optional reason")
expect(commandBuilder.default_member_permissions).toBe(PermissionsBitField.Flags.ModerateMembers.toString());
expect(commandBuilder.options.length).toBe(2);
const commandBuilderUserOption = commandBuilder.options[0] as SlashCommandUserOption;
expect(commandBuilderUserOption.name).toBe("target");
expect(commandBuilderUserOption.description).toBe("The user");
expect(commandBuilderUserOption.required).toBe(true);
const commandBuilderReasonOption = commandBuilder.options[1] as SlashCommandStringOption;
expect(commandBuilderReasonOption.name).toBe("reason");
expect(commandBuilderReasonOption.description).toBe("The reason");
});
});
describe('Execute', () => {
test('Given user has permission, expect user to be warnned', async () => {
process.env = {
ROLES_MODERATOR: 'Moderator',
CHANNELS_LOGS_MOD: 'mod-logs'
test("EXPECT user to be warned", async () => {
let sentEmbeds: EmbedBuilder[] | undefined;
let savedAudit: Audit | undefined;
// Arrange
const targetUser = {
user: {
id: "userId",
tag: "userTag",
avatarURL: jest.fn().mockReturnValue("https://google.com/avatar.png"),
},
member: {},
};
const reason = {
value: "Test reason",
};
const mentionedUser = {
displayAvatarURL: jest.fn(),
tag: 'USERTAG'
} as unknown as User;
const mentionedMember = {
warnnable: true,
warn: jest.fn()
} as unknown as GuildMember;
const logChannel = {
name: 'mod-logs',
send: jest.fn()
send: jest.fn().mockImplementation((opts: any) => {
sentEmbeds = opts.embeds;
}),
} as unknown as TextChannel;
const messageChannelSend = jest.fn();
const messageMentionsUsersFirst = jest.fn()
.mockReturnValue(mentionedUser);
const messageGuildMember = jest.fn()
.mockReturnValue(mentionedMember);
const messageGuildChannelsCacheFind = jest.fn()
.mockImplementation((callback): TextChannel | undefined => {
const result = callback(logChannel);
if (!result) {
return undefined;
}
return logChannel;
});
const message = {
channel: {
send: messageChannelSend
},
mentions: {
users: {
first: messageMentionsUsersFirst
}
const interaction = {
reply: jest.fn(),
options: {
get: jest.fn()
.mockReturnValueOnce(targetUser)
.mockReturnValue(reason),
},
guild: {
member: messageGuildMember ,
channels: {
cache: {
find: messageGuildChannelsCacheFind
}
find: jest.fn().mockReturnValue(logChannel),
},
},
available: true
},
author: {
tag: 'AUTHORTAG'
}
} as unknown as Message;
guildId: "guildId",
user: {
id: "moderatorId",
},
} as unknown as CommandInteraction;
const context: ICommandContext = {
name: 'warn',
args: ['warn', 'Test', 'Reason'],
message: message
};
SettingsHelper.GetSetting = jest.fn().mockResolvedValue("mod-logs");
const warn = new Warn();
const result = await warn.execute(context);
Audit.prototype.Save = jest.fn().mockImplementation((_, audit: Audit) => {
savedAudit = audit;
});
expect(messageChannelSend).toBeCalledTimes(1);
expect(logChannel.send).toBeCalledTimes(1);
});
// Act
const command = new Warn();
await command.execute(interaction);
test('Given user has permissions, expect embeds to be correct', async () => {
process.env = {
ROLES_MODERATOR: 'Moderator',
CHANNELS_LOGS_MOD: 'mod-logs'
};
// Assert
expect(interaction.reply).toHaveBeenCalledTimes(1);
expect(interaction.reply).toHaveBeenCalledWith("Successfully warned user.");
const mentionedUser = {
displayAvatarURL: jest.fn(),
tag: 'USERTAG'
} as unknown as User;
const mentionedMember = {
warnnable: true,
warn: jest.fn()
} as unknown as GuildMember;
const logChannel = {
name: 'mod-logs',
send: jest.fn()
} as unknown as TextChannel;
expect(interaction.options.get).toHaveBeenCalledTimes(2);
expect(interaction.options.get).toHaveBeenCalledWith("target", true);
expect(interaction.options.get).toHaveBeenCalledWith("reason");
const messageChannelSend = jest.fn();
const messageMentionsUsersFirst = jest.fn()
.mockReturnValue(mentionedUser);
const messageGuildMember = jest.fn()
.mockReturnValue(mentionedMember);
const messageGuildChannelsCacheFind = jest.fn()
.mockImplementation((callback): TextChannel | undefined => {
const result = callback(logChannel);
if (!result) {
return undefined;
}
return logChannel;
});
const message = {
channel: {
send: messageChannelSend
},
mentions: {
users: {
first: messageMentionsUsersFirst
}
},
guild: {
member: messageGuildMember ,
channels: {
cache: {
find: messageGuildChannelsCacheFind
}
},
available: true
},
author: {
tag: 'AUTHORTAG'
}
} as unknown as Message;
const context: ICommandContext = {
name: 'warn',
args: ['warn', 'Test', 'Reason'],
message: message
};
const warn = new Warn();
const result = await warn.execute(context);
expect(result.embeds.length).toBe(2);
const logEmbed = result.embeds[0];
const publicEmbed = result.embeds[1];
expect(logEmbed.title).toBe('Member Warned');
expect(publicEmbed.title).toBe("");
expect(publicEmbed.description).toBe('[object Object] has been warned');
expect(logEmbed.fields.length).toBe(3);
expect(publicEmbed.fields.length).toBe(1);
expect(publicEmbed.fields[0].name).toBe('Reason');
expect(publicEmbed.fields[0].value).toBe('Test Reason');
});
test('Given user has permission, expect logEmbed fields to be correct', async () => {
process.env = {
ROLES_MODERATOR: 'Moderator',
CHANNELS_LOGS_MOD: 'mod-logs'
};
const mentionedUser = {
displayAvatarURL: jest.fn().mockReturnValue('URL'),
tag: 'USERTAG'
} as unknown as User;
const mentionedMember = {
warnnable: true,
warn: jest.fn()
} as unknown as GuildMember;
const logChannel = {
name: 'mod-logs',
send: jest.fn()
} as unknown as TextChannel;
const messageChannelSend = jest.fn();
const messageMentionsUsersFirst = jest.fn()
.mockReturnValue(mentionedUser);
const messageGuildMember = jest.fn()
.mockReturnValue(mentionedMember);
const messageGuildChannelsCacheFind = jest.fn()
.mockImplementation((callback): TextChannel | undefined => {
const result = callback(logChannel);
if (!result) {
return undefined;
}
return logChannel;
});
const message = {
channel: {
send: messageChannelSend
},
mentions: {
users: {
first: messageMentionsUsersFirst
}
},
guild: {
member: messageGuildMember ,
channels: {
cache: {
find: messageGuildChannelsCacheFind
}
},
available: true
},
author: {
tag: 'AUTHORTAG'
}
} as unknown as Message;
const context: ICommandContext = {
name: 'warn',
args: ['warn', 'Test', 'Reason'],
message: message
};
const warn = new Warn();
const result = await warn.execute(context);
const logEmbed = result.embeds[0];
const fieldUser = logEmbed.fields[0];
const fieldModerator = logEmbed.fields[1];
const fieldReason = logEmbed.fields[2];
expect(fieldUser.name).toBe("User");
expect(fieldUser.value).toBe("[object Object] `USERTAG`");
expect(logEmbed.thumbnail?.url).toBe("URL");
expect(fieldModerator.name).toBe('Moderator');
expect(fieldModerator.value).toBe('[object Object] `AUTHORTAG`');
expect(fieldReason.name).toBe('Reason');
expect(fieldReason.value).toBe('Test Reason');
});
test('Given user is not mentioned, expect error embed to be sent', async () => {
process.env = {
ROLES_MODERATOR: 'Moderator',
CHANNELS_LOGS_MOD: 'mod-logs'
};
const mentionedMember = {
warnnable: true,
warn: jest.fn()
} as unknown as GuildMember;
const logChannel = {
name: 'mod-logs',
send: jest.fn()
} as unknown as TextChannel;
const messageChannelSend = jest.fn();
const messageMentionsUsersFirst = jest.fn()
.mockReturnValue(null);
const messageGuildMember = jest.fn()
.mockReturnValue(mentionedMember);
const messageGuildChannelsCacheFind = jest.fn()
.mockImplementation((callback): TextChannel | undefined => {
const result = callback(logChannel);
if (!result) {
return undefined;
}
return logChannel;
});
const message = {
channel: {
send: messageChannelSend
},
mentions: {
users: {
first: messageMentionsUsersFirst
}
},
guild: {
member: messageGuildMember ,
channels: {
cache: {
find: messageGuildChannelsCacheFind
}
},
available: true
},
author: {
tag: 'AUTHORTAG'
}
} as unknown as Message;
const context: ICommandContext = {
name: 'warn',
args: ['warn', 'Test', 'Reason'],
message: message
};
const warn = new Warn();
const result = await warn.execute(context);
expect(interaction.guild!.channels.cache.find).toHaveBeenCalledTimes(1);
expect(messageChannelSend).toBeCalledTimes(1);
expect(logChannel.send).not.toBeCalled();
expect(SettingsHelper.GetSetting).toHaveBeenCalledTimes(1);
expect(SettingsHelper.GetSetting).toHaveBeenCalledWith("channels.logs.mod", "guildId");
expect(result.embeds.length).toBe(1);
expect(targetUser.user.avatarURL).toHaveBeenCalledTimes(1);
const embedError = result.embeds[0];
expect(logChannel.send).toHaveBeenCalledTimes(1);
expect(embedError.description).toBe('User does not exist');
expect(sentEmbeds).toBeDefined();
expect(sentEmbeds).toMatchSnapshot("sentEmbeds");
expect(Audit.prototype.Save).toHaveBeenCalledWith(Audit, expect.any(Audit));
expect(savedAudit).toMatchSnapshot({
Id: expect.any(String),
AuditId: expect.any(String),
WhenCreated: expect.any(Date),
WhenUpdated: expect.any(Date)
}, "savedAudit");
});
test('Given member is not in server, expect error embed to be sent', async () => {
process.env = {
ROLES_MODERATOR: 'Moderator',
CHANNELS_LOGS_MOD: 'mod-logs'
test("GIVEN interaction.guild is null, EXPECT nothing to happen", async () => {
let sentEmbeds: EmbedBuilder[] | undefined;
let savedAudit: Audit | undefined;
// Arrange
const targetUser = {
user: {
id: "userId",
tag: "userTag",
avatarURL: jest.fn().mockReturnValue("https://google.com/avatar.png"),
},
member: {},
};
const reason = {
value: "Test reason",
};
const mentionedUser = {
displayAvatarURL: jest.fn(),
tag: 'USERTAG'
} as unknown as User;
const mentionedMember = {
warnnable: true,
warn: jest.fn()
} as unknown as GuildMember;
const logChannel = {
name: 'mod-logs',
send: jest.fn()
send: jest.fn().mockImplementation((opts: any) => {
sentEmbeds = opts.embeds;
}),
} as unknown as TextChannel;
const messageChannelSend = jest.fn();
const messageMentionsUsersFirst = jest.fn()
.mockReturnValue(mentionedUser);
const messageGuildMember = jest.fn()
.mockReturnValue(null);
const messageGuildChannelsCacheFind = jest.fn()
.mockImplementation((callback): TextChannel | undefined => {
const result = callback(logChannel);
if (!result) {
return undefined;
}
return logChannel;
});
const message = {
channel: {
send: messageChannelSend
const interaction = {
reply: jest.fn(),
options: {
get: jest.fn()
.mockReturnValueOnce(targetUser)
.mockReturnValue(reason),
},
mentions: {
users: {
first: messageMentionsUsersFirst
}
guildId: "guildId",
user: {
id: "moderatorId",
},
guild: {
member: messageGuildMember ,
channels: {
cache: {
find: messageGuildChannelsCacheFind
}
},
available: true
},
author: {
tag: 'AUTHORTAG'
}
} as unknown as Message;
} as unknown as CommandInteraction;
const context: ICommandContext = {
name: 'warn',
args: ['warn', 'Test', 'Reason'],
message: message
};
SettingsHelper.GetSetting = jest.fn().mockResolvedValue("mod-logs");
const warn = new Warn();
const result = await warn.execute(context);
Audit.prototype.Save = jest.fn().mockImplementation((_, audit: Audit) => {
savedAudit = audit;
});
expect(messageChannelSend).toBeCalledTimes(1);
expect(logChannel.send).not.toBeCalled();
// Act
const command = new Warn();
await command.execute(interaction);
expect(result.embeds.length).toBe(1);
const embedError = result.embeds[0];
expect(embedError.description).toBe('User is not in this server');
// Assert
expect(interaction.reply).not.toHaveBeenCalled();
expect(Audit.prototype.Save).not.toHaveBeenCalled();
});
test('Given guild is unavailable, expect return and do nothing', async () => {
process.env = {
ROLES_MODERATOR: 'Moderator',
CHANNELS_LOGS_MOD: 'mod-logs'
test("GIVEN interaction.guildId is null, EXPECT nothing to happen", async () => {
let sentEmbeds: EmbedBuilder[] | undefined;
let savedAudit: Audit | undefined;
// Arrange
const targetUser = {
user: {
id: "userId",
tag: "userTag",
avatarURL: jest.fn().mockReturnValue("https://google.com/avatar.png"),
},
member: {},
};
const reason = {
value: "Test reason",
};
const mentionedUser = {
displayAvatarURL: jest.fn(),
tag: 'USERTAG'
} as unknown as User;
const mentionedMember = {
warnnable: true,
warn: jest.fn()
} as unknown as GuildMember;
const logChannel = {
name: 'mod-logs',
send: jest.fn()
send: jest.fn().mockImplementation((opts: any) => {
sentEmbeds = opts.embeds;
}),
} as unknown as TextChannel;
const messageChannelSend = jest.fn();
const messageMentionsUsersFirst = jest.fn()
.mockReturnValue(mentionedUser);
const messageGuildMember = jest.fn()
.mockReturnValue(mentionedMember);
const messageGuildChannelsCacheFind = jest.fn()
.mockImplementation((callback): TextChannel | undefined => {
const result = callback(logChannel);
if (!result) {
return undefined;
}
return logChannel;
});
const message = {
channel: {
send: messageChannelSend
},
mentions: {
users: {
first: messageMentionsUsersFirst
}
const interaction = {
reply: jest.fn(),
options: {
get: jest.fn()
.mockReturnValueOnce(targetUser)
.mockReturnValue(reason),
},
guild: {
member: messageGuildMember ,
channels: {
cache: {
find: messageGuildChannelsCacheFind
}
find: jest.fn().mockReturnValue(logChannel),
},
},
available: false
},
author: {
tag: 'AUTHORTAG'
}
} as unknown as Message;
user: {
id: "moderatorId",
},
} as unknown as CommandInteraction;
const context: ICommandContext = {
name: 'warn',
args: ['warn', 'Test', 'Reason'],
message: message
SettingsHelper.GetSetting = jest.fn().mockResolvedValue("mod-logs");
Audit.prototype.Save = jest.fn().mockImplementation((_, audit: Audit) => {
savedAudit = audit;
});
// Act
const command = new Warn();
await command.execute(interaction);
// Assert
expect(interaction.reply).not.toHaveBeenCalled();
expect(Audit.prototype.Save).not.toHaveBeenCalled();
});
test("GIVEN reasonInput is null, EXPECT reason to be defaulted", async () => {
let sentEmbeds: EmbedBuilder[] | undefined;
let savedAudit: Audit | undefined;
// Arrange
const targetUser = {
user: {
id: "userId",
tag: "userTag",
avatarURL: jest.fn().mockReturnValue("https://google.com/avatar.png"),
},
member: {},
};
const warn = new Warn();
const reason = {
value: "Test reason",
};
const result = await warn.execute(context);
const logChannel = {
send: jest.fn().mockImplementation((opts: any) => {
sentEmbeds = opts.embeds;
}),
} as unknown as TextChannel;
const interaction = {
reply: jest.fn(),
options: {
get: jest.fn()
.mockReturnValueOnce(targetUser)
.mockReturnValue(null),
},
guild: {
channels: {
cache: {
find: jest.fn().mockReturnValue(logChannel),
},
},
},
guildId: "guildId",
user: {
id: "moderatorId",
},
} as unknown as CommandInteraction;
SettingsHelper.GetSetting = jest.fn().mockResolvedValue("mod-logs");
Audit.prototype.Save = jest.fn().mockImplementation((_, audit: Audit) => {
savedAudit = audit;
});
expect(messageChannelSend).not.toBeCalled();
expect(logChannel.send).not.toBeCalled();
expect(result.embeds.length).toBe(0);
// Act
const command = new Warn();
await command.execute(interaction);
// Assert
expect(sentEmbeds).toBeDefined();
expect(sentEmbeds![0].data.fields).toBeDefined();
expect(sentEmbeds![0].data.fields!.length).toBe(2);
const logEmbedReasonField = sentEmbeds![0].data.fields!.find(x => x.name == "Reason");
expect(logEmbedReasonField).toBeDefined();
expect(logEmbedReasonField!.value).toBe("*none*");
});
});
test("GIVEN reasonInput.value is undefined, EXPECT reason to be defaulted", async () => {
let sentEmbeds: EmbedBuilder[] | undefined;
let savedAudit: Audit | undefined;
// Arrange
const targetUser = {
user: {
id: "userId",
tag: "userTag",
avatarURL: jest.fn().mockReturnValue("https://google.com/avatar.png"),
},
member: {},
};
const reason = {
value: undefined,
};
const logChannel = {
send: jest.fn().mockImplementation((opts: any) => {
sentEmbeds = opts.embeds;
}),
} as unknown as TextChannel;
const interaction = {
reply: jest.fn(),
options: {
get: jest.fn()
.mockReturnValueOnce(targetUser)
.mockReturnValue(reason),
},
guild: {
channels: {
cache: {
find: jest.fn().mockReturnValue(logChannel),
},
},
},
guildId: "guildId",
user: {
id: "moderatorId",
},
} as unknown as CommandInteraction;
SettingsHelper.GetSetting = jest.fn().mockResolvedValue("mod-logs");
Audit.prototype.Save = jest.fn().mockImplementation((_, audit: Audit) => {
savedAudit = audit;
});
// Act
const command = new Warn();
await command.execute(interaction);
// Assert
expect(sentEmbeds).toBeDefined();
expect(sentEmbeds![0].data.fields).toBeDefined();
expect(sentEmbeds![0].data.fields!.length).toBe(2);
const logEmbedReasonField = sentEmbeds![0].data.fields!.find(x => x.name == "Reason");
expect(logEmbedReasonField).toBeDefined();
expect(logEmbedReasonField!.value).toBe("*none*");
});
test("GIVEN channels.logs.mod setting is not found, EXPECT command to return", async () => {
let sentEmbeds: EmbedBuilder[] | undefined;
let savedAudit: Audit | undefined;
// Arrange
const targetUser = {
user: {
id: "userId",
tag: "userTag",
avatarURL: jest.fn().mockReturnValue("https://google.com/avatar.png"),
},
member: {},
};
const reason = {
value: "Test reason",
};
const logChannel = {
send: jest.fn().mockImplementation((opts: any) => {
sentEmbeds = opts.embeds;
}),
} as unknown as TextChannel;
const interaction = {
reply: jest.fn(),
options: {
get: jest.fn()
.mockReturnValueOnce(targetUser)
.mockReturnValue(reason),
},
guild: {
channels: {
cache: {
find: jest.fn().mockReturnValue(logChannel),
},
},
},
guildId: "guildId",
user: {
id: "moderatorId",
},
} as unknown as CommandInteraction;
SettingsHelper.GetSetting = jest.fn().mockResolvedValue(undefined);
Audit.prototype.Save = jest.fn().mockImplementation((_, audit: Audit) => {
savedAudit = audit;
});
// Act
const command = new Warn();
await command.execute(interaction);
// Assert
expect(interaction.reply).toHaveBeenCalledTimes(1);
expect(interaction.reply).toHaveBeenCalledWith("Successfully warned user.");
expect(Audit.prototype.Save).toHaveBeenCalledTimes(1);
expect(logChannel.send).not.toHaveBeenCalled();
});
test("GIVEN channel is not found, EXPECT logEmbed to not be sent", async () => {
let sentEmbeds: EmbedBuilder[] | undefined;
let savedAudit: Audit | undefined;
// Arrange
const targetUser = {
user: {
id: "userId",
tag: "userTag",
avatarURL: jest.fn().mockReturnValue("https://google.com/avatar.png"),
},
member: {},
};
const reason = {
value: "Test reason",
};
const logChannel = {
send: jest.fn().mockImplementation((opts: any) => {
sentEmbeds = opts.embeds;
}),
} as unknown as TextChannel;
const interaction = {
reply: jest.fn(),
options: {
get: jest.fn()
.mockReturnValueOnce(targetUser)
.mockReturnValue(reason),
},
guild: {
channels: {
cache: {
find: jest.fn().mockReturnValue(undefined),
},
},
},
guildId: "guildId",
user: {
id: "moderatorId",
},
} as unknown as CommandInteraction;
SettingsHelper.GetSetting = jest.fn().mockResolvedValue("mod-logs");
Audit.prototype.Save = jest.fn().mockImplementation((_, audit: Audit) => {
savedAudit = audit;
});
// Act
const command = new Warn();
await command.execute(interaction);
// Assert
expect(interaction.reply).toHaveBeenCalledTimes(1);
expect(interaction.reply).toHaveBeenCalledWith("Successfully warned user.");
expect(Audit.prototype.Save).toHaveBeenCalledTimes(1);
expect(interaction.guild!.channels.cache.find).toHaveBeenCalledTimes(1);
expect(logChannel.send).not.toHaveBeenCalled();
});
});

View file

@ -0,0 +1,53 @@
import BaseEntity from "../../src/contracts/BaseEntity";
import uuid from "uuid";
jest.mock("uuid", () => {
return {
v4: () => "uuidv4",
};
});
jest.useFakeTimers();
describe('constructor', () => {
test("EXPECT properties to be set", () => {
// Arrange
const systemTime = new Date("2024-06-29T00:00:00.000Z");
jest.setSystemTime(systemTime);
// Act
const entity = new BaseEntity();
// Assert
expect(entity.Id).toBe("uuidv4");
expect(entity.WhenCreated).toStrictEqual(systemTime);
expect(entity.WhenUpdated).toStrictEqual(systemTime);
});
});
describe("Save", () => {
test.todo("EXPECT entity to be saved");
});
describe("Remove", () => {
test.todo("EXPECT entity to be removed");
});
describe("FetchAll", () => {
test.todo("EXPECT all entities to be returned");
test.todo("GIVEN relations parameter is not supplied, EXPECT relations to be defaulted");
});
describe("FetchOneById", () => {
test.todo("EXPECT one entity to be returned");
test.todo("GIVEN relations parameter is not supplied, EXPECT relations to be defaulted");
});
describe("Any", () => {
test.todo("GIVEN at least 1 entity is found, EXPECT true to be returned");
test.todo("GIVEN no entities are found, EXPECT false to be returned");
});

View file

@ -0,0 +1,13 @@
describe("constructor", () => {
test.todo("EXPECT properties to be set");
});
describe("MarkAsUsed", () => {
test.todo("EXPECT LastUsed to be updated");
});
describe("FetchOneByChannelId", () => {
test.todo("EXPECT entity with channel id to be returned");
test.todo("GIVEN relations parameter is not supplied, EXPECT relations to be defaulted");
});

View file

@ -0,0 +1,11 @@
describe("constructor", () => {
test.todo("EXPECT properties to be set");
});
describe("FetchAuditsByUserId", () => {
test.todo("EXPECT all entities with the user id and server id to be returned");
});
describe("FetchAuditByAuditId", () => {
test.todo("EXPECT audit with audit id to be returned");
});

View file

@ -0,0 +1,7 @@
describe("constructor", () => {
test.todo("EXPECT properties to be defined");
});
describe("IsChannelIgnored", () => {
test.todo("EXPECT entity with channel id to be returned");
});

View file

@ -0,0 +1,13 @@
describe("constructor", () => {
test.todo("EXPECT properties to be set");
});
describe("FetchOneByRoleId", () => {
test.todo("EXPECT entity with role id to be returned");
test.todo("GIVEN relations parameter is not supplied, EXPECT relations to be defaulted");
});
describe("FetchAllByServerId", () => {
test.todo("EXPECT all entities with server id to be returned");
});

View file

@ -0,0 +1,11 @@
describe("constructor", () => {
test.todo("EXPECT properties to be set");
});
describe("AddSettingToServer", () => {
test.todo("EXPECT setting to be pushed to server relationship");
});
describe("AddRoleToServer", () => {
test.todo("EXPECT role to be pushed to server relationship");
});

View file

@ -0,0 +1,13 @@
describe("constructor", () => {
test.todo("EXPECT properties to be set");
});
describe("UpdateBasicDetails", () => {
test.todo("EXPECT properties to be updated");
});
describe("FetchOneByKey", () => {
test.todo("EXPECT setting with key to be returned");
test.todo("GIVEN relations parameter is not supplied, EXPECT relations to be defaulted");
});

View file

@ -1,164 +0,0 @@
import { GuildMember, TextChannel, User } from "discord.js";
import MemberEvents from "../../src/events/MemberEvents";
import GuildMemberUpdate from "../../src/events/MemberEvents/GuildMemberUpdate";
describe('GuildMemberAdd', () => {
test('When event is fired, expect embed to be sent to logs channel', async () => {
const currentDate = new Date();
const textChannel = {
send: jest.fn()
} as unknown as TextChannel;
const memberGuildChannelsCacheFind = jest.fn()
.mockReturnValue(textChannel);
const userDisplayAvatarURL = jest.fn();
const guildUser = {
tag: 'USERTAG',
createdAt: currentDate,
id: 'USERID',
displayAvatarURL: userDisplayAvatarURL
} as unknown as User;
const guildMember = {
user: guildUser,
guild: {
channels: {
cache: {
find: memberGuildChannelsCacheFind
}
}
}
} as unknown as GuildMember;
const memberEvents = new MemberEvents();
const result = await memberEvents.guildMemberAdd(guildMember);
expect(textChannel.send).toBeCalledTimes(1);
expect(userDisplayAvatarURL).toBeCalledTimes(1);
expect(result.embeds.length).toBe(1);
// Embed
const embed = result.embeds[0];
expect(embed.title).toBe("Member Joined");
expect(embed.footer?.text).toBe("Id: USERID");
expect(embed.fields.length).toBe(2);
// Embed -> User Field
const embedFieldUser = embed.fields[0];
expect(embedFieldUser.name).toBe("User");
expect(embedFieldUser.value).toBe("[object Object] `USERTAG`");
expect(embedFieldUser.inline).toBeTruthy();
// Embed -> Created Field
const embedFieldCreated = embed.fields[1];
expect(embedFieldCreated.name).toBe("Created");
expect(embedFieldCreated.value).toBe(currentDate.toString());
});
});
describe('GuildMemberRemove', () => {
test('When event is fired, expect embed to be sent to logs channel', async () => {
const currentDate = new Date();
const textChannel = {
send: jest.fn()
} as unknown as TextChannel;
const memberGuildChannelsCacheFind = jest.fn()
.mockReturnValue(textChannel);
const userDisplayAvatarURL = jest.fn();
const guildUser = {
tag: 'USERTAG',
createdAt: currentDate,
id: 'USERID',
displayAvatarURL: userDisplayAvatarURL
} as unknown as User;
const guildMember = {
user: guildUser,
guild: {
channels: {
cache: {
find: memberGuildChannelsCacheFind
}
}
},
joinedAt: currentDate
} as unknown as GuildMember;
const memberEvents = new MemberEvents();
const result = await memberEvents.guildMemberRemove(guildMember);
expect(textChannel.send).toBeCalledTimes(1);
expect(userDisplayAvatarURL).toBeCalledTimes(1);
expect(result.embeds.length).toBe(1);
// Embed
const embed = result.embeds[0];
expect(embed.title).toBe("Member Left");
expect(embed.footer?.text).toBe("Id: USERID");
expect(embed.fields.length).toBe(2);
// Embed -> User Field
const embedFieldUser = embed.fields[0];
expect(embedFieldUser.name).toBe("User");
expect(embedFieldUser.value).toBe("[object Object] `USERTAG`");
expect(embedFieldUser.inline).toBeTruthy();
// Embed -> Joined Field
const embedFieldJoined = embed.fields[1];
expect(embedFieldJoined.name).toBe("Joined");
expect(embedFieldJoined.value).toBe(currentDate.toString());
});
});
describe('GuildMemberUpdate', () => {
test('Given nicknames are the same, expect NicknameChanged NOT to be called', async () => {
const member = {
nickname: 'member'
} as unknown as GuildMember;
const nicknameChanged = jest.fn();
GuildMemberUpdate.prototype.NicknameChanged = nicknameChanged;
const memberEvents = new MemberEvents();
const result = await memberEvents.guildMemberUpdate(member, member);
expect(result.embeds.length).toBe(0);
expect(nicknameChanged).not.toBeCalled();
});
test('Given nicknames are the different, expect NicknameChanged to be called', async () => {
const oldMember = {
nickname: 'oldMember'
} as unknown as GuildMember;
const newMember = {
nickname: 'newMember'
} as unknown as GuildMember;
const nicknameChanged = jest.fn();
GuildMemberUpdate.prototype.NicknameChanged = nicknameChanged;
const memberEvents = new MemberEvents();
const result = await memberEvents.guildMemberUpdate(oldMember, newMember);
expect(result.embeds.length).toBe(0);
expect(nicknameChanged).toBeCalledTimes(1);
});
});

View file

@ -0,0 +1,13 @@
describe('GuildMemberAdd', () => {
test.todo('EXPECT embed to be logged');
test.todo("GIVEN member.guild is null, EXPECT nothing to happen");
test.todo("GIVEN event.member.add.enabled setting is not found, EXPECT nothing to happen");
test.todo("GIVEN event is not enabled, EXPECT nothing to happen");
test.todo("GIVEN event.member.add.channel setting is not found, EXPECT nothing to happen");
test.todo("GIVEN channel is not found, EXPECT nothing to happen");
});

View file

@ -0,0 +1,15 @@
describe('GuildMemberRemove', () => {
test.todo("EXPECT embed to be logged");
test.todo("GIVEN member.guild is null, EXPECT nothing to happen");
test.todo("GIVEN event.member.remove.enabled setting is not found, EXPECT nothing to happen");
test.todo("GIVEN event is not enabled, EXPECT nothing to happen");
test.todo("GIVEN member.joinedAt is null, EXPECT value to be defaulted");
test.todo("GIVEN event.member.remove.channel setting is not found, EXPECT nothing to happen");
test.todo("GIVEN channel is not found, EXPECT nothing to happen");
});

View file

@ -1,235 +1,5 @@
import { GuildMember, TextChannel } from "discord.js";
import GuildMemberUpdate from "../../../src/events/MemberEvents/GuildMemberUpdate";
describe('GuildMemberUpdate', () => {
test.todo("GIVEN user nickname has changed, EXPECT function to be executed");
beforeEach(() => {
process.env = {};
});
describe('Constructor', () => {
test('Expect properties are set', () => {
const oldMember = {
nickname: 'Old Nickname'
} as unknown as GuildMember;
const newMember = {
nickname: 'New Nickname'
} as unknown as GuildMember;
const guildMemberUpdate = new GuildMemberUpdate(oldMember, newMember);
expect(guildMemberUpdate.oldMember).toBe(oldMember);
expect(guildMemberUpdate.newMember).toBe(newMember);
});
});
describe('NicknameChanged', () => {
test('Given nickname has changed from one to another, expect embed to be sent with both', async () => {
process.env = {
CHANNELS_LOGS_MOD: 'mod-logs'
}
const channelSend = jest.fn();
const textChannel = {
name: 'mod-logs',
send: channelSend
} as unknown as TextChannel;
const memberGuildChannelsCacheFind = jest.fn()
.mockReturnValue(textChannel);
const memberUserDisplayAvatarURL = jest.fn();
const oldMember = {
nickname: 'Old Nickname'
} as unknown as GuildMember;
const newMember = {
nickname: 'New Nickname',
guild: {
channels: {
cache: {
find: memberGuildChannelsCacheFind
}
}
},
user: {
tag: 'USERTAG',
id: 'USERID',
displayAvatarURL: memberUserDisplayAvatarURL
}
} as unknown as GuildMember;
const guildMemberUpdate = new GuildMemberUpdate(oldMember, newMember);
const result = await guildMemberUpdate.NicknameChanged();
expect(channelSend).toBeCalledTimes(1);
expect(memberGuildChannelsCacheFind).toBeCalledTimes(1);
expect(memberUserDisplayAvatarURL).toBeCalledTimes(1);
expect(result.embeds.length).toBe(1);
// Embed
const embed = result.embeds[0];
expect(embed.title).toBe('Nickname Changed');
expect(embed.footer?.text).toBe('Id: USERID');
expect(embed.fields.length).toBe(3);
// Embed -> User Field
const embedFieldUser = embed.fields[0];
expect(embedFieldUser.name).toBe('User');
expect(embedFieldUser.value).toBe('[object Object] `USERTAG`');
// Embed -> Before Field
const embedFieldBefore = embed.fields[1];
expect(embedFieldBefore.name).toBe('Before');
expect(embedFieldBefore.value).toBe('Old Nickname');
// Embed -> After Field
const embedFieldAfter = embed.fields[2];
expect(embedFieldAfter.name).toBe('After');
expect(embedFieldAfter.value).toBe('New Nickname');
});
test('Given old nickname was null, expect embed to say old nickname was none', async () => {
process.env = {
CHANNELS_LOGS_MOD: 'mod-logs'
}
const channelSend = jest.fn();
const textChannel = {
name: 'mod-logs',
send: channelSend
} as unknown as TextChannel;
const memberGuildChannelsCacheFind = jest.fn()
.mockReturnValue(textChannel);
const memberUserDisplayAvatarURL = jest.fn();
const oldMember = {} as unknown as GuildMember;
const newMember = {
nickname: 'New Nickname',
guild: {
channels: {
cache: {
find: memberGuildChannelsCacheFind
}
}
},
user: {
tag: 'USERTAG',
id: 'USERID',
displayAvatarURL: memberUserDisplayAvatarURL
}
} as unknown as GuildMember;
const guildMemberUpdate = new GuildMemberUpdate(oldMember, newMember);
const result = await guildMemberUpdate.NicknameChanged();
expect(channelSend).toBeCalledTimes(1);
expect(memberGuildChannelsCacheFind).toBeCalledTimes(1);
expect(memberUserDisplayAvatarURL).toBeCalledTimes(1);
expect(result.embeds.length).toBe(1);
// Embed
const embed = result.embeds[0];
expect(embed.title).toBe('Nickname Changed');
expect(embed.footer?.text).toBe('Id: USERID');
expect(embed.fields.length).toBe(3);
// Embed -> User Field
const embedFieldUser = embed.fields[0];
expect(embedFieldUser.name).toBe('User');
expect(embedFieldUser.value).toBe('[object Object] `USERTAG`');
// Embed -> Before Field
const embedFieldBefore = embed.fields[1];
expect(embedFieldBefore.name).toBe('Before');
expect(embedFieldBefore.value).toBe('*none*');
// Embed -> After Field
const embedFieldAfter = embed.fields[2];
expect(embedFieldAfter.name).toBe('After');
expect(embedFieldAfter.value).toBe('New Nickname');
});
test('Given new nickname was null, expect embed to say new nickname was none', async () => {
process.env = {
CHANNELS_LOGS_MOD: 'mod-logs'
}
const channelSend = jest.fn();
const textChannel = {
name: 'mod-logs',
send: channelSend
} as unknown as TextChannel;
const memberGuildChannelsCacheFind = jest.fn()
.mockReturnValue(textChannel);
const memberUserDisplayAvatarURL = jest.fn();
const oldMember = {
nickname: 'Old Nickname'
} as unknown as GuildMember;
const newMember = {
guild: {
channels: {
cache: {
find: memberGuildChannelsCacheFind
}
}
},
user: {
tag: 'USERTAG',
id: 'USERID',
displayAvatarURL: memberUserDisplayAvatarURL
}
} as unknown as GuildMember;
const guildMemberUpdate = new GuildMemberUpdate(oldMember, newMember);
const result = await guildMemberUpdate.NicknameChanged();
expect(channelSend).toBeCalledTimes(1);
expect(memberGuildChannelsCacheFind).toBeCalledTimes(1);
expect(memberUserDisplayAvatarURL).toBeCalledTimes(1);
expect(result.embeds.length).toBe(1);
// Embed
const embed = result.embeds[0];
expect(embed.title).toBe('Nickname Changed');
expect(embed.footer?.text).toBe('Id: USERID');
expect(embed.fields.length).toBe(3);
// Embed -> User Field
const embedFieldUser = embed.fields[0];
expect(embedFieldUser.name).toBe('User');
expect(embedFieldUser.value).toBe('[object Object] `USERTAG`');
// Embed -> Before Field
const embedFieldBefore = embed.fields[1];
expect(embedFieldBefore.name).toBe('Before');
expect(embedFieldBefore.value).toBe('Old Nickname');
// Embed -> After Field
const embedFieldAfter = embed.fields[2];
expect(embedFieldAfter.name).toBe('After');
expect(embedFieldAfter.value).toBe('*none*');
});
test.todo("GIVEN nickname is the same, EXPECT nothing to happen");
});

View file

@ -0,0 +1,15 @@
describe('NicknameChanged', () => {
test.todo('EXPECT embed to be logged');
test.todo("GIVEN event.member.update.enabled setting is not found, EXPECT nothing to happen");
test.todo("GIVEN event is not enabled, EXPECT nothing to happen");
test.todo("GIVEN oldNickname is null, EXPECT oldNickname to be defaulted");
test.todo("GIVEN newNickname is null, EXPECT newNickname to be defaulted");
test.todo("GIVEN event.member.update.channel setting is not found, EXPECT nothing to happen");
test.todo("GIVEN log channel can not be found, EXPECT nothing to happen");
});

View file

@ -1,648 +0,0 @@
import { Collection, Message, MessageAttachment, TextChannel } from "discord.js";
import MessageEvents from "../../src/events/MessageEvents";
beforeEach(() => {
process.env = {};
});
describe('MessageDelete', () => {
test('Given message was in a guild AND user was NOT a bot, expect message deleted embed to be sent', async () => {
process.env = {
CHANNELS_LOGS_MOD: 'mod-logs'
}
const channelSend = jest.fn();
const textChannel = {
name: 'mod-logs',
send: channelSend
} as unknown as TextChannel;
const memberGuildChannelsCacheFind = jest.fn()
.mockReturnValue(textChannel);
const messageAuthorDisplayAvatarURL = jest.fn();
const messageAttachments = new Collection<string, MessageAttachment>([
[
"0",
{
url: 'image0.png'
} as unknown as MessageAttachment
],
[
"1",
{
url: 'image1.png'
} as unknown as MessageAttachment
]
]);
const message = {
guild: {
channels: {
cache: {
find: memberGuildChannelsCacheFind
}
}
},
author: {
bot: false,
displayAvatarURL: messageAuthorDisplayAvatarURL,
tag: 'USERTAG'
},
channel: {},
content: 'Message Content',
attachments: messageAttachments
} as unknown as Message;
const messageEvents = new MessageEvents();
const result = await messageEvents.messageDelete(message);
expect(channelSend).toBeCalledTimes(1);
expect(memberGuildChannelsCacheFind).toBeCalledTimes(1);
expect(messageAuthorDisplayAvatarURL).toBeCalledTimes(1);
expect(result.embeds.length).toBe(1);
// Embed
const embed = result.embeds[0];
expect(embed.title).toBe('Message Deleted');
expect(embed.fields.length).toBe(4);
// Embed -> User Field
const embedFieldUser = embed.fields[0];
expect(embedFieldUser.name).toBe('User');
expect(embedFieldUser.value).toBe('[object Object] `USERTAG`');
// Embed -> Channel Field
const embedFieldChannel = embed.fields[1];
expect(embedFieldChannel.name).toBe('Channel');
expect(embedFieldChannel.value).toBe('[object Object]');
// Embed -> Content Field
const embedFieldContent = embed.fields[2];
expect(embedFieldContent.name).toBe('Content');
expect(embedFieldContent.value).toBe('```Message Content```');
// Embed -> Attachments Field
const embedFieldAttachments = embed.fields[3];
expect(embedFieldAttachments.name).toBe('Attachments');
expect(embedFieldAttachments.value).toBe('```image0.png\nimage1.png```');
});
test('Given message was not sent in a guild, expect execution stopped', async () => {
process.env = {
CHANNELS_LOGS_MOD: 'mod-logs'
}
const channelSend = jest.fn();
const textChannel = {
name: 'mod-logs',
send: channelSend
} as unknown as TextChannel;
const memberGuildChannelsCacheFind = jest.fn()
.mockReturnValue(textChannel);
const messageAuthorDisplayAvatarURL = jest.fn();
const messageAttachments = new Collection<string, MessageAttachment>([
[
"0",
{
url: 'image0.png'
} as unknown as MessageAttachment
],
[
"1",
{
url: 'image1.png'
} as unknown as MessageAttachment
]
]);
const message = {
author: {
bot: false,
displayAvatarURL: messageAuthorDisplayAvatarURL,
tag: 'USERTAG'
},
channel: {},
content: 'Message Content',
attachments: messageAttachments
} as unknown as Message;
const messageEvents = new MessageEvents();
const result = await messageEvents.messageDelete(message);
expect(channelSend).not.toBeCalled();
expect(memberGuildChannelsCacheFind).not.toBeCalled();
expect(messageAuthorDisplayAvatarURL).not.toBeCalled();
expect(result.embeds.length).toBe(0);
});
test('Given author is a bot, expect execution stopped', async () => {
process.env = {
CHANNELS_LOGS_MOD: 'mod-logs'
}
const channelSend = jest.fn();
const textChannel = {
name: 'mod-logs',
send: channelSend
} as unknown as TextChannel;
const memberGuildChannelsCacheFind = jest.fn()
.mockReturnValue(textChannel);
const messageAuthorDisplayAvatarURL = jest.fn();
const messageAttachments = new Collection<string, MessageAttachment>([
[
"0",
{
url: 'image0.png'
} as unknown as MessageAttachment
],
[
"1",
{
url: 'image1.png'
} as unknown as MessageAttachment
]
]);
const message = {
guild: {
channels: {
cache: {
find: memberGuildChannelsCacheFind
}
}
},
author: {
bot: true,
displayAvatarURL: messageAuthorDisplayAvatarURL,
tag: 'USERTAG'
},
channel: {},
content: 'Message Content',
attachments: messageAttachments
} as unknown as Message;
const messageEvents = new MessageEvents();
const result = await messageEvents.messageDelete(message);
expect(channelSend).not.toBeCalled();
expect(memberGuildChannelsCacheFind).not.toBeCalled();
expect(messageAuthorDisplayAvatarURL).not.toBeCalled();
expect(result.embeds.length).toBe(0);
});
test('Given message does not contain any attachments, expect attachments field to be omitted', async () => {
process.env = {
CHANNELS_LOGS_MOD: 'mod-logs'
}
const channelSend = jest.fn();
const textChannel = {
name: 'mod-logs',
send: channelSend
} as unknown as TextChannel;
const memberGuildChannelsCacheFind = jest.fn()
.mockReturnValue(textChannel);
const messageAuthorDisplayAvatarURL = jest.fn();
const messageAttachments = new Collection<string, MessageAttachment>([]);
const message = {
guild: {
channels: {
cache: {
find: memberGuildChannelsCacheFind
}
}
},
author: {
bot: false,
displayAvatarURL: messageAuthorDisplayAvatarURL,
tag: 'USERTAG'
},
channel: {},
content: 'Message Content',
attachments: messageAttachments
} as unknown as Message;
const messageEvents = new MessageEvents();
const result = await messageEvents.messageDelete(message);
expect(channelSend).toBeCalledTimes(1);
expect(memberGuildChannelsCacheFind).toBeCalledTimes(1);
expect(messageAuthorDisplayAvatarURL).toBeCalledTimes(1);
expect(result.embeds.length).toBe(1);
// Embed
const embed = result.embeds[0];
expect(embed.title).toBe('Message Deleted');
expect(embed.fields.length).toBe(3);
// Embed -> User Field
const embedFieldUser = embed.fields[0];
expect(embedFieldUser.name).toBe('User');
expect(embedFieldUser.value).toBe('[object Object] `USERTAG`');
// Embed -> Channel Field
const embedFieldChannel = embed.fields[1];
expect(embedFieldChannel.name).toBe('Channel');
expect(embedFieldChannel.value).toBe('[object Object]');
// Embed -> Content Field
const embedFieldContent = embed.fields[2];
expect(embedFieldContent.name).toBe('Content');
expect(embedFieldContent.value).toBe('```Message Content```');
});
});
describe('MessageUpdate', () => {
test('Given message is in a guild AND user is not a bot AND the content has actually changed, e xpect log embed to be sent', async () => {
process.env = {
CHANNELS_LOGS_MOD: 'mod-logs'
}
const channelSend = jest.fn();
const textChannel = {
name: 'mod-logs',
send: channelSend
} as unknown as TextChannel;
const memberGuildChannelsCacheFind = jest.fn()
.mockReturnValue(textChannel);
const messageAuthorDisplayAvatarURL = jest.fn();
const oldMessage = {
content: 'Old Message'
} as unknown as Message;
const newMessage = {
guild: {
channels: {
cache: {
find: memberGuildChannelsCacheFind
}
}
},
author: {
bot: false,
displayAvatarURL: messageAuthorDisplayAvatarURL,
tag: 'USERTAG'
},
content: 'New Message',
channel: {},
} as unknown as Message;
const messageEvents = new MessageEvents();
const result = await messageEvents.messageUpdate(oldMessage, newMessage);
expect(channelSend).toBeCalledTimes(1);
expect(memberGuildChannelsCacheFind).toBeCalledTimes(1);
expect(messageAuthorDisplayAvatarURL).toBeCalledTimes(1);
expect(result.embeds.length).toBe(1);
// Embed
const embed = result.embeds[0];
expect(embed.title).toBe('Message Edited');
expect(embed.fields.length).toBe(4);
// Embed -> User Field
const embedFieldUser = embed.fields[0];
expect(embedFieldUser.name).toBe('User');
expect(embedFieldUser.value).toBe('[object Object] `USERTAG`');
expect(embedFieldUser.inline).toBeTruthy();
// Embed -> Channel Field
const embedFieldChannel = embed.fields[1];
expect(embedFieldChannel.name).toBe('Channel');
expect(embedFieldChannel.value).toBe('[object Object]');
expect(embedFieldChannel.inline).toBeTruthy();
// Embed -> Before Field
const embedFieldBefore = embed.fields[2];
expect(embedFieldBefore.name).toBe('Before');
expect(embedFieldBefore.value).toBe('```Old Message```');
// Embed -> After Field
const embedFieldAfter = embed.fields[3];
expect(embedFieldAfter.name).toBe('After');
expect(embedFieldAfter.value).toBe('```New Message```');
});
test('Given message was not in a guild, expect execution stopped', async () => {
process.env = {
CHANNELS_LOGS_MOD: 'mod-logs'
}
const channelSend = jest.fn();
const textChannel = {
name: 'mod-logs',
send: channelSend
} as unknown as TextChannel;
const memberGuildChannelsCacheFind = jest.fn()
.mockReturnValue(textChannel);
const messageAuthorDisplayAvatarURL = jest.fn();
const oldMessage = {
content: 'Old Message'
} as unknown as Message;
const newMessage = {
author: {
bot: false,
displayAvatarURL: messageAuthorDisplayAvatarURL,
tag: 'USERTAG'
},
content: 'New Message',
channel: {},
} as unknown as Message;
const messageEvents = new MessageEvents();
const result = await messageEvents.messageUpdate(oldMessage, newMessage);
expect(channelSend).not.toBeCalled();
expect(memberGuildChannelsCacheFind).not.toBeCalled();
expect(messageAuthorDisplayAvatarURL).not.toBeCalled();
expect(result.embeds.length).toBe(0);
});
test('Given author is a bot, expect execution stopped', async () => {
process.env = {
CHANNELS_LOGS_MOD: 'mod-logs'
}
const channelSend = jest.fn();
const textChannel = {
name: 'mod-logs',
send: channelSend
} as unknown as TextChannel;
const memberGuildChannelsCacheFind = jest.fn()
.mockReturnValue(textChannel);
const messageAuthorDisplayAvatarURL = jest.fn();
const oldMessage = {
content: 'Old Message'
} as unknown as Message;
const newMessage = {
guild: {
channels: {
cache: {
find: memberGuildChannelsCacheFind
}
}
},
author: {
bot: true,
displayAvatarURL: messageAuthorDisplayAvatarURL,
tag: 'USERTAG'
},
content: 'New Message',
channel: {},
} as unknown as Message;
const messageEvents = new MessageEvents();
const result = await messageEvents.messageUpdate(oldMessage, newMessage);
expect(channelSend).not.toBeCalled();
expect(memberGuildChannelsCacheFind).not.toBeCalled();
expect(messageAuthorDisplayAvatarURL).not.toBeCalled();
expect(result.embeds.length).toBe(0);
});
test('Given the message contents are the same, expect execution stopped', async () => {
process.env = {
CHANNELS_LOGS_MOD: 'mod-logs'
}
const channelSend = jest.fn();
const textChannel = {
name: 'mod-logs',
send: channelSend
} as unknown as TextChannel;
const memberGuildChannelsCacheFind = jest.fn()
.mockReturnValue(textChannel);
const messageAuthorDisplayAvatarURL = jest.fn();
const oldMessage = {
content: 'Message'
} as unknown as Message;
const newMessage = {
guild: {
channels: {
cache: {
find: memberGuildChannelsCacheFind
}
}
},
author: {
bot: false,
displayAvatarURL: messageAuthorDisplayAvatarURL,
tag: 'USERTAG'
},
content: 'Message',
channel: {},
} as unknown as Message;
const messageEvents = new MessageEvents();
const result = await messageEvents.messageUpdate(oldMessage, newMessage);
expect(channelSend).not.toBeCalled();
expect(memberGuildChannelsCacheFind).not.toBeCalled();
expect(messageAuthorDisplayAvatarURL).not.toBeCalled();
expect(result.embeds.length).toBe(0);
});
test('Given Old Message did not have a content, expect field to account for this', async () => {
process.env = {
CHANNELS_LOGS_MOD: 'mod-logs'
}
const channelSend = jest.fn();
const textChannel = {
name: 'mod-logs',
send: channelSend
} as unknown as TextChannel;
const memberGuildChannelsCacheFind = jest.fn()
.mockReturnValue(textChannel);
const messageAuthorDisplayAvatarURL = jest.fn();
const oldMessage = {} as unknown as Message;
const newMessage = {
guild: {
channels: {
cache: {
find: memberGuildChannelsCacheFind
}
}
},
author: {
bot: false,
displayAvatarURL: messageAuthorDisplayAvatarURL,
tag: 'USERTAG'
},
content: 'New Message',
channel: {},
} as unknown as Message;
const messageEvents = new MessageEvents();
const result = await messageEvents.messageUpdate(oldMessage, newMessage);
expect(channelSend).toBeCalledTimes(1);
expect(memberGuildChannelsCacheFind).toBeCalledTimes(1);
expect(messageAuthorDisplayAvatarURL).toBeCalledTimes(1);
expect(result.embeds.length).toBe(1);
// Embed
const embed = result.embeds[0];
expect(embed.title).toBe('Message Edited');
expect(embed.fields.length).toBe(4);
// Embed -> User Field
const embedFieldUser = embed.fields[0];
expect(embedFieldUser.name).toBe('User');
expect(embedFieldUser.value).toBe('[object Object] `USERTAG`');
expect(embedFieldUser.inline).toBeTruthy();
// Embed -> Channel Field
const embedFieldChannel = embed.fields[1];
expect(embedFieldChannel.name).toBe('Channel');
expect(embedFieldChannel.value).toBe('[object Object]');
expect(embedFieldChannel.inline).toBeTruthy();
// Embed -> Before Field
const embedFieldBefore = embed.fields[2];
expect(embedFieldBefore.name).toBe('Before');
expect(embedFieldBefore.value).toBe('```*none*```');
// Embed -> After Field
const embedFieldAfter = embed.fields[3];
expect(embedFieldAfter.name).toBe('After');
expect(embedFieldAfter.value).toBe('```New Message```');
});
test('Given New Message does not have a content, expect field to account for this', async () => {
process.env = {
CHANNELS_LOGS_MOD: 'mod-logs'
}
const channelSend = jest.fn();
const textChannel = {
name: 'mod-logs',
send: channelSend
} as unknown as TextChannel;
const memberGuildChannelsCacheFind = jest.fn()
.mockReturnValue(textChannel);
const messageAuthorDisplayAvatarURL = jest.fn();
const oldMessage = {
content: 'Old Message'
} as unknown as Message;
const newMessage = {
guild: {
channels: {
cache: {
find: memberGuildChannelsCacheFind
}
}
},
author: {
bot: false,
displayAvatarURL: messageAuthorDisplayAvatarURL,
tag: 'USERTAG'
},
channel: {},
} as unknown as Message;
const messageEvents = new MessageEvents();
const result = await messageEvents.messageUpdate(oldMessage, newMessage);
expect(channelSend).toBeCalledTimes(1);
expect(memberGuildChannelsCacheFind).toBeCalledTimes(1);
expect(messageAuthorDisplayAvatarURL).toBeCalledTimes(1);
expect(result.embeds.length).toBe(1);
// Embed
const embed = result.embeds[0];
expect(embed.title).toBe('Message Edited');
expect(embed.fields.length).toBe(4);
// Embed -> User Field
const embedFieldUser = embed.fields[0];
expect(embedFieldUser.name).toBe('User');
expect(embedFieldUser.value).toBe('[object Object] `USERTAG`');
expect(embedFieldUser.inline).toBeTruthy();
// Embed -> Channel Field
const embedFieldChannel = embed.fields[1];
expect(embedFieldChannel.name).toBe('Channel');
expect(embedFieldChannel.value).toBe('[object Object]');
expect(embedFieldChannel.inline).toBeTruthy();
// Embed -> Before Field
const embedFieldBefore = embed.fields[2];
expect(embedFieldBefore.name).toBe('Before');
expect(embedFieldBefore.value).toBe('```Old Message```');
// Embed -> After Field
const embedFieldAfter = embed.fields[3];
expect(embedFieldAfter.name).toBe('After');
expect(embedFieldAfter.value).toBe('```*none*```');
});
});

View file

@ -0,0 +1,11 @@
describe('MessageCreate', () => {
test.todo("GIVEN verification is enabled, EXPECT function to execute");
test.todo("GIVEN verification is not enabled, EXPECT nothing to happen");
test.todo("GIVEN message.guild is null, EXPECT nothing to happen");
test.todo("GIVEN message author is a bot, EXPECT nothing to happen");
test.todo("GIVEN verification.enabled is not found, EXPECT nothing to happen");
});

View file

@ -0,0 +1,23 @@
describe("VerificationCheck", () => {
test.todo("GIVEN validation is correct, EXPECT role given");
test.todo("GIVEN validation is incorrect, EXPECT only message to be deleted");
test.todo("GIVEN message.guild is null, EXPECT nothing to happen");
test.todo("GIVEN verification.channel setting is not found, EXPECT nothing to happen");
test.todo("GIVEN channel is not found, EXPECT nothing to happen");
test.todo("GIVEN currentChannel is not found, EXPECT nothing to happen");
test.todo("GIVEN currentChannel is not the verification channel, EXPECT nothing to happen");
test.todo("GIVEN verification.code is null, EXPECT error");
test.todo("GIVEN verification.code is empty, EXPECT error");
test.todo("GIVEN verification.role is null, EXPECT error");
test.todo("GIVEN role is not found, EXPECT error");
});

View file

@ -0,0 +1,21 @@
describe('MessageDelete', () => {
test.todo('EXPECT embed to be logged');
test.todo("GIVEN message.guild is null, EXPECT nothing to happen");
test.todo("GIVEN message author is a bot, EXPECT nothing to happen");
test.todo("GIVEN event.message.delete.enabled setting is not found, EXPECT nothing to happen");
test.todo("GIVEN event is not enabled, EXPECT nothing to happen");
test.todo("GIVEN channel is ignored, EXPECT nothing to happen");
test.todo("GIVEN message content is null, EXPECT content to be defaulted");
test.todo("GIVEN message had at least 1 attachment, EXPECT attachments field to be added");
test.todo("GIVEN event.message.delete.channel setting is not found, EXPECT nothing to happen");
test.todo("GIVEN channel is not found, EXPECT nothing to happen");
});

View file

@ -0,0 +1,23 @@
describe('MessageUpdate', () => {
test.todo("EXPECT embed to be logged");
test.todo("GIVEN newMessage.guild is null, EXPECT nothing to happen");
test.todo("GIVEN message author is a bot, EXPECT nothing to happen");
test.todo("GIVEN message contents are the same, EXPECT nothing to happen");
test.todo("GIVEN event.message.update.enabled setting is not found, EXPECT nothing to happen");
test.todo("GIVEN event is not enabled, EXPECT nothing to happen");
test.todo("GIVEN channel is ignored, EXPECT nothing to happen");
test.todo("GIVEN oldMessage.content is null, EXPECT oldMessage to be defaulted");
test.todo("GIVEN newMessage.content is null, EXPECT newMessage to be defaulted");
test.todo("GIVEN event.message.delete.channel setting is not found, EXPECT nothing to happen");
test.todo("GIVEN channel is not found, EXPECT nothing to happen");
});

View file

@ -0,0 +1,31 @@
describe('TypeToFriendlyText', () => {
test.todo("GIVEN auditType is General, EXPECT General to be returned");
test.todo("GIVEN auditType is Warn, EXPECT Warn to be returned");
test.todo("GIVEN auditType is Mute, EXPECT Mute to be returned");
test.todo("GIVEN auditType is Kick, EXPECT Kick to be returned");
test.todo("GIVEN auditType is Ban, EXPECT Ban to be returned");
test.todo("GIVEN auditType is Timeout, EXPECT Timeout to be returned");
test.todo("GIVEN auditType is none of these, EXPECT Other to be returned");
});
describe('StringToType', () => {
test.todo("GIVEN string is general, EXPECT General to be returned");
test.todo("GIVEN string is warn, EXPECT Warn to be returned");
test.todo("GIVEN string is mute, EXPECT Mute to be returned");
test.todo("GIVEN string is kick, EXPECT Kick to be returned");
test.todo("GIVEN string is ban, EXPECT Ban to be returned");
test.todo("GIVEN string is timeout, EXPECT Timeout to be returned");
test.todo("GIVEN string is none of these, EXPECT General to be returned");
});

View file

@ -0,0 +1,7 @@
describe('Up', () => {
test.todo("EXPECT up query to be executed");
});
describe("Down", () => {
test.todo("EXPECT down query to be executed");
});

View file

@ -0,0 +1,15 @@
describe("GetSetting", () => {
test.todo("EXPECT setting value to be returned");
test.todo("GIVEN server is not set up, EXPECT default value to be returned");
test.todo("GIVEN setting is not set, EXPECT default value to be returned");
});
describe("SetSetting", () => {
test.todo("GIVEN setting is not set, EXPECT value to be added");
test.todo("GIVEN setting is already set, EXPECT value to be updated");
test.todo("GIVEN server is not set up, EXPECT nothing to happen");
});

View file

@ -1,11 +1,15 @@
import StringTools from "../../src/helpers/StringTools";
describe('Capitalise', () => {
test('Expect sentence to be captilised', () => {
const inputString = 'the big brown fox jumps over the lazy dog';
test.todo('EXPECT sentence to be capitalised');
});
const result = StringTools.Capitalise(inputString);
describe('CapitaliseArray', () => {
test.todo("EXPECT words in array to be capitalised");
});
expect(result).toBe('The Big Brown Fox Jumps Over The Lazy Dog');
});
describe("RandomString", () => {
test.todo("EXPECT random string of length to be returned");
});
describe("ReplaceAll", () => {
test.todo("EXPECT all instances of substring to be replaced");
});

View file

@ -0,0 +1,37 @@
describe('constructor', () => {
test.todo("EXPECT properties to be set");
test.todo("GIVEN input has commas, EXPECT these to be removed");
});
describe('GetDays', () => {
test.todo("EXPECT day value to be returned");
});
describe('GetHours', () => {
test.todo("EXPECT hour value to be returned");
});
describe('GetMinutes', () => {
test.todo("EXPECT minute value to be returned");
});
describe('GetSeconds', () => {
test.todo("EXPECT second value to be returned");
});
describe('GetMiliseconds', () => {
test.todo("EXPECT total time returned in miliseconds");
});
describe('GetDateFromNow', () => {
test.todo("EXPECT date returned to be value added to now");
});
describe("GetLength", () => {
test.todo("EXPECT stringified value to be returned");
});
describe("GetLengthShort", () => {
test.todo("EXPECT stringified value to be returned");
});

View file

@ -1,57 +0,0 @@
import { Message } from "discord.js";
import { ICommandContext } from "../../../src/contracts/ICommandContext";
import ErrorEmbed from "../../../src/helpers/embeds/ErrorEmbed";
beforeEach(() => {
process.env = {};
});
describe('Constructor', () => {
test('Expect properties to be set', () => {
process.env = {
EMBED_COLOUR_ERROR: '0xd52803'
}
const message = {} as unknown as Message;
const context: ICommandContext = {
name: 'command',
args: [],
message: message
};
const errorEmbed = new ErrorEmbed(context, 'Error Message');
expect(errorEmbed.color?.toString()).toBe('13969411'); // 0xd52803 in decimal
expect(errorEmbed.description).toBe('Error Message');
expect(errorEmbed.context).toBe(context);
});
});
describe('SendToCurrentChannel', () => {
test('Expect embed to be sent to the current channel in context', () => {
process.env = {
EMBED_COLOUR_ERROR: '0xd52803'
}
const messageChannelSend = jest.fn();
const message = {
channel: {
send: messageChannelSend
}
} as unknown as Message;
const context: ICommandContext = {
name: 'command',
args: [],
message: message
};
const errorEmbed = new ErrorEmbed(context, 'Error Message');
errorEmbed.SendToCurrentChannel();
expect(messageChannelSend).toBeCalledWith(errorEmbed);
});
});

View file

@ -1,292 +0,0 @@
import { Guild, Message, TextChannel, User } from "discord.js";
import { ICommandContext } from "../../../src/contracts/ICommandContext";
import EventEmbed from "../../../src/helpers/embeds/EventEmbed";
import SettingsHelper from "../../../src/helpers/SettingsHelper";
beforeEach(() => {
process.env = {};
jest.resetAllMocks();
});
describe('Constructor', () => {
test('Expect properties to be set', () => {
const guild = {} as unknown as Guild;
const errorEmbed = new EventEmbed(guild, 'Event Message');
expect(errorEmbed.color?.toString()).toBe('3166394'); // 0x3050ba in decimal
expect(errorEmbed.title).toBe('Event Message');
expect(errorEmbed.guild).toBe(guild);
});
});
describe('AddUser', () => {
test('Given setThumbnail is false, add field WITHOUT user thumbnail', () => {
process.env = {
EMBED_COLOUR: '0xd52803',
CHANNELS_LOGS_MESSAGE: 'message-logs',
CHANNELS_LOGS_MEMBER: 'member-logs',
CHANNELS_LOGS_MOD: 'mod-logs'
}
const addField = jest.fn();
const setThumbnail = jest.fn();
const guild = {} as unknown as Guild;
const user = {
tag: 'USERTAG'
} as unknown as User;
const errorEmbed = new EventEmbed(guild, 'Event Message');
errorEmbed.addField = addField;
errorEmbed.setThumbnail = setThumbnail;
errorEmbed.AddUser('User', user);
expect(addField).toBeCalledWith('User', '[object Object] `USERTAG`', true);
expect(setThumbnail).not.toBeCalled();
});
test('Given setThumbnail is true, add field WITH user thumbnail', () => {
process.env = {
EMBED_COLOUR: '0xd52803',
CHANNELS_LOGS_MESSAGE: 'message-logs',
CHANNELS_LOGS_MEMBER: 'member-logs',
CHANNELS_LOGS_MOD: 'mod-logs'
}
const addField = jest.fn();
const setThumbnail = jest.fn();
const displayAvatarURL = jest.fn()
.mockReturnValue('image0.png');
const guild = {} as unknown as Guild;
const user = {
tag: 'USERTAG',
displayAvatarURL: displayAvatarURL
} as unknown as User;
const errorEmbed = new EventEmbed(guild, 'Event Message');
errorEmbed.addField = addField;
errorEmbed.setThumbnail = setThumbnail;
errorEmbed.AddUser('User', user, true);
expect(addField).toBeCalledWith('User', '[object Object] `USERTAG`', true);
expect(setThumbnail).toBeCalledWith('image0.png');
expect(displayAvatarURL).toBeCalled();
});
});
describe('AddReason', () => {
test('Given a non-empty string is supplied, expect field with message', () => {
const guild = {} as Guild;
const eventEmbed = new EventEmbed(guild, "Event Embed");
eventEmbed.addField = jest.fn();
eventEmbed.AddReason("Test reason");
expect(eventEmbed.addField).toBeCalledWith("Reason", "Test reason");
});
test('Given an empty string is supplied, expect field with default message', () => {
const guild = {} as Guild;
const eventEmbed = new EventEmbed(guild, "Event Embed");
eventEmbed.addField = jest.fn();
eventEmbed.AddReason("");
expect(eventEmbed.addField).toBeCalledWith("Reason", "*none*");
});
});
describe('SendToChannel', () => {
test('Given channel can be found, expect embed to be sent to that channel', () => {
const channelSend = jest.fn();
const channel = {
send: channelSend
} as unknown as TextChannel;
const guildChannelsCacheFind = jest.fn()
.mockReturnValue(channel);
const guild = {
channels: {
cache: {
find: guildChannelsCacheFind
}
}
} as unknown as Guild;
const errorEmbed = new EventEmbed(guild, 'Event Message');
errorEmbed.SendToChannel('channel-name');
expect(guildChannelsCacheFind).toBeCalledTimes(1);
expect(channelSend).toBeCalledWith(errorEmbed);
});
test('Given channel can NOT be found, expect error logged', () => {
process.env = {
EMBED_COLOUR: '0xd52803',
CHANNELS_LOGS_MESSAGE: 'message-logs',
CHANNELS_LOGS_MEMBER: 'member-logs',
CHANNELS_LOGS_MOD: 'mod-logs'
}
const guildChannelsCacheFind = jest.fn()
.mockReturnValue(null);
const guild = {
channels: {
cache: {
find: guildChannelsCacheFind
}
}
} as unknown as Guild;
console.error = jest.fn();
const errorEmbed = new EventEmbed(guild, 'Event Message');
errorEmbed.SendToChannel('channel-name');
expect(guildChannelsCacheFind).toBeCalledTimes(1);
expect(console.error).toBeCalledWith('Unable to find channel channel-name');
});
});
describe('SendToMessageLogsChannel', () => {
test('Given setting is set, expect SendToChannel to be called with value', async () => {
const sendToChannel = jest.fn();
const getSetting = jest.fn().mockResolvedValue("message-logs");
const guild = {
id: "guildId"
} as unknown as Guild;
SettingsHelper.GetSetting = getSetting;
const errorEmbed = new EventEmbed(guild, 'Event Message');
errorEmbed.SendToChannel = sendToChannel;
await errorEmbed.SendToMessageLogsChannel();
expect(sendToChannel).toBeCalledWith('message-logs');
expect(getSetting).toBeCalledWith("channels.logs.message", "guildId");
});
test('Given setting is not set, expect function to return', async () => {
const sendToChannel = jest.fn();
const getSetting = jest.fn().mockResolvedValue(undefined);
const guild = {
id: "guildId"
} as unknown as Guild;
SettingsHelper.GetSetting = getSetting;
const errorEmbed = new EventEmbed(guild, 'Event Message');
errorEmbed.SendToChannel = sendToChannel;
await errorEmbed.SendToMessageLogsChannel();
expect(sendToChannel).not.toBeCalled();
expect(getSetting).toBeCalledWith("channels.logs.message", "guildId");
});
});
describe('SendToMemberLogsChannel', () => {
test('Given setting is set, expect SendToChannel to be called with value', async () => {
const sendToChannel = jest.fn();
const getSetting = jest.fn().mockResolvedValue("member-logs");
const guild = {
id: "guildId"
} as unknown as Guild;
SettingsHelper.GetSetting = getSetting;
const errorEmbed = new EventEmbed(guild, 'Event Message');
errorEmbed.SendToChannel = sendToChannel;
await errorEmbed.SendToMemberLogsChannel();
expect(sendToChannel).toBeCalledWith('member-logs');
expect(getSetting).toBeCalledWith("channels.logs.member", "guildId");
});
test('Given setting is not set, expect function to return', async () => {
const sendToChannel = jest.fn();
const getSetting = jest.fn().mockResolvedValue(undefined);
const guild = {
id: "guildId"
} as unknown as Guild;
SettingsHelper.GetSetting = getSetting;
const errorEmbed = new EventEmbed(guild, 'Event Message');
errorEmbed.SendToChannel = sendToChannel;
await errorEmbed.SendToMemberLogsChannel();
expect(sendToChannel).not.toBeCalled();
expect(getSetting).toBeCalledWith("channels.logs.member", "guildId");
});
});
describe('SendToModLogsChannel', () => {
test('Given setting is set, expect SendToChannel to be called with value', async () => {
const sendToChannel = jest.fn();
const getSetting = jest.fn().mockResolvedValue("mod-logs");
const guild = {
id: "guildId"
} as unknown as Guild;
SettingsHelper.GetSetting = getSetting;
const errorEmbed = new EventEmbed(guild, 'Event Message');
errorEmbed.SendToChannel = sendToChannel;
await errorEmbed.SendToModLogsChannel();
expect(sendToChannel).toBeCalledWith('mod-logs');
expect(getSetting).toBeCalledWith("channels.logs.mod", "guildId");
});
test('Given setting is not set, expect function to return', async () => {
const sendToChannel = jest.fn();
const getSetting = jest.fn().mockResolvedValue(undefined);
const guild = {
id: "guildId"
} as unknown as Guild;
SettingsHelper.GetSetting = getSetting;
const errorEmbed = new EventEmbed(guild, 'Event Message');
errorEmbed.SendToChannel = sendToChannel;
await errorEmbed.SendToModLogsChannel();
expect(sendToChannel).not.toBeCalled();
expect(getSetting).toBeCalledWith("channels.logs.mod", "guildId");
});
});

View file

@ -1,407 +0,0 @@
import { Guild, Message, TextChannel, User } from "discord.js";
import { ICommandContext } from "../../../src/contracts/ICommandContext";
import LogEmbed from "../../../src/helpers/embeds/LogEmbed";
import SettingsHelper from "../../../src/helpers/SettingsHelper";
beforeEach(() => {
process.env = {};
jest.resetAllMocks();
});
describe('Constructor', () => {
test('Expect properties to be set', () => {
process.env = {
EMBED_COLOUR: '0xd52803',
CHANNELS_LOGS_MESSAGE: 'message-logs',
CHANNELS_LOGS_MEMBER: 'member-logs',
CHANNELS_LOGS_MOD: 'mod-logs'
}
const messageChannelSend = jest.fn();
const message = {
channel: {
send: messageChannelSend
}
} as unknown as Message;
const context: ICommandContext = {
name: 'command',
args: [],
message: message
};
const errorEmbed = new LogEmbed(context, 'Log Message');
expect(errorEmbed.color?.toString()).toBe('3166394'); // 0x3050ba in decimal
expect(errorEmbed.title).toBe('Log Message');
expect(errorEmbed.context).toBe(context);
});
});
describe('AddUser', () => {
test('Given setThumbnail is false, add field WITHOUT user thumbnail', () => {
process.env = {
EMBED_COLOUR: '0xd52803',
CHANNELS_LOGS_MESSAGE: 'message-logs',
CHANNELS_LOGS_MEMBER: 'member-logs',
CHANNELS_LOGS_MOD: 'mod-logs'
}
const addField = jest.fn();
const setThumbnail = jest.fn();
const user = {
tag: 'USERTAG'
} as unknown as User;
const messageChannelSend = jest.fn();
const message = {
channel: {
send: messageChannelSend
},
author: user
} as unknown as Message;
const context: ICommandContext = {
name: 'command',
args: [],
message: message
};
const errorEmbed = new LogEmbed(context, 'Event Message');
errorEmbed.addField = addField;
errorEmbed.setThumbnail = setThumbnail;
errorEmbed.AddUser('User', user);
expect(addField).toBeCalledWith('User', '[object Object] `USERTAG`', true);
expect(setThumbnail).not.toBeCalled();
});
test('Given setThumbnail is true, add field WITH user thumbnail', () => {
process.env = {
EMBED_COLOUR: '0xd52803',
CHANNELS_LOGS_MESSAGE: 'message-logs',
CHANNELS_LOGS_MEMBER: 'member-logs',
CHANNELS_LOGS_MOD: 'mod-logs'
}
const addField = jest.fn();
const setThumbnail = jest.fn();
const displayAvatarURL = jest.fn()
.mockReturnValue('image0.png');
const user = {
tag: 'USERTAG',
displayAvatarURL: displayAvatarURL
} as unknown as User;
const messageChannelSend = jest.fn();
const message = {
channel: {
send: messageChannelSend
},
author: user
} as unknown as Message;
const context: ICommandContext = {
name: 'command',
args: [],
message: message
};
const errorEmbed = new LogEmbed(context, 'Event Message');
errorEmbed.addField = addField;
errorEmbed.setThumbnail = setThumbnail;
errorEmbed.AddUser('User', user, true);
expect(addField).toBeCalledWith('User', '[object Object] `USERTAG`', true);
expect(setThumbnail).toBeCalledWith('image0.png');
expect(displayAvatarURL).toBeCalled();
});
});
describe('SendToChannel', () => {
test('Given channel can be found, expect embed to be sent to that channel', () => {
process.env = {
EMBED_COLOUR: '0xd52803',
CHANNELS_LOGS_MESSAGE: 'message-logs',
CHANNELS_LOGS_MEMBER: 'member-logs',
CHANNELS_LOGS_MOD: 'mod-logs'
}
const channelSend = jest.fn();
const channel = {
send: channelSend
} as unknown as TextChannel;
const guildChannelsCacheFind = jest.fn()
.mockReturnValue(channel);
const guild = {
channels: {
cache: {
find: guildChannelsCacheFind
}
}
} as unknown as Guild;
const messageChannelSend = jest.fn();
const message = {
channel: {
send: messageChannelSend
},
guild: guild
} as unknown as Message;
const context: ICommandContext = {
name: 'command',
args: [],
message: message
};
const errorEmbed = new LogEmbed(context, 'Event Message');
errorEmbed.SendToChannel('channel-name');
expect(guildChannelsCacheFind).toBeCalledTimes(1);
expect(channelSend).toBeCalledWith(errorEmbed);
});
test('Given channel can NOT be found, expect error logged', () => {
process.env = {
EMBED_COLOUR: '0xd52803',
CHANNELS_LOGS_MESSAGE: 'message-logs',
CHANNELS_LOGS_MEMBER: 'member-logs',
CHANNELS_LOGS_MOD: 'mod-logs'
}
const guildChannelsCacheFind = jest.fn()
.mockReturnValue(null);
const guild = {
channels: {
cache: {
find: guildChannelsCacheFind
}
}
} as unknown as Guild;
console.error = jest.fn();
const messageChannelSend = jest.fn();
const message = {
channel: {
send: messageChannelSend
},
guild: guild
} as unknown as Message;
const context: ICommandContext = {
name: 'command',
args: [],
message: message
};
const errorEmbed = new LogEmbed(context, 'Event Message');
errorEmbed.SendToChannel('channel-name');
expect(guildChannelsCacheFind).toBeCalledTimes(1);
});
});
describe('SendToMessageLogsChannel', () => {
test('Given setting is set, expect SendToChannel to be called with value', async () => {
const sendToChannel = jest.fn();
const getSetting = jest.fn().mockResolvedValue("message-logs");
const guild = {
id: "guildId"
} as unknown as Guild;
const message = {
guild: guild
} as Message;
const context: ICommandContext = {
name: 'log',
args: [],
message: message
};
SettingsHelper.GetSetting = getSetting;
const logEmbed = new LogEmbed(context, 'Event Message');
logEmbed.SendToChannel = sendToChannel;
await logEmbed.SendToMessageLogsChannel();
expect(sendToChannel).toBeCalledWith('message-logs');
expect(getSetting).toBeCalledWith("channels.logs.message", "guildId");
});
test('Given setting is not set, expect function to return', async () => {
const sendToChannel = jest.fn();
const getSetting = jest.fn().mockResolvedValue(undefined);
const guild = {
id: "guildId"
} as unknown as Guild;
const message = {
guild: guild
} as Message;
const context: ICommandContext = {
name: 'log',
args: [],
message: message
};
SettingsHelper.GetSetting = getSetting;
const logEmbed = new LogEmbed(context, 'Event Message');
logEmbed.SendToChannel = sendToChannel;
await logEmbed.SendToMessageLogsChannel();
expect(sendToChannel).not.toBeCalled();
expect(getSetting).toBeCalledWith("channels.logs.message", "guildId");
});
});
describe('SendToMemberLogsChannel', () => {
test('Given setting is set, expect SendToChannel to be called with value', async () => {
const sendToChannel = jest.fn();
const getSetting = jest.fn().mockResolvedValue("member-logs");
const guild = {
id: "guildId"
} as unknown as Guild;
const message = {
guild: guild
} as Message;
const context: ICommandContext = {
name: 'log',
args: [],
message: message
};
SettingsHelper.GetSetting = getSetting;
const logEmbed = new LogEmbed(context, 'Event Message');
logEmbed.SendToChannel = sendToChannel;
await logEmbed.SendToMemberLogsChannel();
expect(sendToChannel).toBeCalledWith('member-logs');
expect(getSetting).toBeCalledWith("channels.logs.member", "guildId");
});
test('Given setting is not set, expect function to return', async () => {
const sendToChannel = jest.fn();
const getSetting = jest.fn().mockResolvedValue(undefined);
const guild = {
id: "guildId"
} as unknown as Guild;
const message = {
guild: guild
} as Message;
const context: ICommandContext = {
name: 'log',
args: [],
message: message
};
SettingsHelper.GetSetting = getSetting;
const logEmbed = new LogEmbed(context, 'Event Message');
logEmbed.SendToChannel = sendToChannel;
await logEmbed.SendToMemberLogsChannel();
expect(sendToChannel).not.toBeCalled();
expect(getSetting).toBeCalledWith("channels.logs.member", "guildId");
});
});
describe('SendToModLogsChannel', () => {
test('Given setting is set, expect SendToChannel to be called with value', async () => {
const sendToChannel = jest.fn();
const getSetting = jest.fn().mockResolvedValue("mod-logs");
const guild = {
id: "guildId"
} as unknown as Guild;
const message = {
guild: guild
} as Message;
const context: ICommandContext = {
name: 'log',
args: [],
message: message
};
SettingsHelper.GetSetting = getSetting;
const logEmbed = new LogEmbed(context, 'Event Message');
logEmbed.SendToChannel = sendToChannel;
await logEmbed.SendToModLogsChannel();
expect(sendToChannel).toBeCalledWith('mod-logs');
expect(getSetting).toBeCalledWith("channels.logs.mod", "guildId");
});
test('Given setting is not set, expect function to return', async () => {
const sendToChannel = jest.fn();
const getSetting = jest.fn().mockResolvedValue(undefined);
const guild = {
id: "guildId"
} as unknown as Guild;
const message = {
guild: guild
} as Message;
const context: ICommandContext = {
name: 'log',
args: [],
message: message
};
SettingsHelper.GetSetting = getSetting;
const logEmbed = new LogEmbed(context, 'Event Message');
logEmbed.SendToChannel = sendToChannel;
await logEmbed.SendToModLogsChannel();
expect(sendToChannel).not.toBeCalled();
expect(getSetting).toBeCalledWith("channels.logs.mod", "guildId");
});
});

View file

@ -1,67 +0,0 @@
import { Guild, Message, TextChannel, User } from "discord.js";
import { ICommandContext } from "../../../src/contracts/ICommandContext";
import PublicEmbed from "../../../src/helpers/embeds/PublicEmbed";
beforeEach(() => {
process.env = {};
});
describe('Constructor', () => {
test('Expect properties to be set', () => {
process.env = {
EMBED_COLOUR: '0xd52803',
CHANNELS_LOGS_MESSAGE: 'message-logs',
CHANNELS_LOGS_MEMBER: 'member-logs',
CHANNELS_LOGS_MOD: 'mod-logs'
}
const messageChannelSend = jest.fn();
const message = {
channel: {
send: messageChannelSend
}
} as unknown as Message;
const context: ICommandContext = {
name: 'command',
args: [],
message: message
};
const errorEmbed = new PublicEmbed(context, 'Log Message', 'Log Description');
expect(errorEmbed.color?.toString()).toBe('13969411'); // 0xd52803 in decimal
expect(errorEmbed.title).toBe('Log Message');
expect(errorEmbed.description).toBe('Log Description');
expect(errorEmbed.context).toBe(context);
});
});
describe('SendToCurrentChannel', () => {
test('Expect embed to be sent to the current channel in context', () => {
process.env = {
EMBED_COLOUR_ERROR: '0xd52803'
}
const messageChannelSend = jest.fn();
const message = {
channel: {
send: messageChannelSend
}
} as unknown as Message;
const context: ICommandContext = {
name: 'command',
args: [],
message: message
};
const errorEmbed = new PublicEmbed(context, 'Message', 'Description');
errorEmbed.SendToCurrentChannel();
expect(messageChannelSend).toBeCalledWith(errorEmbed);
});
});

11
tests/registry.test.ts Normal file
View file

@ -0,0 +1,11 @@
describe('RegisterCommands', () => {
test.todo("EXPECT commands to be registered");
});
describe('RegisterEvents', () => {
test.todo("EXPECT events to be registered");
});
describe('RegisterButtonEvents', () => {
test.todo("EXPECT button events to be registered");
});

4666
yarn.lock Normal file

File diff suppressed because it is too large Load diff