import test from "node:test"; import assert from "node:assert/strict"; import { BlockType, plantGatherForBlock } from "../../shared/definitions/tiles.js"; import { CraftingStationId } from "../../shared/definitions/craftingStations.js"; import { MetalItemId } from "../../shared/definitions/metalItems.js"; import { LeatherItemId } from "../../shared/definitions/leatherItems.js"; import { ClothItemId, rawClothItemId } from "../../shared/definitions/clothItems.js"; import { ItemId, itemDefinition, itemEquipSprite } from "../../shared/definitions/items.js"; import { ClothTier, MaterialTier, metalTierForCloth, metalTierForHerbTier, clothExhaustDurationMs, tierRank } from "../../shared/definitions/materialTiers.js"; import { MobId, mobDefinition, mobSpriteName } from "../../shared/definitions/mobs.js"; import { EquipmentKind } from "../../shared/definitions/equipmentKinds.js"; import { recipeDefinition, recipesForStation } from "../../shared/crafting/stationRecipes.js"; import { resolveItemDualTint, resolveEquipTint, resolveEquipDualTint } from "../../shared/render/tint.js"; import { resolvePlantClippers } from "../../shared/definitions/gathering.js"; import { SkillId } from "../../shared/definitions/skills.js"; import { normalizePlayerSkills } from "../../shared/skills/state.js"; import { totalXpForLevel } from "../../shared/skills/progression.js"; import { emptyEquipment, emptySlots } from "../../shared/items/inventory.js"; import { makeCharacter, makeSimulation } from "./helpers/simulationHarness.js"; import { Component } from "../src/ecs/components.js"; import { mobComponents } from "../src/simulation/spawners.js"; function workerOf (simulation, playerId) { const record = simulation.entityRecord(playerId); return simulation.workers[record.workerId]; } function advanceMs (harness, ms) { const ticks = Math.ceil(ms / 50) + 3; for (let i = 0; i < ticks; i++) { harness.advance(50); harness.simulation.step(); } } 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, ...(stack.instance ? { instance: structuredClone(stack.instance) } : {}) }; }); harness.simulation.applyBag(playerId, { inventory, equipment: emptyEquipment(), vault: [], vaultReserved: [] }); } test("plant clippers use dual metal/leather tint and bar+leather recipe", () => { const def = itemDefinition(MetalItemId.bronzePlantClippers); assert.equal(def.iconKey, "plant-clippers"); assert.equal(def.dualTintFromTier, true); assert.equal(def.leatherTier, "softLeather"); const dual = resolveItemDualTint({ itemId: MetalItemId.bronzePlantClippers }, def); assert.ok(dual?.shaft); assert.ok(dual?.tip); assert.equal(itemEquipSprite(MetalItemId.bronzePlantClippers), "item:plant-clippers:plant-clippers:equip"); assert.equal(def.equipment?.tintFromTier, true); const equipTint = resolveEquipTint(MetalItemId.bronzePlantClippers); assert.ok(equipTint); assert.equal(resolveEquipDualTint({ itemId: MetalItemId.bronzePlantClippers }, def), null); const recipe = recipeDefinition("plant-clippers-bronze"); assert.deepEqual(recipe.inputs, [ { itemId: ItemId.bronzeBar, quantity: 1 }, { itemId: LeatherItemId.softLeather, quantity: 1 } ]); assert.equal(recipe.stationId, CraftingStationId.anvil); }); test("shears are magenta-tinted metal and craft from one bar", () => { const def = itemDefinition(MetalItemId.ironShears); assert.equal(def.equipment.kind, EquipmentKind.shears); assert.equal(def.tintFromTier, true); assert.equal(def.dualTintFromTier, undefined); const recipe = recipeDefinition("shears-iron"); assert.deepEqual(recipe.inputs, [{ itemId: ItemId.ironBar, quantity: 1 }]); assert.equal( recipesForStation(CraftingStationId.anvil).filter((r) => r.craftGroup === "shears").length, 6 ); assert.equal(recipeDefinition("shears-bronze"), null); }); test("sheep and cashmere goat are shearable with fleece/shorn sprites", () => { const sheep = mobDefinition(MobId.sheep); assert.equal(sheep.shearable.clothTier, ClothTier.wool); assert.equal(mobSpriteName(MobId.sheep), "mob:sheep:fleece"); assert.equal(mobSpriteName(MobId.sheep, "shorn"), "mob:sheep:shorn"); const goat = mobDefinition(MobId.cashmereGoat); assert.equal(goat.shearable.clothTier, ClothTier.cashmere); assert.equal(mobSpriteName(MobId.cashmereGoat, "fleece"), "mob:cashmere-goat:fleece"); }); test("fairy uses own sprites, dual-channel kit, and random-element spell bolts", () => { const fairy = mobDefinition(MobId.fairy); assert.equal(fairy.spriteKey, undefined); assert.equal(mobSpriteName(MobId.fairy), "mob:fairy:fairy"); assert.equal(fairy.bloomBody, true); assert.equal(fairy.dualChannelAppearance, true); assert.equal(fairy.preferSpellAttack, true); assert.equal(fairy.randomElementSpell, true); assert.equal(fairy.fleeOnHitTiles, 2); assert.equal(fairy.randomElementMelee, undefined); assert.equal(fairy.lightRadiusTiles, 1); assert.equal(fairy.frames, 2); assert.equal(fairy.drops.length, 1); assert.equal(fairy.drops[0].itemId, ClothItemId.fairySilk); assert.equal(fairy.drops[0].chance, 0.7); assert.equal(MetalItemId.bronzeShears, undefined); assert.equal(itemDefinition(MetalItemId.ironShears)?.equipment?.kind, EquipmentKind.shears); }); test("flax needs steel-or-better clippers; linen fibre is named Flax", () => { assert.equal(itemDefinition(ClothItemId.linen).displayName, "Flax"); assert.equal(metalTierForCloth(ClothTier.linen), MaterialTier.steel); const skills = normalizePlayerSkills(null); skills.xp[SkillId.herbalism] = totalXpForLevel(20); const bronze = resolvePlantClippers({ inventorySlots: [{ itemId: MetalItemId.bronzePlantClippers, quantity: 1 }], equipmentSlots: emptyEquipment().slots, skills, minClothTier: ClothTier.linen }); assert.equal(bronze.ok, false); const steel = resolvePlantClippers({ inventorySlots: [{ itemId: MetalItemId.steelPlantClippers, quantity: 1 }], equipmentSlots: emptyEquipment().slots, skills, minClothTier: ClothTier.linen }); assert.equal(steel.ok, true); const plant = plantGatherForBlock({ typeId: BlockType.flaxPlant, state: "flax-plant" }); assert.equal(plant.kind, "flax"); assert.equal(plant.requiresClippers, true); assert.equal(plant.minClothTier, "linen"); }); test("silk cocoons need adamantium-or-better clippers", () => { assert.equal(metalTierForCloth(ClothTier.silk), MaterialTier.adamantium); assert.match(itemDefinition(ClothItemId.silk).description, /cocoon/i); const skills = normalizePlayerSkills(null); skills.xp[SkillId.herbalism] = totalXpForLevel(50); const mithril = resolvePlantClippers({ inventorySlots: [{ itemId: MetalItemId.mithrilPlantClippers, quantity: 1 }], equipmentSlots: emptyEquipment().slots, skills, minClothTier: ClothTier.silk }); assert.equal(mithril.ok, false); const adamantium = resolvePlantClippers({ inventorySlots: [{ itemId: MetalItemId.adamantiumPlantClippers, quantity: 1 }], equipmentSlots: emptyEquipment().slots, skills, minClothTier: ClothTier.silk }); assert.equal(adamantium.ok, true); const plant = plantGatherForBlock({ typeId: BlockType.silkCocoon, state: "silk-cocoon" }); assert.equal(plant.kind, "silk"); assert.equal(plant.requiresClippers, true); assert.equal(plant.minClothTier, "silk"); }); test("herb clippers require matching metal tier (sage → iron+)", () => { assert.equal(metalTierForHerbTier(2), MaterialTier.iron); assert.equal(metalTierForHerbTier(7), MaterialTier.dragon); const skills = normalizePlayerSkills(null); skills.xp[SkillId.herbalism] = totalXpForLevel(10); const bronze = resolvePlantClippers({ inventorySlots: [{ itemId: MetalItemId.bronzePlantClippers, quantity: 1 }], equipmentSlots: emptyEquipment().slots, skills, minMetalTier: metalTierForHerbTier(2) }); assert.equal(bronze.ok, false); const iron = resolvePlantClippers({ inventorySlots: [{ itemId: MetalItemId.ironPlantClippers, quantity: 1 }], equipmentSlots: emptyEquipment().slots, skills, minMetalTier: metalTierForHerbTier(2) }); assert.equal(iron.ok, true); }); test("shearing a sheep grants wool and sets shorn state", () => { const harness = makeSimulation({ workerCount: 1 }); try { const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); const playerId = harness.simulation.spawnCharacter(1, character); const worker = workerOf(harness.simulation, playerId); setSkillLevel(worker, playerId, SkillId.herbalism, 10); giveInventory(harness, playerId, [ { itemId: MetalItemId.ironShears, quantity: 1 } ]); const sheepId = harness.simulation.spawnEntity(mobComponents({ tileX: 9, tileY: 8, mobTypeId: MobId.sheep, spawnerId: "test-sheep", displayName: "Woolly" })); harness.simulation.enqueue(1, { type: "shear", characterId: character.character_id, targetEntityId: sheepId }); advanceMs(harness, 3500); const creature = worker.world.ecs.get( worker.world.ecs.handleOf(sheepId), Component.creature ); assert.equal(creature.fleeceState, "shorn"); assert.ok(creature.woolReadyAt > harness.now()); const bag = harness.simulation.bagOf(playerId); const woolId = rawClothItemId(ClothTier.wool); assert.ok(bag.inventory.some((s) => s?.itemId === woolId)); assert.equal( clothExhaustDurationMs(ClothTier.wool), tierRank(ClothTier.wool) * 10_000 ); } finally { harness.close(); } });