Files
wikideck/tests/modes-schema.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

86 lines
5.1 KiB
TypeScript

import test from 'node:test';
import assert from 'node:assert/strict';
import { readFileSync } from 'node:fs';
import { parseMode, parseModeJson, parseModeList, resolveRuleset, trendBonus, mappedPower,
MAX_MODE_BYTES, MODE_SCHEMA_VERSION } from '../shared/game/modes.ts';
import { BUILTIN_MODES_DIR, BUILTIN_MODE_IDS, loadModes } from '../server/game/config.ts';
import { parsePeerOrigins } from '../server/game/discovery.ts';
import { stubEngine } from './modes-stub-engine.ts';
import { engineBinding } from '../server/game/engine-binding.ts';
const base = { schema: MODE_SCHEMA_VERSION, id: 'my-mode', version: 1, name: 'My mode', description: 'A test mode.' };
test('modes: minimal document gets safe defaults', () => {
const m = parseMode(base);
assert.deepEqual(m.cards, { preset: 'standard' });
assert.equal(m.decks.kind, 'constructed');
assert.equal(m.limits.maxConcurrentMatches, 32);
assert.deepEqual(m.rules, {});
});
test('modes: built-in example modes parse and resolve against the engine ruleset', () => {
for (const id of BUILTIN_MODE_IDS) {
const m = parseModeJson(readFileSync(`${BUILTIN_MODES_DIR}${id}.json`, 'utf8'));
assert.equal(m.id, id);
assert.doesNotThrow(() => resolveRuleset(engineBinding.defaultRuleset, m.rules, engineBinding.parseRuleset));
}
assert.deepEqual(loadModes({ WIKIDECK_MODES: 'classic,draft' }).map(m => m.id), ['classic', 'draft']);
assert.deepEqual(loadModes({ WIKIDECK_MODES: 'none' }), []);
assert.throws(() => loadModes({ WIKIDECK_MODES: 'classic,classic' }), /Duplicate/);
assert.throws(() => loadModes({ WIKIDECK_MODES: '../../etc/passwd' }), /unknown built-in/);
});
test('modes: schema rejects code, markup, unknown fields and oversize documents', () => {
assert.throws(() => parseMode({ ...base, script: 'alert(1)' }));
assert.throws(() => parseMode({ ...base, name: '<img src=x onerror=alert(1)>' }));
assert.throws(() => parseMode({ ...base, id: 'Bad Id' }));
assert.throws(() => parseMode({ ...base, cards: { preset: 'eval', code: 'x' } }));
assert.throws(() => parseMode({ ...base, cards: { preset: 'trending', maxBonus: 99 } }));
assert.throws(() => parseMode({ ...base, limits: { maxConcurrentMatches: 100000 } }));
assert.throws(() => parseMode({ ...base, rules: { deep: { deeper: { deepest: 1 } } } }));
assert.throws(() => parseModeJson(JSON.stringify(base).replace('}', ',"rules":{"__proto__":{"turns":1}}}')));
assert.throws(() => resolveRuleset(stubEngine.defaultRuleset, JSON.parse('{"__proto__":{"turns":1}}'), stubEngine.parseRuleset));
assert.throws(() => parseModeJson(JSON.stringify({ ...base, description: 'x'.repeat(MAX_MODE_BYTES) })), /exceeds|too_big|400/);
const many = Object.fromEntries(Array.from({ length: 60 }, (_, i) => [`k${i}`, i]));
assert.throws(() => parseMode({ ...base, rules: many }));
});
test('modes: rule overrides merge one level deep and the engine parser has the final say', () => {
const r = resolveRuleset(stubEngine.defaultRuleset, { turns: 5, scoring: { tieBreak: 'first' } }, stubEngine.parseRuleset);
assert.deepEqual(r, { deckSize: 4, turns: 5, scoring: { tieBreak: 'first' } });
assert.throws(() => resolveRuleset(stubEngine.defaultRuleset, { turns: 500 }, stubEngine.parseRuleset));
assert.throws(() => resolveRuleset(stubEngine.defaultRuleset, { unknownRule: 1 }, stubEngine.parseRuleset));
assert.deepEqual(stubEngine.defaultRuleset.scoring, { tieBreak: 'draw' }, 'base ruleset is not mutated');
});
test('modes: trending bonus is bounded, optional and symmetric', () => {
const t = parseMode({ ...base, cards: { preset: 'trending' } }).cards;
assert.equal(t.preset, 'trending');
if (t.preset !== 'trending') return;
assert.equal(trendBonus(t, 60), 2);
assert.equal(trendBonus(t, 1000), 3);
assert.equal(trendBonus(t, -1000), -3);
assert.equal(trendBonus(t, null), 0);
assert.equal(trendBonus(t, Number.NaN), 0);
assert.equal(trendBonus({ ...t, penalise: false }, -80), 0);
assert.equal(mappedPower(4, { preset: 'standard' }, { trendPercent: 500 }), 4);
});
test('modes: peer lists drop invalid modes instead of failing, and are size capped', () => {
const good = { mode: parseMode(base), stats: { playersOnline: 0, openLobbies: 0, liveMatches: 0, matchesPlayed: 3, distinctPlayers: 2 } };
const body = { schema: MODE_SCHEMA_VERSION, host: { origin: 'http://a.example', operator: 'A' }, matchesHosted: 3,
modes: [good, { mode: { ...base, id: 'X' }, stats: good.stats }] };
const { list, rejected } = parseModeList(JSON.stringify(body));
assert.equal(list.modes.length, 1);
assert.equal(rejected, 1);
assert.throws(() => parseModeList(' '.repeat(300 * 1024)), /too large/);
});
test('modes: game peers are operator-configured origins only', () => {
assert.deepEqual(parsePeerOrigins('https://a.example, https://a.example/,https://b.example', false), ['https://a.example', 'https://b.example']);
assert.throws(() => parsePeerOrigins('http://a.example', false));
assert.deepEqual(parsePeerOrigins('http://node-b:3001', true), ['http://node-b:3001']);
assert.throws(() => parsePeerOrigins('https://a.example/api', false));
assert.throws(() => parsePeerOrigins('https://user@a.example', false));
});