BlueGit

timfa questward

Files

questward / server / tests / fillCharge.test.js

fillCharge.test.js

fillCharge.test.js 5.2 KiB Download Raw
1import test from "node:test";
2import assert from "node:assert/strict";
3import {
4 emptySlots,
5 insertStack,
6 stacksMatch,
7 consumeFillCharge,
8 stackQuantity
9} from "../../shared/items/inventory.js";
10import { ItemId, itemDefinition } from "../../shared/definitions/items.js";
11import { dyeItemInstance, dyeColorKey } from "../../shared/definitions/dyes.js";
12import { potionItemInstance } from "../../shared/alchemy/recipes.js";
13import { canCraftFitInventory } from "../../shared/crafting/recipes.js";
14import { ClothTier } from "../../shared/definitions/materialTiers.js";
15import { sweaterItemId, yarnItemId } from "../../shared/definitions/clothItems.js";
16
17const red = { r: 0.8, g: 0.1, b: 0.1 };
18const blue = { r: 0.1, g: 0.2, b: 0.9 };
19
20function dyeStack (color, fillLevel, quantity = 1)
21{
22 return {
23 itemId: ItemId.dye,
24 quantity,
25 instance: dyeItemInstance(color, fillLevel)
26 };
27}
28
29function potionStack (fillLevel, quantity = 1)
30{
31 return {
32 itemId: ItemId.potion,
33 quantity,
34 instance: potionItemInstance({
35 potionKey: "lesserHealthTonic",
36 displayName: "Lesser health tonic",
37 color: { r: 0.95, g: 0.38, b: 0.32 },
38 fillLevel,
39 effect: { type: "health-regen", amountPerSecond: 1, durationMs: 10_000 },
40 effectKey: "health-regen"
41 })
42 };
43}
44
45test("dyes stack only when colour and fill match", () =>
46{
47 const def = itemDefinition(ItemId.dye);
48 const a = dyeStack(red, 4);
49 const b = dyeStack(red, 4);
50 const differentFill = dyeStack(red, 3);
51 const differentColor = dyeStack(blue, 4);
52 assert.equal(stacksMatch(a, b, def), true);
53 assert.equal(stacksMatch(a, differentFill, def), false);
54 assert.equal(stacksMatch(a, differentColor, def), false);
55 const first = insertStack(emptySlots(), { ...a, quantity: 2 }, def);
56 const second = insertStack(first.slots, differentFill, def);
57 assert.equal(second.ok, true);
58 assert.equal(stackQuantity(second.slots[0].quantity), 2);
59 assert.equal(second.slots[0].instance.fillLevel, 4);
60 assert.equal(second.slots[1].instance.fillLevel, 3);
61});
62
63test("potions stack only when type and fill match", () =>
64{
65 const def = itemDefinition(ItemId.potion);
66 assert.equal(stacksMatch(potionStack(4), potionStack(4), def), true);
67 assert.equal(stacksMatch(potionStack(4), potionStack(3), def), false);
68});
69
70test("consumeFillCharge mutates a single bottle in place", () =>
71{
72 const slots = emptySlots();
73 slots[0] = dyeStack(red, 3, 1);
74 const result = consumeFillCharge(slots, 0, { kind: "dye", itemId: ItemId.dye });
75 assert.equal(result.ok, true);
76 assert.equal(result.exhausted, false);
77 assert.equal(slots[0].instance.fillLevel, 3);
78 assert.equal(result.slots[0].instance.fillLevel, 2);
79 assert.equal(result.slots[0].quantity, 1);
80});
81
82test("consumeFillCharge peels from a multi-stack without losing the reduced bottle", () =>
83{
84 const slots = emptySlots();
85 slots[0] = dyeStack(red, 4, 3);
86 const result = consumeFillCharge(slots, 0, { kind: "dye", itemId: ItemId.dye });
87 assert.equal(result.ok, true);
88 assert.equal(result.exhausted, false);
89 const fills = result.slots
90 .filter((slot) => slot?.itemId === ItemId.dye)
91 .flatMap((slot) => Array.from({ length: stackQuantity(slot.quantity) }, () => slot.instance.fillLevel))
92 .sort((a, b) => b - a);
93 assert.deepEqual(fills, [4, 4, 3]);
94 assert.equal(slots[0].quantity, 3);
95});
96
97test("consumeFillCharge returns empty bottle when last sip is drunk", () =>
98{
99 const slots = emptySlots();
100 slots[0] = potionStack(1, 1);
101 const result = consumeFillCharge(slots, 0, {
102 kind: "potion",
103 itemId: ItemId.potion,
104 returnEmptyItemId: ItemId.glassBottle
105 });
106 assert.equal(result.ok, true);
107 assert.equal(result.exhausted, true);
108 assert.equal(result.slots[0]?.itemId, ItemId.glassBottle);
109 assert.equal(result.slots[0]?.quantity, 1);
110});
111
112test("consumeFillCharge refuses multi-stack split when bag is full (no item loss)", () =>
113{
114 const slots = emptySlots();
115 for (let i = 0; i < slots.length; i++)
116 { slots[i] = { itemId: ItemId.apple, quantity: 1 }; }
117 slots[0] = dyeStack(red, 4, 2);
118 const before = structuredClone(slots);
119 const result = consumeFillCharge(slots, 0, { kind: "dye", itemId: ItemId.dye });
120 assert.equal(result.ok, false);
121 assert.equal(result.reason, "space");
122 assert.deepEqual(slots, before);
123});
124
125test("canCraftFitInventory accounts for dye charge split after inputs", () =>
126{
127 const slots = emptySlots();
128 const yarn = yarnItemId(ClothTier.cotton);
129 slots[0] = { itemId: yarn, quantity: 3 };
130 slots[1] = dyeStack(red, 4, 2);
131 // Leave slot 2 empty for the craft output after dye peels into a freed yarn slot.
132 for (let i = 3; i < slots.length; i++)
133 { slots[i] = { itemId: ItemId.apple, quantity: 1 }; }
134 const recipe = {
135 output: { itemId: sweaterItemId(ClothTier.cotton), quantity: 1 },
136 inputs: [{ itemId: yarn, quantity: 3 }],
137 consumeDye: true,
138 dyeColor: red
139 };
140 const dyeKey = dyeColorKey({ kind: "dye", color: red, fillLevel: 4 });
141 assert.equal(canCraftFitInventory(slots, recipe, { dyeColorKey: dyeKey }), true);
142 // Full bag with only the yarn slot freeing: dye peel consumes that free slot → no room for sweater.
143 const tight = emptySlots();
144 tight[0] = { itemId: yarn, quantity: 3 };
145 tight[1] = dyeStack(red, 4, 2);
146 for (let i = 2; i < tight.length; i++)
147 { tight[i] = { itemId: ItemId.apple, quantity: 1 }; }
148 assert.equal(canCraftFitInventory(tight, recipe, { dyeColorKey: dyeKey }), false);
149});