import { describe, expect, it } from 'vitest'

import { mergePartialConfig, prepareBodyPayload } from '../src/core/body-utils.js'
import type { TypedFetchConfig } from '../src/types/config.js'

describe('body utils', () => {
  it('serializes plain objects and preserves samples', () => {
    const payload = { id: 1, name: 'Ada' }
    const prepared = prepareBodyPayload(payload)
    expect(prepared.bodyInit).toBe(JSON.stringify(payload))
    expect(prepared.sample).toEqual(payload)
  })

  it('merges nested config objects deeply', () => {
    const base: TypedFetchConfig = {
      request: { headers: { Authorization: 'token' } }
    }
    const override: TypedFetchConfig = {
      request: { headers: { 'X-Test': '1' } },
      cache: { ttl: 42 }
    }

    const merged = mergePartialConfig(base, override)
    expect(merged.request?.headers).toEqual({ 'X-Test': '1' })
    expect(merged.cache?.ttl).toBe(42)
  })
})
