import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'
import {
  formatLengthOfStay,
  observationStaleness,
  patientRoom,
  stalenessClass,
} from '@/composables/wardFormat'

describe('wardFormat', () => {
  beforeEach(() => {
    vi.useFakeTimers()
    vi.setSystemTime(new Date('2026-06-23T14:00:00Z'))
  })

  afterEach(() => {
    vi.useRealTimers()
  })

  it('formatsLengthOfStay', () => {
    expect(formatLengthOfStay('2026-06-21T14:00:00Z')).toBe('2d')
    expect(formatLengthOfStay('2026-06-23T10:00:00Z')).toBe('4h')
  })

  it('detectsStaleObservations', () => {
    expect(observationStaleness('2026-06-23T13:00:00Z', 'Active')).toBe('fresh')
    expect(observationStaleness('2026-06-23T10:00:00Z', 'Active')).toBe('stale')
    expect(observationStaleness('2026-06-23T08:00:00Z', 'Active')).toBe('critical')
    expect(observationStaleness(null, 'Active')).toBe('missing')
    expect(observationStaleness('2026-06-23T08:00:00Z', 'Discharged')).toBeNull()
  })

  it('mapsStalenessClasses', () => {
    expect(stalenessClass('stale')).toContain('amber')
    expect(stalenessClass('critical')).toContain('red')
  })

  it('usesRoomBedWithFallback', () => {
    expect(patientRoom({ roomBed: 'ICU-1' })).toBe('ICU-1')
    expect(patientRoom({ room: '101' })).toBe('101')
  })
})
