/** Computer levels and human-like choices (docs/09-game-engine.md §9.1). */ import { describe, expect, test } from 'bun:test'; import { SeededRng, secureRandom, seedN } from '@bg/engine'; import { BOT_PLAY_ANCHORS, botPlay, humanBest } from './policy'; import { botDraw, botPersona, idLevel, type BotLevel } from './persona'; const CROCKFORD = '0123456789ABCDEFGHJKMNPQRSTVWXYZ'; const fakeUlid = (r: () => number) => '01J' + Array.from({ length: 23 }, () => CROCKFORD[Math.floor(r() * 32)]).join(''); describe('secureRandom', () => { test('uniform floats in [0, 1)', () => { const n = 100_000; const bins = new Array(10).fill(0); for (let i = 0; i < n; i++) { const x = secureRandom(); expect(x >= 0 && x < 1).toBe(true); bins[Math.floor(x * 10)]++; } for (const b of bins) expect(Math.abs(b - n / 10)).toBeLessThan(n / 10 * 0.05); }); }); describe('bot level', () => { test('a computer without a saved level gets one from its id’s random characters, the same every time', () => { const id = `bot:2:${fakeUlid(secureRandom)}`; expect(idLevel(id)).toBe(idLevel(id)); expect(botDraw('bot:1:01J0000000000000000000000Z')).toBe(0); expect(botDraw('bot:1:01J0000000ZZZZZZ0000000000')).toBeCloseTo(1, 6); expect(idLevel('bot:1:01J0000000000000000000000Z')).toBe(1); expect(idLevel('bot:1:01J0000000ZZZZZZ0000000000')).toBe(60); }); test('id levels are uniform over 1~60; 똑똑한 편 (start above 30) is half', () => { const rng = SeededRng.fromSeed(seedN(3)); const n = 60_000; const bands = [0, 0, 0]; let smart = 0; for (let i = 0; i < n; i++) { const id = `bot:${(i % 7) + 1}:${fakeUlid(() => rng.next())}`; const level = idLevel(id); expect(level >= 1 && level <= 60).toBe(true); bands[Math.floor((level - 1) / 20)]!++; if (botPersona(id).smart) smart++; } for (const b of bands) expect(Math.abs(b / n - 1 / 3)).toBeLessThan(0.015); expect(Math.abs(smart / n - 0.5)).toBeLessThan(0.015); expect(botPersona('bot:1:x', 30).smart).toBe(false); expect(botPersona('bot:1:x', 31).smart).toBe(true); }); test('play settings move steadily from level 1 to 60 and hit the anchors', () => { for (const [level, play] of BOT_PLAY_ANCHORS) { const p = botPlay(level); for (const k of ['slip', 'window', 'lapse', 'miss'] as const) expect(p[k]).toBeCloseTo(play[k], 9); } for (let l = 2; l <= 60; l++) { const a = botPlay(l - 1); const b = botPlay(l); expect(b.slip).toBeLessThanOrEqual(a.slip); expect(b.lapse).toBeLessThanOrEqual(a.lapse); expect(b.miss).toBeLessThanOrEqual(a.miss); } expect(botPlay(0)).toEqual(botPlay(1)); expect(botPlay(99)).toEqual(botPlay(60)); }); }); describe('humanBest', () => { // Scores: 100 best, 95 close, 60 a slip away for easy, 0 a blunder. const moves = ['best', 'close', 'meh', 'blunder']; const value: Record = { best: 100, close: 95, meh: 70, blunder: 0 }; const scale = { small: 10, critical: 1000 }; const share = (level: BotLevel | undefined, xs = moves, v = value) => { const rng = SeededRng.fromSeed(seedN(11)); const n = 20_000; const out: Record = {}; for (let i = 0; i < n; i++) { const m = humanBest(xs, (a) => v[a]!, () => rng.next(), level, scale); out[m] = (out[m] ?? 0) + 1; } for (const k in out) out[k]! /= n; return out; }; test('without a level it is the plain best move', () => { expect(share(undefined)).toEqual({ best: 1 }); }); test('stronger levels play the best move more often and never pick a move far worse than a lapse allows', () => { const weakest = share(1); const easy = share(10); const normal = share(30); const hard = share(50); const strongest = share(60); expect(weakest.best!).toBeLessThan(easy.best!); expect(hard.best!).toBeLessThan(strongest.best!); expect(strongest.best!).toBeGreaterThan(0.96); expect(easy.best!).toBeLessThan(normal.best!); expect(normal.best!).toBeLessThan(hard.best!); expect(hard.best!).toBeGreaterThan(0.9); // "meh" (30 worse = 3 × small) is past every slip window (≤ 2 × small): only a lapse (8 × small) plays it. expect(hard.meh ?? 0).toBeLessThan(0.01); // 100 worse is beyond every window: never played. for (const s of [weakest, easy, normal, hard, strongest]) expect(s.blunder ?? 0).toBe(0); // Close alternative: roughly slip share of the time. expect(Math.abs((normal.close ?? 0) - (botPlay(30).slip + botPlay(30).lapse / 2))).toBeLessThan(0.03); }); test('a must-play move (≥ critical) is missed only at the documented rate', () => { const v = { ...value, best: 5000 }; expect(share(50, moves, v).best).toBe(1); expect(share(60, moves, v).best).toBe(1); for (const level of [1, 10, 30, 41]) expect(Math.abs(1 - share(level, moves, v).best! - botPlay(level).miss)).toBeLessThan(0.015); }); });