import test from "node:test"; import assert from "node:assert/strict"; import { MaterialTier, GatherMaterialId, tierMinSkillLevel, tierMineSkillLevel, tierSmeltSkillLevel, tierRank, miningDurationMs, oreExhaustDurationMs, oreBoulderState, materialFromOreBoulderState, EXHAUSTED_ORE_BOULDER_STATE } from "../../shared/definitions/materialTiers.js"; import { canMineMaterial, oreItemIdForMaterial } from "../../shared/definitions/gathering.js"; import { ItemId, itemDefinition } from "../../shared/definitions/items.js"; import { SkillId } from "../../shared/definitions/skills.js"; import { canEquipItem } from "../../shared/equipment/stats.js"; import { normalizePlayerSkills } from "../../shared/skills/state.js"; import { totalXpForLevel } from "../../shared/skills/progression.js"; import { BlockType, blockDefinition } from "../../shared/definitions/tiles.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 placeOre (worker, tileX, tileY, state) { const data = worker.world.chunkData(Math.floor(tileX / 16), Math.floor(tileY / 16)); const localX = tileX % 16; const localY = tileY % 16; const index = localY * 16 + localX; data.blocks[index] = { typeId: BlockType.boulder, state, config: null }; return { data, index }; } function bagPickaxe (worker, playerId, itemId) { const handle = worker.world.ecs.handleOf(playerId); const inventory = worker.world.ecs.get(handle, Component.inventory); inventory.slots[0] = { itemId, quantity: 1 }; } 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 advanceMs (harness, ms) { const ticks = Math.ceil(ms / 50) + 3; for (let i = 0; i < ticks; i++) { harness.advance(50); harness.simulation.step(); } } test("tier skill gates: equip / mine / smelt", () => { assert.equal(tierMinSkillLevel(MaterialTier.bronze), 1); assert.equal(tierMinSkillLevel(MaterialTier.mithril), 35); assert.equal(tierMinSkillLevel(MaterialTier.dragon), 90); assert.equal(tierMineSkillLevel(MaterialTier.iron), 5); assert.equal(tierMineSkillLevel(MaterialTier.mithril), 18); assert.equal(tierSmeltSkillLevel(MaterialTier.iron), 8); assert.equal(tierSmeltSkillLevel(MaterialTier.steel), 15); assert.equal(tierMineSkillLevel(GatherMaterialId.silver), 5); assert.equal(tierSmeltSkillLevel(GatherMaterialId.silver), 8); assert.equal(tierRank(GatherMaterialId.silver), 2); assert.equal(tierMineSkillLevel(GatherMaterialId.gold), 10); assert.equal(tierSmeltSkillLevel(GatherMaterialId.gold), 15); assert.equal(tierRank(GatherMaterialId.gold), 3); assert.equal(tierRank(MaterialTier.dragon), 7); }); test("mining duration uses resource and tool ranks", () => { assert.equal(miningDurationMs(MaterialTier.bronze, MaterialTier.bronze), 5000); assert.equal(miningDurationMs(MaterialTier.iron, MaterialTier.bronze), 13000); assert.ok(miningDurationMs(MaterialTier.bronze, MaterialTier.dragon) >= 500); }); test("ore exhaust duration is 10s times associated bar rank", () => { assert.equal(oreExhaustDurationMs(MaterialTier.bronze), 10_000); assert.equal(oreExhaustDurationMs(MaterialTier.iron), 20_000); assert.equal(oreExhaustDurationMs(GatherMaterialId.tin), 10_000); assert.equal(oreExhaustDurationMs(GatherMaterialId.copper), 10_000); assert.equal(oreExhaustDurationMs(GatherMaterialId.coal), 30_000); assert.equal(oreExhaustDurationMs(GatherMaterialId.gold), 30_000); assert.equal(oreExhaustDurationMs(GatherMaterialId.silver), 20_000); assert.equal(oreExhaustDurationMs(MaterialTier.dragon), 70_000); }); test("ore boulder states map to materials", () => { assert.equal(materialFromOreBoulderState("tin-ore"), GatherMaterialId.tin); assert.equal(materialFromOreBoulderState("copper-ore"), GatherMaterialId.copper); assert.equal(materialFromOreBoulderState("coal"), GatherMaterialId.coal); assert.equal(materialFromOreBoulderState("silver-ore"), GatherMaterialId.silver); assert.equal(materialFromOreBoulderState("gold-ore"), GatherMaterialId.gold); assert.equal(materialFromOreBoulderState("boulder"), null); assert.equal(materialFromOreBoulderState("bronze-ore"), null); assert.equal(oreBoulderState(MaterialTier.mithril), "mithril-ore"); assert.equal(oreBoulderState(GatherMaterialId.silver), "silver-ore"); assert.equal(oreBoulderState(GatherMaterialId.gold), "gold-ore"); }); test("boulder block exists", () => { assert.equal(blockDefinition(BlockType.boulder)?.key, "boulder"); }); test("iron pickaxe requires Mining 10", () => { const skills = normalizePlayerSkills(null); assert.equal(canEquipItem(skills, itemDefinition(ItemId.ironPickaxe)).ok, false); skills.xp[SkillId.mining] = totalXpForLevel(10); assert.equal(canEquipItem(skills, itemDefinition(ItemId.ironPickaxe)).ok, true); assert.equal(canEquipItem(skills, itemDefinition(ItemId.bronzePickaxe)).ok, true); }); test("canMineMaterial enforces skill and tool tier", () => { assert.equal(canMineMaterial(1, MaterialTier.bronze, MaterialTier.iron).ok, false); // One tier below is allowed once the Mining skill gate is met. assert.equal(canMineMaterial(5, MaterialTier.bronze, MaterialTier.iron).ok, true); assert.equal(canMineMaterial(5, MaterialTier.iron, MaterialTier.iron).ok, true); assert.equal(canMineMaterial(10, MaterialTier.bronze, MaterialTier.steel).ok, false); }); test("mining without a pickaxe is rejected", () => { 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]; placeOre(worker, 9, 8, "copper-ore"); harness.simulation.enqueue(1, { type: "mine", characterId: character.character_id, tileX: 9, tileY: 8 }); const result = harness.simulation.step(); assert.ok(result.events.some((event) => event.type === "command-error" && event.message === "You need a pickaxe to mine this!")); } finally { harness.close(); } }); test("plain boulder cannot be mined", () => { 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]; bagPickaxe(worker, playerId, ItemId.bronzePickaxe); placeOre(worker, 9, 8, EXHAUSTED_ORE_BOULDER_STATE); harness.simulation.enqueue(1, { type: "mine", characterId: character.character_id, tileX: 9, tileY: 8 }); const result = harness.simulation.step(); assert.ok(result.events.some((event) => event.type === "command-error" && (/no ore|exhausted/i.test(event.message ?? "")))); } finally { harness.close(); } }); test("pickaxe two tiers below cannot mine the ore", () => { 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]; bagPickaxe(worker, playerId, ItemId.bronzePickaxe); setSkillLevel(worker, playerId, SkillId.mining, 20); placeOre(worker, 9, 8, "coal"); harness.simulation.enqueue(1, { type: "mine", characterId: character.character_id, tileX: 9, tileY: 8 }); const result = harness.simulation.step(); assert.ok(result.events.some((event) => event.type === "command-error" && event.message === "You need a pickaxe to mine this!")); } finally { harness.close(); } }); test("bronze pickaxe can mine iron ore (one tier below)", () => { 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]; bagPickaxe(worker, playerId, ItemId.bronzePickaxe); setSkillLevel(worker, playerId, SkillId.mining, 10); placeOre(worker, 9, 8, "iron-ore"); harness.simulation.enqueue(1, { type: "mine", characterId: character.character_id, tileX: 9, tileY: 8 }); const start = harness.simulation.step(); assert.ok(start.events.some((event) => event.type === "gather-swing" && event.toolItemId === ItemId.bronzePickaxe), JSON.stringify(start.events)); } finally { harness.close(); } }); test("mining uses the best pickaxe in inventory without equipping", () => { 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]; setSkillLevel(worker, playerId, SkillId.mining, 10); const handle = worker.world.ecs.handleOf(playerId); const inventory = worker.world.ecs.get(handle, Component.inventory); inventory.slots[0] = { itemId: ItemId.bronzePickaxe, quantity: 1 }; inventory.slots[1] = { itemId: ItemId.ironPickaxe, quantity: 1 }; placeOre(worker, 9, 8, "iron-ore"); harness.simulation.enqueue(1, { type: "mine", characterId: character.character_id, tileX: 9, tileY: 8 }); harness.simulation.step(); advanceMs(harness, miningDurationMs(MaterialTier.iron, MaterialTier.iron)); const bag = harness.simulation.bagOf(playerId); assert.ok(countItem(bag.inventory, ItemId.ironOre) >= 1); const oreStack = (bag.inventory ?? []).find((slot) => slot?.itemId === ItemId.ironOre); assert.ok(oreStack?.origins?.some((entry) => entry.origin?.kind === "gathered" && entry.origin?.verb === "Mined" && typeof entry.origin?.displayName === "string" && entry.origin.displayName.length > 0)); } finally { harness.close(); } }); test("mining grants ore and Mining XP then exhausts and respawns the node", () => { 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]; bagPickaxe(worker, playerId, ItemId.bronzePickaxe); const { data, index } = placeOre(worker, 9, 8, "copper-ore"); harness.simulation.enqueue(1, { type: "mine", characterId: character.character_id, tileX: 9, tileY: 8 }); const start = harness.simulation.step(); assert.ok(start.events.some((event) => event.type === "gather-swing" || event.type === "tile-shove")); const swing = start.events.find((event) => event.type === "gather-swing"); assert.equal(swing?.toolItemId, ItemId.bronzePickaxe); advanceMs(harness, miningDurationMs(GatherMaterialId.copper, MaterialTier.bronze)); const bag = harness.simulation.bagOf(playerId); assert.ok(countItem(bag.inventory, ItemId.copperOre) >= 1); const skills = harness.stateDb.getSkills(character.character_id); assert.ok(skills.xp.mining > 0); assert.equal(oreItemIdForMaterial(GatherMaterialId.copper), ItemId.copperOre); assert.equal(data.blocks[index].state, EXHAUSTED_ORE_BOULDER_STATE); harness.simulation.enqueue(1, { type: "mine", characterId: character.character_id, tileX: 9, tileY: 8 }); const exhaustedResult = harness.simulation.step(); assert.ok(exhaustedResult.events.some((event) => event.type === "command-error" && /exhausted/i.test(event.message ?? ""))); advanceMs(harness, oreExhaustDurationMs(GatherMaterialId.copper)); assert.equal(data.blocks[index].state, "copper-ore"); } finally { harness.close(); } }); test("coal mining yields coal and exhausts the node", () => { 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]; setSkillLevel(worker, playerId, SkillId.mining, 20); bagPickaxe(worker, playerId, ItemId.steelPickaxe); const { data, index } = placeOre(worker, 9, 8, "coal"); harness.simulation.enqueue(1, { type: "mine", characterId: character.character_id, tileX: 9, tileY: 8 }); harness.simulation.step(); advanceMs(harness, miningDurationMs(GatherMaterialId.coal, MaterialTier.steel)); const bag = harness.simulation.bagOf(playerId); assert.ok(countItem(bag.inventory, ItemId.coal) >= 1); assert.equal(data.blocks[index].state, EXHAUSTED_ORE_BOULDER_STATE); } finally { harness.close(); } });