import { describe, expect, it } from 'vitest'
import {
  DEFAULT_REPORT_URL_STATE,
  parseReportUrlState,
  serializeReportUrlState,
} from './report-url-state'

describe('report URL state', () => {
  it('uses a trustworthy 24 hour default for an empty URL', () => {
    expect(parseReportUrlState('')).toEqual(DEFAULT_REPORT_URL_STATE)
    expect(serializeReportUrlState(DEFAULT_REPORT_URL_STATE)).toBe('')
  })

  it('round-trips repeated filters, range, severity, and search', () => {
    const search = serializeReportUrlState({
      range: '7d',
      projects: ['overdeck', 'deck'],
      sources: ['actions', 'controller'],
      severity: 'warn',
      q: 'failed deploy',
    })

    expect(parseReportUrlState(search)).toEqual({
      range: '7d',
      projects: ['overdeck', 'deck'],
      sources: ['actions', 'controller'],
      severity: 'warn',
      q: 'failed deploy',
    })
  })

  it('accepts extended retained-history ranges', () => {
    expect(parseReportUrlState('?range=30d').range).toBe('30d')
    expect(parseReportUrlState('?range=90d').range).toBe('90d')
  })

  it('drops unsupported controls and deduplicates repeated filters', () => {
    expect(parseReportUrlState('?range=365d&severity=critical&project=overdeck&project=overdeck&q=++')).toEqual({
      range: '24h',
      projects: ['overdeck'],
      sources: [],
      q: '',
    })
  })
})
