Files
WebApi/tests/test_automatico.py
T

65 lines
3.4 KiB
Python

"""Automático through the API, as the Conectar button uses it: an operator connects
ISSSTELEON's test source and nothing else; the site's language model (LLM_URL: at
ISSSTELEON, its SGLang) drafts the whole pipeline, which is saved and run. What it places
must agree with the pipeline an operator drew, record by record. Real processes and the
real endpoint; nothing faked."""
import json
from test_conectar import ok, wait_for
SOURCE = "resultados"
HOURS = 3600 # with a model answering, it reads every document: an hour or more per run
def staged(platform, pipeline: str) -> dict[tuple, object]:
"""What a pipeline's targets staged, clean rows only: (study, record, field) -> value."""
rows = platform.query(
"select study, _record, field, value_text, value_num, value_bool, value_date "
f"from bronze.staged__observation where _pipeline = '{pipeline}'"
)
return {(s, r, f): next((v for v in vs if v is not None), None) for s, r, f, *vs in rows}
def test_automatico_drafts_a_sites_pipeline_and_runs_it(api, platform, site):
model = ok(api.get("/ingest/llm"))
assert model["available"], model["reason"]
drawn = json.loads(site.drawn)
source = next(n for n in drawn["nodes"] if n["id"] == SOURCE)
# Automático starts from the sources alone: a drawing with more is refused.
ok(api.put("/ingest/pipelines/half", json={"nodes": drawn["nodes"][:2]}))
assert api.post("/ingest/pipelines/half/draft").status_code == 422
assert api.post("/ingest/pipelines/nowhere/draft").status_code == 404
ok(api.put("/ingest/pipelines/auto", json={"nodes": [source]}))
wait_for(api, ok(api.post("/ingest/pipelines/auto/draft"))["run_id"], seconds=3 * HOURS)
saved = ok(api.get("/ingest/pipelines/auto"))
assert saved["version"] == 2
kinds = [n["kind"] for n in saved["pipeline"]["nodes"]]
assert {"read", "route", "extract", "link", "target"} <= set(kinds)
studies = {n["entity"] for n in saved["pipeline"]["nodes"] if n["kind"] == "target"}
assert {"cervical_cytology", "mammography"} <= studies, studies
# Documents no route claims wait for a reviewer, as with a drawn pipeline.
assert any(n.get("otherwise") for n in saved["pipeline"]["nodes"])
# The operator's own drawing over the same source: where both placed a value, the same.
ok(api.put("/ingest/concept-maps/erick_site", json=json.loads(site.concept_map.read_text())))
ok(api.put("/ingest/pipelines/site", json=drawn))
wait_for(api, ok(api.post("/ingest/pipelines/site/runs", json={}))["run_id"], 2 * HOURS)
mine, theirs = staged(platform, "auto"), staged(platform, "site")
both = set(mine) & set(theirs)
differ = {k: (mine[k], theirs[k]) for k in both if str(mine[k]) != str(theirs[k])}
assert both and not differ, differ
for study in ("cervical_cytology", "mammography"):
drawn_records = {r for s, r, _ in theirs if s == study}
assert drawn_records <= {r for s, r, _ in mine if s == study}, study
# Each case review waits on carries the model's reading, and still waits for a person.
link = next(n["id"] for n in saved["pipeline"]["nodes"] if n["kind"] == "link")
persons = ok(api.get("/review/persons", params={"pipeline": "auto", "node": link}))
for entry in persons["to_settle"]:
assert all(c["suggestion"]["reason"] for c in entry["candidates"])
for entry in persons["torn"]:
assert entry["suggestion"]["reason"]