import { describe, expect, it } from 'vitest'
import {
  formatBigIntWhole,
  formatInteger,
  formatRelativeAgeAt,
  formatTimestamp,
  formatWeiAsEth,
} from './format'

describe('formatWeiAsEth', () => {
  it('formats zero and whole ETH values without losing precision', () => {
    expect(formatWeiAsEth('0')).toBe('0 ETH')
    expect(formatWeiAsEth('1000000000000000000')).toBe('1 ETH')
    expect(formatWeiAsEth('123450000000000000000')).toBe('123.45 ETH')
  })

  it('truncates fractional ETH safely using bigint math', () => {
    expect(formatWeiAsEth('123456789123456789')).toBe('0.1234 ETH')
    expect(formatWeiAsEth('9007199254740993')).toBe('0.009 ETH')
  })
})

describe('formatInteger', () => {
  it('uses en-US grouping for finite numbers', () => {
    expect(formatInteger(1234567)).toBe('1,234,567')
    expect(formatInteger(null)).toBe('0')
  })
})

describe('formatBigIntWhole', () => {
  it('groups bigint digits with commas', () => {
    expect(formatBigIntWhole(1234567890123456789n)).toBe('1,234,567,890,123,456,789')
    expect(formatBigIntWhole(-42n)).toBe('-42')
  })
})

describe('formatTimestamp', () => {
  it('uses fixed en-US locale formatting', () => {
    const formatted = formatTimestamp('2026-01-15T12:30:00.000Z')
    expect(formatted).toMatch(/1\/15\/2026/)
    expect(formatted).toMatch(/PM|AM/)
  })
})

describe('formatRelativeAgeAt', () => {
  it('computes relative age from a fixed reference time', () => {
    const reference = Date.parse('2026-06-21T12:00:00.000Z')
    expect(formatRelativeAgeAt(reference, '2026-06-21T11:59:30.000Z')).toBe('30s ago')
  })
})
