Files
wizwar6e/packages/engine/test/durations-doors-cards.test.ts
T
Eric WagonerandClaude Fable 5 695307daa8 Credibility pass: one voice, no scars
A three-reviewer sweep for tells of piecemeal machine generation,
every finding verified against the code before touching it. No
behavior changes; the full suite passes unchanged (plus two
strengthened pins).

Engine: removed four void-silenced fossils (a parseEdgeKey call
voided where it stood, stoneEffect's ignored cardId parameter, the
actualTarget remnant in doCast, a voided loop variable in shadow
upkeep); fixed the initialize-then-overwrite narration in
spawnCreature; replaced a filter(() => false) no-op; waterwall now
rides waveFromEdge instead of carrying its own verbatim copy (and the
single-caller washBack wrapper went with it); blind wall-bumps and
LOS blockers each collapsed to one implementation; the wand-id list
and the "permanent" duration sentinel became named constants; the
ambush number local no longer shadows the imported numberValue
function; assorted reviewer-aimed phrasings rewritten as the
constraints they guard.

Server/deploy: the protocol header now documents all eleven message
types; dropped an eslint pragma with no eslint, a test script with no
tests, and an rsync exclude anchored at a path that never existed
(the real data/ dir now excluded); the Caddy vhost has one source of
truth; stale "pending DNS" note removed — the record resolves.

Web: ~90 lines of CSS swallowed verbatim into a mobile media query
deduplicated; the reduced-motion guard on the board now actually
stops the marked-cell pulse; one shared color module replaces two
drifted palettes; an orphaned doc comment rejoined its function.

Tests: the ten-times-pasted helper block became test/helpers.ts;
wave-numbered files renamed for the behaviors they pin; deliberation
comments and void-ed corpses of unwritten assertions deleted; silent
seed-dependent early-returns now fail loudly; one assertion that
compared a value to itself now pins the home-translation it meant to;
the stored-log single-number command form gained the explicit
compatibility test it deserved.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-08-16 11:56:55 -04:00

349 lines
16 KiB
TypeScript

