import { expect } from 'chai'

import { formatTimespan, formatTime } from '../formatTimespan'

describe('formatTimespan', () => {
  it('formats timespans less than one minute', () => {
    const startTime = new Date()
    const fifteenSeconds = 15 * 1000
    const endTime = new Date(startTime.getTime() + fifteenSeconds)
    const result = formatTimespan(startTime, endTime)
    expect(result).to.equal('15 seconds')
  })

  it('formats timespans over a minute', () => {
    const startTime = new Date()
    const twoMinutes = 2 * 60 * 1000
    const fifteenSeconds = 15 * 1000
    const endTime = new Date(startTime.getTime() + twoMinutes + fifteenSeconds)
    const result = formatTimespan(startTime, endTime)
    expect(result).to.equal('2 minutes 15 seconds')
  })

  it('formats timespans over an hour', () => {
    const startTime = new Date()
    const sixHours = 6 * 60 * 60 * 1000
    const twoMinutes = 2 * 60 * 1000
    const fifteenSeconds = 15 * 1000
    const endTime = new Date(startTime.getTime() + sixHours + twoMinutes + fifteenSeconds)
    const result = formatTimespan(startTime, endTime)
    expect(result).to.equal('6 hours 2 minutes 15 seconds')
  })

  it('excludes zero-duration segments', () => {
    const startTime = new Date()
    const sixHours = 6 * 60 * 60 * 1000
    const endTime = new Date(startTime.getTime() + sixHours)
    const result = formatTimespan(startTime, endTime)
    expect(result).to.equal('6 hours')
  })

  it('displays 0 seconds when less than a second', () => {
    const startTime = new Date()
    const endTime = new Date(startTime.getTime() + 999)
    const result = formatTimespan(startTime, endTime)
    expect(result).to.equal('0 seconds')
  })

  it('displays 0 seconds when no time difference', () => {
    const startTime = new Date()
    const result = formatTimespan(startTime, startTime)
    expect(result).to.equal('0 seconds')
  })

  it('formats singulars', () => {
    const startTime = new Date()
    const oneHour = 1 * 60 * 60 * 1000
    const oneMinute = 1 * 60 * 1000
    const oneSecond = 1 * 1000
    const endTime = new Date(startTime.getTime() + oneHour + oneMinute + oneSecond)
    const result = formatTimespan(startTime, endTime)
    expect(result).to.equal('1 hour 1 minute 1 second')
  })
})

describe('formatTime', () => {
  it('formats times less than one minute', () => {
    expect(formatTime(15)).to.equal('15 seconds')
  })

  it('formats times over a minute', () => {
    expect(formatTime((2 * 60) + 15)).to.equal('2 minutes 15 seconds')
  })

  it('formats times over an hour', () => {
    expect(formatTime((6 * 3600) + 135)).to.equal('6 hours 2 minutes 15 seconds')
  })

  it('excludes zero segments', () => {
    expect(formatTime(6 * 3600)).to.equal('6 hours')
  })

  it('displays 0 seconds when less than a second', () => {
    expect(formatTime(0.99)).to.equal('0 seconds')
    expect(formatTime(0.5)).to.equal('0 seconds')
    expect(formatTime(0)).to.equal('0 seconds')
  })

  it('formats singulars', () => {
    expect(formatTime(3600 + 61)).to.equal('1 hour 1 minute 1 second')
  })
})
