import { describe, expect, it } from 'vitest'; import { hintsLeft, hintGateRemainingMs, hintLockMinutes } from './hints'; // view carries only the two fields hintsLeft reads. const view = (hintsRemaining: number, walletBalance: number) => ({ hintsRemaining, walletBalance }); describe('hintsLeft', () => { it('is zero without a view', () => { expect(hintsLeft(null, 5)).toBe(0); }); it('adds the per-game allowance to the live wallet (fresh view)', () => { // hints_remaining 4 = allowance 1 + wallet 3; live wallet matches the snapshot → 1 + 3. expect(hintsLeft(view(4, 3), 3)).toBe(4); }); it('reflects the LIVE wallet, not the per-game snapshot (the staleness fix)', () => { // The view was fetched when the wallet was 3 (allowance 1), but a wallet hint was since spent // in another game, so the live wallet is 2: the count must drop to 1 + 2 = 3, not stay at 4. expect(hintsLeft(view(4, 3), 2)).toBe(3); }); it('shows just the wallet when the per-game allowance is used up', () => { // allowance 0 (hints_remaining 3 == snapshot wallet 3); live wallet 3 → 0 + 3. expect(hintsLeft(view(3, 3), 3)).toBe(3); }); it('clamps a non-negative allowance and wallet', () => { expect(hintsLeft(view(2, 3), 0)).toBe(0); expect(hintsLeft(view(1, 0), -5)).toBe(1); }); }); describe('hintGateRemainingMs (the vs_ai idle-hint gate, monotonic)', () => { it('is 0 when the gate is open — a null anchor (the human first move, or a non-gated game)', () => { expect(hintGateRemainingMs(null, 60_000, 5000)).toBe(0); }); it('is the window remaining minus the monotonic time elapsed since the anchor', () => { // anchored at mono 1000 with 60s left; 20s of monotonic time later -> 40s left. expect(hintGateRemainingMs(1000, 60_000, 1000 + 20_000)).toBe(40_000); }); it('is 0 once the elapsed monotonic time reaches (or passes) the window', () => { expect(hintGateRemainingMs(1000, 60_000, 1000 + 60_000)).toBe(0); expect(hintGateRemainingMs(1000, 60_000, 1000 + 90_000)).toBe(0); }); }); describe('hintLockMinutes (the toast N, rounded up)', () => { it('rounds up to whole minutes, so a still-closed gate never reads 0', () => { expect(hintLockMinutes(1)).toBe(1); expect(hintLockMinutes(60_000)).toBe(1); expect(hintLockMinutes(61_000)).toBe(2); expect(hintLockMinutes(29 * 60_000)).toBe(29); }); it('is 0 when nothing remains', () => { expect(hintLockMinutes(0)).toBe(0); }); });