From 0992744490eab0a510f0f2d933bd7436ed0df66a Mon Sep 17 00:00:00 2001 From: Claude Agent Date: Thu, 2 Apr 2026 14:46:10 +0000 Subject: [PATCH] refactor(anaf): remove dead code in sanitize_cui, fix empty test MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Remove unreachable OCR-skip fallback (raw_bare can't be all-digits if strip_ro_prefix changed it via OCR fix). Add real test for the checksum result==10→0 branch using CUI 14186770. Co-Authored-By: Claude Opus 4.6 (1M context) --- api/app/services/anaf_service.py | 6 ------ api/tests/test_cui_validation.py | 17 ++++++++--------- 2 files changed, 8 insertions(+), 15 deletions(-) diff --git a/api/app/services/anaf_service.py b/api/app/services/anaf_service.py index 127a6fc..63acfbe 100644 --- a/api/app/services/anaf_service.py +++ b/api/app/services/anaf_service.py @@ -43,7 +43,6 @@ def validate_cui_checksum(bare_cui: str) -> bool: digits = [int(d) for d in bare_cui] check_digit = digits[-1] body = digits[:-1] - # Pad left with zeros to 9 positions padded = [0] * (9 - len(body)) + body total = sum(d * k for d, k in zip(padded, _CUI_KEY)) result = (total * 10) % 11 @@ -66,11 +65,6 @@ def sanitize_cui(raw_cf: str) -> tuple[str, str | None]: if validate_cui(bare) and validate_cui_checksum(bare): return bare, None - # Try without OCR fix (raw, just stripped) - raw_bare = re.sub(r'^RO\s*', '', raw_cf.strip().upper()) - if raw_bare != bare and validate_cui(raw_bare) and validate_cui_checksum(raw_bare): - return raw_bare, None - # Sanitized version passes format but not checksum if validate_cui(bare): return bare, f"CUI {bare} nu trece verificarea cifrei de control" diff --git a/api/tests/test_cui_validation.py b/api/tests/test_cui_validation.py index afd6bc0..014d61f 100644 --- a/api/tests/test_cui_validation.py +++ b/api/tests/test_cui_validation.py @@ -148,15 +148,14 @@ class TestValidateCuiChecksum: assert validate_cui_checksum("1") is False def test_checksum_result_10_becomes_0(self): - """When (sum*10)%11 == 10, check digit should be 0.""" - # Build a CUI where the algorithm yields 10 - # Body 12345678 → pad to 9: 012345678 - # 0*7+1*5+2*3+3*2+4*1+5*7+6*5+7*3+8*2 = 0+5+6+6+4+35+30+21+16 = 123 - # 123*10=1230, 1230%11 = 111*11=1221, remainder=9 → check=9 → not 10 - # Let's just verify a known CUI ending in 0 works - # CUI 46628322 from data: check=2, not 0. Skip this specific edge case test - # and just verify the code path exists - pass + """When (sum*10)%11 == 10, check digit should be 0. + + CUI 14186770: body=1418677, padded=001418677, + sum=0+0+3+8+1+42+35+21+14=124, 1240%11=10 → check=0. + """ + assert validate_cui_checksum("14186770") is True + # Wrong check digit for same body + assert validate_cui_checksum("14186771") is False # ===========================================================================