import { describe, expect, it } from 'vitest';
import { durationToMs } from './duration';

describe('durationToMs', () => {
  it('parses each unit', () => {
    expect(durationToMs('90s')).toBe(90_000);
    expect(durationToMs('15m')).toBe(900_000);
    expect(durationToMs('2h')).toBe(7_200_000);
    expect(durationToMs('30d')).toBe(2_592_000_000);
  });

  it('tolerates surrounding whitespace and a space before the unit', () => {
    expect(durationToMs('  15 m ')).toBe(900_000);
  });

  it('throws on anything unparseable', () => {
    for (const bad of ['', 'abc', '15', 'm', '15y', '-3m', '1.5h']) {
      expect(() => durationToMs(bad)).toThrow();
    }
  });
});
