import { describe, it, expect } from 'vitest'
import { parseNotamRss, extractNotamId } from './notam'

const mockXml = `<?xml version="1.0"?>
<rss version="2.0">
  <channel>
    <title>NOTAMs</title>
    <item>
      <title>D0273/25 NOTAMN PT</title>
      <description>Area restricted for drone operations</description>
      <link>https://notaminfo.com/notam/123</link>
      <pubDate>Mon, 22 Jun 2026 10:00:00 +0000</pubDate>
      <guid>notam-123</guid>
    </item>
  </channel>
</rss>`

describe('parseNotamRss', () => {
  it('parses RSS into notam objects', async () => {
    const notams = await parseNotamRss(mockXml)
    expect(notams).toHaveLength(1)
    expect(notams[0].title).toBe('D0273/25 NOTAMN PT')
  })
  it('extracts notamId from title', async () => {
    const notams = await parseNotamRss(mockXml)
    expect(notams[0].notamId).toBe('D0273/25')
  })
  it('parses pubDate as Date', async () => {
    const notams = await parseNotamRss(mockXml)
    expect(notams[0].publishedAt).toBeInstanceOf(Date)
  })
  it('returns empty for empty channel', async () => {
    const empty = `<?xml version="1.0"?><rss><channel></channel></rss>`
    expect(await parseNotamRss(empty)).toHaveLength(0)
  })
})

describe('extractNotamId', () => {
  it('extracts D-type NOTAM ID', () => expect(extractNotamId('D0273/25 NOTAMN PT')).toBe('D0273/25'))
  it('extracts A-type NOTAM ID', () => expect(extractNotamId('A1234/25 NOTAMN')).toBe('A1234/25'))
  it('returns title as fallback', () => expect(extractNotamId('No ID here')).toBe('No ID here'))
})
