import time import pytest from edf_files import SEX_MAP, hospital, pipeline, small_edf def _ok(response): assert response.status_code == 200, response.text return response.json() def wait_for(api, run_id: str, seconds: int = 900) -> None: deadline = time.monotonic() + seconds while (state := _ok(api.get(f"/runs/{run_id}"))["state"]) in ("queued", "running"): assert time.monotonic() < deadline, f"run {run_id} still {state}" time.sleep(2) assert state == "succeeded" @pytest.mark.parametrize("platform", ["sqlite"], indirect=True) def test_an_uploaded_edf_is_analysed_by_itself(api, tmp_path): hospital_url = hospital(tmp_path / "hospital.db") data = small_edf(tmp_path / "a.edf") def upload(): result = _ok(api.post("/ingest/uploads", files={"file": ("a.edf", data)})) assert set(result) == {"bucket_url", "file_name"} assert result["file_name"] == "a.edf" return result["bucket_url"] def run(): wait_for(api, _ok(api.post("/ingest/pipelines/edf/runs", json={}))["run_id"]) def runs(): return _ok(api.get("/runs", params={"job": "tea_eeg", "domain": "tea"})) bucket_url = upload() _ok(api.put("/ingest/concept-maps/edf_sex", json=SEX_MAP)) _ok(api.put("/ingest/pipelines/edf", json=pipeline(hospital_url, bucket_url))) run() deadline = time.monotonic() + 600 found = runs() while not any(item["state"] == "succeeded" for item in found): assert time.monotonic() < deadline, found time.sleep(2) found = runs() (first,) = found (study,) = _ok(api.get("/tea/eegs")) assert study["study_date"] == "2026-03-02" assert study["lab_record_id"] == "LAB-1" assert study["analysed"] is True assert upload() == bucket_url run() deadline = time.monotonic() + 60 while time.monotonic() < deadline: assert [item["id"] for item in runs()] == [first["id"]] time.sleep(2) assert [item["id"] for item in runs()] == [first["id"]]