Files
wikideck/tests/duel-core-effects.test.ts
claudeanderic dbb89ce9c5 Night 2: 40-card Magic-style duel, PvP with owned decks, polite Wikimedia client, Android mining (#23)
Night 2 (2026-11-17), from the `claude/wikideck` fork's `night/2026-11-17` branch. It follows Night 1 (#22).

## What's in it
- **Duel engine** (fork #72, #80): 40-card decks of cards you own, Notability/Citations, keywords, effects T1–T12, Connected, a hidden-information-safe `viewFor`/`logFor`, and replay. Balance v1 tuning (#84) gives the first player about 51%.
- **Card profiles** (#70, #82): Figure, Location, Event, Institution or Doctrine, plus stats, keywords and rules text, all derived from real article data. Templates only.
- **Design and story** (#66, #69, #83): DUEL_DESIGN.md, plus the match log and the encyclopedic "Battle of X (2026)" recap.
- **PvP** (#74, #77, #81, #76, #85):
  - The server checks ownership, and 40-card eligibility is enforced.
  - Modes: Classic (ranked), Draft and Trending.
  - Invites, quick match, Elo, a ladder and recap pages. Online matches render on the duel board.
- **Duel board UI** (#79): MTG-style card faces and combat declaration, built for 360 px first.
- **Polite Wikimedia client** (#73, #75): throttled, cached and identified requests, the mint-path fix, and a prefetch pool.
- **Android mining** (#78): native RandomX, as a sideload build. The emulator mined a testnet block.
- **Testnet ops** (#67, #68).

PR numbers refer to `claude/wikideck`.

## Status
- **Tests:** 423, of which 422 pass, 0 fail and 1 is skipped. The build is OK (`node:24-alpine`, `--network none`).
- **Testnet:** running this commit (036489c) on r350.
- **Untouched:** no changes to `server/testnet/consensus.ts`, `randomx.ts`, `native/` or the mainnet compose.
- **Review:** night-review reviewed fork #66–#79. #80–#85 came from the agents' unfinished branches and were not reviewed.
- **Known gaps:**
  - The PvP end-to-end test hasn't been run (it needs two eom.dev accounts).
  - Bot matches aren't reachable from Play.
  - Card identity pre-bake hasn't been done.
  - The RandomX verifier latch (`server/testnet/randomx.ts:140`) is still open.

This PR is for Eric to merge.

🤖 Generated with [Claude Code](https://claude.com/claude-code)Reviewed-on: WikiDeck/wikideck#23

Co-authored-by: Claude <claude@eom.dev>
2026-11-18 18:12:24 +00:00

162 lines
9.2 KiB
TypeScript

import test from 'node:test';
import assert from 'node:assert/strict';
import { DEFAULT_DUEL_RULESET, applyAction, createDuel, legalActions, statsOf, toAct, viewFor } from '../shared/duel/index.ts';
import type { DuelAction, DuelCard, DuelState, Seat } from '../shared/duel/index.ts';
import { fig } from './duel-core-fixtures.ts';
/**
* Scenario harness: both players start with exactly `hands` in hand (rest of the
* library is vanilla filler), seat 0 goes first, lots of Citations.
*/
function scenario(hands: [DuelCard[], DuelCard[]], over: Partial<typeof DEFAULT_DUEL_RULESET> = {}): DuelState {
const rules = { ...DEFAULT_DUEL_RULESET, deckSize: 20, openingHand: 0, citationsPerTurn: 5, ...over };
const filler = (seat: number) => Array.from({ length: 20 }, (_, i) => fig(`f${seat}-${i}`, 1, 1));
const decks = [0, 1].map(seat => [...hands[seat], ...filler(seat)].slice(0, rules.deckSize)) as [DuelCard[], DuelCard[]];
let s = structuredClone(createDuel(rules, decks, 'scenario'));
s.firstSeat = 0; s.active = 0;
for (const seat of [0, 1] as Seat[]) {
const p = s.players[seat];
const ids = new Set(hands[seat].map(c => c.cardId));
p.hand = p.library.filter(c => ids.has(c.card.cardId));
p.library = p.library.filter(c => !ids.has(c.card.cardId));
}
s = applyAction(s, 0, { type: 'keep' });
return applyAction(s, 1, { type: 'keep' });
}
const uid = (s: DuelState, id: string) => [...s.players[0].hand, ...s.players[1].hand, ...s.battlefield].find(c => c.card.cardId === id)!.uid;
const act = (s: DuelState, a: DuelAction) => applyAction(s, toAct(s)!, a);
const pass = (s: DuelState) => act(act(s, { type: 'endPhase' }), { type: 'confirmAttack' });
const ev = (id: string, effects: DuelCard['effects'], type: DuelCard['type'] = 'Event'): DuelCard => ({ ...fig(id, 0, 0), type, effects });
test('createToken, maxTokens, and tokens vanish', () => {
const election = ev('election', [{ trigger: 'onPlay', op: 'createToken', amount: 2, token: { name: 'Elected Member', power: 1, toughness: 1, portal: 'Society' } }]);
let s = scenario([[election], []]);
s = act(s, { type: 'play', uid: uid(s, 'election') });
const tokens = s.battlefield.filter(p => p.card.token);
assert.equal(tokens.length, 2);
assert.equal(s.log.filter(e => e.kind === 'tokenCreated').length, 2);
assert.ok(tokens.every(t => t.sick));
});
test('T6 pump then fight uses both chosen targets', () => {
const sport = ev('final', [
{ trigger: 'onPlay', op: 'pump', target: 'ownFigure', power: 2, toughness: 0 },
{ trigger: 'onPlay', op: 'fight', target: 'opponentFigure' },
]);
let s = scenario([[fig('mine', 2, 4), sport], [fig('theirs', 3, 3)]]);
s = act(s, { type: 'play', uid: uid(s, 'mine') });
s = pass(s);
s = act(s, { type: 'play', uid: uid(s, 'theirs') });
s = pass(s);
const plays = legalActions(s, 0).filter(a => a.type === 'play' && a.uid === uid(s, 'final'));
assert.deepEqual(plays, [{ type: 'play', uid: uid(s, 'final'), targets: [{ kind: 'card', uid: uid(s, 'mine') }, { kind: 'card', uid: uid(s, 'theirs') }] }]);
s = act(s, plays[0]);
assert.ok(!s.battlefield.some(p => p.card.cardId === 'theirs'), '4 power kills a 3/3');
assert.equal(s.battlefield.find(p => p.card.cardId === 'mine')!.damage, 3);
assert.equal(statsOf(s, uid(s, 'mine')).power, 4, 'pump lasts until end of turn');
});
test('Doctrine attaches, buffs, and is Deleted when its Figure leaves', () => {
const adopt = ev('monoid', [
{ trigger: 'onPlay', op: 'attach', target: 'ownFigure' },
{ trigger: 'static', op: 'pump', target: 'attached', power: 2, toughness: 2, grant: ['Featured'] },
], 'Doctrine');
const kill = ev('war', [{ trigger: 'onPlay', op: 'damage', target: 'figureOrLocation', amount: 5 }]);
let s = scenario([[fig('host', 1, 1), adopt], [kill]]);
s = act(s, { type: 'play', uid: uid(s, 'host') });
s = act(s, { type: 'play', uid: uid(s, 'monoid'), targets: [{ kind: 'card', uid: uid(s, 'host') }] });
assert.deepEqual(statsOf(s, uid(s, 'host')), { power: 3, toughness: 3 });
assert.ok(viewFor(s, 0).battlefield.find(p => p.card.cardId === 'host')!.keywords.includes('Featured'));
assert.equal(s.log.filter(e => e.kind === 'adopt').length, 1);
s = pass(s);
s = act(s, { type: 'play', uid: uid(s, 'war'), targets: [{ kind: 'card', uid: uid(s, 'host') }] });
assert.equal(s.battlefield.length, 0, 'Figure destroyed, Doctrine Deleted with it');
assert.equal(s.players[0].graveyard.length, 2);
});
test('Doctrine with no legal target is not playable; Protected blocks opposing Events', () => {
const adopt = ev('monoid', [{ trigger: 'onPlay', op: 'attach', target: 'ownFigure' }], 'Doctrine');
const kill = ev('war', [{ trigger: 'onPlay', op: 'damage', target: 'figure', amount: 5 }]);
let s = scenario([[adopt, fig('pol', 2, 2, ['Protected'])], [kill]]);
assert.ok(!legalActions(s, 0).some(a => a.type === 'play' && a.uid === uid(s, 'monoid')));
s = act(s, { type: 'play', uid: uid(s, 'pol') });
s = pass(s);
assert.ok(!legalActions(s, 1).some(a => a.type === 'play' && a.uid === uid(s, 'war')), 'Protected Figure is the only target');
});
test('Well-sourced prevents 1; Stub grows to 2; Legacy draws', () => {
const ping = ev('ping', [{ trigger: 'onPlay', op: 'damage', target: 'figure', amount: 2 }]);
let s = scenario([[fig('ws', 1, 2, ['Well-sourced']), fig('stub', 1, 1, ['Stub']), fig('leg', 1, 1, ['Legacy'])], [ping, ev('ping2', ping.effects)]]);
s = act(s, { type: 'play', uid: uid(s, 'ws') });
s = act(s, { type: 'play', uid: uid(s, 'stub') });
s = act(s, { type: 'play', uid: uid(s, 'leg') });
s = pass(s);
s = act(s, { type: 'play', uid: uid(s, 'ping'), targets: [{ kind: 'card', uid: uid(s, 'ws') }] });
assert.equal(s.battlefield.find(p => p.card.cardId === 'ws')!.damage, 1, 'first point prevented');
const hand0 = s.players[0].hand.length;
s = act(s, { type: 'play', uid: uid(s, 'ping2'), targets: [{ kind: 'card', uid: uid(s, 'leg') }] });
assert.equal(s.players[0].hand.length, hand0 + 1, 'Legacy drew');
for (let i = 0; i < 6; i++) s = pass(s);
assert.deepEqual(statsOf(s, uid(s, 'stub')), { power: 3, toughness: 3 }, 'Stub capped at 2 counters');
});
test('one Location per turn; Territory raises max Citations up to the cap', () => {
const loc = (id: string): DuelCard => ({ ...fig(id, 0, 2), type: 'Location', territory: 1 });
let s = scenario([[loc('l1'), loc('l2'), loc('l3')], []], { citationsPerTurn: 1, maxCitations: 10 });
s = act(s, { type: 'play', uid: uid(s, 'l1') });
assert.ok(!legalActions(s, 0).some(a => a.type === 'play'), 'second Location not allowed (and no Citations left)');
assert.equal(viewFor(s, 0).you.maxCitations, 2);
s = pass(s); s = pass(s);
s = act(s, { type: 'play', uid: uid(s, 'l2') });
s = pass(s); s = pass(s);
s = act(s, { type: 'play', uid: uid(s, 'l3') });
assert.equal(viewFor(s, 0).you.maxCitations, 3 + 2, 'base 3 + Territory capped at 2');
});
test('Garrison Location blocks; Featured needs a Featured/Translated/Multilingual blocker', () => {
const garrison: DuelCard = { ...fig('fort', 0, 3), type: 'Location' };
let s = scenario([[fig('grunt', 2, 2), fig('flyer', 2, 2, ['Featured'])], [garrison, fig('reach', 1, 3, ['Translated'])]]);
s = act(s, { type: 'play', uid: uid(s, 'grunt') });
s = act(s, { type: 'play', uid: uid(s, 'flyer') });
s = pass(s);
s = act(s, { type: 'play', uid: uid(s, 'fort') });
s = act(s, { type: 'play', uid: uid(s, 'reach') });
s = pass(s);
s = act(s, { type: 'endPhase' });
s = act(s, { type: 'toggleAttacker', uid: uid(s, 'grunt') });
s = act(s, { type: 'toggleAttacker', uid: uid(s, 'flyer') });
s = act(s, { type: 'confirmAttack' });
const blocks = legalActions(s, 1).filter(a => a.type === 'block');
assert.deepEqual(blocks.map(b => b.type === 'block' ? `${b.blocker}>${b.attacker}` : ''), [
`${uid(s, 'fort')}>${uid(s, 'grunt')}`, `${uid(s, 'reach')}>${uid(s, 'grunt')}`, `${uid(s, 'reach')}>${uid(s, 'flyer')}`,
]);
s = act(s, { type: 'block', blocker: uid(s, 'fort'), attacker: uid(s, 'grunt') });
s = act(s, { type: 'block', blocker: uid(s, 'reach'), attacker: uid(s, 'flyer') });
s = act(s, { type: 'confirmBlocks' });
assert.equal(s.players[1].life, 20);
assert.equal(s.battlefield.length, 4, 'nobody died: 2 vs 3 toughness, 1 vs 2');
});
test('three consecutive timeouts lose; turnCap decides by Notability', () => {
let s = scenario([[], []]);
for (let i = 0; i < 5 && !s.result; i++) s = applyAction(s, toAct(s)!, { type: 'timeout' });
assert.deepEqual(s.result, { winner: 1, reason: 'timeout' }, 'seat 0 timed out on turns 1, 3, 5');
let c = scenario([[fig('a', 3, 3)], []], { turnCap: 4 });
c = act(c, { type: 'play', uid: uid(c, 'a') });
c = pass(c); c = pass(c);
c = act(c, { type: 'endPhase' });
c = act(c, { type: 'toggleAttacker', uid: uid(c, 'a') });
c = act(c, { type: 'confirmAttack' });
c = pass(c);
assert.deepEqual(c.result, { winner: 0, reason: 'turnCap' });
});
test('secondPlayerCitations gives the second player extra Citations on their first turn only', () => {
let s = scenario([[], []], { citationsPerTurn: 1, secondPlayerCitations: 1 });
assert.equal(s.players[0].citations, 1);
s = pass(s);
assert.equal(s.players[1].citations, 2);
s = pass(s); s = pass(s);
assert.equal(s.players[1].citations, 2, 'turn 4: base 2, no bonus');
});