import { describe, it, expect } from 'vitest';
import { fmt, fmtDate, localDateString, todayStr } from './utils';

describe('fmt (money display)', () => {
  it('formats with thousands separators and two decimals', () => {
    expect(fmt(0)).toBe('$0.00');
    expect(fmt(1234.5)).toBe('$1,234.50');
    expect(fmt(1234567.89)).toBe('$1,234,567.89');
  });

  it('treats null/undefined as zero', () => {
    expect(fmt(null)).toBe('$0.00');
    expect(fmt(undefined)).toBe('$0.00');
  });

  it('places the minus sign before the currency symbol (QA-B6-01)', () => {
    expect(fmt(-50)).toBe('-$50.00');
    expect(fmt(-1234.5)).toBe('-$1,234.50');
    expect(fmt(-0.99)).toBe('-$0.99');
  });
});

describe('local dates', () => {
  it('fmtDate renders M/D/YYYY from ISO', () => {
    expect(fmtDate('2026-06-05')).toBe('6/5/2026');
    expect(fmtDate(null)).toBe('—');
  });

  it('localDateString uses local calendar parts (no UTC drift)', () => {
    // 23:30 local on Jan 31 must stay Jan 31 regardless of timezone
    const lateNight = new Date(2026, 0, 31, 23, 30, 0);
    expect(localDateString(lateNight)).toBe('2026-01-31');
    const earlyMorning = new Date(2026, 5, 1, 0, 5, 0);
    expect(localDateString(earlyMorning)).toBe('2026-06-01');
  });

  it('todayStr matches localDateString for now', () => {
    expect(todayStr()).toBe(localDateString(new Date()));
  });
});
