"""Tests for translate.py auto_translate and main.py title_case.""" import pytest from backend.translate import auto_translate # --------------------------------------------------------------------------- # title_case (inlined here since it's a trivial helper in main.py) # --------------------------------------------------------------------------- def title_case(s: str) -> str: return s.strip().title() if s else s class TestTitleCase: def test_basic(self): assert title_case("pain relief") == "Pain Relief" def test_single_word(self): assert title_case("sleep") == "Sleep" def test_preserves_already_cased(self): assert title_case("Pain Relief") == "Pain Relief" def test_empty_string(self): assert title_case("") == "" def test_none(self): assert title_case(None) is None def test_strips_whitespace(self): assert title_case(" hello world ") == "Hello World" # --------------------------------------------------------------------------- # auto_translate # --------------------------------------------------------------------------- class TestAutoTranslate: def test_empty_string(self): assert auto_translate("") == "" def test_single_keyword(self): assert auto_translate("失眠") == "Insomnia" def test_compound_name(self): result = auto_translate("助眠配方") assert "Sleep" in result assert "Blend" in result def test_head_pain(self): result = auto_translate("头痛") # 头痛 is a single keyword → Headache assert "Headache" in result def test_shoulder_neck_massage(self): result = auto_translate("肩颈按摩") assert "Neck" in result or "Shoulder" in result assert "Massage" in result def test_no_duplicate_words(self): # 肩颈 → "Neck & Shoulder", but should not duplicate if sub-keys match result = auto_translate("肩颈护理") words = result.split() # No exact duplicate consecutive words for i in range(len(words) - 1): if words[i] == words[i + 1]: pytest.fail(f"Duplicate word '{words[i]}' in '{result}'") def test_skincare_blend(self): result = auto_translate("皮肤修复") assert "Skin" in result assert "Repair" in result def test_foot_soak(self): result = auto_translate("泡脚配方") assert "Foot Soak" in result or "Foot" in result def test_ascii_passthrough(self): # Embedded ASCII letters are preserved result = auto_translate("DIY面膜") assert "DIY" in result or "Diy" in result assert "Face Mask" in result or "Mask" in result def test_pure_chinese_returns_english(self): result = auto_translate("薰衣草精华") # Should not return original Chinese; should have English words assert any(c.isascii() and c.isalpha() for c in result) def test_fallback_for_unknown(self): # Completely unknown chars get skipped; if nothing matches, returns original result = auto_translate("㊗㊗㊗") assert result == "㊗㊗㊗" def test_children_sleep(self): result = auto_translate("儿童助眠") assert "Children" in result assert "Sleep" in result def test_menstrual_pain(self): result = auto_translate("痛经调理") assert "Menstrual Pain" in result or "Menstrual" in result assert "Therapy" in result def test_result_is_title_cased(self): result = auto_translate("排毒按摩") # Each word should start with uppercase for word in result.split(): if word == "&": continue assert word[0].isupper(), f"'{word}' in '{result}' is not title-cased"