""" Unit tests for pali_utils — Thai/Pali text utilities. Tests cover: - Thai digit conversion - Pali confusion map variant generation - PyThaiNLP autocorrect (with fallback when library is absent) - Pali correction dictionary lookup - Full correction dict generation """ import pytest from app.services.pali_utils import ( to_thai_digits, THAI_PALI_CONFUSIONS, _generate_pali_variants, PALI_CORRECTIONS, PALI_CORRECTIONS_FULL, POPULAR_TERMS, HAS_PYTHAINLP, _similar_enough, ) class TestToThaiDigits: def test_converts_arabic_to_thai(self): assert to_thai_digits("123") == "๑๒๓" def test_handles_empty(self): assert to_thai_digits("") == "" def test_handles_mixed(self): assert to_thai_digits("มรรค8") == "มรรค๘" def test_leaves_thai_digits_unchanged(self): assert to_thai_digits("๑๒๓") == "๑๒๓" class TestPaliConfusions: def test_confusion_map_has_expected_keys(self): assert "ส" in THAI_PALI_CONFUSIONS assert "ณ" in THAI_PALI_CONFUSIONS assert "ภ" in THAI_PALI_CONFUSIONS def test_generate_variants(self): variants = _generate_pali_variants("สติ") assert "ศติ" in variants or "ษติ" in variants def test_generate_variants_no_confusion(self): """Words without confusion characters should produce no variants.""" variants = _generate_pali_variants("ธรรม") assert len(variants) == 0 or all(v != "ธรรม" for v in variants) class TestPaliCorrections: def test_basic_correction(self): assert PALI_CORRECTIONS["อริยะสัจ"] == "อริยสัจ" def test_niphan_variants(self): assert PALI_CORRECTIONS["นิพพาณ"] == "นิพพาน" assert PALI_CORRECTIONS["นิพาน"] == "นิพพาน" def test_full_dict_includes_hand_curated(self): assert PALI_CORRECTIONS_FULL["อริยะสัจ"] == "อริยสัจ" assert PALI_CORRECTIONS_FULL["นิพพาณ"] == "นิพพาน" def test_full_dict_includes_auto_generated(self): """POPULAR_TERMS should have auto-generated variants via confusion map.""" if "อริยสัจ" in POPULAR_TERMS: has_variant = any( k != "อริยสัจ" and PALI_CORRECTIONS_FULL.get(k) == "อริยสัจ" for k in PALI_CORRECTIONS_FULL ) assert has_variant, "Expected auto-generated variants for อริยสัจ" def test_to_thai_digits_in_corrections(self): """Ensure numbered terms have both digit formats.""" assert "ขัน5" in PALI_CORRECTIONS assert PALI_CORRECTIONS["ขัน5"] == "ขันธ์ ๕" class TestSimilarEnough: def test_identical(self): assert _similar_enough("สติปัฏฐาน", "สติปัฏฐาน") is True def test_similar_passes(self): # Minor variation should pass assert _similar_enough("สติปัฏฐาน", "สติปัฎฐาน") is True def test_vastly_different_fails(self): # Very different should fail assert _similar_enough("ปุพเพนิวาสานุสสติญาณ", "ปพเนวสสตญ") is False def test_empty(self): assert _similar_enough("", "") is True class TestPopularTerms: def test_popular_terms_is_list_of_strings(self): assert isinstance(POPULAR_TERMS, list) assert len(POPULAR_TERMS) > 0 assert all(isinstance(t, str) for t in POPULAR_TERMS) def test_includes_core_doctrines(self): core = {"อริยสัจ", "นิพพาน", "ไตรลักษณ์", "สติปัฏฐาน"} assert core.issubset(set(POPULAR_TERMS))