import { describe, expect, it } from 'vitest'
import {
  buildLoginPath,
  buildProtectedDownloadPath,
  buildRegisterPath,
  buildSupportPath,
  isExternalHref,
  normalizeDownloadPlatform,
} from '../site-routes'

describe('site route helpers', () => {
  it('builds protected download paths that preserve the requested platform', () => {
    expect(buildProtectedDownloadPath('windows')).toBe('/app/downloads?platform=windows')
    expect(buildProtectedDownloadPath('macos')).toBe('/app/downloads?platform=macos')
    expect(buildProtectedDownloadPath()).toBe('/app/downloads')
  })

  it('normalizes supported platform query values and rejects unknown values', () => {
    expect(normalizeDownloadPlatform(' Windows ')).toBe('windows')
    expect(normalizeDownloadPlatform('linux')).toBe('linux')
    expect(normalizeDownloadPlatform('android')).toBeNull()
  })

  it('builds support topic routes and distinguishes external URLs', () => {
    expect(buildSupportPath('launch-readiness')).toBe('/support?topic=launch-readiness')
    expect(buildSupportPath()).toBe('/support')
    expect(isExternalHref('https://checkout.paddle.com')).toBe(true)
    expect(isExternalHref('/support?topic=operator-access')).toBe(false)
  })

  it('builds auth-preserving login and register links for protected destinations', () => {
    expect(buildLoginPath('/app/downloads?platform=windows')).toBe('/login?next=%2Fapp%2Fdownloads%3Fplatform%3Dwindows')
    expect(buildRegisterPath('/app/notices')).toBe('/register?next=%2Fapp%2Fnotices')
    expect(buildLoginPath('//evil.example')).toBe('/login?next=%2Fapp')
  })
})
