import { describe, expect, it } from 'vitest';
import { hintsLeft } 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);
  });
});
