"""Regression tests for defects found in the Aug 2026 agent-mode code review.

R2 - text_coverage._SHEET_ID_RE could not match hyphenated sheet ids (C-001),
     leaving civil/landscape pages sheet_number=None and producing false
     "declared but not in set" reconciliation warnings.
R3 - config bool knobs mixed `== "true"` with the 1/true/yes set, so setting
     EXTRACT_TEXT_RETRY_ENABLED=1 silently DISABLED the retry ladder.

See also tests/agents/test_verifier.py for the verifier's "corrected"
semantics, which are intentional and pinned there.
"""

import importlib
import os
from unittest import mock

from backend.sheet_reconcile import declared_sheet_list, reconcile_sheets
from backend.text_coverage import recover_sheet_number


# --- R2: hyphenated sheet ids ----------------------------------------------

def test_recover_sheet_number_handles_hyphenated_civil_id():
    page_text = ("GENERAL NOTES\n" * 40) + "PROJECT NO 2024-118\nSHEET\nC-001\n"
    assert recover_sheet_number(page_text) == "C-001"


def test_recover_sheet_number_still_handles_plain_ids():
    page_text = ("NOTES\n" * 40) + "SHEET\nS302\n"
    assert recover_sheet_number(page_text) == "S302"


def test_recovered_hyphenated_id_reconciles_against_declared_index():
    """The whole point: a recovered C-001 must not read as a missing sheet."""
    declared = declared_sheet_list({1: "SHEET LIST\nC-001 CIVIL\nA102 PLAN\n"})
    assert declared == ["C-001", "A102"]
    recovered = recover_sheet_number(("X\n" * 40) + "SHEET\nC-001\n")
    recon = reconcile_sheets(
        [{"sheet_number": recovered}, {"sheet_number": "A102"}], declared)
    assert recon["declared_not_in_set"] == []
    assert recon["in_set_not_declared"] == []


# --- R3: boolean env knob parsing ------------------------------------------

def test_numeric_one_enables_ladder_knobs():
    with mock.patch.dict(os.environ, {
        "EXTRACT_TEXT_RETRY_ENABLED": "1",
        "EXTRACT_FALLBACK_ENABLED": "yes",
    }):
        cfg = importlib.reload(importlib.import_module("backend.config"))
        try:
            assert cfg.EXTRACT_TEXT_RETRY_ENABLED is True
            assert cfg.EXTRACT_FALLBACK_ENABLED is True
        finally:
            importlib.reload(cfg)


def test_false_values_still_disable_ladder_knobs():
    with mock.patch.dict(os.environ, {
        "EXTRACT_TEXT_RETRY_ENABLED": "false",
        "EXTRACT_FALLBACK_ENABLED": "0",
    }):
        cfg = importlib.reload(importlib.import_module("backend.config"))
        try:
            assert cfg.EXTRACT_TEXT_RETRY_ENABLED is False
            assert cfg.EXTRACT_FALLBACK_ENABLED is False
        finally:
            importlib.reload(cfg)
