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93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
"""Regression tests for the citation-grounded extraction layer.

These tests are the executable success criteria for the anti-hallucination
guarantees. They are fully deterministic and require NO OpenAI key β€” they
exercise the pure-Python validation/contradiction core that gates the LLM.

Covered failure modes (from the spec's TESTING REQUIREMENTS):
- altered condition ratings
- fabricated materials
- invented locations / entity substitution
- unsupported risk / severity claims
- contradiction generation (rating / condition / operational / duplicate)
- malformed / fabricated sentences (paraphrase with no grounding)
- dropped warranties (positive findings preserved when grounded)
- fabricated monetary totals
- citation to a non-existent chunk
"""

from __future__ import annotations

from app.extraction.citation_validator import (
    span_in_chunk,
    validate_finding,
    validate_findings,
)
from app.extraction.contradiction import audit_contradictions
from app.extraction.schemas import (
    ConditionRating,
    ContradictionKind,
    EvidenceSpan,
    SupportLevel,
    SurveyFinding,
)


def _finding(element: str, rating, text: str, *, chunk_id="c1", span=None) -> SurveyFinding:
    return SurveyFinding(
        section="Roofing",
        element=element,
        condition_rating=rating,
        finding=text,
        evidence=[EvidenceSpan(chunk_id=chunk_id, text=span or text)],
    )


# ── ConditionRating enum: closed set, never fabricated ──────────────────────

def test_rating_enum_coerces_unknown_to_na_not_a_guess():
    assert ConditionRating.coerce("2") is ConditionRating.CR2
    assert ConditionRating.coerce("CR3") is ConditionRating.CR3
    # Garbage must degrade to NA, never to a fabricated severity.
    assert ConditionRating.coerce("urgent") is ConditionRating.NA
    assert ConditionRating.coerce("") is ConditionRating.NA
    assert ConditionRating.coerce(None) is ConditionRating.NA


def test_finding_rating_is_schema_constrained():
    f = SurveyFinding(section="Roofing", element="Ridge", condition_rating="severe", finding="x")
    assert f.condition_rating is ConditionRating.NA


# ── span_in_chunk: verbatim + OCR-drift tolerance, but rejects absent text ──

def test_span_match_verbatim_and_drift():
    chunk = "The main roof covering is natural slate, generally in sound condition."
    assert span_in_chunk("natural slate", chunk)
    assert span_in_chunk("The main roof covering is natural slate", chunk)
    # whitespace/case drift still matches
    assert span_in_chunk("NATURAL   slate", chunk)


def test_span_absent_is_rejected():
    chunk = "The main roof covering is natural slate."
    assert not span_in_chunk("concrete interlocking tiles", chunk)


# ── Altered condition ratings are caught by the contradiction audit ─────────

def test_altered_rating_conflict_detected_and_resolved():
    strong = _finding("Main roof", ConditionRating.CR2, "Slate covering shows slipped tiles.")
    strong.support = SupportLevel.SUPPORTED
    weak = _finding("Main roof", ConditionRating.CR1, "Slate covering shows slipped tiles.")
    weak.support = SupportLevel.PARTIAL
    resolved, reports = audit_contradictions([strong, weak])
    assert len(resolved) == 1
    assert resolved[0].condition_rating is ConditionRating.CR2  # stronger evidence kept
    assert any(r.kind is ContradictionKind.RATING_CONFLICT for r in reports)


# ── Fabricated materials / invented locations: entity substitution ──────────

def test_entity_substitution_is_dropped():
    pool = {"c1": "A London plane tree is located near the rear boundary."}
    # Model swapped the species β€” must be rejected as unsupported entity.
    bad = SurveyFinding(
        section="Grounds", element="Tree", condition_rating="NA",
        finding="A Lombardy Poplar is located near the rear boundary.",
        evidence=[EvidenceSpan(chunk_id="c1", text="located near the rear boundary")],
    )
    support, violations = validate_finding(bad, pool)
    assert support is SupportLevel.NOT_FOUND
    assert any("entity" in v for v in violations)


