import test from "node:test"; import assert from "node:assert/strict"; import { BlockType, GroundType } from "../../shared/definitions/tiles.js"; import { rawFishItemId, cookedFishItemId, baitItemIdForCloth, FishingToolItemId, fishingDifficulty, fishingExhaustDurationMs, normalizeFishingSpotConfig } from "../../shared/definitions/fishItems.js"; import { ClothTier } from "../../shared/definitions/materialTiers.js"; import { yarnItemId } from "../../shared/definitions/clothItems.js"; import { EquipmentKind } from "../../shared/definitions/equipmentKinds.js"; import { SkillId } from "../../shared/definitions/skills.js"; import { emptyEquipment, emptySlots } from "../../shared/items/inventory.js"; import { totalXpForLevel } from "../../shared/skills/progression.js"; import { itemDefinition } from "../../shared/definitions/items.js"; import { makeCharacter, makeSimulation } from "./helpers/simulationHarness.js"; import { Component } from "../src/ecs/components.js"; function workerOf (simulation, playerId) { const record = simulation.entityRecord(playerId); return simulation.workers[record.workerId]; } 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(); } } function placeFishingSpot (worker, tileX, tileY, fishes, groundType = GroundType.deepWater) { const data = worker.world.chunkData(Math.floor(tileX / 16), Math.floor(tileY / 16)); const localX = ((tileX % 16) + 16) % 16; const localY = ((tileY % 16) + 16) % 16; const index = localY * 16 + localX; data.grounds[index] = { typeId: groundType, state: null }; data.blocks[index] = { typeId: BlockType.fishingSpot, state: "fishing-spot", config: normalizeFishingSpotConfig({ fishes }) }; } test("fishing difficulty and exhaust scale with tier", () => { assert.equal(fishingDifficulty(1), 1); assert.equal(fishingDifficulty(2), 10); assert.equal(fishingExhaustDurationMs(3), 30_000); }); test("cooked fish healing is tier times 5", () => { assert.equal(itemDefinition(cookedFishItemId("minnow")).healing, 5); assert.equal(itemDefinition(cookedFishItemId("shark")).healing, 35); }); test("net catches tier-1 fish without bait and exhausts spot", () => { 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); worker.world.rng = () => 0.999; setSkillLevel(worker, playerId, SkillId.fishing, 1); const minnow = rawFishItemId("minnow"); const trout = rawFishItemId("trout"); placeFishingSpot(worker, 9, 8, [minnow, trout]); giveInventory(harness, playerId, [ { itemId: FishingToolItemId.net, quantity: 1 } ]); harness.simulation.enqueue(1, { type: "start-fish", characterId: character.character_id, tileX: 9, tileY: 8 }); advanceMs(harness, 10_000); const bag = harness.simulation.bagOf(playerId); assert.ok(bag.inventory.some((s) => s?.itemId === minnow), "expected minnow"); assert.equal(bag.inventory.some((s) => s?.itemId === trout), false); const block = worker.world.chunkData(0, 0).blocks[8 * 16 + 9]; assert.equal(block.state, "exhausted"); assert.ok(worker.world.fishingSpotRespawns.has("overworld:9,8")); } finally { harness.close(); } }); test("rod consumes highest useful bait for spot", () => { 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); worker.world.rng = () => 0.999; setSkillLevel(worker, playerId, SkillId.fishing, 20); const trout = rawFishItemId("trout"); placeFishingSpot(worker, 9, 8, [trout]); const worms = baitItemIdForCloth(ClothTier.cotton); const maggots = baitItemIdForCloth(ClothTier.wool); const crickets = baitItemIdForCloth(ClothTier.linen); giveInventory(harness, playerId, [ { itemId: FishingToolItemId.oakRod, quantity: 1 }, { itemId: worms, quantity: 2 }, { itemId: maggots, quantity: 2 }, { itemId: crickets, quantity: 2 } ]); harness.simulation.enqueue(1, { type: "start-fish", characterId: character.character_id, tileX: 9, tileY: 8 }); advanceMs(harness, 200); const bag = harness.simulation.bagOf(playerId); assert.equal(bag.inventory.find((s) => s?.itemId === crickets)?.quantity ?? 0, 1); assert.equal(bag.inventory.find((s) => s?.itemId === maggots)?.quantity ?? 0, 2); } finally { harness.close(); } }); test("shallow water fishing blocked in winter", () => { const harness = makeSimulation({ workerCount: 1, now: Date.UTC(2026, 0, 15) }); 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.fishing, 1); placeFishingSpot(worker, 9, 8, [rawFishItemId("minnow")], GroundType.water); giveInventory(harness, playerId, [ { itemId: FishingToolItemId.net, quantity: 1 } ]); harness.simulation.enqueue(1, { type: "start-fish", characterId: character.character_id, tileX: 9, tileY: 8 }); const result = harness.simulation.step(); assert.ok(result.events.some((e) => e.type === "command-error")); assert.equal(worker.world.fishSessions.size, 0); } finally { harness.close(); } }); test("pole + yarn recipes: log makes pole, pole+2 yarn makes rod", () => { const pole = itemDefinition(FishingToolItemId.birchPole); const rod = itemDefinition(FishingToolItemId.birchRod); assert.equal(pole?.fishingPole, true); assert.equal(rod?.poleItemId, FishingToolItemId.birchPole); assert.equal(rod?.equipment?.kind, EquipmentKind.fishingRod); assert.equal(pole?.equipment, undefined); }); test("failed rod fishing can snap the line into pole + yarn", () => { 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); // Always fail skill rolls (0) and always snap the line (0 < 0.05). worker.world.rng = () => 0; setSkillLevel(worker, playerId, SkillId.fishing, 10); const perch = rawFishItemId("perch"); placeFishingSpot(worker, 9, 8, [perch]); const maggots = baitItemIdForCloth(ClothTier.wool); giveInventory(harness, playerId, [ { itemId: FishingToolItemId.birchRod, quantity: 1 }, { itemId: maggots, quantity: 1 } ]); harness.simulation.enqueue(1, { type: "start-fish", characterId: character.character_id, tileX: 9, tileY: 8 }); const events = []; const ticks = Math.ceil(12_000 / 50) + 5; for (let i = 0; i < ticks; i++) { harness.advance(50); const step = harness.simulation.step(); events.push(...(step.events ?? [])); } assert.ok(events.some((e) => e.type === "command-error" && e.message === "The fish got away.")); assert.ok(events.some((e) => e.type === "command-error" && e.message === "Your fishing line broke!")); const bag = harness.simulation.bagOf(playerId); assert.equal(bag.inventory.some((s) => s?.itemId === FishingToolItemId.birchRod), false); assert.ok(bag.inventory.some((s) => s?.itemId === FishingToolItemId.birchPole), "expected pole"); assert.ok(bag.inventory.some((s) => s?.itemId === yarnItemId(ClothTier.wool)), "expected wool yarn"); } finally { harness.close(); } }); test("line-break yarn drops to ground when bag is full", () => { 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); worker.world.rng = () => 0; setSkillLevel(worker, playerId, SkillId.fishing, 10); const perch = rawFishItemId("perch"); placeFishingSpot(worker, 9, 8, [perch]); const maggots = baitItemIdForCloth(ClothTier.wool); const yarn = yarnItemId(ClothTier.wool); // Rod equipped; bag full except bait (consumed on start → one free slot for pole only). const slots = emptySlots(); slots[0] = { itemId: maggots, quantity: 1 }; for (let i = 1; i < slots.length; i++) { slots[i] = { itemId: rawFishItemId("minnow"), quantity: 1 }; } const equipment = emptyEquipment(); equipment.slots.weapon = { itemId: FishingToolItemId.birchRod, quantity: 1 }; harness.simulation.applyBag(playerId, { inventory: slots, equipment, vault: [], vaultReserved: [] }); harness.simulation.enqueue(1, { type: "start-fish", characterId: character.character_id, tileX: 9, tileY: 8 }); const events = []; const ticks = Math.ceil(12_000 / 50) + 5; for (let i = 0; i < ticks; i++) { harness.advance(50); const step = harness.simulation.step(); events.push(...(step.events ?? [])); } assert.ok(events.some((e) => e.message === "Your fishing line broke!")); const bag = harness.simulation.bagOf(playerId); assert.ok(bag.inventory.some((s) => s?.itemId === FishingToolItemId.birchPole)); assert.equal(bag.equipment?.slots?.weapon, null); assert.equal(bag.inventory.some((s) => s?.itemId === yarn), false); assert.ok(events.some((e) => e.type === "ground-item-appear" && e.stack?.itemId === yarn ), "yarn should appear on the ground"); } finally { harness.close(); } });