Coverage for tests/test_language_utils.py: 100%
60 statements
« prev ^ index » next coverage.py v7.15.4, created at 2026-08-09 04:47 +0000
« prev ^ index » next coverage.py v7.15.4, created at 2026-08-09 04:47 +0000
1"""
2Unit tests for language_utils.
3"""
5from language_utils import WHISPER_TO_KOKORO, KOKORO_TO_ISO, to_language
8def test_whisper_to_kokoro() -> None:
9 """WHISPER_TO_KOKORO maps Whisper ISO 639-1 codes to Kokoro single-letter codes."""
10 assert WHISPER_TO_KOKORO["en"] == "a"
11 assert WHISPER_TO_KOKORO["es"] == "e"
12 assert WHISPER_TO_KOKORO["fr"] == "f"
13 assert WHISPER_TO_KOKORO["de"] == "g"
14 assert WHISPER_TO_KOKORO["hi"] == "h"
15 assert WHISPER_TO_KOKORO["it"] == "i"
16 assert WHISPER_TO_KOKORO["ja"] == "j"
17 assert WHISPER_TO_KOKORO["ko"] == "k"
18 assert WHISPER_TO_KOKORO["pt"] == "p"
19 assert WHISPER_TO_KOKORO["ru"] == "r"
20 assert WHISPER_TO_KOKORO["zh"] == "z"
23def test_kokoro_to_iso() -> None:
24 """KOKORO_TO_ISO maps Kokoro single-letter codes to BCP 47 tags."""
25 assert KOKORO_TO_ISO["a"] == "en-US"
26 assert KOKORO_TO_ISO["b"] == "en-GB"
27 assert KOKORO_TO_ISO["e"] == "es"
28 assert KOKORO_TO_ISO["f"] == "fr"
29 assert KOKORO_TO_ISO["g"] == "de"
30 assert KOKORO_TO_ISO["h"] == "hi"
31 assert KOKORO_TO_ISO["i"] == "it"
32 assert KOKORO_TO_ISO["j"] == "ja"
33 assert KOKORO_TO_ISO["k"] == "ko"
34 assert KOKORO_TO_ISO["p"] == "pt-BR"
35 assert KOKORO_TO_ISO["r"] == "ru"
36 assert KOKORO_TO_ISO["z"] == "zh"
39def test_whisper_kokoro_roundtrip() -> None:
40 """Every ISO code in WHISPER_TO_KOKORO has a reverse entry in KOKORO_TO_ISO."""
41 for iso_code, kokoro_code in WHISPER_TO_KOKORO.items():
42 assert kokoro_code in KOKORO_TO_ISO, f"Kokoro code '{kokoro_code}' (from '{iso_code}') missing in KOKORO_TO_ISO"
45def test_kokoro_iso_roundtrip() -> None:
46 """Every Kokoro code in KOKORO_TO_ISO except 'b' (British English) is reachable from WHISPER_TO_KOKORO.
48 'b' (British English) is intentionally absent from WHISPER_TO_KOKORO because
49 Whisper uses 'en' for all English variants.
50 """
51 whisper_to_kokoro_values = set(WHISPER_TO_KOKORO.values())
52 kokoro_only_codes = {"b"} # British English: no Whisper equivalent
53 for kokoro_code in KOKORO_TO_ISO:
54 if kokoro_code in kokoro_only_codes:
55 continue
56 assert kokoro_code in whisper_to_kokoro_values, (
57 f"Kokoro code '{kokoro_code}' in KOKORO_TO_ISO has no entry in WHISPER_TO_KOKORO"
58 )
61def test_to_language_kokoro_codes() -> None:
62 """to_language converts Kokoro single-letter codes to human-readable names."""
63 assert to_language("a") == "English (United States)"
64 assert to_language("b") == "English (United Kingdom)"
65 assert to_language("e") == "Spanish"
66 assert to_language("f") == "French"
67 assert to_language("g") == "German"
68 assert to_language("h") == "Hindi"
69 assert to_language("i") == "Italian"
70 assert to_language("j") == "Japanese"
71 assert to_language("k") == "Korean"
72 assert to_language("p") == "Portuguese (Brazil)"
73 assert to_language("r") == "Russian"
74 assert to_language("z") == "Chinese"
77def test_to_language_iso_codes() -> None:
78 """to_language also accepts ISO 639-1 codes directly."""
79 assert to_language("en") == "English"
80 assert to_language("es") == "Spanish"
81 assert to_language("fr") == "French"
82 assert to_language("zh") == "Chinese"
85def test_to_language_case_insensitive() -> None:
86 """to_language is case-insensitive for Kokoro codes."""
87 assert to_language("E") == to_language("e")
88 assert to_language("F") == to_language("f")
89 assert to_language("A") == to_language("a")
92def test_to_language_unknown_code() -> None:
93 """to_language returns a descriptive string for unrecognised codes (via langcodes)."""
94 assert to_language("xyz") == "Unknown language [xyz]"