Create use effect command (#419)
All checks were successful
Deploy To Stage / build (push) Successful in 48s
Deploy To Stage / deploy (push) Successful in 17s

# Description

Please include a summary of the change and which issue is fixed. Please also include relevant motivation and context. List any dependencies that are required for this change.

#380

## Type of change

Please delete options that are not relevant.

- [ ] Bug fix (non-breaking change which fixes an issue)
- [ ] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing functionality to not work as expected)
- [ ] This change requires a documentation update

# How Has This Been Tested?

Please describe the tests that you ran to verify the changes. Provide instructions so we can reproduce. Please also list any relevant details to your test configuration.

# Checklist

- [ ] My code follows the style guidelines of this project
- [ ] I have performed a self-review of my own code
- [ ] I have commented my code, particularly in hard-to-understand areas
- [ ] I have made corresponding changes to the documentation
- [ ] My changes generate no new warnings
- [ ] I have added tests that provde my fix is effective or that my feature works
- [ ] New and existing unit tests pass locally with my changes
- [ ] Any dependent changes have been merged and published in downstream modules

Reviewed-on: #419
Reviewed-by: VylpesTester <tester@vylpes.com>
Co-authored-by: Ethan Lane <ethan@vylpes.com>
Co-committed-by: Ethan Lane <ethan@vylpes.com>
This commit is contained in:
Ethan Lane 2025-01-25 17:29:01 +00:00 committed by Vylpes
parent 3e81f8ce1d
commit a3248e978a
42 changed files with 1241 additions and 1133 deletions

View file

@ -0,0 +1,109 @@
import { ButtonInteraction, TextChannel } from "discord.js";
import Claim from "../../src/buttonEvents/Claim";
import { ButtonInteraction as ButtonInteractionType } from "../__types__/discord.js";
import User from "../../src/database/entities/app/User";
import GenerateButtonInteractionMock from "../__functions__/discord.js/GenerateButtonInteractionMock";
jest.mock("../../src/client/appLogger");
let interaction: ButtonInteractionType;
beforeEach(() => {
jest.useFakeTimers();
jest.setSystemTime(1000 * 60 * 30);
interaction = GenerateButtonInteractionMock();
interaction.customId = "claim cardNumber claimId droppedBy userId";
});
afterAll(() => {
jest.useRealTimers();
});
test("GIVEN interaction.guild is null, EXPECT nothing to happen", async () => {
// Arrange
interaction.guild = null;
// Act
const claim = new Claim();
await claim.execute(interaction as unknown as ButtonInteraction);
// Assert
expect(interaction.deferUpdate).not.toHaveBeenCalled();
expect(interaction.editReply).not.toHaveBeenCalled();
expect((interaction.channel as TextChannel).send).not.toHaveBeenCalled();
});
test("GIVEN interaction.guildId is null, EXPECT nothing to happen", async () => {
// Arrange
interaction.guildId = null;
// Act
const claim = new Claim();
await claim.execute(interaction as unknown as ButtonInteraction);
// Assert
expect(interaction.deferUpdate).not.toHaveBeenCalled();
expect(interaction.editReply).not.toHaveBeenCalled();
expect((interaction.channel as TextChannel).send).not.toHaveBeenCalled();
});
test("GIVEN interaction.channel is null, EXPECT nothing to happen", async () => {
// Arrange
interaction.channel = null;
// Act
const claim = new Claim();
await claim.execute(interaction as unknown as ButtonInteraction);
// Assert
expect(interaction.deferUpdate).not.toHaveBeenCalled();
expect(interaction.editReply).not.toHaveBeenCalled();
});
test("GIVEN channel is not sendable, EXPECT nothing to happen", async () => {
// Arrange
interaction.channel!.isSendable = jest.fn().mockReturnValue(false);
// Act
const claim = new Claim();
await claim.execute(interaction as unknown as ButtonInteraction);
// Assert
expect(interaction.deferUpdate).not.toHaveBeenCalled();
expect(interaction.editReply).not.toHaveBeenCalled();
expect((interaction.channel as TextChannel).send).not.toHaveBeenCalled();
});
test("GIVEN interaction.message was created more than 5 minutes ago, EXPECT error", async () => {
// Arrange
interaction.message!.createdAt = new Date(0);
// Act
const claim = new Claim();
await claim.execute(interaction as unknown as ButtonInteraction);
// Assert
expect(interaction.channel!.send).toHaveBeenCalledTimes(1);
expect(interaction.channel!.send).toHaveBeenCalledWith("[object Object], Cards can only be claimed within 5 minutes of it being dropped!");
expect(interaction.editReply).not.toHaveBeenCalled();
});
test("GIVEN user.RemoveCurrency fails, EXPECT error", async () => {
// Arrange
User.FetchOneById = jest.fn().mockResolvedValue({
RemoveCurrency: jest.fn().mockReturnValue(false),
Currency: 5,
});
// Act
const claim = new Claim();
await claim.execute(interaction as unknown as ButtonInteraction);
// Assert
expect(interaction.channel!.send).toHaveBeenCalledTimes(1);
expect(interaction.channel!.send).toHaveBeenCalledWith("[object Object], Not enough currency! You need 10 currency, you have 5!");
expect(interaction.editReply).not.toHaveBeenCalled();
});

