"""Maximum AI: full past-report-quality sections.""" from __future__ import annotations from docx import Document from backend.config import settings from backend.core import ingest, section_mapper from backend.core.maximum_compose import compose_maximum_ai from backend.core.medium_expand import expand_medium_ai from backend.core.rag_store import TIER_REFERENCE, get_rag_store def _write_rich_past_roof_report(path): doc = Document() doc.add_paragraph("Section E2 - Roof Coverings") doc.add_paragraph( "The roof coverings were inspected from ground level using binoculars and, " "where accessible, from within the roof void. The main roof covering " "comprises natural slate tiles laid to pitched timber rafters with underfelt " "and counter battens. Some slate tiles to the south elevation appeared " "displaced from their fixings. No signs of widespread moisture ingress were " "noted at the time of inspection." ) doc.save(str(path)) def test_maximum_keeps_full_rich_paragraph(): refs = [ ( "The roof coverings were inspected from ground level using binoculars and, " "where accessible, from within the roof void. The main roof covering " "comprises natural slate tiles laid to pitched timber rafters with underfelt " "and counter battens." ), ] out = compose_maximum_ai(["slate tile slipped south slope"], refs, section_title="Roof Coverings") assert "•" not in out assert out.count(".") >= 2 assert "slate" in out.lower() assert "south slope" in out.lower() or "south" in out.lower() assert "binoculars" in out.lower() assert len(out.split()) >= len(refs[0].split()) * 0.85 def test_maximum_at_least_as_long_as_medium(): refs = [ ( "The roof coverings were inspected from ground level using binoculars. " "The main roof covering comprises natural slate tiles laid to pitched " "timber rafters with underfelt and counter battens." ), ] obs = ["slate tile slipped south slope"] medium = expand_medium_ai(obs, refs[0]) maximum = compose_maximum_ai(obs, refs, section_title="Roof Coverings") assert len(maximum.split()) >= len(medium.split()) * 0.85 assert maximum.count(".") >= medium.count(".") def test_maximum_mode_full_paragraph_in_report(tmp_path, monkeypatch): monkeypatch.setattr(settings, "data_dir", str(tmp_path / "data")) tpl = tmp_path / "template.docx" doc = Document() doc.add_paragraph("D2 Roof coverings") doc.save(str(tpl)) master = tmp_path / "master.docx" doc2 = Document() doc2.add_paragraph("Section E2 - Roof Coverings") doc2.add_paragraph("The roof covering comprises slate laid to pitched timber rafters.") doc2.save(str(master)) past = tmp_path / "past.docx" _write_rich_past_roof_report(past) tenant = "t-max-ref" ingest.ingest_report_template(tenant, tpl) ingest.ingest_standard_paragraphs(tenant, master) ingest.ingest_reference(tenant, past) assert get_rag_store().count(tenant, TIER_REFERENCE) >= 1 result = section_mapper.generate_report( tenant, "slate tile slipped south slope", interference_level="maximum", ) d3 = next(s for s in result.sections if s.section_id == "D2") assert d3.status == "OK" assert "•" not in d3.text assert d3.text.count(".") >= 3 assert "south slope" in d3.text.lower() or "slate" in d3.text.lower() assert "binoculars" in d3.text.lower() or "rafters" in d3.text.lower()