Use Claude Code, Codex, VSCode, Pi, and OpenCode (and 6 other harnesses) for free (1.3B+ free tokens) from your terminal, app, IDE, or phone, and now from the browser with native browser sessions (multi-harness + multi-model) like OpenClaw (voice supported + ToS friendly)
Python
Added new tests
Alishahryar19826d71ff1ce
diff --git a/tests/test_api.py b/tests/test_api.pyindex be8ce4e7..07933933 100644--- a/tests/test_api.py+++ b/tests/test_api.py@@ -98,3 +98,51 @@ def test_error_fallbacks():)assert response.status_code == 529assert response.json()["error"]["type"] == "overloaded_error"++ # Reset side_effect for subsequent tests+ mock_provider.complete.side_effect = None+++def test_generic_exception_returns_500():+ """Non-ProviderError exceptions are caught and returned as HTTPException(500)."""+ mock_provider.complete.side_effect = RuntimeError("unexpected crash")+ response = client.post(+ "/v1/messages",+ json={"model": "test", "messages": [{"role": "user", "content": "Hi"}], "max_tokens": 10, "stream": False},+ )+ assert response.status_code == 500+ mock_provider.complete.side_effect = None+++def test_generic_exception_with_status_code():+ """Exception with status_code attribute uses that status."""+ exc = RuntimeError("bad gateway")+ exc.status_code = 502+ mock_provider.complete.side_effect = exc+ response = client.post(+ "/v1/messages",+ json={"model": "test", "messages": [{"role": "user", "content": "Hi"}], "max_tokens": 10, "stream": False},+ )+ assert response.status_code == 502+ mock_provider.complete.side_effect = None+++def test_count_tokens_endpoint():+ """count_tokens endpoint returns token count."""+ response = client.post(+ "/v1/messages/count_tokens",+ json={"model": "test", "messages": [{"role": "user", "content": "Hello"}]},+ )+ assert response.status_code == 200+ assert "input_tokens" in response.json()+++def test_stop_endpoint_no_handler_no_cli_503():+ """POST /stop without handler or cli_manager returns 503."""+ # Ensure no handler or cli_manager on app state+ if hasattr(app.state, "message_handler"):+ delattr(app.state, "message_handler")+ if hasattr(app.state, "cli_manager"):+ delattr(app.state, "cli_manager")+ response = client.post("/stop")+ assert response.status_code == 503diff --git a/tests/test_config.py b/tests/test_config.pyindex 64da24b7..efa123db 100644--- a/tests/test_config.py+++ b/tests/test_config.py@@ -1,4 +1,9 @@-"""Tests for config/settings.py"""+"""Tests for config/settings.py and config/nim.py"""++import pytest+from pydantic import ValidationError++from config.nim import NimSettingsclass TestSettings:@@ -51,3 +56,161 @@ class TestSettings:from config.settings import NVIDIA_NIM_BASE_URLassert NVIDIA_NIM_BASE_URL == "https://integrate.api.nvidia.com/v1"+++# --- NimSettings Validation Tests ---+++class TestNimSettingsValidBounds:+ """Test that valid values within bounds are accepted."""++ @pytest.mark.parametrize("top_k", [-1, 0, 1, 100])+ def test_top_k_valid(self, top_k):+ """top_k >= -1 should be accepted."""+ s = NimSettings(top_k=top_k)+ assert s.top_k == top_k++ @pytest.mark.parametrize("temp", [0.0, 0.5, 1.0, 2.0])+ def test_temperature_valid(self, temp):+ s = NimSettings(temperature=temp)+ assert s.temperature == temp++ @pytest.mark.parametrize("top_p", [0.0, 0.5, 1.0])+ def test_top_p_valid(self, top_p):+ s = NimSettings(top_p=top_p)+ assert s.top_p == top_p++ @pytest.mark.parametrize("effort", ["low", "medium", "high"])+ def test_reasoning_effort_valid(self, effort):+ s = NimSettings(reasoning_effort=effort)+ assert s.reasoning_effort == effort++ def test_max_tokens_valid(self):+ s = NimSettings(max_tokens=1)+ assert s.max_tokens == 1++ def test_min_tokens_valid(self):+ s = NimSettings(min_tokens=0)+ assert s.min_tokens == 0++ @pytest.mark.parametrize("penalty", [-2.0, 0.0, 2.0])+ def test_presence_penalty_valid(self, penalty):+ s = NimSettings(presence_penalty=penalty)+ assert s.presence_penalty == penalty++ @pytest.mark.parametrize("penalty", [-2.0, 0.0, 2.0])+ def test_frequency_penalty_valid(self, penalty):+ s = NimSettings(frequency_penalty=penalty)+ assert s.frequency_penalty == penalty++ @pytest.mark.parametrize("min_p", [0.0, 0.5, 1.0])+ def test_min_p_valid(self, min_p):+ s = NimSettings(min_p=min_p)+ assert s.min_p == min_p+++class TestNimSettingsInvalidBounds:+ """Test that out-of-range values raise ValidationError."""++ @pytest.mark.parametrize("top_k", [-2, -100])+ def test_top_k_below_lower_bound(self, top_k):+ with pytest.raises((ValidationError, ValueError)):+ NimSettings(top_k=top_k)++ def test_temperature_negative(self):+ with pytest.raises(ValidationError):+ NimSettings(temperature=-0.1)++ @pytest.mark.parametrize("top_p", [-0.1, 1.1])+ def test_top_p_out_of_range(self, top_p):+ with pytest.raises(ValidationError):+ NimSettings(top_p=top_p)++ @pytest.mark.parametrize("penalty", [-2.1, 2.1])+ def test_presence_penalty_out_of_range(self, penalty):+ with pytest.raises(ValidationError):+ NimSettings(presence_penalty=penalty)++ @pytest.mark.parametrize("penalty", [-2.1, 2.1])+ def test_frequency_penalty_out_of_range(self, penalty):+ with pytest.raises(ValidationError):+ NimSettings(frequency_penalty=penalty)++ @pytest.mark.parametrize("min_p", [-0.1, 1.1])+ def test_min_p_out_of_range(self, min_p):+ with pytest.raises(ValidationError):+ NimSettings(min_p=min_p)++ @pytest.mark.parametrize("max_tokens", [0, -1])+ def test_max_tokens_too_low(self, max_tokens):+ with pytest.raises(ValidationError):+ NimSettings(max_tokens=max_tokens)++ def test_min_tokens_negative(self):+ with pytest.raises(ValidationError):+ NimSettings(min_tokens=-1)++ def test_reasoning_effort_invalid(self):+ with pytest.raises(ValidationError):+ NimSettings(reasoning_effort="invalid")+++class TestNimSettingsValidators:+ """Test custom field validators in NimSettings."""++ @pytest.mark.parametrize(+ "seed_val,expected",+ [("", None), (None, None), ("42", 42), (42, 42)],+ ids=["empty_str", "none", "str_42", "int_42"],+ )+ def test_parse_optional_int(self, seed_val, expected):+ s = NimSettings(seed=seed_val)+ assert s.seed == expected++ @pytest.mark.parametrize(+ "stop_val,expected",+ [("", None), ("STOP", "STOP"), (None, None)],+ ids=["empty_str", "valid", "none"],+ )+ def test_parse_optional_str_stop(self, stop_val, expected):+ s = NimSettings(stop=stop_val)+ assert s.stop == expected++ @pytest.mark.parametrize(+ "chat_template_val,expected",+ [("", None), ("template", "template")],+ ids=["empty_str", "valid"],+ )+ def test_parse_optional_str_chat_template(self, chat_template_val, expected):+ s = NimSettings(chat_template=chat_template_val)+ assert s.chat_template == expected++ def test_extra_forbid_rejects_unknown_field(self):+ """NimSettings with extra='forbid' rejects unknown fields."""+ with pytest.raises(ValidationError):+ NimSettings(unknown_field="value")+++class TestSettingsOptionalStr:+ """Test Settings parse_optional_str validator."""++ def test_empty_telegram_token_to_none(self, monkeypatch):+ from config.settings import Settings++ monkeypatch.setenv("TELEGRAM_BOT_TOKEN", "")+ s = Settings()+ assert s.telegram_bot_token is None++ def test_valid_telegram_token_preserved(self, monkeypatch):+ from config.settings import Settings++ monkeypatch.setenv("TELEGRAM_BOT_TOKEN", "abc123")+ s = Settings()+ assert s.telegram_bot_token == "abc123"++ def test_empty_allowed_user_id_to_none(self, monkeypatch):+ from config.settings import Settings++ monkeypatch.setenv("ALLOWED_TELEGRAM_USER_ID", "")+ s = Settings()+ assert s.allowed_telegram_user_id is Nonediff --git a/tests/test_converter.py b/tests/test_converter.pyindex 11fa190b..e3475a3f 100644--- a/tests/test_converter.py+++ b/tests/test_converter.py@@ -1,4 +1,5 @@import json+import pytestfrom providers.nvidia_nim.utils.message_converter import AnthropicToOpenAIConverter# --- Mock Classes ---@@ -279,3 +280,78 @@ def test_input_not_dict():# The converter calls json.dumps(tool_input) if dict, else str(tool_input)# So it should be "some_string"assert result[0]["tool_calls"][0]["function"]["arguments"] == "some_string"+++# --- Parametrized Edge Case Tests ---+++ "system_input,expected",+ [+ ("You are helpful.", {"role": "system", "content": "You are helpful."}),+ (+ [MockBlock(type="text", text="A"), MockBlock(type="text", text="B")],+ {"role": "system", "content": "A\n\nB"},+ ),+ (None, None),+ ("", {"role": "system", "content": ""}),+ ([], None),+ ],+ ids=["string", "list_text", "none", "empty_string", "empty_list"],+)+def test_convert_system_prompt_parametrized(system_input, expected):+ """Parametrized system prompt conversion covering edge cases."""+ result = AnthropicToOpenAIConverter.convert_system_prompt(system_input)+ assert result == expected+++ "content,expected_content",+ [+ ("Hello world", "Hello world"),+ ("", ""),+ ([MockBlock(type="text", text="A"), MockBlock(type="text", text="B")], "A\nB"),+ ([MockBlock(type="text", text="")], ""),+ ],+ ids=["simple_string", "empty_string", "list_blocks", "empty_text_block"],+)+def test_convert_user_message_parametrized(content, expected_content):+ """Parametrized user message conversion."""+ messages = [MockMessage("user", content)]+ result = AnthropicToOpenAIConverter.convert_messages(messages)+ assert len(result) >= 1+ assert result[0]["content"] == expected_content+++def test_convert_assistant_message_unknown_block_type():+ """Unknown block types should be silently skipped."""+ content = [+ MockBlock(type="unknown_type", data="something"),+ MockBlock(type="text", text="visible"),+ ]+ messages = [MockMessage("assistant", content)]+ result = AnthropicToOpenAIConverter.convert_messages(messages)+ assert len(result) == 1+ assert "visible" in result[0]["content"]+++def test_convert_tool_use_none_input():+ """Tool use with None input should not crash."""+ content = [MockBlock(type="tool_use", id="call_n", name="test", input=None)]+ messages = [MockMessage("assistant", content)]+ result = AnthropicToOpenAIConverter.convert_messages(messages)+ assert len(result) == 1+ assert "tool_calls" in result[0]+++def test_convert_multiple_tool_results():+ """Multiple tool results in a single user message."""+ content = [+ MockBlock(type="tool_result", tool_use_id="t1", content="Result 1"),+ MockBlock(type="tool_result", tool_use_id="t2", content="Result 2"),+ ]+ messages = [MockMessage("user", content)]+ result = AnthropicToOpenAIConverter.convert_messages(messages)+ assert len(result) == 2+ assert result[0]["tool_call_id"] == "t1"+ assert result[1]["tool_call_id"] == "t2"diff --git a/tests/test_dependencies.py b/tests/test_dependencies.pyindex 32b0090d..ed4fbc84 100644--- a/tests/test_dependencies.py+++ b/tests/test_dependencies.py@@ -81,3 +81,18 @@ async def test_get_provider_unknown_type():with pytest.raises(ValueError, match="Unknown provider_type"):get_provider()+++async def test_cleanup_provider_aclose_raises():+ """cleanup_provider handles aclose() raising an exception."""+ with patch("api.dependencies.get_settings") as mock_settings:+ mock_settings.return_value = _make_mock_settings()++ provider = get_provider()+ provider._client = AsyncMock()+ provider._client.aclose = AsyncMock(side_effect=RuntimeError("cleanup failed"))++ # Should propagate the error (current behavior - no try/except)+ with pytest.raises(RuntimeError, match="cleanup failed"):+ await cleanup_provider()diff --git a/tests/test_error_mapping.py b/tests/test_error_mapping.pynew file mode 100644index 00000000..18ee8771--- /dev/null+++ b/tests/test_error_mapping.py@@ -0,0 +1,118 @@+"""Tests for providers/nvidia_nim/errors.py error mapping."""++import pytest+from unittest.mock import patch, MagicMock++import openai+from httpx import Response, Request++from providers.nvidia_nim.errors import map_error+from providers.exceptions import (+ AuthenticationError,+ InvalidRequestError,+ RateLimitError,+ OverloadedError,+ APIError,+)+++def _make_openai_error(cls, message="test error", status_code=None):+ """Helper to create openai exceptions with required httpx objects."""+ response = Response(status_code=status_code or 500, request=Request("POST", "http://test"))+ body = {"error": {"message": message}}+ # openai.APIError base class has a different constructor signature+ if cls is openai.APIError:+ return cls(message, request=Request("POST", "http://test"), body=body)+ return cls(message, response=response, body=body)+++class TestMapError:+ """Tests for map_error function."""++ def test_authentication_error(self):+ """openai.AuthenticationError -> AuthenticationError."""+ exc = _make_openai_error(openai.AuthenticationError, status_code=401)+ result = map_error(exc)+ assert isinstance(result, AuthenticationError)+ assert result.status_code == 401++ def test_rate_limit_error(self):+ """openai.RateLimitError -> RateLimitError and triggers global block."""+ exc = _make_openai_error(openai.RateLimitError, status_code=429)+ with patch("providers.nvidia_nim.errors.GlobalRateLimiter") as mock_rl:+ mock_instance = MagicMock()+ mock_rl.get_instance.return_value = mock_instance+ result = map_error(exc)+ assert isinstance(result, RateLimitError)+ assert result.status_code == 429+ mock_instance.set_blocked.assert_called_once_with(60)++ def test_bad_request_error(self):+ """openai.BadRequestError -> InvalidRequestError."""+ exc = _make_openai_error(openai.BadRequestError, status_code=400)+ result = map_error(exc)+ assert isinstance(result, InvalidRequestError)+ assert result.status_code == 400++ @pytest.mark.parametrize(+ "message",+ ["Server overloaded", "No capacity available"],+ ids=["overloaded", "capacity"],+ )+ def test_internal_server_error_overloaded(self, message):+ """InternalServerError with overloaded/capacity keywords -> OverloadedError."""+ exc = _make_openai_error(+ openai.InternalServerError, message=message, status_code=500+ )+ result = map_error(exc)+ assert isinstance(result, OverloadedError)+ assert result.status_code == 529++ def test_internal_server_error_generic(self):+ """InternalServerError without keywords -> APIError(500)."""+ exc = _make_openai_error(+ openai.InternalServerError, message="Unknown error", status_code=500+ )+ result = map_error(exc)+ assert isinstance(result, APIError)+ assert result.status_code == 500++ def test_generic_api_error(self):+ """openai.APIError -> APIError with original status_code."""+ exc = _make_openai_error(openai.APIError, message="Bad gateway", status_code=502)+ result = map_error(exc)+ assert isinstance(result, APIError)++ def test_unmapped_exception_passthrough(self):+ """Non-openai exceptions are returned as-is."""+ exc = RuntimeError("unexpected")+ result = map_error(exc)+ assert result is exc+ assert isinstance(result, RuntimeError)++ def test_value_error_passthrough(self):+ """ValueError passes through unchanged."""+ exc = ValueError("bad value")+ result = map_error(exc)+ assert result is exc++ @pytest.mark.parametrize(+ "exc_cls,expected_cls",+ [+ (openai.AuthenticationError, AuthenticationError),+ (openai.RateLimitError, RateLimitError),+ (openai.BadRequestError, InvalidRequestError),+ ],+ ids=["auth", "rate_limit", "bad_request"],+ )+ def test_mapping_parametrized(self, exc_cls, expected_cls):+ """Parametrized check of openai -> provider error mapping."""+ status_map = {+ openai.AuthenticationError: 401,+ openai.RateLimitError: 429,+ openai.BadRequestError: 400,+ }+ exc = _make_openai_error(exc_cls, status_code=status_map[exc_cls])+ with patch("providers.nvidia_nim.errors.GlobalRateLimiter"):+ result = map_error(exc)+ assert isinstance(result, expected_cls)diff --git a/tests/test_extract_text.py b/tests/test_extract_text.pyindex 40092b3c..80781690 100644--- a/tests/test_extract_text.py+++ b/tests/test_extract_text.py@@ -1,5 +1,6 @@"""Tests for extract_text_from_content helper functions."""+import pytestfrom unittest.mock import MagicMockfrom api.request_utils import extract_text_from_content@@ -90,3 +91,49 @@ class TestLoggingExtractText:assert logging_extract("") == ""assert logging_extract([]) == ""assert logging_extract(None) == ""+++# --- Parametrized Edge Case Tests ---+++def _make_block(text_val):+ b = MagicMock()+ b.text = text_val+ return b+++ "content,expected",+ [+ ("hello world", "hello world"),+ ("", ""),+ (None, ""),+ (42, ""),+ ([], ""),+ (" ", " "),+ ],+ ids=["string", "empty_str", "none", "int", "empty_list", "whitespace_only"],+)+def test_extract_text_scalar_and_empty_parametrized(content, expected):+ """Parametrized scalar and empty input handling."""+ assert extract_text_from_content(content) == expected+++ "func",+ [extract_text_from_content, logging_extract],+ ids=["request_utils", "logging_utils"],+)+def test_both_extract_functions_whitespace_only(func):+ """Both extract functions handle whitespace-only string identically."""+ assert func(" ") == " "+++ "func",+ [extract_text_from_content, logging_extract],+ ids=["request_utils", "logging_utils"],+)+def test_both_extract_functions_unicode(func):+ """Both extract functions handle unicode content."""+ assert func("日本語テスト") == "日本語テスト"diff --git a/tests/test_limiter.py b/tests/test_limiter.pyindex 4746a0ff..d3fc49b3 100644--- a/tests/test_limiter.py+++ b/tests/test_limiter.py@@ -160,3 +160,58 @@ class TestMessagingRateLimiter:f"Should have waited for FloodWait, but took {duration:.2f}s")assert call_count == 2++ @pytest.mark.asyncio+ async def test_flood_wait_retry_after_parsing(self):+ """Error message with 'retry after N' parses the wait seconds."""+ MessagingRateLimiter._instance = None+ limiter = await MessagingRateLimiter.get_instance()++ async def mock_flood():+ raise Exception("Flood wait: retry after 2 seconds")++ try:+ await limiter.enqueue(mock_flood, dedup_key="retry_parse")+ except Exception:+ pass++ # Should have parsed "after 2" -> 2 seconds+ assert limiter._paused_until > 0++ @pytest.mark.asyncio+ async def test_non_flood_exception_no_pause(self):+ """Non-flood exception doesn't trigger pause."""+ MessagingRateLimiter._instance = None+ limiter = await MessagingRateLimiter.get_instance()++ async def mock_error():+ raise ValueError("some regular error")++ try:+ await limiter.enqueue(mock_error, dedup_key="non_flood")+ except ValueError:+ pass++ # Should NOT have paused since it's not a flood error+ assert limiter._paused_until == 0++ @pytest.mark.asyncio+ async def test_flood_with_seconds_attribute(self):+ """Exception with .seconds attribute uses that value for pause."""+ MessagingRateLimiter._instance = None+ limiter = await MessagingRateLimiter.get_instance()++ class FloodWaitCustom(Exception):+ def __init__(self):+ self.seconds = 2+ super().__init__("Flood wait custom")++ async def mock_flood():+ raise FloodWaitCustom()++ try:+ await limiter.enqueue(mock_flood, dedup_key="flood_sec")+ except Exception:+ pass++ assert limiter._paused_until > 0diff --git a/tests/test_model_utils.py b/tests/test_model_utils.pyindex ba20ee48..46cc40cf 100644--- a/tests/test_model_utils.py+++ b/tests/test_model_utils.py@@ -1,3 +1,5 @@+import pytest+from providers.model_utils import (strip_provider_prefixes,is_claude_model,@@ -44,3 +46,82 @@ def test_get_original_model():def test_normalize_model_name_without_default(monkeypatch):monkeypatch.setenv("MODEL", "env-default-model")assert normalize_model_name("claude-3") == "env-default-model"+++# --- Parametrized Edge Case Tests ---+++ "model,expected",+ [+ ("anthropic/claude-3", "claude-3"),+ ("openai/gpt-4", "gpt-4"),+ ("gemini/gemini-pro", "gemini-pro"),+ ("no-prefix", "no-prefix"),+ ("", ""),+ ("anthropic/", ""),+ ("anthropic/openai/nested", "openai/nested"),+ ],+ ids=[+ "anthropic",+ "openai",+ "gemini",+ "no_prefix",+ "empty_string",+ "prefix_only",+ "nested_prefix",+ ],+)+def test_strip_provider_prefixes_parametrized(model, expected):+ """Parametrized prefix stripping with edge cases."""+ assert strip_provider_prefixes(model) == expected+++ "model,expected",+ [+ ("claude-3-sonnet", True),+ ("claude-3-opus", True),+ ("claude-3-haiku", True),+ ("claude-2.1", True),+ ("gpt-4", False),+ ("gemini-pro", False),+ ("", False),+ ("my-claude-wrapper", True), # "claude" as substring+ ("CLAUDE-3-SONNET", True), # case insensitive+ ("sonnet-v2", True), # "sonnet" identifier without "claude"+ ("haiku-model", True), # "haiku" identifier+ ],+ ids=[+ "sonnet",+ "opus",+ "haiku",+ "claude2",+ "gpt4",+ "gemini",+ "empty",+ "claude_substring",+ "uppercase",+ "sonnet_standalone",+ "haiku_standalone",+ ],+)+def test_is_claude_model_parametrized(model, expected):+ """Parametrized Claude model detection with edge cases."""+ assert is_claude_model(model) is expected+++ "model,default,expected",+ [+ ("claude-3-sonnet", "target", "target"),+ ("anthropic/claude-3-opus", "target", "target"),+ ("gpt-4", "target", "gpt-4"),+ ("openai/gpt-3.5-turbo", "target", "openai/gpt-3.5-turbo"),+ ("", "target", ""), # empty string is not a claude model+ ],+ ids=["claude_mapped", "prefixed_claude", "non_claude", "prefixed_non_claude", "empty"],+)+def test_normalize_model_name_parametrized(model, default, expected):+ """Parametrized model normalization."""+ assert normalize_model_name(model, default) == expecteddiff --git a/tests/test_parsers.py b/tests/test_parsers.pyindex c94475bd..72be5df8 100644--- a/tests/test_parsers.py+++ b/tests/test_parsers.py@@ -1,3 +1,5 @@+import pytest+from providers.nvidia_nim.utils.think_parser import ThinkTagParser, ContentTypefrom providers.nvidia_nim.utils.heuristic_tool_parser import HeuristicToolParser@@ -302,3 +304,136 @@ def test_orphan_close_with_valid_think_pair():think_content = "".join(c.content for c in chunks if c.type == ContentType.THINKING)assert text_content == "abc"assert think_content == "thinking"+++# --- Parametrized Edge Case Tests ---+++ "input_text,expected_text",+ [+ ("Hello </think> world", "Hello world"),+ ("</think>Hello world", "Hello world"),+ ("Hello world</think>", "Hello world"),+ ("a</think>b</think>c", "abc"),+ ("</think>", ""),+ ("</think></think>", ""),+ ],+ ids=[+ "middle",+ "start",+ "end",+ "multiple",+ "only_orphan",+ "consecutive_orphans",+ ],+)+def test_orphan_close_tag_parametrized(input_text, expected_text):+ """Parametrized: orphan </think> tags should be stripped from various positions."""+ parser = ThinkTagParser()+ chunks = list(parser.feed(input_text))+ text = "".join(c.content for c in chunks if c.type == ContentType.TEXT)+ assert text == expected_text+ assert "</think>" not in text+++def test_think_tag_parser_empty_input():+ """Empty string input should yield no chunks."""+ parser = ThinkTagParser()+ chunks = list(parser.feed(""))+ assert chunks == []+++def test_think_tag_parser_flush_no_content():+ """Flush with no buffered content should return None."""+ parser = ThinkTagParser()+ result = parser.flush()+ assert result is None+++def test_think_tag_parser_flush_buffered_text():+ """Flush with buffered text returns TEXT chunk."""+ parser = ThinkTagParser()+ # Feed partial tag that stays buffered+ list(parser.feed("Hello <thi"))+ result = parser.flush()+ assert result is not None+ assert result.type == ContentType.TEXT+ assert "<thi" in result.content+++def test_think_tag_parser_flush_inside_think():+ """Flush while inside <think> with buffered partial close tag returns THINKING chunk."""+ parser = ThinkTagParser()+ # Feed content that ends with a potential partial </think> tag, which stays buffered+ chunks = list(parser.feed("<think>partial reasoning</thi"))+ # "partial reasoning" is emitted, "</thi" stays buffered as potential close tag+ assert any(c.type == ContentType.THINKING for c in chunks)+ result = parser.flush()+ assert result is not None+ assert result.type == ContentType.THINKING+ assert "</thi" in result.content+++def test_think_tag_parser_empty_think_tags():+ """Empty <think></think> pair should yield no thinking content."""+ parser = ThinkTagParser()+ chunks = list(parser.feed("<think></think>remaining"))+ # Empty think yields nothing for thinking, just the remaining text+ types = [c.type for c in chunks]+ text = "".join(c.content for c in chunks if c.type == ContentType.TEXT)+ assert text == "remaining"+++def test_think_tag_parser_unicode():+ """Unicode content inside and outside think tags."""+ parser = ThinkTagParser()+ chunks = list(parser.feed("日本語 <think>思考中 🤔</think> 結果"))+ thinking = "".join(c.content for c in chunks if c.type == ContentType.THINKING)+ text = "".join(c.content for c in chunks if c.type == ContentType.TEXT)+ assert thinking == "思考中 🤔"+ assert "日本語" in text+ assert "結果" in text+++def test_heuristic_tool_parser_empty_input():+ """Empty string input should return empty filtered text and no tools."""+ parser = HeuristicToolParser()+ filtered, tools = parser.feed("")+ assert filtered == ""+ assert tools == []+++def test_heuristic_tool_parser_flush_no_tool():+ """Flush when no tool is being parsed should return empty list."""+ parser = HeuristicToolParser()+ parser.feed("plain text")+ tools = parser.flush()+ assert tools == []+++def test_heuristic_tool_parser_unicode_function_name():+ """Unicode characters in function parameters."""+ parser = HeuristicToolParser()+ text = "● <function=Search><parameter=query>日本語テスト</parameter>"+ filtered, tools = parser.feed(text)+ tools.extend(parser.flush())+ assert len(tools) == 1+ assert tools[0]["name"] == "Search"+ assert tools[0]["input"]["query"] == "日本語テスト"+++ "malformed_text",+ [+ "● <function=>",+ "● <function=><parameter=x>v</parameter>",+ ],+ ids=["empty_name", "empty_name_with_param"],+)+def test_heuristic_tool_parser_malformed_function_tag(malformed_text):+ """Malformed function tags should still be handled without crashing."""+ parser = HeuristicToolParser()+ filtered, tools = parser.feed(malformed_text)+ tools.extend(parser.flush())+ # Should not crash; may or may not detect a tool depending on regex matchdiff --git a/tests/test_provider_rate_limit.py b/tests/test_provider_rate_limit.pyindex a786013b..143e300e 100644--- a/tests/test_provider_rate_limit.py+++ b/tests/test_provider_rate_limit.py@@ -85,3 +85,75 @@ class TestProviderRateLimiter:assert total_time >= block_time - 0.1, (f"Reactive block failed, took {total_time:.2f}s")++ @pytest.mark.asyncio+ async def test_set_blocked_zero_immediately_unblocks(self):+ """set_blocked(0) should not actually block."""+ limiter = GlobalRateLimiter.get_instance(rate_limit=100, rate_window=60)+ limiter.set_blocked(0)++ # Should not be blocked since 0 seconds from now is already past+ await asyncio.sleep(0.01)+ assert limiter.is_blocked() is False+ assert limiter.remaining_wait() == 0++ @pytest.mark.asyncio+ async def test_remaining_wait_when_not_blocked(self):+ """remaining_wait() should return 0 when not blocked."""+ limiter = GlobalRateLimiter.get_instance(rate_limit=100, rate_window=60)+ assert limiter.remaining_wait() == 0++ @pytest.mark.asyncio+ async def test_remaining_wait_decreases(self):+ """remaining_wait() should decrease over time."""+ limiter = GlobalRateLimiter.get_instance(rate_limit=100, rate_window=60)+ limiter.set_blocked(2.0)++ wait1 = limiter.remaining_wait()+ assert wait1 > 1.5++ await asyncio.sleep(0.5)+ wait2 = limiter.remaining_wait()+ assert wait2 < wait1++ @pytest.mark.asyncio+ async def test_is_blocked_false_initially(self):+ """is_blocked() should be False for a fresh limiter."""+ limiter = GlobalRateLimiter.get_instance(rate_limit=100, rate_window=60)+ assert limiter.is_blocked() is False++ @pytest.mark.asyncio+ async def test_high_rate_limit_no_throttling(self):+ """Very high rate limit should not cause throttling."""+ GlobalRateLimiter.reset_instance()+ limiter = GlobalRateLimiter.get_instance(rate_limit=10000, rate_window=60)++ start = time.time()+ for _ in range(20):+ await limiter.wait_if_blocked()+ duration = time.time() - start++ # 20 requests with 10000 limit should be near-instant+ assert duration < 1.0, f"High rate limit caused throttling: {duration:.2f}s"++ @pytest.mark.asyncio+ async def test_singleton_pattern(self):+ """get_instance should return the same object."""+ limiter1 = GlobalRateLimiter.get_instance(rate_limit=10, rate_window=1)+ limiter2 = GlobalRateLimiter.get_instance()+ assert limiter1 is limiter2++ @pytest.mark.asyncio+ async def test_reset_instance(self):+ """reset_instance should allow creating a new instance."""+ limiter1 = GlobalRateLimiter.get_instance(rate_limit=10, rate_window=1)+ GlobalRateLimiter.reset_instance()+ limiter2 = GlobalRateLimiter.get_instance(rate_limit=20, rate_window=2)+ assert limiter1 is not limiter2++ @pytest.mark.asyncio+ async def test_wait_if_blocked_returns_false_when_not_blocked(self):+ """wait_if_blocked should return False when not reactively blocked."""+ limiter = GlobalRateLimiter.get_instance(rate_limit=100, rate_window=60)+ result = await limiter.wait_if_blocked()+ assert result is Falsediff --git a/tests/test_request_utils.py b/tests/test_request_utils.pyindex 72e33d9e..80938146 100644--- a/tests/test_request_utils.py+++ b/tests/test_request_utils.py@@ -1,5 +1,6 @@"""Tests for api/request_utils.py module."""+import pytestfrom unittest.mock import MagicMockfrom api.request_utils import (@@ -451,3 +452,93 @@ class TestGetTokenCount:# Double message should have more tokens (including overhead)assert count_double > count_single+++# --- Parametrized Edge Case Tests ---+++ "command,expected",+ [+ ("git status", "git status"),+ ("ls -la", "ls"),+ ("git commit -m 'msg'", "git commit"),+ ("npm install pkg", "npm install"),+ ("ls", "ls"),+ ("python", "python"),+ ("", "none"),+ (" ", "none"),+ ("`whoami`", "command_injection_detected"),+ ("$(whoami)", "command_injection_detected"),+ ("echo $(cat /etc/passwd)", "command_injection_detected"),+ ("git -v", "git"),+ ("DEBUG=1 python script.py", "DEBUG=1 python"),+ ("cargo build", "cargo build"),+ ("cargo --version", "cargo"),+ ],+ ids=[+ "git_status",+ "ls_with_flag",+ "git_commit",+ "npm_install",+ "bare_ls",+ "bare_python",+ "empty",+ "whitespace",+ "injection_backtick",+ "injection_dollar",+ "injection_echo",+ "git_flag",+ "env_var",+ "cargo_build",+ "cargo_flag",+ ],+)+def test_extract_command_prefix_parametrized(command, expected):+ """Parametrized command prefix extraction."""+ assert extract_command_prefix(command) == expected+++def test_extract_command_prefix_unterminated_quote():+ """Unterminated quote falls back to simple split (shlex.split ValueError)."""+ result = extract_command_prefix("git commit -m 'unterminated")+ # Should fall back to command.split()[0] = "git"+ assert result == "git"+++def test_extract_command_prefix_pipe():+ """Piped commands - shlex handles pipe character."""+ result = extract_command_prefix("cat file.txt | grep pattern")+ assert result in ("cat", "cat file.txt")+++ "content,max_tokens,role,expected",+ [+ ("Check my quota", 1, "user", True),+ ("Check my QUOTA", 1, "user", True),+ ("Hello world", 1, "user", False),+ ("Check my quota", 100, "user", False),+ ("Check my quota", 1, "assistant", False),+ ],+ ids=["basic", "case_insensitive", "no_keyword", "wrong_max_tokens", "wrong_role"],+)+def test_quota_check_parametrized(content, max_tokens, role, expected):+ """Parametrized quota check request detection."""+ msg = MagicMock(spec=Message)+ msg.role = role+ msg.content = content++ req = MagicMock(spec=MessagesRequest)+ req.max_tokens = max_tokens+ req.messages = [msg]++ assert is_quota_check_request(req) is expected+++def test_quota_check_empty_messages():+ """Quota check with empty message list should not crash."""+ req = MagicMock(spec=MessagesRequest)+ req.max_tokens = 1+ req.messages = []+ assert is_quota_check_request(req) is Falsediff --git a/tests/test_response_conversion.py b/tests/test_response_conversion.pynew file mode 100644index 00000000..5478f007--- /dev/null+++ b/tests/test_response_conversion.py@@ -0,0 +1,200 @@+"""Tests for providers/nvidia_nim/response.py response conversion."""++import pytest+from unittest.mock import MagicMock++from providers.nvidia_nim.response import convert_response+++def _make_response(+ content="Hello",+ finish_reason="stop",+ tool_calls=None,+ reasoning_content=None,+ reasoning_details=None,+ usage=None,+):+ """Helper to build a minimal OpenAI-format response dict."""+ message = {"content": content, "role": "assistant"}+ if tool_calls is not None:+ message["tool_calls"] = tool_calls+ if reasoning_content is not None:+ message["reasoning_content"] = reasoning_content+ if reasoning_details is not None:+ message["reasoning_details"] = reasoning_details++ return {+ "id": "chatcmpl-test",+ "choices": [{"message": message, "finish_reason": finish_reason}],+ "usage": usage or {"prompt_tokens": 10, "completion_tokens": 5},+ }+++def _make_request(model="test-model"):+ req = MagicMock()+ req.model = model+ return req+++class TestConvertResponse:+ """Tests for convert_response function."""++ def test_simple_text_response(self):+ """Simple text content is preserved."""+ resp = _make_response(content="Hello world")+ result = convert_response(resp, _make_request())+ assert result["content"] == [{"type": "text", "text": "Hello world"}]+ assert result["stop_reason"] == "end_turn"++ def test_empty_content_gets_space(self):+ """Empty content produces a single space text block."""+ resp = _make_response(content="")+ result = convert_response(resp, _make_request())+ assert result["content"] == [{"type": "text", "text": " "}]++ def test_none_content_gets_space(self):+ """None content produces a single space text block."""+ resp = _make_response(content=None)+ result = convert_response(resp, _make_request())+ assert result["content"] == [{"type": "text", "text": " "}]++ def test_reasoning_content_field(self):+ """reasoning_content field is extracted as thinking block."""+ resp = _make_response(+ content="Answer", reasoning_content="I need to think..."+ )+ result = convert_response(resp, _make_request())+ types = [b["type"] for b in result["content"]]+ assert "thinking" in types+ assert "text" in types+ thinking = [b for b in result["content"] if b["type"] == "thinking"]+ assert thinking[0]["thinking"] == "I need to think..."++ def test_reasoning_details_list(self):+ """reasoning_details list is joined into thinking block."""+ resp = _make_response(+ content="Answer",+ reasoning_details=[+ {"text": "Step 1"},+ {"text": "Step 2"},+ ],+ )+ result = convert_response(resp, _make_request())+ thinking = [b for b in result["content"] if b["type"] == "thinking"]+ assert len(thinking) == 1+ assert "Step 1" in thinking[0]["thinking"]+ assert "Step 2" in thinking[0]["thinking"]++ def test_content_with_think_tags(self):+ """Think tags in content string are extracted when no reasoning field."""+ resp = _make_response(content="<think>reasoning here</think>The answer is 42")+ result = convert_response(resp, _make_request())+ types = [b["type"] for b in result["content"]]+ assert "thinking" in types+ text_blocks = [b for b in result["content"] if b["type"] == "text"]+ assert any("42" in b["text"] for b in text_blocks)++ def test_think_tags_skipped_when_reasoning_exists(self):+ """When reasoning_content exists, think tags in content are NOT re-extracted."""+ resp = _make_response(+ content="<think>duplicate</think>Answer",+ reasoning_content="Real reasoning",+ )+ result = convert_response(resp, _make_request())+ thinking = [b for b in result["content"] if b["type"] == "thinking"]+ # Only the reasoning_content should be in thinking, not duplicate extraction+ assert len(thinking) == 1+ assert thinking[0]["thinking"] == "Real reasoning"++ def test_content_as_list(self):+ """Content as list of dicts is preserved."""+ resp = _make_response(+ content=[+ {"type": "text", "text": "Hello"},+ {"type": "text", "text": "World"},+ ]+ )+ result = convert_response(resp, _make_request())+ text_blocks = [b for b in result["content"] if b["type"] == "text"]+ assert len(text_blocks) == 2++ def test_tool_call_valid_json(self):+ """Tool calls with valid JSON arguments are parsed."""+ resp = _make_response(+ content="",+ tool_calls=[+ {+ "id": "call_1",+ "function": {+ "name": "search",+ "arguments": '{"query": "test"}',+ },+ }+ ],+ )+ result = convert_response(resp, _make_request())+ tool_blocks = [b for b in result["content"] if b["type"] == "tool_use"]+ assert len(tool_blocks) == 1+ assert tool_blocks[0]["input"] == {"query": "test"}+ assert tool_blocks[0]["name"] == "search"++ def test_tool_call_invalid_json_fallback(self):+ """Tool call with non-JSON arguments falls back to raw value."""+ resp = _make_response(+ content="",+ tool_calls=[+ {+ "id": "call_2",+ "function": {+ "name": "test",+ "arguments": "not valid json {",+ },+ }+ ],+ )+ result = convert_response(resp, _make_request())+ tool_blocks = [b for b in result["content"] if b["type"] == "tool_use"]+ assert len(tool_blocks) == 1+ assert tool_blocks[0]["input"] == "not valid json {"++ def test_usage_mapping(self):+ """Usage tokens are mapped from OpenAI to Anthropic format."""+ resp = _make_response(+ usage={"prompt_tokens": 100, "completion_tokens": 50}+ )+ result = convert_response(resp, _make_request())+ assert result["usage"]["input_tokens"] == 100+ assert result["usage"]["output_tokens"] == 50+ assert result["usage"]["cache_creation_input_tokens"] == 0+ assert result["usage"]["cache_read_input_tokens"] == 0++ def test_missing_usage(self):+ """Missing usage defaults to zeros."""+ resp = _make_response()+ resp.pop("usage")+ result = convert_response(resp, _make_request())+ assert result["usage"]["input_tokens"] == 0+ assert result["usage"]["output_tokens"] == 0++ @pytest.mark.parametrize(+ "finish_reason,expected_stop",+ [+ ("stop", "end_turn"),+ ("length", "max_tokens"),+ ("tool_calls", "tool_use"),+ ("content_filter", "end_turn"),+ (None, "end_turn"),+ ],+ ids=["stop", "length", "tool_calls", "content_filter", "none"],+ )+ def test_stop_reason_mapping(self, finish_reason, expected_stop):+ """Finish reasons map correctly to Anthropic stop_reasons."""+ resp = _make_response(finish_reason=finish_reason)+ result = convert_response(resp, _make_request())+ assert result["stop_reason"] == expected_stop++ def test_model_from_request(self):+ """Response model comes from original request, not provider response."""+ resp = _make_response()+ result = convert_response(resp, _make_request(model="claude-3"))+ assert result["model"] == "claude-3"diff --git a/tests/test_response_models.py b/tests/test_response_models.pynew file mode 100644index 00000000..eab439c1--- /dev/null+++ b/tests/test_response_models.py@@ -0,0 +1,177 @@+"""Tests for api/models/responses.py Pydantic response models."""++import pytest++from api.models.responses import MessagesResponse, Usage, TokenCountResponse+from api.models.anthropic import ContentBlockText, ContentBlockToolUse, ContentBlockThinking+++class TestUsage:+ """Tests for Usage model."""++ def test_required_fields(self):+ usage = Usage(input_tokens=10, output_tokens=20)+ assert usage.input_tokens == 10+ assert usage.output_tokens == 20++ def test_cache_defaults_zero(self):+ usage = Usage(input_tokens=1, output_tokens=2)+ assert usage.cache_creation_input_tokens == 0+ assert usage.cache_read_input_tokens == 0++ def test_cache_fields_set(self):+ usage = Usage(+ input_tokens=10,+ output_tokens=20,+ cache_creation_input_tokens=5,+ cache_read_input_tokens=3,+ )+ assert usage.cache_creation_input_tokens == 5+ assert usage.cache_read_input_tokens == 3++ def test_serialization(self):+ usage = Usage(input_tokens=10, output_tokens=20)+ data = usage.model_dump()+ assert data == {+ "input_tokens": 10,+ "output_tokens": 20,+ "cache_creation_input_tokens": 0,+ "cache_read_input_tokens": 0,+ }+++class TestTokenCountResponse:+ """Tests for TokenCountResponse model."""++ def test_basic(self):+ resp = TokenCountResponse(input_tokens=42)+ assert resp.input_tokens == 42++ def test_serialization(self):+ resp = TokenCountResponse(input_tokens=100)+ data = resp.model_dump()+ assert data == {"input_tokens": 100}+++class TestMessagesResponse:+ """Tests for MessagesResponse model."""++ def test_minimum_fields(self):+ resp = MessagesResponse(+ id="msg_001",+ model="test-model",+ content=[ContentBlockText(type="text", text="Hello")],+ usage=Usage(input_tokens=10, output_tokens=5),+ )+ assert resp.id == "msg_001"+ assert resp.model == "test-model"+ assert resp.role == "assistant"+ assert resp.type == "message"+ assert resp.stop_reason is None+ assert resp.stop_sequence is None++ def test_with_text_content(self):+ resp = MessagesResponse(+ id="msg_002",+ model="model",+ content=[ContentBlockText(type="text", text="response")],+ usage=Usage(input_tokens=1, output_tokens=1),+ )+ assert len(resp.content) == 1+ assert resp.content[0].type == "text"+ assert resp.content[0].text == "response"++ def test_with_tool_use_content(self):+ resp = MessagesResponse(+ id="msg_003",+ model="model",+ content=[+ ContentBlockToolUse(+ type="tool_use",+ id="tool_1",+ name="Read",+ input={"path": "test.py"},+ )+ ],+ usage=Usage(input_tokens=1, output_tokens=1),+ stop_reason="tool_use",+ )+ assert resp.content[0].type == "tool_use"+ assert resp.content[0].name == "Read"+ assert resp.stop_reason == "tool_use"++ def test_with_thinking_content(self):+ resp = MessagesResponse(+ id="msg_004",+ model="model",+ content=[+ ContentBlockThinking(type="thinking", thinking="Let me reason..."),+ ContentBlockText(type="text", text="Answer"),+ ],+ usage=Usage(input_tokens=5, output_tokens=10),+ )+ assert len(resp.content) == 2+ assert resp.content[0].type == "thinking"+ assert resp.content[0].thinking == "Let me reason..."+ assert resp.content[1].type == "text"++ def test_with_all_content_types(self):+ resp = MessagesResponse(+ id="msg_005",+ model="model",+ content=[+ ContentBlockThinking(type="thinking", thinking="hmm"),+ ContentBlockText(type="text", text="result"),+ ContentBlockToolUse(+ type="tool_use", id="t1", name="Bash", input={"command": "ls"}+ ),+ ],+ usage=Usage(input_tokens=10, output_tokens=20),+ stop_reason="tool_use",+ )+ assert len(resp.content) == 3++ def test_with_dict_content(self):+ """Dict content (unknown block type) should be accepted."""+ resp = MessagesResponse(+ id="msg_006",+ model="model",+ content=[{"type": "custom", "data": "value"}],+ usage=Usage(input_tokens=1, output_tokens=1),+ )+ assert resp.content[0]["type"] == "custom"++ def test_stop_reason_values(self):+ """All valid stop_reason values should be accepted."""+ for reason in ["end_turn", "max_tokens", "stop_sequence", "tool_use"]:+ resp = MessagesResponse(+ id="msg",+ model="model",+ content=[ContentBlockText(type="text", text="x")],+ usage=Usage(input_tokens=1, output_tokens=1),+ stop_reason=reason,+ )+ assert resp.stop_reason == reason++ def test_serialization_round_trip(self):+ resp = MessagesResponse(+ id="msg_rt",+ model="model-v1",+ content=[ContentBlockText(type="text", text="hello")],+ usage=Usage(input_tokens=10, output_tokens=5),+ stop_reason="end_turn",+ )+ data = resp.model_dump()+ restored = MessagesResponse(**data)+ assert restored.id == resp.id+ assert restored.model == resp.model+ assert restored.stop_reason == resp.stop_reason++ def test_empty_content_list(self):+ resp = MessagesResponse(+ id="msg_empty",+ model="model",+ content=[],+ usage=Usage(input_tokens=0, output_tokens=0),+ )+ assert resp.content == []diff --git a/tests/test_session_store_edge_cases.py b/tests/test_session_store_edge_cases.pynew file mode 100644index 00000000..561ef718--- /dev/null+++ b/tests/test_session_store_edge_cases.py@@ -0,0 +1,152 @@+"""Edge case tests for messaging/session.py SessionStore."""++import json+import os+import pytest+from unittest.mock import patch++from messaging.session import SessionStore+++def tmp_store(tmp_path):+ """Create a SessionStore using a temp file."""+ path = str(tmp_path / "sessions.json")+ return SessionStore(storage_path=path)+++class TestSessionStoreLoadEdgeCases:+ """Tests for loading corrupted/malformed data."""++ def test_load_corrupted_json(self, tmp_path):+ """Corrupted JSON file is handled gracefully (logs error, starts empty)."""+ path = str(tmp_path / "sessions.json")+ with open(path, "w") as f:+ f.write("{invalid json")++ store = SessionStore(storage_path=path)+ assert len(store._sessions) == 0+ assert len(store._trees) == 0++ def test_load_truncated_json(self, tmp_path):+ """Truncated JSON file is handled gracefully."""+ path = str(tmp_path / "sessions.json")+ with open(path, "w") as f:+ f.write('{"sessions": {"s1": {"session_id": "s1"')++ store = SessionStore(storage_path=path)+ assert len(store._sessions) == 0++ def test_load_empty_file(self, tmp_path):+ """Empty file is handled gracefully."""+ path = str(tmp_path / "sessions.json")+ with open(path, "w") as f:+ f.write("")++ store = SessionStore(storage_path=path)+ assert len(store._sessions) == 0++ def test_load_nonexistent_file(self, tmp_path):+ """Non-existent file starts with empty state."""+ path = str(tmp_path / "nonexistent.json")+ store = SessionStore(storage_path=path)+ assert len(store._sessions) == 0++ def test_load_legacy_int_fields(self, tmp_path):+ """Legacy format with int chat_id/msg_id is converted to string."""+ path = str(tmp_path / "sessions.json")+ data = {+ "sessions": {+ "s1": {+ "session_id": "s1",+ "chat_id": 12345,+ "initial_msg_id": 100,+ "last_msg_id": 200,+ "platform": "telegram",+ "created_at": "2025-01-01T00:00:00+00:00",+ "updated_at": "2025-01-01T00:00:00+00:00",+ }+ },+ "trees": {},+ "node_to_tree": {},+ }+ with open(path, "w") as f:+ json.dump(data, f)++ store = SessionStore(storage_path=path)+ record = store._sessions["s1"]+ assert record.chat_id == "12345"+ assert record.initial_msg_id == "100"+ assert record.last_msg_id == "200"+++class TestSessionStoreSaveEdgeCases:+ """Tests for save failure handling."""++ def test_save_io_error_handled(self, tmp_store):+ """Write failure in _save() is logged but doesn't raise."""+ tmp_store.save_session("s1", "c1", "m1")+ # Make the path read-only dir to trigger write error+ with patch("builtins.open", side_effect=IOError("disk full")):+ # _save() catches all exceptions and logs them+ tmp_store._save()+ # Should not raise+++class TestSessionStoreOperationEdgeCases:+ """Tests for edge cases in session operations."""++ def test_rename_session_not_found(self, tmp_store):+ """rename_session with non-existent old_id returns False."""+ result = tmp_store.rename_session("nonexistent", "new_id")+ assert result is False++ def test_update_last_message_not_found(self, tmp_store):+ """update_last_message with unknown session_id logs warning."""+ # Should not raise+ tmp_store.update_last_message("nonexistent", "msg_1")++ def test_get_session_by_msg_not_found(self, tmp_store):+ """Looking up non-existent message returns None."""+ result = tmp_store.get_session_by_msg("c1", "m999")+ assert result is None++ def test_get_session_record_not_found(self, tmp_store):+ """Getting non-existent session record returns None."""+ result = tmp_store.get_session_record("nonexistent")+ assert result is None+++class TestSessionStoreCleanupEdgeCases:+ """Tests for cleanup with malformed data."""++ def test_cleanup_sessions_malformed_timestamp(self, tmp_store):+ """Malformed created_at in cleanup doesn't crash."""+ tmp_store.save_session("s1", "c1", "m1")+ # Corrupt the timestamp+ tmp_store._sessions["s1"].created_at = "not-a-date"+ # Should not crash - the except block silently skips bad records+ removed = tmp_store.cleanup_old_sessions(max_age_days=0)+ assert removed == 0 # Skipped due to parse error++ def test_cleanup_trees_malformed_timestamp(self, tmp_store):+ """Malformed created_at in cleanup_old_trees doesn't crash."""+ tmp_store._trees["root1"] = {+ "nodes": {"root1": {"created_at": "bad-date"}}+ }+ removed = tmp_store.cleanup_old_trees(max_age_days=0)+ assert removed == 0 # Skipped due to parse error++ def test_cleanup_trees_missing_created_at(self, tmp_store):+ """Tree node without created_at is not cleaned up."""+ tmp_store._trees["root1"] = {+ "nodes": {"root1": {}}+ }+ removed = tmp_store.cleanup_old_trees(max_age_days=0)+ assert removed == 0++ def test_update_tree_node_nonexistent_tree(self, tmp_store):+ """Updating a node in a nonexistent tree logs warning."""+ tmp_store.update_tree_node("nonexistent", "node1", {"data": "test"})+ # Should not crash, tree not created+ assert "nonexistent" not in tmp_store._treesdiff --git a/tests/test_sse_builder.py b/tests/test_sse_builder.pynew file mode 100644index 00000000..9582a280--- /dev/null+++ b/tests/test_sse_builder.py@@ -0,0 +1,390 @@+"""Tests for providers/nvidia_nim/utils/sse_builder.py."""++import json+import pytest+from unittest.mock import patch++from providers.nvidia_nim.utils.sse_builder import (+ SSEBuilder,+ ContentBlockManager,+ map_stop_reason,+ STOP_REASON_MAP,+)+++def _parse_sse(sse_str: str) -> dict:+ """Parse an SSE event string into its data payload."""+ for line in sse_str.strip().split("\n"):+ if line.startswith("data: "):+ return json.loads(line[len("data: "):])+ raise ValueError(f"No data line found in SSE: {sse_str}")+++class TestMapStopReason:+ """Tests for map_stop_reason function."""++ @pytest.mark.parametrize(+ "openai_reason,expected",+ [+ ("stop", "end_turn"),+ ("length", "max_tokens"),+ ("tool_calls", "tool_use"),+ ("content_filter", "end_turn"),+ (None, "end_turn"),+ ("unknown_value", "end_turn"),+ ("", "end_turn"),+ ],+ ids=[+ "stop",+ "length",+ "tool_calls",+ "content_filter",+ "none",+ "unknown",+ "empty_string",+ ],+ )+ def test_map_stop_reason(self, openai_reason, expected):+ assert map_stop_reason(openai_reason) == expected+++class TestContentBlockManager:+ """Tests for ContentBlockManager."""++ def test_allocate_index_increments(self):+ mgr = ContentBlockManager()+ assert mgr.allocate_index() == 0+ assert mgr.allocate_index() == 1+ assert mgr.allocate_index() == 2++ def test_initial_state(self):+ mgr = ContentBlockManager()+ assert mgr.thinking_index == -1+ assert mgr.text_index == -1+ assert mgr.thinking_started is False+ assert mgr.text_started is False+ assert mgr.tool_indices == {}+++class TestSSEBuilderMessageLifecycle:+ """Tests for message_start, message_delta, message_stop, done."""++ def test_message_start(self):+ builder = SSEBuilder("msg_123", "test-model", input_tokens=50)+ sse = builder.message_start()++ assert "event: message_start" in sse+ data = _parse_sse(sse)+ assert data["type"] == "message_start"+ msg = data["message"]+ assert msg["id"] == "msg_123"+ assert msg["model"] == "test-model"+ assert msg["role"] == "assistant"+ assert msg["content"] == []+ assert msg["usage"]["input_tokens"] == 50+ assert msg["usage"]["output_tokens"] == 1++ def test_message_delta(self):+ builder = SSEBuilder("msg_1", "model")+ sse = builder.message_delta("end_turn", 42)++ assert "event: message_delta" in sse+ data = _parse_sse(sse)+ assert data["type"] == "message_delta"+ assert data["delta"]["stop_reason"] == "end_turn"+ assert data["usage"]["output_tokens"] == 42++ def test_message_stop(self):+ builder = SSEBuilder("msg_1", "model")+ sse = builder.message_stop()++ assert "event: message_stop" in sse+ data = _parse_sse(sse)+ assert data["type"] == "message_stop"++ def test_done(self):+ builder = SSEBuilder("msg_1", "model")+ assert builder.done() == "[DONE]\n\n"+++class TestSSEBuilderContentBlocks:+ """Tests for content block start/delta/stop events."""++ def test_content_block_start_text(self):+ builder = SSEBuilder("msg_1", "model")+ sse = builder.content_block_start(0, "text", text="hello")++ data = _parse_sse(sse)+ assert data["type"] == "content_block_start"+ assert data["index"] == 0+ assert data["content_block"]["type"] == "text"+ assert data["content_block"]["text"] == "hello"++ def test_content_block_start_thinking(self):+ builder = SSEBuilder("msg_1", "model")+ sse = builder.content_block_start(1, "thinking")++ data = _parse_sse(sse)+ assert data["content_block"]["type"] == "thinking"+ assert data["content_block"]["thinking"] == ""++ def test_content_block_start_tool_use(self):+ builder = SSEBuilder("msg_1", "model")+ sse = builder.content_block_start(+ 2, "tool_use", id="tool_123", name="Read", input={}+ )++ data = _parse_sse(sse)+ assert data["content_block"]["type"] == "tool_use"+ assert data["content_block"]["id"] == "tool_123"+ assert data["content_block"]["name"] == "Read"+ assert data["content_block"]["input"] == {}++ def test_content_block_delta_text(self):+ builder = SSEBuilder("msg_1", "model")+ sse = builder.content_block_delta(0, "text_delta", "hello world")++ data = _parse_sse(sse)+ assert data["type"] == "content_block_delta"+ assert data["index"] == 0+ assert data["delta"]["type"] == "text_delta"+ assert data["delta"]["text"] == "hello world"++ def test_content_block_delta_thinking(self):+ builder = SSEBuilder("msg_1", "model")+ sse = builder.content_block_delta(1, "thinking_delta", "reasoning...")++ data = _parse_sse(sse)+ assert data["delta"]["type"] == "thinking_delta"+ assert data["delta"]["thinking"] == "reasoning..."++ def test_content_block_delta_input_json(self):+ builder = SSEBuilder("msg_1", "model")+ sse = builder.content_block_delta(2, "input_json_delta", '{"key": "val"}')++ data = _parse_sse(sse)+ assert data["delta"]["type"] == "input_json_delta"+ assert data["delta"]["partial_json"] == '{"key": "val"}'++ def test_content_block_stop(self):+ builder = SSEBuilder("msg_1", "model")+ sse = builder.content_block_stop(0)++ data = _parse_sse(sse)+ assert data["type"] == "content_block_stop"+ assert data["index"] == 0+++class TestSSEBuilderHighLevelHelpers:+ """Tests for high-level thinking/text/tool block helpers."""++ def test_start_and_stop_thinking_block(self):+ builder = SSEBuilder("msg_1", "model")++ start_sse = builder.start_thinking_block()+ data = _parse_sse(start_sse)+ assert data["content_block"]["type"] == "thinking"+ assert builder.blocks.thinking_started is True+ assert builder.blocks.thinking_index == 0++ stop_sse = builder.stop_thinking_block()+ data = _parse_sse(stop_sse)+ assert data["type"] == "content_block_stop"+ assert builder.blocks.thinking_started is False++ def test_emit_thinking_delta_accumulates(self):+ builder = SSEBuilder("msg_1", "model")+ builder.start_thinking_block()++ builder.emit_thinking_delta("part1 ")+ builder.emit_thinking_delta("part2")++ assert builder.accumulated_reasoning == "part1 part2"++ def test_start_and_stop_text_block(self):+ builder = SSEBuilder("msg_1", "model")++ start_sse = builder.start_text_block()+ data = _parse_sse(start_sse)+ assert data["content_block"]["type"] == "text"+ assert builder.blocks.text_started is True+ assert builder.blocks.text_index == 0++ stop_sse = builder.stop_text_block()+ assert builder.blocks.text_started is False++ def test_emit_text_delta_accumulates(self):+ builder = SSEBuilder("msg_1", "model")+ builder.start_text_block()++ builder.emit_text_delta("hello ")+ builder.emit_text_delta("world")++ assert builder.accumulated_text == "hello world"++ def test_start_tool_block(self):+ builder = SSEBuilder("msg_1", "model")+ sse = builder.start_tool_block(0, "tool_abc", "Grep")++ data = _parse_sse(sse)+ assert data["content_block"]["type"] == "tool_use"+ assert data["content_block"]["id"] == "tool_abc"+ assert data["content_block"]["name"] == "Grep"+ assert 0 in builder.blocks.tool_indices++ def test_emit_tool_delta(self):+ builder = SSEBuilder("msg_1", "model")+ builder.start_tool_block(0, "tool_abc", "Grep")++ sse = builder.emit_tool_delta(0, '{"pattern":')+ data = _parse_sse(sse)+ assert data["delta"]["partial_json"] == '{"pattern":'+ assert builder.blocks.tool_contents[0] == '{"pattern":'++ def test_stop_tool_block(self):+ builder = SSEBuilder("msg_1", "model")+ builder.start_tool_block(0, "tool_abc", "Grep")++ sse = builder.stop_tool_block(0)+ data = _parse_sse(sse)+ assert data["type"] == "content_block_stop"+++class TestSSEBuilderStateManagement:+ """Tests for ensure_thinking_block, ensure_text_block, close_all_blocks."""++ def test_ensure_thinking_block_closes_text_first(self):+ builder = SSEBuilder("msg_1", "model")+ builder.start_text_block()+ assert builder.blocks.text_started is True++ events = list(builder.ensure_thinking_block())+ # Should close text then start thinking+ assert len(events) == 2+ assert builder.blocks.text_started is False+ assert builder.blocks.thinking_started is True++ def test_ensure_thinking_block_noop_if_already_started(self):+ builder = SSEBuilder("msg_1", "model")+ builder.start_thinking_block()++ events = list(builder.ensure_thinking_block())+ assert events == []++ def test_ensure_text_block_closes_thinking_first(self):+ builder = SSEBuilder("msg_1", "model")+ builder.start_thinking_block()+ assert builder.blocks.thinking_started is True++ events = list(builder.ensure_text_block())+ # Should close thinking then start text+ assert len(events) == 2+ assert builder.blocks.thinking_started is False+ assert builder.blocks.text_started is True++ def test_ensure_text_block_noop_if_already_started(self):+ builder = SSEBuilder("msg_1", "model")+ builder.start_text_block()++ events = list(builder.ensure_text_block())+ assert events == []++ def test_close_content_blocks(self):+ builder = SSEBuilder("msg_1", "model")+ builder.start_thinking_block()+ builder.stop_thinking_block()+ builder.start_text_block()++ events = list(builder.close_content_blocks())+ # Should close text (thinking already stopped)+ assert len(events) == 1+ assert builder.blocks.text_started is False++ def test_close_all_blocks(self):+ builder = SSEBuilder("msg_1", "model")+ builder.start_thinking_block()+ builder.stop_thinking_block()+ builder.start_text_block()+ builder.start_tool_block(0, "t1", "Read")+ builder.start_tool_block(1, "t2", "Write")++ events = list(builder.close_all_blocks())+ # Close text + 2 tool blocks (thinking already stopped)+ assert len(events) == 3+ assert builder.blocks.text_started is False++ def test_close_all_blocks_empty(self):+ builder = SSEBuilder("msg_1", "model")+ events = list(builder.close_all_blocks())+ assert events == []+++class TestSSEBuilderError:+ """Tests for emit_error."""++ def test_emit_error(self):+ builder = SSEBuilder("msg_1", "model")+ events = list(builder.emit_error("Something went wrong"))++ assert len(events) == 3 # start, delta, stop+ start_data = _parse_sse(events[0])+ assert start_data["content_block"]["type"] == "text"++ delta_data = _parse_sse(events[1])+ assert delta_data["delta"]["text"] == "Something went wrong"++ stop_data = _parse_sse(events[2])+ assert stop_data["type"] == "content_block_stop"+++class TestSSEBuilderTokenEstimation:+ """Tests for estimate_output_tokens."""++ def test_estimate_with_text_only(self):+ builder = SSEBuilder("msg_1", "model")+ builder.start_text_block()+ builder.emit_text_delta("hello world")++ tokens = builder.estimate_output_tokens()+ assert tokens > 0++ def test_estimate_with_reasoning(self):+ builder = SSEBuilder("msg_1", "model")+ builder.start_thinking_block()+ builder.emit_thinking_delta("deep thought")+ builder.stop_thinking_block()+ builder.start_text_block()+ builder.emit_text_delta("answer")++ tokens = builder.estimate_output_tokens()+ assert tokens > 0++ def test_estimate_empty(self):+ builder = SSEBuilder("msg_1", "model")+ tokens = builder.estimate_output_tokens()+ assert tokens == 0++ def test_estimate_without_tiktoken(self):+ """Fallback estimation when tiktoken is not available."""+ builder = SSEBuilder("msg_1", "model")+ builder.start_text_block()+ builder.emit_text_delta("a" * 100) # 100 chars -> ~25 tokens++ with patch(+ "providers.nvidia_nim.utils.sse_builder.ENCODER", None+ ):+ tokens = builder.estimate_output_tokens()+ assert tokens == 25 # 100 // 4++ def test_estimate_with_tools_no_tiktoken(self):+ """Fallback tool token estimation."""+ builder = SSEBuilder("msg_1", "model")+ builder.start_tool_block(0, "t1", "Read")+ builder.emit_tool_delta(0, '{"path":"test.py"}')++ with patch(+ "providers.nvidia_nim.utils.sse_builder.ENCODER", None+ ):+ tokens = builder.estimate_output_tokens()+ # 1 tool * 50 = 50+ assert tokens == 50diff --git a/tests/test_streaming_errors.py b/tests/test_streaming_errors.pynew file mode 100644index 00000000..7da208bd--- /dev/null+++ b/tests/test_streaming_errors.py@@ -0,0 +1,327 @@+"""Tests for streaming error handling in providers/nvidia_nim/client.py."""++import json+import pytest+from unittest.mock import AsyncMock, MagicMock, patch++from providers.nvidia_nim import NvidiaNimProvider+from providers.base import ProviderConfig+from config.nim import NimSettings+++class AsyncStreamMock:+ """Async iterable mock that yields chunks then optionally raises."""++ def __init__(self, chunks, error=None):+ self._chunks = chunks+ self._error = error++ def __aiter__(self):+ return self._aiter()++ async def _aiter(self):+ for chunk in self._chunks:+ yield chunk+ if self._error:+ raise self._error+++def _make_provider():+ """Create a provider instance for testing."""+ config = ProviderConfig(+ api_key="test_key",+ base_url="https://test.api.nvidia.com/v1",+ rate_limit=10,+ rate_window=60,+ nim_settings=NimSettings(),+ )+ return NvidiaNimProvider(config)+++def _make_request(model="test-model", stream=True):+ """Create a mock request with all fields build_request_body needs."""+ req = MagicMock()+ req.model = model+ req.stream = stream+ req.messages = []+ req.system = None+ req.tools = None+ req.tool_choice = None+ req.metadata = None+ req.max_tokens = 4096+ req.temperature = None+ req.top_p = None+ req.top_k = None+ req.stop_sequences = None+ req.extra_body = None+ req.thinking = None+ return req+++def _make_chunk(content=None, finish_reason=None, tool_calls=None, reasoning_content=None):+ """Create a mock streaming chunk."""+ delta = MagicMock()+ delta.content = content+ delta.tool_calls = tool_calls+ delta.reasoning_content = reasoning_content if reasoning_content else None++ choice = MagicMock()+ choice.delta = delta+ choice.finish_reason = finish_reason++ chunk = MagicMock()+ chunk.choices = [choice]+ chunk.usage = None+ return chunk+++async def _collect_stream(provider, request):+ """Collect all SSE events from a stream."""+ events = []+ async for event in provider.stream_response(request):+ events.append(event)+ return events+++class TestStreamingExceptionHandling:+ """Tests for error paths during stream_response."""++ @pytest.mark.asyncio+ async def test_api_error_emits_sse_error_event(self):+ """When API raises during streaming, SSE error event is emitted."""+ provider = _make_provider()+ request = _make_request()++ mock_stream = AsyncMock()+ mock_stream.__aiter__ = MagicMock(side_effect=RuntimeError("API failed"))++ with patch.object(+ provider._client.chat.completions,+ "create",+ new_callable=AsyncMock,+ side_effect=RuntimeError("API failed"),+ ):+ with patch.object(provider._global_rate_limiter, "wait_if_blocked", new_callable=AsyncMock, return_value=False):+ events = await _collect_stream(provider, request)++ # Should have message_start, error text block, close blocks, message_delta, message_stop, done+ event_text = "".join(events)+ assert "message_start" in event_text+ assert "API failed" in event_text+ assert "message_stop" in event_text+ assert "[DONE]" in event_text++ @pytest.mark.asyncio+ async def test_error_after_partial_content(self):+ """Error after partial content: blocks closed, error emitted."""+ provider = _make_provider()+ request = _make_request()++ chunk1 = _make_chunk(content="Hello ")+ stream_mock = AsyncStreamMock([chunk1], error=RuntimeError("Connection lost"))++ with patch.object(+ provider._client.chat.completions,+ "create",+ new_callable=AsyncMock,+ return_value=stream_mock,+ ):+ with patch.object(provider._global_rate_limiter, "wait_if_blocked", new_callable=AsyncMock, return_value=False):+ events = await _collect_stream(provider, request)++ event_text = "".join(events)+ assert "Hello" in event_text+ assert "Connection lost" in event_text+ assert "message_stop" in event_text++ @pytest.mark.asyncio+ async def test_empty_response_gets_space(self):+ """Empty response with no text/tools gets a single space text block."""+ provider = _make_provider()+ request = _make_request()++ empty_chunk = _make_chunk(finish_reason="stop")+ stream_mock = AsyncStreamMock([empty_chunk])++ with patch.object(+ provider._client.chat.completions,+ "create",+ new_callable=AsyncMock,+ return_value=stream_mock,+ ):+ with patch.object(provider._global_rate_limiter, "wait_if_blocked", new_callable=AsyncMock, return_value=False):+ events = await _collect_stream(provider, request)++ event_text = "".join(events)+ assert '"text_delta"' in event_text+ assert "message_stop" in event_text++ @pytest.mark.asyncio+ async def test_stream_with_thinking_content(self):+ """Thinking content via think tags is emitted as thinking blocks."""+ provider = _make_provider()+ request = _make_request()++ chunk1 = _make_chunk(content="<think>reasoning</think>answer")+ chunk2 = _make_chunk(finish_reason="stop")+ stream_mock = AsyncStreamMock([chunk1, chunk2])++ with patch.object(+ provider._client.chat.completions,+ "create",+ new_callable=AsyncMock,+ return_value=stream_mock,+ ):+ with patch.object(provider._global_rate_limiter, "wait_if_blocked", new_callable=AsyncMock, return_value=False):+ events = await _collect_stream(provider, request)++ event_text = "".join(events)+ assert "thinking" in event_text+ assert "reasoning" in event_text+ assert "answer" in event_text++ @pytest.mark.asyncio+ async def test_stream_with_reasoning_content_field(self):+ """reasoning_content delta field is emitted as thinking block."""+ provider = _make_provider()+ request = _make_request()++ chunk1 = _make_chunk(reasoning_content="I think...")+ chunk2 = _make_chunk(content="The answer")+ chunk3 = _make_chunk(finish_reason="stop")+ stream_mock = AsyncStreamMock([chunk1, chunk2, chunk3])++ with patch.object(+ provider._client.chat.completions,+ "create",+ new_callable=AsyncMock,+ return_value=stream_mock,+ ):+ with patch.object(provider._global_rate_limiter, "wait_if_blocked", new_callable=AsyncMock, return_value=False):+ events = await _collect_stream(provider, request)++ event_text = "".join(events)+ assert "thinking_delta" in event_text+ assert "I think..." in event_text+ assert "The answer" in event_text++ @pytest.mark.asyncio+ async def test_stream_rate_limited_shows_notice(self):+ """When globally rate limited, a notice is shown before stream starts."""+ provider = _make_provider()+ request = _make_request()++ chunk1 = _make_chunk(content="Response")+ chunk2 = _make_chunk(finish_reason="stop")+ stream_mock = AsyncStreamMock([chunk1, chunk2])++ with patch.object(+ provider._client.chat.completions,+ "create",+ new_callable=AsyncMock,+ return_value=stream_mock,+ ):+ with patch.object(provider._global_rate_limiter, "wait_if_blocked", new_callable=AsyncMock, return_value=True):+ events = await _collect_stream(provider, request)++ event_text = "".join(events)+ assert "rate limit" in event_text.lower()+ assert "Response" in event_text+++class TestProcessToolCall:+ """Tests for _process_tool_call method."""++ def test_tool_call_with_id(self):+ """Tool call with id starts a tool block."""+ provider = _make_provider()+ from providers.nvidia_nim.utils import SSEBuilder++ sse = SSEBuilder("msg_test", "test-model")+ tc = {+ "index": 0,+ "id": "call_123",+ "function": {"name": "search", "arguments": '{"q": "test"}'},+ }+ events = list(provider._process_tool_call(tc, sse))+ event_text = "".join(events)+ assert "tool_use" in event_text+ assert "search" in event_text+ assert "call_123" in event_text++ def test_tool_call_without_id_generates_uuid(self):+ """Tool call without id generates a uuid-based id."""+ provider = _make_provider()+ from providers.nvidia_nim.utils import SSEBuilder++ sse = SSEBuilder("msg_test", "test-model")+ tc = {+ "index": 0,+ "id": None,+ "function": {"name": "test", "arguments": "{}"},+ }+ events = list(provider._process_tool_call(tc, sse))+ event_text = "".join(events)+ assert "tool_" in event_text++ def test_task_tool_forces_background_false(self):+ """Task tool with run_in_background=true is forced to false."""+ provider = _make_provider()+ from providers.nvidia_nim.utils import SSEBuilder++ sse = SSEBuilder("msg_test", "test-model")+ args = json.dumps({"run_in_background": True, "prompt": "test"})+ tc = {+ "index": 0,+ "id": "call_task",+ "function": {"name": "Task", "arguments": args},+ }+ events = list(provider._process_tool_call(tc, sse))+ event_text = "".join(events)+ # The intercepted args should have run_in_background=false+ assert "false" in event_text.lower()++ def test_task_tool_invalid_json_logs_warning(self):+ """Invalid JSON args for Task tool doesn't crash."""+ provider = _make_provider()+ from providers.nvidia_nim.utils import SSEBuilder++ sse = SSEBuilder("msg_test", "test-model")+ tc = {+ "index": 0,+ "id": "call_task2",+ "function": {"name": "Task", "arguments": "not json"},+ }+ # Should not raise+ events = list(provider._process_tool_call(tc, sse))+ assert len(events) > 0++ def test_negative_tool_index_fallback(self):+ """tc_index < 0 uses len(tool_indices) as fallback."""+ provider = _make_provider()+ from providers.nvidia_nim.utils import SSEBuilder++ sse = SSEBuilder("msg_test", "test-model")+ tc = {+ "index": -1,+ "id": "call_neg",+ "function": {"name": "test", "arguments": "{}"},+ }+ events = list(provider._process_tool_call(tc, sse))+ # Should not crash, should still emit events+ assert len(events) > 0++ def test_tool_args_emitted_as_delta(self):+ """Arguments are emitted as input_json_delta events."""+ provider = _make_provider()+ from providers.nvidia_nim.utils import SSEBuilder++ sse = SSEBuilder("msg_test", "test-model")+ tc = {+ "index": 0,+ "id": "call_args",+ "function": {"name": "grep", "arguments": '{"pattern": "test"}'},+ }+ events = list(provider._process_tool_call(tc, sse))+ event_text = "".join(events)+ assert "input_json_delta" in event_textdiff --git a/tests/test_tree_concurrency.py b/tests/test_tree_concurrency.pynew file mode 100644index 00000000..04ae9455--- /dev/null+++ b/tests/test_tree_concurrency.py@@ -0,0 +1,602 @@+"""Concurrency and race condition tests for tree data structures and queue manager."""++import pytest+import pytest_asyncio+import asyncio++from messaging.models import IncomingMessage+from messaging.tree_data import MessageState, MessageNode, MessageTree+from messaging.tree_queue import TreeQueueManager+++def _make_incoming(text: str = "hello", msg_id: str = "m1") -> IncomingMessage:+ """Create a minimal IncomingMessage for testing."""+ return IncomingMessage(+ text=text,+ chat_id="chat1",+ user_id="user1",+ message_id=msg_id,+ platform="test",+ )+++def _make_tree(root_id: str = "root") -> MessageTree:+ """Create a tree with a single root node."""+ root = MessageNode(+ node_id=root_id,+ incoming=_make_incoming(msg_id=root_id),+ status_message_id=f"status_{root_id}",+ state=MessageState.PENDING,+ )+ return MessageTree(root)+++class TestMessageTreeConcurrency:+ """Concurrency tests for MessageTree operations."""++ @pytest.mark.asyncio+ async def test_concurrent_add_node_serialized(self):+ """Concurrent add_node calls should all succeed via lock serialization."""+ tree = _make_tree("root")+ count = 10++ async def add(i: int):+ return await tree.add_node(+ node_id=f"child_{i}",+ incoming=_make_incoming(msg_id=f"child_{i}"),+ status_message_id=f"status_{i}",+ parent_id="root",+ )++ results = await asyncio.gather(*[add(i) for i in range(count)])++ assert len(results) == count+ # All nodes plus root+ assert len(tree.all_nodes()) == count + 1+ # Root should have all children+ root = tree.get_root()+ assert len(root.children_ids) == count++ @pytest.mark.asyncio+ async def test_concurrent_enqueue_dequeue_no_loss(self):+ """Concurrent enqueue/dequeue should not lose items."""+ tree = _make_tree("root")++ # Add nodes first+ for i in range(10):+ await tree.add_node(+ node_id=f"n{i}",+ incoming=_make_incoming(msg_id=f"n{i}"),+ status_message_id=f"s{i}",+ parent_id="root",+ )++ # Enqueue all concurrently+ await asyncio.gather(*[tree.enqueue(f"n{i}") for i in range(10)])+ assert tree.get_queue_size() == 10++ # Dequeue all+ dequeued = []+ for _ in range(10):+ nid = await tree.dequeue()+ if nid:+ dequeued.append(nid)++ assert len(dequeued) == 10+ assert set(dequeued) == {f"n{i}" for i in range(10)}+ assert tree.get_queue_size() == 0++ @pytest.mark.asyncio+ async def test_dequeue_empty_returns_none(self):+ """Dequeue on empty queue returns None."""+ tree = _make_tree("root")+ result = await tree.dequeue()+ assert result is None++ @pytest.mark.asyncio+ async def test_concurrent_update_state(self):+ """Concurrent state updates should all apply (last writer wins)."""+ tree = _make_tree("root")+ for i in range(5):+ await tree.add_node(+ node_id=f"n{i}",+ incoming=_make_incoming(msg_id=f"n{i}"),+ status_message_id=f"s{i}",+ parent_id="root",+ )++ # Update all nodes concurrently+ await asyncio.gather(+ *[+ tree.update_state(f"n{i}", MessageState.IN_PROGRESS)+ for i in range(5)+ ]+ )++ for i in range(5):+ node = tree.get_node(f"n{i}")+ assert node.state == MessageState.IN_PROGRESS++ @pytest.mark.asyncio+ async def test_update_state_nonexistent_node(self):+ """Updating state of a nonexistent node should not raise."""+ tree = _make_tree("root")+ # Should just log a warning, not raise+ await tree.update_state("nonexistent", MessageState.ERROR)++ @pytest.mark.asyncio+ async def test_add_node_invalid_parent_raises(self):+ """Adding a node with nonexistent parent should raise ValueError."""+ tree = _make_tree("root")+ with pytest.raises(ValueError, match="not found in tree"):+ await tree.add_node(+ node_id="child",+ incoming=_make_incoming(),+ status_message_id="s1",+ parent_id="nonexistent",+ )++ @pytest.mark.asyncio+ async def test_queue_snapshot_matches_enqueue_order(self):+ """Queue snapshot should return items in FIFO order."""+ tree = _make_tree("root")+ for i in range(5):+ await tree.add_node(+ node_id=f"n{i}",+ incoming=_make_incoming(msg_id=f"n{i}"),+ status_message_id=f"s{i}",+ parent_id="root",+ )++ for i in range(5):+ await tree.enqueue(f"n{i}")++ snapshot = await tree.get_queue_snapshot()+ assert snapshot == [f"n{i}" for i in range(5)]++ @pytest.mark.asyncio+ async def test_enqueue_returns_position(self):+ """Enqueue should return 1-indexed position."""+ tree = _make_tree("root")+ for i in range(3):+ await tree.add_node(+ node_id=f"n{i}",+ incoming=_make_incoming(msg_id=f"n{i}"),+ status_message_id=f"s{i}",+ parent_id="root",+ )++ pos1 = await tree.enqueue("n0")+ pos2 = await tree.enqueue("n1")+ pos3 = await tree.enqueue("n2")++ assert pos1 == 1+ assert pos2 == 2+ assert pos3 == 3+++class TestMessageTreeNavigation:+ """Tests for tree navigation methods."""++ @pytest.mark.asyncio+ async def test_get_children(self):+ """get_children returns child nodes."""+ tree = _make_tree("root")+ await tree.add_node("c1", _make_incoming(msg_id="c1"), "s1", "root")+ await tree.add_node("c2", _make_incoming(msg_id="c2"), "s2", "root")++ children = tree.get_children("root")+ assert len(children) == 2+ assert {c.node_id for c in children} == {"c1", "c2"}++ def test_get_children_nonexistent(self):+ """get_children for nonexistent node returns empty list."""+ tree = _make_tree("root")+ assert tree.get_children("nonexistent") == []++ def test_get_parent_root(self):+ """Root node has no parent."""+ tree = _make_tree("root")+ assert tree.get_parent("root") is None++ @pytest.mark.asyncio+ async def test_get_parent_child(self):+ """Child node's parent is the root."""+ tree = _make_tree("root")+ await tree.add_node("c1", _make_incoming(msg_id="c1"), "s1", "root")+ parent = tree.get_parent("c1")+ assert parent is not None+ assert parent.node_id == "root"++ @pytest.mark.asyncio+ async def test_get_parent_session_id(self):+ """get_parent_session_id returns parent's session_id."""+ tree = _make_tree("root")+ await tree.update_state("root", MessageState.COMPLETED, session_id="sess_abc")+ await tree.add_node("c1", _make_incoming(msg_id="c1"), "s1", "root")++ session_id = tree.get_parent_session_id("c1")+ assert session_id == "sess_abc"++ def test_get_parent_session_id_root(self):+ """Root node has no parent session."""+ tree = _make_tree("root")+ assert tree.get_parent_session_id("root") is None++ def test_has_node(self):+ """has_node returns True for existing nodes."""+ tree = _make_tree("root")+ assert tree.has_node("root") is True+ assert tree.has_node("nonexistent") is False++ @pytest.mark.asyncio+ async def test_find_node_by_status_message(self):+ """find_node_by_status_message finds the right node."""+ tree = _make_tree("root")+ await tree.add_node("c1", _make_incoming(msg_id="c1"), "status_c1", "root")++ found = tree.find_node_by_status_message("status_c1")+ assert found is not None+ assert found.node_id == "c1"++ def test_find_node_by_status_message_not_found(self):+ """find_node_by_status_message returns None if not found."""+ tree = _make_tree("root")+ assert tree.find_node_by_status_message("nonexistent") is None+++class TestMessageTreeSerialization:+ """Tests for tree serialization/deserialization."""++ @pytest.mark.asyncio+ async def test_round_trip(self):+ """Tree should survive serialization round-trip."""+ tree = _make_tree("root")+ await tree.add_node("c1", _make_incoming(msg_id="c1"), "s1", "root")+ await tree.add_node("c2", _make_incoming(msg_id="c2"), "s2", "root")+ await tree.update_state("root", MessageState.COMPLETED, session_id="sess1")++ data = tree.to_dict()+ restored = MessageTree.from_dict(data)++ assert restored.root_id == "root"+ assert len(restored.all_nodes()) == 3+ root = restored.get_root()+ assert root.state == MessageState.COMPLETED+ assert root.session_id == "sess1"+ assert set(root.children_ids) == {"c1", "c2"}++ @pytest.mark.asyncio+ async def test_node_round_trip(self):+ """MessageNode should survive serialization round-trip."""+ node = MessageNode(+ node_id="n1",+ incoming=_make_incoming(msg_id="n1"),+ status_message_id="s1",+ state=MessageState.COMPLETED,+ parent_id="root",+ session_id="sess_test",+ error_message="test error",+ )+ data = node.to_dict()+ restored = MessageNode.from_dict(data)++ assert restored.node_id == "n1"+ assert restored.state == MessageState.COMPLETED+ assert restored.session_id == "sess_test"+ assert restored.error_message == "test error"+ assert restored.parent_id == "root"+++class TestTreeQueueManagerConcurrency:+ """Concurrency tests for TreeQueueManager."""++ @pytest.mark.asyncio+ async def test_concurrent_create_trees(self):+ """Creating multiple trees concurrently should all succeed."""+ mgr = TreeQueueManager()++ async def create(i: int):+ return await mgr.create_tree(+ node_id=f"root_{i}",+ incoming=_make_incoming(msg_id=f"root_{i}"),+ status_message_id=f"status_{i}",+ )++ trees = await asyncio.gather(*[create(i) for i in range(10)])+ assert len(trees) == 10+ assert mgr.get_tree_count() == 10++ @pytest.mark.asyncio+ async def test_add_to_tree_concurrent(self):+ """Adding replies to a tree concurrently should all succeed."""+ mgr = TreeQueueManager()+ await mgr.create_tree("root", _make_incoming(msg_id="root"), "s_root")++ async def add_reply(i: int):+ return await mgr.add_to_tree(+ parent_node_id="root",+ node_id=f"reply_{i}",+ incoming=_make_incoming(msg_id=f"reply_{i}"),+ status_message_id=f"s_reply_{i}",+ )++ results = await asyncio.gather(*[add_reply(i) for i in range(5)])+ assert len(results) == 5+ tree = mgr.get_tree("root")+ assert len(tree.all_nodes()) == 6 # root + 5 replies++ @pytest.mark.asyncio+ async def test_add_to_tree_invalid_parent(self):+ """Adding to a nonexistent parent should raise ValueError."""+ mgr = TreeQueueManager()+ await mgr.create_tree("root", _make_incoming(msg_id="root"), "s_root")++ with pytest.raises(ValueError, match="not found"):+ await mgr.add_to_tree(+ parent_node_id="nonexistent",+ node_id="reply",+ incoming=_make_incoming(),+ status_message_id="s1",+ )++ @pytest.mark.asyncio+ async def test_enqueue_and_process(self):+ """Enqueue should process immediately if tree is free."""+ mgr = TreeQueueManager()+ tree = await mgr.create_tree("root", _make_incoming(msg_id="root"), "s_root")++ processed = []++ async def processor(node_id, node):+ processed.append(node_id)++ queued = await mgr.enqueue("root", processor)+ # Should process immediately (not queued)+ assert queued is False++ # Wait for the async task to complete+ await asyncio.sleep(0.1)+ assert "root" in processed++ @pytest.mark.asyncio+ async def test_enqueue_queues_when_busy(self):+ """Enqueue should queue when tree is already processing."""+ mgr = TreeQueueManager()+ tree = await mgr.create_tree("root", _make_incoming(msg_id="root"), "s_root")+ _, _ = await mgr.add_to_tree("root", "c1", _make_incoming(msg_id="c1"), "s1")++ processing_started = asyncio.Event()+ release = asyncio.Event()++ async def slow_processor(node_id, node):+ processing_started.set()+ await release.wait()++ # Start processing root (will block)+ queued_first = await mgr.enqueue("root", slow_processor)+ assert queued_first is False+ await processing_started.wait()++ # Now tree is busy, second enqueue should be queued+ queued_second = await mgr.enqueue("c1", slow_processor)+ assert queued_second is True++ # Release the blocker so things clean up+ release.set()+ await asyncio.sleep(0.2)++ @pytest.mark.asyncio+ async def test_cancel_tree(self):+ """cancel_tree should cancel in-progress and queued nodes."""+ mgr = TreeQueueManager()+ tree = await mgr.create_tree("root", _make_incoming(msg_id="root"), "s_root")+ _, _ = await mgr.add_to_tree("root", "c1", _make_incoming(msg_id="c1"), "s1")+ _, _ = await mgr.add_to_tree("root", "c2", _make_incoming(msg_id="c2"), "s2")++ processing_started = asyncio.Event()++ async def slow_processor(node_id, node):+ processing_started.set()+ await asyncio.sleep(10) # Long running++ # Start processing root+ await mgr.enqueue("root", slow_processor)+ await processing_started.wait()++ # Queue additional nodes+ await mgr.enqueue("c1", slow_processor)+ await mgr.enqueue("c2", slow_processor)++ # Cancel the tree+ cancelled = mgr.cancel_tree("root")+ assert len(cancelled) >= 1 # At least the current + queued++ # Tree should no longer be processing+ assert tree._is_processing is False++ @pytest.mark.asyncio+ async def test_cancel_nonexistent_tree(self):+ """cancel_tree for nonexistent tree returns empty list."""+ mgr = TreeQueueManager()+ result = mgr.cancel_tree("nonexistent")+ assert result == []++ @pytest.mark.asyncio+ async def test_cancel_all(self):+ """cancel_all cancels all trees."""+ mgr = TreeQueueManager()+ await mgr.create_tree("t1", _make_incoming(msg_id="t1"), "s1")+ await mgr.create_tree("t2", _make_incoming(msg_id="t2"), "s2")++ # Mark nodes as PENDING (they already are by default)+ cancelled = await mgr.cancel_all()+ # Nodes were PENDING but not in queue, so cleanup_stale logic applies+ # At minimum, it should not raise+ assert isinstance(cancelled, list)++ @pytest.mark.asyncio+ async def test_cleanup_stale_nodes(self):+ """cleanup_stale_nodes marks PENDING/IN_PROGRESS nodes as ERROR."""+ mgr = TreeQueueManager()+ tree = await mgr.create_tree("root", _make_incoming(msg_id="root"), "s_root")+ _, _ = await mgr.add_to_tree("root", "c1", _make_incoming(msg_id="c1"), "s1")++ # Root is PENDING, c1 is PENDING+ count = mgr.cleanup_stale_nodes()+ assert count == 2++ root = tree.get_node("root")+ assert root.state == MessageState.ERROR+ assert "restart" in root.error_message++ @pytest.mark.asyncio+ async def test_mark_node_error_with_propagation(self):+ """mark_node_error should propagate to pending children."""+ mgr = TreeQueueManager()+ tree = await mgr.create_tree("root", _make_incoming(msg_id="root"), "s_root")+ _, _ = await mgr.add_to_tree("root", "c1", _make_incoming(msg_id="c1"), "s1")+ _, _ = await mgr.add_to_tree("c1", "c2", _make_incoming(msg_id="c2"), "s2")++ affected = await mgr.mark_node_error("root", "something failed")+ # root + c1 + c2 should all be marked+ assert len(affected) >= 1+ root = tree.get_node("root")+ assert root.state == MessageState.ERROR++ @pytest.mark.asyncio+ async def test_mark_node_error_nonexistent(self):+ """mark_node_error for nonexistent node returns empty."""+ mgr = TreeQueueManager()+ result = await mgr.mark_node_error("nonexistent", "err")+ assert result == []++ @pytest.mark.asyncio+ async def test_get_tree_for_node(self):+ """get_tree_for_node returns the right tree."""+ mgr = TreeQueueManager()+ await mgr.create_tree("root", _make_incoming(msg_id="root"), "s_root")+ _, _ = await mgr.add_to_tree("root", "c1", _make_incoming(msg_id="c1"), "s1")++ tree = mgr.get_tree_for_node("c1")+ assert tree is not None+ assert tree.root_id == "root"++ def test_get_tree_for_node_nonexistent(self):+ """get_tree_for_node returns None for unknown nodes."""+ mgr = TreeQueueManager()+ assert mgr.get_tree_for_node("nonexistent") is None++ @pytest.mark.asyncio+ async def test_enqueue_no_tree(self):+ """Enqueue for a node not in any tree returns False."""+ mgr = TreeQueueManager()++ async def dummy(nid, node):+ pass++ result = await mgr.enqueue("nonexistent", dummy)+ assert result is False++ @pytest.mark.asyncio+ async def test_serialization_round_trip(self):+ """TreeQueueManager should survive serialization round-trip."""+ mgr = TreeQueueManager()+ await mgr.create_tree("root", _make_incoming(msg_id="root"), "s_root")+ _, _ = await mgr.add_to_tree("root", "c1", _make_incoming(msg_id="c1"), "s1")++ data = mgr.to_dict()+ restored = TreeQueueManager.from_dict(data)++ assert restored.get_tree_count() == 1+ assert restored.get_node("c1") is not None++ @pytest.mark.asyncio+ async def test_rapid_messages_all_queued(self):+ """Rapid sequential enqueues should all be queued without loss."""+ mgr = TreeQueueManager()+ tree = await mgr.create_tree("root", _make_incoming(msg_id="root"), "s_root")++ # Add 10 child nodes+ for i in range(10):+ await mgr.add_to_tree(+ "root", f"c{i}", _make_incoming(msg_id=f"c{i}"), f"s{i}"+ )++ blocker = asyncio.Event()++ async def blocking_processor(nid, node):+ await blocker.wait()++ # Start processing root (blocks)+ await mgr.enqueue("root", blocking_processor)+ await asyncio.sleep(0.05) # Let task start++ # Rapidly enqueue all children+ results = []+ for i in range(10):+ r = await mgr.enqueue(f"c{i}", blocking_processor)+ results.append(r)++ # All should be queued (True)+ assert all(r is True for r in results)+ assert tree.get_queue_size() == 10++ # Cleanup+ blocker.set()+ await asyncio.sleep(0.1)++ @pytest.mark.asyncio+ async def test_concurrent_trees_independent(self):+ """Processing in one tree shouldn't affect another."""+ mgr = TreeQueueManager()+ await mgr.create_tree("t1", _make_incoming(msg_id="t1"), "s1")+ await mgr.create_tree("t2", _make_incoming(msg_id="t2"), "s2")++ processed = []++ async def processor(nid, node):+ processed.append(nid)++ # Process both trees+ await mgr.enqueue("t1", processor)+ await mgr.enqueue("t2", processor)+ await asyncio.sleep(0.2)++ assert "t1" in processed+ assert "t2" in processed++ @pytest.mark.asyncio+ async def test_callbacks_invoked(self):+ """Queue update and node started callbacks should fire."""+ queue_updates = []+ node_starts = []++ async def on_queue_update(tree):+ queue_updates.append(tree.root_id)++ async def on_node_started(tree, node_id):+ node_starts.append(node_id)++ mgr = TreeQueueManager(+ queue_update_callback=on_queue_update,+ node_started_callback=on_node_started,+ )+ tree = await mgr.create_tree("root", _make_incoming(msg_id="root"), "s_root")+ _, _ = await mgr.add_to_tree("root", "c1", _make_incoming(msg_id="c1"), "s1")++ blocker = asyncio.Event()++ async def slow_proc(nid, node):+ if nid == "root":+ blocker.set()+ await asyncio.sleep(0.1)++ # Process root then c1 should be dequeued+ await mgr.enqueue("root", slow_proc)+ await blocker.wait()+ await mgr.enqueue("c1", slow_proc)+ await asyncio.sleep(0.5)++ # c1 was dequeued from queue, so callbacks should have fired+ assert len(queue_updates) >= 1 or len(node_starts) >= 1