import { describe, it, expect } from 'vitest'
import { detectNewCriticalAlerts, isNotifiableCriticalAlert } from '@/composables/criticalAlertDetect'

const criticalOpen = {
  id: 'a1',
  severity: 'CRITICAL',
  status: 'OPEN',
  alertType: 'SOFA_SEPSIS',
}

const criticalAcknowledged = {
  id: 'a2',
  severity: 'CRITICAL',
  status: 'ACKNOWLEDGED',
  alertType: 'NEWS2_EMERGENCY',
}

const warningOpen = {
  id: 'a3',
  severity: 'WARNING',
  status: 'OPEN',
  alertType: 'NEWS2_WARNING',
}

describe('criticalAlertDetect', () => {
  it('identifiesNotifiableCriticalAlerts', () => {
    expect(isNotifiableCriticalAlert(criticalOpen)).toBe(true)
    expect(isNotifiableCriticalAlert(criticalAcknowledged)).toBe(false)
    expect(isNotifiableCriticalAlert(warningOpen)).toBe(false)
  })

  it('seedsSeenIdsOnFirstPoll', () => {
    const result = detectNewCriticalAlerts([criticalOpen, warningOpen], [], false)
    expect(result.newAlerts).toEqual([])
    expect(result.nextSeenIds).toEqual(['a1'])
    expect(result.seeded).toBe(true)
  })

  it('detectsNewCriticalAlertsAfterSeed', () => {
    const seeded = detectNewCriticalAlerts([criticalOpen], [], false)
    const next = detectNewCriticalAlerts(
      [criticalOpen, { ...criticalOpen, id: 'a4', alertType: 'GCS_CRITICAL' }],
      seeded.nextSeenIds,
      seeded.seeded,
    )
    expect(next.newAlerts.map(alert => alert.id)).toEqual(['a4'])
  })
})
