import { describe, expect, it } from 'vitest';
import { formatDate, formatLocaleNumber, formatNumber, toNum } from './format';

describe('format utilities', () => {
  it('coerces numeric input safely', () => {
    expect(toNum(' 12.5 ')).toBe(12.5);
    expect(toNum(3)).toBe(3);
    expect(toNum('')).toBeNull();
    expect(toNum(Number.NaN)).toBeNull();
    expect(toNum('not a number')).toBeNull();
  });

  it('formats fixed and locale numbers', () => {
    expect(formatNumber(12.345, 2)).toBe('12.35');
    expect(formatNumber(null, 2)).toBe('N/A');
    expect(formatLocaleNumber(1234.56, 1, 'en-AU')).toBe('1,234.6');
    expect(formatLocaleNumber(undefined, 1)).toBe('-');
  });

  it('formats dates with a fallback for empty values', () => {
    expect(formatDate(null)).toBe('-');
    expect(formatDate('not-a-date')).toBe('not-a-date');
    expect(formatDate('2026-06-04', { day: '2-digit', month: 'short', year: 'numeric' }, 'en-AU')).toContain('2026');
  });
});
