Files
wikideck/tests/game-derive.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

173 lines
11 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.
import test from 'node:test';
import assert from 'node:assert/strict';
import {
RULESET_V1_0, percentile, percentiles, fame, costFor, homeStat, trendFor, keywordFor, deriveCard, deriveDeck,
type CardInput, type PercentileTable, type DerivationRules, type Faction,
} from '../shared/game/index.ts';
import { forAll } from './game/property.ts';
/** Cut points 1..99: score s sits at percentile ~s, which makes examples readable. */
const LINEAR = Array.from({ length: 99 }, (_, i) => i + 1);
const TABLE: PercentileTable = { attention: LINEAR, depth: LINEAR, reach: LINEAR };
const RULES: DerivationRules = RULESET_V1_0; // pinned: these examples are v1.0 numbers
const LIVE = { trendPercent: null, dailyAverage: null };
function input(stats: CardInput['snapshot']['stats'], overrides: Partial<CardInput> = {}): CardInput {
return { cardId: 'c1', pageId: 1, title: 'T', category: 'Science', rarity: 'Common',
snapshot: { stats }, live: LIVE, check: 'verified', ...overrides };
}
test('percentile counts cuts strictly below plus half of equal ones, rounded down (§1.1)', () => {
assert.equal(percentile(0, LINEAR), 0);
assert.equal(percentile(50, LINEAR), 49);
assert.equal(percentile(50.5, LINEAR), 50);
assert.equal(percentile(1000, LINEAR), 99);
assert.equal(percentile(5, [5, 5, 5, 5]), 2);
assert.equal(percentile(5, [1, 5, 5, 9]), 2);
});
test('null attention gives a null percentile and is ignored by fame (§1.1, §1.2)', () => {
const pct = percentiles({ attention: null, depth: 61, reach: 31 }, TABLE);
assert.deepEqual(pct, { attention: null, depth: 60, reach: 30 });
assert.equal(fame(pct), 45);
});
test('fame rounds half up', () => {
assert.equal(fame({ attention: 10, depth: 11, reach: null }), 11);
assert.equal(fame({ attention: 10, depth: 10, reach: 11 }), 10);
assert.equal(fame({ attention: 10, depth: 11, reach: 11 }), 11);
});
test('cost is 1 + thresholds ≤ fame, spanning 1–6 (§1.2)', () => {
const t = RULES.costThresholds;
assert.deepEqual([0, 14, 15, 54, 55, 89, 90, 100].map(f => costFor(f, t)), [1, 1, 2, 3, 4, 5, 6, 6]);
});
test('home stat: highest percentile, ties depth > reach > attention, never null (§1.4)', () => {
assert.equal(homeStat({ attention: 90, depth: 10, reach: 10 }), 'attention');
assert.equal(homeStat({ attention: 50, depth: 50, reach: 50 }), 'depth');
assert.equal(homeStat({ attention: 50, depth: 40, reach: 50 }), 'reach');
assert.equal(homeStat({ attention: null, depth: 0, reach: 0 }), 'depth');
assert.equal(homeStat({ attention: null, depth: 0, reach: 1 }), 'reach');
});
test('trend: ±1 at inclusive thresholds, 0 for null, 0 under trendMinDaily (§1.6)', () => {
const modeRules = { ...RULES, trendDown: -25 }; // a mode that turns the down arrow back on
const t = (trendPercent: number | null, dailyAverage: number | null = 100) => trendFor({ trendPercent, dailyAverage }, modeRules);
assert.deepEqual([t(25), t(24.9), t(-25), t(-24.9), t(null), t(Number.NaN), t(1e9)], [1, 0, -1, 0, 0, 0, 1]);
assert.equal(trendFor({ trendPercent: -99, dailyAverage: 1000 }, RULES), 0, 'v1 default: no down arrow');
assert.equal(t(80, 19.9), 0);
assert.equal(t(80, 20), 1);
assert.equal(t(80, null), 0);
});
test('keyword choice: cost 1 vanilla, faction keyword otherwise, disabled → plain vanilla (§10.1)', () => {
const expected: Record<Faction, string> = { Culture: 'viral', Nature: 'rising', People: 'local',
Science: 'encyclopedic', History: 'nostalgic', Space: 'translated' };
for (const [faction, keyword] of Object.entries(expected) as [Faction, string][]) {
assert.deepEqual(keywordFor(faction, 1, false, RULES), { keyword: null, vanilla: true });
assert.deepEqual(keywordFor(faction, 3, false, RULES), { keyword, vanilla: false });
assert.deepEqual(keywordFor(faction, 3, false, { keywordsEnabled: [] }), { keyword: null, vanilla: false });
}
assert.equal(keywordFor('Culture', 3, true, RULES).keyword, 'viral', 'edition is off by default');
assert.equal(keywordFor('Culture', 3, true, { keywordsEnabled: ['edition', 'viral'] }).keyword, 'edition');
assert.equal(keywordFor('Culture', 1, true, { keywordsEnabled: ['edition'] }).keyword, null, 'cost 1 stays vanilla');
});
test('deriveCard: cost, power, vanillaBonus, home, trend and source', () => {
// pct 80/70/60 → fame 70 → cost 4 → power 6, Science → encyclopedic
const card = deriveCard(input({ attention: 81, depth: 71, reach: 61 }, { live: { trendPercent: 40, dailyAverage: 50 } }), TABLE, RULES);
assert.deepEqual(card, { cardId: 'c1', pageId: 1, title: 'T', faction: 'Science', rarity: 'Common',
cost: 4, power: 6, home: 'attention', trend: 1, keyword: 'encyclopedic', source: 'owned', check: 'verified' });
const cheap = deriveCard(input({ attention: 5, depth: 5, reach: 5 }, { check: 'loaner' }), TABLE, RULES);
assert.deepEqual([cheap.cost, cheap.power, cheap.keyword, cheap.source], [1, 2, null, 'loaner']);
const disabled = deriveCard(input({ attention: 81, depth: 71, reach: 61 }), TABLE, { ...RULES, keywordsEnabled: [] });
assert.deepEqual([disabled.power, disabled.keyword], [6, null]);
});
test('rarity is cosmetic: it never changes cost, power or keyword (§1.7)', () => {
const stats = { attention: 40, depth: 60, reach: 20 };
const cards = (['Common', 'Rare', 'Epic', 'Legendary'] as const).map(rarity => deriveCard(input(stats, { rarity }), TABLE, RULES));
for (const card of cards) assert.deepEqual({ ...card, rarity: 'Common' }, { ...cards[0], rarity: 'Common' });
});
test('unchecked cards are capped at uncheckedCostCap and keep their home stat (§1.10)', () => {
const stats = { attention: 100, depth: 100, reach: 99 };
const checked = deriveCard(input(stats), TABLE, RULES);
const unchecked = deriveCard(input(stats, { check: 'unchecked' }), TABLE, RULES);
assert.equal(checked.cost, 6);
assert.equal(unchecked.cost, 4);
assert.equal(unchecked.power, RULES.powerByCost[4]);
assert.equal(unchecked.home, checked.home);
});
test('powerDelta from keywordParams lowers keyword cards only, clamped to −3..0', () => {
const rules: DerivationRules = { ...RULES, keywordParams: { encyclopedic: { powerDelta: -2 }, viral: { powerDelta: -9 } } };
assert.equal(deriveCard(input({ attention: 81, depth: 71, reach: 61 }), TABLE, rules).power, 4);
assert.equal(deriveCard(input({ attention: 81, depth: 71, reach: 61 }, { category: 'Culture' }), TABLE, rules).power, 3);
assert.equal(deriveCard(input({ attention: 5, depth: 5, reach: 5 }), TABLE, rules).power, 2);
});
test('deriveDeck marks copies for Edition when it is enabled', () => {
const a = input({ attention: 50, depth: 50, reach: 50 }, { cardId: 'a', pageId: 7, category: 'Culture' });
const b = { ...a, cardId: 'b' };
const c = { ...a, cardId: 'c', pageId: 8 };
const rules = { ...RULES, keywordsEnabled: [...RULES.keywordsEnabled, 'edition'] };
assert.deepEqual(deriveDeck([a, b, c], TABLE, rules).map(card => card.keyword), ['edition', 'edition', 'viral']);
assert.deepEqual(deriveDeck([a, b, c], TABLE, RULES).map(card => card.keyword), ['viral', 'viral', 'viral']);
});
test('power depends only on cost: forged stats only move a card up the curve (§1.3)', () => {
forAll(300, g => {
const stats = { attention: g.bool(0.1) ? null : g.int(0, 100), depth: g.int(0, 100), reach: g.int(0, 100) };
const card = deriveCard(input(stats, { category: g.pick(['Space', 'Nature', 'People', 'Science', 'History', 'Culture'] as const) }), TABLE, RULES);
assert.ok(card.cost >= 1 && card.cost <= 6);
assert.equal(card.power, RULES.powerByCost[card.cost] + (card.cost === 1 ? RULES.vanillaBonus : 0));
assert.ok(stats[card.home] !== null, 'home is never a null stat');
const bumped = { ...stats, depth: Math.min(100, stats.depth + g.int(1, 30)) };
assert.ok(deriveCard(input(bumped), TABLE, RULES).cost >= card.cost, 'raising a stat never lowers cost');
});
});
test('deriveCard rejects a rules config with no power for the derived cost', () => {
assert.throws(() => deriveCard(input({ attention: 100, depth: 100, reach: 100 }), TABLE, { ...RULES, powerByCost: [0, 1, 2] }), RangeError);
});
test('pinned values replace §1.1–1.5 for loaners only (§2.1)', () => {
const pinned = { cost: 5, home: 'reach' as const, faction: 'History' as const };
const stats = { attention: 5, depth: 5, reach: 5 };
const loaner = deriveCard(input(stats, { check: 'loaner', pinned, rarity: 'Epic', live: { trendPercent: 99, dailyAverage: 999 } }), TABLE, RULES);
assert.deepEqual([loaner.cost, loaner.power, loaner.home, loaner.faction, loaner.keyword, loaner.rarity, loaner.trend, loaner.source],
[5, 8, 'reach', 'History', 'nostalgic', 'Common', 0, 'loaner']);
const owned = deriveCard(input(stats, { pinned }), TABLE, RULES);
assert.deepEqual([owned.cost, owned.faction], [1, 'Science'], 'pinned is ignored when check !== loaner');
const cheap = deriveCard(input(stats, { check: 'loaner', pinned: { ...pinned, cost: 1 } }), TABLE, RULES);
assert.deepEqual([cheap.power, cheap.keyword], [2, null], 'vanillaBonus still applies to pinned cost-1');
});
test('cultureKeyword (v1.1 hook): home gives Culture cards {attention: viral, depth: encyclopedic, reach: translated}', () => {
const home = { ...RULES, cultureKeyword: 'home' as const };
const culture = (stats: CardInput['snapshot']['stats']) => input(stats, { category: 'Culture' });
assert.equal(deriveCard(culture({ attention: 80, depth: 40, reach: 40 }), TABLE, RULES).keyword, 'viral', 'v1 default: faction');
assert.equal(deriveCard(culture({ attention: 80, depth: 40, reach: 40 }), TABLE, home).keyword, 'viral');
assert.equal(deriveCard(culture({ attention: 40, depth: 80, reach: 40 }), TABLE, home).keyword, 'encyclopedic');
assert.equal(deriveCard(culture({ attention: 40, depth: 40, reach: 80 }), TABLE, home).keyword, 'translated');
assert.equal(deriveCard(input({ attention: 40, depth: 40, reach: 80 }, { category: 'History' }), TABLE, home).keyword, 'nostalgic', 'other factions unchanged');
assert.equal(deriveCard(culture({ attention: 5, depth: 5, reach: 5 }), TABLE, home).keyword, null, 'cost 1 still vanilla');
});
test('a full Ruleset works as DerivationRules (hosts can derive with the mode rules)', async () => {
const { DEFAULT_RULESET } = await import('../shared/game/index.ts');
const a = deriveCard(input({ attention: 81, depth: 71, reach: 61 }), TABLE, DEFAULT_RULESET);
const b = deriveCard(input({ attention: 81, depth: 71, reach: 61 }), TABLE);
assert.deepEqual(a, b, 'default derivation rules are DEFAULT_RULESET');
});
test('parseRuleset accepts a stored pre-v1.1 ruleset without cultureKeyword (defaults to faction)', async () => {
const { parseRuleset, RULESET_V1_0 } = await import('../shared/game/index.ts');
const { cultureKeyword: _omit, ...old } = structuredClone(RULESET_V1_0);
const parsed = parseRuleset(old);
assert.equal(parsed.cultureKeyword, 'faction');
assert.deepEqual(parsed, RULESET_V1_0);
});