BlueGit

timfa questward

Files

questward / server / tests / alchemy.test.js

alchemy.test.js

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