forked from WikiDeck/wikideck
- keywords.ts: viral, rising, local, encyclopedic, nostalgic, translated (v1); edition, obscure, wildcard (off by default); §10.3 Portals with capacity, allOnTopic and topicBonus hooks; isOnTopic, keywordEnabled, hiddenFrom - ruleset.ts: §10.3 default Topic pool, keyword/portal param defaults - starter.ts: placeholder loaners carry their faction keyword (§10.1) - derive.ts: percentile -> fame -> cost -> power, home, trend, keyword choice - tests: game-keywords, game-derive, game-property, game-golden (§12, todo until core implements the frozen §3-§7 rules) Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
133 lines
7.7 KiB
TypeScript
133 lines
7.7 KiB
TypeScript
import test from 'node:test';
|
||
import assert from 'node:assert/strict';
|
||
import {
|
||
DEFAULT_DERIVATION, 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 = DEFAULT_DERIVATION;
|
||
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 t = (trendPercent: number | null, dailyAverage: number | null = 100) => trendFor({ trendPercent, dailyAverage }, RULES);
|
||
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(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);
|
||
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' });
|
||
const cheap = deriveCard(input({ attention: 5, depth: 5, reach: 5 }, { check: 'loaner' }), TABLE);
|
||
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));
|
||
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);
|
||
const unchecked = deriveCard(input(stats, { check: 'unchecked' }), TABLE);
|
||
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).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);
|
||
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).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);
|
||
});
|