2023-11-03 22:39:33 +01:00
|
|
|
/* eslint-disable @typescript-eslint/naming-convention */
|
2023-11-01 04:08:13 +01:00
|
|
|
import "reflect-metadata";
|
|
|
|
import { container } from "tsyringe";
|
2023-11-10 23:21:20 +01:00
|
|
|
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
2023-11-01 04:08:13 +01:00
|
|
|
|
|
|
|
import { InsuranceController } from "@spt-aki/controllers/InsuranceController";
|
2023-11-02 06:47:28 +01:00
|
|
|
import { ProfileInsuranceFactory } from "@tests/__factories__/ProfileInsurance.factory";
|
2023-11-01 04:08:13 +01:00
|
|
|
|
2023-11-10 23:21:20 +01:00
|
|
|
import { ItemHelper } from "@spt-aki/helpers/ItemHelper";
|
2023-11-01 04:08:13 +01:00
|
|
|
import { TraderHelper } from "@spt-aki/helpers/TraderHelper";
|
|
|
|
import { Item } from "@spt-aki/models/eft/common/tables/IItem";
|
2023-11-02 06:47:28 +01:00
|
|
|
import { Insurance } from "@spt-aki/models/eft/profile/IAkiProfile";
|
2023-11-10 23:21:20 +01:00
|
|
|
import { MessageType } from "@spt-aki/models/enums/MessageType";
|
2023-11-01 04:08:13 +01:00
|
|
|
|
|
|
|
describe("InsuranceController", () =>
|
|
|
|
{
|
|
|
|
let insuranceController: any; // Using "any" to access private/protected methods without type errors.
|
2023-11-02 06:47:28 +01:00
|
|
|
let insuranceFixture: Insurance[];
|
2023-11-01 04:08:13 +01:00
|
|
|
|
2023-11-02 06:47:28 +01:00
|
|
|
beforeEach(() =>
|
2023-11-01 04:08:13 +01:00
|
|
|
{
|
|
|
|
insuranceController = container.resolve<InsuranceController>("InsuranceController");
|
2023-11-02 06:47:28 +01:00
|
|
|
insuranceFixture = new ProfileInsuranceFactory().adjustPackageDates().get();
|
2023-11-01 04:08:13 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() =>
|
|
|
|
{
|
|
|
|
vi.restoreAllMocks();
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("processReturn", () =>
|
|
|
|
{
|
|
|
|
it("should process return for all profiles", () =>
|
|
|
|
{
|
|
|
|
const session1 = "session1";
|
|
|
|
const session2 = "session2";
|
2023-11-13 18:38:16 +01:00
|
|
|
const profiles = { [session1]: {}, [session2]: {} };
|
2023-11-01 04:08:13 +01:00
|
|
|
const getProfilesSpy = vi.spyOn(insuranceController.saveServer, "getProfiles").mockReturnValue(profiles);
|
2023-11-10 23:21:20 +01:00
|
|
|
const processReturnByProfileSpy = vi.spyOn(insuranceController, "processReturnByProfile").mockReturnValue(
|
|
|
|
vi.fn(),
|
|
|
|
);
|
2023-11-01 04:08:13 +01:00
|
|
|
|
|
|
|
// Execute the method.
|
|
|
|
insuranceController.processReturn();
|
|
|
|
|
|
|
|
// Should make a call to get all of the profiles.
|
|
|
|
expect(getProfilesSpy).toHaveBeenCalledTimes(1);
|
|
|
|
|
|
|
|
// Should process each returned profile.
|
|
|
|
expect(processReturnByProfileSpy).toHaveBeenCalledTimes(2);
|
|
|
|
expect(processReturnByProfileSpy).toHaveBeenCalledWith(session1);
|
|
|
|
expect(processReturnByProfileSpy).toHaveBeenCalledWith(session2);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should not attempt to process profiles if no profiles exist", () =>
|
|
|
|
{
|
2023-11-10 23:21:20 +01:00
|
|
|
const processReturnByProfileSpy = vi.spyOn(insuranceController, "processReturnByProfile")
|
|
|
|
.mockImplementation(vi.fn());
|
2023-11-01 04:08:13 +01:00
|
|
|
|
|
|
|
// Execute the method.
|
|
|
|
insuranceController.processReturn();
|
|
|
|
|
|
|
|
// Should not process any profiles.
|
2023-11-02 06:47:28 +01:00
|
|
|
expect(processReturnByProfileSpy).not.toHaveBeenCalled();
|
2023-11-01 04:08:13 +01:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2023-11-03 22:39:33 +01:00
|
|
|
describe("processReturnByProfile", () =>
|
|
|
|
{
|
|
|
|
it("should process insurance for a profile", () =>
|
|
|
|
{
|
|
|
|
const sessionId = "session-id";
|
|
|
|
|
|
|
|
// Mock internal methods.
|
2023-11-13 18:31:52 +01:00
|
|
|
const mockFilterInsuredItems = vi.spyOn(insuranceController, "filterInsuredItems").mockReturnValue(
|
|
|
|
insuranceFixture,
|
|
|
|
);
|
|
|
|
const mockProcessInsuredItems = vi.spyOn(insuranceController, "processInsuredItems").mockImplementation(
|
|
|
|
vi.fn(),
|
|
|
|
);
|
2023-11-03 22:39:33 +01:00
|
|
|
|
|
|
|
insuranceController.processReturnByProfile(sessionId);
|
|
|
|
|
|
|
|
// Verify that the correct methods were called.
|
|
|
|
expect(mockFilterInsuredItems).toBeCalledTimes(1);
|
|
|
|
expect(mockProcessInsuredItems).toHaveBeenNthCalledWith(1, insuranceFixture, sessionId);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should skip processing if there are no insurance packages found within the profile", () =>
|
|
|
|
{
|
|
|
|
const sessionId = "session-id";
|
|
|
|
|
|
|
|
// Mock internal methods.
|
2023-11-13 18:31:52 +01:00
|
|
|
const mockFilterInsuredItems = vi.spyOn(insuranceController, "filterInsuredItems").mockReturnValue([]); // Return an empty array.
|
|
|
|
const mockProcessInsuredItems = vi.spyOn(insuranceController, "processInsuredItems").mockImplementation(
|
|
|
|
vi.fn(),
|
|
|
|
);
|
2023-11-03 22:39:33 +01:00
|
|
|
|
|
|
|
insuranceController.processReturnByProfile(sessionId);
|
|
|
|
|
|
|
|
// Verify that the correct methods were called.
|
|
|
|
expect(mockFilterInsuredItems).toBeCalledTimes(1);
|
|
|
|
expect(mockProcessInsuredItems).not.toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("filterInsuredItems", () =>
|
|
|
|
{
|
|
|
|
it("should return all insurance packages if no time is specified", () =>
|
|
|
|
{
|
|
|
|
const sessionID = "session-id";
|
|
|
|
const insured = JSON.parse(JSON.stringify(insuranceFixture));
|
|
|
|
|
|
|
|
// Mock getProfile to return the fixture.
|
2023-11-13 18:31:52 +01:00
|
|
|
const mockGetProfile = vi.spyOn(insuranceController.saveServer, "getProfile").mockReturnValue({
|
|
|
|
insurance: insured,
|
|
|
|
});
|
2023-11-03 22:39:33 +01:00
|
|
|
const mockLoggerDebug = vi.spyOn(insuranceController.logger, "debug");
|
|
|
|
|
|
|
|
// Execute the method.
|
|
|
|
const insuredFiltered = insuranceController.filterInsuredItems(sessionID);
|
|
|
|
|
|
|
|
// Verify that the correct methods were called.
|
|
|
|
expect(mockGetProfile).toBeCalledTimes(1);
|
2023-11-10 23:21:20 +01:00
|
|
|
expect(mockLoggerDebug).toBeCalledWith(
|
|
|
|
`Found ${insuranceFixture.length} insurance packages in profile ${sessionID}`,
|
|
|
|
);
|
2023-11-03 22:39:33 +01:00
|
|
|
expect(insuredFiltered.length).toBe(insuranceFixture.length);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should filter out insurance packages with scheduledTime values in the future", () =>
|
|
|
|
{
|
|
|
|
const sessionID = "session-id";
|
|
|
|
const insured = JSON.parse(JSON.stringify(insuranceFixture));
|
|
|
|
|
|
|
|
// Set the scheduledTime to 2 hours in the future so it should be skipped over.
|
|
|
|
insured[0].scheduledTime = Math.floor((Date.now() / 1000) + (2 * 60 * 60));
|
|
|
|
|
|
|
|
// Mock getProfile to return the fixture.
|
2023-11-13 18:31:52 +01:00
|
|
|
const mockGetProfile = vi.spyOn(insuranceController.saveServer, "getProfile").mockReturnValue({
|
|
|
|
insurance: insured,
|
|
|
|
});
|
2023-11-03 22:39:33 +01:00
|
|
|
const mockLoggerDebug = vi.spyOn(insuranceController.logger, "debug");
|
|
|
|
|
|
|
|
// Execute the method.
|
|
|
|
const insuredFiltered = insuranceController.filterInsuredItems(sessionID);
|
|
|
|
|
|
|
|
// Verify that the correct methods were called.
|
|
|
|
expect(mockGetProfile).toBeCalledTimes(1);
|
2023-11-10 23:21:20 +01:00
|
|
|
expect(mockLoggerDebug).toBeCalledWith(
|
|
|
|
`Found ${insuranceFixture.length} insurance packages in profile ${sessionID}`,
|
|
|
|
);
|
2023-11-03 22:39:33 +01:00
|
|
|
expect(insuredFiltered.length).toBe(insuranceFixture.length - 1); // Should be 1 less than the original fixture.
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should return an empty array if no insurance packages match the criteria", () =>
|
|
|
|
{
|
|
|
|
const sessionID = "session-id";
|
|
|
|
const insured = JSON.parse(JSON.stringify(insuranceFixture));
|
|
|
|
|
|
|
|
// Mock getProfile to return the fixture.
|
2023-11-13 18:31:52 +01:00
|
|
|
const mockGetProfile = vi.spyOn(insuranceController.saveServer, "getProfile").mockReturnValue({
|
|
|
|
insurance: insured,
|
|
|
|
});
|
2023-11-03 22:39:33 +01:00
|
|
|
const mockLoggerDebug = vi.spyOn(insuranceController.logger, "debug");
|
|
|
|
|
|
|
|
// Execute the method, passing in a time that's two hours in the past. The function should use this past
|
|
|
|
// date as the target to judge if an insurance package is ready to be sent or not.
|
2023-11-10 23:21:20 +01:00
|
|
|
const insuredFiltered = insuranceController.filterInsuredItems(
|
|
|
|
sessionID,
|
|
|
|
Math.floor((Date.now() / 1000) - (2 * 60 * 60)),
|
|
|
|
);
|
2023-11-03 22:39:33 +01:00
|
|
|
|
|
|
|
// Verify that the correct methods were called.
|
|
|
|
expect(mockGetProfile).toBeCalledTimes(1);
|
2023-11-10 23:21:20 +01:00
|
|
|
expect(mockLoggerDebug).toBeCalledWith(
|
|
|
|
`Found ${insuranceFixture.length} insurance packages in profile ${sessionID}`,
|
|
|
|
);
|
2023-11-03 22:39:33 +01:00
|
|
|
|
|
|
|
// Verify that the returned array is empty.
|
|
|
|
expect(insuredFiltered.length).toBe(0);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("processInsuredItems", () =>
|
|
|
|
{
|
|
|
|
it("should log information about the insurance package", () =>
|
|
|
|
{
|
|
|
|
const sessionId = "session-id";
|
|
|
|
|
|
|
|
// Spy on the logger.debug method.
|
|
|
|
const mockLoggerDebug = vi.spyOn(insuranceController.logger, "debug");
|
|
|
|
vi.spyOn(insuranceController, "findItemsToDelete").mockImplementation(vi.fn());
|
|
|
|
vi.spyOn(insuranceController, "removeItemsFromInsurance").mockImplementation(vi.fn());
|
|
|
|
vi.spyOn(insuranceController, "adoptOrphanedItems").mockImplementation(vi.fn());
|
|
|
|
vi.spyOn(insuranceController, "sendMail").mockImplementation(vi.fn());
|
|
|
|
vi.spyOn(insuranceController, "removeInsurancePackageFromProfile").mockImplementation(vi.fn());
|
|
|
|
|
|
|
|
// Execute the method.
|
|
|
|
insuranceController.processInsuredItems(insuranceFixture, sessionId);
|
|
|
|
|
|
|
|
// Verify that the log was written.
|
2023-11-10 23:21:20 +01:00
|
|
|
expect(mockLoggerDebug).toBeCalledWith(
|
|
|
|
`Processing ${insuranceFixture.length} insurance packages, which includes a total of ${
|
|
|
|
insuranceController.countAllInsuranceItems(insuranceFixture)
|
|
|
|
} items, in profile ${sessionId}`,
|
|
|
|
);
|
2023-11-03 22:39:33 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should call processing methods once per insurance package", () =>
|
|
|
|
{
|
|
|
|
const sessionId = "session-id";
|
|
|
|
const packageCount = insuranceFixture.length;
|
|
|
|
|
|
|
|
// Spy on the processing methods.
|
2023-11-10 23:21:20 +01:00
|
|
|
const mockFindItemsToDelete = vi.spyOn(insuranceController, "findItemsToDelete").mockImplementation(
|
|
|
|
vi.fn(),
|
|
|
|
);
|
|
|
|
const mockRemoveItemsFromInsurance = vi.spyOn(insuranceController, "removeItemsFromInsurance")
|
|
|
|
.mockImplementation(vi.fn());
|
|
|
|
const mockAdoptOrphanedItems = vi.spyOn(insuranceController, "adoptOrphanedItems").mockImplementation(
|
|
|
|
vi.fn(),
|
|
|
|
);
|
2023-11-03 22:39:33 +01:00
|
|
|
const mockSendMail = vi.spyOn(insuranceController, "sendMail").mockImplementation(vi.fn());
|
2023-11-10 23:21:20 +01:00
|
|
|
const mockRemoveInsurancePackageFromProfile = vi.spyOn(
|
|
|
|
insuranceController,
|
|
|
|
"removeInsurancePackageFromProfile",
|
|
|
|
).mockImplementation(vi.fn());
|
2023-11-03 22:39:33 +01:00
|
|
|
|
|
|
|
// Execute the method.
|
|
|
|
insuranceController.processInsuredItems(insuranceFixture, sessionId);
|
|
|
|
|
|
|
|
// Verify that the processing methods were called once per insurance package.
|
|
|
|
expect(mockFindItemsToDelete).toBeCalledTimes(packageCount);
|
|
|
|
expect(mockRemoveItemsFromInsurance).toBeCalledTimes(packageCount);
|
|
|
|
expect(mockAdoptOrphanedItems).toBeCalledTimes(packageCount);
|
|
|
|
expect(mockSendMail).toBeCalledTimes(packageCount);
|
|
|
|
expect(mockRemoveInsurancePackageFromProfile).toBeCalledTimes(packageCount);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("countAllInsuranceItems", () =>
|
|
|
|
{
|
|
|
|
it("should return the total number of items in all insurance packages", () =>
|
|
|
|
{
|
2023-11-13 18:31:52 +01:00
|
|
|
const insurance = [{
|
|
|
|
_id: "1",
|
|
|
|
upd: 1234567890,
|
2023-11-13 18:38:16 +01:00
|
|
|
items: [{ _id: "1", parentId: "1", slotId: "1" }, { _id: "2", parentId: "1", slotId: "2" }],
|
2023-11-13 18:31:52 +01:00
|
|
|
}, {
|
|
|
|
_id: "2",
|
|
|
|
upd: 1234567890,
|
2023-11-13 18:38:16 +01:00
|
|
|
items: [{ _id: "3", parentId: "2", slotId: "1" }, { _id: "4", parentId: "2", slotId: "2" }, {
|
2023-11-13 18:31:52 +01:00
|
|
|
_id: "5",
|
|
|
|
parentId: "2",
|
|
|
|
slotId: "3",
|
|
|
|
}],
|
|
|
|
}];
|
2023-11-03 22:39:33 +01:00
|
|
|
const expectedCount = 5; // 2 items in the first package + 3 items in the second package.
|
|
|
|
|
|
|
|
// Execute the method.
|
|
|
|
const actualCount = insuranceController.countAllInsuranceItems(insurance);
|
|
|
|
|
|
|
|
// Verify that the result is correct.
|
|
|
|
expect(actualCount).toBe(expectedCount);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should return 0 if there are no insurance packages", () =>
|
|
|
|
{
|
|
|
|
const insurance = [];
|
|
|
|
const expectedCount = 0;
|
|
|
|
|
|
|
|
// Execute the method.
|
|
|
|
const actualCount = insuranceController.countAllInsuranceItems(insurance);
|
|
|
|
|
|
|
|
// Verify that the result is correct.
|
|
|
|
expect(actualCount).toBe(expectedCount);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should return 0 if there are no items in any of the insurance packages", () =>
|
|
|
|
{
|
2023-11-13 18:38:16 +01:00
|
|
|
const insurance = [{ _id: "1", upd: 1234567890, items: [] }, { _id: "2", upd: 1234567890, items: [] }];
|
2023-11-03 22:39:33 +01:00
|
|
|
const expectedCount = 0;
|
|
|
|
|
|
|
|
// Execute the method.
|
|
|
|
const actualCount = insuranceController.countAllInsuranceItems(insurance);
|
|
|
|
|
|
|
|
// Verify that the result is correct.
|
|
|
|
expect(actualCount).toBe(expectedCount);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("removeInsurancePackageFromProfile", () =>
|
|
|
|
{
|
|
|
|
it("should remove the specified insurance package from the profile", () =>
|
|
|
|
{
|
|
|
|
const sessionID = "session-id";
|
2023-11-13 18:38:16 +01:00
|
|
|
const packageToRemove = { date: "01.11.2023", time: "10:51", location: "factory4_day" };
|
2023-11-03 22:39:33 +01:00
|
|
|
const profile = {
|
2023-11-13 18:31:52 +01:00
|
|
|
insurance: [{
|
2023-11-13 18:38:16 +01:00
|
|
|
messageContent: { systemData: { date: "01.11.2023", time: "11:18", location: "factory4_day" } },
|
2023-11-13 18:31:52 +01:00
|
|
|
}, { // This one should be removed
|
2023-11-13 18:38:16 +01:00
|
|
|
messageContent: { systemData: { date: "01.11.2023", time: "10:51", location: "factory4_day" } },
|
2023-11-13 18:31:52 +01:00
|
|
|
}],
|
2023-11-03 22:39:33 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
// Mock the getProfile method to return the above profile.
|
|
|
|
vi.spyOn(insuranceController.saveServer, "getProfile").mockReturnValue(profile);
|
|
|
|
|
|
|
|
// Execute the method.
|
|
|
|
insuranceController.removeInsurancePackageFromProfile(sessionID, packageToRemove);
|
|
|
|
|
|
|
|
// Verify that the specified insurance package was removed.
|
|
|
|
expect(profile.insurance.length).toBe(1);
|
|
|
|
expect(profile.insurance[0].messageContent.systemData).toStrictEqual({
|
|
|
|
date: "01.11.2023",
|
|
|
|
time: "11:18",
|
2023-11-10 23:21:20 +01:00
|
|
|
location: "factory4_day",
|
2023-11-03 22:39:33 +01:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should log a message indicating that the package was removed", () =>
|
|
|
|
{
|
|
|
|
const sessionID = "session-id";
|
2023-11-13 18:38:16 +01:00
|
|
|
const packageToRemove = { date: "01.11.2023", time: "10:51", location: "factory4_day" };
|
2023-11-03 22:39:33 +01:00
|
|
|
const profile = {
|
2023-11-13 18:31:52 +01:00
|
|
|
insurance: [{
|
2023-11-13 18:38:16 +01:00
|
|
|
messageContent: { systemData: { date: "01.11.2023", time: "10:51", location: "factory4_day" } },
|
2023-11-13 18:31:52 +01:00
|
|
|
}],
|
2023-11-03 22:39:33 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
// Mock the getProfile method to return the above profile.
|
|
|
|
vi.spyOn(insuranceController.saveServer, "getProfile").mockReturnValue(profile);
|
|
|
|
|
|
|
|
// Spy on the logger.debug method.
|
|
|
|
const mockLoggerDebug = vi.spyOn(insuranceController.logger, "debug");
|
|
|
|
|
|
|
|
// Execute the method.
|
|
|
|
insuranceController.removeInsurancePackageFromProfile(sessionID, packageToRemove);
|
|
|
|
|
|
|
|
// Verify that the log was written.
|
2023-11-10 23:21:20 +01:00
|
|
|
expect(mockLoggerDebug).toBeCalledWith(
|
|
|
|
`Removed insurance package with date: ${packageToRemove.date}, time: ${packageToRemove.time}, and location: ${packageToRemove.location} from profile ${sessionID}. Remaining packages: ${profile.insurance.length}`,
|
|
|
|
);
|
2023-11-03 22:39:33 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should not remove any packages if the specified package is not found", () =>
|
|
|
|
{
|
|
|
|
const sessionID = "session-id";
|
2023-11-13 18:38:16 +01:00
|
|
|
const packageToRemove = { date: "01.11.2023", time: "10:51", location: "factory4_day" };
|
2023-11-03 22:39:33 +01:00
|
|
|
const profile = {
|
2023-11-13 18:31:52 +01:00
|
|
|
insurance: [{
|
2023-11-13 18:38:16 +01:00
|
|
|
messageContent: { systemData: { date: "02.11.2023", time: "10:50", location: "factory4_night" } },
|
2023-11-13 18:31:52 +01:00
|
|
|
}],
|
2023-11-03 22:39:33 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
// Mock the getProfile method to return the above profile.
|
|
|
|
vi.spyOn(insuranceController.saveServer, "getProfile").mockReturnValue(profile);
|
|
|
|
|
|
|
|
// Execute the method.
|
|
|
|
insuranceController.removeInsurancePackageFromProfile(sessionID, packageToRemove);
|
|
|
|
|
|
|
|
// Verify that no packages were removed.
|
|
|
|
expect(profile.insurance.length).toBe(1);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2023-11-01 04:08:13 +01:00
|
|
|
describe("findItemsToDelete", () =>
|
|
|
|
{
|
2023-11-02 06:47:28 +01:00
|
|
|
it("should handle an empty insurance package", () =>
|
2023-11-01 04:08:13 +01:00
|
|
|
{
|
2023-11-02 06:47:28 +01:00
|
|
|
const insurancePackage = insuranceFixture[0];
|
|
|
|
insurancePackage.items = [];
|
|
|
|
|
2023-11-03 22:39:33 +01:00
|
|
|
// Execute the method.
|
2023-11-02 06:47:28 +01:00
|
|
|
const result = insuranceController.findItemsToDelete(insurancePackage);
|
2023-11-03 22:39:33 +01:00
|
|
|
|
|
|
|
// Verify that the result is correct.
|
2023-11-01 04:08:13 +01:00
|
|
|
expect(result.size).toBe(0);
|
|
|
|
});
|
|
|
|
|
2023-11-02 06:47:28 +01:00
|
|
|
it("should handle regular items", () =>
|
2023-11-01 04:08:13 +01:00
|
|
|
{
|
2023-11-02 06:47:28 +01:00
|
|
|
// Remove attachment items from the fixture.
|
|
|
|
insuranceFixture = new ProfileInsuranceFactory().adjustPackageDates().removeAttachmentItems().get();
|
|
|
|
const insured = insuranceFixture[0];
|
|
|
|
const numberOfItems = insured.items.length;
|
|
|
|
|
|
|
|
// Mock helper methods.
|
|
|
|
const mockPopulateItemsMap = vi.spyOn(insuranceController, "populateItemsMap");
|
|
|
|
const mockPopulateParentAttachmentsMap = vi.spyOn(insuranceController, "populateParentAttachmentsMap");
|
|
|
|
const mockIsAttachmentAttached = vi.spyOn(insuranceController.itemHelper, "isAttachmentAttached");
|
2023-11-10 23:21:20 +01:00
|
|
|
const mockProcessAttachments = vi.spyOn(insuranceController, "processAttachments").mockImplementation(
|
|
|
|
vi.fn(),
|
|
|
|
);
|
2023-11-02 06:47:28 +01:00
|
|
|
|
|
|
|
// Add all items to the toDelete set. Not realistic, but it's fine for this test.
|
2023-11-01 04:08:13 +01:00
|
|
|
const mockProcessRegularItems = vi.fn((insured, toDelete) =>
|
|
|
|
{
|
2023-11-10 23:21:20 +01:00
|
|
|
insured.items.forEach((item) => toDelete.add(item._id));
|
2023-11-01 04:08:13 +01:00
|
|
|
});
|
|
|
|
vi.spyOn(insuranceController, "processRegularItems").mockImplementation(mockProcessRegularItems);
|
|
|
|
|
|
|
|
// Execute the method.
|
|
|
|
const result = insuranceController.findItemsToDelete(insured);
|
|
|
|
|
2023-11-02 06:47:28 +01:00
|
|
|
// Verify that the correct methods were called.
|
|
|
|
expect(mockPopulateItemsMap).toHaveBeenCalledTimes(1);
|
|
|
|
expect(mockPopulateParentAttachmentsMap).toHaveBeenCalledTimes(1);
|
|
|
|
expect(mockIsAttachmentAttached).toHaveBeenCalledTimes(numberOfItems + 1); // Once for each item, plus once more
|
|
|
|
expect(mockProcessRegularItems).toHaveBeenCalledTimes(1);
|
2023-11-01 04:08:13 +01:00
|
|
|
expect(mockProcessAttachments).not.toHaveBeenCalled();
|
2023-11-02 06:47:28 +01:00
|
|
|
|
|
|
|
// Verify that the result is correct.
|
|
|
|
expect(result.size).toBe(numberOfItems);
|
2023-11-10 23:21:20 +01:00
|
|
|
expect(result).toEqual(new Set(insured.items.map((item) => item._id)));
|
2023-11-01 04:08:13 +01:00
|
|
|
});
|
|
|
|
|
2023-11-02 06:47:28 +01:00
|
|
|
it("should ignore orphaned attachments", () =>
|
2023-11-01 04:08:13 +01:00
|
|
|
{
|
2023-11-02 06:47:28 +01:00
|
|
|
// Remove regular items from the fixture, creating orphaned attachments.
|
|
|
|
insuranceFixture = new ProfileInsuranceFactory().adjustPackageDates().removeRegularItems().get();
|
|
|
|
const insured = insuranceFixture[0];
|
|
|
|
|
|
|
|
// Mock helper methods.
|
|
|
|
const mockPopulateItemsMap = vi.spyOn(insuranceController, "populateItemsMap");
|
|
|
|
const mockProcessRegularItems = vi.spyOn(insuranceController, "processRegularItems");
|
|
|
|
const mockProcessAttachments = vi.spyOn(insuranceController, "processAttachments");
|
|
|
|
|
|
|
|
// Since no parent attachments exist, the map should be empty.
|
|
|
|
const mockPopulateParentAttachmentsMap = vi.fn(() =>
|
2023-11-01 04:08:13 +01:00
|
|
|
{
|
2023-11-02 06:47:28 +01:00
|
|
|
return new Map<string, Item[]>();
|
2023-11-01 04:08:13 +01:00
|
|
|
});
|
2023-11-10 23:21:20 +01:00
|
|
|
vi.spyOn(insuranceController, "populateParentAttachmentsMap").mockImplementation(
|
|
|
|
mockPopulateParentAttachmentsMap,
|
|
|
|
);
|
2023-11-01 04:08:13 +01:00
|
|
|
|
|
|
|
// Execute the method.
|
|
|
|
const result = insuranceController.findItemsToDelete(insured);
|
|
|
|
|
2023-11-02 06:47:28 +01:00
|
|
|
// Verify that the correct methods were called.
|
|
|
|
expect(mockPopulateItemsMap).toHaveBeenCalled();
|
|
|
|
expect(mockPopulateParentAttachmentsMap).toHaveBeenCalled();
|
2023-11-01 04:08:13 +01:00
|
|
|
expect(mockProcessRegularItems).not.toHaveBeenCalled();
|
2023-11-02 06:47:28 +01:00
|
|
|
expect(mockProcessAttachments).not.toHaveBeenCalled();
|
|
|
|
|
|
|
|
// Verify that the result is correct.
|
|
|
|
expect(result.size).toBe(0);
|
|
|
|
expect(result).toEqual(new Set());
|
2023-11-01 04:08:13 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should handle a mix of regular items and attachments", () =>
|
|
|
|
{
|
2023-11-02 06:47:28 +01:00
|
|
|
const insured = insuranceFixture[0];
|
|
|
|
const numberOfItems = insured.items.length;
|
|
|
|
|
|
|
|
// Mock helper methods.
|
|
|
|
const mockPopulateItemsMap = vi.spyOn(insuranceController, "populateItemsMap");
|
|
|
|
const mockPopulateParentAttachmentsMap = vi.spyOn(insuranceController, "populateParentAttachmentsMap");
|
|
|
|
|
|
|
|
// Add all items to the toDelete set. Not realistic, but it's fine for this test.
|
|
|
|
const mockProcessRegularItems = vi.fn((insured, toDelete) =>
|
2023-11-01 04:08:13 +01:00
|
|
|
{
|
2023-11-10 23:21:20 +01:00
|
|
|
insured.items.forEach((item) => toDelete.add(item._id));
|
2023-11-01 04:08:13 +01:00
|
|
|
});
|
|
|
|
vi.spyOn(insuranceController, "processRegularItems").mockImplementation(mockProcessRegularItems);
|
2023-11-02 06:47:28 +01:00
|
|
|
const mockProcessAttachments = vi.fn((parentAttachmentsMap, itemsMap, traderId, toDelete) =>
|
|
|
|
{
|
2023-11-10 23:21:20 +01:00
|
|
|
insured.items.forEach((item) => toDelete.add(item._id));
|
2023-11-02 06:47:28 +01:00
|
|
|
});
|
2023-11-01 04:08:13 +01:00
|
|
|
vi.spyOn(insuranceController, "processAttachments").mockImplementation(mockProcessAttachments);
|
|
|
|
|
|
|
|
// Execute the method.
|
|
|
|
const result = insuranceController.findItemsToDelete(insured);
|
|
|
|
|
2023-11-02 06:47:28 +01:00
|
|
|
// Verify that the correct methods were called.
|
|
|
|
expect(mockPopulateItemsMap).toHaveBeenCalled();
|
|
|
|
expect(mockPopulateParentAttachmentsMap).toHaveBeenCalled();
|
|
|
|
expect(mockProcessRegularItems).toHaveBeenCalled();
|
|
|
|
expect(mockProcessAttachments).toHaveBeenCalled();
|
|
|
|
|
|
|
|
// Verify that the result is correct.
|
|
|
|
expect(result.size).toBe(numberOfItems);
|
2023-11-10 23:21:20 +01:00
|
|
|
expect(result).toEqual(new Set(insured.items.map((item) => item._id)));
|
2023-11-01 04:08:13 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should return an empty set if no items are to be deleted", () =>
|
|
|
|
{
|
2023-11-02 06:47:28 +01:00
|
|
|
const insured = insuranceFixture[0];
|
2023-11-01 04:08:13 +01:00
|
|
|
|
2023-11-02 06:47:28 +01:00
|
|
|
// Mock helper methods.
|
|
|
|
const mockPopulateItemsMap = vi.spyOn(insuranceController, "populateItemsMap");
|
|
|
|
const mockPopulateParentAttachmentsMap = vi.spyOn(insuranceController, "populateParentAttachmentsMap");
|
|
|
|
|
|
|
|
// Don't add any items to the toDelete set.
|
2023-11-10 23:21:20 +01:00
|
|
|
const mockProcessRegularItems = vi.spyOn(insuranceController, "processRegularItems").mockImplementation(
|
|
|
|
vi.fn(),
|
|
|
|
);
|
|
|
|
const mockProcessAttachments = vi.spyOn(insuranceController, "processAttachments").mockImplementation(
|
|
|
|
vi.fn(),
|
|
|
|
);
|
2023-11-01 04:08:13 +01:00
|
|
|
|
|
|
|
// Execute the method.
|
|
|
|
const result = insuranceController.findItemsToDelete(insured);
|
|
|
|
|
2023-11-02 06:47:28 +01:00
|
|
|
// Verify that the correct methods were called.
|
|
|
|
expect(mockPopulateItemsMap).toHaveBeenCalled();
|
|
|
|
expect(mockPopulateParentAttachmentsMap).toHaveBeenCalled();
|
|
|
|
expect(mockProcessRegularItems).toHaveBeenCalled();
|
|
|
|
expect(mockProcessAttachments).toHaveBeenCalled();
|
|
|
|
|
|
|
|
// Verify that the result is correct.
|
2023-11-01 04:08:13 +01:00
|
|
|
expect(result.size).toBe(0);
|
|
|
|
expect(result).toEqual(new Set());
|
|
|
|
});
|
|
|
|
|
2023-11-02 06:47:28 +01:00
|
|
|
it("should log the number of items to be deleted", () =>
|
2023-11-01 04:08:13 +01:00
|
|
|
{
|
2023-11-02 06:47:28 +01:00
|
|
|
const insured = insuranceFixture[0];
|
|
|
|
const numberOfItems = insured.items.length;
|
|
|
|
|
|
|
|
// Mock helper methods.
|
|
|
|
const mockLoggerDebug = vi.spyOn(insuranceController.logger, "debug");
|
|
|
|
|
|
|
|
// Add all items to the toDelete set. Not realistic, but it's fine for this test.
|
2023-11-01 04:08:13 +01:00
|
|
|
const mockProcessRegularItems = vi.fn((insured, toDelete) =>
|
|
|
|
{
|
2023-11-10 23:21:20 +01:00
|
|
|
insured.items.forEach((item) => toDelete.add(item._id));
|
2023-11-01 04:08:13 +01:00
|
|
|
});
|
|
|
|
vi.spyOn(insuranceController, "processRegularItems").mockImplementation(mockProcessRegularItems);
|
2023-11-02 06:47:28 +01:00
|
|
|
const mockProcessAttachments = vi.fn((parentAttachmentsMap, itemsMap, traderId, toDelete) =>
|
|
|
|
{
|
2023-11-10 23:21:20 +01:00
|
|
|
insured.items.forEach((item) => toDelete.add(item._id));
|
2023-11-02 06:47:28 +01:00
|
|
|
});
|
|
|
|
vi.spyOn(insuranceController, "processAttachments").mockImplementation(mockProcessAttachments);
|
2023-11-01 04:08:13 +01:00
|
|
|
|
|
|
|
// Execute the method.
|
|
|
|
const result = insuranceController.findItemsToDelete(insured);
|
|
|
|
|
2023-11-02 06:47:28 +01:00
|
|
|
// Verify that the result is the correct size, and the size is logged.
|
|
|
|
expect(result.size).toBe(numberOfItems);
|
|
|
|
expect(mockLoggerDebug).toBeCalledWith(`Marked ${numberOfItems} items for deletion from insurance.`);
|
2023-11-01 04:08:13 +01:00
|
|
|
});
|
2023-11-02 06:47:28 +01:00
|
|
|
});
|
2023-11-01 04:08:13 +01:00
|
|
|
|
2023-11-02 06:47:28 +01:00
|
|
|
describe("populateParentAttachmentsMap", () =>
|
|
|
|
{
|
|
|
|
it("should correctly map gun to all of its attachments", () =>
|
2023-11-01 04:08:13 +01:00
|
|
|
{
|
2023-11-02 06:47:28 +01:00
|
|
|
const insured = insuranceFixture[0];
|
2023-11-01 04:08:13 +01:00
|
|
|
|
2023-11-02 06:47:28 +01:00
|
|
|
// Generate the items map.
|
|
|
|
const itemsMap = insuranceController.populateItemsMap(insured);
|
2023-11-01 04:08:13 +01:00
|
|
|
|
|
|
|
// Execute the method.
|
2023-11-02 06:47:28 +01:00
|
|
|
const result = insuranceController.populateParentAttachmentsMap(insured, itemsMap);
|
2023-11-01 04:08:13 +01:00
|
|
|
|
2023-11-02 06:47:28 +01:00
|
|
|
// Verify that the map is populated correctly.
|
|
|
|
expect(result.size).toBe(6); // There are 6 base-level items in this insurance package.
|
|
|
|
|
|
|
|
const gun = result.get("911a0f04d5d9c7e239807ae0");
|
|
|
|
expect(gun.length).toBe(7); // This AK has 7 attachments.
|
|
|
|
|
|
|
|
// The attachments should be mapped to the AK properly...
|
|
|
|
const validAttachmentTemplates = [
|
|
|
|
"677c209ebb45445ebb42c405",
|
|
|
|
"4bd10f89836fd9f86aedcac1",
|
|
|
|
"8b1327270791b142ac341b03",
|
|
|
|
"da8cde1b3024c336f6e06152",
|
|
|
|
"bc041c0011d76f714b898400",
|
|
|
|
"9f8d7880a6e0a47a211ec5d3",
|
2023-11-10 23:21:20 +01:00
|
|
|
"db2ef9442178910eba985b51",
|
2023-11-02 06:47:28 +01:00
|
|
|
];
|
2023-11-10 23:21:20 +01:00
|
|
|
validAttachmentTemplates.forEach((value) =>
|
2023-11-02 06:47:28 +01:00
|
|
|
{
|
|
|
|
// Verify that each template is present in the array of attachments.
|
2023-11-10 23:21:20 +01:00
|
|
|
expect(gun.some((item) => item._id === value)).toBe(true);
|
2023-11-02 06:47:28 +01:00
|
|
|
});
|
2023-11-01 04:08:13 +01:00
|
|
|
});
|
|
|
|
|
2023-11-02 06:47:28 +01:00
|
|
|
it("should ignore gun accessories that cannot be modified in-raid", () =>
|
2023-11-01 04:08:13 +01:00
|
|
|
{
|
2023-11-02 06:47:28 +01:00
|
|
|
const insured = insuranceFixture[0];
|
2023-11-01 04:08:13 +01:00
|
|
|
|
2023-11-02 06:47:28 +01:00
|
|
|
// Generate the items map.
|
|
|
|
const itemsMap = insuranceController.populateItemsMap(insured);
|
2023-11-01 04:08:13 +01:00
|
|
|
|
2023-11-02 06:47:28 +01:00
|
|
|
// Execute the method.
|
|
|
|
const result = insuranceController.populateParentAttachmentsMap(insured, itemsMap);
|
|
|
|
|
|
|
|
// Verify that the map is populated correctly.
|
|
|
|
expect(result.size).toBe(6); // There are 6 base-level items in this insurance package.
|
|
|
|
|
|
|
|
const gun = result.get("911a0f04d5d9c7e239807ae0");
|
|
|
|
expect(gun.length).toBe(7); // This AK has 7 valid attachments.
|
|
|
|
|
|
|
|
// These are attachments for the AK, but they are not raid moddable, so they should not be mapped.
|
|
|
|
const invalidAttachmentTemplates = [
|
|
|
|
"1e0b177df108c0c117028812",
|
|
|
|
"c9278dd8251e99578bf7a274",
|
|
|
|
"402b4086535a50ef7d9cef88",
|
2023-11-10 23:21:20 +01:00
|
|
|
"566335b3df586f34b47f5e35",
|
2023-11-02 06:47:28 +01:00
|
|
|
];
|
2023-11-10 23:21:20 +01:00
|
|
|
invalidAttachmentTemplates.forEach((value) =>
|
2023-11-01 04:08:13 +01:00
|
|
|
{
|
2023-11-10 23:21:20 +01:00
|
|
|
expect(gun.every((item) => item._id !== value)).toBe(true);
|
2023-11-01 04:08:13 +01:00
|
|
|
});
|
2023-11-02 06:47:28 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should correctly map helmet to all of its attachments", () =>
|
|
|
|
{
|
|
|
|
const insured = insuranceFixture[0];
|
2023-11-01 04:08:13 +01:00
|
|
|
|
2023-11-02 06:47:28 +01:00
|
|
|
// Generate the items map.
|
|
|
|
const itemsMap = insuranceController.populateItemsMap(insured);
|
|
|
|
|
|
|
|
// Execute the method.
|
|
|
|
const result = insuranceController.populateParentAttachmentsMap(insured, itemsMap);
|
|
|
|
|
|
|
|
// Verify that the map is populated correctly.
|
|
|
|
expect(result.size).toBe(6); // There are 6 base-level items in this insurance package.
|
|
|
|
|
|
|
|
const gun = result.get("3679078e05f5b14466d6a730");
|
|
|
|
expect(gun.length).toBe(5); // This LShZ-2DTM has 5 valid attachments.
|
|
|
|
|
|
|
|
// The attachments should be mapped to the AK properly...
|
|
|
|
const validAttachmentTemplates = [
|
|
|
|
"a2b0c716162c5e31ec28c55a",
|
|
|
|
"dc565f750342cb2d19eeda06",
|
|
|
|
"e9ff62601669d9e2ea9c2fbb",
|
|
|
|
"ac134d7cf6c9d8e25edd0015",
|
2023-11-10 23:21:20 +01:00
|
|
|
"22274b895ecc80d51c3cba1c",
|
2023-11-02 06:47:28 +01:00
|
|
|
];
|
2023-11-10 23:21:20 +01:00
|
|
|
validAttachmentTemplates.forEach((value) =>
|
2023-11-01 04:08:13 +01:00
|
|
|
{
|
2023-11-02 06:47:28 +01:00
|
|
|
// Verify that each template is present in the array of attachments.
|
2023-11-10 23:21:20 +01:00
|
|
|
expect(gun.some((item) => item._id === value)).toBe(true);
|
2023-11-01 04:08:13 +01:00
|
|
|
});
|
2023-11-02 06:47:28 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should correctly map gun to all of its attachments when gun is within a container", () =>
|
|
|
|
{
|
|
|
|
const insured = insuranceFixture[0];
|
|
|
|
|
|
|
|
// Generate the items map.
|
|
|
|
const itemsMap = insuranceController.populateItemsMap(insured);
|
2023-11-01 04:08:13 +01:00
|
|
|
|
|
|
|
// Execute the method.
|
|
|
|
const result = insuranceController.populateParentAttachmentsMap(insured, itemsMap);
|
|
|
|
|
|
|
|
// Verify that the map is populated correctly.
|
2023-11-02 06:47:28 +01:00
|
|
|
expect(result.size).toBe(6); // There are 6 base-level items in this insurance package.
|
|
|
|
|
|
|
|
const gun = result.get("351180f3248d45c71cb2ebdc");
|
2023-11-10 23:21:20 +01:00
|
|
|
expect(insured.items.find((item) => item._id === "351180f3248d45c71cb2ebdc").slotId).toBe("main");
|
2023-11-02 06:47:28 +01:00
|
|
|
expect(gun.length).toBe(14); // This AS VAL has 14 valid attachments.
|
2023-11-01 04:08:13 +01:00
|
|
|
});
|
|
|
|
|
2023-11-02 06:47:28 +01:00
|
|
|
it("should not map items that do not have a main-parent", () =>
|
2023-11-01 04:08:13 +01:00
|
|
|
{
|
2023-11-02 06:47:28 +01:00
|
|
|
insuranceFixture = new ProfileInsuranceFactory().adjustPackageDates().removeRegularItems().get();
|
|
|
|
const insured = insuranceFixture[0];
|
|
|
|
|
|
|
|
// Generate the items map.
|
|
|
|
const itemsMap = insuranceController.populateItemsMap(insured);
|
|
|
|
|
2023-11-01 04:08:13 +01:00
|
|
|
// Execute the method.
|
|
|
|
const result = insuranceController.populateParentAttachmentsMap(insured, itemsMap);
|
|
|
|
|
2023-11-02 06:47:28 +01:00
|
|
|
// Verify that the map is populated correctly.
|
2023-11-01 04:08:13 +01:00
|
|
|
expect(result.size).toBe(0);
|
|
|
|
});
|
|
|
|
|
2023-11-02 06:47:28 +01:00
|
|
|
it("should log a warning when an item does not have a main-parent", () =>
|
2023-11-01 04:08:13 +01:00
|
|
|
{
|
2023-11-02 06:47:28 +01:00
|
|
|
insuranceFixture = new ProfileInsuranceFactory().adjustPackageDates().removeRegularItems().get();
|
|
|
|
const insured = insuranceFixture[0];
|
|
|
|
|
|
|
|
// Generate the items map.
|
|
|
|
const itemsMap = insuranceController.populateItemsMap(insured);
|
2023-11-01 04:08:13 +01:00
|
|
|
|
2023-11-02 06:47:28 +01:00
|
|
|
// Suppress warnings.
|
|
|
|
const mockLoggerWarning = vi.spyOn(insuranceController.logger, "warning").mockImplementation(vi.fn());
|
2023-11-01 04:08:13 +01:00
|
|
|
|
|
|
|
// Execute the method.
|
2023-11-02 06:47:28 +01:00
|
|
|
insuranceController.populateParentAttachmentsMap(insured, itemsMap);
|
2023-11-01 04:08:13 +01:00
|
|
|
|
2023-11-02 06:47:28 +01:00
|
|
|
// Verify that the warning was logged.
|
|
|
|
expect(mockLoggerWarning).toHaveBeenCalled();
|
2023-11-01 04:08:13 +01:00
|
|
|
});
|
2023-11-02 06:47:28 +01:00
|
|
|
});
|
2023-11-01 04:08:13 +01:00
|
|
|
|
2023-11-02 06:47:28 +01:00
|
|
|
describe("processRegularItems", () =>
|
|
|
|
{
|
|
|
|
it("should process regular items and their non-attachment children", () =>
|
2023-11-01 04:08:13 +01:00
|
|
|
{
|
2023-11-02 06:47:28 +01:00
|
|
|
insuranceFixture = new ProfileInsuranceFactory().adjustPackageDates().removeAttachmentItems().get();
|
|
|
|
const insured = insuranceFixture[0];
|
|
|
|
const numberOfItems = insured.items.length;
|
|
|
|
const toDelete = new Set<string>();
|
|
|
|
|
|
|
|
// Mock helper methods.
|
|
|
|
const mockIsAttachmentAttached = vi.spyOn(insuranceController.itemHelper, "isAttachmentAttached");
|
2023-11-10 23:21:20 +01:00
|
|
|
const mockFindAndReturnChildrenAsItems = vi.spyOn(
|
|
|
|
insuranceController.itemHelper,
|
|
|
|
"findAndReturnChildrenAsItems",
|
|
|
|
);
|
2023-11-01 04:08:13 +01:00
|
|
|
|
2023-11-02 06:47:28 +01:00
|
|
|
// Mock rollForDelete to return true for all items. Not realistic, but it's fine for this test.
|
|
|
|
const mockRollForDelete = vi.spyOn(insuranceController, "rollForDelete").mockReturnValue(true);
|
2023-11-01 04:08:13 +01:00
|
|
|
|
|
|
|
// Execute the method.
|
2023-11-02 06:47:28 +01:00
|
|
|
insuranceController.processRegularItems(insured, toDelete);
|
2023-11-01 04:08:13 +01:00
|
|
|
|
2023-11-02 06:47:28 +01:00
|
|
|
// Verify that the correct methods were called.
|
|
|
|
expect(mockIsAttachmentAttached).toHaveBeenCalled();
|
|
|
|
expect(mockFindAndReturnChildrenAsItems).toHaveBeenCalled();
|
|
|
|
expect(mockRollForDelete).toHaveBeenCalledTimes(numberOfItems);
|
|
|
|
|
|
|
|
// Verify that all items were added to the toDelete set.
|
2023-11-10 23:21:20 +01:00
|
|
|
expect(toDelete).toEqual(new Set(insured.items.map((item) => item._id)));
|
2023-11-01 04:08:13 +01:00
|
|
|
});
|
|
|
|
|
2023-11-02 06:47:28 +01:00
|
|
|
it("should not roll attached attachments", () =>
|
2023-11-01 04:08:13 +01:00
|
|
|
{
|
2023-11-02 06:47:28 +01:00
|
|
|
const insured = insuranceFixture[0];
|
|
|
|
const toDelete = new Set<string>();
|
2023-11-01 04:08:13 +01:00
|
|
|
|
2023-11-02 06:47:28 +01:00
|
|
|
// Mock isAttachmentAttached to return true for all items.
|
2023-11-01 04:08:13 +01:00
|
|
|
vi.spyOn(insuranceController.itemHelper, "isAttachmentAttached").mockReturnValue(true);
|
|
|
|
|
2023-11-02 06:47:28 +01:00
|
|
|
// Mock rollForDelete to return true for all items.
|
|
|
|
const mockRollForDelete = vi.spyOn(insuranceController, "rollForDelete").mockReturnValue(true);
|
2023-11-01 04:08:13 +01:00
|
|
|
|
|
|
|
// Execute the method.
|
2023-11-02 06:47:28 +01:00
|
|
|
insuranceController.processRegularItems(insured, toDelete);
|
|
|
|
|
|
|
|
// Verify that a roll was not made for any items.
|
|
|
|
expect(mockRollForDelete).not.toHaveBeenCalled();
|
2023-11-01 04:08:13 +01:00
|
|
|
|
2023-11-02 06:47:28 +01:00
|
|
|
// Verify that no items were added to the toDelete set.
|
|
|
|
expect(toDelete).toEqual(new Set());
|
2023-11-01 04:08:13 +01:00
|
|
|
});
|
|
|
|
|
2023-11-02 06:47:28 +01:00
|
|
|
it("should mark attachments for deletion when parent is marked for deletion", () =>
|
2023-11-01 04:08:13 +01:00
|
|
|
{
|
2023-11-02 06:47:28 +01:00
|
|
|
const itemHelper = container.resolve<ItemHelper>("ItemHelper");
|
|
|
|
|
|
|
|
const insured = insuranceFixture[0];
|
2023-11-01 04:08:13 +01:00
|
|
|
const toDelete = new Set<string>();
|
|
|
|
|
2023-11-02 06:47:28 +01:00
|
|
|
// Mock rollForDelete to return true for all base-parent items.
|
|
|
|
const mockRollForDelete = vi.fn((traderId, insuredItem) =>
|
2023-11-01 04:08:13 +01:00
|
|
|
{
|
2023-11-02 06:47:28 +01:00
|
|
|
return !itemHelper.isAttachmentAttached(insuredItem);
|
2023-11-01 04:08:13 +01:00
|
|
|
});
|
2023-11-02 06:47:28 +01:00
|
|
|
vi.spyOn(insuranceController, "rollForDelete").mockImplementation(mockRollForDelete);
|
2023-11-01 04:08:13 +01:00
|
|
|
|
|
|
|
// Execute the method.
|
|
|
|
insuranceController.processRegularItems(insured, toDelete);
|
|
|
|
|
2023-11-02 06:47:28 +01:00
|
|
|
// Verify that all items were added to the toDelete set.
|
2023-11-10 23:21:20 +01:00
|
|
|
expect(toDelete).toEqual(new Set(insured.items.map((item) => item._id)));
|
2023-11-01 04:08:13 +01:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("processAttachments", () =>
|
|
|
|
{
|
2023-11-02 06:47:28 +01:00
|
|
|
it("should iterate over each parent item", () =>
|
2023-11-01 04:08:13 +01:00
|
|
|
{
|
2023-11-02 06:47:28 +01:00
|
|
|
const insured = insuranceFixture[0];
|
|
|
|
const itemsMap = insuranceController.populateItemsMap(insured);
|
|
|
|
const parentToAttachmentMap = insuranceController.populateParentAttachmentsMap(insured, itemsMap);
|
2023-11-01 04:08:13 +01:00
|
|
|
const toDelete = new Set<string>();
|
|
|
|
|
|
|
|
// Mock helper methods.
|
2023-11-02 06:47:28 +01:00
|
|
|
const mockProcessAttachmentByParent = vi.spyOn(insuranceController, "processAttachmentByParent");
|
2023-11-01 04:08:13 +01:00
|
|
|
|
2023-11-02 06:47:28 +01:00
|
|
|
// Execute the method.
|
|
|
|
insuranceController.processAttachments(parentToAttachmentMap, itemsMap, insured.traderId, toDelete);
|
2023-11-01 04:08:13 +01:00
|
|
|
|
2023-11-02 06:47:28 +01:00
|
|
|
// Verify
|
|
|
|
expect(mockProcessAttachmentByParent).toHaveBeenCalledTimes(parentToAttachmentMap.size);
|
|
|
|
});
|
2023-11-01 04:08:13 +01:00
|
|
|
|
2023-11-02 06:47:28 +01:00
|
|
|
it("should log the name of each parent item", () =>
|
|
|
|
{
|
|
|
|
const itemHelper = container.resolve<ItemHelper>("ItemHelper");
|
2023-11-01 04:08:13 +01:00
|
|
|
|
2023-11-02 06:47:28 +01:00
|
|
|
const insured = insuranceFixture[0];
|
|
|
|
const itemsMap = insuranceController.populateItemsMap(insured);
|
|
|
|
const parentToAttachmentMap = insuranceController.populateParentAttachmentsMap(insured, itemsMap);
|
|
|
|
const toDelete = new Set<string>();
|
2023-11-01 04:08:13 +01:00
|
|
|
|
2023-11-02 06:47:28 +01:00
|
|
|
// Mock helper methods.
|
|
|
|
const mockLoggerDebug = vi.spyOn(insuranceController.logger, "debug");
|
|
|
|
|
|
|
|
// Execute the method.
|
|
|
|
insuranceController.processAttachments(parentToAttachmentMap, itemsMap, insured.traderId, toDelete);
|
|
|
|
|
|
|
|
// Verify that the name of each parent item is logged.
|
|
|
|
for (const [parentId] of parentToAttachmentMap)
|
|
|
|
{
|
|
|
|
const parentItem = itemsMap.get(parentId);
|
|
|
|
if (parentItem)
|
|
|
|
{
|
2023-11-10 23:21:20 +01:00
|
|
|
const expectedMessage = `Processing attachments for parent item: ${
|
|
|
|
itemHelper.getItemName(parentItem._tpl)
|
|
|
|
}`;
|
2023-11-02 06:47:28 +01:00
|
|
|
expect(mockLoggerDebug).toHaveBeenCalledWith(expectedMessage);
|
|
|
|
}
|
|
|
|
}
|
2023-11-01 04:08:13 +01:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("processAttachmentByParent", () =>
|
|
|
|
{
|
2023-11-02 06:47:28 +01:00
|
|
|
it("should handle sorting, rolling, and deleting attachments by calling helper methods", () =>
|
2023-11-01 04:08:13 +01:00
|
|
|
{
|
2023-11-02 06:47:28 +01:00
|
|
|
const insured = insuranceFixture[0];
|
|
|
|
const itemsMap = insuranceController.populateItemsMap(insured);
|
|
|
|
const parentToAttachmentMap = insuranceController.populateParentAttachmentsMap(insured, itemsMap);
|
|
|
|
const attachments = parentToAttachmentMap.entries().next().value;
|
|
|
|
const toDelete = new Set<string>();
|
|
|
|
|
|
|
|
// Mock helper methods.
|
|
|
|
const mockSortAttachmentsByPrice = vi.spyOn(insuranceController, "sortAttachmentsByPrice");
|
|
|
|
const mockCountSuccessfulRolls = vi.spyOn(insuranceController, "countSuccessfulRolls").mockReturnValue(4);
|
|
|
|
const mockAttachmentDeletionByValue = vi.spyOn(insuranceController, "attachmentDeletionByValue");
|
|
|
|
|
|
|
|
// Execute the method.
|
|
|
|
insuranceController.processAttachmentByParent(attachments, insured.traderId, toDelete);
|
|
|
|
|
|
|
|
// Verify that helper methods are called.
|
|
|
|
expect(mockSortAttachmentsByPrice).toHaveBeenCalledWith(attachments);
|
|
|
|
expect(mockCountSuccessfulRolls).toHaveBeenCalled();
|
|
|
|
expect(mockAttachmentDeletionByValue).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should log attachment details and number of successful rolls", () =>
|
|
|
|
{
|
|
|
|
const insured = insuranceFixture[0];
|
|
|
|
const itemsMap = insuranceController.populateItemsMap(insured);
|
|
|
|
const parentToAttachmentMap = insuranceController.populateParentAttachmentsMap(insured, itemsMap);
|
|
|
|
const attachments = parentToAttachmentMap.values().next().value;
|
2023-11-01 04:08:13 +01:00
|
|
|
const toDelete = new Set<string>();
|
2023-11-02 06:47:28 +01:00
|
|
|
const successfulRolls = 4;
|
2023-11-01 04:08:13 +01:00
|
|
|
|
|
|
|
// Mock helper methods.
|
2023-11-02 06:47:28 +01:00
|
|
|
const mockLogAttachmentsDetails = vi.spyOn(insuranceController, "logAttachmentsDetails");
|
|
|
|
vi.spyOn(insuranceController, "countSuccessfulRolls").mockReturnValue(successfulRolls);
|
|
|
|
const mockLoggerDebug = vi.spyOn(insuranceController.logger, "debug").mockImplementation(vi.fn());
|
2023-11-01 04:08:13 +01:00
|
|
|
|
|
|
|
// Execute the method.
|
2023-11-02 06:47:28 +01:00
|
|
|
insuranceController.processAttachmentByParent(attachments, insured.traderId, toDelete);
|
2023-11-01 04:08:13 +01:00
|
|
|
|
2023-11-02 06:47:28 +01:00
|
|
|
// Verify that the logs were called/written.
|
|
|
|
expect(mockLogAttachmentsDetails).toBeCalled();
|
|
|
|
expect(mockLoggerDebug).toHaveBeenCalledWith(`Number of successful rolls: ${successfulRolls}`);
|
2023-11-01 04:08:13 +01:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("sortAttachmentsByPrice", () =>
|
|
|
|
{
|
|
|
|
it("should sort the attachments array by maxPrice in descending order", () =>
|
|
|
|
{
|
2023-11-02 06:47:28 +01:00
|
|
|
const insured = insuranceFixture[0];
|
|
|
|
const itemsMap = insuranceController.populateItemsMap(insured);
|
|
|
|
const parentToAttachmentMap = insuranceController.populateParentAttachmentsMap(insured, itemsMap);
|
|
|
|
const attachments = parentToAttachmentMap.values().next().value;
|
2023-11-01 04:08:13 +01:00
|
|
|
|
|
|
|
// Execute the method.
|
2023-11-02 06:47:28 +01:00
|
|
|
const sortedAttachments = insuranceController.sortAttachmentsByPrice(attachments);
|
2023-11-01 04:08:13 +01:00
|
|
|
|
2023-11-02 06:47:28 +01:00
|
|
|
// Verify the length of the sorted attachments array
|
|
|
|
expect(sortedAttachments.length).toBe(5);
|
|
|
|
|
|
|
|
// Verify that the attachments are sorted by maxPrice in descending order
|
|
|
|
for (let i = 1; i < sortedAttachments.length; i++)
|
|
|
|
{
|
|
|
|
expect(sortedAttachments[i - 1].maxPrice).toBeGreaterThanOrEqual(sortedAttachments[i].maxPrice);
|
|
|
|
}
|
2023-11-01 04:08:13 +01:00
|
|
|
});
|
|
|
|
|
2023-11-02 06:47:28 +01:00
|
|
|
it("should place attachments with null maxPrice at the bottom of the sorted list", () =>
|
2023-11-01 04:08:13 +01:00
|
|
|
{
|
2023-11-02 06:47:28 +01:00
|
|
|
const insured = insuranceFixture[0];
|
|
|
|
const itemsMap = insuranceController.populateItemsMap(insured);
|
|
|
|
const parentToAttachmentMap = insuranceController.populateParentAttachmentsMap(insured, itemsMap);
|
|
|
|
const attachments = parentToAttachmentMap.values().next().value;
|
2023-11-01 04:08:13 +01:00
|
|
|
|
2023-11-02 06:47:28 +01:00
|
|
|
// Set the maxPrice of the first two attachments to null.
|
2023-11-10 23:21:20 +01:00
|
|
|
vi.spyOn(insuranceController.itemHelper, "getItemMaxPrice").mockReturnValueOnce(null).mockReturnValueOnce(
|
|
|
|
null,
|
|
|
|
);
|
2023-11-01 04:08:13 +01:00
|
|
|
|
|
|
|
// Execute the method.
|
2023-11-02 06:47:28 +01:00
|
|
|
const sortedAttachments = insuranceController.sortAttachmentsByPrice(attachments);
|
2023-11-01 04:08:13 +01:00
|
|
|
|
2023-11-02 06:47:28 +01:00
|
|
|
// Verify that the attachments with null maxPrice are at the bottom of the list
|
|
|
|
const nullPriceAttachments = sortedAttachments.slice(-2);
|
2023-11-10 23:21:20 +01:00
|
|
|
nullPriceAttachments.forEach((attachment) =>
|
2023-11-02 06:47:28 +01:00
|
|
|
{
|
|
|
|
expect(attachment.maxPrice).toBeNull();
|
|
|
|
});
|
|
|
|
|
|
|
|
// Verify that the rest of the attachments are sorted by maxPrice in descending order
|
|
|
|
for (let i = 1; i < sortedAttachments.length - 2; i++)
|
|
|
|
{
|
|
|
|
expect(sortedAttachments[i - 1].maxPrice).toBeGreaterThanOrEqual(sortedAttachments[i].maxPrice);
|
|
|
|
}
|
2023-11-01 04:08:13 +01:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("logAttachmentsDetails", () =>
|
|
|
|
{
|
|
|
|
it("should log details for each attachment", () =>
|
|
|
|
{
|
2023-11-13 18:38:16 +01:00
|
|
|
const attachments = [{ _id: "item1", name: "Item 1", maxPrice: 100 }, {
|
2023-11-13 18:31:52 +01:00
|
|
|
_id: "item2",
|
|
|
|
name: "Item 2",
|
|
|
|
maxPrice: 200,
|
|
|
|
}];
|
2023-11-01 04:08:13 +01:00
|
|
|
|
|
|
|
// Mock the logger.debug function.
|
2023-11-03 22:39:33 +01:00
|
|
|
const loggerDebugSpy = vi.spyOn(insuranceController.logger, "debug");
|
2023-11-01 04:08:13 +01:00
|
|
|
|
|
|
|
// Execute the method.
|
|
|
|
insuranceController.logAttachmentsDetails(attachments);
|
|
|
|
|
|
|
|
// Verify that logger.debug was called correctly.
|
|
|
|
expect(loggerDebugSpy).toHaveBeenCalledTimes(2);
|
|
|
|
expect(loggerDebugSpy).toHaveBeenNthCalledWith(1, "Child Item - Name: Item 1, Max Price: 100");
|
|
|
|
expect(loggerDebugSpy).toHaveBeenNthCalledWith(2, "Child Item - Name: Item 2, Max Price: 200");
|
|
|
|
});
|
2023-11-03 22:39:33 +01:00
|
|
|
|
|
|
|
it("should not log anything when there are no attachments", () =>
|
|
|
|
{
|
|
|
|
const attachments = [];
|
|
|
|
|
|
|
|
// Mock the logger.debug function.
|
|
|
|
const loggerDebugSpy = vi.spyOn(insuranceController.logger, "debug");
|
|
|
|
|
|
|
|
// Execute the method.
|
|
|
|
insuranceController.logAttachmentsDetails(attachments);
|
|
|
|
|
|
|
|
// Verify that logger.debug was called correctly.
|
|
|
|
expect(loggerDebugSpy).not.toHaveBeenCalled();
|
|
|
|
});
|
2023-11-01 04:08:13 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
describe("countSuccessfulRolls", () =>
|
|
|
|
{
|
|
|
|
it("should count the number of successful rolls based on the rollForDelete method", () =>
|
|
|
|
{
|
2023-11-03 22:39:33 +01:00
|
|
|
const insured = insuranceFixture[0];
|
|
|
|
const itemsMap = insuranceController.populateItemsMap(insured);
|
|
|
|
const parentToAttachmentMap = insuranceController.populateParentAttachmentsMap(insured, itemsMap);
|
|
|
|
const attachments = parentToAttachmentMap.values().next().value;
|
2023-11-01 04:08:13 +01:00
|
|
|
|
2023-11-03 22:39:33 +01:00
|
|
|
// Mock rollForDelete to return true for the first two attachments.
|
|
|
|
const mockRollForDelete = vi.spyOn(insuranceController, "rollForDelete").mockReturnValue(false)
|
2023-11-13 18:31:52 +01:00
|
|
|
.mockReturnValueOnce(true).mockReturnValueOnce(true);
|
2023-11-01 04:08:13 +01:00
|
|
|
|
|
|
|
// Execute the method.
|
2023-11-03 22:39:33 +01:00
|
|
|
const result = insuranceController.countSuccessfulRolls(attachments, insured.traderId);
|
2023-11-01 04:08:13 +01:00
|
|
|
|
2023-11-03 22:39:33 +01:00
|
|
|
// Verify that two successful rolls were counted.
|
|
|
|
expect(mockRollForDelete).toHaveBeenCalledTimes(attachments.length);
|
2023-11-01 04:08:13 +01:00
|
|
|
expect(result).toBe(2);
|
|
|
|
});
|
|
|
|
|
2023-11-03 22:39:33 +01:00
|
|
|
it("should count the number of successful rolls based on the rollForDelete method", () =>
|
2023-11-01 04:08:13 +01:00
|
|
|
{
|
2023-11-03 22:39:33 +01:00
|
|
|
const insured = insuranceFixture[0];
|
|
|
|
const itemsMap = insuranceController.populateItemsMap(insured);
|
|
|
|
const parentToAttachmentMap = insuranceController.populateParentAttachmentsMap(insured, itemsMap);
|
|
|
|
const attachments = parentToAttachmentMap.values().next().value;
|
2023-11-01 04:08:13 +01:00
|
|
|
|
2023-11-03 22:39:33 +01:00
|
|
|
// Mock rollForDelete to return false.
|
|
|
|
const mockRollForDelete = vi.spyOn(insuranceController, "rollForDelete").mockReturnValue(false);
|
2023-11-01 04:08:13 +01:00
|
|
|
|
|
|
|
// Execute the method.
|
2023-11-03 22:39:33 +01:00
|
|
|
const result = insuranceController.countSuccessfulRolls(attachments, insured.traderId);
|
2023-11-01 04:08:13 +01:00
|
|
|
|
2023-11-03 22:39:33 +01:00
|
|
|
// Verify that zero successful rolls were counted.
|
|
|
|
expect(mockRollForDelete).toHaveBeenCalledTimes(attachments.length);
|
2023-11-01 04:08:13 +01:00
|
|
|
expect(result).toBe(0);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should return zero if there are no attachments", () =>
|
|
|
|
{
|
2023-11-03 22:39:33 +01:00
|
|
|
const insured = insuranceFixture[0];
|
2023-11-01 04:08:13 +01:00
|
|
|
const attachments = [];
|
2023-11-03 22:39:33 +01:00
|
|
|
|
|
|
|
// Spy on rollForDelete to ensure it is not called.
|
|
|
|
const mockRollForDelete = vi.spyOn(insuranceController, "rollForDelete");
|
2023-11-01 04:08:13 +01:00
|
|
|
|
|
|
|
// Execute the method.
|
2023-11-03 22:39:33 +01:00
|
|
|
const result = insuranceController.countSuccessfulRolls(attachments, insured.traderId);
|
2023-11-01 04:08:13 +01:00
|
|
|
|
|
|
|
// Verify that zero successful rolls were returned.
|
2023-11-03 22:39:33 +01:00
|
|
|
expect(mockRollForDelete).not.toHaveBeenCalled();
|
2023-11-01 04:08:13 +01:00
|
|
|
expect(result).toBe(0);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("attachmentDeletionByValue", () =>
|
|
|
|
{
|
2023-11-03 22:39:33 +01:00
|
|
|
it("should add the correct number of attachments to the toDelete set", () =>
|
2023-11-01 04:08:13 +01:00
|
|
|
{
|
2023-11-03 22:39:33 +01:00
|
|
|
const insured = insuranceFixture[0];
|
|
|
|
const itemsMap = insuranceController.populateItemsMap(insured);
|
|
|
|
const parentToAttachmentMap = insuranceController.populateParentAttachmentsMap(insured, itemsMap);
|
|
|
|
const attachments = parentToAttachmentMap.values().next().value;
|
|
|
|
|
2023-11-01 04:08:13 +01:00
|
|
|
const successfulRolls = 2;
|
|
|
|
const toDelete = new Set<string>();
|
|
|
|
|
|
|
|
// Execute the method.
|
|
|
|
insuranceController.attachmentDeletionByValue(attachments, successfulRolls, toDelete);
|
|
|
|
|
|
|
|
// Should add the first two valuable attachments to the toDelete set.
|
2023-11-03 22:39:33 +01:00
|
|
|
expect(toDelete.size).toEqual(successfulRolls);
|
2023-11-01 04:08:13 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should not add any attachments to toDelete if successfulRolls is zero", () =>
|
|
|
|
{
|
2023-11-03 22:39:33 +01:00
|
|
|
const insured = insuranceFixture[0];
|
|
|
|
const itemsMap = insuranceController.populateItemsMap(insured);
|
|
|
|
const parentToAttachmentMap = insuranceController.populateParentAttachmentsMap(insured, itemsMap);
|
|
|
|
const attachments = parentToAttachmentMap.values().next().value;
|
|
|
|
|
2023-11-01 04:08:13 +01:00
|
|
|
const successfulRolls = 0;
|
|
|
|
const toDelete = new Set<string>();
|
|
|
|
|
|
|
|
// Execute the method.
|
|
|
|
insuranceController.attachmentDeletionByValue(attachments, successfulRolls, toDelete);
|
|
|
|
|
2023-11-03 22:39:33 +01:00
|
|
|
// Should be empty.
|
|
|
|
expect(toDelete.size).toEqual(successfulRolls);
|
2023-11-01 04:08:13 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should add all attachments to toDelete if successfulRolls is greater than the number of attachments", () =>
|
|
|
|
{
|
2023-11-03 22:39:33 +01:00
|
|
|
const insured = insuranceFixture[0];
|
|
|
|
const itemsMap = insuranceController.populateItemsMap(insured);
|
|
|
|
const parentToAttachmentMap = insuranceController.populateParentAttachmentsMap(insured, itemsMap);
|
|
|
|
const attachments = parentToAttachmentMap.values().next().value;
|
|
|
|
|
|
|
|
const successfulRolls = 999;
|
2023-11-01 04:08:13 +01:00
|
|
|
const toDelete = new Set<string>();
|
|
|
|
|
|
|
|
// Execute the method.
|
|
|
|
insuranceController.attachmentDeletionByValue(attachments, successfulRolls, toDelete);
|
|
|
|
|
2023-11-03 22:39:33 +01:00
|
|
|
// Should be empty.
|
|
|
|
expect(toDelete.size).toBeLessThan(successfulRolls);
|
|
|
|
expect(toDelete.size).toEqual(attachments.length);
|
2023-11-01 04:08:13 +01:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("removeItemsFromInsurance", () =>
|
|
|
|
{
|
|
|
|
it("should remove items from insurance based on the toDelete set", () =>
|
|
|
|
{
|
2023-11-03 22:39:33 +01:00
|
|
|
const insured = insuranceFixture[0];
|
|
|
|
|
|
|
|
const toDelete = new Set<string>();
|
|
|
|
const numberToDelete = 2;
|
|
|
|
for (let i = 0; i < numberToDelete; i++)
|
|
|
|
{
|
|
|
|
toDelete.add(insured.items[i]._id);
|
|
|
|
}
|
2023-11-01 04:08:13 +01:00
|
|
|
|
|
|
|
// Execute the method.
|
|
|
|
insuranceController.removeItemsFromInsurance(insured, toDelete);
|
|
|
|
|
2023-11-03 22:39:33 +01:00
|
|
|
// Ensure that the items in the toDelete set are not present in the insured items array.
|
2023-11-10 23:21:20 +01:00
|
|
|
toDelete.forEach((toDeleteId) =>
|
2023-11-03 22:39:33 +01:00
|
|
|
{
|
2023-11-10 23:21:20 +01:00
|
|
|
expect(insured.items.some((item) => item._id === toDeleteId)).toBe(false);
|
2023-11-03 22:39:33 +01:00
|
|
|
});
|
2023-11-01 04:08:13 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should not remove any items if toDelete set is empty", () =>
|
|
|
|
{
|
2023-11-03 22:39:33 +01:00
|
|
|
const insured = insuranceFixture[0];
|
|
|
|
const originalCount = insured.items.length;
|
2023-11-01 04:08:13 +01:00
|
|
|
const toDelete = new Set<string>();
|
|
|
|
|
|
|
|
// Execute the method.
|
|
|
|
insuranceController.removeItemsFromInsurance(insured, toDelete);
|
|
|
|
|
2023-11-03 22:39:33 +01:00
|
|
|
// Ensure that no items were removed.
|
|
|
|
expect(insured.items.length).toBe(originalCount);
|
2023-11-01 04:08:13 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should leave the insurance items empty if all are to be deleted", () =>
|
|
|
|
{
|
2023-11-03 22:39:33 +01:00
|
|
|
const insured = insuranceFixture[0];
|
|
|
|
const originalCount = insured.items.length;
|
|
|
|
const toDelete = new Set<string>();
|
2023-11-10 23:21:20 +01:00
|
|
|
insured.items.forEach((item) => toDelete.add(item._id));
|
2023-11-03 22:39:33 +01:00
|
|
|
|
|
|
|
// All of the items should be added to the toDelete set.
|
|
|
|
expect(originalCount).toBe(toDelete.size);
|
2023-11-01 04:08:13 +01:00
|
|
|
|
|
|
|
// Execute the method.
|
|
|
|
insuranceController.removeItemsFromInsurance(insured, toDelete);
|
|
|
|
|
2023-11-03 22:39:33 +01:00
|
|
|
// Ensure that all items were removed.
|
|
|
|
expect(insured.items.length).toBe(0);
|
2023-11-01 04:08:13 +01:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("adoptOrphanedItems", () =>
|
|
|
|
{
|
|
|
|
it("should adopt orphaned items by resetting them as base-level items", () =>
|
|
|
|
{
|
2023-11-03 22:39:33 +01:00
|
|
|
// Get all of the items, so that we can dynamically find the hideout item.
|
|
|
|
const insured = insuranceFixture[0];
|
|
|
|
const hideoutParentId = insuranceController.fetchHideoutItemParent(insured.items);
|
|
|
|
|
|
|
|
// Manually set one of the items to be orphaned.
|
|
|
|
insured.items[0].parentId = "9999"; // Should not exist in the items array.
|
|
|
|
insured.items[0].slotId = "main"; // Should not be "hideout".
|
2023-11-01 04:08:13 +01:00
|
|
|
|
2023-11-03 22:39:33 +01:00
|
|
|
// Iterate over the items and find an individual orphaned item.
|
2023-11-10 23:21:20 +01:00
|
|
|
const orphanedItem = insured.items.find((item) =>
|
|
|
|
!insured.items.some((parent) => parent._id === item.parentId)
|
|
|
|
);
|
2023-11-03 22:39:33 +01:00
|
|
|
|
|
|
|
// Setup tests to verify that the orphaned item we added is in fact orphaned.
|
|
|
|
expect(orphanedItem.parentId).toBe(insured.items[0].parentId);
|
|
|
|
expect(orphanedItem.slotId).toBe(insured.items[0].slotId);
|
2023-11-01 04:08:13 +01:00
|
|
|
|
|
|
|
// Execute the method.
|
|
|
|
insuranceController.adoptOrphanedItems(insured);
|
|
|
|
|
2023-11-03 22:39:33 +01:00
|
|
|
// Verify that the orphaned items have been adopted.
|
|
|
|
expect(orphanedItem.parentId).toBe(hideoutParentId);
|
|
|
|
expect(orphanedItem.slotId).toBe("hideout");
|
2023-11-01 04:08:13 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should not adopt items that are not orphaned", () =>
|
|
|
|
{
|
2023-11-03 22:39:33 +01:00
|
|
|
const unmodified = insuranceFixture[0];
|
2023-11-01 04:08:13 +01:00
|
|
|
|
2023-11-03 22:39:33 +01:00
|
|
|
// Create a deep copy of the insured items array.
|
|
|
|
const insured = JSON.parse(JSON.stringify(insuranceFixture[0]));
|
2023-11-01 04:08:13 +01:00
|
|
|
|
|
|
|
// Execute the method.
|
|
|
|
insuranceController.adoptOrphanedItems(insured);
|
|
|
|
|
2023-11-03 22:39:33 +01:00
|
|
|
// Verify that the orphaned items have been adopted.
|
|
|
|
expect(insured).toStrictEqual(unmodified);
|
2023-11-01 04:08:13 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should remove location data from adopted items", () =>
|
|
|
|
{
|
2023-11-03 22:39:33 +01:00
|
|
|
const insured = insuranceFixture[0];
|
2023-11-01 04:08:13 +01:00
|
|
|
|
2023-11-03 22:39:33 +01:00
|
|
|
// Manually set one of the items to be orphaned.
|
|
|
|
insured.items[0].parentId = "9999"; // Should not exist in the items array.
|
|
|
|
insured.items[0].slotId = "main"; // Should not be "hideout".
|
2023-11-13 18:38:16 +01:00
|
|
|
insured.items[0].location = { x: 1, y: 2, r: 3, isSearched: true }; // Should be removed.
|
2023-11-03 22:39:33 +01:00
|
|
|
|
|
|
|
// Iterate over the items and find an individual orphaned item.
|
2023-11-10 23:21:20 +01:00
|
|
|
const orphanedItem = insured.items.find((item) =>
|
|
|
|
!insured.items.some((parent) => parent._id === item.parentId)
|
|
|
|
);
|
2023-11-03 22:39:33 +01:00
|
|
|
|
|
|
|
// Setup tests to verify that the orphaned item we added is in fact orphaned.
|
|
|
|
expect(orphanedItem.parentId).toBe(insured.items[0].parentId);
|
|
|
|
expect(orphanedItem.slotId).toBe(insured.items[0].slotId);
|
2023-11-01 04:08:13 +01:00
|
|
|
|
|
|
|
// Execute the method.
|
|
|
|
insuranceController.adoptOrphanedItems(insured);
|
|
|
|
|
2023-11-03 22:39:33 +01:00
|
|
|
// Verify that the orphaned items have been adopted.
|
|
|
|
expect(orphanedItem).not.toHaveProperty("location");
|
2023-11-01 04:08:13 +01:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("fetchHideoutItemParent", () =>
|
|
|
|
{
|
2023-11-03 22:39:33 +01:00
|
|
|
it("should return the parentId value of an item that has a slotId of 'hideout'", () =>
|
2023-11-01 04:08:13 +01:00
|
|
|
{
|
2023-11-03 22:39:33 +01:00
|
|
|
const insured = insuranceFixture[0];
|
|
|
|
const hideoutParentId = insuranceController.fetchHideoutItemParent(insured.items);
|
2023-11-01 04:08:13 +01:00
|
|
|
|
|
|
|
// Execute the method.
|
2023-11-03 22:39:33 +01:00
|
|
|
const result = insuranceController.fetchHideoutItemParent(insured.items);
|
2023-11-01 04:08:13 +01:00
|
|
|
|
|
|
|
// Verify that the hideout item parentId is returned.
|
2023-11-03 22:39:33 +01:00
|
|
|
expect(result).toBe(hideoutParentId);
|
2023-11-01 04:08:13 +01:00
|
|
|
});
|
|
|
|
|
2023-11-03 22:39:33 +01:00
|
|
|
it("should return an empty string if no item with a slotId of 'hideout' could be found", () =>
|
2023-11-01 04:08:13 +01:00
|
|
|
{
|
2023-11-03 22:39:33 +01:00
|
|
|
// Fetch a bunch of orphaned items that don't have a hideout parent.
|
|
|
|
const insuranceFixture = new ProfileInsuranceFactory().adjustPackageDates().removeRegularItems().get();
|
|
|
|
const insured = insuranceFixture[0];
|
2023-11-01 04:08:13 +01:00
|
|
|
|
|
|
|
// Execute the method.
|
2023-11-03 22:39:33 +01:00
|
|
|
const result = insuranceController.fetchHideoutItemParent(insured.items);
|
2023-11-01 04:08:13 +01:00
|
|
|
|
2023-11-03 22:39:33 +01:00
|
|
|
// Verify that the hideout item parentId is returned.
|
2023-11-01 04:08:13 +01:00
|
|
|
expect(result).toBe("");
|
|
|
|
});
|
2023-11-03 22:39:33 +01:00
|
|
|
|
|
|
|
it("should log a warning if the base-level item does not exist", () =>
|
|
|
|
{
|
|
|
|
// Fetch a bunch of orphaned items that don't have a hideout parent.
|
|
|
|
const insuranceFixture = new ProfileInsuranceFactory().adjustPackageDates().removeRegularItems().get();
|
|
|
|
const insured = insuranceFixture[0];
|
|
|
|
|
|
|
|
// Spy on the logger.
|
|
|
|
const loggerWarningSpy = vi.spyOn(insuranceController.logger, "warning");
|
|
|
|
|
|
|
|
// Execute the method.
|
|
|
|
insuranceController.fetchHideoutItemParent(insured.items);
|
|
|
|
|
|
|
|
// Verify that the hideout item parentId is returned.
|
|
|
|
expect(loggerWarningSpy).toHaveBeenCalled();
|
|
|
|
});
|
2023-11-01 04:08:13 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
describe("sendMail", () =>
|
|
|
|
{
|
|
|
|
it("should send insurance failed message when no items are present", () =>
|
|
|
|
{
|
|
|
|
const traderHelper = container.resolve<TraderHelper>("TraderHelper");
|
|
|
|
|
2023-11-03 22:39:33 +01:00
|
|
|
const insurance = insuranceFixture[0];
|
|
|
|
insurance.items = []; // Empty the items array
|
|
|
|
const sessionID = "session-id";
|
2023-11-01 04:08:13 +01:00
|
|
|
const insuranceFailedTpl = "failed-message-template";
|
|
|
|
|
|
|
|
// Mock the randomUtil to return a static failed template string.
|
2023-11-13 18:31:52 +01:00
|
|
|
const mockGetArrayValue = vi.spyOn(insuranceController.randomUtil, "getArrayValue").mockReturnValue(
|
|
|
|
insuranceFailedTpl,
|
|
|
|
);
|
2023-11-01 04:08:13 +01:00
|
|
|
|
|
|
|
// Don't actually send the message.
|
2023-11-03 22:39:33 +01:00
|
|
|
const sendMessageSpy = vi.spyOn(insuranceController.mailSendService, "sendLocalisedNpcMessageToPlayer")
|
|
|
|
.mockImplementation(vi.fn());
|
2023-11-01 04:08:13 +01:00
|
|
|
|
|
|
|
// Execute the method.
|
|
|
|
insuranceController.sendMail(sessionID, insurance);
|
|
|
|
|
2023-11-03 22:39:33 +01:00
|
|
|
// Verify that the randomUtil.getArrayValue method was called.
|
|
|
|
expect(mockGetArrayValue).toBeCalled();
|
|
|
|
|
2023-11-01 04:08:13 +01:00
|
|
|
// Verify that the insurance failed message was sent.
|
2023-11-03 22:39:33 +01:00
|
|
|
expect(sendMessageSpy).toHaveBeenCalledWith(
|
2023-11-01 04:08:13 +01:00
|
|
|
sessionID,
|
|
|
|
traderHelper.getTraderById(insurance.traderId),
|
|
|
|
MessageType.INSURANCE_RETURN,
|
|
|
|
insuranceFailedTpl,
|
|
|
|
insurance.items,
|
|
|
|
insurance.messageContent.maxStorageTime,
|
2023-11-10 23:21:20 +01:00
|
|
|
insurance.messageContent.systemData,
|
2023-11-01 04:08:13 +01:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should not send insurance failed message when items are present", () =>
|
|
|
|
{
|
|
|
|
const traderHelper = container.resolve<TraderHelper>("TraderHelper");
|
|
|
|
|
2023-11-03 22:39:33 +01:00
|
|
|
const insurance = insuranceFixture[0];
|
|
|
|
const sessionID = "session-id";
|
2023-11-01 04:08:13 +01:00
|
|
|
const insuranceFailedTpl = "failed-message-template";
|
|
|
|
|
|
|
|
// Mock the randomUtil to return a static failed template string.
|
2023-11-13 18:31:52 +01:00
|
|
|
const mockGetArrayValue = vi.spyOn(insuranceController.randomUtil, "getArrayValue").mockReturnValue(
|
|
|
|
insuranceFailedTpl,
|
|
|
|
);
|
2023-11-01 04:08:13 +01:00
|
|
|
|
|
|
|
// Don't actually send the message.
|
2023-11-03 22:39:33 +01:00
|
|
|
const sendMessageSpy = vi.spyOn(insuranceController.mailSendService, "sendLocalisedNpcMessageToPlayer")
|
|
|
|
.mockImplementation(vi.fn());
|
2023-11-01 04:08:13 +01:00
|
|
|
|
|
|
|
// Execute the method.
|
|
|
|
insuranceController.sendMail(sessionID, insurance);
|
|
|
|
|
2023-11-03 22:39:33 +01:00
|
|
|
// Verify that the randomUtil.getArrayValue method was not called.
|
|
|
|
expect(mockGetArrayValue).not.toBeCalled();
|
|
|
|
|
2023-11-01 04:08:13 +01:00
|
|
|
// Verify that the insurance failed message was not sent.
|
2023-11-03 22:39:33 +01:00
|
|
|
expect(sendMessageSpy).toHaveBeenCalledWith(
|
2023-11-01 04:08:13 +01:00
|
|
|
sessionID,
|
|
|
|
traderHelper.getTraderById(insurance.traderId),
|
|
|
|
MessageType.INSURANCE_RETURN,
|
2023-11-03 22:39:33 +01:00
|
|
|
insurance.messageContent.templateId,
|
2023-11-01 04:08:13 +01:00
|
|
|
insurance.items,
|
|
|
|
insurance.messageContent.maxStorageTime,
|
2023-11-10 23:21:20 +01:00
|
|
|
insurance.messageContent.systemData,
|
2023-11-01 04:08:13 +01:00
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("rollForDelete", () =>
|
|
|
|
{
|
|
|
|
it("should return true when random roll is equal to trader return chance", () =>
|
|
|
|
{
|
|
|
|
vi.spyOn(insuranceController.randomUtil, "getInt").mockReturnValue(8500); // Our "random" roll.
|
|
|
|
const traderId = "54cb57776803fa99248b456e"; // Therapist (85% return chance)
|
|
|
|
insuranceController.insuranceConfig = {
|
|
|
|
returnChancePercent: {
|
2023-11-10 23:21:20 +01:00
|
|
|
[traderId]: 85, // Force 85% return chance
|
|
|
|
},
|
2023-11-01 04:08:13 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
// Execute the method.
|
|
|
|
const result = insuranceController.rollForDelete(traderId);
|
|
|
|
|
|
|
|
// Verify that the result is true.
|
|
|
|
expect(result).toBe(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should return true when random roll is greater than trader return chance", () =>
|
|
|
|
{
|
|
|
|
vi.spyOn(insuranceController.randomUtil, "getInt").mockReturnValue(8501); // Our "random" roll.
|
|
|
|
const traderId = "54cb57776803fa99248b456e"; // Therapist (85% return chance)
|
|
|
|
insuranceController.insuranceConfig = {
|
|
|
|
returnChancePercent: {
|
2023-11-10 23:21:20 +01:00
|
|
|
[traderId]: 85, // Force 85% return chance
|
|
|
|
},
|
2023-11-01 04:08:13 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
// Execute the method.
|
|
|
|
const result = insuranceController.rollForDelete(traderId);
|
|
|
|
|
|
|
|
// Verify that the result is true.
|
|
|
|
expect(result).toBe(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should return false when random roll is less than trader return chance", () =>
|
|
|
|
{
|
|
|
|
vi.spyOn(insuranceController.randomUtil, "getInt").mockReturnValue(8499); // Our "random" roll.
|
|
|
|
const traderId = "54cb57776803fa99248b456e"; // Therapist (85% return chance)
|
|
|
|
insuranceController.insuranceConfig = {
|
|
|
|
returnChancePercent: {
|
2023-11-10 23:21:20 +01:00
|
|
|
[traderId]: 85, // Force 85% return chance
|
|
|
|
},
|
2023-11-01 04:08:13 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
// Execute the method.
|
|
|
|
const result = insuranceController.rollForDelete(traderId);
|
|
|
|
|
|
|
|
// Verify that the result is false.
|
|
|
|
expect(result).toBe(false);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should log error if trader can not be found", () =>
|
|
|
|
{
|
|
|
|
const traderId = "invalid-trader-id";
|
|
|
|
|
2023-11-03 22:39:33 +01:00
|
|
|
const loggerErrorSpy = vi.spyOn(insuranceController.logger, "error");
|
2023-11-01 04:08:13 +01:00
|
|
|
|
|
|
|
// Execute the method.
|
|
|
|
insuranceController.rollForDelete(traderId);
|
|
|
|
|
|
|
|
// Verify that the logger.error method was called.
|
|
|
|
expect(loggerErrorSpy).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should return null if trader can not be found", () =>
|
|
|
|
{
|
|
|
|
const traderId = "invalid-trader-id";
|
|
|
|
|
|
|
|
// Execute the method.
|
|
|
|
const result = insuranceController.rollForDelete(traderId);
|
|
|
|
|
|
|
|
// Verify that the result is null.
|
|
|
|
expect(result).toBe(null);
|
|
|
|
});
|
|
|
|
});
|
2023-11-03 22:39:33 +01:00
|
|
|
|
|
|
|
describe("insure", () =>
|
|
|
|
{
|
2023-11-13 17:51:52 +01:00
|
|
|
let pmcData: any;
|
|
|
|
let body: any;
|
|
|
|
let sessionId: string;
|
|
|
|
let insuranceController: any;
|
|
|
|
let mockGetPremium: any;
|
|
|
|
let mockPayMoney: any;
|
|
|
|
let mockGetOutput: any;
|
2023-11-03 22:39:33 +01:00
|
|
|
|
|
|
|
beforeEach(() =>
|
|
|
|
{
|
|
|
|
insuranceController = container.resolve<InsuranceController>("InsuranceController");
|
|
|
|
|
|
|
|
// Setup shared test data.
|
|
|
|
pmcData = {
|
2023-11-13 18:38:16 +01:00
|
|
|
Inventory: { items: [{ _id: "item1", otherProps: "value1" }, { _id: "item2", otherProps: "value2" }] },
|
2023-11-10 23:21:20 +01:00
|
|
|
InsuredItems: [],
|
2023-11-03 22:39:33 +01:00
|
|
|
};
|
2023-11-13 18:38:16 +01:00
|
|
|
body = { items: ["item1", "item2"], tid: "someTraderId" };
|
2023-11-03 22:39:33 +01:00
|
|
|
sessionId = "session-id";
|
|
|
|
|
|
|
|
// Setup shared mocks.
|
|
|
|
mockGetPremium = vi.spyOn(insuranceController.insuranceService, "getPremium").mockReturnValue(100);
|
|
|
|
mockPayMoney = vi.spyOn(insuranceController.paymentService, "payMoney").mockReturnValue({
|
|
|
|
warnings: [],
|
2023-11-10 23:21:20 +01:00
|
|
|
otherProperty: "property-value",
|
2023-11-03 22:39:33 +01:00
|
|
|
});
|
|
|
|
mockGetOutput = vi.spyOn(insuranceController.eventOutputHolder, "getOutput").mockReturnValue({
|
|
|
|
warnings: [],
|
2023-11-10 23:21:20 +01:00
|
|
|
otherProperty: "property-value",
|
2023-11-03 22:39:33 +01:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should create a hash of inventory items by ID", () =>
|
|
|
|
{
|
|
|
|
// Execute the method.
|
|
|
|
insuranceController.insure(pmcData, body, sessionId);
|
|
|
|
|
|
|
|
// Since the inventoryItemsHash is internal to the function, we cannot check it directly. However, we can
|
|
|
|
// infer its correctness by ensuring the payMoney function is called with the right "scheme_items" property.
|
|
|
|
expect(mockPayMoney).toHaveBeenCalledWith(
|
|
|
|
pmcData,
|
|
|
|
{
|
2023-11-13 18:38:16 +01:00
|
|
|
scheme_items: [{ id: "item1", count: 100 }, { id: "item2", count: 100 }],
|
2023-11-03 22:39:33 +01:00
|
|
|
tid: "someTraderId",
|
|
|
|
Action: "",
|
|
|
|
type: "",
|
|
|
|
item_id: "",
|
|
|
|
count: 0,
|
2023-11-10 23:21:20 +01:00
|
|
|
scheme_id: 0,
|
2023-11-03 22:39:33 +01:00
|
|
|
},
|
2023-11-10 23:21:20 +01:00
|
|
|
sessionId,
|
2023-11-13 18:38:16 +01:00
|
|
|
{ warnings: [], otherProperty: "property-value" },
|
2023-11-03 22:39:33 +01:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should calculate the insurance premium for each item to insure", () =>
|
|
|
|
{
|
|
|
|
// Execute the method.
|
|
|
|
insuranceController.insure(pmcData, body, sessionId);
|
|
|
|
|
|
|
|
// Verify that getPremium is called with each item from the pmcData.Inventory.items array.
|
|
|
|
for (const item of pmcData.Inventory.items)
|
|
|
|
{
|
|
|
|
expect(mockGetPremium).toHaveBeenCalledWith(pmcData, item, body.tid);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify that getPremium was called the correct number of times.
|
|
|
|
expect(mockGetPremium).toHaveBeenCalledTimes(body.items.length);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should call the payment service with the correct parameters", () =>
|
|
|
|
{
|
|
|
|
// Execute the method.
|
|
|
|
insuranceController.insure(pmcData, body, sessionId);
|
|
|
|
|
|
|
|
// Define the expected payment options structure based on the setup data.
|
|
|
|
const expectedPaymentOptions = {
|
2023-11-13 18:38:16 +01:00
|
|
|
scheme_items: [{ id: "item1", count: 100 }, { id: "item2", count: 100 }],
|
2023-11-03 22:39:33 +01:00
|
|
|
tid: body.tid,
|
|
|
|
Action: "",
|
|
|
|
type: "",
|
|
|
|
item_id: "",
|
|
|
|
count: 0,
|
2023-11-10 23:21:20 +01:00
|
|
|
scheme_id: 0,
|
2023-11-03 22:39:33 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
// Verify that the paymentService's payMoney method was called once with the expected parameters.
|
|
|
|
expect(mockPayMoney).toHaveBeenCalledWith(pmcData, expectedPaymentOptions, sessionId, expect.any(Object));
|
|
|
|
|
|
|
|
// Verify that the output passed to payMoney is the one obtained from getOutput.
|
|
|
|
expect(mockPayMoney).toHaveBeenCalledWith(
|
|
|
|
pmcData,
|
|
|
|
expectedPaymentOptions,
|
|
|
|
sessionId,
|
2023-11-10 23:21:20 +01:00
|
|
|
mockGetOutput.mock.results[0].value,
|
2023-11-03 22:39:33 +01:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should add items to InsuredItems after successful payment", () =>
|
|
|
|
{
|
|
|
|
// Execute the method.
|
|
|
|
insuranceController.insure(pmcData, body, sessionId);
|
|
|
|
|
|
|
|
// Verify that the InsuredItems array has been populated with the correct items.
|
2023-11-10 23:21:20 +01:00
|
|
|
const insuredItemIds = pmcData.InsuredItems.map((insuredItem) => insuredItem.itemId);
|
2023-11-03 22:39:33 +01:00
|
|
|
expect(insuredItemIds).toContain("item1");
|
|
|
|
expect(insuredItemIds).toContain("item2");
|
|
|
|
|
|
|
|
// Verify that the number of InsuredItems matches the number of items intended to be insured.
|
|
|
|
expect(pmcData.InsuredItems.length).toBe(body.items.length);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should return the output with warnings if payment fails", () =>
|
|
|
|
{
|
|
|
|
// Override the payMoney mock to simulate a payment failure with a warning.
|
|
|
|
const expectedPayMoneyReturn = {
|
2023-11-13 18:38:16 +01:00
|
|
|
warnings: [{ index: 0, errmsg: "Not enough money to complete transaction", code: 500 }],
|
2023-11-10 23:21:20 +01:00
|
|
|
otherProperty: "property-value",
|
2023-11-03 22:39:33 +01:00
|
|
|
};
|
|
|
|
mockPayMoney.mockReturnValue(expectedPayMoneyReturn);
|
|
|
|
|
|
|
|
// Execute the method.
|
|
|
|
const response = insuranceController.insure(pmcData, body, sessionId);
|
|
|
|
|
|
|
|
// Verify that the response contains the warnings from the payment failure
|
|
|
|
expect(response.warnings).toStrictEqual(expectedPayMoneyReturn.warnings);
|
|
|
|
|
|
|
|
// Verify that other properties from the response are still present.
|
|
|
|
expect(response).toHaveProperty("otherProperty", "property-value");
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should not add items to InsuredItems if payment fails", () =>
|
|
|
|
{
|
|
|
|
// Override the payMoney mock to simulate a payment failure with a warning.
|
|
|
|
const expectedPayMoneyReturn = {
|
2023-11-13 18:38:16 +01:00
|
|
|
warnings: [{ index: 0, errmsg: "Not enough money to complete transaction", code: 500 }],
|
2023-11-10 23:21:20 +01:00
|
|
|
otherProperty: "property-value",
|
2023-11-03 22:39:33 +01:00
|
|
|
};
|
|
|
|
mockPayMoney.mockReturnValue(expectedPayMoneyReturn);
|
|
|
|
|
|
|
|
// Execute the method.
|
|
|
|
insuranceController.insure(pmcData, body, sessionId);
|
|
|
|
|
|
|
|
// Verify that the InsuredItems array has not been populated.
|
|
|
|
expect(pmcData.InsuredItems).toHaveLength(0);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("cost", () =>
|
|
|
|
{
|
|
|
|
let sessionId: string;
|
|
|
|
|
|
|
|
beforeEach(() =>
|
|
|
|
{
|
|
|
|
insuranceController = container.resolve<InsuranceController>("InsuranceController");
|
|
|
|
|
|
|
|
sessionId = "session-id";
|
|
|
|
|
|
|
|
vi.spyOn(insuranceController.profileHelper, "getPmcProfile").mockReturnValue({
|
|
|
|
Inventory: {
|
2023-11-13 18:38:16 +01:00
|
|
|
items: [{ _id: "itemId1", _tpl: "itemTpl1", otherProperty: "property-value1" }, {
|
2023-11-13 18:31:52 +01:00
|
|
|
_id: "itemId2",
|
|
|
|
_tpl: "itemTpl2",
|
|
|
|
otherProperty: "property-value2",
|
2023-11-13 18:38:16 +01:00
|
|
|
}, { _id: "itemId3", _tpl: "itemTpl3", otherProperty: "property-value3" }],
|
2023-11-10 23:21:20 +01:00
|
|
|
},
|
2023-11-03 22:39:33 +01:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should return an empty object if no traders and items are specified", () =>
|
|
|
|
{
|
2023-11-13 18:38:16 +01:00
|
|
|
const request = { traders: [], items: [] };
|
2023-11-03 22:39:33 +01:00
|
|
|
const expected = {};
|
|
|
|
|
|
|
|
const result = insuranceController.cost(request, sessionId);
|
|
|
|
|
|
|
|
expect(result).toEqual(expected);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should return an empty object if no items are specified", () =>
|
|
|
|
{
|
2023-11-13 18:38:16 +01:00
|
|
|
const request = { traders: ["prapor"], items: [] };
|
|
|
|
const expected = { prapor: {} };
|
2023-11-03 22:39:33 +01:00
|
|
|
|
|
|
|
const result = insuranceController.cost(request, sessionId);
|
|
|
|
|
|
|
|
expect(result).toEqual(expected);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should return an empty object if no trader is specified but items are", () =>
|
|
|
|
{
|
2023-11-13 18:38:16 +01:00
|
|
|
const request = { traders: [], items: ["itemId1", "itemId2"] };
|
2023-11-03 22:39:33 +01:00
|
|
|
const expected = {};
|
|
|
|
|
|
|
|
const result = insuranceController.cost(request, sessionId);
|
|
|
|
|
|
|
|
expect(result).toEqual(expected);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should return the expected cost for each item and trader", () =>
|
|
|
|
{
|
2023-11-13 18:38:16 +01:00
|
|
|
const request = { traders: ["prapor", "therapist"], items: ["itemId1", "itemId2", "itemId3"] };
|
2023-11-03 22:39:33 +01:00
|
|
|
const expected = {
|
2023-11-13 18:38:16 +01:00
|
|
|
prapor: { itemTpl1: 100, itemTpl2: 200, itemTpl3: 300 },
|
|
|
|
therapist: { itemTpl1: 150, itemTpl2: 250, itemTpl3: 350 },
|
2023-11-03 22:39:33 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
// Mock the InsuranceService.getPremium method to return the expected values.
|
2023-11-13 18:31:52 +01:00
|
|
|
vi.spyOn(insuranceController.insuranceService, "getPremium").mockReturnValueOnce(100).mockReturnValueOnce(
|
|
|
|
200,
|
|
|
|
).mockReturnValueOnce(300).mockReturnValueOnce(150).mockReturnValueOnce(250).mockReturnValueOnce(350);
|
2023-11-03 22:39:33 +01:00
|
|
|
|
|
|
|
const result = insuranceController.cost(request, sessionId);
|
|
|
|
|
|
|
|
expect(result).toEqual(expected);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should skip items that are not in the player's inventory", () =>
|
|
|
|
{
|
|
|
|
const request = {
|
|
|
|
traders: ["prapor"],
|
|
|
|
items: [
|
|
|
|
"itemId1",
|
|
|
|
"itemId2",
|
2023-11-10 23:21:20 +01:00
|
|
|
"itemId4", // Doesn't exist in the player's inventory.
|
|
|
|
],
|
2023-11-03 22:39:33 +01:00
|
|
|
};
|
2023-11-13 18:38:16 +01:00
|
|
|
const expected = { prapor: { itemTpl1: 100, itemTpl2: 200 } };
|
2023-11-03 22:39:33 +01:00
|
|
|
|
|
|
|
// Mock the InsuranceService.getPremium method to return the expected values.
|
2023-11-13 18:31:52 +01:00
|
|
|
vi.spyOn(insuranceController.insuranceService, "getPremium").mockReturnValueOnce(100).mockReturnValueOnce(
|
|
|
|
200,
|
|
|
|
);
|
2023-11-03 22:39:33 +01:00
|
|
|
|
|
|
|
const result = insuranceController.cost(request, sessionId);
|
|
|
|
|
|
|
|
expect(result).toEqual(expected);
|
|
|
|
});
|
|
|
|
});
|
2023-11-01 04:08:13 +01:00
|
|
|
});
|