// HP formula roll: parse "2d6+9" style, roll, return total.
const shared = require('@ttrpg/shared');
const { rollHpFormula } = shared;

describe('rollHpFormula', () => {
  test('basic NdM', () => {
    const r = rollHpFormula('2d6');
    expect(r).toBeGreaterThanOrEqual(2);
    expect(r).toBeLessThanOrEqual(12);
  });

  test('NdM+bonus', () => {
    const r = rollHpFormula('2d6+9');
    expect(r).toBeGreaterThanOrEqual(11);
    expect(r).toBeLessThanOrEqual(21);
  });

  test('NdM-bonus', () => {
    const r = rollHpFormula('3d8-2');
    expect(r).toBeGreaterThanOrEqual(1);
    expect(r).toBeLessThanOrEqual(22);
  });

  test('single die', () => {
    const r = rollHpFormula('1d20');
    expect(r).toBeGreaterThanOrEqual(1);
    expect(r).toBeLessThanOrEqual(20);
  });

  test('case insensitive', () => {
    const r = rollHpFormula('2D6+5');
    expect(r).toBeGreaterThanOrEqual(7);
    expect(r).toBeLessThanOrEqual(17);
  });

  test('whitespace tolerant', () => {
    const r = rollHpFormula(' 2d6 + 9 ');
    expect(r).toBeGreaterThanOrEqual(11);
    expect(r).toBeLessThanOrEqual(21);
  });

  test('large die', () => {
    const r = rollHpFormula('4d12+10');
    expect(r).toBeGreaterThanOrEqual(14);
    expect(r).toBeLessThanOrEqual(58);
  });

  test('invalid returns null', () => {
    expect(rollHpFormula('abc')).toBeNull();
    expect(rollHpFormula('')).toBeNull();
    expect(rollHpFormula(null)).toBeNull();
    expect(rollHpFormula(undefined)).toBeNull();
    expect(rollHpFormula('2d')).toBeNull();
    expect(rollHpFormula('d6')).toBeNull();
  });

  test('zero dice invalid', () => {
    expect(rollHpFormula('0d6')).toBeNull();
  });
});