View file

@ -1,127 +1,66 @@
import {ButtonInteraction} from "discord.js";
import { ButtonInteraction } from "discord.js";
import Effects from "../../src/buttonEvents/Effects";
import EffectHelper from "../../src/helpers/EffectHelper";
import GenerateButtonInteractionMock from "../__functions__/discord.js/GenerateButtonInteractionMock";
import { ButtonInteraction as ButtonInteractionType } from "../__types__/discord.js";
import List from "../../src/buttonEvents/Effects/List";
import Use from "../../src/buttonEvents/Effects/Use";
import AppLogger from "../../src/client/appLogger";
describe("execute", () => {
describe("GIVEN action in custom id is list", () => {
const interaction = {
customId: "effects list",
} as unknown as ButtonInteraction;
jest.mock("../../src/client/appLogger");
jest.mock("../../src/buttonEvents/Effects/List");
jest.mock("../../src/buttonEvents/Effects/Use");
let listSpy: jest.SpyInstance;
let interaction: ButtonInteractionType;
beforeAll(async () => {
const effects = new Effects();
beforeEach(() => {
jest.resetAllMocks();
listSpy = jest.spyOn(effects as unknown as {"List": () => object}, "List")
.mockImplementation();
await effects.execute(interaction);
});
test("EXPECT list function to be called", () => {
expect(listSpy).toHaveBeenCalledTimes(1);
expect(listSpy).toHaveBeenCalledWith(interaction);
});
});
interaction = GenerateButtonInteractionMock();
interaction.customId = "effects";
});
describe("List", () => {
let interaction: ButtonInteraction;
test("GIVEN action is list, EXPECT list function to be called", async () => {
// Arrange
interaction.customId = "effects list";
const embed = {
name: "Embed",
};
// Act
const effects = new Effects();
await effects.execute(interaction as unknown as ButtonInteraction);
const row = {
name: "Row",
};
// Assert
expect(List).toHaveBeenCalledTimes(1);
expect(List).toHaveBeenCalledWith(interaction);
beforeEach(() => {
interaction = {
customId: "effects list",
user: {
id: "userId",
},
update: jest.fn(),
reply: jest.fn(),
} as unknown as ButtonInteraction;
});
describe("GIVEN page is a valid number", () => {
beforeEach(async () => {
interaction.customId += " 1";
EffectHelper.GenerateEffectEmbed = jest.fn()
.mockResolvedValue({
embed,
row,
});
const effects = new Effects();
await effects.execute(interaction);
});
test("EXPECT EffectHelper.GenerateEffectEmbed to be called", () => {
expect(EffectHelper.GenerateEffectEmbed).toHaveBeenCalledTimes(1);
expect(EffectHelper.GenerateEffectEmbed).toHaveBeenCalledWith("userId", 1);
});
test("EXPECT interaction to be updated", () => {
expect(interaction.update).toHaveBeenCalledTimes(1);
expect(interaction.update).toHaveBeenCalledWith({
embeds: [ embed ],
components: [ row ],
});
});
});
describe("GIVEN page in custom id is not supplied", () => {
beforeEach(async () => {
EffectHelper.GenerateEffectEmbed = jest.fn()
.mockResolvedValue({
embed,
row,
});
const effects = new Effects();
await effects.execute(interaction);
});
test("EXPECT interaction to be replied with error", () => {
expect(interaction.reply).toHaveBeenCalledTimes(1);
expect(interaction.reply).toHaveBeenCalledWith("Page option is not a valid number");
});
test("EXPECT interaction to not be updated", () => {
expect(interaction.update).not.toHaveBeenCalled();
});
});
describe("GIVEN page in custom id is not a number", () => {
beforeEach(async () => {
interaction.customId += " test";
EffectHelper.GenerateEffectEmbed = jest.fn()
.mockResolvedValue({
embed,
row,
});
const effects = new Effects();
await effects.execute(interaction);
});
test("EXPECT interaction to be replied with error", () => {
expect(interaction.reply).toHaveBeenCalledTimes(1);
expect(interaction.reply).toHaveBeenCalledWith("Page option is not a valid number");
});
test("EXPECT interaction to not be updated", () => {
expect(interaction.update).not.toHaveBeenCalled();
});
});
expect(Use.Execute).not.toHaveBeenCalled();
});
test("GIVEN action is use, EXPECT use function to be called", async () => {
// Arrange
interaction.customId = "effects use";
// Act
const effects = new Effects();
await effects.execute(interaction as unknown as ButtonInteraction);
// Assert
expect(Use.Execute).toHaveBeenCalledTimes(1);
expect(Use.Execute).toHaveBeenCalledWith(interaction);
expect(List).not.toHaveBeenCalled();
});
test("GIVEN action is invalid, EXPECT nothing to be called", async () => {
// Arrange
interaction.customId = "effects invalid";
// Act
const effects = new Effects();
await effects.execute(interaction as unknown as ButtonInteraction);
// Assert
expect(List).not.toHaveBeenCalled();
expect(Use.Execute).not.toHaveBeenCalled();
expect(AppLogger.LogError).toHaveBeenCalledTimes(1);
expect(AppLogger.LogError).toHaveBeenCalledWith("Buttons/Effects", "Unknown action, invalid");
});

View file

@ -0,0 +1,50 @@
import { ActionRowBuilder, ButtonBuilder, ButtonInteraction, EmbedBuilder } from "discord.js";
import List from "../../../src/buttonEvents/Effects/List";
import EffectHelper from "../../../src/helpers/EffectHelper";
import { mock } from "jest-mock-extended";
jest.mock("../../../src/helpers/EffectHelper");
let interaction: ReturnType<typeof mock<ButtonInteraction>>;
beforeEach(() => {
jest.resetAllMocks();
(EffectHelper.GenerateEffectEmbed as jest.Mock).mockResolvedValue({
embed: mock<EmbedBuilder>(),
row: mock<ActionRowBuilder<ButtonBuilder>>(),
});
interaction = mock<ButtonInteraction>();
interaction.user.id = "userId";
interaction.customId = "effects list 1";
});
test("GIVEN pageOption is NOT a number, EXPECT error", async () => {
// Arrange
interaction.customId = "effects list invalid";
// Act
await List(interaction);
// Assert
expect(interaction.reply).toHaveBeenCalledTimes(1);
expect(interaction.reply).toHaveBeenCalledWith("Page option is not a valid number")
expect(EffectHelper.GenerateEffectEmbed).not.toHaveBeenCalled();
expect(interaction.update).not.toHaveBeenCalled();
});
test("GIVEN pageOption is a number, EXPECT interaction updated", async () => {
// Arrange
interaction.customId = "effects list 1";
// Act
await List(interaction);
// Assert
expect(EffectHelper.GenerateEffectEmbed).toHaveBeenCalledTimes(1);
expect(EffectHelper.GenerateEffectEmbed).toHaveBeenCalledWith("userId", 1);
expect(interaction.update).toHaveBeenCalledTimes(1);
});

View file

@ -0,0 +1,148 @@
import { ButtonInteraction, InteractionResponse, InteractionUpdateOptions, MessagePayload } from "discord.js";
import Use from "../../../src/buttonEvents/Effects/Use";
import { mock } from "jest-mock-extended";
import AppLogger from "../../../src/client/appLogger";
import EffectHelper from "../../../src/helpers/EffectHelper";
jest.mock("../../../src/client/appLogger");
jest.mock("../../../src/helpers/EffectHelper");
beforeEach(() => {
jest.resetAllMocks();
jest.useFakeTimers();
jest.setSystemTime(0);
});
afterAll(() => {
jest.useRealTimers();
});
describe("Execute", () => {
test("GIVEN subaction is unknown, EXPECT nothing to be called", async () => {
// Arrange
const interaction = mock<ButtonInteraction>();
interaction.customId = "effects use invalud";
// Act
await Use.Execute(interaction);
// Assert
expect(interaction.reply).not.toHaveBeenCalled();
expect(interaction.update).not.toHaveBeenCalled();
});
});
describe("UseConfirm", () => {
let interaction = mock<ButtonInteraction>();
beforeEach(() => {
interaction = mock<ButtonInteraction>();
interaction.customId = "effects use confirm";
});
test("GIVEN effectDetail is not found, EXPECT error", async () => {
// Arrange
interaction.customId += " invalid";
// Act
await Use.Execute(interaction);
// Assert
expect(AppLogger.LogError).toHaveBeenCalledTimes(1);
expect(AppLogger.LogError).toHaveBeenCalledWith("Button/Effects/Use", "Effect not found, invalid");
expect(interaction.reply).toHaveBeenCalledTimes(1);
expect(interaction.reply).toHaveBeenCalledWith("Effect not found in system!");
});
test("GIVEN EffectHelper.UseEffect failed, EXPECT error", async () => {
// Arrange
interaction.customId += " unclaimed";
interaction.user.id = "userId";
(EffectHelper.UseEffect as jest.Mock).mockResolvedValue(false);
const whenExpires = new Date(Date.now() + 10 * 60 * 1000);
// Act
await Use.Execute(interaction);
// Assert
expect(EffectHelper.UseEffect).toHaveBeenCalledTimes(1);
expect(EffectHelper.UseEffect).toHaveBeenCalledWith("userId", "unclaimed", whenExpires);
expect(interaction.reply).toHaveBeenCalledTimes(1);
expect(interaction.reply).toHaveBeenCalledWith("Unable to use effect! Please make sure you have it in your inventory and is not on cooldown");
});
test("GIVEN EffectHelper.UseEffect succeeded, EXPECT interaction updated", async () => {
let updatedWith;
// Arrange
interaction.customId += " unclaimed";
interaction.user.id = "userId";
interaction.update.mockImplementation(async (opts: string | MessagePayload | InteractionUpdateOptions) => {
updatedWith = opts;
return mock<InteractionResponse<boolean>>();
});
(EffectHelper.UseEffect as jest.Mock).mockResolvedValue(true);
const whenExpires = new Date(Date.now() + 10 * 60 * 1000);
// Act
await Use.Execute(interaction);
// Assert
expect(EffectHelper.UseEffect).toHaveBeenCalledTimes(1);
expect(EffectHelper.UseEffect).toHaveBeenCalledWith("userId", "unclaimed", whenExpires);
expect(interaction.update).toHaveBeenCalledTimes(1);
expect(updatedWith).toMatchSnapshot();
});
});
describe("UseCancel", () => {
let interaction = mock<ButtonInteraction>();
beforeEach(() => {
interaction = mock<ButtonInteraction>();
interaction.customId = "effects use cancel";
});
test("GIVEN effectDetail is not found, EXPECT error", async () => {
// Arrange
interaction.customId += " invalid";
// Act
await Use.Execute(interaction);
// Assert
expect(AppLogger.LogError).toHaveBeenCalledTimes(1);
expect(AppLogger.LogError).toHaveBeenCalledWith("Button/Effects/Cancel", "Effect not found, invalid");
expect(interaction.reply).toHaveBeenCalledTimes(1);
expect(interaction.reply).toHaveBeenCalledWith("Effect not found in system!");
});
test("GIVEN effectDetail is found, EXPECT interaction updated", async () => {
let updatedWith;
// Arrange
interaction.customId += " unclaimed";
interaction.user.id = "userId";
interaction.update.mockImplementation(async (opts: string | MessagePayload | InteractionUpdateOptions) => {
updatedWith = opts;
return mock<InteractionResponse<boolean>>();
});
// Act
await Use.Execute(interaction);
// Assert
expect(interaction.update).toHaveBeenCalledTimes(1);
expect(updatedWith).toMatchSnapshot();
});
});

View file

@ -0,0 +1,95 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`UseCancel GIVEN effectDetail is found, EXPECT interaction updated 1`] = `
{
"components": [
{
"components": [
{
"custom_id": "effects use confirm unclaimed",
"disabled": true,
"emoji": undefined,
"label": "Confirm",
"style": 1,
"type": 2,
},
{
"custom_id": "effects use cancel unclaimed",
"disabled": true,
"emoji": undefined,
"label": "Cancel",
"style": 4,
"type": 2,
},
],
"type": 1,
},
],
"embeds": [
{
"color": 13882323,
"description": "The effect from your inventory has not been used",
"fields": [
{
"inline": true,
"name": "Effect",
"value": "Unclaimed Chance Up",
},
{
"inline": true,
"name": "Expires",
"value": "10m",
},
],
"title": "Effect Use Cancelled",
},
],
}
`;
exports[`UseConfirm GIVEN EffectHelper.UseEffect succeeded, EXPECT interaction updated 1`] = `
{
"components": [
{
"components": [
{
"custom_id": "effects use confirm unclaimed",
"disabled": true,
"emoji": undefined,
"label": "Confirm",
"style": 1,
"type": 2,
},
{
"custom_id": "effects use cancel unclaimed",
"disabled": true,
"emoji": undefined,
"label": "Cancel",
"style": 4,
"type": 2,
},
],
"type": 1,
},
],
"embeds": [
{
"color": 2263842,
"description": "You now have an active effect!",
"fields": [
{
"inline": true,
"name": "Effect",
"value": "Unclaimed Chance Up",
},
{
"inline": true,
"name": "Expires",
"value": "<t:600:f>",
},
],
"title": "Effect Used",
},
],
}
`;