Spaces:
Runtime error
Runtime error
| """Minimum AI: full past-report paragraph with notes mapped in.""" | |
| from __future__ import annotations | |
| from docx import Document | |
| from backend.config import settings | |
| from backend.core import ingest, section_mapper | |
| from backend.core.minimum_weave import map_minimum_reference | |
| from backend.core.rag_store import TIER_REFERENCE, get_rag_store | |
| from backend.models.schema import TemplateSchema | |
| def _write_past_roof_report(path): | |
| doc = Document() | |
| doc.add_paragraph("D2\nRoof coverings") | |
| doc.add_paragraph( | |
| "The roof covering comprised slate tiles laid to pitched timber rafters. " | |
| "Inspection was limited to ground level using binoculars." | |
| ) | |
| doc.save(str(path)) | |
| def test_minimum_keeps_full_reference_paragraph(): | |
| reference = ( | |
| "The roof covering comprised slate tiles laid to pitched timber rafters. " | |
| "Inspection was limited to ground level using binoculars." | |
| ) | |
| out = map_minimum_reference(reference, ["slate tile slipped south slope"], TemplateSchema()) | |
| assert "•" not in out | |
| assert out.count(".") >= 2 | |
| assert "slate" in out.lower() | |
| assert "south slope" in out.lower() | |
| assert "binoculars" in out.lower() | |
| assert "timber rafters" in out.lower() | |
| assert len(out.split()) >= len(reference.split()) * 0.9 | |
| def test_minimum_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("RICS Home Survey Level 3") | |
| doc.save(str(tpl)) | |
| master = tmp_path / "master.docx" | |
| doc2 = Document() | |
| doc2.add_paragraph("Section D2 - 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_past_roof_report(past) | |
| tenant = "t-min" | |
| 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, | |
| "D2: slate tile slipped south slope", | |
| interference_level="minimum", | |
| ) | |
| 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(".") >= 2 | |
| assert "binoculars" in d3.text.lower() | |
| assert "south slope" in d3.text.lower() | |
| def test_sections_with_notes_generated_from_past_report(tmp_path, monkeypatch): | |
| monkeypatch.setattr(settings, "data_dir", str(tmp_path / "data")) | |
| tpl = tmp_path / "template.docx" | |
| doc = Document() | |
| doc.add_paragraph("RICS Home Survey Level 3") | |
| doc.save(str(tpl)) | |
| past = tmp_path / "past.docx" | |
| doc2 = Document() | |
| doc2.add_paragraph("D1\nChimney stacks") | |
| doc2.add_paragraph( | |
| "The chimney stacks were inspected from ground level using binoculars." | |
| ) | |
| doc2.add_paragraph("D2\nRoof coverings") | |
| doc2.add_paragraph( | |
| "The roof covering comprised slate tiles laid to pitched timber rafters." | |
| ) | |
| doc2.save(str(past)) | |
| tenant = "t-all" | |
| ingest.ingest_report_template(tenant, tpl) | |
| ingest.ingest_reference(tenant, past) | |
| result = section_mapper.generate_report( | |
| tenant, | |
| "D1: mortar open at crown\n\nD2: slate tile slipped south slope", | |
| interference_level="minimum", | |
| ) | |
| section_ids = {s.section_id for s in result.sections if s.section_id != "UNASSIGNED"} | |
| assert "D1" in section_ids | |
| assert "D2" in section_ids | |
| d1 = next(s for s in result.sections if s.section_id == "D1") | |
| d2 = next(s for s in result.sections if s.section_id == "D2") | |
| assert d1.status == "OK" | |
| assert d2.status == "OK" | |
| assert "chimney" in d1.text.lower() or "stack" in d1.text.lower() | |
| assert "south slope" in d2.text.lower() | |