git-subtree-dir: fiscal git-subtree-mainline:d95165fc8fgit-subtree-split:102ba84500
100 lines
5.6 KiB
Python
100 lines
5.6 KiB
Python
"""COPY-ONLY E2E of the TECHNICAL_PLACEHOLDER case found by the operator: the SGV refuses a product without NCM, so it was saved with CLASSIFICACAO_FISCAL = '00000000'.
|
|
|
|
python scripts/enrichment_placeholder_e2e.py
|
|
|
|
Real Firebird COPY + the real Agent worker + the real Hub resolver (in-process, fake providers, no internet). Proves: placeholder detected and classified (DRY_RUN = zero writes), replaced under ENABLED,
|
|
CEST/IPI filled only when safe, cClassTrib typed by the operator untouched, a real NCM (or an unlisted filler such as 99999999) preserved even when the KB disagrees, second run = 0 writes.
|
|
"""
|
|
import json
|
|
import sys
|
|
import threading
|
|
import time
|
|
from pathlib import Path
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
sys.path.insert(0, str(ROOT)); sys.path.insert(0, str(ROOT / "scripts")); sys.path.insert(0, str(ROOT / "agent" / "src"))
|
|
from dotenv import load_dotenv # noqa: E402
|
|
|
|
load_dotenv(ROOT / ".env")
|
|
import enrichment_copy_e2e as ce # noqa: E402
|
|
from gti_fiscal_agent import enrichment as E # noqa: E402
|
|
|
|
base = ce.base
|
|
|
|
|
|
def insert(code, ncm, ean, cclasstrib=None, name="COCA-COLA ORIGINAL LATA 350 ML"):
|
|
conn = base.connect(True); cur = conn.cursor()
|
|
cur.execute("INSERT INTO C000025 (CODIGO, PRODUTO, CODBARRA, CLASSIFICACAO_FISCAL, CCLASSTRIB) VALUES (?, ?, ?, ?, ?)", (code, name, ean, ncm, cclasstrib))
|
|
conn.commit(); conn.close()
|
|
|
|
|
|
def row(code):
|
|
conn = base.connect(False); cur = conn.cursor()
|
|
cur.execute("SELECT CLASSIFICACAO_FISCAL, CEST, IPI, CCLASSTRIB FROM C000025 WHERE CODIGO=?", (code,))
|
|
r = cur.fetchone(); conn.close()
|
|
return tuple(None if v is None else (v if isinstance(v, float) else str(v).strip()) for v in r) if r else None
|
|
|
|
|
|
def main():
|
|
checks = {}
|
|
base.restore_copy()
|
|
hub = ce.InProcessHub()
|
|
from central.app.models import CestEntry
|
|
for entry in hub.session.query(CestEntry).all():
|
|
entry.description = "COCA-COLA ORIGINAL LATA 350 ML" # official CEST text close enough to the product for the (conservative) description match
|
|
hub.session.commit()
|
|
for suffix in ("", "-wal", "-shm"):
|
|
try:
|
|
(ROOT / "scratch_enrich" / f"enrich_ph_e2e.db{suffix}").unlink()
|
|
except FileNotFoundError:
|
|
pass
|
|
store = E.EnrichmentStore(ROOT / "scratch_enrich" / "enrich_ph_e2e.db")
|
|
worker = E.EnrichmentWorker(hub=hub, store=store, read_conn=lambda: base.connect(False), write_conn=lambda: base.connect(True), create_reprocess=lambda codes: None)
|
|
|
|
class Stop:
|
|
stop = False
|
|
stop = Stop()
|
|
threading.Thread(target=worker.run_forever, args=(stop,), daemon=True).start()
|
|
ean = ce.gtin("789700000001")
|
|
hub.features = {"features_configured": True, "corrector_enabled": True, "enrichment_mode": "DRY_RUN"}
|
|
worker._features_at = -999
|
|
time.sleep(6) # baseline taken while the function is active
|
|
insert("PH001", "00000000", ean, cclasstrib="200009")
|
|
time.sleep(9)
|
|
checks["PLACEHOLDER_PRODUCT_DETECTED_DRY_RUN"] = store.get("PH001") is not None and hub.calls >= 1
|
|
checks["DRY_RUN_ZERO_WRITES"] = row("PH001") == ("00000000", None, None, "200009")
|
|
decisions = [it for rep in hub.reports for it in rep["fields"] if rep["product_code"] == "PH001"]
|
|
checks["DRY_RUN_DECISION_GENERATED_FOR_NCM"] = any(d["field"] == "CLASSIFICACAO_FISCAL" and d["outcome"] == "DRY_RUN" and d["old"] == "00000000" and d["new"] == "22021000" for d in decisions)
|
|
hub.features = {"features_configured": True, "corrector_enabled": True, "enrichment_mode": "ENABLED"}
|
|
worker._features_at = -999
|
|
worker._auth_cache = (None, 0.0)
|
|
insert("PH002", "00000000", ean, cclasstrib="200009") # same real scenario, function already ENABLED
|
|
t0 = time.monotonic()
|
|
while time.monotonic() - t0 < 25 and row("PH002")[0] == "00000000":
|
|
time.sleep(0.2)
|
|
time.sleep(4) # remaining fields + audit report follow the NCM write
|
|
r2 = row("PH002")
|
|
checks["ENABLED_PLACEHOLDER_REPLACED"] = r2[0] == "22021000"
|
|
checks["CEST_IPI_ONLY_WHEN_SAFE"] = r2[2] == 0.0 and r2[1] == "0300700"
|
|
checks["CCLASSTRIB_OPERATOR_VALUE_UNTOUCHED"] = r2[3] == "200009"
|
|
applied = [it for rep in hub.reports for it in rep["fields"] if rep["product_code"] == "PH002" and it["outcome"] == "APPLIED"]
|
|
checks["AUDIT_RECORDS_THE_REPLACED_PLACEHOLDER"] = any(a["field"] == "CLASSIFICACAO_FISCAL" and a["old"] == "00000000" for a in applied)
|
|
# negative: real NCM / unlisted filler are preserved even though the KB says 22021000
|
|
insert("PH003", "19059090", ean); insert("PH004", "99999999", ean); insert("PH005", "22021000", ean)
|
|
time.sleep(12)
|
|
checks["EXISTING_REAL_VALUE_PRESERVED"] = row("PH003")[0] == "19059090" and row("PH005")[0] == "22021000"
|
|
checks["UNLISTED_FILLER_NOT_OVERWRITTEN"] = row("PH004")[0] == "99999999"
|
|
calls_before, writes_before = hub.calls, sum(1 for rep in hub.reports for it in rep["fields"] if it["outcome"] == "APPLIED")
|
|
time.sleep(14)
|
|
writes_after = sum(1 for rep in hub.reports for it in rep["fields"] if it["outcome"] == "APPLIED")
|
|
checks["SECOND_RUN_WRITES_0"] = writes_after == writes_before and row("PH002")[0] == "22021000"
|
|
stop.stop = True
|
|
result = {"copy_only": True, "checks": checks, "ALL_PASS": all(checks.values()), "PH002": row("PH002")}
|
|
(ROOT / "docs" / "runs" / "enrichment_placeholder_e2e.json").write_text(json.dumps(result, indent=1), encoding="utf-8")
|
|
print(json.dumps(result, indent=1))
|
|
sys.exit(0 if result["ALL_PASS"] else 1)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|