def test_correct_entity_is_supported():
    pool = {"c1": "A London plane tree is located near the rear boundary."}
    good = SurveyFinding(
        section="Grounds", element="Tree", condition_rating="NA",
        finding="A London plane tree is located near the rear boundary.",
        evidence=[EvidenceSpan(chunk_id="c1", text="A London plane tree is located near the rear boundary")],
    )
    support, violations = validate_finding(good, pool)
    assert support is SupportLevel.SUPPORTED
    assert violations == []


# ── Fabricated monetary totals / numbers ────────────────────────────────────

def test_fabricated_total_is_dropped():
    pool = {"c1": "Repairs to the parapet are recommended."}
    bad = SurveyFinding(
        section="Roofing", element="Parapet", condition_rating="2",
        finding="Repairs to the parapet are recommended at a cost of Β£12,500.",
        evidence=[EvidenceSpan(chunk_id="c1", text="Repairs to the parapet are recommended")],
    )
    support, violations = validate_finding(bad, pool)
    assert support is SupportLevel.NOT_FOUND
    assert any("number" in v or "amount" in v for v in violations)


def test_grounded_total_is_preserved():
    pool = {"c1": "Repairs to the parapet are recommended at a cost of Β£12,500."}
    good = SurveyFinding(
        section="Roofing", element="Parapet", condition_rating="2",
        finding="Repairs to the parapet are recommended at a cost of Β£12,500.",
        evidence=[EvidenceSpan(chunk_id="c1", text="Repairs to the parapet are recommended at a cost of Β£12,500")],
    )
    support, _ = validate_finding(good, pool)
    assert support is SupportLevel.SUPPORTED


# ── Unsupported risk / severity escalation ──────────────────────────────────

def test_unsupported_severity_is_dropped():
    pool = {"c1": "There is minor surface staining to the ceiling."}
    bad = SurveyFinding(
        section="Interior", element="Ceiling", condition_rating="2",
        finding="There is minor surface staining to the ceiling, a catastrophic and unsafe defect.",
        evidence=[EvidenceSpan(chunk_id="c1", text="There is minor surface staining to the ceiling")],
    )
    support, violations = validate_finding(bad, pool)
    assert support is SupportLevel.NOT_FOUND
    assert any("severity" in v for v in violations)


def test_severity_allowed_when_in_source():
    pool = {"c1": "The boiler flue is unsafe and must not be used."}
    good = SurveyFinding(
        section="Services", element="Boiler flue", condition_rating="3",
        finding="The boiler flue is unsafe and must not be used.",
        evidence=[EvidenceSpan(chunk_id="c1", text="The boiler flue is unsafe and must not be used")],
    )
    support, _ = validate_finding(good, pool)
    assert support is SupportLevel.SUPPORTED


# ── Citation to a non-existent chunk ────────────────────────────────────────

def test_citation_to_unknown_chunk_is_rejected():
    pool = {"c1": "Slate covering is sound."}
    bad = SurveyFinding(
        section="Roofing", element="Covering", condition_rating="1",
        finding="Slate covering is sound.",
        evidence=[EvidenceSpan(chunk_id="ghost", text="Slate covering is sound")],
    )
    support, violations = validate_finding(bad, pool)
    assert support is SupportLevel.NOT_FOUND
    assert any("unknown chunk_id" in v for v in violations)


# ── Malformed / paraphrased fabrication with no grounding ───────────────────

def test_ungrounded_paraphrase_is_dropped():
    pool = {"c1": "The flat roof is covered in felt."}
    bad = SurveyFinding(
        section="Roofing", element="Flat roof", condition_rating="2",
        finding="Extensive structural movement threatens imminent failure of the dwelling.",
        evidence=[EvidenceSpan(chunk_id="c1", text="The flat roof is covered in felt")],
    )
    support, _ = validate_finding(bad, pool)
    assert support is SupportLevel.NOT_FOUND


# ── Positive findings / warranties preserved when grounded ──────────────────

def test_warranty_preserved():
    pool = {"c1": "The replacement boiler was fitted in 2021 and carries a 10 year manufacturer warranty."}
    good = SurveyFinding(
        section="Services", element="Boiler", condition_rating="1",
        finding="The replacement boiler was fitted in 2021 and carries a 10 year manufacturer warranty.",
        evidence=[EvidenceSpan(
            chunk_id="c1",
            text="The replacement boiler was fitted in 2021 and carries a 10 year manufacturer warranty",
        )],
    )
    support, _ = validate_finding(good, pool)
    assert support is SupportLevel.SUPPORTED


