import { describe, expect, it } from 'vitest'

import { cn, msToTime, originalCreatedAtString, toDate } from '@/lib/utils'

describe ('utils', () => {
  it ('converts strings to dates and leaves date instances intact', () => {
    const date = new Date ('2026-01-02T03:04:05Z')

    expect (toDate ('2026-01-02T03:04:05Z')).toBeInstanceOf (Date)
    expect (toDate (date)).toBe (date)
  })

  it ('merges conditional Tailwind classes', () => {
    const hidden = false

    expect (cn ('p-2', hidden && 'hidden', 'p-4')).toBe ('p-4')
  })

  it ('renders unknown original creation time ranges', () => {
    expect (originalCreatedAtString (null, null)).toBe ('年月日不詳')
    expect (
        originalCreatedAtString (
            '2026-01-01T00:00:00+09:00',
            '2026-01-02T00:00:00+09:00',
        ),
    ).toContain ('時刻不詳')
  })
})

describe ('msToTime', () => {
  it ('keeps milliseconds when present', () => {
    expect (msToTime (60_500)).toBe ('1:00.500')
    expect (msToTime (60_001)).toBe ('1:00.001')
  })

  it ('omits milliseconds when they are zero', () => {
    expect (msToTime (60_000)).toBe ('1:00')
  })
})
