import test from "node:test"; import assert from "node:assert/strict"; import { emptySlots, insertStack, stacksMatch, consumeFillCharge, stackQuantity } from "../../shared/items/inventory.js"; import { ItemId, itemDefinition } from "../../shared/definitions/items.js"; import { dyeItemInstance } from "../../shared/definitions/dyes.js"; import { potionItemInstance } from "../../shared/alchemy/recipes.js"; import { canCraftFitInventory } from "../../shared/crafting/recipes.js"; import { ClothTier } from "../../shared/definitions/materialTiers.js"; import { sweaterItemId, yarnItemId } from "../../shared/definitions/clothItems.js"; const red = { r: 0.8, g: 0.1, b: 0.1 }; const blue = { r: 0.1, g: 0.2, b: 0.9 }; function dyeStack (color, fillLevel, quantity = 1) { return { itemId: ItemId.dye, quantity, instance: dyeItemInstance(color, fillLevel) }; } function potionStack (fillLevel, quantity = 1) { return { itemId: ItemId.potion, quantity, instance: potionItemInstance({ potionKey: "lesserHealthTonic", displayName: "Lesser health tonic", color: { r: 0.95, g: 0.38, b: 0.32 }, fillLevel, effect: { type: "health-regen", amountPerSecond: 1, durationMs: 10_000 }, effectKey: "health-regen" }) }; } test("dyes stack only when colour and fill match", () => { const def = itemDefinition(ItemId.dye); const a = dyeStack(red, 4); const b = dyeStack(red, 4); const differentFill = dyeStack(red, 3); const differentColor = dyeStack(blue, 4); assert.equal(stacksMatch(a, b, def), true); assert.equal(stacksMatch(a, differentFill, def), false); assert.equal(stacksMatch(a, differentColor, def), false); const first = insertStack(emptySlots(), { ...a, quantity: 2 }, def); const second = insertStack(first.slots, differentFill, def); assert.equal(second.ok, true); assert.equal(stackQuantity(second.slots[0].quantity), 2); assert.equal(second.slots[0].instance.fillLevel, 4); assert.equal(second.slots[1].instance.fillLevel, 3); }); test("potions stack only when type and fill match", () => { const def = itemDefinition(ItemId.potion); assert.equal(stacksMatch(potionStack(4), potionStack(4), def), true); assert.equal(stacksMatch(potionStack(4), potionStack(3), def), false); }); test("consumeFillCharge mutates a single bottle in place", () => { const slots = emptySlots(); slots[0] = dyeStack(red, 3, 1); const result = consumeFillCharge(slots, 0, { kind: "dye", itemId: ItemId.dye }); assert.equal(result.ok, true); assert.equal(result.exhausted, false); assert.equal(slots[0].instance.fillLevel, 3); assert.equal(result.slots[0].instance.fillLevel, 2); assert.equal(result.slots[0].quantity, 1); }); test("consumeFillCharge peels from a multi-stack without losing the reduced bottle", () => { const slots = emptySlots(); slots[0] = dyeStack(red, 4, 3); const result = consumeFillCharge(slots, 0, { kind: "dye", itemId: ItemId.dye }); assert.equal(result.ok, true); assert.equal(result.exhausted, false); const fills = result.slots .filter((slot) => slot?.itemId === ItemId.dye) .flatMap((slot) => Array.from({ length: stackQuantity(slot.quantity) }, () => slot.instance.fillLevel)) .sort((a, b) => b - a); assert.deepEqual(fills, [4, 4, 3]); assert.equal(slots[0].quantity, 3); }); test("consumeFillCharge returns empty bottle when last sip is drunk", () => { const slots = emptySlots(); slots[0] = potionStack(1, 1); const result = consumeFillCharge(slots, 0, { kind: "potion", itemId: ItemId.potion, returnEmptyItemId: ItemId.glassBottle }); assert.equal(result.ok, true); assert.equal(result.exhausted, true); assert.equal(result.slots[0]?.itemId, ItemId.glassBottle); assert.equal(result.slots[0]?.quantity, 1); }); test("consumeFillCharge refuses multi-stack split when bag is full (no item loss)", () => { const slots = emptySlots(); for (let i = 0; i < slots.length; i++) { slots[i] = { itemId: ItemId.apple, quantity: 1 }; } slots[0] = dyeStack(red, 4, 2); const before = structuredClone(slots); const result = consumeFillCharge(slots, 0, { kind: "dye", itemId: ItemId.dye }); assert.equal(result.ok, false); assert.equal(result.reason, "space"); assert.deepEqual(slots, before); }); test("canCraftFitInventory accounts for dye charge split after inputs", () => { const slots = emptySlots(); const yarn = yarnItemId(ClothTier.cotton); slots[0] = { itemId: yarn, quantity: 3 }; slots[1] = dyeStack(red, 4, 2); // Leave slot 2 empty for the craft output after dye peels into a freed yarn slot. for (let i = 3; i < slots.length; i++) { slots[i] = { itemId: ItemId.apple, quantity: 1 }; } const recipe = { output: { itemId: sweaterItemId(ClothTier.cotton), quantity: 1 }, inputs: [{ itemId: yarn, quantity: 3 }], consumeDye: true, dyeColor: red }; assert.equal(canCraftFitInventory(slots, recipe, { dyeSlotIndex: 1 }), true); // Full bag with only the yarn slot freeing: dye peel consumes that free slot → no room for sweater. const tight = emptySlots(); tight[0] = { itemId: yarn, quantity: 3 }; tight[1] = dyeStack(red, 4, 2); for (let i = 2; i < tight.length; i++) { tight[i] = { itemId: ItemId.apple, quantity: 1 }; } assert.equal(canCraftFitInventory(tight, recipe, { dyeSlotIndex: 1 }), false); });