2023-11-05 17:44:08 +01:00
|
|
|
import "reflect-metadata";
|
2024-07-23 17:12:53 +02:00
|
|
|
import { ItemBaseClassService } from "@spt/services/ItemBaseClassService";
|
2023-11-10 23:21:20 +01:00
|
|
|
import { container } from "tsyringe";
|
|
|
|
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
2023-11-13 17:43:37 +01:00
|
|
|
|
2024-07-23 17:12:53 +02:00
|
|
|
describe("ItemBaseClassService", () => {
|
2023-11-05 17:44:08 +01:00
|
|
|
let itemBaseClassService: any;
|
|
|
|
|
2024-07-23 17:12:53 +02:00
|
|
|
beforeEach(() => {
|
2023-11-05 17:44:08 +01:00
|
|
|
itemBaseClassService = container.resolve<ItemBaseClassService>("ItemBaseClassService");
|
|
|
|
});
|
|
|
|
|
2024-07-23 17:12:53 +02:00
|
|
|
afterEach(() => {
|
2023-11-05 17:44:08 +01:00
|
|
|
vi.restoreAllMocks();
|
|
|
|
});
|
|
|
|
|
2024-07-23 17:12:53 +02:00
|
|
|
describe("hydrateItemBaseClassCache", () => {
|
|
|
|
it("should hydrate itemBaseClassesCache dictionary property", () => {
|
2023-11-05 17:44:08 +01:00
|
|
|
itemBaseClassService.cacheGenerated = false;
|
|
|
|
itemBaseClassService.itemBaseClassesCache = {};
|
|
|
|
itemBaseClassService.hydrateItemBaseClassCache();
|
|
|
|
|
|
|
|
expect(Object.keys(itemBaseClassService.itemBaseClassesCache).length).greaterThan(100);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2024-07-23 17:12:53 +02:00
|
|
|
describe("itemHasBaseClass", () => {
|
|
|
|
it("should return false when undefined is passed in", () => {
|
2023-11-05 17:44:08 +01:00
|
|
|
const result = itemBaseClassService.itemHasBaseClass(undefined, []);
|
|
|
|
|
|
|
|
expect(result).toBe(false);
|
|
|
|
});
|
|
|
|
|
2024-07-23 17:12:53 +02:00
|
|
|
it("should return false when the base item type is passed in", () => {
|
2023-11-05 17:44:08 +01:00
|
|
|
// Remove item from base cache
|
2024-04-11 05:15:28 +02:00
|
|
|
const result = itemBaseClassService.itemHasBaseClass("54009119af1c881c07000029", []);
|
2023-11-05 17:44:08 +01:00
|
|
|
|
|
|
|
expect(result).toBe(false);
|
|
|
|
});
|
|
|
|
|
2024-07-23 17:12:53 +02:00
|
|
|
it("should return true when a med item is passed in with the meds base class", () => {
|
2023-11-05 17:44:08 +01:00
|
|
|
const salewaTpl = "544fb45d4bdc2dee738b4568";
|
|
|
|
|
|
|
|
// Remove item from base cache
|
|
|
|
delete itemBaseClassService.itemBaseClassesCache[salewaTpl];
|
2024-04-11 05:15:28 +02:00
|
|
|
const result = itemBaseClassService.itemHasBaseClass(salewaTpl, ["543be5664bdc2dd4348b4569"]);
|
2023-11-05 17:44:08 +01:00
|
|
|
|
|
|
|
expect(result).toBe(true);
|
|
|
|
});
|
|
|
|
|
2024-07-23 17:12:53 +02:00
|
|
|
it("should return true when an item and two matching base classes are passed in", () => {
|
2023-11-05 17:44:08 +01:00
|
|
|
const salewaTpl = "544fb45d4bdc2dee738b4568";
|
|
|
|
|
|
|
|
// Remove item from base cache
|
|
|
|
delete itemBaseClassService.itemBaseClassesCache[salewaTpl];
|
2023-11-10 23:21:20 +01:00
|
|
|
const result = itemBaseClassService.itemHasBaseClass(salewaTpl, [
|
|
|
|
"543be5664bdc2dd4348b4569",
|
|
|
|
"54009119af1c881c07000029",
|
2024-04-11 05:15:28 +02:00
|
|
|
]); // "Meds" and "Item" base classes
|
2023-11-05 17:44:08 +01:00
|
|
|
|
|
|
|
expect(result).toBe(true);
|
|
|
|
});
|
|
|
|
|
2024-07-23 17:12:53 +02:00
|
|
|
it("should return true when an item is passed in and cache has not been generated", () => {
|
2023-11-05 17:44:08 +01:00
|
|
|
// Set cache to false
|
|
|
|
itemBaseClassService.cacheGenerated = false;
|
2023-11-06 20:39:12 +01:00
|
|
|
|
2023-11-05 17:44:08 +01:00
|
|
|
const hydrateItemBaseClassCacheSpy = vi.spyOn(itemBaseClassService, "hydrateItemBaseClassCache");
|
2023-11-06 20:39:12 +01:00
|
|
|
|
2023-11-05 17:44:08 +01:00
|
|
|
// Remove item from base cache
|
|
|
|
const salewaTpl = "544fb45d4bdc2dee738b4568";
|
|
|
|
delete itemBaseClassService.itemBaseClassesCache[salewaTpl];
|
|
|
|
|
|
|
|
// Perform check
|
2024-04-11 05:15:28 +02:00
|
|
|
const result = itemBaseClassService.itemHasBaseClass(salewaTpl, ["543be5664bdc2dd4348b4569"]);
|
2023-11-05 17:44:08 +01:00
|
|
|
|
|
|
|
expect(result).toBe(true);
|
|
|
|
expect(hydrateItemBaseClassCacheSpy).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
2024-07-23 17:12:53 +02:00
|
|
|
it("should return false for any item template ID that does not exist", () => {
|
2024-04-11 05:15:28 +02:00
|
|
|
const result = itemBaseClassService.itemHasBaseClass("not-a-valid-template-id", [
|
|
|
|
"543be5664bdc2dd4348b4569",
|
|
|
|
]);
|
|
|
|
|
|
|
|
expect(result).toBe(false);
|
|
|
|
});
|
|
|
|
|
2024-07-23 17:12:53 +02:00
|
|
|
it("should return false for any item template ID without the Item type ", () => {
|
2024-04-11 05:15:28 +02:00
|
|
|
const result = itemBaseClassService.itemHasBaseClass("54009119af1c881c07000029", [
|
|
|
|
"543be5664bdc2dd4348b4569",
|
|
|
|
]);
|
|
|
|
|
|
|
|
expect(result).toBe(false);
|
2023-11-05 17:44:08 +01:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2024-07-23 17:12:53 +02:00
|
|
|
describe("getItemBaseClasses", () => {
|
|
|
|
it("should return empty array when undefined is passed in", () => {
|
2023-11-05 17:44:08 +01:00
|
|
|
const result = itemBaseClassService.getItemBaseClasses(undefined);
|
|
|
|
|
|
|
|
expect(result).toStrictEqual([]);
|
|
|
|
});
|
|
|
|
|
2024-07-23 17:12:53 +02:00
|
|
|
it("should return empty array when the base item type is passed in", () => {
|
2023-11-05 17:44:08 +01:00
|
|
|
// Remove item from base cache
|
|
|
|
const result = itemBaseClassService.getItemBaseClasses("54009119af1c881c07000029");
|
|
|
|
|
|
|
|
expect(result).toStrictEqual([]);
|
|
|
|
});
|
|
|
|
|
2024-07-23 17:12:53 +02:00
|
|
|
it("should return array of 3 items when an item is passed in", () => {
|
2023-11-05 17:44:08 +01:00
|
|
|
const salewaTpl = "544fb45d4bdc2dee738b4568";
|
|
|
|
|
|
|
|
const result = itemBaseClassService.getItemBaseClasses(salewaTpl);
|
|
|
|
|
|
|
|
expect(result.length).toBe(3);
|
|
|
|
});
|
|
|
|
|
2024-07-23 17:12:53 +02:00
|
|
|
it("should return array of 3 items when an item is passed in and cache has not been generated", () => {
|
2023-11-05 17:44:08 +01:00
|
|
|
itemBaseClassService.cacheGenerated = false;
|
|
|
|
const hydrateItemBaseClassCacheSpy = vi.spyOn(itemBaseClassService, "hydrateItemBaseClassCache");
|
|
|
|
|
|
|
|
const salewaTpl = "544fb45d4bdc2dee738b4568";
|
|
|
|
const result = itemBaseClassService.getItemBaseClasses(salewaTpl);
|
|
|
|
|
|
|
|
expect(result.length).toBe(3);
|
|
|
|
expect(hydrateItemBaseClassCacheSpy).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
2024-07-23 17:12:53 +02:00
|
|
|
it("should return base item type when an item is passed in", () => {
|
2023-11-05 17:44:08 +01:00
|
|
|
const salewaTpl = "544fb45d4bdc2dee738b4568";
|
|
|
|
const result = itemBaseClassService.getItemBaseClasses(salewaTpl);
|
|
|
|
|
|
|
|
expect(result).toContain("54009119af1c881c07000029");
|
|
|
|
expect(result).toContain("5448f39d4bdc2d0a728b4568");
|
|
|
|
});
|
|
|
|
|
2024-07-23 17:12:53 +02:00
|
|
|
it("should return empty array when an invalid item is passed in", () => {
|
2023-11-05 17:44:08 +01:00
|
|
|
const result = itemBaseClassService.getItemBaseClasses("fakeTpl");
|
|
|
|
expect(result).toStrictEqual([]);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|