forked from WikiDeck/wikideck
[duel-cards] engine integration test (local, for after the engine merges)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,48 @@
|
||||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import { readFileSync } from 'node:fs';
|
||||
import { createDuel, legalActions, applyAction, toAct, isOver, statsOf, DEFAULT_DUEL_RULESET } from '../shared/duel/index.ts';
|
||||
import { deriveSample, type SampleCard } from '../shared/duel/sample.ts';
|
||||
import { Rng } from '../shared/game/rng.ts';
|
||||
import type { DuelCard } from '../shared/duel/derive.ts';
|
||||
|
||||
const SAMPLE = (JSON.parse(readFileSync(new URL('../docs/game/card-sample.json', import.meta.url), 'utf8')) as { cards: SampleCard[] }).cards;
|
||||
const CARDS = deriveSample(SAMPLE);
|
||||
|
||||
function deck(rng: Rng, prefix: string): DuelCard[] {
|
||||
const pool = [...CARDS];
|
||||
const out: DuelCard[] = [];
|
||||
while (out.length < 40) out.push(pool.splice(rng.int(pool.length), 1)[0]);
|
||||
return out.map(c => ({ ...c, cardId: `${prefix}:${c.cardId}` }));
|
||||
}
|
||||
|
||||
test('derived decks play to completion in the duel engine, and Doctrines change the adopted Figure', () => {
|
||||
let adoptions = 0, checked = 0;
|
||||
for (let g = 0; g < 40; g++) {
|
||||
const rng = Rng.fromSeed(`duel-cards-engine-${g}`);
|
||||
let state = createDuel(DEFAULT_DUEL_RULESET, [deck(rng, 'a'), deck(rng, 'b')], `seed-${g}`);
|
||||
for (let steps = 0; !isOver(state) && steps < 3000; steps++) {
|
||||
const seat = toAct(state)!;
|
||||
const actions = legalActions(state, seat);
|
||||
assert.ok(actions.length > 0);
|
||||
const action = actions[rng.int(actions.length)];
|
||||
const prev = state;
|
||||
state = applyAction(state, seat, action);
|
||||
for (const ev of state.log.slice(prev.log.length)) {
|
||||
if (ev.kind !== 'adopt') continue;
|
||||
adoptions++;
|
||||
const doctrine = state.battlefield.find(p => p.uid === ev.card.uid);
|
||||
const figure = state.battlefield.find(p => p.uid === ev.to.uid);
|
||||
if (!doctrine || !figure || !prev.battlefield.some(p => p.uid === figure.uid)) continue;
|
||||
const buff = doctrine.card.effects.find(e => e.trigger === 'static' && e.target === 'attached')!;
|
||||
const was = statsOf(prev, figure.uid), now = statsOf(state, figure.uid);
|
||||
// Exactly the Doctrine's ±k, plus at most a Connected bonus (+1 or +2) between the two cards.
|
||||
const dp = now.power - was.power - buff.power!, dt = now.toughness - was.toughness - buff.toughness!;
|
||||
assert.ok(dp >= 0 && dp <= 2 && dt >= 0 && dt <= 2, `${doctrine.card.title} on ${figure.card.title}: ${JSON.stringify({ was, now, buff })}`);
|
||||
checked++;
|
||||
}
|
||||
}
|
||||
assert.ok(isOver(state), `game ${g} did not finish`);
|
||||
}
|
||||
assert.ok(adoptions > 0 && checked > 0, `adoptions ${adoptions}`);
|
||||
});
|
||||
Reference in New Issue
Block a user