Test - ItemHelper - 3.8 Refactor

- Removes unused code from getRepairableItemQualityValue in itemHelper
- Updated tests for getRepairableItemQualityValue to no longer assume a special calculation for armour
- Added tests for adoptOrphanedItems (moved from insuranceController)
This commit is contained in:
Refringe 2024-04-11 21:04:06 -04:00
parent 7d3eaf735a
commit 5aebbb9c86
No known key found for this signature in database
GPG Key ID: 7715B85B4A6306ED
2 changed files with 72 additions and 36 deletions

View File

@ -429,19 +429,12 @@ export class ItemHelper
if (repairable.Durability > repairable.MaxDurability)
{
this.logger.warning(
`Max durability: ${repairable.MaxDurability} for item id: ${item._id} was below Durability: ${repairable.Durability}, adjusting values to match`,
`Max durability: ${repairable.MaxDurability} for item id: ${item._id} was below durability: ${repairable.Durability}, adjusting values to match`,
);
repairable.MaxDurability = repairable.Durability;
}
// Armor
if (itemDetails._props.armorClass)
{
return repairable.MaxDurability / itemDetails._props.MaxDurability;
}
// Weapon
// Get max dura from props, if it isnt there use repairable max dura value
// Attempt to get the max durability from _props. If not available, use Repairable max durability value instead.
const maxDurability = (itemDetails._props.MaxDurability)
? itemDetails._props.MaxDurability
: repairable.MaxDurability;

View File

@ -522,32 +522,7 @@ describe("ItemHelper", () =>
describe("getRepairableItemQualityValue", () =>
{
it("should return the correct quality value for armour items", () =>
{
const armour = itemHelper.getItem("5648a7494bdc2d9d488b4583")[1];
const repairable: Repairable = { Durability: 25, MaxDurability: 50 };
const mockItem: Item = { _id: "", _tpl: "" };
const result = (itemHelper as any).getRepairableItemQualityValue(armour, repairable, mockItem);
expect(result).toBe(0.5);
});
it("should not use the Repairable MaxDurability property for armour", () =>
{
const armour = itemHelper.getItem("5648a7494bdc2d9d488b4583")[1];
const repairable: Repairable = {
Durability: 25,
MaxDurability: 1000, // This should be ignored.
};
const mockItem: Item = { _id: "", _tpl: "" };
const result = (itemHelper as any).getRepairableItemQualityValue(armour, repairable, mockItem);
expect(result).toBe(0.5);
});
it("should return the correct quality value for weapon items", () =>
it("should return the correct quality value", () =>
{
const weapon = itemHelper.getItem("5a38e6bac4a2826c6e06d79b")[1]; // "TOZ-106 20ga bolt-action shotgun"
const repairable: Repairable = { Durability: 50, MaxDurability: 100 };
@ -559,7 +534,7 @@ describe("ItemHelper", () =>
expect(result).toBe(Math.sqrt(0.5));
});
it("should fall back to using Repairable MaxDurability for weapon items", () =>
it("should fall back to using Repairable MaxDurability", () =>
{
const weapon = itemHelper.getItem("5a38e6bac4a2826c6e06d79b")[1]; // "TOZ-106 20ga bolt-action shotgun"
weapon._props.MaxDurability = undefined; // Remove the MaxDurability property.
@ -899,6 +874,74 @@ describe("ItemHelper", () =>
});
});
describe("adoptOrphanedItems", () =>
{
it("should adopt orphaned items by resetting them as base-level items", () =>
{
const rootId = "root-id";
const items = [
{ _id: "first-id", _tpl: "anything1", parentId: "does-not-exist", slotId: "main" },
{ _id: "second-id", _tpl: "anything2", parentId: "first-id", slotId: "slot-id" },
{ _id: "third-id", _tpl: "anything3", parentId: "second-id", slotId: "slot-id" },
{ _id: "forth-id", _tpl: "anything4", parentId: "third-id", slotId: "slot-id" },
];
// Iterate over the items and find the individual orphaned item.
const orphanedItem = items.find((item) => !items.some((parent) => parent._id === item.parentId));
// Setup tests to verify that the orphaned item is in fact orphaned.
expect(orphanedItem.parentId).toBe(items[0].parentId);
expect(orphanedItem.slotId).toBe(items[0].slotId);
// Execute the method.
(itemHelper as any).adoptOrphanedItems(rootId, items);
// Verify that the orphaned items have been adopted.
expect(orphanedItem.parentId).toBe(rootId);
expect(orphanedItem.slotId).toBe("hideout");
});
it("should not adopt items that are not orphaned", () =>
{
const rootId = "root-id";
const items = [
{ _id: "first-id", _tpl: "anything1", parentId: rootId, slotId: "hideout" },
{ _id: "second-id", _tpl: "anything2", parentId: "first-id", slotId: "slot-id" },
{ _id: "third-id", _tpl: "anything3", parentId: "second-id", slotId: "slot-id" },
{ _id: "forth-id", _tpl: "anything4", parentId: "third-id", slotId: "slot-id" },
];
// Execute the method.
const adopted = (itemHelper as any).adoptOrphanedItems(rootId, items);
// Verify that the orphaned items have been adopted.
expect(adopted).toStrictEqual(items);
});
it("should remove location data from adopted items", () =>
{
const rootId = "root-id";
const items = [
{
_id: "first-id",
_tpl: "anything1",
parentId: "does-not-exist",
slotId: "main",
location: { x: 1, y: 2, r: 3, isSearched: true }, // Should be removed.
},
{ _id: "second-id", _tpl: "anything2", parentId: "first-id", slotId: "slot-id" },
{ _id: "third-id", _tpl: "anything3", parentId: "second-id", slotId: "slot-id" },
{ _id: "forth-id", _tpl: "anything4", parentId: "third-id", slotId: "slot-id" },
];
// Execute the method.
(itemHelper as any).adoptOrphanedItems(rootId, items);
// Verify that the location property has been removed.
expect(items).not.toHaveProperty("location");
});
});
describe("splitStack", () =>
{
it("should return array of two items when provided item over its natural stack size limit", () =>