// §22/G-15 — getRFCMain/getBranch build collection-scoped URLs when a project +
// collection are supplied (so an entry outside the default collection reads its
// own content repo / subfolder), and fall back to the slug-only routes otherwise.
import { describe, it, expect, vi, afterEach } from 'vitest'
import { getRFCMain, getBranch } from './api.js'

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

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

describe('G-15 collection-scoped branch/body URLs', () => {
  it('getRFCMain scopes to project+collection when both are given', async () => {
    const f = mockFetch()
    await getRFCMain('login', 'rfc-app', 'specs')
    expect(f).toHaveBeenCalledWith('/api/projects/rfc-app/collections/specs/rfcs/login/main')
  })

  it('getRFCMain falls back to the slug-only route without a collection', async () => {
    const f = mockFetch()
    await getRFCMain('login')
    expect(f).toHaveBeenCalledWith('/api/rfcs/login/main')
  })

  it('getBranch scopes to project+collection and encodes the branch', async () => {
    const f = mockFetch()
    await getBranch('login', 'edit/foo', 'rfc-app', 'specs')
    expect(f).toHaveBeenCalledWith(
      '/api/projects/rfc-app/collections/specs/rfcs/login/branches/edit%2Ffoo')
  })

  it('getBranch falls back to the slug-only route without a collection', async () => {
    const f = mockFetch()
    await getBranch('login', 'main')
    expect(f).toHaveBeenCalledWith('/api/rfcs/login/branches/main')
  })
})
