import { describe, it, expect } from 'vitest'
import { defineComponent } from 'vue'
import { mount } from '@vue/test-utils'
import { useChartData } from '@/composables/useChartData'

function mountChartData(observations, code) {
  let exposed
  const Comp = defineComponent({
    setup() {
      exposed = useChartData(observations, code)
      return () => null
    },
  })
  mount(Comp)
  return exposed
}

const observations = [
  { observationCode: 'HEART_RATE', value: 80, recordedAt: '2026-06-19T12:00:00Z' },
  { observationCode: 'RESP_RATE', value: 18, recordedAt: '2026-06-19T12:00:00Z' },
  { observationCode: 'HEART_RATE', value: 72, recordedAt: '2026-06-19T11:00:00Z' },
]

describe('useChartData', () => {
  it('filtersObservationsByCode', () => {
    const { chartData } = mountChartData(observations, 'HEART_RATE')
    expect(chartData.value.datasets[0].data).toEqual([72, 80])
    expect(chartData.value.labels).toHaveLength(2)
    expect(chartData.value.timestamps).toEqual([
      '2026-06-19T11:00:00Z',
      '2026-06-19T12:00:00Z',
    ])
  })

  it('sortsChronologically', () => {
    const { chartData } = mountChartData(observations, 'HEART_RATE')
    expect(chartData.value.datasets[0].data[0]).toBe(72)
    expect(chartData.value.datasets[0].data[1]).toBe(80)
  })

  it('handlesEmptyData', () => {
    const { chartData } = mountChartData([], 'HEART_RATE')
    expect(chartData.value.labels).toEqual([])
    expect(chartData.value.datasets[0].data).toEqual([])
  })
})