import { describe, expect, it } from "vitest";
import { applyCommand, activePlayer, boardView, sustainedOn, type GameState } from "../src/game";
import { cellKey, edgeKey, neighbor, type Side } from "../src/board";
import type { CardInstance } from "../src/cards";
import { newGame, must, giveCard, toRound2, faceOff, castAt } from "./helpers";
describe("duration spells", () => {
it("slow reduces movement to 1, blocks number cards, and halves attacks", () => {
let { state } = newGame();
state = toRound2(state);
const { attacker, defender } = faceOff(state);
const slow = giveCard(state, attacker, "slow");
giveCard(state, attacker, "number-4", "N", 1);
state = castAt(state, attacker, defender, slow, { numberInstanceIds: ["number-4#N"] });
expect(sustainedOn(state, defender, "slow").length).toBe(1);
// Defender's turn: 1 movement, no number cards, no attack (1st slowed turn).
state = must(state, attacker, { type: "endTurn", draw: 0 });
expect(activePlayer(state).id).toBe(defender);
expect(state.turn.movementAllowance).toBe(1);
expect(state.turn.attackForbidden).toBe(true);
const num = giveCard(state, defender, "number-3", "M", 0);
expect(applyCommand(state, defender, { type: "playNumberForMovement", instanceId: num.instanceId }).ok).toBe(false);
// Second slowed turn: attack allowed again.
state = must(state, defender, { type: "endTurn", draw: 0 });
state = must(state, attacker, { type: "endTurn", draw: 0 });
expect(activePlayer(state).id).toBe(defender);
expect(state.turn.attackForbidden).toBe(false);
});
it("duration spells expire at the start of the caster's turn", () => {
let { state } = newGame();
state = toRound2(state);
const { attacker, defender } = faceOff(state);
const ns = giveCard(state, attacker, "no-spell");
// Duration 1 (no number card).
state = castAt(state, attacker, defender, ns);
expect(sustainedOn(state, defender, "no-spell").length).toBe(1);
// Defender cannot cast while it lasts.
const fb = giveCard(state, defender, "fireball", "F", 1);
state = must(state, attacker, { type: "endTurn", draw: 0 });
const refused = applyCommand(state, defender, {
type: "cast", instanceId: fb.instanceId, target: { kind: "player", playerId: attacker },
});
expect(refused.ok).toBe(false);
// Back to the caster: the spell expires at their turn start.
state = must(state, defender, { type: "endTurn", draw: 0 });
expect(sustainedOn(state, defender, "no-spell").length).toBe(0);
});
it("medusa paralyzes and grants damage immunity", () => {
let { state } = newGame();
state = toRound2(state);
const { attacker, defender } = faceOff(state);
const med = giveCard(state, attacker, "medusa");
giveCard(state, attacker, "number-2", "N", 1);
// Duration 2 so it survives past the caster's next turn start.
state = castAt(state, attacker, defender, med, { numberInstanceIds: ["number-2#N"] });
// Defender is immune to damage while paralyzed.
state = must(state, attacker, { type: "endTurn", draw: 0 });
expect(activePlayer(state).id).toBe(defender);
expect(applyCommand(state, defender, { type: "move", direction: "N" }).ok).toBe(false);
state = must(state, defender, { type: "endTurn", draw: 0 });
// Attacker punches the frozen defender: no damage.
state = must(state, attacker, { type: "punch", targetId: defender });
// Defender cannot counteract under medusa; they pass.
state = must(state, defender, { type: "pass" });
expect(state.players.find((p) => p.id === defender)!.life).toBe(15);
});
it("invisible makes attacks miss 3 times out of 4 (deterministic per seed)", () => {
let hits = 0, misses = 0;
for (let seed = 1; seed <= 12; seed++) {
let { state } = newGame(seed);
state = toRound2(state);
const { attacker, defender } = faceOff(state);
const inv = giveCard(state, defender, "invisible", "I", 0);
// Defender casts invisible on their own turn first — rearrange: give
// attacker the attack, let defender cast invisible when active.
// Simpler: attach directly via a cast from the defender's turn.
state.players.find((p) => p.id === defender)!.hand[0] = inv;
// Attacker ends turn; defender casts invisible; attacker attacks.
state = must(state, attacker, { type: "endTurn", draw: 0 });
state = must(state, defender, { type: "cast", instanceId: inv.instanceId });
state = must(state, defender, { type: "endTurn", draw: 0 });
const fb = giveCard(state, attacker, "fireball", "F", 0);
state = castAt(state, attacker, defender, fb);
const life = state.players.find((p) => p.id === defender)!.life;
if (life < 15) hits++;
else misses++;
}
expect(hits + misses).toBe(12);
expect(misses).toBeGreaterThan(hits); // 75% miss rate over 12 seeds
});
});
describe("doors", () => {
function findDoor(state: GameState): { cell: { x: number; y: number }; side: Side } {
const view = boardView(state);
for (const [key, edgeState] of Object.entries(view.edges)) {
if (edgeState !== "door") continue;
const [kind, coords] = key.split(":") as [string, string];
const [x, y] = coords.split(",").map(Number) as [number, number];
return kind === "V" ? { cell: { x, y }, side: "E" } : { cell: { x, y }, side: "S" };
}
throw new Error("no door on this board");
}
it("pick lock opens an adjacent door until end of turn", () => {
let { state } = newGame();
const me = activePlayer(state);
const door = findDoor(state);
me.position = { ...door.cell };
const other = neighbor(door.cell, door.side);
const dir = door.side;
// Locked: blocked.
expect(applyCommand(state, me.id, { type: "move", direction: dir }).ok).toBe(false);
const pl = giveCard(state, me.id, "pick-lock");
state = must(state, me.id, {
type: "cast", instanceId: pl.instanceId,
target: { kind: "edge", cell: door.cell, side: door.side },
});
state = must(state, me.id, { type: "move", direction: dir });
expect(cellKey(activePlayer(state).position)).toBe(cellKey(other));
// Relocks when the turn ends.
state = must(state, me.id, { type: "endTurn", draw: 0 });
state = must(state, activePlayer(state).id, { type: "endTurn", draw: 0 });
expect(state.openDoorEdges.length).toBe(0);
expect(applyCommand(state, me.id, { type: "move", direction: dir === "E" ? "W" : "N" }).ok).toBe(false);
});
it("remove lock is permanent; jam lock seals the door for everyone", () => {
let { state } = newGame();
const me = activePlayer(state);
const door = findDoor(state);
me.position = { ...door.cell };
const key = edgeKey(door.cell, door.side);
const rl = giveCard(state, me.id, "remove-lock");
state = must(state, me.id, {
type: "cast", instanceId: rl.instanceId,
target: { kind: "edge", cell: door.cell, side: door.side },
});
expect(state.doorStates[key]).toBe("removed");
state = must(state, me.id, { type: "move", direction: door.side });
// Jamming a removed lock is refused.
const jl = giveCard(state, me.id, "jam-lock");
const refused = applyCommand(state, me.id, {
type: "cast", instanceId: jl.instanceId,
target: { kind: "edge", cell: door.cell, side: door.side },
});
expect(refused.ok).toBe(false);
});
});
describe("movement spells", () => {
it("teleport jumps up to four spaces through walls and ends movement", () => {
let { state } = newGame();
const me = activePlayer(state);
const from = me.position;
const tp = giveCard(state, me.id, "teleport");
const far = { x: from.x, y: from.y >= 4 ? from.y - 4 : from.y + 4 };
state = must(state, me.id, {
type: "cast", instanceId: tp.instanceId, target: { kind: "cell", cell: far },
});
expect(cellKey(activePlayer(state).position)).toBe(cellKey(far));
expect(applyCommand(state, me.id, { type: "move", direction: "N" }).ok).toBe(false);
});
it("teleport refuses jumps beyond four spaces", () => {
const { state } = newGame();
const me = activePlayer(state);
const tp = giveCard(state, me.id, "teleport");
const tooFar = { x: me.position.x, y: me.position.y >= 5 ? me.position.y - 5 : me.position.y + 5 };
const result = applyCommand(state, me.id, {
type: "cast", instanceId: tp.instanceId, target: { kind: "cell", cell: tooFar },
});
expect(result.ok).toBe(false);
});
it("swap trades places and consumes movement", () => {
let { state } = newGame();
state = toRound2(state);
const attacker = activePlayer(state);
const defender = state.players.find((p) => p.id !== attacker.id)!;
const aPos = { ...attacker.position };
const bPos = { ...defender.position };
const sw = giveCard(state, attacker.id, "swap");
state = castAt(state, attacker.id, defender.id, sw);
expect(cellKey(state.players.find((p) => p.id === attacker.id)!.position)).toBe(cellKey(bPos));
expect(cellKey(state.players.find((p) => p.id === defender.id)!.position)).toBe(cellKey(aPos));
expect(state.turn.movementUsed).toBe(state.turn.movementAllowance);
});
it("power run trades life for movement", () => {
let { state } = newGame();
const me = activePlayer(state);
const pr = giveCard(state, me.id, "power-run");
state = must(state, me.id, {
type: "cast", instanceId: pr.instanceId, params: { points: 3 },
});
expect(state.players.find((p) => p.id === me.id)!.life).toBe(12);
expect(state.turn.movementAllowance).toBe(6);
});
it("pass through wall grants a one-wall step", () => {
let { state } = newGame();
const me = activePlayer(state);
// Find a direction blocked by a wall with a real cell behind it.
const view = boardView(state);
let dir: Side | null = null;
for (const side of ["N", "S", "E", "W"] as Side[]) {
const k = edgeKey(me.position, side);
if (view.edges[k] === "wall" && view.cells[cellKey(neighbor(me.position, side))]) {
dir = side;
break;
}
}
if (!dir) throw new Error("setup: seed 42 lost its adjacent wall");
const ptw = giveCard(state, me.id, "pass-through-wall");
state = must(state, me.id, { type: "cast", instanceId: ptw.instanceId });
state = must(state, me.id, { type: "move", direction: dir });
expect(state.players.find((p) => p.id === me.id)!.passWallCharges).toBe(0);
});
});
describe("card warfare", () => {
it("card erasure discards a named card; thought steal takes two at random", () => {
let { state } = newGame();
state = toRound2(state);
const { attacker, defender } = faceOff(state);
giveCard(state, defender, "fireball", "V", 0);
const ce = giveCard(state, attacker, "card-erasure");
state = castAt(state, attacker, defender, ce, { params: { cardId: "fireball" } });
const d = state.players.find((p) => p.id === defender)!;
expect(d.hand.some((c) => c.cardId === "fireball")).toBe(false);
expect(d.hand.length).toBe(6);
state = must(state, attacker, { type: "endTurn", draw: 0 });
state = must(state, defender, { type: "endTurn", draw: 0 });
const ts = giveCard(state, attacker, "thought-steal");
const handBefore = state.players.find((p) => p.id === attacker)!.hand.length;
state = castAt(state, attacker, defender, ts);
// -1 (thought steal cast) +2 stolen
expect(state.players.find((p) => p.id === attacker)!.hand.length).toBe(handBefore + 1);
expect(state.players.find((p) => p.id === defender)!.hand.length).toBe(4);
});
it("power drain transfers life; sudden death does 10", () => {
let { state } = newGame();
state = toRound2(state);
const { attacker, defender } = faceOff(state);
const pd = giveCard(state, attacker, "power-drain");
giveCard(state, attacker, "number-4", "N", 1);
state = castAt(state, attacker, defender, pd, { numberInstanceIds: ["number-4#N"] });
expect(state.players.find((p) => p.id === defender)!.life).toBe(11);
expect(state.players.find((p) => p.id === attacker)!.life).toBe(19);
state = must(state, attacker, { type: "endTurn", draw: 0 });
state = must(state, defender, { type: "endTurn", draw: 0 });
const sd = giveCard(state, attacker, "sudden-death");
state = castAt(state, attacker, defender, sd);
expect(state.players.find((p) => p.id === defender)!.life).toBe(1);
});
it("wizardblade needs the same square, uses a number card, and stays displayed", () => {
let { state } = newGame();
state = toRound2(state);
const attacker = activePlayer(state);
const defender = state.players.find((p) => p.id !== attacker.id)!;
const wb = giveCard(state, attacker.id, "wizardblade");
giveCard(state, attacker.id, "number-3", "N", 1);
// Not same square: refused.
defender.position = { x: attacker.position.x, y: attacker.position.y === 0 ? 1 : attacker.position.y - 1 };
const refused = applyCommand(state, attacker.id, {
type: "cast", instanceId: wb.instanceId, numberInstanceIds: ["number-3#N"],
target: { kind: "player", playerId: defender.id },
});
expect(refused.ok).toBe(false);
defender.position = { ...attacker.position };
state = castAt(state, attacker.id, defender.id, wb, { numberInstanceIds: ["number-3#N"] });
expect(state.players.find((p) => p.id === defender.id)!.life).toBe(12);
const a = state.players.find((p) => p.id === attacker.id)!;
expect(a.hand.some((c) => c.cardId === "wizardblade")).toBe(true);
expect(a.displayed).toContain(wb.instanceId);
});
});
describe("cast modifiers", () => {
it("amplify doubles fireball; add joins two number cards", () => {
let { state } = newGame();
state = toRound2(state);
const { attacker, defender } = faceOff(state);
const fb = giveCard(state, attacker, "fireball");
giveCard(state, attacker, "amplify", "A", 1);
state = castAt(state, attacker, defender, fb, { amplifyInstanceIds: ["amplify#A"] });
expect(state.players.find((p) => p.id === defender)!.life).toBe(5); // 5 x2
state = must(state, attacker, { type: "endTurn", draw: 0 });
state = must(state, defender, { type: "endTurn", draw: 0 });
const lb = giveCard(state, attacker, "lightning-blast");
giveCard(state, attacker, "number-2", "N1", 1);
giveCard(state, attacker, "number-3", "N2", 2);
giveCard(state, attacker, "add", "AD", 3);
// Two numbers without ADD: refused.
const refused = applyCommand(state, attacker, {
type: "cast", instanceId: lb.instanceId,
numberInstanceIds: ["number-2#N1", "number-3#N2"],
target: { kind: "player", playerId: defender },
});
expect(refused.ok).toBe(false);
state = castAt(state, attacker, defender, lb, {
numberInstanceIds: ["number-2#N1", "number-3#N2"],
addInstanceId: "add#AD",
});
expect(state.players.find((p) => p.id === defender)!.life).toBe(0); // 5 dmg on 5 life
});
it("reverse turns damage into healing but keeps secondary effects", () => {
let { state } = newGame();
state = toRound2(state);
const { attacker, defender } = faceOff(state);
const lb = giveCard(state, attacker, "lightning-blast");
giveCard(state, attacker, "number-4", "N", 1);
giveCard(state, defender, "reverse", "R", 0);
state = must(state, attacker, {
type: "cast", instanceId: lb.instanceId, numberInstanceIds: ["number-4#N"],
target: { kind: "player", playerId: defender },
});
state = must(state, defender, { type: "counteract", instanceId: "reverse#R" });
state = must(state, attacker, { type: "pass" });
state = must(state, defender, { type: "pass" });
const d = state.players.find((p) => p.id === defender)!;
expect(d.life).toBe(19); // gained 4 instead of losing it
expect(d.lostTurns).toBe(1); // the stun still applies
});
});