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 { ArmourItemId } from "../../shared/definitions/armourItems.js"; import { SkillId } from "../../shared/definitions/skills.js"; import { MaterialTier, GatherMaterialId, tierSmeltSkillLevel, smeltingDurationMs, associatedBarTier } from "../../shared/definitions/materialTiers.js"; import { craftDurationMs } from "../../shared/definitions/gathering.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 recipes: bronze is tin+copper; steel is iron+coal; high tiers need coal", () => { assert.equal(blockDefinition(BlockType.furnace)?.key, "furnace"); const bronze = recipeDefinition("smelt-bronze"); assert.deepEqual(bronze.inputs.map((row) => row.itemId).sort(), [ ItemId.copperOre, ItemId.tinOre ].sort()); assert.equal(bronze.output.itemId, ItemId.bronzeBar); assert.equal(bronze.minSkillLevel, tierSmeltSkillLevel(MaterialTier.bronze)); const steel = recipesForStation(CraftingStationId.furnace) .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); const silver = recipesForStation(CraftingStationId.furnace) .find((recipe) => recipe.recipeId === "smelt-silver"); assert.ok(silver); assert.deepEqual(silver.inputs.map((row) => row.itemId), [ItemId.silverOre]); assert.equal(silver.output.itemId, ItemId.silverBar); assert.equal(silver.minSkillLevel, tierSmeltSkillLevel(GatherMaterialId.silver)); const gold = recipesForStation(CraftingStationId.furnace) .find((recipe) => recipe.recipeId === "smelt-gold"); assert.ok(gold); assert.deepEqual(gold.inputs.map((row) => row.itemId), [ItemId.goldOre]); assert.equal(gold.output.itemId, ItemId.goldBar); assert.equal(gold.minSkillLevel, tierSmeltSkillLevel(GatherMaterialId.gold)); const adamantium = recipeDefinition("smelt-adamantium"); assert.deepEqual(adamantium.inputs, [ { itemId: ItemId.adamantiumOre, quantity: 1 }, { itemId: ItemId.coal, quantity: 1 } ]); const orichalcum = recipeDefinition("smelt-orichalcum"); assert.deepEqual(orichalcum.inputs, [ { itemId: ItemId.orichalcumOre, quantity: 1 }, { itemId: ItemId.coal, quantity: 3 } ]); const dragon = recipeDefinition("smelt-dragon"); assert.deepEqual(dragon.inputs, [ { itemId: ItemId.dragonOre, quantity: 1 }, { itemId: ItemId.coal, quantity: 5 } ]); }); test("tin copper coal map to associated bar tiers for smelt duration", () => { assert.equal(associatedBarTier(GatherMaterialId.tin), MaterialTier.bronze); assert.equal(associatedBarTier(GatherMaterialId.copper), MaterialTier.bronze); assert.equal(associatedBarTier(GatherMaterialId.coal), MaterialTier.steel); assert.equal(associatedBarTier(GatherMaterialId.silver), GatherMaterialId.silver); assert.equal(associatedBarTier(GatherMaterialId.gold), GatherMaterialId.gold); assert.equal(smeltingDurationMs(MaterialTier.bronze), 2000); assert.equal(smeltingDurationMs(GatherMaterialId.tin), 2000); assert.equal(smeltingDurationMs(MaterialTier.steel), 6000); assert.equal(smeltingDurationMs(GatherMaterialId.coal), 6000); assert.equal(smeltingDurationMs(GatherMaterialId.silver), 4000); assert.equal(smeltingDurationMs(GatherMaterialId.gold), 6000); assert.equal(craftDurationMs(recipeDefinition("smelt-bronze"), CRAFT_DURATION_MS), 2000); assert.equal(craftDurationMs(recipeDefinition("smelt-steel"), CRAFT_DURATION_MS), 6000); assert.equal(craftDurationMs(recipeDefinition("smelt-silver"), CRAFT_DURATION_MS), 4000); assert.equal(craftDurationMs(recipeDefinition("smelt-gold"), CRAFT_DURATION_MS), 6000); }); 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 tin and copper, 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.tinOre, quantity: 1 }, { itemId: ItemId.copperOre, 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, craftDurationMs(recipeDefinition("smelt-bronze"), CRAFT_DURATION_MS)); const bag = harness.simulation.bagOf(playerId); assert.equal(countItem(bag.inventory, ItemId.tinOre), 0); assert.equal(countItem(bag.inventory, ItemId.copperOre), 0); assert.equal(countItem(bag.inventory, ItemId.bronzeBar), 1); const bar = (bag.inventory ?? []).find((slot) => slot?.itemId === ItemId.bronzeBar); assert.equal(bar?.origins?.[0]?.origin?.verb, "Forged"); assert.match(bar?.origins?.[0]?.origin?.displayName ?? "", /^Ada/); 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, craftDurationMs(recipeDefinition("smelt-steel"), 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, [{ itemId: ItemId.copperOre, 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" && /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.tinOre, quantity: 1 }, { itemId: ItemId.copperOre, 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(); } }); function placeStation (worker, tileX, tileY, typeId, state) { 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, state, config: null }; } test("bronze pickaxe chain: pine log → handle, bar → head, assemble pickaxe", () => { 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]; placeStation(worker, 9, 8, BlockType.woodworkingBench, "woodworking-bench"); placeStation(worker, 9, 9, BlockType.anvil, "anvil"); placeStation(worker, 8, 9, BlockType.craftingBench, "crafting-bench"); giveInventory(harness, playerId, [ { itemId: ItemId.pineLog, quantity: 1 }, { itemId: ItemId.bronzeBar, quantity: 1 } ]); harness.simulation.enqueue(1, { type: "start-craft", characterId: character.character_id, stationId: CraftingStationId.woodworkingBench, recipeId: "handle-pine" }); advanceMs(harness, craftDurationMs(recipeDefinition("handle-pine"), CRAFT_DURATION_MS)); let bag = harness.simulation.bagOf(playerId); assert.equal(countItem(bag.inventory, ItemId.pineHandle), 1); assert.equal(countItem(bag.inventory, ItemId.pineLog), 0); const handle = (bag.inventory ?? []).find((slot) => slot?.itemId === ItemId.pineHandle); assert.equal(handle?.origins?.[0]?.origin?.verb, "Carved"); harness.simulation.enqueue(1, { type: "start-craft", characterId: character.character_id, stationId: CraftingStationId.anvil, recipeId: "pickaxe-head-bronze" }); advanceMs(harness, craftDurationMs(recipeDefinition("pickaxe-head-bronze"), CRAFT_DURATION_MS)); bag = harness.simulation.bagOf(playerId); assert.equal(countItem(bag.inventory, ItemId.bronzePickaxeHead), 1); assert.equal(countItem(bag.inventory, ItemId.bronzeBar), 0); const head = (bag.inventory ?? []).find((slot) => slot?.itemId === ItemId.bronzePickaxeHead); assert.equal(head?.origins?.[0]?.origin?.verb, "Smithed"); harness.simulation.enqueue(1, { type: "start-craft", characterId: character.character_id, stationId: CraftingStationId.craftingBench, recipeId: "pickaxe-bronze" }); advanceMs(harness, craftDurationMs(recipeDefinition("pickaxe-bronze"), CRAFT_DURATION_MS)); bag = harness.simulation.bagOf(playerId); assert.equal(countItem(bag.inventory, ItemId.bronzePickaxe), 1); assert.equal(countItem(bag.inventory, ItemId.pineHandle), 0); assert.equal(countItem(bag.inventory, ItemId.bronzePickaxeHead), 0); const pickaxe = (bag.inventory ?? []).find((slot) => slot?.itemId === ItemId.bronzePickaxe); assert.equal(pickaxe?.origins?.[0]?.origin?.verb, "Assembled"); assert.match(pickaxe?.origins?.[0]?.origin?.displayName ?? "", /^Ada/); } finally { harness.close(); } }); test("anvil armour recipes use bar counts per piece", () => { const chest = recipeDefinition("armour-chestplate-bronze"); assert.ok(chest); assert.equal(chest.stationId, CraftingStationId.anvil); assert.equal(chest.output.itemId, ArmourItemId.bronzeChestplate); assert.deepEqual(chest.inputs, [{ itemId: ItemId.bronzeBar, quantity: 6 }]); const greaves = recipeDefinition("armour-greaves-iron"); assert.deepEqual(greaves.inputs, [{ itemId: ItemId.ironBar, quantity: 4 }]); const boots = recipeDefinition("armour-boots-steel"); assert.deepEqual(boots.inputs, [{ itemId: ItemId.steelBar, quantity: 2 }]); const helmet = recipeDefinition("armour-helmet-mithril"); assert.deepEqual(helmet.inputs, [{ itemId: ItemId.mithrilBar, quantity: 2 }]); const fullHelm = recipeDefinition("armour-full-helm-adamantium"); assert.deepEqual(fullHelm.inputs, [{ itemId: ItemId.adamantiumBar, quantity: 3 }]); const horned = recipeDefinition("armour-horned-helm-orichalcum"); assert.deepEqual(horned.inputs, [ { itemId: ItemId.orichalcumBar, quantity: 2 }, { itemId: ItemId.bones, quantity: 1 } ]); const anvilArmour = recipesForStation(CraftingStationId.anvil) .filter((recipe) => recipe.recipeId.startsWith("armour-")); assert.equal(anvilArmour.length, 7 * 6); });