import importlib.util import json import struct import sys from pathlib import Path import pytest ROOT = Path(__file__).parents[1] spec = importlib.util.spec_from_file_location("extract_sgv_dfm_bindings", ROOT / "scripts" / "extract_sgv_dfm_bindings.py") dfm = importlib.util.module_from_spec(spec); sys.modules[spec.name] = dfm; spec.loader.exec_module(dfm) def s(text): b = text.encode("latin-1"); return bytes([len(b)]) + b def prop_str(name, value): return s(name) + b"\x06" + s(value) def component(cls, name, props=b"", kids=b""): return s(cls) + s(name) + props + b"\x00" + kids + b"\x00" def test_parser_reads_nested_controls_datafields_and_collections(): grid_cols = s("Columns") + b"\x0e" + b"\x01" + s("Visible") + b"\x09" + b"\x00" + b"\x01" + s("FieldName") + b"\x06" + s("QTDE") + b"\x00" + b"\x00" edit = component("TDBEdit", "DBEdit72", prop_str("DataField", "CFOP_ENTRADA") + prop_str("DataSource", "dsproduto2")) group = component("TGroupBox", "FlatGroupBox5", prop_str("Caption", "Nota Fiscal de Entrada"), edit) grid = component("TDBGrid", "Grid1", grid_cols) form = b"TPF0" + component("Tfrm", "frm", b"", group + grid) r = dfm.R(form, 4); tree = r.component() out = []; dfm.walk(tree, [], out) ctl = next(c for c in out if c["DataField"] == "CFOP_ENTRADA") assert ctl["name"] == "DBEdit72" and ctl["DataSource"] == "dsproduto2" and any("Nota Fiscal de Entrada" in p for p in ctl["path"]) assert r.p == len(form) # whole stream consumed, collections included def test_ui_map_uses_dfm_evidence_and_keeps_policies(): 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 by = {f["ui_label"]: f for f in d["fields"]} assert by["Pis Entrada"]["field"] == "COD_ENTRADA_PIS" and by["Cofins Saída"]["field"] == "COD_SAIDA_COFINS" assert by["NCM"]["field"] == "CLASSIFICACAO_FISCAL" and by["Seletor ao lado do CEST (T/I/N/S)"]["field"] == "SITUACAO_TRIBUTARIA" assert by["Código da origem da Mercadoria (0/1/2)"]["classification"] == "CONFIRMED_STRONG" and by["Código da origem da Mercadoria (0/1/2)"]["write_target"] assert by["CSOSN Informações - Significados"]["classification"] == "NO_MATCH" and not by["CSOSN Informações - Significados"]["write_target"] for f in d["fields"]: if f["write_target"]: assert f["classification"] == "CONFIRMED_STRONG" and f["table"] == "C000025" and f["evidence_basis"]