test_insights_unicode_flags.py
1 """Tests for Unicode dash normalization in /insights command flag parsing. 2 3 Telegram on iOS auto-converts -- to em/en dashes. The /insights handler 4 normalizes these before parsing --days and --source flags. 5 """ 6 import re 7 import pytest 8 9 10 # The regex from gateway/run.py insights handler 11 _UNICODE_DASH_RE = re.compile(r'[\u2012\u2013\u2014\u2015](days|source)') 12 13 14 def _normalize_insights_args(raw: str) -> str: 15 """Apply the same normalization as the /insights handler.""" 16 return _UNICODE_DASH_RE.sub(r'--\1', raw) 17 18 19 class TestInsightsUnicodeDashFlags: 20 """--days and --source must survive iOS Unicode dash conversion.""" 21 22 @pytest.mark.parametrize("input_str,expected", [ 23 # Standard double hyphen (baseline) 24 ("--days 7", "--days 7"), 25 ("--source telegram", "--source telegram"), 26 # Em dash (U+2014) 27 ("\u2014days 7", "--days 7"), 28 ("\u2014source telegram", "--source telegram"), 29 # En dash (U+2013) 30 ("\u2013days 7", "--days 7"), 31 ("\u2013source telegram", "--source telegram"), 32 # Figure dash (U+2012) 33 ("\u2012days 7", "--days 7"), 34 # Horizontal bar (U+2015) 35 ("\u2015days 7", "--days 7"), 36 # Combined flags with em dashes 37 ("\u2014days 30 \u2014source cli", "--days 30 --source cli"), 38 ]) 39 def test_unicode_dash_normalized(self, input_str, expected): 40 result = _normalize_insights_args(input_str) 41 assert result == expected 42 43 def test_regular_hyphens_unaffected(self): 44 """Normal --days/--source must pass through unchanged.""" 45 assert _normalize_insights_args("--days 7 --source discord") == "--days 7 --source discord" 46 47 def test_bare_number_still_works(self): 48 """Shorthand /insights 7 (no flag) must not be mangled.""" 49 assert _normalize_insights_args("7") == "7" 50 51 def test_no_flags_unchanged(self): 52 """Input with no flags passes through as-is.""" 53 assert _normalize_insights_args("") == "" 54 assert _normalize_insights_args("30") == "30"