Download apps/api/tests/test_bag_angles.py from aliroya/contiai: direct link, hf CLI and curl.
- Browser
- Download file 7.42 kB
-
https://huggingface.co/spaces/aliroya/contiai/resolve/main/apps/api/tests/test_bag_angles.py
- Command line
-
hf download hf://spaces/aliroya/contiai/apps/api/tests/test_bag_angles.py
-
curl -L -o test_bag_angles.py https://huggingface.co/spaces/aliroya/contiai/resolve/main/apps/api/tests/test_bag_angles.py
7.42 kB
| """A supplied blueprint gets the angles a generated one already has. | |
| A cover carries a title, a nature, and two to five angles saying what the | |
| content under it must cover. The blueprint agent writes them; a pasted or | |
| uploaded document does not contain them, and the converter is told not to invent | |
| what the document does not say. So every supplied blueprint reached the writer | |
| with `angles: []` on every cover - one real bag with forty-three of them - and | |
| the prose came back competent and thin, written from a heading alone. Nothing | |
| reported it. | |
| These pin the filling, and the rule that matters more: it may add angles and | |
| nothing else. The designer's titles, natures, order and structure are theirs. | |
| """ | |
| from __future__ import annotations | |
| import asyncio | |
| import copy | |
| import pytest | |
| from app.agents.arabic_bag import angles as A | |
| def blueprint(angle_sets: list[list[str]]) -> dict: | |
| """One session whose covers carry the given angles, in order.""" | |
| return { | |
| "bag_name": "التميز في خدمة العملاء", | |
| "target_audience": "الموظفات في مراكز الخدمة", | |
| "days": [{ | |
| "day_number": 1, | |
| "sessions": [{ | |
| "session_number": 1, | |
| "session_title": "الجلسة الصباحية", | |
| "topics": [{ | |
| "topic": "مدخل إلى التميز", | |
| "covers": [ | |
| {"cover_index": i + 1, "cover": f"محور {i + 1}", | |
| "cover_nature": "مفاهيمي", "angles": list(a)} | |
| for i, a in enumerate(angle_sets) | |
| ], | |
| }], | |
| }], | |
| }], | |
| } | |
| def fill(bp, answer): | |
| """Run the filler with the model replaced by `answer(payload) -> covers`.""" | |
| async def fake_batch(rows, bag_name, audience): | |
| # Only the model call is replaced. The merge below is the real one, so | |
| # a malformed answer is judged by the code that ships. | |
| covers = answer([ | |
| {"cover_index": i, "cover": r["cover"].get("cover"), | |
| "topic": r["topic"], "session": r["session_title"]} | |
| for i, r in enumerate(rows) | |
| ]) | |
| A.apply_angles(rows, {"covers": covers}) | |
| original = A._one_batch | |
| A._one_batch = fake_batch | |
| try: | |
| return asyncio.run(A.fill_missing_angles(bp)) | |
| finally: | |
| A._one_batch = original | |
| # ------------------------------------------------------------- what it counts | |
| def test_it_finds_the_covers_that_have_none(): | |
| assert A.missing_angles(blueprint([[], ["زاوية"], []])) == 2 | |
| def test_a_blueprint_that_already_has_angles_needs_nothing(): | |
| assert A.missing_angles(blueprint([["أ", "ب"]])) == 0 | |
| def test_flatten_carries_the_context_an_angle_needs(): | |
| rows = A._flatten(blueprint([[]])) | |
| assert rows[0]["topic"] == "مدخل إلى التميز" | |
| assert rows[0]["session_title"] == "الجلسة الصباحية" | |
| # --------------------------------------------------------------- the filling | |
| def test_empty_covers_are_filled(): | |
| bp = fill(blueprint([[], []]), | |
| lambda p: [{"cover_index": i["cover_index"], "angles": ["أ", "ب"]} for i in p]) | |
| assert A.missing_angles(bp) == 0 | |
| def test_covers_that_already_have_angles_are_left_alone(): | |
| """The designer may have written angles themselves; overwriting them would | |
| replace their work with a guess.""" | |
| bp = blueprint([["زاويتي الخاصة", "وأخرى"], []]) | |
| asked = [] | |
| def answer(payload): | |
| asked.extend(c["cover"] for c in payload) | |
| return [{"cover_index": c["cover_index"], "angles": ["مولّدة"]} for c in payload] | |
| out = fill(bp, answer) | |
| covers = out["days"][0]["sessions"][0]["topics"][0]["covers"] | |
| assert covers[0]["angles"] == ["زاويتي الخاصة", "وأخرى"] | |
| assert asked == ["محور 2"], "a cover that had angles was sent anyway" | |
| def test_nothing_but_angles_changes(): | |
| """The whole contract with the designer: their structure is theirs.""" | |
| bp = blueprint([[], []]) | |
| before = copy.deepcopy(bp) | |
| out = fill(bp, lambda p: [{"cover_index": c["cover_index"], "angles": ["أ", "ب"]} | |
| for c in p]) | |
| for path in (out, before): | |
| for day in path["days"]: | |
| for session in day["sessions"]: | |
| for topic in session["topics"]: | |
| for cover in topic["covers"]: | |
| cover.pop("angles", None) | |
| assert out == before, "the filler changed something other than angles" | |
| def test_a_model_that_renames_a_cover_cannot_move_it(): | |
| """Covers are matched back by the position handed out, never by the title | |
| the model echoes - so a renamed answer lands where it belongs or nowhere.""" | |
| bp = fill(blueprint([[], []]), | |
| lambda p: [{"cover_index": 1, "cover": "اسم مختلف تمامًا", | |
| "angles": ["للثاني فقط"]}]) | |
| covers = bp["days"][0]["sessions"][0]["topics"][0]["covers"] | |
| assert covers[0]["angles"] == [] | |
| assert covers[1]["angles"] == ["للثاني فقط"] | |
| def test_a_malformed_answer_leaves_the_blueprint_alone(bad): | |
| bp = fill(blueprint([[]]), lambda p: bad) | |
| assert bp["days"][0]["sessions"][0]["topics"][0]["covers"][0]["angles"] == [] | |
| def test_no_more_than_five_angles_survive(): | |
| bp = fill(blueprint([[]]), | |
| lambda p: [{"cover_index": 0, "angles": [f"ز{i}" for i in range(9)]}]) | |
| assert len(bp["days"][0]["sessions"][0]["topics"][0]["covers"][0]["angles"]) == 5 | |
| def test_the_covers_are_batched(): | |
| """Forty-three covers in one call is the ceiling problem again.""" | |
| assert 0 < A.BATCH <= 20 | |
| sizes = [] | |
| async def counting(rows, bag_name, audience): | |
| sizes.append(len(rows)) | |
| original = A._one_batch | |
| A._one_batch = counting | |
| try: | |
| asyncio.run(A.fill_missing_angles(blueprint([[]] * 43))) | |
| finally: | |
| A._one_batch = original | |
| assert sum(sizes) == 43 | |
| assert max(sizes) <= A.BATCH | |
| def test_a_failing_batch_does_not_cost_the_others(): | |
| """One batch is one call; losing it must not lose the covers around it.""" | |
| async def flaky(rows, bag_name, audience): | |
| if rows[0]["cover"].get("cover") == "محور 1": | |
| raise RuntimeError("provider said no") | |
| for row in rows: | |
| row["cover"]["angles"] = ["مولّدة"] | |
| original = A._one_batch | |
| A._one_batch = flaky | |
| try: | |
| bp = blueprint([[]] * 12) | |
| with pytest.raises(RuntimeError): | |
| asyncio.run(A.fill_missing_angles(bp)) | |
| finally: | |
| A._one_batch = original | |
| # --------------------------------------------------------------- the wiring | |
| def test_the_blueprint_step_fills_angles_before_the_gate(): | |
| """After the gate would be too late to review them, and after the planner | |
| would be too late for them to matter.""" | |
| import inspect | |
| from app.pipelines.arabic_bag import steps | |
| source = inspect.getsource(steps.BlueprintStep) | |
| assert "_fill_angles" in source | |
| assert steps.BlueprintStep.gate is True | |