"""Z.AI-извлечение: сборка запроса и разбор ответа — без API.""" from pathlib import Path import pytest from rag import corpus from rag.extract import zai @pytest.fixture() def lecture(tmp_path: Path) -> corpus.Lecture: seg = tmp_path / "segments.txt" seg.write_text("[00:01] сегмент про контур 80 кГц\n", encoding="utf-8") return corpus.Lecture(id="тест", fragments=( corpus.Transcript(id="тест__фр1", source="mass", path=seg), )) VALID_JSON = ('{"lecture_id": "тест", "cards": [{"type": "принцип", "title": "т",' '"statement": "с", "layer": "радиотехника",' '"sources": [{"video": "тест__фр1", "timecode": "00:01", "quote": "контур"}]}],' '"asr_findings": []}') def test_request_contains_schema_and_rules(lecture): kw = zai.build_request_kwargs(lecture) assert "json-схеме" in kw["system"].lower() assert "ExtractionResult" in kw["system"] or "cards" in kw["system"] assert "НИЧЕГО НЕ ВЫДУМЫВАТЬ" in kw["system"] assert "=== ФРАГМЕНТ: тест__фр1 ===" in kw["messages"][0]["content"] def test_parse_plain_json(): result = zai.parse_result(VALID_JSON) assert result.lecture_id == "тест" assert len(result.cards) == 1 def test_parse_fenced_json(): fenced = f"```json\n{VALID_JSON}\n```" assert zai.parse_result(fenced).lecture_id == "тест" def test_parse_garbage_raises(): with pytest.raises(Exception): zai.parse_result("вот ваш JSON: непонятно что")