import { describe, it, expect } from 'vitest'
import { getRelevantHeaders } from './get-relevant-headers'

describe('getRelevantHeaders', () => {
  it('should return empty string for undefined headers', () => {
    expect(getRelevantHeaders(undefined)).toBe('')
  })

  it('should return empty string for empty headers object', () => {
    expect(getRelevantHeaders({})).toBe('')
  })

  it('should extract authorization header', () => {
    const headers = {
      authorization: 'Bearer token123',
      'content-type': 'application/json',
    }
    const result = getRelevantHeaders(headers)
    expect(result).toBe(JSON.stringify({ authorization: 'Bearer token123' }))
  })

  it('should extract cache-control header', () => {
    const headers = {
      'cache-control': 'no-cache',
      'x-custom-header': 'value',
    }
    const result = getRelevantHeaders(headers)
    expect(result).toBe(JSON.stringify({ 'cache-control': 'no-cache' }))
  })

  it('should extract multiple relevant headers', () => {
    const headers = {
      authorization: 'Bearer token123',
      'cache-control': 'no-cache',
      'content-type': 'application/json',
    }
    const result = getRelevantHeaders(headers)
    const parsed = JSON.parse(result) as Record<string, string>
    expect(parsed).toEqual({
      authorization: 'Bearer token123',
      'cache-control': 'no-cache',
    })
  })

  it('should ignore non-relevant headers', () => {
    const headers = {
      'content-type': 'application/json',
      'x-custom-header': 'value',
      accept: 'application/json',
    }
    expect(getRelevantHeaders(headers)).toBe('')
  })

  it('should handle case-insensitive header names', () => {
    const headers = {
      Authorization: 'Bearer token123',
      'Cache-Control': 'no-cache',
      'CONTENT-TYPE': 'application/json',
    }
    const result = getRelevantHeaders(headers)
    const parsed = JSON.parse(result) as Record<string, string>
    expect(parsed).toEqual({
      authorization: 'Bearer token123',
      'cache-control': 'no-cache',
    })
  })

  it('should normalize header names to lowercase', () => {
    const headers = {
      AUTHORIZATION: 'Bearer ABC',
    }
    const result = getRelevantHeaders(headers)
    expect(result).toContain('"authorization"')
  })
})
