From f18c9c1960a3eefba553943e65ce509da8378002 Mon Sep 17 00:00:00 2001 From: Javier Martinez Date: Tue, 14 Jul 2026 16:15:58 +0200 Subject: [PATCH] test: isolate chat mocks and cancellation timing --- tests/fixtures/mock_injector.py | 3 ++- tests/server/chat/anthropic/test_anthropic_client.py | 1 + tests/server/chat/test_chat_routes.py | 2 +- 3 files changed, 4 insertions(+), 2 deletions(-) diff --git a/tests/fixtures/mock_injector.py b/tests/fixtures/mock_injector.py index c73d2b0d..eb916966 100644 --- a/tests/fixtures/mock_injector.py +++ b/tests/fixtures/mock_injector.py @@ -3,7 +3,7 @@ from typing import Any from unittest.mock import MagicMock import pytest -from injector import Provider, ScopeDecorator, singleton +from injector import Provider, ScopeDecorator, SingletonScope, singleton from private_gpt.di import create_application_injector from private_gpt.settings.settings import Settings, unsafe_settings @@ -25,6 +25,7 @@ class MockInjector: if mock is None: mock = MagicMock() self.test_injector.binder.bind(interface, to=mock, scope=scope) + self.test_injector.get(SingletonScope)._context.pop(interface, None) return mock # type: ignore def bind_settings(self, settings: dict[str, Any]) -> Settings: diff --git a/tests/server/chat/anthropic/test_anthropic_client.py b/tests/server/chat/anthropic/test_anthropic_client.py index e1170a41..fb9e3f7f 100644 --- a/tests/server/chat/anthropic/test_anthropic_client.py +++ b/tests/server/chat/anthropic/test_anthropic_client.py @@ -692,6 +692,7 @@ def test_messages_count_tokens_endpoint_forwards_message_input_and_options( test_client: TestClient, httpx_mock: HTTPXMock, ) -> None: + injector.get(ChatService) chat_service = Mock() chat_service.count_tokens = AsyncMock( return_value=CountTokensOutput(input_tokens=11) diff --git a/tests/server/chat/test_chat_routes.py b/tests/server/chat/test_chat_routes.py index 3ec64ef6..16ece9c1 100644 --- a/tests/server/chat/test_chat_routes.py +++ b/tests/server/chat/test_chat_routes.py @@ -2233,7 +2233,7 @@ async def test_chat_cancels_llm_astream_on_client_disconnection( await request_task try: - await asyncio.wait_for(llm_generator_closed.wait(), timeout=2.0) + await asyncio.wait_for(llm_generator_closed.wait(), timeout=5.0) except TimeoutError: pytest.fail( "LLM astream_chat_with_tools generator was not closed after client disconnection"