from datetime import date from flask import Flask from models.cacu.instance_publication import EpisodeStudies, FollowUpInstance, publish from webapi.follow_up.instances import instance_follow_up_blueprint def test_instance_list_links_to_studies_in_the_same_publication(tmp_path): path = tmp_path / "simulated.sqlite" cutoff = date(2026, 10, 20) row = FollowUpInstance( "ins-1", "P-DEMO", "cin2_pim_post_treatment", 526, date(2026, 10, 15), "pendiente", "rules-v1", 301, "eoc-07", motivos=("vph_pendiente",), ) studies = [ {"tipo": "citologia", "toma_ref": "pr-526", "resultados": [{"valor": "NORMAL"}]}, {"tipo": "vph", "toma_ref": "pr-526", "estado": "pendiente"}, {"tipo": "referencia_colposcopia", "orden_ref": "ord-1"}, {"tipo": "colposcopia", "procedimiento_ref": "pr-204", "orden_ref": "ord-1"}, {"tipo": "biopsia", "procedimiento_ref": "pr-240"}, {"tipo": "tratamiento", "procedimiento_ref": "pr-301"}, ] publish(path, "synthetic-1", cutoff, [row], [EpisodeStudies("eoc-07", "P-DEMO", studies)]) app = Flask(__name__) app.register_blueprint(instance_follow_up_blueprint(path), url_prefix="/cacu") client = app.test_client() url = "/cacu/seguimiento/instancias" response = client.get(url + "?escenario=cin2_pim_post_treatment&elegibilidad=pendiente") assert response.status_code == 200 body = response.json assert body["simulado"] and body["paginacion"]["total_items"] == 1 item, = body["items"] assert "pac_id" not in item and item["candidato_procedimiento_ref"] == "pr-526" assert client.get(item["estudios_url"]).json["estudios"] == studies assert client.get(url + "?page_size=201").status_code == 400 assert client.get(url + "?publicacion_id=unknown").status_code == 404 assert client.get("/cacu/episodios/eoc-07/estudios").status_code == 400 def test_application_registers_instance_routes_only_when_enabled(monkeypatch, tmp_path): from webapi import create_app monkeypatch.delenv("CACU_SIMULATED_INSTANCE_PUBLICATION", raising=False) assert not any(rule.rule == "/cacu/seguimiento/instancias" for rule in create_app().url_map.iter_rules()) path = tmp_path / "simulated.sqlite" publish(path, "synthetic", date(2026, 10, 20), []) monkeypatch.setenv("CACU_SIMULATED_INSTANCE_PUBLICATION", str(path)) response = create_app().test_client().get("/cacu/seguimiento/instancias") assert response.status_code == 200 and response.json["publicacion_id"] == "synthetic"