import { describe, it, expect } from 'vitest'
import {
  buildVitalsObservations,
  isPlausibleValue,
  validateVitalsForm,
} from '@/composables/vitalsForm'

describe('vitalsForm', () => {
  it('validatesPlausibleRanges', () => {
    expect(isPlausibleValue('HEART_RATE', 78)).toBe(true)
    expect(isPlausibleValue('HEART_RATE', 350)).toBe(false)
    expect(isPlausibleValue('TEMP_C', 37.2)).toBe(true)
    expect(isPlausibleValue('TEMP_C', 10)).toBe(false)
  })

  it('requiresAtLeastOneValue', () => {
    const result = validateVitalsForm({
      heartRate: '',
      respRate: '',
      systolicBp: '',
      diastolicBp: '',
      spo2: '',
      tempC: '',
      avpu: '',
    })
    expect(result.valid).toBe(false)
    expect(result.errors._form).toBeTruthy()
  })

  it('buildsBatchObservations', () => {
    const observations = buildVitalsObservations({
      heartRate: '88',
      respRate: '18',
      systolicBp: '120',
      diastolicBp: '80',
      spo2: '97',
      tempC: '37.1',
      avpu: '0',
    }, '2026-06-23T12:00:00Z')

    expect(observations).toHaveLength(7)
    expect(observations[0]).toMatchObject({
      observationCode: 'HEART_RATE',
      value: 88,
      unit: 'bpm',
      source: 'Manual',
      recordedAt: '2026-06-23T12:00:00Z',
    })
    expect(observations.find(obs => obs.observationCode === 'AVPU')?.value).toBe(0)
  })
})
