import test from "node:test"; import assert from "node:assert/strict"; import { SpellId } from "../../shared/definitions/spells.js"; import { SPELL_PROJECTILE_MS } from "../../shared/spells/constants.js"; import { mobComponents } from "../src/simulation/spawners.js"; import { MobId } from "../../shared/definitions/mobs.js"; import { makeCharacter, makeSimulation } from "./helpers/simulationHarness.js"; test("cast light resolves without command error and can place a spell light", () => { const harness = makeSimulation({ workerCount: 1 }); try { const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); harness.simulation.spawnCharacter(1, character); harness.simulation.enqueue(1, { type: "cast-spell-tile", characterId: character.character_id, spellId: SpellId.light, tileX: 10, tileY: 8 }); const cast = harness.simulation.step(); assert.equal( cast.events.some((event) => event.type === "command-error"), false, JSON.stringify(cast.events) ); assert.ok( cast.events.some((event) => event.type === "spell-fizzle" || event.type === "spell-projectile"), "expected fizzle or projectile" ); if (!cast.events.some((event) => event.type === "spell-projectile")) { return; } harness.advance(SPELL_PROJECTILE_MS + 50); const impact = harness.simulation.step(); assert.ok( impact.events.some((event) => event.type === "spell-light-appear"), JSON.stringify(impact.events.map((event) => event.type)) ); } finally { harness.close(); } }); test("out-of-range spell cast paths toward the nearest in-range tile", () => { const harness = makeSimulation({ workerCount: 1 }); try { const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); const playerId = harness.simulation.spawnCharacter(1, character); harness.simulation.enqueue(1, { type: "cast-spell-tile", characterId: character.character_id, spellId: SpellId.light, tileX: 30, tileY: 8 }); const cast = harness.simulation.step(); assert.equal(cast.events.some((event) => event.type === "command-error"), false); const start = harness.simulation.entityRecord(playerId); for (let i = 0; i < 80; i++) { harness.simulation.step(); } const moved = harness.simulation.entityRecord(playerId); assert.ok( moved.tileX !== start.tileX || moved.tileY !== start.tileY, `expected movement from (${start.tileX},${start.tileY}), got (${moved.tileX},${moved.tileY})` ); } finally { harness.close(); } }); test("attack from out of range paths toward the target", () => { const harness = makeSimulation({ workerCount: 1 }); try { const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); const playerId = harness.simulation.spawnCharacter(1, character); const deerId = harness.simulation.spawnEntity(mobComponents({ tileX: 24, tileY: 8, mobTypeId: MobId.deer })); harness.simulation.enqueue(1, { type: "attack", characterId: character.character_id, targetEntityId: deerId }); harness.simulation.step(); const record = harness.simulation.entityRecord(playerId); assert.ok(record.tileX < 24); for (let i = 0; i < 80 && record.tileX < 20; i++) { harness.simulation.step(); } assert.ok(harness.simulation.entityRecord(playerId).tileX > 8); } finally { harness.close(); } });