import { describe, it, expect } from 'vitest'
import {
  entitiesToFeatures,
  getCotClusters,
  loadCotCluster,
} from '../../app/utils/cotMapLayer.js'

function makeEntities(n, centerLat = 37.7, centerLng = -122.4) {
  return Array.from({ length: n }, (_, i) => ({
    id: `ICAO.${i}`,
    lat: centerLat + (i % 10) * 0.01,
    lng: centerLng + Math.floor(i / 10) * 0.01,
    type: 'a-f-A-C-F',
    label: `AC${i}`,
  }))
}

describe('cotMapLayer', () => {
  it('converts entities to GeoJSON points', () => {
    const features = entitiesToFeatures([
      { id: 'ICAO.1', lat: 37.7, lng: -122.4, type: 'a-f-A-C-F' },
      { id: 'bad', lat: 'x', lng: 0 },
    ])
    expect(features).toHaveLength(1)
    expect(features[0].geometry.coordinates).toEqual([-122.4, 37.7])
    expect(features[0].properties.entity.id).toBe('ICAO.1')
  })

  it('clusters dense tracks at low zoom', () => {
    loadCotCluster(makeEntities(50))
    const view = { west: -123, south: 37, east: -122, north: 38, zoom: 6 }
    const clusters = getCotClusters(view)
    const clusterCount = clusters.filter(f => f.properties?.cluster).length
    const pointCount = clusters.filter(f => !f.properties?.cluster).length
    expect(clusterCount).toBeGreaterThan(0)
    expect(clusterCount + pointCount).toBeLessThan(50)
  })

  it('shows individual markers at high zoom', () => {
    loadCotCluster(makeEntities(20))
    const view = { west: -123, south: 37, east: -122, north: 38, zoom: 15 }
    const clusters = getCotClusters(view)
    expect(clusters.every(f => !f.properties?.cluster)).toBe(true)
    expect(clusters).toHaveLength(20)
  })
})
