import test from "node:test"; import assert from "node:assert/strict"; import { NeighborBit, computeNeighborConnectMask, neighborConnectStateName, parseNeighborConnectMask, composeNeighborConnectState, neighborConnectBlocksMatch, parseMaterialNeighborConnectState } from "../../shared/world/neighborConnect.js"; import { CENTRAL_EXCHANGE_STAFF, pickExchangeClerk } from "../../shared/market/exchange.js"; import { MobId, normalizeSpawnerConfig, isCentralExchangeSpawner } from "../../shared/definitions/mobs.js"; import { BlockType, BLOCK_DEFINITIONS } from "../../shared/definitions/tiles.js"; import { ItemId } from "../../shared/definitions/items.js"; import { emptyEquipment, emptySlots, addCoinsToSlots } from "../../shared/items/inventory.js"; import { makeCharacter, makeSimulation } from "./helpers/simulationHarness.js"; import { createEmptyChunkData, encodeChunkBlob } from "../src/world/chunkData.js"; import { CHUNK_SIZE } from "../../shared/protocol/constants.js"; import { Component } from "../src/ecs/components.js"; test("neighbor connect masks cover all NESW combinations", () => { assert.equal(neighborConnectStateName(0), "0"); assert.equal(neighborConnectStateName(NeighborBit.n | NeighborBit.e | NeighborBit.s | NeighborBit.w), "15"); assert.equal(parseNeighborConnectMask("6"), NeighborBit.e | NeighborBit.s); const mask = computeNeighborConnectMask((dx, dy) => dx === 1 || dy === -1); assert.equal(mask, NeighborBit.n | NeighborBit.e); }); test("material neighbor-connect states keep wood id and only match same material", () => { const def = BLOCK_DEFINITIONS[BlockType.table]; assert.equal(composeNeighborConnectState(def, "oak", 3), "oak-3"); assert.deepEqual(parseMaterialNeighborConnectState("birch-10", def), { material: "birch", mask: 10 }); const pine = { typeId: BlockType.table, state: "pine-0" }; const pineNeighbor = { typeId: BlockType.table, state: "pine-2" }; const oakNeighbor = { typeId: BlockType.table, state: "oak-2" }; assert.equal(neighborConnectBlocksMatch(pine, pineNeighbor, def), true); assert.equal(neighborConnectBlocksMatch(pine, oakNeighbor, def), false); }); test("CE staff entries include sex labels and pickExchangeClerk returns a name", () => { assert.ok(CENTRAL_EXCHANGE_STAFF.length >= 2); for (const staff of CENTRAL_EXCHANGE_STAFF) { assert.ok(staff.name); assert.ok(staff.sex === "male" || staff.sex === "female"); } const name = pickExchangeClerk(() => 0); assert.equal(name, CENTRAL_EXCHANGE_STAFF[0].name); }); test("central exchange spawner config locks staff count and strips shop options", () => { const config = normalizeSpawnerConfig({ mobTypeId: MobId.centralExchange, maxAlive: 2, sells: [{ itemId: ItemId.apple, quantity: 1 }], buysItems: true, displayName: "ignored" }, "ce:0"); assert.equal(config.centralExchange, true); assert.equal(config.maxAlive, CENTRAL_EXCHANGE_STAFF.length); assert.equal(config.sells.length, 0); assert.equal(config.buysItems, false); assert.equal(isCentralExchangeSpawner(config), true); }); test("central exchange spawner places every clerk with matching names", () => { const harness = makeSimulation({ workerCount: 1 }); try { const data = createEmptyChunkData(); data.open = true; data.blocks[8 * CHUNK_SIZE + 9] = { typeId: BlockType.marketplace, state: "0" }; data.blocks[8 * CHUNK_SIZE + 10] = { typeId: BlockType.bank, state: null }; data.spawners = [{ localX: 8, localY: 8, config: normalizeSpawnerConfig({ mobTypeId: MobId.centralExchange, spawnRangeTiles: 6, maxWanderRangeTiles: 6, equipment: { chest: ItemId.clothShirt } }, "ce-test") }]; harness.worldDb.saveChunkVersion(0, 0, harness.worldDb.getActiveRevisionId(), true, encodeChunkBlob(data)); harness.simulation.reloadWorld(); harness.advance(50); for (let i = 0; i < 30; i++) { harness.simulation.step(); } const record = harness.simulation.entityRecord( harness.simulation.spawnCharacter(1, makeCharacter(harness.stateDb, { tileX: 7, tileY: 7 })) ); const worker = harness.simulation.workers[record.workerId]; for (let i = 0; i < 40; i++) { harness.simulation.step(); } const clerks = []; for (const entity of worker.world.ecs.query(Component.spawnOrigin, Component.appearance)) { if (!entity.spawnOrigin?.centralExchange) { continue; } clerks.push(entity.appearance.firstName); } assert.equal(clerks.length, CENTRAL_EXCHANGE_STAFF.length); for (const staff of CENTRAL_EXCHANGE_STAFF) { assert.ok(clerks.includes(staff.name), staff.name); } } finally { harness.close(); } }); test("opening the market stations a CE clerk on the opposite side of the counter", () => { const harness = makeSimulation({ workerCount: 1 }); try { const data = createEmptyChunkData(); data.open = true; // Counter at (9,8); player will stand at (8,8); staff station = (10,8). data.blocks[8 * CHUNK_SIZE + 9] = { typeId: BlockType.marketplace, state: "0" }; data.blocks[8 * CHUNK_SIZE + 11] = { typeId: BlockType.bank, state: null }; data.spawners = [{ localX: 12, localY: 8, config: normalizeSpawnerConfig({ mobTypeId: MobId.centralExchange, spawnRangeTiles: 8, maxWanderRangeTiles: 8 }, "ce-station") }]; harness.worldDb.saveChunkVersion(0, 0, harness.worldDb.getActiveRevisionId(), true, encodeChunkBlob(data)); harness.simulation.reloadWorld(); const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); const funded = addCoinsToSlots(emptySlots(), 10); harness.stateDb.saveBag(character.character_id, { inventory: funded.slots, equipment: emptyEquipment(), vault: [], vaultReserved: [] }); const playerId = harness.simulation.spawnCharacter(1, character); for (let i = 0; i < 40; i++) { harness.simulation.step(); } harness.simulation.enqueue(1, { type: "open-market", characterId: character.character_id }); harness.simulation.step(); const record = harness.simulation.entityRecord(playerId); const worker = harness.simulation.workers[record.workerId]; let stationed = false; for (const entity of worker.world.ecs.query(Component.spawnOrigin)) { if (!entity.spawnOrigin?.centralExchange) { continue; } if (entity.spawnOrigin.stationTileX === 10 && entity.spawnOrigin.stationTileY === 8) { stationed = true; } } assert.equal(stationed, true); } finally { harness.close(); } });