Files
wikideck/tests/bots-sim.test.ts
T
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

68 lines
3.5 KiB
TypeScript

import { test } from 'node:test';
import assert from 'node:assert/strict';
import { existsSync } from 'node:fs';
import { makeBot, mulberry32 } from '../shared/bots/index.ts';
import type { EngineAdapter } from '../shared/bots/types.ts';
import { playMatch, decidedTurn } from '../scripts/sim/match.ts';
import { aggregate, rate, toMarkdown } from '../scripts/sim/stats.ts';
import { toyEngine } from '../scripts/sim/toy-engine.ts';
test('toy matches are deterministic and terminate', () => {
const decks = toyEngine.decks('toy');
const a = playMatch(toyEngine, 'toy', [decks[0], decks[1]], ['greedy', 'random'], 42);
const b = playMatch(toyEngine, 'toy', [decks[0], decks[1]], ['greedy', 'random'], 42);
assert.equal(a.truncated, false);
assert.deepEqual({ ...a, ms: 0 }, { ...b, ms: 0 });
});
test('greedy beats random on the toy engine', () => {
const decks = toyEngine.decks('toy');
const recs = Array.from({ length: 60 }, (_, i) => playMatch(toyEngine, 'toy', [decks[i % 3], decks[(i + 1) % 3]], i % 2 ? ['greedy', 'random'] : ['random', 'greedy'], i));
const stats = aggregate(recs);
assert.ok(stats.skill['greedy>random'].rate > 0.7, JSON.stringify(stats.skill));
assert.match(toMarkdown(stats), /First-player win rate/);
});
test('stats helpers', () => {
const r = rate(50, 100);
assert.ok(r.lo < 0.5 && r.hi > 0.5);
assert.equal(decidedTurn([0.2, -0.3, 0.4, 0.6], 0), 3);
assert.equal(decidedTurn([0.2, 0.3], null), null);
});
test('bots only return legal actions on shared/game', { skip: !existsSync(new URL('../shared/game/index.ts', import.meta.url)) }, async () => {
const { gameEngine } = await import('../shared/bots/game-adapter.ts');
const engine = gameEngine as EngineAdapter<any, any>;
const decks = engine.decks(engine.rulesets()[0]);
for (const name of ['random', 'greedy', 'lookahead-lite']) {
const bot = makeBot(name, engine);
let s = engine.createMatch(engine.rulesets()[0], [decks[0], decks[decks.length - 1]], 7);
const rng = mulberry32(1);
let steps = 0;
while (!engine.isOver(s) && steps++ < 500) {
const seat = engine.toAct(s);
const a = bot(engine.observe ? engine.observe(s, seat) : s, seat, rng);
assert.ok(engine.legalActions(s, seat).some(x => JSON.stringify(x) === JSON.stringify(a)), `${name} chose illegal ${JSON.stringify(a)}`);
s = engine.applyAction(s, a);
}
assert.ok(engine.isOver(s), `${name} match did not finish`);
}
});
test('seat 1 bot input does not depend on what seat 0 has queued', { skip: !existsSync(new URL('../shared/game/index.ts', import.meta.url)) }, async () => {
const { gameEngine, stateFromView, evaluateFor, starterPoolCards } = await import('../shared/bots/game-adapter.ts');
const engine = gameEngine as EngineAdapter<any, any>;
const decks = engine.decks(engine.rulesets()[0]);
for (let seed = 1; seed <= 20; seed++) {
const s0 = engine.createMatch(engine.rulesets()[0], [decks[0], decks[1]], seed);
const plays = engine.legalActions(s0, 0).filter((a: any) => a.type === 'play');
if (!plays.length) continue;
const s1 = engine.applyAction(s0, plays[0]);
const s2 = engine.applyAction(s1, { type: 'commit', player: 0 });
const pool = starterPoolCards();
const ev = (s: any) => evaluateFor(stateFromView(engine.observe!(s, 1) as any, mulberry32(9), pool), 1);
assert.equal(ev(s1), ev(s0), `seed ${seed}: queued play changed seat 1's evaluation`);
assert.equal(ev(s2), ev(s0), `seed ${seed}: commit changed seat 1's evaluation`);
}
});