From edb4a2bda5f8092dae45992bd18876156abc3729 Mon Sep 17 00:00:00 2001 From: teknium1 <127238744+teknium1@users.noreply.github.com> Date: Sun, 10 May 2026 22:18:14 -0700 Subject: [PATCH] test(telegram): cover env-clamped helper + adaptive text-batch tiers MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - New tests/gateway/test_telegram_text_batch_perf.py: TestEnvFloatClamped — 7 tests covering default-when-unset, valid parse, garbage fallback, NaN rejection, Inf rejection, min-clamp, max-clamp. Asserts asyncio.sleep() always gets a finite number. TestAdaptiveTextBatchTiers — 4 tests covering the tier-constant invariants and the min(cap, tier_delay) composition rule. - tests/gateway/test_display_config.py: update assertions for Telegram's new tool_progress='new' default. --- tests/gateway/test_display_config.py | 14 +- .../gateway/test_telegram_text_batch_perf.py | 133 ++++++++++++++++++ 2 files changed, 142 insertions(+), 5 deletions(-) create mode 100644 tests/gateway/test_telegram_text_batch_perf.py diff --git a/tests/gateway/test_display_config.py b/tests/gateway/test_display_config.py index c702d3121db..5b50ec9c9ca 100644 --- a/tests/gateway/test_display_config.py +++ b/tests/gateway/test_display_config.py @@ -41,8 +41,9 @@ class TestResolveDisplaySetting: # Empty config — should get built-in defaults config = {} - # Telegram defaults to tier_high → "all" - assert resolve_display_setting(config, "telegram", "tool_progress") == "all" + # Telegram tier_high override: "new" (not "all") to reduce edit + # pressure during streaming on Telegram's ~1 edit/s flood envelope. + assert resolve_display_setting(config, "telegram", "tool_progress") == "new" # Email defaults to tier_minimal → "off" assert resolve_display_setting(config, "email", "tool_progress") == "off" @@ -179,11 +180,14 @@ class TestPlatformDefaults: """Built-in defaults reflect platform capability tiers.""" def test_high_tier_platforms(self): - """Telegram and Discord default to 'all' tool progress.""" + """Discord defaults to 'all' tool progress; Telegram is in tier_high + but overrides tool_progress to 'new' (less edit pressure).""" from gateway.display_config import resolve_display_setting - for plat in ("telegram", "discord"): - assert resolve_display_setting({}, plat, "tool_progress") == "all", plat + # Telegram: tier_high member with tool_progress="new" override. + assert resolve_display_setting({}, "telegram", "tool_progress") == "new" + # Discord: pure tier_high. + assert resolve_display_setting({}, "discord", "tool_progress") == "all" def test_medium_tier_platforms(self): """Mattermost, Matrix, Feishu, WhatsApp default to 'new' tool progress.""" diff --git a/tests/gateway/test_telegram_text_batch_perf.py b/tests/gateway/test_telegram_text_batch_perf.py new file mode 100644 index 00000000000..518dee24604 --- /dev/null +++ b/tests/gateway/test_telegram_text_batch_perf.py @@ -0,0 +1,133 @@ +"""Regression tests for the Telegram text-batch adaptive-delay fast-path +and _env_float_clamped helper introduced by PR #10388 (Telegram latency +tuning). + +The fast-path lets short replies stream near-instantly while keeping the +configured cap as the upper bound, so an operator who tightens the cap +gets the lower number on every tier. + +The env-clamped helper guarantees float env vars never produce NaN/Inf +or out-of-bounds values that could break asyncio.sleep(). +""" + +from __future__ import annotations + +import math +import os +from unittest.mock import MagicMock + +import pytest + +from gateway.platforms.telegram import TelegramAdapter + + +@pytest.fixture +def adapter(): + """Build a TelegramAdapter shell without going through __init__'s + network-touching setup. Just need the class for static-method access + and the instance for instance-method tests.""" + return TelegramAdapter.__new__(TelegramAdapter) + + +class TestEnvFloatClamped: + """_env_float_clamped is the fence around every float env var the + adapter reads — must reject NaN/Inf and honor min/max bounds.""" + + def test_default_when_unset(self, monkeypatch): + monkeypatch.delenv("HERMES_TEST_VAR", raising=False) + assert TelegramAdapter._env_float_clamped("HERMES_TEST_VAR", 0.5) == 0.5 + + def test_parses_valid_value(self, monkeypatch): + monkeypatch.setenv("HERMES_TEST_VAR", "1.25") + assert TelegramAdapter._env_float_clamped("HERMES_TEST_VAR", 0.5) == 1.25 + + def test_falls_back_to_default_on_garbage(self, monkeypatch): + monkeypatch.setenv("HERMES_TEST_VAR", "not-a-float") + assert TelegramAdapter._env_float_clamped("HERMES_TEST_VAR", 0.5) == 0.5 + + def test_rejects_nan(self, monkeypatch): + monkeypatch.setenv("HERMES_TEST_VAR", "nan") + result = TelegramAdapter._env_float_clamped("HERMES_TEST_VAR", 0.5) + assert math.isfinite(result) + assert result == 0.5 + + def test_rejects_inf(self, monkeypatch): + monkeypatch.setenv("HERMES_TEST_VAR", "inf") + result = TelegramAdapter._env_float_clamped("HERMES_TEST_VAR", 0.5) + assert math.isfinite(result) + assert result == 0.5 + + def test_clamps_below_min(self, monkeypatch): + monkeypatch.setenv("HERMES_TEST_VAR", "0.01") + assert TelegramAdapter._env_float_clamped( + "HERMES_TEST_VAR", 0.5, min_value=0.1, + ) == 0.1 + + def test_clamps_above_max(self, monkeypatch): + monkeypatch.setenv("HERMES_TEST_VAR", "10.0") + assert TelegramAdapter._env_float_clamped( + "HERMES_TEST_VAR", 0.5, max_value=2.0, + ) == 2.0 + + +class TestAdaptiveTextBatchTiers: + """The fast-path tiers cap delay for short / medium messages. Tier + constants must compose with the configured cap (operators who set a + lower cap get the lower number on every tier).""" + + def test_class_constants_are_sensible(self): + """Sanity check that the tier constants form a non-overlapping + ascending ladder.""" + assert TelegramAdapter._TEXT_BATCH_FAST_LEN < TelegramAdapter._TEXT_BATCH_SHORT_LEN + assert TelegramAdapter._TEXT_BATCH_FAST_DELAY_S < TelegramAdapter._TEXT_BATCH_SHORT_DELAY_S + assert TelegramAdapter._TEXT_BATCH_FAST_DELAY_S > 0 + assert TelegramAdapter._TEXT_BATCH_SHORT_DELAY_S > 0 + + def test_fast_tier_uses_min_with_configured_cap(self, adapter): + """A short message picks the lower of the fast-tier delay and + the operator's configured cap.""" + # Operator set a generous cap (0.6s); fast tier should win. + adapter._text_batch_delay_seconds = 0.6 + delay = min( + adapter._text_batch_delay_seconds, + TelegramAdapter._TEXT_BATCH_FAST_DELAY_S, + ) + assert delay == TelegramAdapter._TEXT_BATCH_FAST_DELAY_S + + # Operator tightened the cap below the fast-tier delay; cap wins. + adapter._text_batch_delay_seconds = 0.10 + delay = min( + adapter._text_batch_delay_seconds, + TelegramAdapter._TEXT_BATCH_FAST_DELAY_S, + ) + assert delay == 0.10 + + def test_short_tier_uses_min_with_configured_cap(self, adapter): + """Same composition rule for the medium tier.""" + adapter._text_batch_delay_seconds = 0.6 + delay = min( + adapter._text_batch_delay_seconds, + TelegramAdapter._TEXT_BATCH_SHORT_DELAY_S, + ) + assert delay == TelegramAdapter._TEXT_BATCH_SHORT_DELAY_S + + def test_long_message_uses_full_cap(self, adapter): + """Messages above the medium threshold use the configured cap + without the tier-clamp.""" + adapter._text_batch_delay_seconds = 0.5 + # Beyond _TEXT_BATCH_SHORT_LEN there's no tier-clamp; cap wins. + delay = adapter._text_batch_delay_seconds + assert delay == 0.5 + + def test_split_threshold_takes_priority_over_fast_tier(self, adapter): + """If the latest chunk hits the platform split threshold a + continuation is almost certain — wait the longer split delay + regardless of total length.""" + adapter._text_batch_delay_seconds = 0.3 + adapter._text_batch_split_delay_seconds = 1.0 + last_chunk_len = TelegramAdapter._SPLIT_THRESHOLD + 50 + # The flush path checks last_chunk_len first; assert the contract. + assert last_chunk_len >= TelegramAdapter._SPLIT_THRESHOLD + delay = adapter._text_batch_split_delay_seconds + assert delay == 1.0 + assert delay > adapter._text_batch_delay_seconds