Files
wikideck/tests/ui-bots.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: #22

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

82 lines
3.9 KiB
TypeScript

import test from 'node:test';
import assert from 'node:assert/strict';
import { DEFAULT_RULESET } from '../shared/game/index.ts';
import { engine } from '../src/game/adapter.ts';
import { BOTS as LOBBY_BOTS, randomBot } from '../src/game/bots.ts';
import { opponentDeck, starterDeck } from '../src/game/cards.ts';
import { BOTS, makeUiBot } from '../src/game/ai/index.ts';
import type { MatchView, PlayerIndex, UIAction } from '../src/game/types.ts';
type B = { choose(view: MatchView, legal: UIAction[], rand: () => number): UIAction };
function rand(seed: number) { return () => ((seed = (seed * 1103515245 + 12345) >>> 0) / 4294967296); }
/** Plays one real-engine match through ui-a's adapter, the way the match screen drives a bot seat. */
function play(bots: [B, B], seed: string) {
let s = engine.createMatch(DEFAULT_RULESET, [starterDeck(), opponentDeck(seed)], seed);
const r = rand(seed.length * 7919 + seed.charCodeAt(seed.length - 1));
for (let steps = 0; steps < 2000 && !engine.isOver(s); steps++) {
for (const p of [0, 1] as PlayerIndex[]) {
const legal = engine.legal(s, p);
if (!legal.length) continue;
const pick = bots[p].choose(engine.view(s, p), legal.filter(a => a.type !== 'concede'), r);
assert.notEqual(pick.type, 'concede');
s = engine.apply(s, p, pick);
}
}
assert.ok(engine.isOver(s), 'match finishes');
return engine.result(s, 0).winner;
}
function winRate(a: B, b: B, n: number) {
let score = 0;
for (let i = 0; i < n; i++) {
const flip = i % 2 === 1;
const w = play(flip ? [b, a] : [a, b], `bots-${i}`);
score += w === null ? 0.5 : w === (flip ? 1 : 0) ? 1 : 0;
}
return score / n;
}
const [easy, normal] = BOTS;
// Normal runs sim-balance's lookahead (~1.5 s of CPU per match), so the default run is small;
// UI_BOTS_FULL=1 plays 100 matches per pairing.
const N = process.env.UI_BOTS_FULL ? 100 : 16;
test('BOTS exports easy and normal in the Bot shape', () => {
assert.deepEqual(BOTS.map(b => b.id), ['easy', 'normal']);
assert.deepEqual(LOBBY_BOTS.map(b => b.id), ['easy', 'normal'], 'the lobby offers the easy and normal bots');
for (const b of BOTS) assert.ok(b.name && b.description);
});
test('normal bot beats random play more than 80% of the time', () => {
const r = winRate(normal, randomBot, N);
console.log(`normal vs random ${r}`); assert.ok(r > 0.8, `normal vs random ${r}`);
});
test('easy bot beats random, and normal beats easy', () => {
const e = winRate(easy, randomBot, 100), g = winRate(normal, easy, N);
console.log(`easy vs random ${e}, normal vs easy ${g}`); assert.ok(e > 0.5, `easy vs random ${e}`);
assert.ok(g > 0.55, `normal vs easy ${g}`);
});
test('bots never concede or un-queue, and end the turn when nothing fits', () => {
const view = { hand: [], lanes: [] } as unknown as MatchView;
for (const b of BOTS) {
const only: UIAction[] = [{ type: 'unstage', cardId: 'x' }, { type: 'concede' }, { type: 'endTurn' }];
for (let i = 0; i < 20; i++) assert.equal(b.choose(view, only, rand(i)).type, 'endTurn');
}
});
test('an engine-level chooser is used when the adapter attaches engine actions', () => {
const seen: unknown[] = [];
const bot = makeUiBot('normal', (ev, _r) => { seen.push(ev); return { type: 'play', player: 1, cardId: 'b', lane: 2 }; });
const legal = [
{ type: 'play', cardId: 'a', lane: 0, engine: { type: 'play', player: 1, cardId: 'a', lane: 0 } },
{ type: 'play', cardId: 'b', lane: 2, engine: { lane: 2, cardId: 'b', player: 1, type: 'play' } },
{ type: 'endTurn', engine: { type: 'commit', player: 1 } },
] as unknown as UIAction[];
const view = { hand: [], lanes: [], engine: { view: { you: 1 } } } as unknown as MatchView;
assert.equal(bot.choose(view, legal, rand(1)), legal[1]);
assert.deepEqual(seen, [{ view: { you: 1 } }], 'the engine chooser gets only the viewFor output');
});