From ad0d2aef0806b2b76a12100077eba4ea349f3465 Mon Sep 17 00:00:00 2001 From: Ethan Lane Date: Wed, 5 Mar 2025 18:51:15 +0000 Subject: [PATCH 1/2] Add buy confirmation tests --- tests/buttonEvents/Effects/Buy.test.ts | 151 ++++++++++++++++++++++++- 1 file changed, 146 insertions(+), 5 deletions(-) diff --git a/tests/buttonEvents/Effects/Buy.test.ts b/tests/buttonEvents/Effects/Buy.test.ts index 10f4daf..06f701c 100644 --- a/tests/buttonEvents/Effects/Buy.test.ts +++ b/tests/buttonEvents/Effects/Buy.test.ts @@ -4,6 +4,7 @@ import GenerateButtonInteractionMock from "../../__functions__/discord.js/Genera import { ButtonInteraction as ButtonInteractionType } from "../../__types__/discord.js"; import AppLogger from "../../../src/client/appLogger"; import EffectHelper from "../../../src/helpers/EffectHelper"; +import EmbedColours from "../../../src/constants/EmbedColours"; jest.mock("../../../src/client/appLogger"); jest.mock("../../../src/helpers/EffectHelper"); @@ -13,6 +14,11 @@ let interaction: ButtonInteractionType; beforeEach(() => { jest.resetAllMocks(); + jest.spyOn(Buy as any, "Confirm") + .mockRestore(); + jest.spyOn(Buy as any, "Cancel") + .mockRestore(); + interaction = GenerateButtonInteractionMock(); interaction.customId = "effects buy"; @@ -85,6 +91,8 @@ describe("Confirm", () => { const embed = { id: "embed", + setColor: jest.fn(), + setFooter: jest.fn(), }; const row = { id: "row", @@ -108,17 +116,150 @@ describe("Confirm", () => { expect(EffectHelper.GenerateEffectBuyEmbed).toHaveBeenCalledTimes(1); expect(EffectHelper.GenerateEffectBuyEmbed).toHaveBeenCalledWith("userId", "id", 1, true); + expect(embed.setColor).toHaveBeenCalledTimes(1); + expect(embed.setColor).toHaveBeenCalledWith(EmbedColours.Success); + + expect(embed.setFooter).toHaveBeenCalledTimes(1); + expect(embed.setFooter).toHaveBeenCalledWith({ text: "Purchased" }); + expect(interaction.reply).not.toHaveBeenCalled(); expect(AppLogger.LogError).not.toHaveBeenCalled(); }); - test.todo("GIVEN id is not supplied, EXPECT error"); + test("GIVEN id is not supplied, EXPECT error", async () => { + // Assert + const embed = { + id: "embed", + setColor: jest.fn(), + setFooter: jest.fn(), + }; + const row = { + id: "row", + }; + + (EffectHelper.GenerateEffectBuyEmbed as jest.Mock).mockResolvedValue({ + embed, + row, + }); - test.todo("GIVEN quantity is not supplied, EXPECT error"); + // Act + await Buy.Execute(interaction as unknown as ButtonInteraction); - test.todo("GIVEN quantity is not a number, EXPECT error"); + // Assert + expect(AppLogger.LogError).toHaveBeenCalledTimes(1); + expect(AppLogger.LogError).toHaveBeenCalledWith("Buy Confirm", "Not enough parameters"); - test.todo("GIVEN quantity is 0, EXPECT error"); + expect(EffectHelper.GenerateEffectBuyEmbed).not.toHaveBeenCalled(); + expect(interaction.reply).not.toHaveBeenCalled(); + expect(interaction.update).not.toHaveBeenCalled(); + }); - test.todo("GIVEN GenerateEffectBuyEmbed returns with a string, EXPECT error replied"); + test("GIVEN quantity is not supplied, EXPECT error", async () => { + // Assert + interaction.customId += " id"; + + const embed = { + id: "embed", + setColor: jest.fn(), + setFooter: jest.fn(), + }; + const row = { + id: "row", + }; + + (EffectHelper.GenerateEffectBuyEmbed as jest.Mock).mockResolvedValue({ + embed, + row, + }); + + // Act + await Buy.Execute(interaction as unknown as ButtonInteraction); + + // Assert + expect(AppLogger.LogError).toHaveBeenCalledTimes(1); + expect(AppLogger.LogError).toHaveBeenCalledWith("Buy Confirm", "Not enough parameters"); + + expect(EffectHelper.GenerateEffectBuyEmbed).not.toHaveBeenCalled(); + expect(interaction.reply).not.toHaveBeenCalled(); + expect(interaction.update).not.toHaveBeenCalled(); + }); + + test("GIVEN quantity is not a number, EXPECT error", async () => { + // Assert + interaction.customId += " id invalid"; + + const embed = { + id: "embed", + setColor: jest.fn(), + setFooter: jest.fn(), + }; + const row = { + id: "row", + }; + + (EffectHelper.GenerateEffectBuyEmbed as jest.Mock).mockResolvedValue({ + embed, + row, + }); + + // Act + await Buy.Execute(interaction as unknown as ButtonInteraction); + + // Assert + expect(AppLogger.LogError).toHaveBeenCalledTimes(1); + expect(AppLogger.LogError).toHaveBeenCalledWith("Buy Confirm", "Invalid number"); + + expect(EffectHelper.GenerateEffectBuyEmbed).not.toHaveBeenCalled(); + expect(interaction.reply).not.toHaveBeenCalled(); + expect(interaction.update).not.toHaveBeenCalled(); + }); + + test("GIVEN quantity is 0, EXPECT error", async () => { + // Assert + interaction.customId += " id 0"; + + const embed = { + id: "embed", + setColor: jest.fn(), + setFooter: jest.fn(), + }; + const row = { + id: "row", + }; + + (EffectHelper.GenerateEffectBuyEmbed as jest.Mock).mockResolvedValue({ + embed, + row, + }); + + // Act + await Buy.Execute(interaction as unknown as ButtonInteraction); + + // Assert + expect(AppLogger.LogError).toHaveBeenCalledTimes(1); + expect(AppLogger.LogError).toHaveBeenCalledWith("Buy Confirm", "Invalid number"); + + expect(EffectHelper.GenerateEffectBuyEmbed).not.toHaveBeenCalled(); + expect(interaction.reply).not.toHaveBeenCalled(); + expect(interaction.update).not.toHaveBeenCalled(); + }); + + test("GIVEN GenerateEffectBuyEmbed returns with a string, EXPECT error replied", async () => { + // Assert + interaction.customId += " id 1"; + + (EffectHelper.GenerateEffectBuyEmbed as jest.Mock).mockResolvedValue("Test error"); + + // Act + await Buy.Execute(interaction as unknown as ButtonInteraction); + + // Assert + expect(interaction.reply).toHaveBeenCalledTimes(1); + expect(interaction.reply).toHaveBeenCalledWith("Test error"); + + expect(EffectHelper.GenerateEffectBuyEmbed).toHaveBeenCalledTimes(1); + + expect(interaction.update).not.toHaveBeenCalled(); + expect(AppLogger.LogError).not.toHaveBeenCalled(); + }); }); From 1f5a19f10169c4610374a54009a4c395d8bdd9d4 Mon Sep 17 00:00:00 2001 From: Ethan Lane Date: Wed, 5 Mar 2025 19:05:10 +0000 Subject: [PATCH 2/2] WIP: Implement adding to the users inventory on confirm --- src/buttonEvents/Effects/Buy.ts | 28 ++++++++++++++++++++++++++ tests/buttonEvents/Effects/Buy.test.ts | 4 ++++ 2 files changed, 32 insertions(+) diff --git a/src/buttonEvents/Effects/Buy.ts b/src/buttonEvents/Effects/Buy.ts index 1dad272..473eb08 100644 --- a/src/buttonEvents/Effects/Buy.ts +++ b/src/buttonEvents/Effects/Buy.ts @@ -2,6 +2,8 @@ import {ButtonInteraction} from "discord.js"; import AppLogger from "../../client/appLogger"; import EffectHelper from "../../helpers/EffectHelper"; import EmbedColours from "../../constants/EmbedColours"; +import User from "../../database/entities/app/User"; +import {EffectDetails} from "../../constants/EffectDetails"; export default class Buy { public static async Execute(interaction: ButtonInteraction) { @@ -27,6 +29,13 @@ export default class Buy { AppLogger.LogError("Buy Confirm", "Not enough parameters"); return; } + + const effectDetail = EffectDetails.get(id); + + if (!effectDetail) { + AppLogger.LogError("Buy Confirm", "Effect detail not found!"); + return; + } const quantityNumber = Number(quantity); @@ -35,6 +44,25 @@ export default class Buy { return; } + const totalCost = effectDetail.cost * quantityNumber; + + const user = await User.FetchOneById(User, interaction.user.id); + + if (!user) { + AppLogger.LogError("Buy Confirm", "Unable to find user"); + return; + } + + if (user.Currency < totalCost) { + interaction.reply(`You don't have enough currency to buy this! You have \`${user.Currency} Currency\` and need \`${totalCost} Currency\`!`); + return; + } + + user.RemoveCurrency(totalCost); + await user.Save(User, user); + + await EffectHelper.AddEffectToUserInventory(interaction.user.id, id, quantityNumber); + const generatedEmbed = await EffectHelper.GenerateEffectBuyEmbed(interaction.user.id, id, quantityNumber, true); if (typeof generatedEmbed == "string") { diff --git a/tests/buttonEvents/Effects/Buy.test.ts b/tests/buttonEvents/Effects/Buy.test.ts index 06f701c..6c7ae6b 100644 --- a/tests/buttonEvents/Effects/Buy.test.ts +++ b/tests/buttonEvents/Effects/Buy.test.ts @@ -244,6 +244,10 @@ describe("Confirm", () => { expect(interaction.update).not.toHaveBeenCalled(); }); + test.todo("GIVEN user is not found, EXPECT error"); + + test.todo("GIVEN user does not have enough currency, EXPECT error"); + test("GIVEN GenerateEffectBuyEmbed returns with a string, EXPECT error replied", async () => { // Assert interaction.customId += " id 1";