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

const criticalOpen = {
  id: 'a1',
  severity: 'Critical',
  status: 'Open',
  alertType: 'SofaSepsis',
}

const criticalAcknowledged = {
  id: 'a2',
  severity: 'Critical',
  status: 'Acknowledged',
  alertType: 'News2Emergency',
}

const warningOpen = {
  id: 'a3',
  severity: 'Warning',
  status: 'Open',
  alertType: 'News2Warning',
}

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: 'GcsCritical' }],
      seeded.nextSeenIds,
      seeded.seeded,
    )
    expect(next.newAlerts.map(alert => alert.id)).toEqual(['a4'])
  })
})
