BlueGit

timfa questward

Files

questward / server / tests / spells.test.js

spells.test.js

spells.test.js 7.5 KiB Download Raw
1import test from "node:test";
2import assert from "node:assert/strict";
3import { SpellId, spellDefinition } from "../../shared/definitions/spells.js";
4import { SPELL_PROJECTILE_MS } from "../../shared/spells/constants.js";
5import { mobComponents } from "../src/simulation/spawners.js";
6import { MobId } from "../../shared/definitions/mobs.js";
7import { makeCharacter, makeSimulation } from "./helpers/simulationHarness.js";
8import { Component } from "../src/ecs/components.js";
9import { emptyEquipment, emptySlots } from "../../shared/items/inventory.js";
10import
11 {
12 spellIdFromSpellbookStack,
13 spellbookItemIdForSpell,
14 SPELLBOOK_ITEM_DEFINITIONS
15 } from "../../shared/definitions/spellItems.js";
16import { itemDefinition } from "../../shared/definitions/items.js";
17import { normalizePlayerSpells } from "../../shared/spells/state.js";
18import { createSpellbookStack } from "../src/spells/service.js";
19
20function workerOf (simulation, entityId)
21{
22 for (const worker of simulation.workers.values())
23 {
24 if (worker.world.ecs.handleOf(entityId) != null)
25 { return worker; }
26 }
27 return null;
28}
29
30test("cast light resolves without command error and can place a spell light", () =>
31{
32 const harness = makeSimulation({ workerCount: 1 });
33 try
34 {
35 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
36 harness.simulation.spawnCharacter(1, character);
37
38 harness.simulation.enqueue(1, {
39 type: "cast-spell-tile",
40 characterId: character.character_id,
41 spellId: SpellId.light,
42 tileX: 10,
43 tileY: 8
44 });
45 const cast = harness.simulation.step();
46 assert.equal(
47 cast.events.some((event) => event.type === "command-error"),
48 false,
49 JSON.stringify(cast.events)
50 );
51 assert.ok(
52 cast.events.some((event) => event.type === "spell-fizzle" || event.type === "spell-projectile"),
53 "expected fizzle or projectile"
54 );
55
56 if (!cast.events.some((event) => event.type === "spell-projectile"))
57 { return; }
58
59 harness.advance(SPELL_PROJECTILE_MS + 50);
60 const impact = harness.simulation.step();
61 assert.ok(
62 impact.events.some((event) => event.type === "spell-light-appear"),
63 JSON.stringify(impact.events.map((event) => event.type))
64 );
65 }
66 finally
67 { harness.close(); }
68});
69
70test("out-of-range spell cast paths toward the nearest in-range tile", () =>
71{
72 const harness = makeSimulation({ workerCount: 1 });
73 try
74 {
75 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
76 const playerId = harness.simulation.spawnCharacter(1, character);
77 harness.simulation.enqueue(1, {
78 type: "cast-spell-tile",
79 characterId: character.character_id,
80 spellId: SpellId.light,
81 tileX: 30,
82 tileY: 8
83 });
84 const cast = harness.simulation.step();
85 assert.equal(cast.events.some((event) => event.type === "command-error"), false);
86 const start = harness.simulation.entityRecord(playerId);
87 for (let i = 0; i < 80; i++)
88 { harness.simulation.step(); }
89 const moved = harness.simulation.entityRecord(playerId);
90 assert.ok(
91 moved.tileX !== start.tileX || moved.tileY !== start.tileY,
92 `expected movement from (${start.tileX},${start.tileY}), got (${moved.tileX},${moved.tileY})`
93 );
94 }
95 finally
96 { harness.close(); }
97});
98
99test("attack from out of range paths toward the target", () =>
100{
101 const harness = makeSimulation({ workerCount: 1 });
102 try
103 {
104 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
105 const playerId = harness.simulation.spawnCharacter(1, character);
106 const deerId = harness.simulation.spawnEntity(mobComponents({
107 tileX: 24,
108 tileY: 8,
109 mobTypeId: MobId.deer
110 }));
111 harness.simulation.enqueue(1, {
112 type: "attack",
113 characterId: character.character_id,
114 targetEntityId: deerId
115 });
116 harness.simulation.step();
117 const record = harness.simulation.entityRecord(playerId);
118 assert.ok(record.tileX < 24);
119 for (let i = 0; i < 80 && record.tileX < 20; i++)
120 { harness.simulation.step(); }
121 assert.ok(harness.simulation.entityRecord(playerId).tileX > 8);
122 }
123 finally
124 { harness.close(); }
125});
126
127test("spellbook stacks resolve a spell id even without an instance", () =>
128{
129 const itemId = spellbookItemIdForSpell(SpellId.manaBullet);
130 const def = itemDefinition(itemId);
131 assert.ok(def?.defaultInstance?.kind === "spellbook");
132 assert.equal(spellIdFromSpellbookStack({ itemId, quantity: 1 }, def), SpellId.manaBullet);
133 assert.equal(spellIdFromSpellbookStack({ itemId, quantity: 1, instance: null }, def), SpellId.manaBullet);
134 assert.equal(
135 spellIdFromSpellbookStack({ itemId, quantity: 1, instance: def.defaultInstance }, def),
136 SpellId.manaBullet
137 );
138 assert.ok(SPELLBOOK_ITEM_DEFINITIONS[itemId]?.consumableSpellId === SpellId.manaBullet);
139});
140
141test("read-spellbook learns the spell and consumes a catalog-style book", () =>
142{
143 const harness = makeSimulation({ workerCount: 1 });
144 try
145 {
146 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
147 const itemId = spellbookItemIdForSpell(SpellId.manaBullet);
148 const slots = emptySlots();
149 slots[0] = { itemId, quantity: 1, instance: null };
150 harness.stateDb.saveBag(character.character_id, {
151 inventory: slots,
152 equipment: emptyEquipment(),
153 vault: [],
154 vaultReserved: []
155 });
156 const playerId = harness.simulation.spawnCharacter(1, character);
157 const worker = workerOf(harness.simulation, playerId);
158 const handle = worker.world.ecs.handleOf(playerId);
159 const before = normalizePlayerSpells(worker.world.ecs.get(handle, Component.spells));
160 assert.equal(before.known.includes(SpellId.manaBullet), false);
161
162 harness.simulation.enqueue(1, {
163 type: "read-spellbook",
164 characterId: character.character_id,
165 slotIndex: 0
166 });
167 const result = harness.simulation.step();
168 assert.equal(
169 result.events.some((event) => event.type === "command-error"),
170 false,
171 JSON.stringify(result.events)
172 );
173 assert.ok(result.events.some((event) => event.type === "spells"));
174 const updated = result.events.find((event) => event.type === "spells");
175 assert.ok(updated.spells.some((spell) => spell.spellId === SpellId.manaBullet));
176 assert.equal(spellDefinition(SpellId.manaBullet).displayName, "Mana Bullet");
177
178 const inv = worker.world.ecs.get(handle, Component.inventory);
179 assert.equal(inv.slots[0], null);
180 const after = normalizePlayerSpells(worker.world.ecs.get(handle, Component.spells));
181 assert.ok(after.known.includes(SpellId.manaBullet));
182 }
183 finally
184 { harness.close(); }
185});
186
187test("read-spellbook with instance stack also works and rejects duplicates", () =>
188{
189 const harness = makeSimulation({ workerCount: 1 });
190 try
191 {
192 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
193 const stack = createSpellbookStack(SpellId.magicMissile);
194 assert.ok(stack?.instance?.kind === "spellbook");
195 const slots = emptySlots();
196 slots[0] = stack;
197 slots[1] = structuredClone(stack);
198 harness.stateDb.saveBag(character.character_id, {
199 inventory: slots,
200 equipment: emptyEquipment(),
201 vault: [],
202 vaultReserved: []
203 });
204 const playerId = harness.simulation.spawnCharacter(1, character);
205
206 harness.simulation.enqueue(1, {
207 type: "read-spellbook",
208 characterId: character.character_id,
209 slotIndex: 0
210 });
211 const learned = harness.simulation.step();
212 assert.equal(learned.events.some((event) => event.type === "command-error"), false);
213
214 harness.simulation.enqueue(1, {
215 type: "read-spellbook",
216 characterId: character.character_id,
217 slotIndex: 1
218 });
219 const again = harness.simulation.step();
220 assert.ok(again.events.some((event) => event.type === "command-error"));
221 const err = again.events.find((event) => event.type === "command-error");
222 assert.match(err.message ?? "", /already know/i);
223 }
224 finally
225 { harness.close(); }
226});