import { describe, it, expect } from 'vitest'
import {
  emptyThresholdForm,
  thresholdFormToPayload,
  validateThresholdForm,
} from '@/composables/thresholdForm'

describe('thresholdForm', () => {
  it('validatesRequiredFields', () => {
    const result = validateThresholdForm(emptyThresholdForm())
    expect(result.valid).toBe(false)
    expect(result.errors.observationCode).toBeTruthy()
  })

  it('validatesThresholdOrdering', () => {
    const result = validateThresholdForm({
      observationCode: 'HEART_RATE',
      displayName: 'Heart Rate',
      unit: 'bpm',
      criticalLow: '60',
      warningLow: '50',
      warningHigh: '100',
      criticalHigh: '130',
    })
    expect(result.valid).toBe(false)
    expect(result.errors.warningLow).toBeTruthy()
  })

  it('buildsPayloadWithNullBounds', () => {
    const payload = thresholdFormToPayload({
      observationCode: 'SPO2',
      displayName: 'SpO₂',
      unit: '%',
      criticalLow: '',
      warningLow: '92',
      warningHigh: '',
      criticalHigh: '88',
    })
    expect(payload).toEqual({
      observationCode: 'SPO2',
      displayName: 'SpO₂',
      unit: '%',
      criticalLow: null,
      warningLow: 92,
      warningHigh: null,
      criticalHigh: 88,
    })
  })
})
