from __future__ import annotations

from pathlib import Path

from fastapi.testclient import TestClient

from beaver.interfaces.web.app import create_app
from beaver.memory.skills import SkillLearningCandidate
from beaver.services.agent_service import AgentService


def test_skill_learning_candidates_and_run_once_api(tmp_path: Path) -> None:
    service = AgentService(workspace=tmp_path)
    loaded = service.create_loop().boot()
    loaded.skill_learning_store.record_learning_candidate(  # type: ignore[union-attr]
        SkillLearningCandidate(
            candidate_id="candidate-1",
            kind="new_skill",
            source_run_ids=[],
            source_session_ids=[],
            related_skill_names=[],
            reason="test",
        )
    )
    app = create_app(service=service, manage_service_lifecycle=False)

    with TestClient(app) as client:
        candidates = client.get("/api/skills/candidates").json()
        run_once = client.post("/api/skills/learning/run-once").json()

    assert candidates[0]["candidate_id"] == "candidate-1"
    assert "risk_level" in candidates[0]
    assert run_once["processed"] >= 0
