| 1 | import test from "node:test"; |
| 2 | import assert from "node:assert/strict"; |
| 3 | import { createEmptyChunkData, encodeChunkBlob } from "../src/world/chunkData.js"; |
| 4 | import { makeCharacter, makeSimulation } from "./helpers/simulationHarness.js"; |
| 5 | import { ItemId } from "../../shared/definitions/items.js"; |
| 6 | import { Component } from "../src/ecs/components.js"; |
| 7 | import |
| 8 | { |
| 9 | normalizeItemSpawnerConfig, |
| 10 | isItemSpawnerConfig, |
| 11 | ItemSpawnerAppearance, |
| 12 | ITEM_SPAWNER_LOCKED_MESSAGE, |
| 13 | ITEM_SPAWNER_OPEN_DURATION_MS |
| 14 | } from "../../shared/definitions/itemSpawners.js"; |
| 15 | import { normalizeSpawnerConfig } from "../../shared/definitions/mobs.js"; |
| 16 | import { emptySlots, emptyEquipment, INVENTORY_SLOTS } from "../../shared/items/inventory.js"; |
| 17 | import { ConditionKind } from "../../shared/quests/conditions.js"; |
| 18 | import |
| 19 | { |
| 20 | itemContainerAtTile, |
| 21 | ensureItemSpawnerState |
| 22 | } from "../src/itemSpawners/service.js"; |
| 23 | |
| 24 | function workerOf (simulation, playerId) |
| 25 | { |
| 26 | const record = simulation.entityRecord(playerId); |
| 27 | return simulation.workers[record.workerId]; |
| 28 | } |
| 29 | |
| 30 | function advanceMs (harness, ms) |
| 31 | { |
| 32 | const ticks = Math.ceil(ms / 50) + 3; |
| 33 | for (let i = 0; i < ticks; i++) |
| 34 | { |
| 35 | harness.advance(50); |
| 36 | harness.simulation.step(); |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | function placeItemSpawner (harness, localX, localY, config) |
| 41 | { |
| 42 | const data = createEmptyChunkData(); |
| 43 | data.open = true; |
| 44 | data.spawners = [{ |
| 45 | localX, |
| 46 | localY, |
| 47 | config: normalizeItemSpawnerConfig({ |
| 48 | spawnerId: `item-${localX}-${localY}`, |
| 49 | ...config |
| 50 | }) |
| 51 | }]; |
| 52 | harness.worldDb.saveChunkVersion( |
| 53 | 0, |
| 54 | 0, |
| 55 | harness.worldDb.getActiveRevisionId(), |
| 56 | true, |
| 57 | encodeChunkBlob(data) |
| 58 | ); |
| 59 | harness.simulation.reloadWorld(); |
| 60 | } |
| 61 | |
| 62 | test("normalizeItemSpawnerConfig auto-switches Appearance Item when multiple stacks", () => |
| 63 | { |
| 64 | const config = normalizeSpawnerConfig({ |
| 65 | spawnerKind: "item", |
| 66 | items: [ |
| 67 | { itemId: ItemId.stick, quantity: 1 }, |
| 68 | { itemId: ItemId.apple, quantity: 2 } |
| 69 | ], |
| 70 | appearance: ItemSpawnerAppearance.item |
| 71 | }, "t1"); |
| 72 | assert.equal(isItemSpawnerConfig(config), true); |
| 73 | assert.equal(config.appearance, ItemSpawnerAppearance.chest); |
| 74 | assert.equal(config.items.length, 2); |
| 75 | assert.equal(config.spawnRangeTiles, 0); |
| 76 | }); |
| 77 | |
| 78 | test("item appearance spawns ground piles that refill after pickup", () => |
| 79 | { |
| 80 | const harness = makeSimulation({ workerCount: 1 }); |
| 81 | try |
| 82 | { |
| 83 | placeItemSpawner(harness, 9, 8, { |
| 84 | appearance: ItemSpawnerAppearance.item, |
| 85 | items: [{ itemId: ItemId.stick, quantity: 3 }], |
| 86 | respawnDelaySeconds: 1, |
| 87 | spawnRangeTiles: 0 |
| 88 | }); |
| 89 | const character = makeCharacter(harness.stateDb, { tileX: 9, tileY: 8 }); |
| 90 | harness.stateDb.saveBag(character.character_id, { |
| 91 | inventory: emptySlots(), |
| 92 | equipment: emptyEquipment(), |
| 93 | vault: [], |
| 94 | vaultReserved: [] |
| 95 | }); |
| 96 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 97 | advanceMs(harness, 200); |
| 98 | |
| 99 | const worker = workerOf(harness.simulation, playerId); |
| 100 | const ground = [...worker.world.groundItems.values()].filter( |
| 101 | (item) => item.source === "item-spawner" || item.itemSpawnerId |
| 102 | ); |
| 103 | assert.ok(ground.length >= 1, "expected spawner ground item"); |
| 104 | const groundItemId = ground[0].groundItemId; |
| 105 | assert.equal(ground[0].stack.itemId, ItemId.stick); |
| 106 | |
| 107 | harness.simulation.enqueue(1, { |
| 108 | type: "pickup-item", |
| 109 | characterId: character.character_id, |
| 110 | groundItemId |
| 111 | }); |
| 112 | harness.simulation.step(); |
| 113 | assert.equal( |
| 114 | [...worker.world.groundItems.values()].filter((i) => i.itemSpawnerId).length, |
| 115 | 0 |
| 116 | ); |
| 117 | |
| 118 | advanceMs(harness, 1500); |
| 119 | const again = [...worker.world.groundItems.values()].filter( |
| 120 | (item) => item.source === "item-spawner" || item.itemSpawnerId |
| 121 | ); |
| 122 | assert.ok(again.length >= 1, "expected respawned ground item"); |
| 123 | } |
| 124 | finally |
| 125 | { harness.close(); } |
| 126 | }); |
| 127 | |
| 128 | test("chest container opens after channel and gives loot", () => |
| 129 | { |
| 130 | const harness = makeSimulation({ workerCount: 1 }); |
| 131 | try |
| 132 | { |
| 133 | placeItemSpawner(harness, 9, 8, { |
| 134 | appearance: ItemSpawnerAppearance.chest, |
| 135 | material: "oak", |
| 136 | items: [{ itemId: ItemId.apple, quantity: 2 }], |
| 137 | respawnDelaySeconds: 60, |
| 138 | spawnRangeTiles: 0 |
| 139 | }); |
| 140 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 141 | harness.stateDb.saveBag(character.character_id, { |
| 142 | inventory: emptySlots(), |
| 143 | equipment: emptyEquipment(), |
| 144 | vault: [], |
| 145 | vaultReserved: [] |
| 146 | }); |
| 147 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 148 | advanceMs(harness, 200); |
| 149 | |
| 150 | const worker = workerOf(harness.simulation, playerId); |
| 151 | const container = itemContainerAtTile(worker.world, 9, 8); |
| 152 | assert.ok(container, "expected chest container"); |
| 153 | assert.equal(container.appearance, "chest"); |
| 154 | assert.equal(container.material, "oak"); |
| 155 | |
| 156 | harness.simulation.enqueue(1, { |
| 157 | type: "open-item-container", |
| 158 | characterId: character.character_id, |
| 159 | containerId: container.containerId |
| 160 | }); |
| 161 | const start = harness.simulation.step(); |
| 162 | assert.ok( |
| 163 | start.events.some((e) => e.type === "tile-shove") |
| 164 | || ensureItemSpawnerState(worker.world).openSessions.has(character.character_id), |
| 165 | "expected open session or shove" |
| 166 | ); |
| 167 | |
| 168 | advanceMs(harness, ITEM_SPAWNER_OPEN_DURATION_MS + 200); |
| 169 | assert.equal(itemContainerAtTile(worker.world, 9, 8), null); |
| 170 | |
| 171 | const handle = worker.world.ecs.handleOf(playerId); |
| 172 | const inventory = worker.world.ecs.get(handle, Component.inventory); |
| 173 | const apples = (inventory?.slots ?? []).filter((s) => s?.itemId === ItemId.apple); |
| 174 | const qty = apples.reduce((n, s) => n + (s.quantity | 0), 0); |
| 175 | assert.equal(qty, 2); |
| 176 | } |
| 177 | finally |
| 178 | { harness.close(); } |
| 179 | }); |
| 180 | |
| 181 | test("locked container toast when condition fails", () => |
| 182 | { |
| 183 | const harness = makeSimulation({ workerCount: 1 }); |
| 184 | try |
| 185 | { |
| 186 | placeItemSpawner(harness, 9, 8, { |
| 187 | appearance: ItemSpawnerAppearance.barrel, |
| 188 | items: [{ itemId: ItemId.stick, quantity: 1 }], |
| 189 | when: { |
| 190 | mode: "allOf", |
| 191 | all: [{ kind: ConditionKind.hasItem, itemId: ItemId.apple, quantity: 1 }] |
| 192 | } |
| 193 | }); |
| 194 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 195 | harness.stateDb.saveBag(character.character_id, { |
| 196 | inventory: emptySlots(), |
| 197 | equipment: emptyEquipment(), |
| 198 | vault: [], |
| 199 | vaultReserved: [] |
| 200 | }); |
| 201 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 202 | advanceMs(harness, 200); |
| 203 | const worker = workerOf(harness.simulation, playerId); |
| 204 | const container = itemContainerAtTile(worker.world, 9, 8); |
| 205 | assert.ok(container); |
| 206 | |
| 207 | harness.simulation.enqueue(1, { |
| 208 | type: "open-item-container", |
| 209 | characterId: character.character_id, |
| 210 | containerId: container.containerId |
| 211 | }); |
| 212 | harness.simulation.step(); |
| 213 | const result = (() => |
| 214 | { |
| 215 | const events = []; |
| 216 | const ticks = Math.ceil(ITEM_SPAWNER_OPEN_DURATION_MS / 50) + 5; |
| 217 | for (let i = 0; i < ticks; i++) |
| 218 | { |
| 219 | harness.advance(50); |
| 220 | events.push(...(harness.simulation.step().events ?? [])); |
| 221 | } |
| 222 | return events; |
| 223 | })(); |
| 224 | const toast = result.find((e) => e.type === "toast" && e.message === ITEM_SPAWNER_LOCKED_MESSAGE); |
| 225 | assert.ok(toast, JSON.stringify(result.filter((e) => e.type === "toast"))); |
| 226 | assert.ok(itemContainerAtTile(worker.world, 9, 8), "locked open must not despawn container"); |
| 227 | } |
| 228 | finally |
| 229 | { harness.close(); } |
| 230 | }); |
| 231 | |
| 232 | test("opening on the container tile is rejected", () => |
| 233 | { |
| 234 | const harness = makeSimulation({ workerCount: 1 }); |
| 235 | try |
| 236 | { |
| 237 | placeItemSpawner(harness, 9, 8, { |
| 238 | appearance: ItemSpawnerAppearance.crate, |
| 239 | items: [{ itemId: ItemId.stick, quantity: 1 }] |
| 240 | }); |
| 241 | const character = makeCharacter(harness.stateDb, { tileX: 9, tileY: 8 }); |
| 242 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 243 | advanceMs(harness, 200); |
| 244 | const worker = workerOf(harness.simulation, playerId); |
| 245 | const container = itemContainerAtTile(worker.world, 9, 8); |
| 246 | assert.ok(container); |
| 247 | |
| 248 | harness.simulation.enqueue(1, { |
| 249 | type: "open-item-container", |
| 250 | characterId: character.character_id, |
| 251 | containerId: container.containerId |
| 252 | }); |
| 253 | const step = harness.simulation.step(); |
| 254 | const err = step.events.find((e) => e.type === "command-error"); |
| 255 | assert.ok(err, JSON.stringify(step.events)); |
| 256 | assert.match(err.message, /next to the container/i); |
| 257 | } |
| 258 | finally |
| 259 | { harness.close(); } |
| 260 | }); |