Files
wikideck/tests/game-golden.test.ts
claudeanderic 4f7a8a7c74 Night 1: Edit War card game, eom.dev accounts, peer-hosted modes, Android and iOS apps (#22)
Overnight build by 17 Claude agents (2026-11-16 22:45 → 01:30 EST), coordinated in [claude/wikideck milestone 63](https://gitea.eom.dev/claude/wikideck/milestone/63). **For Eric's review. Do not merge without it.** The full write-up is in `docs/night/REPORT.md`.

**What's in it** (38 reviewed PRs, squash-merged into `night/2026-11-16`):
- **Edit War:**
  - a 3-lane, 6-turn card game built on what WikiDeck cards actually hold
  - a pure, deterministic, off-chain rules engine (`shared/game/`, with the API in `docs/game/ENGINE_API.md`); rules are data and v1.1 is the default
  - a mobile-first Play UI with a lobby, deck builder, four loaner starter decks, a match against the AI, results and a tutorial
  - a landing page (fixes #3)
- **Design and balance:** `docs/game/DESIGN.md`, `CARD_DATA.md` (236 sampled articles) and `BALANCE.md` (bot simulations). Starter pairings went from 23–77% on v1 to roughly even on v1.1.
- **eom.dev accounts:** Dex OIDC with PKCE, plus off-chain wallet linking, where the client builds the message it signs (`server/account/`, `src/auth/`).
- **Peer-hosted game modes:** modes are JSON data, never code. Node API at `/api/game/*`, with host credit and leaderboards. Three example modes (`shared/game/modes.ts`, `server/game/`, `docs/game/HOSTING.md`).
- **Mobile:** Capacitor 7 Android app (a debug APK played a full match against the AI in the emulator), and an iOS project waiting on Xcode (`docs/night/ANDROID.md`, `docs/night/IOS.md`).
- **Testnet tooling:** `deploy/night/` (LAN-only, 3 nodes) and an optional `WIKIDECK_USER_AGENT` (the default is unchanged).

**Not touched:** consensus and protocol (`server/chain.ts`, `shared/protocol.ts`, `server/testnet/{consensus,ledger,…}.ts`, `native/`), the live deployment and mainnet. Matches run off-chain and never move or burn NFTs.

**Checks:** the final integration gate passed in `node:24-alpine`: `npm test` 241/242 with 0 fail (1 skipped), and `npm run build` passes. The repo needs Node 24, because Node 20 fails 12 tests falsely.

**Decisions for Eric** (see REPORT.md, Known issues and Next steps):
1. whether the live node should set `WIKIDECK_USER_AGENT`
2. the Xcode licence, which you should accept yourself
3. #49 in the fork, a bot change held after the freeze
4. on-chain host rewards (proposal only, in HOSTING.md)

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

Co-authored-by: Claude <claude@eom.dev>
2026-11-17 17:40:58 +00:00

152 lines
6.8 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.
/**
* Golden test: DESIGN.md §12 worked example (PR #17 @ cf2fceb), encoded exactly.
* The Topic order, hands and initiative are injected; each player's per-turn
* commit list is the table's, in the listed queue order. Owner: engine-b.
*
* Passes against night/engine/core e0ac157 (frozen §3–§7 rules).
*/
import test from 'node:test';
import assert from 'node:assert/strict';
import {
RULESET_V1_0, RULESET_V1_1, applyAction, createMatch, isOver, lanePower, result, starterDeck, topicsLed,
type Action, type GameCard, type MatchState, type PlayerId, type Ruleset, type Stat, type Faction,
} from '../shared/game/index.ts';
const opts = {};
/** §9 defaults the example depends on. */
const FROZEN = {
laneCapacity: 3, powerByCost: [0, 1, 2, 4, 6, 8, 10], onTopicBonus: [0, 1, 1, 2, 2, 3, 3],
vanillaBonus: 1, energyCarryMax: 1, underdog: false, portalReveal: [2, 3, 4],
};
const c = (owner: string, title: string, faction: Faction, cost: number, power: number, home: Stat, keyword: string | null): GameCard =>
({ cardId: `${owner}:${title}`, pageId: title.length * 1000 + cost, title, faction, rarity: 'Common',
cost, power, home, trend: 0, keyword, source: 'loaner' });
const ANA = [
c('ana', 'Octopus', 'Nature', 1, 2, 'depth', null),
c('ana', 'Moon', 'Space', 2, 2, 'reach', 'translated'),
c('ana', 'Coral reef', 'Nature', 2, 2, 'depth', 'rising'),
c('ana', 'Albert Einstein', 'People', 4, 6, 'attention', 'local'),
c('ana', 'Ancient Rome', 'History', 5, 8, 'depth', 'nostalgic'),
c('ana', 'Library of Alexandria', 'History', 4, 6, 'depth', 'nostalgic'),
c('ana', 'Periodic table', 'Science', 3, 4, 'depth', 'encyclopedic'),
];
const BO = [
c('bo', 'Haiku', 'Culture', 1, 2, 'attention', null),
c('bo', 'Mars', 'Space', 2, 2, 'reach', 'translated'),
c('bo', 'Jazz', 'Culture', 2, 2, 'attention', 'viral'),
c('bo', 'Pop music', 'Culture', 4, 6, 'attention', 'viral'),
c('bo', 'Jupiter', 'Space', 5, 8, 'reach', 'translated'),
c('bo', 'Fern', 'Nature', 3, 4, 'depth', 'rising'),
c('bo', 'Mona Lisa', 'Culture', 3, 4, 'attention', 'viral'),
];
const T1 = 0, T2 = 1, T3 = 2;
/** [player, title, lane] in queue order, per turn (§12 table). */
const PLAN: [PlayerId, string, number][][] = [
[[0, 'Octopus', T1], [1, 'Haiku', T2]],
[[0, 'Moon', T3]], // Bo passes to carry 1
[[0, 'Coral reef', T1], [1, 'Mars', T3], [1, 'Jazz', T2]],
[[1, 'Pop music', T3], [0, 'Albert Einstein', T2]],
[[0, 'Ancient Rome', T3], [1, 'Jupiter', T1]],
[[1, 'Fern', T1], [0, 'Library of Alexandria', T1], [1, 'Mona Lisa', T2], [0, 'Periodic table', T2]],
];
function setup(base: Ruleset = RULESET_V1_0): MatchState {
const rules: Ruleset = structuredClone(base); // §12 is pinned to the v1.0 ruleset; §12.1 passes v1.1
const state = createMatch(rules, [starterDeck('golden-a'), starterDeck('golden-b')], 1);
const pool = state.ruleset.topicPool;
const find = (id: string) => pool.find(t => t.id === id) ?? assert.fail(`topic ${id} missing from the default pool`);
const topics = [find('archive'), find('main-page'), find('translations')];
state.lanes = topics.map(topic => ({ topic, portalRevealed: false, cards: [[], []] }));
// Inject: every card the example plays starts in hand, nothing left to draw.
// The §12 table gives v1.0-derived cards; under another ruleset apply its keyword powerDelta (§10.1).
const adjust = (cards: GameCard[]) => cards.map(c => ({ ...c,
power: c.power + (c.keyword ? (rules.keywordParams[c.keyword]?.powerDelta ?? 0) : 0) }));
state.players[0].hand = adjust(structuredClone(ANA));
state.players[1].hand = adjust(structuredClone(BO));
for (const p of state.players) p.deck = [];
state.initiative = 0; // Ana has initiative on odd turns
return state;
}
const id = (player: PlayerId, title: string) => `${player === 0 ? 'ana' : 'bo'}:${title}`;
function playTurn(state: MatchState, plays: [PlayerId, string, number][]): MatchState {
for (const [player, title, lane] of plays) state = applyAction(state, { type: 'play', player, cardId: id(player, title), lane } as Action);
state = applyAction(state, { type: 'commit', player: 0 });
return applyAction(state, { type: 'commit', player: 1 });
}
test('§12 golden: RULESET_V1_0 matches the frozen §9 v1.0 values', opts, () => {
for (const [key, value] of Object.entries(FROZEN)) assert.deepEqual((RULESET_V1_0 as unknown as Record<string, unknown>)[key], value, key);
});
test('§12 golden: final Topic powers 19–15, 15–19, 13–10; Ana (p0) wins 2–1', opts, () => {
let state = setup();
const energyAtStart: [number, number][] = [];
const snapshots: MatchState[] = [];
for (const plays of PLAN) {
energyAtStart.push([state.players[0].energy, state.players[1].energy]);
state = playTurn(state, plays);
snapshots.push(state);
}
assert.ok(isOver(state));
const r = result(state)!;
assert.deepEqual(r.lanePower, [[19, 15], [15, 19], [13, 10]]);
assert.equal(r.winner, 0);
assert.deepEqual(r.topicsLed, [2, 1]);
// Carry-over: Bo 4 at the start of turn 3; Ana 5, 6, 7 at the start of turns 4, 5, 6.
assert.equal(energyAtStart[2][1], 4);
assert.deepEqual(energyAtStart.slice(3).map(e => e[0]), [5, 6, 7]);
// After turn 5: T2 is 9–9 and Bo leads it by consensus (2 on-topic cards to 1).
const afterFive = snapshots[4];
assert.deepEqual(lanePower(afterFive, T2), [9, 9]);
assert.deepEqual(lanePower(afterFive, T1), [9, 8]);
assert.deepEqual(lanePower(afterFive, T3), [13, 10]);
assert.deepEqual(topicsLed(afterFive), [2, 1]);
});
test('§12 golden: per-turn Topic powers match the table', opts, () => {
const expected = [
[[3, 0], [0, 3], [0, 0]],
[[4, 0], [0, 3], [3, 0]],
[[8, 0], [0, 8], [3, 3]],
[[9, 0], [9, 8], [3, 9]],
[[9, 8], [9, 9], [13, 10]],
];
let state = setup();
PLAN.slice(0, 5).forEach((plays, turn) => {
state = playTurn(state, plays);
assert.deepEqual(state.lanes.map((_, lane) => lanePower(state, lane)), expected[turn], `after turn ${turn + 1}`);
});
});
test('§12.1 golden (v1.1 ruleset): 18–14, 15–21, 13–11; Ana wins 2–1; T1 8–8 consensus to Ana after turn 5', () => {
let state = setup(RULESET_V1_1);
const after: [number, number][][] = [];
for (const plays of PLAN) {
state = playTurn(state, plays);
after.push(state.lanes.map((_, lane) => lanePower(state, lane)));
}
assert.deepEqual(after.slice(0, 5), [
[[3, 0], [0, 3], [0, 0]],
[[4, 0], [0, 3], [3, 0]],
[[7, 0], [0, 9], [3, 3]],
[[8, 0], [9, 9], [3, 10]],
[[8, 8], [9, 10], [13, 11]],
]);
const r = result(state)!;
assert.deepEqual(r.lanePower, [[18, 14], [15, 21], [13, 11]]);
assert.equal(r.winner, 0);
// Replay turns 1–5 to check the T1 consensus lead.
let five = setup(RULESET_V1_1);
for (const plays of PLAN.slice(0, 5)) five = playTurn(five, plays);
assert.deepEqual(lanePower(five, T1), [8, 8]);
assert.deepEqual(topicsLed(five), [2, 1]); // T1 by consensus + T3; Bo leads T2
});