BlueGit

timfa questward

Files

questward / server / tests / alchemy.test.js

alchemy.test.js

alchemy.test.js 5.2 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} from "../../shared/alchemy/recipes.js";
12import { emptyEquipment, emptySlots } from "../../shared/items/inventory.js";
13import { createEmptyChunkData, encodeChunkBlob } from "../src/world/chunkData.js";
14import {
15 makeCharacter,
16 makeSimulation
17} from "./helpers/simulationHarness.js";
18
19function placeCauldron (worldDb, tileX, tileY)
20{
21 const chunkX = Math.floor(tileX / 16);
22 const chunkY = Math.floor(tileY / 16);
23 const data = createEmptyChunkData();
24 data.open = true;
25 const localX = tileX - chunkX * 16;
26 const localY = tileY - chunkY * 16;
27 data.blocks[localY * 16 + localX] = { typeId: BlockType.cauldron, state: 0 };
28 worldDb.saveChunkVersion(
29 chunkX,
30 chunkY,
31 worldDb.getActiveRevisionId(),
32 true,
33 encodeChunkBlob(data)
34 );
35}
36
37function countItem (slots, itemId)
38{
39 return (slots ?? [])
40 .filter((slot) => slot?.itemId === itemId)
41 .reduce((sum, slot) => sum + (slot.quantity | 0), 0);
42}
43
44test("lesser health tonic ratios and steps evaluate to a success batch", () =>
45{
46 const potion = resolvedAlchemyPotions().find((entry) => entry.potionKey === "lesserHealthTonic");
47 assert.ok(potion);
48 const evaluation = evaluateAlchemyBottle({
49 skills: { levels: { alchemy: 1 } },
50 brewStacks: [
51 { itemId: ItemId.thyme, quantity: 2 },
52 { itemId: ItemId.mint, quantity: 2 }
53 ],
54 takenSteps: [
55 { kind: AlchemyStepKind.insert, itemId: ItemId.thyme },
56 { kind: AlchemyStepKind.heat },
57 { kind: AlchemyStepKind.insert, itemId: ItemId.mint },
58 { kind: AlchemyStepKind.stir }
59 ]
60 });
61 assert.equal(evaluation.success, true);
62 assert.equal(evaluation.batch, 2);
63 assert.equal(evaluation.result.potionKey, "lesserHealthTonic");
64});
65
66test("same wrong steps hash to the same unstable potion", () =>
67{
68 const steps = [
69 { kind: AlchemyStepKind.heat },
70 { kind: AlchemyStepKind.stir },
71 { kind: AlchemyStepKind.cool }
72 ];
73 const a = unstablePotionFromSteps(steps);
74 const b = unstablePotionFromSteps(steps);
75 assert.equal(a.potionKey, b.potionKey);
76 assert.equal(a.stepsHash, hashAlchemySteps(steps));
77 assert.ok(["stamina-drain", "glow", "shrink", "blind"].includes(a.effectKey));
78});
79
80test("cauldron bottles lesser health tonic when steps and ratios match", () =>
81{
82 const harness = makeSimulation({ workerCount: 1 });
83 try
84 {
85 placeCauldron(harness.worldDb, 8, 8);
86 harness.simulation.reloadWorld();
87 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 9 });
88 const inventory = emptySlots();
89 inventory[0] = { itemId: ItemId.thyme, quantity: 2 };
90 inventory[1] = { itemId: ItemId.mint, quantity: 2 };
91 inventory[2] = { itemId: ItemId.glassBottle, quantity: 2 };
92 inventory[3] = { itemId: ItemId.paper, quantity: 1 };
93 harness.stateDb.saveBag(character.character_id, {
94 inventory,
95 equipment: emptyEquipment(),
96 vault: [],
97 vaultReserved: []
98 });
99 const entityId = harness.simulation.spawnCharacter(1, character);
100 const cid = character.character_id;
101
102 harness.simulation.enqueue(1, { type: "open-cauldron", characterId: cid });
103 harness.simulation.step();
104
105 harness.simulation.enqueue(1, {
106 type: "cauldron-set-slot",
107 characterId: cid,
108 slotIndex: 0,
109 itemId: ItemId.thyme,
110 quantity: 2
111 });
112 harness.simulation.enqueue(1, {
113 type: "cauldron-set-slot",
114 characterId: cid,
115 slotIndex: 1,
116 itemId: ItemId.mint,
117 quantity: 2
118 });
119 harness.simulation.step();
120
121 harness.simulation.enqueue(1, { type: "cauldron-insert", characterId: cid, slotIndex: 0 });
122 harness.simulation.enqueue(1, {
123 type: "cauldron-action",
124 characterId: cid,
125 action: AlchemyStepKind.heat
126 });
127 harness.simulation.enqueue(1, { type: "cauldron-insert", characterId: cid, slotIndex: 1 });
128 harness.simulation.enqueue(1, {
129 type: "cauldron-action",
130 characterId: cid,
131 action: AlchemyStepKind.stir
132 });
133 harness.simulation.enqueue(1, { type: "cauldron-bottle", characterId: cid });
134 harness.simulation.step();
135
136 const bag = harness.simulation.bagOf(entityId);
137 assert.equal(countItem(bag.inventory, ItemId.thyme), 0);
138 assert.equal(countItem(bag.inventory, ItemId.mint), 0);
139 assert.equal(countItem(bag.inventory, ItemId.glassBottle), 0);
140 assert.equal(countItem(bag.inventory, ItemId.potion), 2);
141 const potion = bag.inventory.find((slot) => slot?.itemId === ItemId.potion);
142 assert.equal(potion.instance?.potionKey, "lesserHealthTonic");
143 assert.equal(potion.instance?.unstable, false);
144
145 harness.simulation.enqueue(1, { type: "cauldron-save-recipe", characterId: cid });
146 harness.simulation.step();
147 const after = harness.simulation.bagOf(entityId);
148 assert.equal(countItem(after.inventory, ItemId.paper), 0);
149 assert.equal(countItem(after.inventory, ItemId.recipePaper), 1);
150 const recipe = after.inventory.find((slot) => slot?.itemId === ItemId.recipePaper);
151 assert.match(recipe.instance?.body ?? "", /Lesser health tonic/);
152 assert.match(recipe.instance?.body ?? "", /Alchemy level required/);
153 }
154 finally
155 { harness.close(); }
156});