# tests/test_addabug_register.py
import importlib.util, os
ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
def _load(name, path):
    s = importlib.util.spec_from_file_location(name, path); m = importlib.util.module_from_spec(s); s.loader.exec_module(m); return m
cov = _load("sg_ledger_e2e", os.path.join(ROOT, "ledger.py"))

def _real_registry():
    cells = cov.bench.load_cells()
    detectors, _ = cov.manifest.load(ROOT)
    solutions, _ = cov.manifest.load(ROOT, kind="solution")   # discovers sql-parameterize from disk — no name in code
    return cov.build(cells, detectors, cov.load_confirmed(), cov.taxonomy_classes(), solutions)

def _s4_row(reg):
    rows = [r for r in reg["defects"] if r["defect"] == "S4-kb-spaceids-sqli"]
    assert len(rows) == 1, "exactly one S4 defect row"
    return rows[0]

def test_s4_full_lifecycle_with_honest_empties():
    row = _s4_row(_real_registry())
    assert row["provenance"].get("discovered_by") == "harvest"   # Discovered ✓
    assert "baseline" in row["detectors"]                        # Re-discovery = band-1 (baseline floor)
    assert row["resolution"] == "suggested"                      # Resolution = located-suggestion adapter
    assert row["ratcheted"] is False                             # Prevention = none (no confirmed S4 entry) — HONEST empty

def test_register_not_rewrite_no_hardcoded_defect_in_ledger():
    src = open(os.path.join(ROOT, "ledger.py"), encoding="utf-8").read()
    assert "sql-parameterize" not in src and "S4-kb" not in src  # the row is DERIVED from manifests, never named in code
