from __future__ import annotations import pytest from backend.app.api.routes import strategy @pytest.fixture(autouse=True) def _reset_cluster_caches() -> None: strategy._gold_cluster_cache = None strategy._master_cluster_cache = None yield strategy._gold_cluster_cache = None strategy._master_cluster_cache = None def test_get_cluster_doctors_uses_gold_dataset_when_available(monkeypatch: pytest.MonkeyPatch) -> None: records = [ { "code_name": "HCP-00-001", "institution": "Alpha Cancer Center", "institution_type": "cancer_center", "institution_country": "US", "topics": ["Biomarker Discovery", "Precision Medicine"], "h_index": 45, "works_count": 160, "cited_by_count": 6200, "i10_index": 88, "years_active": 18, }, { "code_name": "HCP-01-001", "institution": "Beta Medical Center", "institution_type": "academic_medical_center", "institution_country": "GB", "topics": ["Liquid Biopsy"], "h_index": 28, "works_count": 90, "cited_by_count": 2100, "i10_index": 40, "years_active": 9, }, ] monkeypatch.setattr(strategy, "_load_bronze_hcp_records", lambda: ([], {})) monkeypatch.setattr(strategy, "_load_gold_records", lambda: records) monkeypatch.setattr(strategy, "run_gmm_clustering", lambda payload, min_k=4, **kwargs: {"assignments": [0, 1], "k": 2}) response = strategy.get_cluster_doctors(0, limit=10, region=None) assert response["source"] == "gold" assert response["total_in_db"] == 2 assert response["total_in_cluster"] == 1 assert response["doctors"][0]["name"] == "HCP-00-001" assert response["doctors"][0]["company"] == "Alpha Cancer Center" assert response["doctors"][0]["primary_specialty"] == "Biomarker Discovery" def test_get_strategy_clusters_returns_dynamic_unique_labels(monkeypatch: pytest.MonkeyPatch) -> None: monkeypatch.setattr( strategy, "_get_clustered_doctors", lambda: { "k": 5, "source": "bronze_dynamic_master", "doctors": [ {"cluster_id": 0}, {"cluster_id": 1}, {"cluster_id": 1}, {"cluster_id": 4}, ], }, ) monkeypatch.setattr(strategy, "_get_gold_clustered_doctors", lambda: {"k": 0, "source": "gold", "doctors": []}) response = strategy.get_strategy_clusters(region=None) clusters = response["clusters"] assert response["k"] == 5 assert [cluster["cluster_id"] for cluster in clusters] == [0, 1, 2, 3, 4] names = [cluster["name"] for cluster in clusters] assert len(names) == len(set(names)) assert clusters[1]["total_in_cluster"] == 2 assert clusters[2]["total_in_cluster"] == 0