BlueGit

timfa questward

Files

questward / server / tests / herbalism.test.js

herbalism.test.js

herbalism.test.js 4.8 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 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.bronzePlantClippers, 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 skills.xp[SkillId.herbalism] = totalXpForLevel(5);
60
61 harness.simulation.enqueue(1, {
62 type: "pick",
63 characterId: character.character_id,
64 tileX: 8,
65 tileY: 8
66 });
67 harness.simulation.step();
68 harness.advance(4000);
69 harness.simulation.step();
70 assert.equal(countItem(harness.simulation.bagOf(entityId).inventory, ItemId.sage), 1);
71 }
72 finally
73 { harness.close(); }
74});
75
76test("aconite herb plant rejects low herbalism", () =>
77{
78 const harness = makeSimulation({ workerCount: 1 });
79 try
80 {
81 placeBlock(harness.worldDb, 8, 8, BlockType.herbPlant, HerbId.aconite);
82 harness.simulation.reloadWorld();
83 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 9 });
84 const entityId = harness.simulation.spawnCharacter(1, character);
85 const bag = harness.simulation.bagOf(entityId);
86 bag.inventory = emptySlots();
87 bag.inventory[0] = { itemId: ItemId.bronzePlantClippers, quantity: 1 };
88 bag.equipment = emptyEquipment();
89 harness.simulation.applyBag(entityId, bag);
90
91 harness.simulation.enqueue(1, {
92 type: "pick",
93 characterId: character.character_id,
94 tileX: 8,
95 tileY: 8
96 });
97 const result = harness.simulation.step();
98 const error = result.events.find((event) => event.type === "command-error");
99 assert.ok(error);
100 assert.match(error.message, /Herbalism 45/i);
101 }
102 finally
103 { harness.close(); }
104});
105
106test("starter potions evaluate with herb recipes", () =>
107{
108 const tonic = resolvedAlchemyPotions().find((entry) => entry.potionKey === "lesserHealthTonic");
109 assert.ok(tonic);
110 const evaluation = evaluateAlchemyBottle({
111 skills: { levels: { alchemy: 1 } },
112 brewStacks: [
113 { itemId: ItemId.thyme, quantity: 1 },
114 { itemId: ItemId.mint, quantity: 1 }
115 ],
116 takenSteps: [
117 { kind: AlchemyStepKind.insert, itemId: ItemId.thyme },
118 { kind: AlchemyStepKind.heat },
119 { kind: AlchemyStepKind.insert, itemId: ItemId.mint },
120 { kind: AlchemyStepKind.stir }
121 ]
122 });
123 assert.equal(evaluation.success, true);
124 assert.equal(evaluation.result.potionKey, "lesserHealthTonic");
125 assert.equal(evaluation.result.effect.type, "health-regen");
126
127 const strength = evaluateAlchemyBottle({
128 skills: { levels: { alchemy: 1 } },
129 brewStacks: [
130 { itemId: ItemId.thyme, quantity: 3 },
131 { itemId: ItemId.nettle, quantity: 2 }
132 ],
133 takenSteps: [
134 { kind: AlchemyStepKind.insert, itemId: ItemId.thyme },
135 { kind: AlchemyStepKind.stir },
136 { kind: AlchemyStepKind.insert, itemId: ItemId.nettle },
137 { kind: AlchemyStepKind.heat }
138 ]
139 });
140 assert.equal(strength.success, true);
141 assert.equal(strength.result.potionKey, "strengthPotion");
142});