import { describe, it, expect } from 'bun:test'
import { formatCurrency, roundCurrency, toCents, toDollars } from '../../src/utils/currency.js'

describe('formatCurrency', () => {
  it('formats whole dollars', () => {
    expect(formatCurrency(100)).toBe('$100.00')
  })

  it('formats cents', () => {
    expect(formatCurrency(49.99)).toBe('$49.99')
  })

  it('formats zero', () => {
    expect(formatCurrency(0)).toBe('$0.00')
  })

  it('formats thousands with comma', () => {
    expect(formatCurrency(1234.56)).toBe('$1,234.56')
  })

  it('formats negative amounts', () => {
    expect(formatCurrency(-25.50)).toBe('-$25.50')
  })
})

describe('roundCurrency', () => {
  it('rounds to 2 decimal places', () => {
    expect(roundCurrency(1.005)).toBe(1.01)
    expect(roundCurrency(1.004)).toBe(1)
  })

  it('handles already-rounded values', () => {
    expect(roundCurrency(10.50)).toBe(10.50)
  })

  it('handles zero', () => {
    expect(roundCurrency(0)).toBe(0)
  })
})

describe('toCents', () => {
  it('converts dollars to cents', () => {
    expect(toCents(1.00)).toBe(100)
    expect(toCents(49.99)).toBe(4999)
  })

  it('rounds fractional cents', () => {
    // 1.005 * 100 = 100.49999... in IEEE 754, so Math.round gives 100
    // This is a known floating point limitation — use toCents(roundCurrency(x)) for precision
    expect(toCents(1.005)).toBe(100)
    expect(toCents(1.006)).toBe(101)
  })

  it('handles zero', () => {
    expect(toCents(0)).toBe(0)
  })
})

describe('toDollars', () => {
  it('converts cents to dollars', () => {
    expect(toDollars(100)).toBe(1.00)
    expect(toDollars(4999)).toBe(49.99)
  })

  it('handles zero', () => {
    expect(toDollars(0)).toBe(0)
  })
})
