BlueGit

timfa questward

Files

questward / server / tests / alchemy.test.js

alchemy.test.js

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