# ── Contradiction: satisfactory vs defective ────────────────────────────────

def test_satisfactory_vs_defective_contradiction():
    a = _finding("Gutters", ConditionRating.NA, "The gutters are in good condition and sound.")
    a.support = SupportLevel.SUPPORTED
    b = _finding("Gutters", ConditionRating.NA, "The gutters are defective and leaking badly.")
    b.support = SupportLevel.NOT_FOUND
    resolved, reports = audit_contradictions([a, b])
    assert len(resolved) == 1
    assert resolved[0] is a  # evidence-stronger side kept
    assert any(r.kind is ContradictionKind.CONDITION for r in reports)


# ── Contradiction: operational vs non-operational ───────────────────────────

def test_operational_contradiction():
    a = _finding("Heating", ConditionRating.NA, "The heating system is fully operational and working.")
    a.support = SupportLevel.SUPPORTED
    b = _finding("Heating", ConditionRating.NA, "The heating system is not operational and out of order.")
    b.support = SupportLevel.PARTIAL
    resolved, reports = audit_contradictions([a, b])
    assert len(resolved) == 1
    assert any(r.kind is ContradictionKind.OPERATIONAL for r in reports)


# ── Duplicate collapse ──────────────────────────────────────────────────────

def test_duplicate_findings_collapsed():
    a = _finding("Chimney", ConditionRating.CR2, "The chimney stack shows perished pointing requiring repair.")
    b = _finding("Chimney", ConditionRating.CR2, "The chimney stack shows perished pointing requiring repair.")
    resolved, reports = audit_contradictions([a, b])
    assert len(resolved) == 1
    assert any(r.kind is ContradictionKind.DUPLICATE for r in reports)


# ── End-to-end gate: mixed batch keeps only grounded, non-conflicting ───────

def test_validate_findings_batch_drops_unsupported():
    pool = {
        "c1": "The main roof is natural slate in sound condition.",
        "c2": "The rear addition roof is covered in felt with no visible defects.",
    }
    findings = [
        SurveyFinding(section="Roofing", element="Main roof", condition_rating="1",
                      finding="The main roof is natural slate in sound condition.",
                      evidence=[EvidenceSpan(chunk_id="c1", text="The main roof is natural slate in sound condition")]),
        SurveyFinding(section="Roofing", element="Rear roof", condition_rating="3",
                      finding="The rear addition roof has collapsed and is deadly.",
                      evidence=[EvidenceSpan(chunk_id="c2", text="The rear addition roof is covered in felt")]),
    ]
    kept, dropped = validate_findings(findings, pool)
    assert len(kept) == 1
    assert kept[0].element == "Main roof"
    assert len(dropped) == 1


"""Section-domain scoping (STEP 6) β€” prevent cross-section contamination."""


def test_classify_section_aliases_and_keywords():
    from app.extraction.domain_scope import classify_section

    assert classify_section("Roof coverings") == "roofing"
    assert classify_section("Chimney stacks") == "chimney"
    assert classify_section("Rainwater pipes and gutters") == "rainwater"
    assert classify_section("Electricity") == "electrical"
    assert classify_section("About the property") == "general"


def test_classify_text_dominant_domain():
    from app.extraction.domain_scope import classify_text

    assert classify_text("The natural slate roof covering has slipped tiles at the ridge.") == "roofing"
    assert classify_text("The consumer unit lacks RCD protection on the circuits.") == "electrical"
    assert classify_text("This paragraph is generic boilerplate with no domain.") == "general"


def test_scope_chunks_excludes_foreign_domain():
    from app.extraction.domain_scope import scope_chunks

    class _Row:
        def __init__(self, text):
            self.text = text

    chunks = [
        _Row("The slate roof covering is sound at the ridge and eaves."),
        _Row("The drainage manhole and inspection chamber were inspected."),
        _Row("General introductory text about the inspection."),
    ]
    kept = scope_chunks("roofing", chunks)
    texts = [c.text for c in kept]
    assert any("slate roof" in t for t in texts)
    assert any("introductory" in t for t in texts)  # general is admissible
    assert not any("manhole" in t for t in texts)   # drainage excluded


