From 60ead44b302e91ccf223becae123c797ba69cb51 Mon Sep 17 00:00:00 2001 From: Dev Date: Sat, 18 Nov 2023 15:01:51 +0000 Subject: [PATCH] Add HealthController.healthTreatment() tests --- .../controllers/HealthController.test.ts | 219 ++++++++++++++++++ 1 file changed, 219 insertions(+) create mode 100644 project/tests/controllers/HealthController.test.ts diff --git a/project/tests/controllers/HealthController.test.ts b/project/tests/controllers/HealthController.test.ts new file mode 100644 index 00000000..47dd992b --- /dev/null +++ b/project/tests/controllers/HealthController.test.ts @@ -0,0 +1,219 @@ +/* eslint-disable @typescript-eslint/naming-convention */ +import "reflect-metadata"; +import { container } from "tsyringe"; +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; + +import { HealthController } from "@spt-aki/controllers/HealthController"; +import { IPmcData } from "@spt-aki/models/eft/common/IPmcData"; +import { IHealthTreatmentRequestData } from "@spt-aki/models/eft/health/IHealthTreatmentRequestData"; + +describe("HealthController", () => +{ + let healthController: HealthController; // Using "any" to access private/protected methods without type errors. + + beforeEach(() => + { + healthController = container.resolve("HealthController"); + }); + + afterEach(() => + { + vi.restoreAllMocks(); + }); + + describe("healthTreatment", () => + { + it("Should Heal Players heavy bleed and heal chest to full hp", () => + { + const maxHealth = 100 + const pmcData = { + Health: { + BodyParts: { + Chest: { + Health: { + Current: 50, // Has damage + Maximum: maxHealth + }, + Effects: {HeavyBleeding: { + Time: 20 + } + } + } + } + } + }; + const bleedRemovalAndLimbHealRequest = { + Action: "RestoreHealth", + trader: "54cb57776803fa99248b456e", // Therapist + difference: { + BodyParts: { + Chest: { + Health: 23, // > 0 value means it will heal + Effects: ["HeavyBleeding"] // non-null means it will remove effect from player + } + } + } + }; + const sessionId = "12345"; + + // Mock output generation + vi.spyOn((healthController as any).eventOutputHolder, "getOutput").mockReturnValue( + { + warnings: {}, + profileChanges: { + "12345": { + health: {} + } + } + }); + + // Mock payment + vi.spyOn((healthController as any).paymentService, "payMoney").mockReturnValue( + { + warnings: {}, + profileChanges: { + "12345": { + health: {} + } + } + }); + + const result = healthController.healthTreatment(pmcData as unknown as IPmcData, bleedRemovalAndLimbHealRequest as IHealthTreatmentRequestData, sessionId); + + // Has healed chest to full + expect(result.profileChanges[sessionId].health.BodyParts.Chest.Health.Current).equals(maxHealth); + + // Has removed Heavy bleed effect from chest + expect(result.profileChanges[sessionId].health.BodyParts.Chest).not.toHaveProperty("Effects"); + }); + + it("Should Heal Players heavy bleed and leave limb health at existing value", () => + { + const maxHealth = 100 + const pmcData = { + Health: { + BodyParts: { + Chest: { + Health: { + Current: 50, // Has damage + Maximum: maxHealth + }, + Effects: {HeavyBleeding: { + Time: 20 + } + } + } + } + } + }; + const limbOnlyHealRequest = { + Action: "RestoreHealth", + trader: "54cb57776803fa99248b456e", // Therapist + difference: { + BodyParts: { + Chest: { + Health: 23, // > 0 value means it will heal limb to full + Effects: null // null means no healing of effects + } + } + } + }; + const sessionId = "12345"; + + // Mock output generation + vi.spyOn((healthController as any).eventOutputHolder, "getOutput").mockReturnValue( + { + warnings: {}, + profileChanges: { + "12345": { + health: {} + } + } + }); + + // Mock payment + vi.spyOn((healthController as any).paymentService, "payMoney").mockReturnValue( + { + warnings: {}, + profileChanges: { + "12345": { + health: {} + } + } + }); + + const result = healthController.healthTreatment(pmcData as unknown as IPmcData, limbOnlyHealRequest as IHealthTreatmentRequestData, sessionId); + + // Has healed chest to full + expect(result.profileChanges[sessionId].health.BodyParts.Chest.Health.Current).equals(maxHealth); + + // Has not removed Heavy bleed effect from chest + expect(result.profileChanges[sessionId].health.BodyParts.Chest).toHaveProperty("Effects"); + }); + + it("Should Heal Players heavy bleed and leave limb health at existing value", () => + { + const maxHealth = 100 + const currentHealth = 50; + const pmcData = { + Health: { + BodyParts: { + Chest: { + Health: { + Current: currentHealth, // Has damage + Maximum: maxHealth + }, + Effects: {HeavyBleeding: { + Time: 20 + } + } + } + } + } + }; + const limbOnlyHealRequest = { + Action: "RestoreHealth", + trader: "54cb57776803fa99248b456e", // Therapist + difference: { + BodyParts: { + Chest: { + Health: 0, // 0 value means it will not heal and damage + Effects: null // null means no healing of effects + } + } + } + }; + const sessionId = "12345"; + + // Mock output generation + vi.spyOn((healthController as any).eventOutputHolder, "getOutput").mockReturnValue( + { + warnings: {}, + profileChanges: { + "12345": { + health: {} + } + } + }); + + // Mock payment + vi.spyOn((healthController as any).paymentService, "payMoney").mockReturnValue( + { + warnings: {}, + profileChanges: { + "12345": { + health: {} + } + } + }); + + const result = healthController.healthTreatment(pmcData as unknown as IPmcData, limbOnlyHealRequest as IHealthTreatmentRequestData, sessionId); + + // Has not healed chest to full + expect(result.profileChanges[sessionId].health.BodyParts.Chest.Health.Current).equals(currentHealth); + + // Has not removed Heavy bleed effect from chest + expect(result.profileChanges[sessionId].health.BodyParts.Chest).toHaveProperty("Effects"); + }); + }); +});