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

import { createResource, type ResourceDefinition } from '../src/core/resource-builder.js'

describe('resource builder', () => {
  const response = new Response('{}')

  it('expands params and merges query defaults', async () => {
    const calls: Array<{ method: string; url: string; bodySample?: unknown }> = []
    const requester = {
      async request<T>(method: string, url: string, init?: RequestInit, bodySample?: unknown) {
        calls.push({ method, url, bodySample })
        return { data: { ok: true } as T, response }
      }
    }

    const definition: ResourceDefinition = {
      show: { method: 'GET', path: '', query: { expand: 'profile' } }
    }

    const resource = createResource(requester, '/users/:id', definition, { query: { locale: 'en' } })
    await resource.show({ params: { id: 42 }, query: { expand: 'posts' } })

    expect(calls).toHaveLength(1)
    expect(calls[0]!.url).toBe('/users/42?locale=en&expand=posts')
    expect(resource.$path({ id: 42 }, { search: 'text' })).toBe('/users/42?locale=en&search=text')
  })
})
