"""Tests for the exam source allocation planner. The planner's whole value rests on a distinction the coverage metric cannot make: did the generator write from more of the book, or did it merely name more chunk ids? These tests pin the behaviours that keep the difference meaningful — briefs anchored to real chunks, thin sources excluded rather than padded, and an empty plan degrading to exactly today's prompt. """ from __future__ import annotations from app.services.gateway import ( _build_source_plan_prompt, _parse_source_plan, _render_allocation, ) CHUNKS = ["chunk-a", "chunk-b", "chunk-c", "chunk-d"] def _row(idx: int, **over) -> dict: row = { "source_index": idx, "concept": f"مفهوم {idx}", "learning_objective": f"هدف {idx}", "question_count": 2, "difficulty": ["medium", "hard"], "reason": "سبب", } row.update(over) return row class TestParseSourcePlan: def test_maps_source_index_to_chunk_id(self): plan = _parse_source_plan({"coverage": [_row(1), _row(3)]}, CHUNKS) assert set(plan) == {"chunk-a", "chunk-c"} assert plan["chunk-a"]["concept"] == "مفهوم 1" def test_accepts_bare_array(self): # Same shape drift the red team hit: a model returning the list # directly instead of the documented envelope must not cost the plan. plan = _parse_source_plan([_row(2)], CHUNKS) assert set(plan) == {"chunk-b"} def test_drops_out_of_range_index(self): # A source_index past the block list is the planner inventing a source; # anchoring it anyway would attach a brief to the wrong chunk. plan = _parse_source_plan({"coverage": [_row(9), _row(0), _row(-1)]}, CHUNKS) assert plan == {} def test_drops_rows_without_a_concept(self): plan = _parse_source_plan({"coverage": [_row(1, concept=" ")]}, CHUNKS) assert plan == {} def test_survives_non_numeric_index_and_count(self): plan = _parse_source_plan( {"coverage": [_row(1, question_count="two"), _row("x")]}, CHUNKS) assert plan["chunk-a"]["question_count"] == 0 def test_clamps_absurd_question_count(self): plan = _parse_source_plan({"coverage": [_row(1, question_count=500)]}, CHUNKS) assert plan["chunk-a"]["question_count"] == 10 def test_filters_invalid_difficulty_labels(self): plan = _parse_source_plan( {"coverage": [_row(1, difficulty=["hard", "impossible", "MEDIUM"])]}, CHUNKS) assert plan["chunk-a"]["difficulty"] == ["hard", "medium"] def test_garbage_payload_yields_empty_plan(self): for junk in (None, "text", 42, {"unexpected": 1}): assert _parse_source_plan(junk, CHUNKS) == {} class TestRenderAllocation: def test_renumbers_to_this_batch(self): # The plan is global but a batch only sees its own page of sources, so # a brief must be addressed by the batch's index, not the plan's. plan = _parse_source_plan({"coverage": [_row(3)]}, CHUNKS) rendered = _render_allocation(["chunk-c"], plan) assert "[[المصدر 1]]" in rendered assert "[[المصدر 3]]" not in rendered def test_omits_chunks_the_plan_skipped(self): plan = _parse_source_plan( {"coverage": [_row(1), _row(2, question_count=0)]}, CHUNKS) rendered = _render_allocation(["chunk-a", "chunk-b"], plan) assert "مفهوم 1" in rendered assert "مفهوم 2" not in rendered # and says so explicitly, so the generator doesn't quietly use it anyway assert "تجاهلها" in rendered def test_empty_plan_renders_nothing(self): # The A/B control depends on this: no plan must mean a byte-identical # prompt to the unplanned path, not an empty header. assert _render_allocation(CHUNKS, {}) == "" def test_all_chunks_skipped_renders_nothing(self): plan = _parse_source_plan({"coverage": [_row(1, question_count=0)]}, CHUNKS) assert _render_allocation(["chunk-a"], plan) == "" def test_includes_objective_and_difficulty(self): plan = _parse_source_plan({"coverage": [_row(1)]}, CHUNKS) rendered = _render_allocation(["chunk-a"], plan) assert "هدف 1" in rendered assert "medium" in rendered class TestPlannerPrompt: def test_forbids_writing_questions(self): # The experiment only reads if the generator still writes every # question; a planner that drifts into authoring them invalidates it. prompt = _build_source_plan_prompt(61) assert "التخطيط فقط" in prompt assert "وليس كتابة الأسئلة" in prompt def test_states_the_question_budget(self): assert "61" in _build_source_plan_prompt(61) def test_instructs_refusal_of_thin_sources(self): # Without this the planner assigns a question to every chunk and the # stage becomes an expensive no-op. assert "لا تخصص أسئلة لمصدر" in _build_source_plan_prompt(20)