"""Tests for response formatting functions."""
from __future__ import annotations
from sample_data import (
SAMPLE_LANGUAGES_RESPONSE,
SAMPLE_SINGLE_LANGUAGE,
SAMPLE_TRANSLATION_RESPONSE,
SAMPLE_TRANSLATION_WITH_TRANSLIT,
)
from levelang_mcp.formatting import (
format_comparison,
format_language_detail,
format_language_list,
format_translation,
)
class TestFormatTranslation:
def test_basic_translation(self):
result = format_translation(SAMPLE_TRANSLATION_RESPONSE)
assert "Translation: Bonjour le monde" in result
assert "Level: A2 (elementary proficiency)" in result
assert "Mood: Casual" in result
assert "Provider: gemini / gemini-2.5-flash-lite" in result
assert "Processing time: 800ms" in result
def test_transliteration_omitted_when_null(self):
result = format_translation(SAMPLE_TRANSLATION_RESPONSE)
assert "Transliteration" not in result
def test_transliteration_included_when_present(self):
result = format_translation(SAMPLE_TRANSLATION_WITH_TRANSLIT)
assert "Transliteration: n\u01d0 h\u01ceo sh\u00ec ji\u00e8" in result
def test_transcription_omitted_when_null(self):
result = format_translation(SAMPLE_TRANSLATION_RESPONSE)
assert "Transcription" not in result
def test_transcription_included_when_present(self):
response = {
**SAMPLE_TRANSLATION_RESPONSE,
"transcription": "Hello world",
}
result = format_translation(response)
assert "Transcription: Hello world" in result
def test_mode_included_when_present(self):
result = format_translation(SAMPLE_TRANSLATION_RESPONSE)
assert "Mode: Written" in result
def test_mode_omitted_when_absent(self):
response = {
"translation": "Bonjour",
"transliteration": None,
"transcription": None,
"metadata": {
"level": "A2",
"mood": "Casual",
},
}
result = format_translation(response)
assert "Mode:" not in result
def test_minimal_metadata(self):
response = {
"translation": "Bonjour",
"transliteration": None,
"transcription": None,
"metadata": {},
}
result = format_translation(response)
assert result == "Translation: Bonjour"
class TestFormatLanguageList:
def test_formats_multiple_languages(self):
result = format_language_list(SAMPLE_LANGUAGES_RESPONSE)
assert "French (fra)" in result
assert "Mandarin Chinese (cmn)" in result
def test_shows_levels(self):
result = format_language_list(SAMPLE_LANGUAGES_RESPONSE)
assert "Beginner, Intermediate, Advanced, Fluent" in result
def test_shows_moods(self):
result = format_language_list(SAMPLE_LANGUAGES_RESPONSE)
assert "Casual, Polite, Formal" in result
def test_shows_modes(self):
result = format_language_list(SAMPLE_LANGUAGES_RESPONSE)
assert "Modes: Written, Spoken" in result
def test_omits_modes_when_empty(self):
result = format_language_list(SAMPLE_LANGUAGES_RESPONSE)
# Mandarin has empty modes list -- should not show a Modes line
cmn_section = result.split("Mandarin Chinese (cmn)")[1]
assert "Modes:" not in cmn_section
def test_empty_languages(self):
result = format_language_list({"languages": [], "total_count": 0})
assert result == "No languages available."
class TestFormatLanguageDetail:
def test_header(self):
result = format_language_detail(SAMPLE_SINGLE_LANGUAGE)
assert result.startswith("French (fra)")
def test_levels_with_descriptions(self):
result = format_language_detail(SAMPLE_SINGLE_LANGUAGE)
assert "Beginner: Basic vocabulary" in result
assert "Intermediate: Complex structures" in result
def test_moods_with_default_marker(self):
result = format_language_detail(SAMPLE_SINGLE_LANGUAGE)
assert "Casual (default): Everyday conversation" in result
assert "Formal: Professional contexts" in result
# Formal should NOT have default marker
assert "Formal (default)" not in result
def test_modes_with_default_marker(self):
result = format_language_detail(SAMPLE_SINGLE_LANGUAGE)
assert (
"Written (default): Standard written French as taught in textbooks"
in result
)
assert (
"Spoken: How native French speakers actually talk in everyday conversation"
in result
)
# Spoken should NOT have default marker
assert "Spoken (default)" not in result
def test_can_be_used_as(self):
result = format_language_detail(SAMPLE_SINGLE_LANGUAGE)
assert "Can be used as: target" in result
class TestFormatComparison:
def test_basic_comparison(self):
results = [
{
"level": "beginner",
"ok": True,
"result": {
"translation": "Bonjour",
"transliteration": None,
"metadata": {"processing_time_ms": 800},
},
},
{
"level": "advanced",
"ok": True,
"result": {
"translation": "Bonjour, comment allez-vous",
"transliteration": None,
"metadata": {"processing_time_ms": 1000},
},
},
]
result = format_comparison("Hello", "French", "casual", results)
assert 'Comparing translations of: "Hello"' in result
assert "French" in result
assert "── Beginner ──" in result
assert "── Advanced ──" in result
assert "Bonjour" in result
def test_comparison_with_error(self):
results = [
{
"level": "beginner",
"ok": True,
"result": {
"translation": "Bonjour",
"transliteration": None,
"metadata": {},
},
},
{
"level": "advanced",
"ok": False,
"error": "Provider timeout",
},
]
result = format_comparison("Hello", "French", "casual", results)
assert "Bonjour" in result
assert "Error: Provider timeout" in result
def test_comparison_with_transliteration(self):
results = [
{
"level": "beginner",
"ok": True,
"result": {
"translation": "你好",
"transliteration": "nǐ hǎo",
"metadata": {"processing_time_ms": 500},
},
},
]
result = format_comparison("Hello", "Mandarin Chinese", "casual", results)
assert "nǐ hǎo" in result
def test_empty_results(self):
result = format_comparison("Hello", "French", "casual", [])
assert 'Comparing translations of: "Hello"' in result
assert "French" in result
def test_mood_capitalized_in_header(self):
result = format_comparison("Hello", "French", "formal", [])
assert "Mood: Formal" in result
def test_processing_time_displayed(self):
results = [
{
"level": "beginner",
"ok": True,
"result": {
"translation": "Bonjour",
"transliteration": None,
"metadata": {"processing_time_ms": 1234},
},
},
]
result = format_comparison("Hello", "French", "casual", results)
assert "(1234ms)" in result
def test_mode_shown_in_header_when_spoken(self):
result = format_comparison("Hello", "French", "casual", [], mode="spoken")
assert "Mode: Spoken" in result
def test_mode_hidden_in_header_when_written(self):
result = format_comparison("Hello", "French", "casual", [], mode="written")
assert "Mode:" not in result
def test_mode_hidden_in_header_when_none(self):
result = format_comparison("Hello", "French", "casual", [], mode=None)
assert "Mode:" not in result
def test_no_translation_fallback(self):
results = [
{
"level": "beginner",
"ok": True,
"result": {
"transliteration": None,
"metadata": {},
},
},
]
result = format_comparison("Hello", "French", "casual", results)
assert "(no translation)" in result