Files
wikideck/tests/ui-duel-model.test.ts

121 lines
6.9 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import test from 'node:test';
import assert from 'node:assert/strict';
import {
EMPTY_DECL, NO_LEGAL, blockList, cardSummary, eligibility, firstSentence, previewCombat, tapAttacker, tapBlocker, toggleAttacker, typeLine,
type BoardCard, type Legal,
} from '../src/game/duel/model.ts';
import { PREVIEW_POOL, previewView } from '../src/game/duel/mock.ts';
const fig = (id: string, power: number, toughness: number, extra: Partial<BoardCard> = {}): BoardCard =>
({ id, title: id, type: 'Figure', portal: 'Society', cost: 2, power, toughness, keywords: [], rulesText: '', ...extra });
test('attack declaration: one tap toggles a legal attacker, illegal ones are ignored', () => {
const legal: Legal = { ...NO_LEGAL, attackers: ['a', 'b'] };
let d = toggleAttacker(EMPTY_DECL, 'a', legal);
assert.deepEqual(d.attackers, ['a']);
d = toggleAttacker(d, 'z', legal);
assert.deepEqual(d.attackers, ['a']);
d = toggleAttacker(d, 'b', legal);
d = toggleAttacker(d, 'a', legal);
assert.deepEqual(d.attackers, ['b']);
});
test('block declaration: two taps per blocker, single blocks, re-tap clears', () => {
const legal: Legal = { ...NO_LEGAL, blockers: { x: ['A', 'B'], y: ['A'], z: [] } };
let d = tapBlocker(EMPTY_DECL, 'x', legal);
assert.equal(d.picked, 'x');
d = tapAttacker(d, 'A', legal);
assert.deepEqual(blockList(d), [{ blockerId: 'x', attackerId: 'A' }]);
assert.equal(d.picked, undefined);
// y takes over A; x is freed (at most one blocker per attacker).
d = tapAttacker(tapBlocker(d, 'y', legal), 'A', legal);
assert.deepEqual(blockList(d), [{ blockerId: 'y', attackerId: 'A' }]);
// y can't block B: the tap is ignored and y stays picked.
d = tapAttacker(tapBlocker(EMPTY_DECL, 'y', legal), 'B', legal);
assert.deepEqual(blockList(d), []);
// A blocker with no legal attacker can't be picked.
assert.equal(tapBlocker(EMPTY_DECL, 'z', legal).picked, undefined);
// Re-tapping an assigned blocker removes the block; so does tapping its attacker with nothing picked.
d = tapAttacker(tapBlocker(EMPTY_DECL, 'x', legal), 'B', legal);
assert.deepEqual(blockList(tapBlocker(d, 'x', legal)), []);
assert.deepEqual(blockList(tapAttacker(d, 'B', legal)), []);
// One blocker per attacker, and one attacker per blocker.
d = tapAttacker(tapBlocker(d, 'x', legal), 'A', legal);
assert.equal(blockList(d).length <= 1, true);
});
test('combat preview: simultaneous damage, single blocks, Well-sourced, marked damage', () => {
const cards = new Map([fig('a', 3, 3), fig('b', 2, 2), fig('c', 4, 1), fig('w', 1, 3, { keywords: ['Well-sourced'] }), fig('d', 2, 3, { damage: 1 })].map(c => [c.id, c]));
const out = previewCombat([{ attackerId: 'a', blockerId: 'b' }, { attackerId: 'c' }], cards);
assert.equal(out.toPlayer, 4);
assert.deepEqual(out.destroyed.sort(), ['b']);
// Well-sourced prevents the first 1: a 3-power attacker deals 2 to a 1/3, which survives.
assert.deepEqual(previewCombat([{ attackerId: 'a', blockerId: 'w' }], cards).destroyed, []);
// Damage already marked counts: 2 more kills a 2/3 with 1 damage.
assert.deepEqual(previewCombat([{ attackerId: 'b', blockerId: 'd' }], cards).destroyed.sort(), ['b', 'd']);
});
test('eligibility: exactly the deck size, no borrowed cards, progress label', () => {
assert.deepEqual(eligibility(23), { eligible: false, owned: 23, needed: 17, label: '23 / 40 cards' });
assert.equal(eligibility(40).eligible, true);
assert.equal(eligibility(57).label, '40 / 40 cards');
assert.equal(eligibility(12, 12).eligible, true); // testnet mode file may lower deckSize
});
test('flavour: first sentence of the extract, not split on initials or abbreviations', () => {
assert.equal(firstSentence('J. R. R. Tolkien was an English writer. He wrote books.'), 'J. R. R. Tolkien was an English writer.');
assert.equal(firstSentence('St. Georgen am Längsee is a town. It is in Austria.'), 'St. Georgen am Längsee is a town.');
assert.equal(firstSentence('Pi is about 3.14 in value. More.'), 'Pi is about 3.14 in value.');
assert.equal(firstSentence('No full stop here'), 'No full stop here');
assert.equal(firstSentence(''), undefined);
assert.ok((firstSentence('word '.repeat(100)) ?? '').length <= 220);
});
test('type line and accessible summary', () => {
const c = fig('Barack Obama', 3, 4, { subtype: 'People · 1961–', keywords: ['Protected'], tapped: true });
assert.equal(typeLine(c), 'Figure — People · 1961–');
assert.equal(cardSummary(c), 'Barack Obama, Figure — People · 1961–, cost 2, 3/4, Protected, tapped');
});
test('preview scenes: a full 5 + 5 board with every card from the real sample', () => {
assert.equal(PREVIEW_POOL.length, 236);
for (const scene of ['main', 'attack', 'block', 'over'] as const) {
const { view } = previewView(scene);
assert.equal(view.me.battlefield.length, 5);
assert.equal(view.opponent.battlefield.length, 5);
}
for (const c of PREVIEW_POOL) {
assert.ok(c.keywords.length <= 2, c.title);
assert.ok(c.cost >= 1 && c.cost <= 6, c.title);
}
});
test('DuelCard → board card: type line from real data only, flavour is one sentence', async () => {
const { boardCard, lifespan } = await import('../src/game/duel/cards.ts');
assert.equal(lifespan('Sultan of Aceh (1530–1537/9)'), '1530–1537');
assert.equal(lifespan('American voice actor and singer (born 1963)'), '1963–');
assert.equal(lifespan('American botanist (c.1872–1919)'), '1872–1919');
assert.equal(lifespan('Species of grasshopper'), undefined);
const c = boardCard({ cardId: 't', pageId: 1, revisionId: 9, title: 'Salahuddin of Aceh', type: 'Figure', subtype: null, portal: 'History',
cost: 4, power: 4, toughness: 3, keywords: ['Legacy'], rulesText: 'Legacy.', flavor: 'Sultan of Aceh (1530–1537/9). Second sentence.' });
assert.equal(typeLine(c), 'Figure — History · 1530–1537');
assert.equal(c.flavour, 'Sultan of Aceh (1530–1537/9).');
assert.equal(c.url, 'https://en.wikipedia.org/wiki/Salahuddin_of_Aceh');
const e = boardCard({ cardId: 'e', pageId: 2, revisionId: 9, title: '1954 Gold Coast general election', type: 'Event', subtype: 'election', portal: 'Society',
cost: 4, power: null, toughness: null, keywords: [], rulesText: 'x', flavor: '' });
assert.equal(typeLine(e), 'Event — Society · Election');
assert.equal(e.power, undefined);
});
test('targeting: multi-step sequences (own Figure, then theirs), players as targets', async () => {
const { nextTargets, targetsComplete, playerTarget } = await import('../src/game/duel/model.ts');
const seqs = [['a', 'x'], ['a', 'y'], ['b', 'x']];
assert.deepEqual([...nextTargets(seqs, [])].sort(), ['a', 'b']);
assert.deepEqual([...nextTargets(seqs, ['a'])].sort(), ['x', 'y']);
assert.deepEqual([...nextTargets(seqs, ['b'])], ['x']);
assert.equal(targetsComplete(seqs, ['a']), false);
assert.equal(targetsComplete(seqs, ['a', 'y']), true);
assert.equal(targetsComplete([[]], []), true);
assert.equal(nextTargets([[playerTarget(1)], ['c']], []).has('player:1'), true);
});