import importlib.util import json import sys from pathlib import Path import pytest ROOT = Path(__file__).parents[1] def load(name, rel): sys.path.insert(0, str(ROOT / "agent" / "src")); sys.path.insert(0, str(ROOT / "src")) spec = importlib.util.spec_from_file_location(name, ROOT / rel) mod = importlib.util.module_from_spec(spec); sys.modules[name] = mod; spec.loader.exec_module(mod) return mod snap = load("sgv_product_snapshot", "scripts/sgv_product_snapshot.py") audit = load("audit_firebird_database", "scripts/audit_firebird_database.py") executor = load("firebird_write_executor", "scripts/firebird_write_executor.py") def test_diff_lists_changed_columns_and_flags_technical_ones(): before = {"CFOP": "5102", "CSOSN": "102", "ULTIMA_ALTERACAO": "2026-01-01"} after = {"CFOP": "5405", "CSOSN": "102", "ULTIMA_ALTERACAO": "2026-02-02"} d = {c["FIELD"]: c for c in snap.compute_diff(before, after)} assert d["CFOP"]["BEFORE"] == "5102" and d["CFOP"]["AFTER"] == "5405" and not d["CFOP"]["technical"] assert d["ULTIMA_ALTERACAO"]["technical"] and "CSOSN" not in d def test_ledger_confirms_only_single_column_changes(tmp_path, monkeypatch): monkeypatch.setattr(snap, "LEDGER", tmp_path / "ledger.json") ok = snap.record("CFOP saĆ­da", "1", [{"FIELD": "CFOP", "BEFORE": "a", "AFTER": "b", "technical": False}, {"FIELD": "ULTIMA_ALTERACAO", "BEFORE": 1, "AFTER": 2, "technical": True}]) two = snap.record("X", "1", [{"FIELD": "A", "BEFORE": 1, "AFTER": 2, "technical": False}, {"FIELD": "B", "BEFORE": 1, "AFTER": 2, "technical": False}]) none = snap.record("Y", "1", []) assert ok["confirmed"] and not two["confirmed"] and not none["confirmed"] assert len(json.loads((tmp_path / "ledger.json").read_text(encoding="utf-8"))) == 3 def test_company_context_is_read_generically_and_fails_closed(): from gti_fiscal_agent.firebird_reader import parse_company_context assert parse_company_context([("sp", "3")]) == {"uf": "SP", "crt": "3"} for bad in ([], [("BA", "1"), ("SP", "1")], [("", "1")], [("BA", "9")]): with pytest.raises(ValueError): parse_company_context(bad) def test_audit_helpers(): assert audit.ncm_bucket("22030000") == "eight_digits" and audit.ncm_bucket(" ") == "blank" and audit.ncm_bucket("2203.00") == "other_format" c = audit.compat({"CFOP": 37, "IPI": 27, "CST": 8}) assert c["CFOP"] == "OK" and c["IPI"] == "OK" and c["CST"].startswith("TYPE_DIFFERS") and c["CSOSN"] == "MISSING" @pytest.mark.parametrize("table", ["NFCE", "NFCE_ITENS", "C000032", "C000061", "C000062", "C000088", "INVENTARIO"]) def test_transactional_tables_are_never_writable(table): with pytest.raises(executor.WriteRefused): executor.assert_allowed_field(table, "CFOP") def test_ui_map_write_targets_have_evidence_and_transactional_writes_forbidden(): p = ROOT / "docs" / "SGV_PRODUCT_FISCAL_UI_MAP_V1.json" if not p.exists(): pytest.skip("map not generated") d = json.loads(p.read_text(encoding="utf-8")) assert d["transactional_table_writes_allowed"] is False for f in d["fields"]: assert f["table"] == "C000025" if f["write_target"]: assert f["confirmed_by_ui_diff"] or f["evidence_basis"] assert f["field"] if f.get("classification") in ("PROBABLE", "AMBIGUOUS", "NO_MATCH"): assert not f["write_target"]