// §22 S2 — the API client builds collection-scoped URLs when a collection id is
// supplied, and falls back to the project/default-collection paths otherwise.
import { describe, it, expect, vi, afterEach } from 'vitest'
import { listRFCs, getRFC, proposeRFC, listCollections } from './api.js'

function mockFetch() {
  const fn = vi.fn(async () => ({
    ok: true,
    status: 200,
    json: async () => ({ items: [] }),
  }))
  global.fetch = fn
  return fn
}

afterEach(() => { vi.restoreAllMocks() })

describe('collection-scoped api URLs', () => {
  it('listRFCs scopes to a collection when given one', async () => {
    const f = mockFetch()
    await listRFCs('ohm', 'features')
    expect(f).toHaveBeenCalledWith('/api/projects/ohm/collections/features/rfcs')
  })

  it('listRFCs falls back to the project default path without a collection', async () => {
    const f = mockFetch()
    await listRFCs('ohm')
    expect(f).toHaveBeenCalledWith('/api/projects/ohm/rfcs')
  })

  it('getRFC scopes to a collection when given one', async () => {
    const f = mockFetch()
    await getRFC('ohm', 'login', 'features')
    expect(f).toHaveBeenCalledWith('/api/projects/ohm/collections/features/rfcs/login')
  })

  it('proposeRFC targets the collection-scoped propose route', async () => {
    const f = mockFetch()
    await proposeRFC('ohm', { title: 'T', slug: 's', pitch: 'p', tags: [], collectionId: 'features' })
    expect(f.mock.calls[0][0]).toBe('/api/projects/ohm/collections/features/rfcs/propose')
  })

  it('listCollections hits the project collections route', async () => {
    const f = mockFetch()
    await listCollections('ohm')
    expect(f).toHaveBeenCalledWith('/api/projects/ohm/collections')
  })
})
