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>
48 lines
1.5 KiB
TypeScript
48 lines
1.5 KiB
TypeScript
/**
|
|
* Tiny dependency-free property-testing helper. Each case gets its own seed
|
|
* so a failure reports a reproducible seed: rerun with PROP_SEED=<seed>.
|
|
*/
|
|
export function mulberry32(seed: number): () => number {
|
|
let a = seed >>> 0;
|
|
return () => {
|
|
a = (a + 0x6d2b79f5) >>> 0;
|
|
let t = a;
|
|
t = Math.imul(t ^ (t >>> 15), t | 1);
|
|
t ^= t + Math.imul(t ^ (t >>> 7), t | 61);
|
|
return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
|
|
};
|
|
}
|
|
|
|
export interface Gen {
|
|
rand: () => number;
|
|
int(min: number, max: number): number;
|
|
pick<T>(items: readonly T[]): T;
|
|
bool(p?: number): boolean;
|
|
}
|
|
|
|
export function gen(seed: number): Gen {
|
|
const rand = mulberry32(seed);
|
|
return {
|
|
rand,
|
|
int: (min, max) => min + Math.floor(rand() * (max - min + 1)),
|
|
pick: items => {
|
|
if (!items.length) throw new Error('pick from empty list');
|
|
return items[Math.floor(rand() * items.length)];
|
|
},
|
|
bool: (p = 0.5) => rand() < p,
|
|
};
|
|
}
|
|
|
|
export function forAll(runs: number, property: (g: Gen, seed: number) => void, baseSeed = 0x5eed): void {
|
|
const fixed = process.env.PROP_SEED;
|
|
const seeds = fixed ? [Number(fixed)] : Array.from({ length: runs }, (_, i) => (baseSeed + i * 2654435761) >>> 0);
|
|
for (const seed of seeds) {
|
|
try {
|
|
property(gen(seed), seed);
|
|
} catch (error) {
|
|
const message = error instanceof Error ? error.message : String(error);
|
|
throw new Error(`Property failed for seed ${seed} (rerun with PROP_SEED=${seed}): ${message}`, { cause: error });
|
|
}
|
|
}
|
|
}
|