BlueGit

timfa questward

Files

questward / server / tests / alchemy.test.js

alchemy.test.js

alchemy.test.js 15.3 KiB Download Raw
1import test from "node:test";
2import assert from "node:assert/strict";
3import { BlockType } from "../../shared/definitions/tiles.js";
4import { ItemId, itemDefinition } from "../../shared/definitions/items.js";
5import { flowerItemInstance } from "../../shared/definitions/dyes.js";
6import { AlchemyStepKind } from "../../shared/definitions/alchemy.js";
7import
8 {
9 evaluateAlchemyBottle,
10 hashAlchemySteps,
11 resolvedAlchemyPotions,
12 unstablePotionFromSteps,
13 potionItemInstance,
14 recipePaperBody,
15 formatAlchemyStep
16 } from "../../shared/alchemy/recipes.js";
17import { alchemyPotionDefinition } from "../../shared/definitions/alchemy.js";
18import { emptyEquipment, emptySlots, insertStack } from "../../shared/items/inventory.js";
19import { createEmptyChunkData, encodeChunkBlob } from "../src/world/chunkData.js";
20import { Component } from "../src/ecs/components.js";
21import
22 {
23 makeCharacter,
24 makeSimulation,
25 stepTimes
26 } from "./helpers/simulationHarness.js";
27
28function placeCauldron (worldDb, tileX, tileY)
29{
30 const chunkX = Math.floor(tileX / 16);
31 const chunkY = Math.floor(tileY / 16);
32 const data = createEmptyChunkData();
33 data.open = true;
34 const localX = tileX - chunkX * 16;
35 const localY = tileY - chunkY * 16;
36 data.blocks[localY * 16 + localX] = { typeId: BlockType.cauldron, state: 0 };
37 worldDb.saveChunkVersion(
38 chunkX,
39 chunkY,
40 worldDb.getActiveRevisionId(),
41 true,
42 encodeChunkBlob(data)
43 );
44}
45
46function countItem (slots, itemId)
47{
48 return (slots ?? [])
49 .filter((slot) => slot?.itemId === itemId)
50 .reduce((sum, slot) => sum + (slot.quantity | 0), 0);
51}
52
53test("recipe paper body resolves ingredient keys to display names", () =>
54{
55 const potion = alchemyPotionDefinition("tanningBrew");
56 assert.ok(potion);
57 const body = recipePaperBody(potion);
58 assert.match(body, /2× Oak bark/);
59 assert.match(body, /1× Rock salt/);
60 assert.match(body, /1× Bottle of Animal Fat \(full bottle\)/);
61 assert.doesNotMatch(body, /oak-bark/);
62 assert.doesNotMatch(body, /rock-salt/);
63 assert.match(body, /- insert Oak bark/);
64 assert.match(body, /- insert Rock salt/);
65 assert.equal(
66 formatAlchemyStep({ kind: AlchemyStepKind.insert, itemKey: "oak-bark" }),
67 "insert Oak bark"
68 );
69});
70
71test("lesser health tonic ratios and steps evaluate to a success batch", () =>
72{
73 const potion = resolvedAlchemyPotions().find((entry) => entry.potionKey === "lesserHealthTonic");
74 assert.ok(potion);
75 const evaluation = evaluateAlchemyBottle({
76 skills: { levels: { alchemy: 1 } },
77 brewStacks: [
78 { itemId: ItemId.thyme, quantity: 2 },
79 { itemId: ItemId.mint, quantity: 2 }
80 ],
81 takenSteps: [
82 { kind: AlchemyStepKind.insert, itemId: ItemId.thyme },
83 { kind: AlchemyStepKind.heat },
84 { kind: AlchemyStepKind.insert, itemId: ItemId.mint },
85 { kind: AlchemyStepKind.stir }
86 ]
87 });
88 assert.equal(evaluation.success, true);
89 assert.equal(evaluation.batch, 2);
90 assert.equal(evaluation.result.potionKey, "lesserHealthTonic");
91});
92
93test("same wrong steps hash to the same unstable potion", () =>
94{
95 const steps = [
96 { kind: AlchemyStepKind.heat },
97 { kind: AlchemyStepKind.stir },
98 { kind: AlchemyStepKind.cool }
99 ];
100 const a = unstablePotionFromSteps(steps);
101 const b = unstablePotionFromSteps(steps);
102 assert.equal(a.potionKey, b.potionKey);
103 assert.equal(a.stepsHash, hashAlchemySteps(steps));
104 assert.ok(["stamina-drain", "glow", "shrink", "blind"].includes(a.effectKey));
105});
106
107test("cauldron bottles lesser health tonic when steps and ratios match", () =>
108{
109 const harness = makeSimulation({ workerCount: 1 });
110 try
111 {
112 placeCauldron(harness.worldDb, 8, 8);
113 harness.simulation.reloadWorld();
114 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 9 });
115 const inventory = emptySlots();
116 inventory[0] = { itemId: ItemId.thyme, quantity: 2 };
117 inventory[1] = { itemId: ItemId.mint, quantity: 2 };
118 inventory[2] = { itemId: ItemId.glassBottle, quantity: 2 };
119 inventory[3] = { itemId: ItemId.paper, quantity: 1 };
120 harness.stateDb.saveBag(character.character_id, {
121 inventory,
122 equipment: emptyEquipment(),
123 vault: [],
124 vaultReserved: []
125 });
126 const entityId = harness.simulation.spawnCharacter(1, character);
127 const cid = character.character_id;
128 const alchemyBefore = harness.stateDb.getSkills(cid).xp.alchemy ?? 0;
129
130 harness.simulation.enqueue(1, { type: "open-cauldron", characterId: cid });
131 harness.simulation.step();
132
133 harness.simulation.enqueue(1, {
134 type: "cauldron-insert",
135 characterId: cid,
136 itemId: ItemId.thyme,
137 quantity: 2
138 });
139 harness.simulation.enqueue(1, {
140 type: "cauldron-action",
141 characterId: cid,
142 action: AlchemyStepKind.heat
143 });
144 harness.simulation.enqueue(1, {
145 type: "cauldron-insert",
146 characterId: cid,
147 itemId: ItemId.mint,
148 quantity: 2
149 });
150 harness.simulation.enqueue(1, {
151 type: "cauldron-action",
152 characterId: cid,
153 action: AlchemyStepKind.stir
154 });
155 harness.simulation.step();
156
157 // Soft claims only — ingredients stay in the bag until bottle.
158 let bag = harness.simulation.bagOf(entityId);
159 assert.equal(countItem(bag.inventory, ItemId.thyme), 2);
160 assert.equal(countItem(bag.inventory, ItemId.mint), 2);
161 assert.equal(countItem(bag.inventory, ItemId.glassBottle), 2);
162
163 harness.simulation.enqueue(1, { type: "cauldron-bottle", characterId: cid });
164 harness.simulation.step();
165
166 bag = harness.simulation.bagOf(entityId);
167 assert.equal(countItem(bag.inventory, ItemId.thyme), 0);
168 assert.equal(countItem(bag.inventory, ItemId.mint), 0);
169 assert.equal(countItem(bag.inventory, ItemId.glassBottle), 0);
170 assert.equal(countItem(bag.inventory, ItemId.potion), 2);
171 const potion = bag.inventory.find((slot) => slot?.itemId === ItemId.potion);
172 assert.equal(potion.instance?.potionKey, "lesserHealthTonic");
173 assert.equal(potion.instance?.unstable, false);
174 const alchemyAfter = harness.stateDb.getSkills(cid).xp.alchemy ?? 0;
175 // thyme×2 (tier 2) + mint×2 (tier 1), alchemy 2×
176 assert.equal(alchemyAfter - alchemyBefore, 12);
177
178 harness.simulation.enqueue(1, { type: "cauldron-save-recipe", characterId: cid });
179 harness.simulation.step();
180 const after = harness.simulation.bagOf(entityId);
181 assert.equal(countItem(after.inventory, ItemId.paper), 0);
182 assert.equal(countItem(after.inventory, ItemId.recipePaper), 1);
183 const recipe = after.inventory.find((slot) => slot?.itemId === ItemId.recipePaper);
184 assert.match(recipe.instance?.body ?? "", /Lesser health tonic/);
185 assert.match(recipe.instance?.body ?? "", /Alchemy level required/);
186 assert.equal(recipe.instance?.displayName, "Lesser health tonic recipe");
187 assert.equal(recipe.instance?.authorDisplayName, `${character.first_name} ${character.last_name}`);
188 }
189 finally
190 { harness.close(); }
191});
192
193test("cauldron clear / close after insert does not consume ingredients", () =>
194{
195 const harness = makeSimulation({ workerCount: 1 });
196 try
197 {
198 placeCauldron(harness.worldDb, 8, 8);
199 harness.simulation.reloadWorld();
200 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 9 });
201 const inventory = emptySlots();
202 inventory[0] = { itemId: ItemId.thyme, quantity: 2 };
203 inventory[1] = { itemId: ItemId.glassBottle, quantity: 1 };
204 harness.stateDb.saveBag(character.character_id, {
205 inventory,
206 equipment: emptyEquipment(),
207 vault: [],
208 vaultReserved: []
209 });
210 const entityId = harness.simulation.spawnCharacter(1, character);
211 const cid = character.character_id;
212
213 harness.simulation.enqueue(1, { type: "open-cauldron", characterId: cid });
214 harness.simulation.step();
215 harness.simulation.enqueue(1, {
216 type: "cauldron-insert",
217 characterId: cid,
218 itemId: ItemId.thyme,
219 quantity: 2
220 });
221 harness.simulation.step();
222 assert.equal(countItem(harness.simulation.bagOf(entityId).inventory, ItemId.thyme), 2);
223
224 harness.simulation.enqueue(1, { type: "cauldron-clear", characterId: cid });
225 harness.simulation.step();
226 assert.equal(countItem(harness.simulation.bagOf(entityId).inventory, ItemId.thyme), 2);
227
228 harness.simulation.enqueue(1, {
229 type: "cauldron-insert",
230 characterId: cid,
231 itemId: ItemId.thyme,
232 quantity: 2
233 });
234 harness.simulation.step();
235 harness.simulation.enqueue(1, { type: "cauldron-close", characterId: cid });
236 harness.simulation.step();
237 assert.equal(countItem(harness.simulation.bagOf(entityId).inventory, ItemId.thyme), 2);
238 }
239 finally
240 { harness.close(); }
241});
242
243test("cauldron rejects a fifth distinct ingredient type", () =>
244{
245 const harness = makeSimulation({ workerCount: 1 });
246 try
247 {
248 placeCauldron(harness.worldDb, 8, 8);
249 harness.simulation.reloadWorld();
250 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 9 });
251 const inventory = emptySlots();
252 inventory[0] = { itemId: ItemId.thyme, quantity: 1 };
253 inventory[1] = { itemId: ItemId.mint, quantity: 1 };
254 inventory[2] = { itemId: ItemId.lavender, quantity: 1 };
255 inventory[3] = { itemId: ItemId.nettle, quantity: 1 };
256 inventory[4] = { itemId: ItemId.rosemary, quantity: 1 };
257 inventory[5] = { itemId: ItemId.glassBottle, quantity: 1 };
258 harness.stateDb.saveBag(character.character_id, {
259 inventory,
260 equipment: emptyEquipment(),
261 vault: [],
262 vaultReserved: []
263 });
264 harness.simulation.spawnCharacter(1, character);
265 const cid = character.character_id;
266
267 harness.simulation.enqueue(1, { type: "open-cauldron", characterId: cid });
268 harness.simulation.step();
269 for (const itemId of [ItemId.thyme, ItemId.mint, ItemId.lavender, ItemId.nettle])
270 {
271 harness.simulation.enqueue(1, {
272 type: "cauldron-insert",
273 characterId: cid,
274 itemId,
275 quantity: 1
276 });
277 }
278 const four = harness.simulation.step();
279 assert.equal(four.events.some((event) => event.type === "command-error"), false, JSON.stringify(four.events));
280
281 harness.simulation.enqueue(1, {
282 type: "cauldron-insert",
283 characterId: cid,
284 itemId: ItemId.rosemary,
285 quantity: 1
286 });
287 const fifth = harness.simulation.step();
288 const error = fifth.events.find((event) => event.type === "command-error");
289 assert.ok(error, JSON.stringify(fifth.events));
290 assert.match(error.message, /at most 4/i);
291 }
292 finally
293 { harness.close(); }
294});
295
296test("drinking a lesser health tonic applies health regen", () =>
297{
298 const harness = makeSimulation({ workerCount: 1 });
299 try
300 {
301 const character = makeCharacter(harness.stateDb);
302 const inventory = emptySlots();
303 const instance = potionItemInstance({
304 potionKey: "lesserHealthTonic",
305 displayName: "Lesser health tonic",
306 unstable: false,
307 color: { r: 0.35, g: 0.78, b: 0.42 },
308 fillLevel: 4,
309 effect: { type: "health-regen", amountPerSecond: 1, durationMs: 10_000 },
310 effectKey: "health-regen"
311 });
312 const inserted = insertStack(
313 inventory,
314 { itemId: ItemId.potion, quantity: 1, instance },
315 itemDefinition(ItemId.potion)
316 );
317 harness.stateDb.saveBag(character.character_id, {
318 inventory: inserted.slots,
319 equipment: emptyEquipment(),
320 vault: [],
321 vaultReserved: []
322 });
323 const entityId = harness.simulation.spawnCharacter(1, character);
324 const record = harness.simulation.directory.get(entityId);
325 const worker = harness.simulation.workers[record.workerId];
326 const handle = worker.world.ecs.handleOf(entityId);
327 const health = worker.world.ecs.get(handle, Component.health);
328 health.current = 4;
329
330 const slotIndex = inserted.slots.findIndex((slot) => slot?.itemId === ItemId.potion);
331 harness.simulation.enqueue(1, { type: "drink-item", characterId: character.character_id, slotIndex });
332 const drink = harness.simulation.step();
333 assert.equal(drink.events.some((event) => event.type === "command-error"), false, JSON.stringify(drink.events));
334 assert.ok(drink.events.some((event) => event.type === "status-effects"));
335 const status = worker.world.ecs.get(handle, Component.statusEffects);
336 assert.ok(status?.effects?.some((entry) => entry.type === "health-regen"));
337
338 stepTimes(harness.simulation, 25);
339 assert.ok(Number(worker.world.ecs.get(handle, Component.health)?.current) > 4);
340 }
341 finally
342 { harness.close(); }
343});
344
345test("creating dye awards 2 alchemy XP per flower and caps a slot at 16", () =>
346{
347 const harness = makeSimulation({ workerCount: 1 });
348 try
349 {
350 placeCauldron(harness.worldDb, 8, 8);
351 harness.simulation.reloadWorld();
352 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 9 });
353 const inventory = emptySlots();
354 const instance = flowerItemInstance("red");
355 const filled = insertStack(
356 inventory,
357 { itemId: ItemId.flower, quantity: 16, instance },
358 itemDefinition(ItemId.flower)
359 );
360 filled.slots[1] = { itemId: ItemId.glassBottle, quantity: 16 };
361 harness.stateDb.saveBag(character.character_id, {
362 inventory: filled.slots,
363 equipment: emptyEquipment(),
364 vault: [],
365 vaultReserved: []
366 });
367 const entityId = harness.simulation.spawnCharacter(1, character);
368 const cid = character.character_id;
369 const alchemyBefore = harness.stateDb.getSkills(cid).xp.alchemy ?? 0;
370
371 harness.simulation.enqueue(1, { type: "open-cauldron", characterId: cid });
372 harness.simulation.step();
373 harness.simulation.enqueue(1, {
374 type: "cauldron-create-dye",
375 characterId: cid,
376 flowers: [{ itemId: ItemId.flower, quantity: 99, instance }]
377 });
378 const made = harness.simulation.step();
379 assert.equal(made.events.some((event) => event.type === "command-error"), false, JSON.stringify(made.events));
380 const alchemyAfter = harness.stateDb.getSkills(cid).xp.alchemy ?? 0;
381 assert.equal(alchemyAfter - alchemyBefore, 32);
382 const bag = harness.simulation.bagOf(entityId);
383 assert.equal(countItem(bag.inventory, ItemId.flower), 0);
384 assert.equal(countItem(bag.inventory, ItemId.dye), 16);
385 }
386 finally
387 { harness.close(); }
388});
389
390test("create dye with fewer bottles than flowers returns dyeConfirm on inventory", () =>
391{
392 const harness = makeSimulation({ workerCount: 1 });
393 try
394 {
395 placeCauldron(harness.worldDb, 8, 8);
396 harness.simulation.reloadWorld();
397 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 9 });
398 const yellow = flowerItemInstance("yellow");
399 const magenta = flowerItemInstance("magenta");
400 let slots = emptySlots();
401 slots = insertStack(
402 slots,
403 { itemId: ItemId.flower, quantity: 1, instance: yellow },
404 itemDefinition(ItemId.flower)
405 ).slots;
406 slots = insertStack(
407 slots,
408 { itemId: ItemId.flower, quantity: 1, instance: magenta },
409 itemDefinition(ItemId.flower)
410 ).slots;
411 slots = insertStack(
412 slots,
413 { itemId: ItemId.glassBottle, quantity: 1 },
414 itemDefinition(ItemId.glassBottle)
415 ).slots;
416 harness.stateDb.saveBag(character.character_id, {
417 inventory: slots,
418 equipment: emptyEquipment(),
419 vault: [],
420 vaultReserved: []
421 });
422 const entityId = harness.simulation.spawnCharacter(1, character);
423 const cid = character.character_id;
424
425 harness.simulation.enqueue(1, { type: "open-cauldron", characterId: cid });
426 harness.simulation.step();
427 harness.simulation.enqueue(1, {
428 type: "cauldron-create-dye",
429 characterId: cid,
430 confirmOverflow: false,
431 flowers: [
432 { itemId: ItemId.flower, quantity: 1, instance: yellow },
433 { itemId: ItemId.flower, quantity: 1, instance: magenta }
434 ]
435 });
436 const made = harness.simulation.step();
437 const inv = made.events.find((event) => event.type === "inventory" && event.dyeConfirm);
438 assert.ok(inv, JSON.stringify(made.events));
439 assert.deepEqual(inv.dyeConfirm, { wanted: 2, bottles: 1, lost: 1 });
440 const bag = harness.simulation.bagOf(entityId);
441 assert.equal(countItem(bag.inventory, ItemId.flower), 2);
442 assert.equal(countItem(bag.inventory, ItemId.dye), 0);
443 assert.equal(countItem(bag.inventory, ItemId.glassBottle), 1);
444 }
445 finally
446 { harness.close(); }
447});