import test from "node:test"; import assert from "node:assert/strict"; import { SpellId, spellDefinition } from "../../shared/definitions/spells.js"; import { SPELL_PROJECTILE_MS, SPELL_COOLDOWN_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"; import { Component } from "../src/ecs/components.js"; import { emptyEquipment, emptySlots } from "../../shared/items/inventory.js"; import { spellIdFromSpellbookStack, spellbookItemIdForSpell, SPELLBOOK_ITEM_DEFINITIONS } from "../../shared/definitions/spellItems.js"; import { itemDefinition } from "../../shared/definitions/items.js"; import { normalizePlayerSpells } from "../../shared/spells/state.js"; import { createSpellbookStack } from "../src/spells/service.js"; import { SkillId } from "../../shared/definitions/skills.js"; import { totalXpForLevel } from "../../shared/skills/progression.js"; import { engageTarget, ensureMobCombat } from "../src/combat/service.js"; function workerOf (simulation, entityId) { for (const worker of simulation.workers.values()) { if (worker.world.ecs.handleOf(entityId) != null) { return worker; } } return null; } 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(); } }); test("spellbook stacks resolve a spell id even without an instance", () => { const itemId = spellbookItemIdForSpell(SpellId.manaBullet); const def = itemDefinition(itemId); assert.ok(def?.defaultInstance?.kind === "spellbook"); assert.equal(spellIdFromSpellbookStack({ itemId, quantity: 1 }, def), SpellId.manaBullet); assert.equal(spellIdFromSpellbookStack({ itemId, quantity: 1, instance: null }, def), SpellId.manaBullet); assert.equal( spellIdFromSpellbookStack({ itemId, quantity: 1, instance: def.defaultInstance }, def), SpellId.manaBullet ); assert.ok(SPELLBOOK_ITEM_DEFINITIONS[itemId]?.consumableSpellId === SpellId.manaBullet); }); test("read-spellbook learns the spell and consumes a catalog-style book", () => { const harness = makeSimulation({ workerCount: 1 }); try { const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); const itemId = spellbookItemIdForSpell(SpellId.manaBullet); const slots = emptySlots(); slots[0] = { itemId, quantity: 1, instance: null }; harness.stateDb.saveBag(character.character_id, { inventory: slots, equipment: emptyEquipment(), vault: [], vaultReserved: [] }); const playerId = harness.simulation.spawnCharacter(1, character); const worker = workerOf(harness.simulation, playerId); const handle = worker.world.ecs.handleOf(playerId); const before = normalizePlayerSpells(worker.world.ecs.get(handle, Component.spells)); assert.equal(before.known.includes(SpellId.manaBullet), false); harness.simulation.enqueue(1, { type: "read-spellbook", characterId: character.character_id, slotIndex: 0 }); const result = harness.simulation.step(); assert.equal( result.events.some((event) => event.type === "command-error"), false, JSON.stringify(result.events) ); assert.ok(result.events.some((event) => event.type === "spells")); const updated = result.events.find((event) => event.type === "spells"); assert.ok(updated.spells.some((spell) => spell.spellId === SpellId.manaBullet)); assert.equal(spellDefinition(SpellId.manaBullet).displayName, "Mana Bullet"); const inv = worker.world.ecs.get(handle, Component.inventory); assert.equal(inv.slots[0], null); const after = normalizePlayerSpells(worker.world.ecs.get(handle, Component.spells)); assert.ok(after.known.includes(SpellId.manaBullet)); } finally { harness.close(); } }); test("read-spellbook with instance stack also works and rejects duplicates", () => { const harness = makeSimulation({ workerCount: 1 }); try { const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); const stack = createSpellbookStack(SpellId.magicMissile); assert.ok(stack?.instance?.kind === "spellbook"); const slots = emptySlots(); slots[0] = stack; slots[1] = structuredClone(stack); harness.stateDb.saveBag(character.character_id, { inventory: slots, equipment: emptyEquipment(), vault: [], vaultReserved: [] }); const playerId = harness.simulation.spawnCharacter(1, character); harness.simulation.enqueue(1, { type: "read-spellbook", characterId: character.character_id, slotIndex: 0 }); const learned = harness.simulation.step(); assert.equal(learned.events.some((event) => event.type === "command-error"), false); harness.simulation.enqueue(1, { type: "read-spellbook", characterId: character.character_id, slotIndex: 1 }); const again = harness.simulation.step(); assert.ok(again.events.some((event) => event.type === "command-error")); const err = again.events.find((event) => event.type === "command-error"); assert.match(err.message ?? "", /already know/i); } finally { harness.close(); } }); test("auto-cast repeats an entity damage spell until cancelled by move", () => { const harness = makeSimulation({ workerCount: 1 }); const random = Math.random; Math.random = () => 0.999; try { const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); const playerId = harness.simulation.spawnCharacter(1, character); const deerId = harness.simulation.spawnEntity(mobComponents({ tileX: 10, tileY: 8, mobTypeId: MobId.deer, level: 40 })); const worker = workerOf(harness.simulation, playerId); const handle = worker.world.ecs.handleOf(playerId); const deerHandle = worker.world.ecs.handleOf(deerId); const deerHealth = worker.world.ecs.get(deerHandle, Component.health); deerHealth.max = 500; deerHealth.current = 500; const spells = worker.world.ecs.get(handle, Component.spells); spells.known.push(SpellId.manaBullet); spells.autoCast = true; const skills = worker.world.ecs.get(handle, Component.skills); skills.xp[SkillId.magic] = totalXpForLevel(20); skills.xp[SkillId.mind] = totalXpForLevel(20); harness.simulation.enqueue(1, { type: "cast-spell-entity", characterId: character.character_id, spellId: SpellId.manaBullet, targetEntityId: deerId }); const first = harness.simulation.step(); assert.ok(first.events.some((e) => e.type === "spell-projectile" || e.type === "spell-fizzle")); const casting = worker.world.ecs.get(handle, Component.spellcasting); assert.equal(casting.repeatSpellId, SpellId.manaBullet); assert.equal(casting.repeatTargetEntityId, deerId); harness.advance(SPELL_COOLDOWN_MS + 10); const second = harness.simulation.step(); assert.ok( second.events.some((e) => e.type === "spell-projectile" || e.type === "spell-fizzle"), `expected auto-cast to fire again after cooldown; got ${second.events.map((e) => e.type).join(",")}` ); assert.equal(worker.world.ecs.get(handle, Component.spellcasting).repeatSpellId, SpellId.manaBullet); harness.simulation.enqueue(1, { type: "move-to", characterId: character.character_id, tileX: 6, tileY: 8 }); harness.simulation.step(); assert.equal(worker.world.ecs.get(handle, Component.spellcasting).repeatSpellId, null); harness.advance(SPELL_COOLDOWN_MS + 10); const afterMove = harness.simulation.step(); assert.equal( afterMove.events.some((e) => e.type === "spell-projectile" && e.spellId === SpellId.manaBullet), false ); } finally { Math.random = random; harness.close(); } }); test("casting a spell clears the walk path", () => { const harness = makeSimulation({ workerCount: 1 }); const random = Math.random; Math.random = () => 0.999; try { const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); const playerId = harness.simulation.spawnCharacter(1, character); const deerId = harness.simulation.spawnEntity(mobComponents({ tileX: 10, tileY: 8, mobTypeId: MobId.deer })); const worker = workerOf(harness.simulation, playerId); const handle = worker.world.ecs.handleOf(playerId); const spells = worker.world.ecs.get(handle, Component.spells); spells.known.push(SpellId.manaBullet); const skills = worker.world.ecs.get(handle, Component.skills); skills.xp[SkillId.magic] = totalXpForLevel(20); harness.simulation.enqueue(1, { type: "move-to", characterId: character.character_id, tileX: 14, tileY: 8 }); harness.simulation.step(); assert.ok(worker.world.ecs.get(handle, Component.path)?.tiles?.length > 0); harness.simulation.enqueue(1, { type: "cast-spell-entity", characterId: character.character_id, spellId: SpellId.manaBullet, targetEntityId: deerId }); harness.simulation.step(); assert.equal(worker.world.ecs.get(handle, Component.path)?.tiles?.length ?? 0, 0); } finally { Math.random = random; harness.close(); } }); test("physical attack cancels spell auto-cast", () => { const harness = makeSimulation({ workerCount: 1 }); const random = Math.random; Math.random = () => 0.999; try { const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); const playerId = harness.simulation.spawnCharacter(1, character); const deerId = harness.simulation.spawnEntity(mobComponents({ tileX: 9, tileY: 8, mobTypeId: MobId.deer })); const worker = workerOf(harness.simulation, playerId); const handle = worker.world.ecs.handleOf(playerId); const spells = worker.world.ecs.get(handle, Component.spells); spells.known.push(SpellId.manaBullet); spells.autoCast = true; const skills = worker.world.ecs.get(handle, Component.skills); skills.xp[SkillId.magic] = totalXpForLevel(20); harness.simulation.enqueue(1, { type: "cast-spell-entity", characterId: character.character_id, spellId: SpellId.manaBullet, targetEntityId: deerId }); harness.simulation.step(); assert.equal(worker.world.ecs.get(handle, Component.spellcasting).repeatSpellId, SpellId.manaBullet); harness.simulation.enqueue(1, { type: "attack", characterId: character.character_id, targetEntityId: deerId }); harness.simulation.step(); assert.equal(worker.world.ecs.get(handle, Component.spellcasting).repeatSpellId, null); } finally { Math.random = random; harness.close(); } }); test("cancel-arm after a cast does not clear auto-repeat", () => { const harness = makeSimulation({ workerCount: 1 }); const random = Math.random; Math.random = () => 0.999; try { const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); const playerId = harness.simulation.spawnCharacter(1, character); const deerId = harness.simulation.spawnEntity(mobComponents({ tileX: 10, tileY: 8, mobTypeId: MobId.deer, level: 40 })); const worker = workerOf(harness.simulation, playerId); const handle = worker.world.ecs.handleOf(playerId); const deerHandle = worker.world.ecs.handleOf(deerId); worker.world.ecs.get(deerHandle, Component.health).max = 500; worker.world.ecs.get(deerHandle, Component.health).current = 500; const spells = worker.world.ecs.get(handle, Component.spells); spells.known.push(SpellId.manaBullet); spells.autoCast = true; const skills = worker.world.ecs.get(handle, Component.skills); skills.xp[SkillId.magic] = totalXpForLevel(20); skills.xp[SkillId.mind] = totalXpForLevel(20); harness.simulation.enqueue(1, { type: "cast-spell-entity", characterId: character.character_id, spellId: SpellId.manaBullet, targetEntityId: deerId }); harness.simulation.step(); assert.equal(worker.world.ecs.get(handle, Component.spellcasting).repeatSpellId, SpellId.manaBullet); /** Late right-click cancel while the client still thought the spell was armed. */ harness.simulation.enqueue(1, { type: "cancel-arm-spell", characterId: character.character_id }); harness.simulation.step(); assert.equal(worker.world.ecs.get(handle, Component.spellcasting).repeatSpellId, SpellId.manaBullet); harness.advance(SPELL_COOLDOWN_MS + 10); const again = harness.simulation.step(); assert.ok( again.events.some((e) => e.type === "spell-projectile" || e.type === "spell-fizzle"), "auto-cast must continue after cancel-arm" ); } finally { Math.random = random; harness.close(); } }); test("re-arming the same auto-cast spell keeps repeating", () => { const harness = makeSimulation({ workerCount: 1 }); const random = Math.random; Math.random = () => 0.999; try { const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); const playerId = harness.simulation.spawnCharacter(1, character); const deerId = harness.simulation.spawnEntity(mobComponents({ tileX: 10, tileY: 8, mobTypeId: MobId.deer, level: 40 })); const worker = workerOf(harness.simulation, playerId); const handle = worker.world.ecs.handleOf(playerId); const deerHandle = worker.world.ecs.handleOf(deerId); worker.world.ecs.get(deerHandle, Component.health).max = 500; worker.world.ecs.get(deerHandle, Component.health).current = 500; const spells = worker.world.ecs.get(handle, Component.spells); spells.known.push(SpellId.manaBullet); spells.autoCast = true; const skills = worker.world.ecs.get(handle, Component.skills); skills.xp[SkillId.magic] = totalXpForLevel(20); skills.xp[SkillId.mind] = totalXpForLevel(20); harness.simulation.enqueue(1, { type: "cast-spell-entity", characterId: character.character_id, spellId: SpellId.manaBullet, targetEntityId: deerId }); harness.simulation.step(); assert.equal(worker.world.ecs.get(handle, Component.spellcasting).repeatSpellId, SpellId.manaBullet); harness.simulation.enqueue(1, { type: "arm-spell", characterId: character.character_id, spellId: SpellId.manaBullet }); harness.simulation.step(); assert.equal(worker.world.ecs.get(handle, Component.spellcasting).repeatSpellId, SpellId.manaBullet); assert.equal(worker.world.ecs.get(handle, Component.spellcasting).armedSpellId, SpellId.manaBullet); } finally { Math.random = random; harness.close(); } }); test("auto-retaliate does not engage while auto-casting an attack spell", () => { const harness = makeSimulation({ workerCount: 1 }); const random = Math.random; Math.random = () => 0.999; try { const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); const playerId = harness.simulation.spawnCharacter(1, character); const deerId = harness.simulation.spawnEntity(mobComponents({ tileX: 10, tileY: 8, mobTypeId: MobId.deer, level: 40 })); const ratId = harness.simulation.spawnEntity(mobComponents({ tileX: 9, tileY: 8, mobTypeId: MobId.rat, spawnerId: null })); const worker = workerOf(harness.simulation, playerId); const handle = worker.world.ecs.handleOf(playerId); const deerHandle = worker.world.ecs.handleOf(deerId); const deerHealth = worker.world.ecs.get(deerHandle, Component.health); deerHealth.max = 500_000; deerHealth.current = 500_000; const spells = worker.world.ecs.get(handle, Component.spells); spells.known.push(SpellId.manaBullet); spells.autoCast = true; const skills = worker.world.ecs.get(handle, Component.skills); skills.xp[SkillId.magic] = totalXpForLevel(20); skills.xp[SkillId.mind] = totalXpForLevel(20); harness.simulation.enqueue(1, { type: "cast-spell-entity", characterId: character.character_id, spellId: SpellId.manaBullet, targetEntityId: deerId }); harness.simulation.step(); assert.equal(worker.world.ecs.get(handle, Component.spellcasting).repeatSpellId, SpellId.manaBullet); assert.equal(worker.world.ecs.get(handle, Component.combat)?.targetEntityId ?? null, null); assert.ok(ensureMobCombat(worker.world.ecs, ratId)); engageTarget(worker.world.ecs, ratId, playerId, harness.now()); let ratSwung = false; for (let i = 0; i < 80; i++) { harness.advance(50); const events = harness.simulation.step().events; if (events.some((event) => event.type === "combat-swing" && event.attackerId === ratId && event.targetId === playerId )) { ratSwung = true; break; } } assert.ok(ratSwung, "expected the rat to swing at the player"); assert.equal( worker.world.ecs.get(handle, Component.combat)?.targetEntityId ?? null, null, "auto-cast is the attack — no melee retaliate engage" ); assert.equal(worker.world.ecs.get(handle, Component.spellcasting).repeatSpellId, SpellId.manaBullet); } finally { Math.random = random; harness.close(); } }); test("death clears active auto-repeat but keeps auto-cast for respawn", () => { const harness = makeSimulation({ workerCount: 1 }); const random = Math.random; Math.random = () => 0.999; try { const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); const playerId = harness.simulation.spawnCharacter(1, character); const deerId = harness.simulation.spawnEntity(mobComponents({ tileX: 10, tileY: 8, mobTypeId: MobId.deer, level: 40 })); const worker = workerOf(harness.simulation, playerId); const handle = worker.world.ecs.handleOf(playerId); const deerHandle = worker.world.ecs.handleOf(deerId); worker.world.ecs.get(deerHandle, Component.health).max = 500; worker.world.ecs.get(deerHandle, Component.health).current = 500; const spells = worker.world.ecs.get(handle, Component.spells); spells.known.push(SpellId.manaBullet); spells.autoCast = true; const skills = worker.world.ecs.get(handle, Component.skills); skills.xp[SkillId.magic] = totalXpForLevel(20); skills.xp[SkillId.mind] = totalXpForLevel(20); harness.simulation.enqueue(1, { type: "cast-spell-entity", characterId: character.character_id, spellId: SpellId.manaBullet, targetEntityId: deerId }); harness.simulation.step(); assert.equal(worker.world.ecs.get(handle, Component.spellcasting).repeatSpellId, SpellId.manaBullet); const killerId = harness.simulation.spawnEntity(mobComponents({ tileX: 9, tileY: 8, mobTypeId: MobId.deer, spawnerId: null })); worker.world.ecs.get(worker.world.ecs.handleOf(killerId), Component.health).current = 1_000_000; worker.world.ecs.get(worker.world.ecs.handleOf(killerId), Component.health).max = 1_000_000; worker.world.ecs.get(handle, Component.health).current = 1; harness.simulation.enqueue(1, { type: "attack", characterId: character.character_id, targetEntityId: killerId }); let died = false; for (let i = 0; i < 100; i++) { harness.advance(1000); const events = harness.simulation.step().events; if (events.some((event) => event.type === "player-died" && event.entityId === playerId)) { died = true; break; } } assert.ok(died, "player should die from retaliation"); assert.equal(worker.world.ecs.get(handle, Component.spells).autoCast, true); assert.equal(worker.world.ecs.get(handle, Component.spellcasting).repeatSpellId, null); assert.equal(worker.world.ecs.get(handle, Component.spellcasting).armedSpellId, null); harness.simulation.enqueue(1, { type: "respawn", characterId: character.character_id }); harness.simulation.step(); assert.equal(worker.world.ecs.has(handle, Component.dead), false); assert.equal(worker.world.ecs.get(handle, Component.spells).autoCast, true); const casting = worker.world.ecs.get(handle, Component.spellcasting); casting.nextCastAt = 0; const pos = worker.world.ecs.get(handle, Component.position); pos.tileX = 8; pos.tileY = 8; worker.world.ecs.get(deerHandle, Component.health).current = 500; harness.simulation.enqueue(1, { type: "cast-spell-entity", characterId: character.character_id, spellId: SpellId.manaBullet, targetEntityId: deerId }); const castResult = harness.simulation.step(); assert.equal( castResult.events.some((event) => event.type === "command-error"), false, castResult.events.find((event) => event.type === "command-error")?.message ?? "cast after respawn should succeed" ); assert.equal( worker.world.ecs.get(handle, Component.spellcasting).repeatSpellId, SpellId.manaBullet, "auto-cast preference still arms repeat after respawn" ); } finally { Math.random = random; harness.close(); } });