Files
wikideck/tests/duel-cards-engine.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

49 lines
2.5 KiB
TypeScript

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}`);
});