BlueGit

timfa questward

Files

questward / server / tests / herbalism.test.js

herbalism.test.js

herbalism.test.js 6.1 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 { HerbId } from "../../shared/definitions/herbs.js";
6import { SkillId } from "../../shared/definitions/skills.js";
7import { AlchemyStepKind } from "../../shared/definitions/alchemy.js";
8import { evaluateAlchemyBottle, resolvedAlchemyPotions } from "../../shared/alchemy/recipes.js";
9import { emptyEquipment, emptySlots } from "../../shared/items/inventory.js";
10import { totalXpForLevel } from "../../shared/skills/progression.js";
11import { Component } from "../src/ecs/components.js";
12import { createEmptyChunkData, encodeChunkBlob } from "../src/world/chunkData.js";
13import { makeCharacter, makeSimulation } from "./helpers/simulationHarness.js";
14
15function placeBlock (worldDb, tileX, tileY, typeId, state = 0)
16{
17 const chunkX = Math.floor(tileX / 16);
18 const chunkY = Math.floor(tileY / 16);
19 const data = createEmptyChunkData();
20 data.open = true;
21 const localX = tileX - chunkX * 16;
22 const localY = tileY - chunkY * 16;
23 data.blocks[localY * 16 + localX] = { typeId, state };
24 worldDb.saveChunkVersion(
25 chunkX,
26 chunkY,
27 worldDb.getActiveRevisionId(),
28 true,
29 encodeChunkBlob(data)
30 );
31}
32
33function countItem (slots, itemId)
34{
35 return (slots ?? [])
36 .filter((slot) => slot?.itemId === itemId)
37 .reduce((sum, slot) => sum + (slot.quantity | 0), 0);
38}
39
40test("sage herb plant yields sage with matching-tier clippers", () =>
41{
42 const harness = makeSimulation({ workerCount: 1 });
43 try
44 {
45 placeBlock(harness.worldDb, 8, 8, BlockType.herbPlant, HerbId.sage);
46 harness.simulation.reloadWorld();
47 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 9 });
48 const entityId = harness.simulation.spawnCharacter(1, character);
49 const bag = harness.simulation.bagOf(entityId);
50 bag.inventory = emptySlots();
51 bag.inventory[0] = { itemId: ItemId.ironPlantClippers, quantity: 1 };
52 bag.equipment = emptyEquipment();
53 harness.simulation.applyBag(entityId, bag);
54
55 const record = harness.simulation.entityRecord(entityId);
56 const worker = harness.simulation.workers[record.workerId];
57 const handle = worker.world.ecs.handleOf(entityId);
58 const skills = worker.world.ecs.get(handle, Component.skills);
59 /** Iron clippers need herbalism 10 to equip (sage gather gate is only 5). */
60 skills.xp[SkillId.herbalism] = totalXpForLevel(10);
61
62 harness.simulation.enqueue(1, {
63 type: "pick",
64 characterId: character.character_id,
65 tileX: 8,
66 tileY: 8
67 });
68 harness.simulation.step();
69 harness.advance(4000);
70 harness.simulation.step();
71 assert.equal(countItem(harness.simulation.bagOf(entityId).inventory, ItemId.sage), 1);
72 }
73 finally
74 { harness.close(); }
75});
76
77test("sage herb plant rejects under-tier clippers", () =>
78{
79 const harness = makeSimulation({ workerCount: 1 });
80 try
81 {
82 placeBlock(harness.worldDb, 8, 8, BlockType.herbPlant, HerbId.sage);
83 harness.simulation.reloadWorld();
84 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 9 });
85 const entityId = harness.simulation.spawnCharacter(1, character);
86 const bag = harness.simulation.bagOf(entityId);
87 bag.inventory = emptySlots();
88 bag.inventory[0] = { itemId: ItemId.bronzePlantClippers, quantity: 1 };
89 bag.equipment = emptyEquipment();
90 harness.simulation.applyBag(entityId, bag);
91
92 const record = harness.simulation.entityRecord(entityId);
93 const worker = harness.simulation.workers[record.workerId];
94 const handle = worker.world.ecs.handleOf(entityId);
95 const skills = worker.world.ecs.get(handle, Component.skills);
96 skills.xp[SkillId.herbalism] = totalXpForLevel(10);
97
98 harness.simulation.enqueue(1, {
99 type: "pick",
100 characterId: character.character_id,
101 tileX: 8,
102 tileY: 8
103 });
104 const result = harness.simulation.step();
105 const error = result.events.find((event) => event.type === "command-error");
106 assert.ok(error);
107 assert.match(error.message, /Iron plant clippers/i);
108 }
109 finally
110 { harness.close(); }
111});
112
113test("aconite herb plant rejects low herbalism", () =>
114{
115 const harness = makeSimulation({ workerCount: 1 });
116 try
117 {
118 placeBlock(harness.worldDb, 8, 8, BlockType.herbPlant, HerbId.aconite);
119 harness.simulation.reloadWorld();
120 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 9 });
121 const entityId = harness.simulation.spawnCharacter(1, character);
122 const bag = harness.simulation.bagOf(entityId);
123 bag.inventory = emptySlots();
124 bag.inventory[0] = { itemId: ItemId.bronzePlantClippers, quantity: 1 };
125 bag.equipment = emptyEquipment();
126 harness.simulation.applyBag(entityId, bag);
127
128 harness.simulation.enqueue(1, {
129 type: "pick",
130 characterId: character.character_id,
131 tileX: 8,
132 tileY: 8
133 });
134 const result = harness.simulation.step();
135 const error = result.events.find((event) => event.type === "command-error");
136 assert.ok(error);
137 assert.match(error.message, /Herbalism 45/i);
138 }
139 finally
140 { harness.close(); }
141});
142
143test("starter potions evaluate with herb recipes", () =>
144{
145 const tonic = resolvedAlchemyPotions().find((entry) => entry.potionKey === "lesserHealthTonic");
146 assert.ok(tonic);
147 const evaluation = evaluateAlchemyBottle({
148 skills: { levels: { alchemy: 1 } },
149 brewStacks: [
150 { itemId: ItemId.thyme, quantity: 1 },
151 { itemId: ItemId.mint, quantity: 1 }
152 ],
153 takenSteps: [
154 { kind: AlchemyStepKind.insert, itemId: ItemId.thyme },
155 { kind: AlchemyStepKind.heat },
156 { kind: AlchemyStepKind.insert, itemId: ItemId.mint },
157 { kind: AlchemyStepKind.stir }
158 ]
159 });
160 assert.equal(evaluation.success, true);
161 assert.equal(evaluation.result.potionKey, "lesserHealthTonic");
162 assert.equal(evaluation.result.effect.type, "health-regen");
163
164 const strength = evaluateAlchemyBottle({
165 skills: { levels: { alchemy: 1 } },
166 brewStacks: [
167 { itemId: ItemId.thyme, quantity: 3 },
168 { itemId: ItemId.nettle, quantity: 2 }
169 ],
170 takenSteps: [
171 { kind: AlchemyStepKind.insert, itemId: ItemId.thyme },
172 { kind: AlchemyStepKind.stir },
173 { kind: AlchemyStepKind.insert, itemId: ItemId.nettle },
174 { kind: AlchemyStepKind.heat }
175 ]
176 });
177 assert.equal(strength.success, true);
178 assert.equal(strength.result.potionKey, "strengthPotion");
179});