BlueGit

timfa questward

Files

questward / server / tests / tanning-ingredients.test.js

tanning-ingredients.test.js

tanning-ingredients.test.js 9.5 KiB Download Raw
1import test from "node:test";
2import assert from "node:assert/strict";
3import { BlockType } from "../../shared/definitions/tiles.js";
4import { ItemId, itemDefinition, itemDisplayName, itemSpriteNameForStack } from "../../shared/definitions/items.js";
5import { CraftingItemId } from "../../shared/definitions/craftingItems.js";
6import {
7 WoodItemId,
8 debarkedLogInstance,
9 isLogDebarked,
10 barkItemIdForWood
11} from "../../shared/definitions/woodItems.js";
12import { WoodTier, GatherMaterialId, MaterialTier, oreBoulderState } from "../../shared/definitions/materialTiers.js";
13import { SkillId } from "../../shared/definitions/skills.js";
14import { LeatherItemId } from "../../shared/definitions/leatherItems.js";
15import { AlchemyStepKind } from "../../shared/definitions/alchemy.js";
16import { canMineMaterial, oreItemIdForMaterial } from "../../shared/definitions/gathering.js";
17import {
18 clearResolvedAlchemyPotionsCache,
19 resolvedAlchemyPotions,
20 evaluateAlchemyBottle,
21 potionItemInstance
22} from "../../shared/alchemy/recipes.js";
23import { StatusEffectType } from "../../shared/statusEffects/rules.js";
24import { isFullPotionBottle } from "../../shared/items/charges.js";
25import { emptyEquipment, emptySlots } from "../../shared/items/inventory.js";
26import { totalXpForLevel } from "../../shared/skills/progression.js";
27import { createEmptyChunkData, encodeChunkBlob } from "../src/world/chunkData.js";
28import { addGroundItem } from "../src/items/bags.js";
29import { Component } from "../src/ecs/components.js";
30import { makeCharacter, makeSimulation } from "./helpers/simulationHarness.js";
31
32function placeCauldron (worldDb, tileX, tileY)
33{
34 const chunkX = Math.floor(tileX / 16);
35 const chunkY = Math.floor(tileY / 16);
36 const data = createEmptyChunkData();
37 data.open = true;
38 const localX = tileX - chunkX * 16;
39 const localY = tileY - chunkY * 16;
40 data.blocks[localY * 16 + localX] = { typeId: BlockType.cauldron, state: 0 };
41 worldDb.saveChunkVersion(
42 chunkX,
43 chunkY,
44 worldDb.getActiveRevisionId(),
45 true,
46 encodeChunkBlob(data)
47 );
48}
49
50function countItem (slots, itemId)
51{
52 return (slots ?? [])
53 .filter((slot) => slot?.itemId === itemId)
54 .reduce((sum, slot) => sum + (slot.quantity | 0), 0);
55}
56
57function setSkillLevel (harness, characterId, skillId, level)
58{
59 const skills = harness.stateDb.getSkills(characterId);
60 skills.xp[skillId] = totalXpForLevel(level);
61 harness.stateDb.saveSkills(characterId, skills);
62}
63
64test("rock salt mines with bronze pick and uses salt-ore boulder state", () =>
65{
66 assert.equal(oreBoulderState(GatherMaterialId.salt), "salt-ore");
67 assert.equal(oreItemIdForMaterial(GatherMaterialId.salt), CraftingItemId.salt);
68 assert.equal(canMineMaterial(1, MaterialTier.bronze, GatherMaterialId.salt).ok, true);
69 const def = itemDefinition(CraftingItemId.salt);
70 assert.equal(def.key, "rock-salt");
71 assert.equal(def.iconKey, "ash");
72 assert.equal(def.tier, GatherMaterialId.salt);
73});
74
75test("debarked logs keep oak itemId but change name and sprite", () =>
76{
77 const plain = { itemId: WoodItemId.oakLog, quantity: 1 };
78 const stripped = { itemId: WoodItemId.oakLog, quantity: 1, instance: debarkedLogInstance() };
79 assert.equal(isLogDebarked(plain), false);
80 assert.equal(isLogDebarked(stripped), true);
81 assert.equal(itemDisplayName(plain), "Oak log");
82 assert.equal(itemDisplayName(stripped), "Debarked Oak log");
83 assert.match(itemSpriteNameForStack(stripped), /log-debarked/);
84 assert.equal(barkItemIdForWood(WoodTier.oak), WoodItemId.oakBark);
85});
86
87test("debark-log on ground oak log yields bark and debarked flag", () =>
88{
89 const harness = makeSimulation({ workerCount: 1 });
90 try
91 {
92 harness.simulation.reloadWorld();
93 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
94 setSkillLevel(harness, character.character_id, SkillId.woodcutting, 20);
95 const inventory = emptySlots();
96 inventory[0] = { itemId: ItemId.ironAxe, quantity: 1 };
97 harness.stateDb.saveBag(character.character_id, {
98 inventory,
99 equipment: emptyEquipment(),
100 vault: [],
101 vaultReserved: []
102 });
103 const entityId = harness.simulation.spawnCharacter(1, character);
104 const record = harness.simulation.entityRecord(entityId);
105 const worker = harness.simulation.workers[record.workerId];
106
107 const groundItemId = (worker.world.nextGroundItemId = (worker.world.nextGroundItemId ?? 1) + 1);
108 addGroundItem(worker.world, {
109 groundItemId,
110 tileX: 8,
111 tileY: 9,
112 stack: { itemId: WoodItemId.oakLog, quantity: 1 },
113 droppedAt: Date.now(),
114 expiresAt: Date.now() + 600_000,
115 source: "test"
116 });
117
118 harness.simulation.enqueue(1, {
119 type: "debark-log",
120 characterId: character.character_id,
121 groundItemId
122 });
123 harness.simulation.step();
124
125 const item = worker.world.groundItems.get(groundItemId);
126 assert.ok(item);
127 assert.equal(isLogDebarked(item.stack), true);
128 assert.equal(item.stack.itemId, WoodItemId.oakLog);
129
130 const bark = [...worker.world.groundItems.values()]
131 .find((row) => row.stack?.itemId === WoodItemId.oakBark);
132 assert.ok(bark);
133 assert.equal(bark.stack.quantity, 1);
134 }
135 finally
136 { harness.close(); }
137});
138
139test("animal fat and tanning brew alchemy recipes resolve", () =>
140{
141 clearResolvedAlchemyPotionsCache();
142 const potions = resolvedAlchemyPotions();
143 const fat = potions.find((row) => row.potionKey === "animalFat");
144 const tan = potions.find((row) => row.potionKey === "tanningBrew");
145 assert.ok(fat);
146 assert.equal(fat.effect?.type, StatusEffectType.foodPoisoning);
147 assert.ok(tan);
148 assert.equal(tan.outputItemId, LeatherItemId.tanningBrew);
149 assert.equal(tan.requiresEmptyBottles, false);
150
151 const fatEval = evaluateAlchemyBottle({
152 skills: { levels: { [SkillId.alchemy]: 1 } },
153 brewStacks: [{ itemId: CraftingItemId.rawMeat, quantity: 1 }],
154 takenSteps: fat.steps
155 });
156 assert.equal(fatEval.success, true);
157 assert.equal(fatEval.result.potionKey, "animalFat");
158
159 const fatBottle = potionItemInstance(fatEval.result);
160 assert.equal(isFullPotionBottle({ itemId: ItemId.potion, instance: fatBottle }), true);
161
162 const tanEval = evaluateAlchemyBottle({
163 skills: { levels: { [SkillId.alchemy]: 10 } },
164 brewStacks: [
165 { itemId: WoodItemId.oakBark, quantity: 2 },
166 { itemId: CraftingItemId.salt, quantity: 1 },
167 { itemId: ItemId.potion, quantity: 1, instance: fatBottle }
168 ],
169 takenSteps: tan.steps
170 });
171 assert.equal(tanEval.success, true);
172 assert.equal(tanEval.result.outputItemId, LeatherItemId.tanningBrew);
173});
174
175test("cauldron bottles animal fat then tanning brew returns the empty bottle", () =>
176{
177 const harness = makeSimulation({ workerCount: 1 });
178 try
179 {
180 placeCauldron(harness.worldDb, 8, 8);
181 harness.simulation.reloadWorld();
182 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 9 });
183 setSkillLevel(harness, character.character_id, SkillId.alchemy, 10);
184 const inventory = emptySlots();
185 inventory[0] = { itemId: CraftingItemId.rawMeat, quantity: 1 };
186 inventory[1] = { itemId: ItemId.glassBottle, quantity: 1 };
187 inventory[2] = { itemId: WoodItemId.oakBark, quantity: 2 };
188 inventory[3] = { itemId: CraftingItemId.salt, quantity: 1 };
189 harness.stateDb.saveBag(character.character_id, {
190 inventory,
191 equipment: emptyEquipment(),
192 vault: [],
193 vaultReserved: []
194 });
195 const entityId = harness.simulation.spawnCharacter(1, character);
196 const cid = character.character_id;
197
198 harness.simulation.enqueue(1, { type: "open-cauldron", characterId: cid });
199 harness.simulation.step();
200 harness.simulation.enqueue(1, {
201 type: "cauldron-insert",
202 characterId: cid,
203 itemId: CraftingItemId.rawMeat,
204 quantity: 1
205 });
206 for (const action of [AlchemyStepKind.heat, AlchemyStepKind.stir, AlchemyStepKind.cool])
207 {
208 harness.simulation.enqueue(1, {
209 type: "cauldron-action",
210 characterId: cid,
211 action
212 });
213 }
214 harness.simulation.enqueue(1, { type: "cauldron-bottle", characterId: cid });
215 harness.simulation.step();
216
217 let bag = harness.simulation.bagOf(entityId);
218 assert.equal(countItem(bag.inventory, CraftingItemId.rawMeat), 0);
219 assert.equal(countItem(bag.inventory, ItemId.potion), 1);
220 const fatSlot = bag.inventory.find((slot) => slot?.itemId === ItemId.potion);
221 assert.equal(fatSlot.instance.potionKey, "animalFat");
222 assert.equal(countItem(bag.inventory, ItemId.glassBottle), 0);
223
224 harness.simulation.enqueue(1, { type: "cauldron-clear", characterId: cid });
225 harness.simulation.step();
226 harness.simulation.enqueue(1, {
227 type: "cauldron-insert",
228 characterId: cid,
229 itemId: WoodItemId.oakBark,
230 quantity: 2
231 });
232 harness.simulation.enqueue(1, {
233 type: "cauldron-action",
234 characterId: cid,
235 action: AlchemyStepKind.heat
236 });
237 harness.simulation.enqueue(1, {
238 type: "cauldron-insert",
239 characterId: cid,
240 itemId: CraftingItemId.salt,
241 quantity: 1
242 });
243 harness.simulation.enqueue(1, {
244 type: "cauldron-action",
245 characterId: cid,
246 action: AlchemyStepKind.stir
247 });
248 harness.simulation.enqueue(1, {
249 type: "cauldron-insert",
250 characterId: cid,
251 itemId: ItemId.potion,
252 quantity: 1,
253 instance: structuredClone(fatSlot.instance)
254 });
255 harness.simulation.enqueue(1, {
256 type: "cauldron-action",
257 characterId: cid,
258 action: AlchemyStepKind.cool
259 });
260 harness.simulation.enqueue(1, { type: "cauldron-bottle", characterId: cid });
261 harness.simulation.step();
262
263 bag = harness.simulation.bagOf(entityId);
264 assert.equal(countItem(bag.inventory, LeatherItemId.tanningBrew), 1);
265 assert.equal(countItem(bag.inventory, ItemId.potion), 0);
266 assert.equal(countItem(bag.inventory, ItemId.glassBottle), 1);
267 assert.equal(countItem(bag.inventory, WoodItemId.oakBark), 0);
268 assert.equal(countItem(bag.inventory, CraftingItemId.salt), 0);
269 }
270 finally
271 { harness.close(); }
272});