Files
wikideck/tests/game-derive.test.ts

106 lines
5.0 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 {
percentile, percentiles, fame, costFor, homeStat, trendFor, deriveCard,
type CardInput, type PercentileTable, type DerivationRules,
} from '../shared/game/derive.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 = {
costThresholds: [15, 35, 55, 75, 90], powerByCost: [0, 1, 2, 4, 5, 7, 9], trendUp: 25, trendDown: -25,
};
function input(stats: CardInput['snapshot']['stats'], trendPercent: number | null = null): CardInput {
return { cardId: 'c1', pageId: 1, title: 'T', category: 'Science', rarity: 'Common',
snapshot: { stats }, live: { trendPercent } };
}
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); // 49 below + 0.5 equal → 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); // 1 + 2/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); // 10.5 → 11
assert.equal(fame({ attention: 10, depth: 10, reach: 11 }), 10); // 10.33 → 10
assert.equal(fame({ attention: 10, depth: 11, reach: 11 }), 11); // 10.67 → 11
});
test('cost is 1 + thresholds ≤ fame, spanning 1–6 (§1.2)', () => {
const t = RULES.costThresholds;
assert.equal(costFor(0, t), 1);
assert.equal(costFor(14, t), 1);
assert.equal(costFor(15, t), 2);
assert.equal(costFor(54, t), 3);
assert.equal(costFor(55, t), 4);
assert.equal(costFor(89, t), 5);
assert.equal(costFor(90, t), 6);
assert.equal(costFor(100, t), 6);
});
test('home stat is the highest percentile, ties broken depth > reach > attention (§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: 60, depth: 40, reach: 60 }), 'reach');
assert.equal(homeStat({ attention: null, depth: 0, reach: 0 }), 'depth');
assert.equal(homeStat({ attention: null, depth: 0, reach: 1 }), 'reach');
});
test('trend is ±1 at the inclusive thresholds and 0 for null (§1.6)', () => {
assert.equal(trendFor(25, RULES), 1);
assert.equal(trendFor(24.9, RULES), 0);
assert.equal(trendFor(-25, RULES), -1);
assert.equal(trendFor(-24.9, RULES), 0);
assert.equal(trendFor(null, RULES), 0);
assert.equal(trendFor(Number.NaN, RULES), 0);
assert.equal(trendFor(1e9, RULES), 1); // inflated live views are still worth only +1
});
test('deriveCard assembles cost, power, home, faction and trend', () => {
const card = deriveCard(input({ attention: 81, depth: 71, reach: 61 }, 40), RULES, TABLE);
// pct 80/70/60 → fame 70 → cost 4 → power 5
assert.deepEqual(card, { cardId: 'c1', pageId: 1, title: 'T', faction: 'Science', rarity: 'Common',
cost: 4, power: 5, home: 'attention', trend: 1, keyword: null });
});
test('deriveCard passes the keyword picker the derived card', () => {
const card = deriveCard(input({ attention: 10, depth: 20, reach: 30 }), RULES, TABLE,
base => `${base.faction}:${base.home}:${base.cost}`);
assert.equal(card.keyword, 'Science:reach:2');
});
test('power depends only on cost: forged 100/100/100 stats only move a card up the curve (§1.3)', () => {
const forged = deriveCard(input({ attention: 100, depth: 100, reach: 100 }), RULES, TABLE);
assert.equal(forged.cost, 6);
assert.equal(forged.power, RULES.powerByCost[6]);
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, g.bool(0.2) ? null : g.int(-100, 100)), RULES, TABLE);
assert.ok(card.cost >= 1 && card.cost <= 6);
assert.equal(card.power, RULES.powerByCost[card.cost]);
assert.ok(stats[card.home] !== null, 'home is never a null stat');
// Raising any single stat never lowers cost.
const bumped = { ...stats, depth: Math.min(100, stats.depth + g.int(1, 30)) };
assert.ok(deriveCard(input(bumped), RULES, TABLE).cost >= card.cost);
});
});
test('deriveCard rejects a rules config without a power for the derived cost', () => {
assert.throws(() => deriveCard(input({ attention: 100, depth: 100, reach: 100 }),
{ ...RULES, powerByCost: [0, 1, 2] }, TABLE), RangeError);
});