import test from "node:test"; import assert from "node:assert/strict"; import { BlockType, blockDefinition } from "../../shared/definitions/tiles.js"; import { CraftingStationId, CRAFT_DURATION_MS } from "../../shared/definitions/craftingStations.js"; import { ItemId } from "../../shared/definitions/items.js"; import { SkillId } from "../../shared/definitions/skills.js"; import { MaterialTier, tierSmeltSkillLevel } from "../../shared/definitions/materialTiers.js"; import { recipesForStation, recipeDefinition } from "../../shared/crafting/stationRecipes.js"; import { emptyEquipment, emptySlots } from "../../shared/items/inventory.js"; import { totalXpForLevel } from "../../shared/skills/progression.js"; import { makeCharacter, makeSimulation } from "./helpers/simulationHarness.js"; import { Component } from "../src/ecs/components.js"; function countItem (slots, itemId) { return (slots ?? []).reduce((sum, slot) => { if (slot?.itemId === itemId) { return sum + (slot.quantity | 0); } return sum; }, 0); } function placeFurnace (worker, tileX, tileY) { const data = worker.world.chunkData(Math.floor(tileX / 16), Math.floor(tileY / 16)); const localX = tileX % 16; const localY = tileY % 16; data.blocks[localY * 16 + localX] = { typeId: BlockType.furnace, state: "furnace", config: null }; } function setSkillLevel (worker, playerId, skillId, level) { const handle = worker.world.ecs.handleOf(playerId); const skills = worker.world.ecs.get(handle, Component.skills); skills.xp[skillId] = totalXpForLevel(level); } function giveInventory (harness, playerId, items) { const inventory = emptySlots(); items.forEach((stack, index) => { inventory[index] = { itemId: stack.itemId, quantity: stack.quantity }; }); harness.simulation.applyBag(playerId, { inventory, equipment: emptyEquipment(), vault: [], vaultReserved: [] }); } function advanceMs (harness, ms) { const ticks = Math.ceil(ms / 50) + 3; for (let i = 0; i < ticks; i++) { harness.advance(50); harness.simulation.step(); } } test("furnace block and steel recipe exist", () => { assert.equal(blockDefinition(BlockType.furnace)?.key, "furnace"); const recipes = recipesForStation(CraftingStationId.furnace); const steel = recipes.find((recipe) => recipe.recipeId === "smelt-steel"); assert.ok(steel); assert.deepEqual(steel.inputs.map((row) => row.itemId).sort(), [ ItemId.coal, ItemId.ironOre ].sort()); assert.equal(steel.output.itemId, ItemId.steelBar); assert.equal(recipeDefinition("smelt-bronze")?.minSkillLevel, tierSmeltSkillLevel(MaterialTier.bronze)); }); test("open furnace near station emits shove and station-open", () => { const harness = makeSimulation({ workerCount: 1 }); try { const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); const playerId = harness.simulation.spawnCharacter(1, character); const record = harness.simulation.entityRecord(playerId); const worker = harness.simulation.workers[record.workerId]; placeFurnace(worker, 9, 8); harness.simulation.enqueue(1, { type: "open-station", characterId: character.character_id, stationId: CraftingStationId.furnace }); const result = harness.simulation.step(); assert.ok(result.events.some((event) => event.type === "tile-shove")); assert.ok(result.events.some((event) => event.type === "station-open" && event.stationId === CraftingStationId.furnace)); } finally { harness.close(); } }); test("smelting bronze consumes ore, grants bar and Smithing XP", () => { const harness = makeSimulation({ workerCount: 1 }); try { const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); const playerId = harness.simulation.spawnCharacter(1, character); const record = harness.simulation.entityRecord(playerId); const worker = harness.simulation.workers[record.workerId]; placeFurnace(worker, 9, 8); giveInventory(harness, playerId, [{ itemId: ItemId.bronzeOre, quantity: 1 }]); harness.simulation.enqueue(1, { type: "start-craft", characterId: character.character_id, stationId: CraftingStationId.furnace, recipeId: "smelt-bronze" }); const start = harness.simulation.step(); assert.ok(start.events.some((event) => event.type === "tile-shove")); assert.ok(start.events.some((event) => event.type === "item-bubble" && event.itemId === ItemId.bronzeBar)); const smithingBefore = harness.stateDb.getSkills(character.character_id).xp.smithing ?? 0; advanceMs(harness, CRAFT_DURATION_MS); const bag = harness.simulation.bagOf(playerId); assert.equal(countItem(bag.inventory, ItemId.bronzeOre), 0); assert.equal(countItem(bag.inventory, ItemId.bronzeBar), 1); const smithingAfter = harness.stateDb.getSkills(character.character_id).xp.smithing ?? 0; assert.ok(smithingAfter > smithingBefore); } finally { harness.close(); } }); test("smelting steel requires iron ore and coal", () => { const harness = makeSimulation({ workerCount: 1 }); try { const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); const playerId = harness.simulation.spawnCharacter(1, character); const record = harness.simulation.entityRecord(playerId); const worker = harness.simulation.workers[record.workerId]; placeFurnace(worker, 9, 8); setSkillLevel(worker, playerId, SkillId.smithing, tierSmeltSkillLevel(MaterialTier.steel)); giveInventory(harness, playerId, [ { itemId: ItemId.ironOre, quantity: 1 }, { itemId: ItemId.coal, quantity: 1 } ]); harness.simulation.enqueue(1, { type: "start-craft", characterId: character.character_id, stationId: CraftingStationId.furnace, recipeId: "smelt-steel" }); harness.simulation.step(); advanceMs(harness, CRAFT_DURATION_MS); const bag = harness.simulation.bagOf(playerId); assert.equal(countItem(bag.inventory, ItemId.ironOre), 0); assert.equal(countItem(bag.inventory, ItemId.coal), 0); assert.equal(countItem(bag.inventory, ItemId.steelBar), 1); } finally { harness.close(); } }); test("craft rejects missing materials", () => { const harness = makeSimulation({ workerCount: 1 }); try { const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); const playerId = harness.simulation.spawnCharacter(1, character); const record = harness.simulation.entityRecord(playerId); const worker = harness.simulation.workers[record.workerId]; placeFurnace(worker, 9, 8); giveInventory(harness, playerId, []); harness.simulation.enqueue(1, { type: "start-craft", characterId: character.character_id, stationId: CraftingStationId.furnace, recipeId: "smelt-bronze" }); const result = harness.simulation.step(); assert.ok(result.events.some((event) => event.type === "command-error" && /missing|materials/i.test(event.message ?? ""))); } finally { harness.close(); } }); test("craft rejects low Smithing for iron bars", () => { const harness = makeSimulation({ workerCount: 1 }); try { const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); const playerId = harness.simulation.spawnCharacter(1, character); const record = harness.simulation.entityRecord(playerId); const worker = harness.simulation.workers[record.workerId]; placeFurnace(worker, 9, 8); giveInventory(harness, playerId, [{ itemId: ItemId.ironOre, quantity: 1 }]); harness.simulation.enqueue(1, { type: "start-craft", characterId: character.character_id, stationId: CraftingStationId.furnace, recipeId: "smelt-iron" }); const result = harness.simulation.step(); assert.ok(result.events.some((event) => event.type === "command-error" && /smithing|requires/i.test(event.message ?? ""))); } finally { harness.close(); } }); test("craft rejects furnace recipes when not next to a furnace", () => { const harness = makeSimulation({ workerCount: 1 }); try { const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); const playerId = harness.simulation.spawnCharacter(1, character); giveInventory(harness, playerId, [{ itemId: ItemId.bronzeOre, quantity: 1 }]); harness.simulation.enqueue(1, { type: "start-craft", characterId: character.character_id, stationId: CraftingStationId.furnace, recipeId: "smelt-bronze" }); const result = harness.simulation.step(); assert.ok(result.events.some((event) => event.type === "command-error" && /station|next to/i.test(event.message ?? ""))); } finally { harness.close(); } });