BlueGit

timfa questward

Files

questward / server / tests / alchemy.test.js

alchemy.test.js

alchemy.test.js 7.3 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-set-slot",
111 characterId: cid,
112 slotIndex: 0,
113 itemId: ItemId.thyme,
114 quantity: 2
115 });
116 harness.simulation.enqueue(1, {
117 type: "cauldron-set-slot",
118 characterId: cid,
119 slotIndex: 1,
120 itemId: ItemId.mint,
121 quantity: 2
122 });
123 harness.simulation.step();
124
125 harness.simulation.enqueue(1, { type: "cauldron-insert", characterId: cid, slotIndex: 0 });
126 harness.simulation.enqueue(1, {
127 type: "cauldron-action",
128 characterId: cid,
129 action: AlchemyStepKind.heat
130 });
131 harness.simulation.enqueue(1, { type: "cauldron-insert", characterId: cid, slotIndex: 1 });
132 harness.simulation.enqueue(1, {
133 type: "cauldron-action",
134 characterId: cid,
135 action: AlchemyStepKind.stir
136 });
137 harness.simulation.enqueue(1, { type: "cauldron-bottle", characterId: cid });
138 harness.simulation.step();
139
140 const bag = harness.simulation.bagOf(entityId);
141 assert.equal(countItem(bag.inventory, ItemId.thyme), 0);
142 assert.equal(countItem(bag.inventory, ItemId.mint), 0);
143 assert.equal(countItem(bag.inventory, ItemId.glassBottle), 0);
144 assert.equal(countItem(bag.inventory, ItemId.potion), 2);
145 const potion = bag.inventory.find((slot) => slot?.itemId === ItemId.potion);
146 assert.equal(potion.instance?.potionKey, "lesserHealthTonic");
147 assert.equal(potion.instance?.unstable, false);
148
149 harness.simulation.enqueue(1, { type: "cauldron-save-recipe", characterId: cid });
150 harness.simulation.step();
151 const after = harness.simulation.bagOf(entityId);
152 assert.equal(countItem(after.inventory, ItemId.paper), 0);
153 assert.equal(countItem(after.inventory, ItemId.recipePaper), 1);
154 const recipe = after.inventory.find((slot) => slot?.itemId === ItemId.recipePaper);
155 assert.match(recipe.instance?.body ?? "", /Lesser health tonic/);
156 assert.match(recipe.instance?.body ?? "", /Alchemy level required/);
157 assert.equal(recipe.instance?.displayName, "Lesser health tonic Recipe");
158 assert.equal(recipe.instance?.authorDisplayName, `${character.first_name} ${character.last_name}`);
159 }
160 finally
161 { harness.close(); }
162});
163
164test("drinking a lesser health tonic applies health regen", () =>
165{
166 const harness = makeSimulation({ workerCount: 1 });
167 try
168 {
169 const character = makeCharacter(harness.stateDb);
170 const inventory = emptySlots();
171 const instance = potionItemInstance({
172 potionKey: "lesserHealthTonic",
173 displayName: "Lesser health tonic",
174 unstable: false,
175 color: { r: 0.35, g: 0.78, b: 0.42 },
176 fillLevel: 4,
177 effect: { type: "health-regen", amountPerSecond: 1, durationMs: 10_000 },
178 effectKey: "health-regen"
179 });
180 const inserted = insertStack(
181 inventory,
182 { itemId: ItemId.potion, quantity: 1, instance },
183 itemDefinition(ItemId.potion)
184 );
185 harness.stateDb.saveBag(character.character_id, {
186 inventory: inserted.slots,
187 equipment: emptyEquipment(),
188 vault: [],
189 vaultReserved: []
190 });
191 const entityId = harness.simulation.spawnCharacter(1, character);
192 const record = harness.simulation.directory.get(entityId);
193 const worker = harness.simulation.workers[record.workerId];
194 const handle = worker.world.ecs.handleOf(entityId);
195 const health = worker.world.ecs.get(handle, Component.health);
196 health.current = 5;
197
198 const slotIndex = inserted.slots.findIndex((slot) => slot?.itemId === ItemId.potion);
199 harness.simulation.enqueue(1, { type: "drink-item", characterId: character.character_id, slotIndex });
200 const drink = harness.simulation.step();
201 assert.equal(drink.events.some((event) => event.type === "command-error"), false, JSON.stringify(drink.events));
202 assert.ok(drink.events.some((event) => event.type === "status-effects"));
203 const status = worker.world.ecs.get(handle, Component.statusEffects);
204 assert.ok(status?.effects?.some((entry) => entry.type === "health-regen"));
205
206 stepTimes(harness.simulation, 25);
207 assert.ok(Number(worker.world.ecs.get(handle, Component.health)?.current) > 5);
208 }
209 finally
210 { harness.close(); }
211});