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("green draught ratios and steps evaluate to a success batch", () =>
45{
46 const potion = resolvedAlchemyPotions().find((entry) => entry.potionKey === "greenDraught");
47 assert.ok(potion);
48 const evaluation = evaluateAlchemyBottle({
49 skills: { levels: { alchemy: 1 } },
50 brewStacks: [
51 { itemId: ItemId.greenHerb, quantity: 4 },
52 { itemId: ItemId.apple, quantity: 2 }
53 ],
54 takenSteps: [
55 { kind: AlchemyStepKind.insert, itemId: ItemId.greenHerb },
56 { kind: AlchemyStepKind.heat },
57 { kind: AlchemyStepKind.stir },
58 { kind: AlchemyStepKind.insert, itemId: ItemId.apple },
59 { kind: AlchemyStepKind.cool }
60 ]
61 });
62 assert.equal(evaluation.success, true);
63 assert.equal(evaluation.batch, 2);
64 assert.equal(evaluation.result.potionKey, "greenDraught");
65});
66
67test("same wrong steps hash to the same unstable potion", () =>
68{
69 const steps = [
70 { kind: AlchemyStepKind.heat },
71 { kind: AlchemyStepKind.stir },
72 { kind: AlchemyStepKind.cool }
73 ];
74 const a = unstablePotionFromSteps(steps);
75 const b = unstablePotionFromSteps(steps);
76 assert.equal(a.potionKey, b.potionKey);
77 assert.equal(a.stepsHash, hashAlchemySteps(steps));
78});
79
80test("cauldron bottles green draught 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.greenHerb, quantity: 4 };
90 inventory[1] = { itemId: ItemId.apple, 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.greenHerb,
110 quantity: 4
111 });
112 harness.simulation.enqueue(1, {
113 type: "cauldron-set-slot",
114 characterId: cid,
115 slotIndex: 1,
116 itemId: ItemId.apple,
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, {
128 type: "cauldron-action",
129 characterId: cid,
130 action: AlchemyStepKind.stir
131 });
132 harness.simulation.enqueue(1, { type: "cauldron-insert", characterId: cid, slotIndex: 1 });
133 harness.simulation.enqueue(1, {
134 type: "cauldron-action",
135 characterId: cid,
136 action: AlchemyStepKind.cool
137 });
138 harness.simulation.enqueue(1, { type: "cauldron-bottle", characterId: cid });
139 harness.simulation.step();
140
141 const bag = harness.simulation.bagOf(entityId);
142 assert.equal(countItem(bag.inventory, ItemId.greenHerb), 0);
143 assert.equal(countItem(bag.inventory, ItemId.apple), 0);
144 assert.equal(countItem(bag.inventory, ItemId.glassBottle), 0);
145 assert.equal(countItem(bag.inventory, ItemId.potion), 2);
146 const potion = bag.inventory.find((slot) => slot?.itemId === ItemId.potion);
147 assert.equal(potion.instance?.potionKey, "greenDraught");
148 assert.equal(potion.instance?.unstable, false);
149
150 harness.simulation.enqueue(1, { type: "cauldron-save-recipe", characterId: cid });
151 harness.simulation.step();
152 const after = harness.simulation.bagOf(entityId);
153 assert.equal(countItem(after.inventory, ItemId.paper), 0);
154 assert.equal(countItem(after.inventory, ItemId.recipePaper), 1);
155 const recipe = after.inventory.find((slot) => slot?.itemId === ItemId.recipePaper);
156 assert.match(recipe.instance?.body ?? "", /Green draught/);
157 assert.match(recipe.instance?.body ?? "", /Alchemy level required/);
158 }
159 finally
160 { harness.close(); }
161});