def test_scope_chunks_never_starves_under_strict():
    from app.extraction.domain_scope import scope_chunks

    class _Row:
        def __init__(self, text):
            self.text = text

    # All chunks belong to a different domain -> strict returns originals
    # rather than an empty pool.
    chunks = [_Row("The consumer unit and wiring circuits were inspected.")]
    kept = scope_chunks("roofing", chunks, strict=True)
    assert len(kept) == 1
    kept_nonstrict = scope_chunks("roofing", chunks, strict=False)
    assert kept_nonstrict == []


def test_section_extraction_confidence():
    pool = {"c1": "The main roof is natural slate in sound condition."}
    f = SurveyFinding(section="Roofing", element="Main roof", condition_rating="1",
                      finding="The main roof is natural slate in sound condition.",
                      evidence=[EvidenceSpan(chunk_id="c1", text="The main roof is natural slate in sound condition")])
    kept, _ = validate_findings([f], pool)
    from app.extraction.schemas import SectionExtraction
    sec = SectionExtraction(section="Roofing", findings=kept)
    assert sec.confidence == 1.0


"""Post-generation output validator + abstention (forbidden phrases / metrics)."""


def test_output_validator_flags_forbidden_phrases():
    from app.extraction.output_validator import find_violations, is_clean

    bad = "The roof appears to be defective and overall reliability is questionable."
    v = find_violations(bad, evidence="")
    assert any("appears to" in x for x in v)
    assert any("overall" in x for x in v)
    assert not is_clean(bad)


def test_output_validator_flags_fabricated_percentage_and_score():
    from app.extraction.output_validator import find_violations

    v = find_violations("Authenticity score is high with 87% confidence.", evidence="")
    assert any("percentage" in x for x in v)
    assert any("metric" in x for x in v)


def test_output_validator_allows_terms_present_in_evidence():
    from app.extraction.output_validator import is_clean

    # "unsafe" is permitted because it is verbatim in the evidence.
    text = "The flue is unsafe."
    evidence = "The boiler flue is unsafe and must not be used."
    assert is_clean(text, evidence)


def test_output_validator_clean_text_passes():
    from app.extraction.output_validator import is_clean

    assert is_clean("The main roof covering is natural slate.", evidence="")


def test_abstain_on_low_confidence_and_attribution_failure():
    from app.extraction.output_validator import should_abstain

    assert should_abstain("clean text", "", confidence=0.5, min_confidence=1.0)
    assert should_abstain("clean text", "", evidence_aligned=False)
    assert should_abstain("clean text", "", source_attributed=False)
    assert not should_abstain("The roof is slate.", "", confidence=1.0)


def test_findings_to_atomic_claims_grounded():
    from app.extraction.extractor import findings_to_atomic_claims
    from app.extraction.schemas import ClaimType

    f = SurveyFinding(
        section="Roofing", element="Main roof", condition_rating="2",
        finding="The main roof covering is natural slate with slipped tiles.",
        evidence=[EvidenceSpan(chunk_id="c1", page=12, section_label="Page 12",
                               text="The main roof covering is natural slate with slipped tiles")],
    )
    f.support = SupportLevel.SUPPORTED
    claims = findings_to_atomic_claims([f])
    types = {c.claim_type for c in claims}
    assert ClaimType.CONDITION_RATING in types
    assert ClaimType.OBSERVATION in types
    rating = next(c for c in claims if c.claim_type is ClaimType.CONDITION_RATING)
    assert "is 2" in rating.claim
    assert rating.evidence.chunk_id == "c1"
    assert rating.evidence.page == 12
    assert rating.verification.supported is True
    assert rating.verification.confidence == 1.0


def test_findings_to_atomic_claims_abstains_below_confidence():
    from app.extraction.extractor import findings_to_atomic_claims

    f = SurveyFinding(
        section="Roofing", element="Main roof", condition_rating="2",
        finding="The main roof covering is natural slate.",
        evidence=[EvidenceSpan(chunk_id="c1", text="The main roof covering is natural slate")],
    )
    f.support = SupportLevel.PARTIAL  # confidence 0.5 < default min 1.0
    assert findings_to_atomic_claims([f], min_confidence=1.0) == []
    # Lowering the bar admits them.
    assert findings_to_atomic_claims([f], min_confidence=0.5)