import { describe, it, expect, beforeEach, vi } from 'vitest'
import { clearSubscribers, registerSubscriber } from '../../../server/utils/cotSubscribers.js'
import {
  isOpenSkyPollActive,
  resetTrackingFeedForTests,
  scheduleOpenSkyPollForTests,
} from '../../../server/utils/trackingFeed.js'

describe('trackingFeed', () => {
  beforeEach(() => {
    clearSubscribers()
    resetTrackingFeedForTests()
  })

  it('does not start OpenSky poll without SSE subscribers', () => {
    scheduleOpenSkyPollForTests()
    expect(isOpenSkyPollActive()).toBe(false)
  })

  it('starts OpenSky poll when a subscriber is registered', () => {
    registerSubscriber({
      bbox: { west: -123, south: 37, east: -122, north: 38 },
      layers: new Set(['air']),
      push: vi.fn(),
    })
    scheduleOpenSkyPollForTests()
    expect(isOpenSkyPollActive()).toBe(true)
  })
})
