BlueGit

timfa questward

Files

questward / server / tests / crafting.test.js

crafting.test.js

crafting.test.js 12.0 KiB Download Raw
1import test from "node:test";
2import assert from "node:assert/strict";
3import { BlockType, blockDefinition } from "../../shared/definitions/tiles.js";
4import { CraftingStationId, CRAFT_DURATION_MS } from "../../shared/definitions/craftingStations.js";
5import { ItemId } from "../../shared/definitions/items.js";
6import { SkillId } from "../../shared/definitions/skills.js";
7import {
8 MaterialTier,
9 GatherMaterialId,
10 tierSmeltSkillLevel,
11 smeltingDurationMs,
12 associatedBarTier
13} from "../../shared/definitions/materialTiers.js";
14import { craftDurationMs } from "../../shared/definitions/gathering.js";
15import { recipesForStation, recipeDefinition } from "../../shared/crafting/stationRecipes.js";
16import { emptyEquipment, emptySlots } from "../../shared/items/inventory.js";
17import { totalXpForLevel } from "../../shared/skills/progression.js";
18import { makeCharacter, makeSimulation } from "./helpers/simulationHarness.js";
19import { Component } from "../src/ecs/components.js";
20
21function countItem (slots, itemId)
22{
23 return (slots ?? []).reduce((sum, slot) =>
24 {
25 if (slot?.itemId === itemId)
26 { return sum + (slot.quantity | 0); }
27 return sum;
28 }, 0);
29}
30
31function placeFurnace (worker, tileX, tileY)
32{
33 const data = worker.world.chunkData(Math.floor(tileX / 16), Math.floor(tileY / 16));
34 const localX = tileX % 16;
35 const localY = tileY % 16;
36 data.blocks[localY * 16 + localX] = {
37 typeId: BlockType.furnace,
38 state: "furnace",
39 config: null
40 };
41}
42
43function setSkillLevel (worker, playerId, skillId, level)
44{
45 const handle = worker.world.ecs.handleOf(playerId);
46 const skills = worker.world.ecs.get(handle, Component.skills);
47 skills.xp[skillId] = totalXpForLevel(level);
48}
49
50function giveInventory (harness, playerId, items)
51{
52 const inventory = emptySlots();
53 items.forEach((stack, index) =>
54 {
55 inventory[index] = { itemId: stack.itemId, quantity: stack.quantity };
56 });
57 harness.simulation.applyBag(playerId, {
58 inventory,
59 equipment: emptyEquipment(),
60 vault: [],
61 vaultReserved: []
62 });
63}
64
65function advanceMs (harness, ms)
66{
67 const ticks = Math.ceil(ms / 50) + 3;
68 for (let i = 0; i < ticks; i++)
69 {
70 harness.advance(50);
71 harness.simulation.step();
72 }
73}
74
75test("furnace recipes: bronze is tin+copper; steel is iron+coal", () =>
76{
77 assert.equal(blockDefinition(BlockType.furnace)?.key, "furnace");
78 const bronze = recipeDefinition("smelt-bronze");
79 assert.deepEqual(bronze.inputs.map((row) => row.itemId).sort(), [
80 ItemId.copperOre,
81 ItemId.tinOre
82 ].sort());
83 assert.equal(bronze.output.itemId, ItemId.bronzeBar);
84 assert.equal(bronze.minSkillLevel, tierSmeltSkillLevel(MaterialTier.bronze));
85
86 const steel = recipesForStation(CraftingStationId.furnace)
87 .find((recipe) => recipe.recipeId === "smelt-steel");
88 assert.ok(steel);
89 assert.deepEqual(steel.inputs.map((row) => row.itemId).sort(), [
90 ItemId.coal,
91 ItemId.ironOre
92 ].sort());
93 assert.equal(steel.output.itemId, ItemId.steelBar);
94});
95
96test("tin copper coal map to associated bar tiers for smelt duration", () =>
97{
98 assert.equal(associatedBarTier(GatherMaterialId.tin), MaterialTier.bronze);
99 assert.equal(associatedBarTier(GatherMaterialId.copper), MaterialTier.bronze);
100 assert.equal(associatedBarTier(GatherMaterialId.coal), MaterialTier.steel);
101 assert.equal(smeltingDurationMs(MaterialTier.bronze), 2000);
102 assert.equal(smeltingDurationMs(GatherMaterialId.tin), 2000);
103 assert.equal(smeltingDurationMs(MaterialTier.steel), 6000);
104 assert.equal(smeltingDurationMs(GatherMaterialId.coal), 6000);
105 assert.equal(craftDurationMs(recipeDefinition("smelt-bronze"), CRAFT_DURATION_MS), 2000);
106 assert.equal(craftDurationMs(recipeDefinition("smelt-steel"), CRAFT_DURATION_MS), 6000);
107});
108
109test("open furnace near station emits shove and station-open", () =>
110{
111 const harness = makeSimulation({ workerCount: 1 });
112 try
113 {
114 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
115 const playerId = harness.simulation.spawnCharacter(1, character);
116 const record = harness.simulation.entityRecord(playerId);
117 const worker = harness.simulation.workers[record.workerId];
118 placeFurnace(worker, 9, 8);
119
120 harness.simulation.enqueue(1, {
121 type: "open-station",
122 characterId: character.character_id,
123 stationId: CraftingStationId.furnace
124 });
125 const result = harness.simulation.step();
126 assert.ok(result.events.some((event) => event.type === "tile-shove"));
127 assert.ok(result.events.some((event) =>
128 event.type === "station-open" && event.stationId === CraftingStationId.furnace));
129 }
130 finally
131 { harness.close(); }
132});
133
134test("smelting bronze consumes tin and copper, grants bar and Smithing XP", () =>
135{
136 const harness = makeSimulation({ workerCount: 1 });
137 try
138 {
139 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
140 const playerId = harness.simulation.spawnCharacter(1, character);
141 const record = harness.simulation.entityRecord(playerId);
142 const worker = harness.simulation.workers[record.workerId];
143 placeFurnace(worker, 9, 8);
144 giveInventory(harness, playerId, [
145 { itemId: ItemId.tinOre, quantity: 1 },
146 { itemId: ItemId.copperOre, quantity: 1 }
147 ]);
148
149 harness.simulation.enqueue(1, {
150 type: "start-craft",
151 characterId: character.character_id,
152 stationId: CraftingStationId.furnace,
153 recipeId: "smelt-bronze"
154 });
155 const start = harness.simulation.step();
156 assert.ok(start.events.some((event) => event.type === "tile-shove"));
157 assert.ok(start.events.some((event) =>
158 event.type === "item-bubble" && event.itemId === ItemId.bronzeBar));
159
160 const smithingBefore = harness.stateDb.getSkills(character.character_id).xp.smithing ?? 0;
161 advanceMs(harness, craftDurationMs(recipeDefinition("smelt-bronze"), CRAFT_DURATION_MS));
162
163 const bag = harness.simulation.bagOf(playerId);
164 assert.equal(countItem(bag.inventory, ItemId.tinOre), 0);
165 assert.equal(countItem(bag.inventory, ItemId.copperOre), 0);
166 assert.equal(countItem(bag.inventory, ItemId.bronzeBar), 1);
167 const smithingAfter = harness.stateDb.getSkills(character.character_id).xp.smithing ?? 0;
168 assert.ok(smithingAfter > smithingBefore);
169 }
170 finally
171 { harness.close(); }
172});
173
174test("smelting steel requires iron ore and coal", () =>
175{
176 const harness = makeSimulation({ workerCount: 1 });
177 try
178 {
179 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
180 const playerId = harness.simulation.spawnCharacter(1, character);
181 const record = harness.simulation.entityRecord(playerId);
182 const worker = harness.simulation.workers[record.workerId];
183 placeFurnace(worker, 9, 8);
184 setSkillLevel(worker, playerId, SkillId.smithing, tierSmeltSkillLevel(MaterialTier.steel));
185 giveInventory(harness, playerId, [
186 { itemId: ItemId.ironOre, quantity: 1 },
187 { itemId: ItemId.coal, quantity: 1 }
188 ]);
189
190 harness.simulation.enqueue(1, {
191 type: "start-craft",
192 characterId: character.character_id,
193 stationId: CraftingStationId.furnace,
194 recipeId: "smelt-steel"
195 });
196 harness.simulation.step();
197 advanceMs(harness, craftDurationMs(recipeDefinition("smelt-steel"), CRAFT_DURATION_MS));
198
199 const bag = harness.simulation.bagOf(playerId);
200 assert.equal(countItem(bag.inventory, ItemId.ironOre), 0);
201 assert.equal(countItem(bag.inventory, ItemId.coal), 0);
202 assert.equal(countItem(bag.inventory, ItemId.steelBar), 1);
203 }
204 finally
205 { harness.close(); }
206});
207
208test("craft rejects missing materials", () =>
209{
210 const harness = makeSimulation({ workerCount: 1 });
211 try
212 {
213 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
214 const playerId = harness.simulation.spawnCharacter(1, character);
215 const record = harness.simulation.entityRecord(playerId);
216 const worker = harness.simulation.workers[record.workerId];
217 placeFurnace(worker, 9, 8);
218 giveInventory(harness, playerId, [{ itemId: ItemId.copperOre, quantity: 1 }]);
219
220 harness.simulation.enqueue(1, {
221 type: "start-craft",
222 characterId: character.character_id,
223 stationId: CraftingStationId.furnace,
224 recipeId: "smelt-bronze"
225 });
226 const result = harness.simulation.step();
227 assert.ok(result.events.some((event) =>
228 event.type === "command-error" && /missing|materials/i.test(event.message ?? "")));
229 }
230 finally
231 { harness.close(); }
232});
233
234test("craft rejects low Smithing for iron bars", () =>
235{
236 const harness = makeSimulation({ workerCount: 1 });
237 try
238 {
239 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
240 const playerId = harness.simulation.spawnCharacter(1, character);
241 const record = harness.simulation.entityRecord(playerId);
242 const worker = harness.simulation.workers[record.workerId];
243 placeFurnace(worker, 9, 8);
244 giveInventory(harness, playerId, [{ itemId: ItemId.ironOre, quantity: 1 }]);
245
246 harness.simulation.enqueue(1, {
247 type: "start-craft",
248 characterId: character.character_id,
249 stationId: CraftingStationId.furnace,
250 recipeId: "smelt-iron"
251 });
252 const result = harness.simulation.step();
253 assert.ok(result.events.some((event) =>
254 event.type === "command-error" && /smithing|requires/i.test(event.message ?? "")));
255 }
256 finally
257 { harness.close(); }
258});
259
260test("craft rejects furnace recipes when not next to a furnace", () =>
261{
262 const harness = makeSimulation({ workerCount: 1 });
263 try
264 {
265 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
266 const playerId = harness.simulation.spawnCharacter(1, character);
267 giveInventory(harness, playerId, [
268 { itemId: ItemId.tinOre, quantity: 1 },
269 { itemId: ItemId.copperOre, quantity: 1 }
270 ]);
271
272 harness.simulation.enqueue(1, {
273 type: "start-craft",
274 characterId: character.character_id,
275 stationId: CraftingStationId.furnace,
276 recipeId: "smelt-bronze"
277 });
278 const result = harness.simulation.step();
279 assert.ok(result.events.some((event) =>
280 event.type === "command-error" && /station|next to/i.test(event.message ?? "")));
281 }
282 finally
283 { harness.close(); }
284});
285
286function placeStation (worker, tileX, tileY, typeId, state)
287{
288 const data = worker.world.chunkData(Math.floor(tileX / 16), Math.floor(tileY / 16));
289 const localX = tileX % 16;
290 const localY = tileY % 16;
291 data.blocks[localY * 16 + localX] = {
292 typeId,
293 state,
294 config: null
295 };
296}
297
298test("bronze pickaxe chain: pine log → handle, bar → head, assemble pickaxe", () =>
299{
300 const harness = makeSimulation({ workerCount: 1 });
301 try
302 {
303 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
304 const playerId = harness.simulation.spawnCharacter(1, character);
305 const record = harness.simulation.entityRecord(playerId);
306 const worker = harness.simulation.workers[record.workerId];
307 placeStation(worker, 9, 8, BlockType.woodworkingBench, "woodworking-bench");
308 placeStation(worker, 9, 9, BlockType.anvil, "anvil");
309 giveInventory(harness, playerId, [
310 { itemId: ItemId.pineLog, quantity: 1 },
311 { itemId: ItemId.bronzeBar, quantity: 1 }
312 ]);
313
314 harness.simulation.enqueue(1, {
315 type: "start-craft",
316 characterId: character.character_id,
317 stationId: CraftingStationId.woodworkingBench,
318 recipeId: "handle-pine"
319 });
320 advanceMs(harness, craftDurationMs(recipeDefinition("handle-pine"), CRAFT_DURATION_MS));
321 let bag = harness.simulation.bagOf(playerId);
322 assert.equal(countItem(bag.inventory, ItemId.pineHandle), 1);
323 assert.equal(countItem(bag.inventory, ItemId.pineLog), 0);
324
325 harness.simulation.enqueue(1, {
326 type: "start-craft",
327 characterId: character.character_id,
328 stationId: CraftingStationId.anvil,
329 recipeId: "pickaxe-head-bronze"
330 });
331 advanceMs(harness, craftDurationMs(recipeDefinition("pickaxe-head-bronze"), CRAFT_DURATION_MS));
332 bag = harness.simulation.bagOf(playerId);
333 assert.equal(countItem(bag.inventory, ItemId.bronzePickaxeHead), 1);
334 assert.equal(countItem(bag.inventory, ItemId.bronzeBar), 0);
335
336 harness.simulation.enqueue(1, {
337 type: "start-craft",
338 characterId: character.character_id,
339 stationId: CraftingStationId.woodworkingBench,
340 recipeId: "pickaxe-bronze"
341 });
342 advanceMs(harness, craftDurationMs(recipeDefinition("pickaxe-bronze"), CRAFT_DURATION_MS));
343 bag = harness.simulation.bagOf(playerId);
344 assert.equal(countItem(bag.inventory, ItemId.bronzePickaxe), 1);
345 assert.equal(countItem(bag.inventory, ItemId.pineHandle), 0);
346 assert.equal(countItem(bag.inventory, ItemId.bronzePickaxeHead), 0);
347 }
348 finally
349 { harness.close(); }
350});