import pytest
from fastapi.testclient import TestClient

from polyform_backend.api import create_app


@pytest.fixture
def api(tmp_path):
    app=create_app(str(tmp_path / "test.db"))
    with TestClient(app) as client:
        yield client


@pytest.fixture
def release(api):
    body={"application":"zipdiv","version":"1.0.0","spec_functions":[
        {"name":"parse_header","implementations":[{"id":"header_a","name":"strict"},{"id":"header_bad","name":"faulty","provenance":{"experimental":True}}]},
        {"name":"normalize_path","implementations":[{"id":"path_a","name":"segments"},{"id":"path_b","name":"canonical"}]},
    ]}
    response=api.post("/api/v1/releases",json=body)
    assert response.status_code == 201
    return response.json()


def register(api,release,n,strategy="balanced"):
    r=api.post("/api/v1/clients",json={"release_id":release["id"],"installation_id":f"install-{strategy}-{n}","strategy":strategy})
    assert r.status_code == 201
    return r.json()
