import test from "node:test"; import assert from "node:assert/strict"; import { BlockType } from "../../shared/definitions/tiles.js"; import { ItemId } from "../../shared/definitions/items.js"; import { SkillId } from "../../shared/definitions/skills.js"; import { isAdjacentTile, canTalkOrShopNear } from "../../shared/navigation/adjacency.js"; import { totalXpForLevel } from "../../shared/skills/progression.js"; import { makeCharacter, makeSimulation } from "./helpers/simulationHarness.js"; import { Component } from "../src/ecs/components.js"; test("isAdjacentTile excludes same tile and allows neighbours", () => { assert.equal(isAdjacentTile(5, 5, 5, 5), false); assert.equal(isAdjacentTile(5, 5, 6, 5), true); assert.equal(isAdjacentTile(5, 5, 7, 5), false); }); test("canTalkOrShopNear allows standing across a table or marketplace counter", () => { const blocks = new Map([ ["10,8", BlockType.table], ["20,8", BlockType.marketplace] ]); const getBlock = (x, y) => blocks.get(`${x},${y}`) ?? null; // NPC at 10,9; table at 10,8; player at 10,7 — over the table. assert.equal(canTalkOrShopNear(10, 7, 10, 9, getBlock), true); assert.equal(isAdjacentTile(10, 7, 10, 9), false); // Direct adjacency still works without furniture. assert.equal(canTalkOrShopNear(11, 9, 10, 9, getBlock), true); // Two tiles away with no counter is too far. assert.equal(canTalkOrShopNear(10, 7, 10, 9, () => null), false); // Marketplace counter works the same way. assert.equal(canTalkOrShopNear(20, 7, 20, 9, getBlock), true); }); 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 }; } function placeFlower (worker, tileX, tileY) { 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.flower, state: "red", config: null }; } test("mining rejects standing on the ore boulder", () => { const harness = makeSimulation({ workerCount: 1 }); try { const character = makeCharacter(harness.stateDb, { tileX: 9, tileY: 8 }); const playerId = harness.simulation.spawnCharacter(1, character); const record = harness.simulation.entityRecord(playerId); const worker = harness.simulation.workers[record.workerId]; const handle = worker.world.ecs.handleOf(playerId); worker.world.ecs.get(handle, Component.inventory).slots[0] = { itemId: ItemId.bronzePickaxe, quantity: 1 }; const skills = worker.world.ecs.get(handle, Component.skills); skills.xp[SkillId.mining] = totalXpForLevel(10); placeOre(worker, 9, 8, "iron-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" && /stand next/i.test(event.message ?? ""))); } finally { harness.close(); } }); test("picking rejects standing on the plant", () => { const harness = makeSimulation({ workerCount: 1 }); try { const character = makeCharacter(harness.stateDb, { tileX: 9, tileY: 8 }); const playerId = harness.simulation.spawnCharacter(1, character); const record = harness.simulation.entityRecord(playerId); const worker = harness.simulation.workers[record.workerId]; placeFlower(worker, 9, 8); harness.simulation.enqueue(1, { type: "pick", characterId: character.character_id, tileX: 9, tileY: 8 }); const result = harness.simulation.step(); assert.ok(result.events.some((event) => event.type === "command-error" && /stand next/i.test(event.message ?? ""))); } finally { harness.close(); } });