diff --git a/project/.vscode/launch.json b/project/.vscode/launch.json index 56bdf049..e9a7190b 100644 --- a/project/.vscode/launch.json +++ b/project/.vscode/launch.json @@ -8,8 +8,8 @@ "name": "Debug", "type": "node", "runtimeVersion": "18.15.0", + "runtimeExecutable": "node", "request": "launch", - "runtimeExecutable": "npm", "sourceMaps": true, "runtimeArgs": [ "run", @@ -25,6 +25,7 @@ "name": "Run Jest UnitTests", "type": "node", "runtimeVersion": "18.15.0", + "runtimeExecutable": "node", "request": "launch", "program": "${workspaceFolder}/node_modules/.bin/jest", "args": [ @@ -38,4 +39,4 @@ } } ] -} \ No newline at end of file +} diff --git a/project/Server.code-workspace b/project/Server.code-workspace index be7a3097..12291d34 100644 --- a/project/Server.code-workspace +++ b/project/Server.code-workspace @@ -15,8 +15,8 @@ }, "eslint.validate": ["typescript"], "editor.codeActionsOnSave": { - "source.organizeImports": true, + "source.organizeImports": false, "source.fixAll.eslint": true } }, -} \ No newline at end of file +} diff --git a/project/jest.config.ts b/project/jest.config.ts index e9b6e540..14e8c2b0 100644 --- a/project/jest.config.ts +++ b/project/jest.config.ts @@ -1,10 +1,10 @@ -import { pathsToModuleNameMapper } from "ts-jest"; import type { JestConfigWithTsJest } from "ts-jest"; +import { pathsToModuleNameMapper } from "ts-jest"; import { compilerOptions } from "./tsconfig.json"; const config: JestConfigWithTsJest = { preset: "ts-jest", - testEnvironment: "node", + testEnvironment: "./tests/CustomEnvironment.ts", roots: [ "./tests/" ], diff --git a/project/tests/BotDifficultyHelper.test.ts b/project/tests/BotDifficultyHelper.test.ts deleted file mode 100644 index 2ad0462e..00000000 --- a/project/tests/BotDifficultyHelper.test.ts +++ /dev/null @@ -1,82 +0,0 @@ -import "reflect-metadata"; - -import { beforeEach, describe, expect, it } from "@jest/globals"; -import { BotDifficultyHelper } from "@spt-aki/helpers/BotDifficultyHelper"; -import { BotHelper } from "@spt-aki/helpers/BotHelper"; -import { ConfigTypes } from "@spt-aki/models/enums/ConfigTypes"; -import { ILogger } from "@spt-aki/models/spt/utils/ILogger"; -import { ConfigServer } from "@spt-aki/servers/ConfigServer"; -import { DatabaseServer } from "@spt-aki/servers/DatabaseServer"; -import { LocalisationService } from "@spt-aki/services/LocalisationService"; -import { JsonUtil } from "@spt-aki/utils/JsonUtil"; -import { RandomUtil } from "@spt-aki/utils/RandomUtil"; -import { MockHelper } from "./common/MockHelper"; -import { TestHelper } from "./common/TestHelper"; - -const mockHelper = new MockHelper(); -let testHelper: TestHelper; -let logger: ILogger; -let jsonUtil: JsonUtil; -let randomUtil: RandomUtil; -let configServer: ConfigServer; -let localisationService: LocalisationService; -let databaseServer: DatabaseServer; -let botHelper: BotHelper; - -describe("BotHelper", () => { - beforeAll(async () => { - testHelper = await TestHelper.fetchTestHelper(); - logger = testHelper.getTestLogger(); - jsonUtil = testHelper.getTestJsonUtil(); - randomUtil = testHelper.getTestRandomUtil(); - configServer = testHelper.getTestConfigServer(); - localisationService = testHelper.getTestLocalisationService(); - databaseServer = testHelper.getTestDatabaseServer(); - botHelper = testHelper.getTestBotHelper(); - }) - - let botDifficultyHelper: BotDifficultyHelper; - beforeEach(() => - { - botDifficultyHelper = new BotDifficultyHelper(logger, jsonUtil, databaseServer, randomUtil, localisationService, botHelper, configServer); - }); - - it("BotDifficultyHelper type check", () => - { - expect(botDifficultyHelper).toBeInstanceOf(BotDifficultyHelper); - }); - - it("chooseRandomDifficulty()", () => - { - expect(["easy", "normal", "hard", "impossible"]).toContain(botDifficultyHelper.chooseRandomDifficulty()); - }); - - it("getPmcDifficultySettings() easy", () => - { - expect(botDifficultyHelper.getPmcDifficultySettings("bear", "easy", "sptUsec", "sptBear")).not.toBeFalsy(); - }); - - it("getPmcDifficultySettings() random", () => - { - expect(botDifficultyHelper.getPmcDifficultySettings("usec", "random", "sptUsec", "sptBear")).not.toBeFalsy(); - }); - - it("getPmcDifficultySettings() difficulty set to 'medium' in config", () => - { - - const configServerMock = mockHelper.getMockConfigServer(); - const mockBotConfig = { - kind: "aki-bot", - pmc: { - difficulty: "medium", - enemyTypes: ["assault"] - } - }; - configServerMock.setup(x => x.getConfig(ConfigTypes.BOT)).returns(() => mockBotConfig); - - const testOnlyHelper = new BotDifficultyHelper(logger, jsonUtil, databaseServer, randomUtil, localisationService, botHelper, configServerMock.object); - - expect(testOnlyHelper.getPmcDifficultySettings("usec", "medium", "sptUsec", "sptBear")).not.toBeFalsy(); - }); - -}); \ No newline at end of file diff --git a/project/tests/BotHelper.test.ts b/project/tests/BotHelper.test.ts deleted file mode 100644 index 19960074..00000000 --- a/project/tests/BotHelper.test.ts +++ /dev/null @@ -1,64 +0,0 @@ -import "reflect-metadata"; - -import { beforeEach, describe, expect, it } from "@jest/globals"; -import { BotHelper } from "@spt-aki/helpers/BotHelper"; -import { ILogger } from "@spt-aki/models/spt/utils/ILogger"; -import { ConfigServer } from "@spt-aki/servers/ConfigServer"; -import { DatabaseServer } from "@spt-aki/servers/DatabaseServer"; -import { LocalisationService } from "@spt-aki/services/LocalisationService"; -import { JsonUtil } from "@spt-aki/utils/JsonUtil"; -import { RandomUtil } from "@spt-aki/utils/RandomUtil"; -import { TestHelper } from "./common/TestHelper"; - -let testHelper: TestHelper; -let logger: ILogger; -let jsonUtil: JsonUtil; -let randomUtil: RandomUtil; -let configServer: ConfigServer; -let localisationService: LocalisationService; -let databaseServer: DatabaseServer; - - -describe("BotHelper", () => { - beforeAll(async () => { - testHelper = await TestHelper.fetchTestHelper(); - logger = testHelper.getTestLogger(); - jsonUtil = testHelper.getTestJsonUtil(); - randomUtil = testHelper.getTestRandomUtil(); - configServer = testHelper.getTestConfigServer(); - localisationService = testHelper.getTestLocalisationService(); - databaseServer = testHelper.getTestDatabaseServer(); - }) - - let helper: BotHelper; - beforeEach(() => { - helper = new BotHelper(logger, jsonUtil, databaseServer, randomUtil, localisationService, configServer); - }); - - it("BotHelper type check", () => - { - expect(helper).toBeInstanceOf(BotHelper); - }); - - it("isBotPmc()", () => - { - expect(helper.isBotPmc("usec")).toBe(true); - expect(helper.isBotPmc("bear")).toBe(true); - expect(helper.isBotPmc("faketype")).toBe(false); - }); - - it("isBotFollower()", () => - { - expect(helper.isBotFollower("followerBully")).toBe(true); - expect(helper.isBotFollower("FoLlOwErBULlY")).toBe(true); - expect(helper.isBotFollower("followerSanitar")).toBe(true); - expect(helper.isBotFollower("botFollower")).toBe(false); - }); - - it("getBotTemplate()", () => - { - expect(helper.getBotTemplate("assault")).not.toBeFalsy(); - expect(helper.getBotTemplate("fakebottype")).toBeFalsy(); - expect(helper.getBotTemplate("")).toBeFalsy(); - }); -}); \ No newline at end of file diff --git a/project/tests/CustomEnvironment.ts b/project/tests/CustomEnvironment.ts new file mode 100644 index 00000000..6ab3b87d --- /dev/null +++ b/project/tests/CustomEnvironment.ts @@ -0,0 +1,31 @@ +import "reflect-metadata"; + +import NodeEnvironment from "jest-environment-node"; +import type {EnvironmentContext} from "@jest/environment"; +import type {JestEnvironmentConfig} from "@jest/environment"; + +import { container } from "tsyringe"; +import { Container } from "@spt-aki/di/Container"; + +class CustomEnvironment extends NodeEnvironment +{ + constructor(config: JestEnvironmentConfig, context: EnvironmentContext) + { + super(config, context); + } + + async setup(): Promise + { + await super.setup(); + + Container.registerTypes(container); + this.global.container = container; + } + + async teardown(): Promise + { + await super.teardown(); + } +} + +module.exports = CustomEnvironment; diff --git a/project/tests/PaymentHelper.test.ts b/project/tests/PaymentHelper.test.ts deleted file mode 100644 index 0dd9d5e0..00000000 --- a/project/tests/PaymentHelper.test.ts +++ /dev/null @@ -1,54 +0,0 @@ -import "reflect-metadata"; - -import { beforeEach, describe, expect, it } from "@jest/globals"; -import { PaymentHelper } from "@spt-aki/helpers/PaymentHelper"; - -describe("test text", () => -{ - let helper: PaymentHelper; - beforeEach(() => - { - helper = new PaymentHelper(); - }); - - it("PaymentHelper type check", () => - { - expect(helper).toBeInstanceOf(PaymentHelper); - }); - - it("isMoneyTpl() USD", () => - { - expect(helper.isMoneyTpl("5696686a4bdc2da3298b456a")).toBe(true); - }); - - it("isMoneyTpl() euro", () => - { - expect(helper.isMoneyTpl("569668774bdc2da2298b4568")).toBe(true); // euro - }); - - it("isMoneyTpl() rouble", () => - { - expect(helper.isMoneyTpl("5696686a4bdc2da3298b456a")).toBe(true); // rub - expect(helper.isMoneyTpl("")).toBe(false); - expect(helper.isMoneyTpl("faketpl")).toBe(false); - }); - - it("isMoneyTpl() empty tpl", () => - { - expect(helper.isMoneyTpl("")).toBe(false); - }); - - it("isMoneyTpl() invalid tpl", () => - { - expect(helper.isMoneyTpl("faketpl")).toBe(false); - }); - - it("getCurrency()", () => - { - expect(helper.getCurrency("EUR")).toBe("569668774bdc2da2298b4568"); - expect(helper.getCurrency("USD")).toBe("5696686a4bdc2da3298b456a"); - expect(helper.getCurrency("RUB")).toBe("5449016a4bdc2d6f028b456f"); - expect(helper.getCurrency("1234353434534")).toBe(""); - expect(helper.getCurrency("")).toBe(""); - }); -}); \ No newline at end of file diff --git a/project/tests/RepairHelper.test.ts b/project/tests/RepairHelper.test.ts deleted file mode 100644 index a6c0636c..00000000 --- a/project/tests/RepairHelper.test.ts +++ /dev/null @@ -1,213 +0,0 @@ -import "reflect-metadata"; - -import { RepairHelper } from "@spt-aki/helpers/RepairHelper"; -import { Item } from "@spt-aki/models/eft/common/tables/IItem"; -import { ITemplateItem } from "@spt-aki/models/eft/common/tables/ITemplateItem"; -import { ILogger } from "@spt-aki/models/spt/utils/ILogger"; -import { ConfigServer } from "@spt-aki/servers/ConfigServer"; -import { DatabaseServer } from "@spt-aki/servers/DatabaseServer"; -import { JsonUtil } from "@spt-aki/utils/JsonUtil"; -import { RandomUtil } from "@spt-aki/utils/RandomUtil"; -import { TestHelper } from "./common/TestHelper"; - -let testHelper: TestHelper; -let logger: ILogger; -let jsonUtil: JsonUtil; -let randomUtil: RandomUtil; -let configServer: ConfigServer; -let databaseServer: DatabaseServer; -let helper: RepairHelper; - -describe("BotHelper", () => { - - beforeAll(async () => { - testHelper = await TestHelper.fetchTestHelper(); - logger = testHelper.getTestLogger(); - jsonUtil = testHelper.getTestJsonUtil(); - randomUtil = testHelper.getTestRandomUtil(); - configServer = testHelper.getTestConfigServer(); - databaseServer = testHelper.getTestDatabaseServer(); - }) - - beforeEach(() => { - helper = new RepairHelper( - logger, - jsonUtil, - randomUtil, - databaseServer, - configServer); - }); - - it("RepairHelper type check", () => - { - expect(helper).toBeInstanceOf(RepairHelper); - }); - - it("updateItemDurability() repairkit with slick armor with max dura degradation", () => - { - const slickTpl = "5e4abb5086f77406975c9342"; - const itemToRepair: Item = { - _id: "12345", - _tpl: slickTpl, - upd: { - Repairable: { - Durability: 60, - MaxDurability: 80 - } - } - }; - const itemToRepairDetails = databaseServer.getTables().templates?.items[slickTpl]; - const isArmor = true; - const useRepairKit = true; - const useDegridation = true; - - helper.updateItemDurability(itemToRepair, itemToRepairDetails, isArmor, 5, useRepairKit, 1, useDegridation); - - expect(itemToRepair.upd?.Repairable?.Durability).toBeLessThan(80); - }); - - it("updateItemDurability() trader with slick armor with max dura degradation - partial repair", () => - { - const slickTpl = "5e4abb5086f77406975c9342"; - const itemToRepair: Item = { - _id: "12345", - _tpl: slickTpl, - upd: { - Repairable: { - Durability: 60, - MaxDurability: 80 - } - } - }; - const itemToRepairDetails = databaseServer.getTables().templates?.items[slickTpl]; - const isArmor = true; - const useRepairKit = false; - const useDegridation = true; - - helper.updateItemDurability(itemToRepair, itemToRepairDetails, isArmor, 5, useRepairKit, 1.2, useDegridation); - - expect(itemToRepair.upd?.Repairable?.Durability).toBeLessThan(80); - expect(itemToRepair.upd?.Repairable?.Durability).toBeLessThanOrEqual(itemToRepair.upd?.Repairable?.MaxDurability); - }); - - it("updateItemDurability() trader with slick armor, no dura degradation", () => - { - const slickTpl = "5e4abb5086f77406975c9342"; - const itemToRepair: Item = { - _id: "12345", - _tpl: slickTpl, - upd: { - Repairable: { - Durability: 60, - MaxDurability: 80 - } - } - }; - const itemToRepairDetails = databaseServer.getTables().templates?.items[slickTpl]; - const isArmor = true; - const useRepairKit = false; - const useDegradation = false; - - helper.updateItemDurability(itemToRepair, itemToRepairDetails, isArmor, 20, useRepairKit, 1.2, useDegradation); - - expect(itemToRepair.upd?.Repairable?.Durability).toBe(80); - expect(itemToRepair.upd?.Repairable?.Durability).toBeLessThanOrEqual(itemToRepair.upd?.Repairable?.MaxDurability); - }); - - it("updateItemDurability() repairkit with g36 with max dura degradation - Full repair", () => - { - const itemCurrentDura = 68; - const itemCurrentMaxDura = 100; - const duraDifference = itemCurrentMaxDura - itemCurrentDura; - const g36Tpl = "623063e994fc3f7b302a9696"; - const itemToRepair: Item = { - _id: "12345", - _tpl: g36Tpl, - upd: { - Repairable: { - Durability: itemCurrentDura, - MaxDurability: itemCurrentMaxDura - } - } - }; - const itemToRepairDetails = databaseServer.getTables().templates?.items[g36Tpl]; - const isArmor = false; - const useRepairKit = true; - const useDegradation = true; - - helper.updateItemDurability(itemToRepair, itemToRepairDetails, isArmor, duraDifference, useRepairKit, 1, useDegradation); - - expect(itemToRepair.upd?.Repairable?.Durability).toBeLessThan(100); - expect(itemToRepair.upd?.Repairable?.Durability).toBeLessThanOrEqual(itemToRepair.upd?.Repairable?.MaxDurability); - }); - - it("updateItemDurability() trader with g36 with max dura degradation - Full repair", () => - { - const itemCurrentDura = 68; - const itemCurrentMaxDura = 100; - const duraDifference = itemCurrentMaxDura - itemCurrentDura; - const g36Tpl = "623063e994fc3f7b302a9696"; - const itemToRepair: Item = { - _id: "12345", - _tpl: g36Tpl, - upd: { - Repairable: { - Durability: itemCurrentDura, - MaxDurability: itemCurrentMaxDura - } - } - }; - const itemToRepairDetails = databaseServer.getTables().templates?.items[g36Tpl]; - const isArmor = false; - const useRepairKit = false; - const useDegradation = true; - - helper.updateItemDurability(itemToRepair, itemToRepairDetails, isArmor, duraDifference, useRepairKit, 1.2, useDegradation); - - expect(itemToRepair.upd?.Repairable?.Durability).toBeLessThan(100); - expect(itemToRepair.upd?.Repairable?.Durability).toBeLessThanOrEqual(itemToRepair.upd?.Repairable?.MaxDurability); - }); - - it("updateItemDurability() faceshield broken use repairkit with max dura degradation", () => - { - const twExfilBallisticFaceShieldTpl = "5e00cdd986f7747473332240"; - const itemToRepair: Item = { - _id: "12345", - _tpl: twExfilBallisticFaceShieldTpl, - upd: { - Repairable: { - Durability: 30, - MaxDurability: 45 - }, - FaceShield: { - Hits: 2 - } - } - }; - - const itemToRepairDetails = databaseServer.getTables().templates?.items[twExfilBallisticFaceShieldTpl]; - const isArmor = true; - const useRepairKit = true; - const useDegradation = true; - - helper.updateItemDurability(itemToRepair, itemToRepairDetails, isArmor, 5, useRepairKit, 1, useDegradation); - - expect(itemToRepair.upd?.FaceShield?.Hits).toBe(0); - expect(itemToRepair.upd?.Repairable?.Durability).toBeLessThan(45); - expect(itemToRepair.upd?.Repairable?.Durability).toBeLessThanOrEqual(itemToRepair.upd?.Repairable?.MaxDurability); - }); - - it("isWeaponTemplate() g36 weapon", () => - { - const result = helper.isWeaponTemplate("623063e994fc3f7b302a9696"); - - expect(result).toBe(true); - }); - - it("isWeaponTemplate() slick armor", () => - { - const result = helper.isWeaponTemplate("5e4abb5086f77406975c9342"); - - expect(result).toBe(false); - }); -}); \ No newline at end of file diff --git a/project/tests/common/MockHelper.ts b/project/tests/common/MockHelper.ts deleted file mode 100644 index 1a59d4ba..00000000 --- a/project/tests/common/MockHelper.ts +++ /dev/null @@ -1,57 +0,0 @@ -import * as TypeMoq from "typemoq"; -import { QuestHelper } from "@spt-aki/helpers/QuestHelper"; -import { TraderHelper } from "@spt-aki/helpers/TraderHelper"; - -import { ImageRouter } from "@spt-aki/routers/ImageRouter"; -import { ItemEventRouter } from "@spt-aki/routers/ItemEventRouter"; -import { ConfigServer } from "@spt-aki/servers/ConfigServer"; -import { PaymentService } from "@spt-aki/services/PaymentService"; -import { VFS } from "@spt-aki/utils/VFS"; - -export class MockHelper -{ - public getMockImageRouter(): TypeMoq.IMock - { - return TypeMoq.Mock.ofType(ImageRouter); - } - - public getMockVFS(): TypeMoq.IMock - { - //vfsMock.setup(x => x.getFiles(TypeMoq.It.isAnyString())).returns(() => []); - return TypeMoq.Mock.ofType(VFS); - } - - public getMockConfigServer(): TypeMoq.IMock - { - return TypeMoq.Mock.ofType(TestConfigServer); - } - - public getItemEventRouter(): TypeMoq.IMock - { - return TypeMoq.Mock.ofType(ItemEventRouter); - } - - public getQuestHelper(): TypeMoq.IMock - { - return TypeMoq.Mock.ofType(QuestHelper); - } - - public getTraderHelper(): TypeMoq.IMock - { - return TypeMoq.Mock.ofType(TraderHelper); - } - - public getPaymentServiceMock(): TypeMoq.IMock - { - return TypeMoq.Mock.ofType(PaymentService); - } - -} - -export class TestConfigServer extends ConfigServer -{ - public override initialize(): void - { - return; - } -} \ No newline at end of file diff --git a/project/tests/common/TestHelper.ts b/project/tests/common/TestHelper.ts deleted file mode 100644 index 7dd7eaa9..00000000 --- a/project/tests/common/TestHelper.ts +++ /dev/null @@ -1,156 +0,0 @@ -import { BotHelper } from "@spt-aki/helpers/BotHelper"; -import { HandbookHelper } from "@spt-aki/helpers/HandbookHelper"; -import { ItemHelper } from "@spt-aki/helpers/ItemHelper"; -import { ILogger } from "@spt-aki/models/spt/utils/ILogger"; -import { ConfigServer } from "@spt-aki/servers/ConfigServer"; -import { DatabaseServer } from "@spt-aki/servers/DatabaseServer"; -import { ItemBaseClassService } from "@spt-aki/services/ItemBaseClassService"; -import { LocaleService } from "@spt-aki/services/LocaleService"; -import { LocalisationService } from "@spt-aki/services/LocalisationService"; -import { AsyncQueue } from "@spt-aki/utils/AsyncQueue"; -import { DatabaseImporter } from "@spt-aki/utils/DatabaseImporter"; -import { EncodingUtil } from "@spt-aki/utils/EncodingUtil"; -import { HashUtil } from "@spt-aki/utils/HashUtil"; -import { ImporterUtil } from "@spt-aki/utils/ImporterUtil"; -import { JsonUtil } from "@spt-aki/utils/JsonUtil"; -import { MathUtil } from "@spt-aki/utils/MathUtil"; -import { ObjectId } from "@spt-aki/utils/ObjectId"; -import { RandomUtil } from "@spt-aki/utils/RandomUtil"; -import { TimeUtil } from "@spt-aki/utils/TimeUtil"; -import { UUidGenerator } from "@spt-aki/utils/UUidGenerator"; -import { VFS } from "@spt-aki/utils/VFS"; -import { MockHelper } from "./MockHelper"; -import WinstonLogger from "./__mocks__/WinstonLogger"; - -export class TestHelper -{ - logger: ILogger; - asyncQueue: AsyncQueue; - uuidGenerator: UUidGenerator; - timeUtil: TimeUtil; - vfs: VFS; - hashUtil: HashUtil; - jsonUtil: JsonUtil; - randomUtil: RandomUtil; - encodingUtil: EncodingUtil; - importerUtil: ImporterUtil; - configServer: ConfigServer; - objectId: ObjectId; - mathUtil: MathUtil; - databaseServer: DatabaseServer; - itemHelper: ItemHelper; - localeService: LocaleService; - localisationService: LocalisationService; - handbookHelper: HandbookHelper; - itemBaseClassService: ItemBaseClassService; - botHelper: BotHelper; - - public static async fetchTestHelper() - { - const initTestHelper = new TestHelper(); - const mockHelper = new MockHelper(); - - const dbImporter = new DatabaseImporter( - initTestHelper.logger, - initTestHelper.vfs, - initTestHelper.jsonUtil, - initTestHelper.localisationService, - initTestHelper.databaseServer, - mockHelper.getMockImageRouter().object, - initTestHelper.encodingUtil, - initTestHelper.hashUtil, - initTestHelper.importerUtil - ); - await dbImporter.onLoad(); - return initTestHelper; - } - - constructor() - { - - this.logger = new WinstonLogger(); - this.asyncQueue = new AsyncQueue(); - this.uuidGenerator = new UUidGenerator(); - this.timeUtil = new TimeUtil(); - this.vfs = new VFS(this.asyncQueue, this.uuidGenerator); - this.hashUtil = new HashUtil(this.timeUtil); - this.jsonUtil = new JsonUtil(this.vfs, this.hashUtil, this.logger); - this.randomUtil = new RandomUtil(this.jsonUtil, this.logger); - this.configServer = new ConfigServer(this.logger, this.vfs, this.jsonUtil); - this.objectId = new ObjectId(this.timeUtil); - this.mathUtil = new MathUtil(); - - this.databaseServer = new DatabaseServer(); - this.localeService = new LocaleService(this.logger, this.databaseServer, this.configServer); - this.localisationService = new LocalisationService(this.logger, this.localeService); - - this.encodingUtil = new EncodingUtil(); - this.importerUtil = new ImporterUtil(this.vfs, this.jsonUtil); - - this.handbookHelper = new HandbookHelper(this.databaseServer); - this.itemBaseClassService = new ItemBaseClassService(this.logger, this.localisationService, this.databaseServer); - this.itemHelper = new ItemHelper(this.logger, this.hashUtil, this.jsonUtil, this.randomUtil, this.objectId, this.mathUtil, this.databaseServer, this.handbookHelper, this.itemBaseClassService, this.localisationService, this.localeService); - this.botHelper = new BotHelper(this.logger, this.jsonUtil, this.databaseServer, this.randomUtil, this.localisationService, this.configServer); - } - - public getTestLogger(): ILogger - { - return this.logger; - } - - public getTestUuidGenerator(): UUidGenerator - { - return this.uuidGenerator; - } - - - public getTestVFS(): VFS - { - return this.vfs; - } - - public getTestHashUtil(): HashUtil - { - return new HashUtil(this.timeUtil); - } - - public getTestJsonUtil(): JsonUtil - { - return this.jsonUtil; - } - - public getTestRandomUtil(): RandomUtil - { - return this.randomUtil; - } - - public getTestConfigServer(): ConfigServer - { - return this.configServer; - } - - public getTestItemHelper(): ItemHelper - { - return this.itemHelper; - } - - public getTestDatabaseServer(): DatabaseServer - { - return this.databaseServer; - } - - public getTestLocalisationService(): LocalisationService - { - return this.localisationService; - } - - public getTestBotHelper(): BotHelper - { - return this.botHelper; - } - - public getTestMathUtil(): MathUtil - { - return this.mathUtil; - } -} \ No newline at end of file diff --git a/project/tests/common/__mocks__/WinstonLogger.ts b/project/tests/common/__mocks__/WinstonLogger.ts deleted file mode 100644 index 07b879a5..00000000 --- a/project/tests/common/__mocks__/WinstonLogger.ts +++ /dev/null @@ -1,42 +0,0 @@ -import { Daum } from "@spt-aki/models/eft/itemEvent/IItemEventRouterRequest"; -import { LogBackgroundColor } from "@spt-aki/models/spt/logging/LogBackgroundColor"; -import { LogTextColor } from "@spt-aki/models/spt/logging/LogTextColor"; -import { ILogger } from "@spt-aki/models/spt/utils/ILogger"; - -export default class WinstonLogger implements ILogger -{ - - writeToLogFile(data: string | Daum): void - { - console.log(`writeToLogFile ${data}`); - } - logWithColor(data: string | Record, textColor: LogTextColor, backgroundColor?: LogBackgroundColor | undefined): void - { - console.log(`logWithColor ${data}`); - } - error(data: string): void - { - console.log(`error ${data}`); - } - warning(data: string): void - { - console.log(`warning ${data}`); - } - success(data: string): void - { - console.log(`success ${data}`); - } - info(data: string): void - { - console.log(`info ${data}`); - } - debug(data: string | Record, onlyShowInConsole?: boolean | undefined): void - { - console.log(`debug ${data}`); - } - - log(msg: string): void - { - console.log(msg); - } -} \ No newline at end of file diff --git a/project/tests/helpers/ItemHelper.test.ts b/project/tests/helpers/ItemHelper.test.ts new file mode 100644 index 00000000..a0465437 --- /dev/null +++ b/project/tests/helpers/ItemHelper.test.ts @@ -0,0 +1,28 @@ +import "reflect-metadata"; + +import { container } from "tsyringe"; +import { ItemHelper } from "@spt-aki/helpers/ItemHelper"; + +describe("ItemHelper", () => +{ + let itemHelper: ItemHelper; + + beforeEach(() => + { + itemHelper = container.resolve("ItemHelper"); + }); + + afterEach(() => + { + jest.restoreAllMocks(); + }); + + describe("isValidItem", () => + { + it("should return false when item details are not available", () => + { + const result = itemHelper.isValidItem("non-existent-item"); + expect(result).toBe(false); + }); + }); +}); diff --git a/project/tests/utils/HashUtil.test.ts b/project/tests/utils/HashUtil.test.ts deleted file mode 100644 index a78715de..00000000 --- a/project/tests/utils/HashUtil.test.ts +++ /dev/null @@ -1,32 +0,0 @@ -import "reflect-metadata"; - -import { beforeEach, describe, expect, it } from "@jest/globals"; -import { HashUtil } from "@spt-aki/utils/HashUtil"; -import { TestHelper } from "../common/TestHelper"; - -const testHelper = new TestHelper(); - -describe("test text", () => -{ - let hashUtil: HashUtil; - beforeEach(() => - { - hashUtil = testHelper.getTestHashUtil(); - }); - - it("HashUtil type check", () => - { - expect(hashUtil).toBeInstanceOf(HashUtil); - }); - - it("generate()", () => - { - expect(hashUtil.generate()).toHaveLength(24); - }); - - it("generateMd5ForData()", () => - { - expect(hashUtil.generateMd5ForData("test")).toBe("098f6bcd4621d373cade4e832627b4f6"); - expect(hashUtil.generateMd5ForData("longerString12345678910")).toBe("c3e76c3c118c14e357e61ae1dbad4cf7"); - }); -}); \ No newline at end of file diff --git a/project/tests/utils/ItemHelper.test.ts b/project/tests/utils/ItemHelper.test.ts deleted file mode 100644 index 66bba0a5..00000000 --- a/project/tests/utils/ItemHelper.test.ts +++ /dev/null @@ -1,272 +0,0 @@ -import "reflect-metadata"; - -import { beforeEach, describe, expect, it } from "@jest/globals"; -import { ItemHelper } from "@spt-aki/helpers/ItemHelper"; -import { Item } from "@spt-aki/models/eft/common/tables/IItem"; -import { BaseClasses } from "@spt-aki/models/enums/BaseClasses"; -import { TestHelper } from "../common/TestHelper"; - -let testHelper: TestHelper; - -describe("test text", () => -{ - beforeAll(async () => - { - testHelper = await TestHelper.fetchTestHelper(); - }); - - let itemHelper: ItemHelper; - beforeEach(() => - { - itemHelper = testHelper.getTestItemHelper(); - }); - - it("ItemHelper type check", () => - { - expect(itemHelper).toBeInstanceOf(ItemHelper); - }); - - it("isDogtag() usec", () => - { - expect(itemHelper.isDogtag("59f32c3b86f77472a31742f0")).toBe(true); - }); - - it("isDogtag() bear", () => - { - expect(itemHelper.isDogtag("59f32bb586f774757e1e8442")).toBe(true); - }); - - it("isValidItem() valid item screwdriver", () => - { - expect(itemHelper.isValidItem("590c2d8786f774245b1f03f3")).toBe(true); // screwdriver - }); - - it("isValidItem() invalid item tool base item", () => - { - expect(itemHelper.isValidItem("57864bb7245977548b3b66c2")).toBe(false); // tool base item - }); - - it("isValidItem() invalid item tpl", () => - { - expect(itemHelper.isValidItem("fakeitem")).toBe(false); - }); - - it("isValidItem() valid item with base type in blacklist", () => - { - expect(itemHelper.isValidItem("5b3f3af486f774679e752c1f", [BaseClasses.ARMBAND])).toBe(false); - }); - - it("getItemPrice() valid screwdriver item", () => - { - expect(itemHelper.getItemPrice("590c2d8786f774245b1f03f3")).toBe(3500); // screwdriver - }); - - it("getItemPrice() invalid tpl forces return of 0 price", () => - { - expect(itemHelper.getItemPrice("fakeitem")).toBe(0); - }); - - it("fixItemStackCount() item with no upd object", () => - { - const itemWithNoUpd: Item = { - _id: "test", - _tpl: "123456789" - }; - const result = itemHelper.fixItemStackCount(itemWithNoUpd); - expect(result.upd?.StackObjectsCount).toBe(1); - }); - - it("fixItemStackCount() item with upd object no StackObjectsCount property", () => - { - const itemWithUpdNoStack: Item = { - _id: "test", - _tpl: "123456789", - upd: {} - }; - const result = itemHelper.fixItemStackCount(itemWithUpdNoStack); - expect(result.upd?.StackObjectsCount).toBe(1); - }); - - it("fixItemStackCount() item with upd object and custom stack count", () => - { - const itemWithUpdAndStack: Item = { - _id: "test", - _tpl: "123456789", - upd: { StackObjectsCount: 2 } - }; - const result = itemHelper.fixItemStackCount(itemWithUpdAndStack); - expect(result.upd?.StackObjectsCount).toBe(2); - }); - - it("getItemStackSize() stack size of 4", () => - { - const itemWithStackSizeOf4: Item = { - _id: "", - _tpl: "", - upd: { StackObjectsCount: 4} - }; - const result = itemHelper.getItemStackSize(itemWithStackSizeOf4); - expect(result).toBe(4); - }); - - it("getItemStackSize() upd object no stack property", () => - { - const itemWithUpdNoStack: Item = { - _id: "", - _tpl: "", - upd: {} - }; - const result = itemHelper.getItemStackSize(itemWithUpdNoStack); - expect(result).toBe(1); - }); - - it("getItemStackSize() no upd object", () => - { - const itemWithNoUpdObject: Item = { - _id: "", - _tpl: "" - }; - const result = itemHelper.getItemStackSize(itemWithNoUpdObject); - expect(result).toBe(1); - }); - - it("getItemQualityModifier() no upd object", () => - { - const itemWithNoUpdObject: Item = { - _id: "", - _tpl: "" - }; - const result = itemHelper.getItemQualityModifier(itemWithNoUpdObject); - expect(result).toBe(1); - }); - - it("getItemQualityModifier() grizzly medkit with full hp", () => - { - const medkitItem: Item = { - _id: "", - _tpl: "590c657e86f77412b013051d", - upd: { - MedKit: { - HpResource: 1800 - } - } - }; - const result = itemHelper.getItemQualityModifier(medkitItem); - expect(result).toBe(1); - }); - - it("getItemQualityModifier() grizzly medkit with 0 hp", () => - { - const medkitItem: Item = { - _id: "", - _tpl: "590c657e86f77412b013051d", - upd: { - MedKit: { - HpResource: 0 - } - } - }; - const result = itemHelper.getItemQualityModifier(medkitItem); - expect(result).toBe(0.01); - }); - - it("getItemQualityModifier() repairable slick with full hp", () => - { - const medkitItem: Item = { - _id: "", - _tpl: "5e4abb5086f77406975c9342", - upd: { - Repairable: { - Durability: 80, - MaxDurability: 80 - } - } - }; - const result = itemHelper.getItemQualityModifier(medkitItem); - expect(result).toBe(1); - }); - - it("getItemQualityModifier() repairable slick with 0 hp", () => - { - const medkitItem: Item = { - _id: "", - _tpl: "5e4abb5086f77406975c9342", - upd: { - Repairable: { - Durability: 0, - MaxDurability: 80 - } - } - }; - const result = itemHelper.getItemQualityModifier(medkitItem); - expect(result).toBe(0.01); - }); - - it("getRepairableItemQualityValue() repairable MDR weapon with max durability", () => - { - const mdrItem: Item = { - _id: "", - _tpl: "5c488a752e221602b412af63", - upd: { - Repairable: { - Durability: 100, - MaxDurability: 100 - } - } - }; - - const result = itemHelper.getItemQualityModifier(mdrItem); - expect(result).toBe(1); - }); - - it("hasBuyRestrictions() item with restrictions, not reached", () => - { - const item: Item = { - _id: "", - _tpl: "", - upd: { - BuyRestrictionCurrent: 0, - BuyRestrictionMax: 5 - } - }; - - const result = itemHelper.hasBuyRestrictions(item); - expect(result).toBe(true); - }); - - it("hasBuyRestrictions() item no restrictions defined", () => - { - const item: Item = { - _id: "", - _tpl: "", - upd: { } - }; - - const result = itemHelper.hasBuyRestrictions(item); - expect(result).toBe(false); - }); - - it("doesItemOrParentsIdMatch() item doesnt exist", () => - { - const result = itemHelper.doesItemOrParentsIdMatch("fakeTpl", []); - expect(result).toBe(false); - }); - - it("isQuestItem() item is quest item", () => - { - const result = itemHelper.isQuestItem("5ae9a3f586f7740aab00e4e6"); - expect(result).toBe(true); - }); - - it("isQuestItem() item is not quest item", () => - { - const result = itemHelper.isQuestItem("590c392f86f77444754deb29"); - expect(result).toBe(false); - }); - - it("isQuestItem() invalid item tpl", () => - { - const result = itemHelper.isQuestItem("faketpl"); - expect(result).toBe(false); - }); -}); \ No newline at end of file diff --git a/project/tests/utils/RandomUtil.test.ts b/project/tests/utils/RandomUtil.test.ts deleted file mode 100644 index 4e69cfc1..00000000 --- a/project/tests/utils/RandomUtil.test.ts +++ /dev/null @@ -1,51 +0,0 @@ -import "reflect-metadata"; - -import { beforeEach, describe, expect, it } from "@jest/globals"; - -import { RandomUtil } from "@spt-aki/utils/RandomUtil"; -import { TestHelper } from "../common/TestHelper"; - -const testHelper = new TestHelper(); - -const logger = testHelper.getTestLogger(); -const jsonUtil = testHelper.getTestJsonUtil(); - -describe("test text", () => -{ - let randomUtil: RandomUtil; - beforeEach(() => - { - randomUtil = new RandomUtil(jsonUtil, logger); - }); - - it("RandomUtil type check", () => - { - expect(randomUtil).toBeInstanceOf(RandomUtil); - }); - - it("getInt()", () => - { - expect(randomUtil.getInt(1,1)).toBe(1); - expect(randomUtil.getInt(100,100)).toBe(100); - expect([256,257,258,259,260,261]).toContain(randomUtil.getInt(256,261)); - }); - - it("getIntEx()", () => - { - expect(randomUtil.getIntEx(1)).toBe(1); - expect([1,2,3]).toContain(randomUtil.getIntEx(5)); - expect([1,2,3,4,5,6,7,8]).toContain(randomUtil.getIntEx(10)); - }); - - it("getFloat()", () => - { - const zeroToOneFloat = randomUtil.getFloat(0, 1); - expect(zeroToOneFloat).toBeGreaterThanOrEqual(0); - expect(zeroToOneFloat).toBeLessThan(1); - }); - - it("getBool()", () => - { - expect([true, false]).toContain(randomUtil.getBool()); - }); -}); \ No newline at end of file diff --git a/project/tests/utils/TimeUtil.test.ts b/project/tests/utils/TimeUtil.test.ts deleted file mode 100644 index be3a50aa..00000000 --- a/project/tests/utils/TimeUtil.test.ts +++ /dev/null @@ -1,74 +0,0 @@ -import "reflect-metadata"; - -import { beforeEach, describe, expect, it } from "@jest/globals"; - -import { TimeUtil } from "@spt-aki/utils/TimeUtil"; - -describe("test text", () => -{ - let timeUtil: TimeUtil; - beforeEach(() => - { - timeUtil = new TimeUtil(); - }); - - it("BotHelper type check", () => - { - expect(timeUtil).toBeInstanceOf(TimeUtil); - }); - - it("formatTime()", () => - { - expect(timeUtil.formatTime(new Date(2020, 1, 1, 15, 24, 0))).toBe("15-24-00"); - }); - - it("formatDate()", () => - { - expect(timeUtil.formatDate(new Date(2020, 4, 13))).toBe("2020-05-13"); - }); - - it("getDate()", () => - { - const currentDate = timeUtil.formatDate(new Date()); - expect(timeUtil.getDate()).toBe(currentDate); - }); - - it("getTime()", () => - { - const currentTime = timeUtil.formatTime(new Date()); - expect(timeUtil.getTime()).toBe(currentTime); - }); - - it("getHoursAsSeconds() one hour", () => - { - expect(timeUtil.getHoursAsSeconds(1)).toBe(3600); - }); - - it("getHoursAsSeconds() three hours", () => - { - expect(timeUtil.getHoursAsSeconds(3)).toBe(10800); - }); - - it("getTimestamp()", () => - { - const currentTimestampSecondsFloored = Math.floor(new Date().getTime() / 1000); - expect(timeUtil.getTimestamp()).toBe(currentTimestampSecondsFloored); - }); - - it("getTimeMailFormat()", () => - { - const currentTime = new Date(); - const currentTimeMinutes = (currentTime.getMinutes()).toString().padStart(2,"0"); - const currentTimeHours = (currentTime.getHours()).toString().padStart(2,"0"); - expect(timeUtil.getTimeMailFormat()).toBe(`${currentTimeHours}:${currentTimeMinutes}`); - }); - - it("getDateMailFormat()", () => - { - const currentTime = new Date(); - const currentDay = (currentTime.getDate()).toString().padStart(2,"0"); - const currentMonth = (currentTime.getMonth() + 1).toString().padStart(2,"0"); - const currentYear = currentTime.getFullYear(); - expect(timeUtil.getDateMailFormat()).toBe(`${currentDay}.${currentMonth}.${currentYear}`); - }); -}); \ No newline at end of file diff --git a/project/tests/utils/UUidGenerator.test.ts b/project/tests/utils/UUidGenerator.test.ts deleted file mode 100644 index 1a9a61e6..00000000 --- a/project/tests/utils/UUidGenerator.test.ts +++ /dev/null @@ -1,28 +0,0 @@ -import "reflect-metadata"; - -import { beforeEach, describe, expect, it } from "@jest/globals"; -import { UUidGenerator } from "@spt-aki/utils/UUidGenerator"; -import { TestHelper } from "../common/TestHelper"; - -const testHelper = new TestHelper(); - -describe("test text", () => -{ - let uuidGenerator: UUidGenerator; - beforeEach(() => - { - uuidGenerator = testHelper.getTestUuidGenerator(); - }); - - it("UUidGenerator type check", () => - { - expect(uuidGenerator).toBeInstanceOf(UUidGenerator); - }); - - it("generate()", () => - { - expect(uuidGenerator.generate()).toHaveLength(36); - expect(uuidGenerator.generate()).toContain("-"); - expect(uuidGenerator.generate()).toContain("4"); - }); -}); \ No newline at end of file