BlueGit

timfa questward

Files

questward / server / tests / alchemy.test.js

alchemy.test.js

alchemy.test.js 9.0 KiB Download Raw
1import test from "node:test";
2import assert from "node:assert/strict";
3import { BlockType } from "../../shared/definitions/tiles.js";
4import { ItemId } from "../../shared/definitions/items.js";
5import { AlchemyStepKind } from "../../shared/definitions/alchemy.js";
6import {
7 evaluateAlchemyBottle,
8 hashAlchemySteps,
9 resolvedAlchemyPotions,
10 unstablePotionFromSteps,
11 potionItemInstance
12} from "../../shared/alchemy/recipes.js";
13import { emptyEquipment, emptySlots, insertStack } from "../../shared/items/inventory.js";
14import { itemDefinition } from "../../shared/definitions/items.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
106 harness.simulation.enqueue(1, { type: "open-cauldron", characterId: cid });
107 harness.simulation.step();
108
109 harness.simulation.enqueue(1, {
110 type: "cauldron-insert",
111 characterId: cid,
112 itemId: ItemId.thyme,
113 quantity: 2
114 });
115 harness.simulation.enqueue(1, {
116 type: "cauldron-action",
117 characterId: cid,
118 action: AlchemyStepKind.heat
119 });
120 harness.simulation.enqueue(1, {
121 type: "cauldron-insert",
122 characterId: cid,
123 itemId: ItemId.mint,
124 quantity: 2
125 });
126 harness.simulation.enqueue(1, {
127 type: "cauldron-action",
128 characterId: cid,
129 action: AlchemyStepKind.stir
130 });
131 harness.simulation.step();
132
133 // Soft claims only — ingredients stay in the bag until bottle.
134 let bag = harness.simulation.bagOf(entityId);
135 assert.equal(countItem(bag.inventory, ItemId.thyme), 2);
136 assert.equal(countItem(bag.inventory, ItemId.mint), 2);
137 assert.equal(countItem(bag.inventory, ItemId.glassBottle), 2);
138
139 harness.simulation.enqueue(1, { type: "cauldron-bottle", characterId: cid });
140 harness.simulation.step();
141
142 bag = harness.simulation.bagOf(entityId);
143 assert.equal(countItem(bag.inventory, ItemId.thyme), 0);
144 assert.equal(countItem(bag.inventory, ItemId.mint), 0);
145 assert.equal(countItem(bag.inventory, ItemId.glassBottle), 0);
146 assert.equal(countItem(bag.inventory, ItemId.potion), 2);
147 const potion = bag.inventory.find((slot) => slot?.itemId === ItemId.potion);
148 assert.equal(potion.instance?.potionKey, "lesserHealthTonic");
149 assert.equal(potion.instance?.unstable, false);
150
151 harness.simulation.enqueue(1, { type: "cauldron-save-recipe", characterId: cid });
152 harness.simulation.step();
153 const after = harness.simulation.bagOf(entityId);
154 assert.equal(countItem(after.inventory, ItemId.paper), 0);
155 assert.equal(countItem(after.inventory, ItemId.recipePaper), 1);
156 const recipe = after.inventory.find((slot) => slot?.itemId === ItemId.recipePaper);
157 assert.match(recipe.instance?.body ?? "", /Lesser health tonic/);
158 assert.match(recipe.instance?.body ?? "", /Alchemy level required/);
159 assert.equal(recipe.instance?.displayName, "Lesser health tonic Recipe");
160 assert.equal(recipe.instance?.authorDisplayName, `${character.first_name} ${character.last_name}`);
161 }
162 finally
163 { harness.close(); }
164});
165
166test("cauldron clear / close after insert does not consume ingredients", () =>
167{
168 const harness = makeSimulation({ workerCount: 1 });
169 try
170 {
171 placeCauldron(harness.worldDb, 8, 8);
172 harness.simulation.reloadWorld();
173 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 9 });
174 const inventory = emptySlots();
175 inventory[0] = { itemId: ItemId.thyme, quantity: 2 };
176 inventory[1] = { itemId: ItemId.glassBottle, quantity: 1 };
177 harness.stateDb.saveBag(character.character_id, {
178 inventory,
179 equipment: emptyEquipment(),
180 vault: [],
181 vaultReserved: []
182 });
183 const entityId = harness.simulation.spawnCharacter(1, character);
184 const cid = character.character_id;
185
186 harness.simulation.enqueue(1, { type: "open-cauldron", characterId: cid });
187 harness.simulation.step();
188 harness.simulation.enqueue(1, {
189 type: "cauldron-insert",
190 characterId: cid,
191 itemId: ItemId.thyme,
192 quantity: 2
193 });
194 harness.simulation.step();
195 assert.equal(countItem(harness.simulation.bagOf(entityId).inventory, ItemId.thyme), 2);
196
197 harness.simulation.enqueue(1, { type: "cauldron-clear", characterId: cid });
198 harness.simulation.step();
199 assert.equal(countItem(harness.simulation.bagOf(entityId).inventory, ItemId.thyme), 2);
200
201 harness.simulation.enqueue(1, {
202 type: "cauldron-insert",
203 characterId: cid,
204 itemId: ItemId.thyme,
205 quantity: 2
206 });
207 harness.simulation.step();
208 harness.simulation.enqueue(1, { type: "cauldron-close", characterId: cid });
209 harness.simulation.step();
210 assert.equal(countItem(harness.simulation.bagOf(entityId).inventory, ItemId.thyme), 2);
211 }
212 finally
213 { harness.close(); }
214});
215
216test("drinking a lesser health tonic applies health regen", () =>
217{
218 const harness = makeSimulation({ workerCount: 1 });
219 try
220 {
221 const character = makeCharacter(harness.stateDb);
222 const inventory = emptySlots();
223 const instance = potionItemInstance({
224 potionKey: "lesserHealthTonic",
225 displayName: "Lesser health tonic",
226 unstable: false,
227 color: { r: 0.35, g: 0.78, b: 0.42 },
228 fillLevel: 4,
229 effect: { type: "health-regen", amountPerSecond: 1, durationMs: 10_000 },
230 effectKey: "health-regen"
231 });
232 const inserted = insertStack(
233 inventory,
234 { itemId: ItemId.potion, quantity: 1, instance },
235 itemDefinition(ItemId.potion)
236 );
237 harness.stateDb.saveBag(character.character_id, {
238 inventory: inserted.slots,
239 equipment: emptyEquipment(),
240 vault: [],
241 vaultReserved: []
242 });
243 const entityId = harness.simulation.spawnCharacter(1, character);
244 const record = harness.simulation.directory.get(entityId);
245 const worker = harness.simulation.workers[record.workerId];
246 const handle = worker.world.ecs.handleOf(entityId);
247 const health = worker.world.ecs.get(handle, Component.health);
248 health.current = 5;
249
250 const slotIndex = inserted.slots.findIndex((slot) => slot?.itemId === ItemId.potion);
251 harness.simulation.enqueue(1, { type: "drink-item", characterId: character.character_id, slotIndex });
252 const drink = harness.simulation.step();
253 assert.equal(drink.events.some((event) => event.type === "command-error"), false, JSON.stringify(drink.events));
254 assert.ok(drink.events.some((event) => event.type === "status-effects"));
255 const status = worker.world.ecs.get(handle, Component.statusEffects);
256 assert.ok(status?.effects?.some((entry) => entry.type === "health-regen"));
257
258 stepTimes(harness.simulation, 25);
259 assert.ok(Number(worker.world.ecs.get(handle, Component.health)?.current) > 5);
260 }
261 finally
262 { harness.close(); }
263});