From c5d2329a61186edc93a3ea866090e2c2970359f1 Mon Sep 17 00:00:00 2001 From: Alex Date: Sat, 8 Aug 2026 12:28:20 +0100 Subject: [PATCH] fix: more test fixes --- application/llm/anthropic.py | 7 +++--- .../services/test_stream_processor_utils.py | 25 ++++++++++++++++--- .../api/answer/test_source_access_control.py | 15 +++++++++++ 3 files changed, 39 insertions(+), 8 deletions(-) diff --git a/application/llm/anthropic.py b/application/llm/anthropic.py index 21fc818c..7944dff6 100644 --- a/application/llm/anthropic.py +++ b/application/llm/anthropic.py @@ -383,10 +383,9 @@ class AnthropicLLM(BaseLLM): return try: output = int(getattr(usage, "output_tokens", 0) or 0) - if output_only: - # Input counts come from the message_start record below. - base_input = created = cached = 0 - else: + # On a message_delta the input bins are absent; the counts kept at + # message_start are reused below instead. + if not output_only: base_input = int(getattr(usage, "input_tokens", 0) or 0) created = int(getattr(usage, "cache_creation_input_tokens", 0) or 0) cached = int(getattr(usage, "cache_read_input_tokens", 0) or 0) diff --git a/tests/api/answer/services/test_stream_processor_utils.py b/tests/api/answer/services/test_stream_processor_utils.py index 629518d0..b93ac3af 100644 --- a/tests/api/answer/services/test_stream_processor_utils.py +++ b/tests/api/answer/services/test_stream_processor_utils.py @@ -9,6 +9,8 @@ from unittest.mock import patch import pytest +import application.api.answer.services.stream_processor as sp_mod + @contextmanager def _patch_db(conn): @@ -350,6 +352,23 @@ class TestGetAgentKey: with _patch_db(pg_conn), pytest.raises(Exception): sp._get_agent_key(str(agent["id"]), "not-owner") +def _stub_db_readonly(monkeypatch, sp_mod): + """``_load_request_sources`` opens a connection to run the access check. + + Patch the name bound in ``stream_processor`` — it is imported at module + load, so rebinding the source module has no effect. Without this these + pass only where a database happens to be reachable. + """ + import contextlib + from unittest.mock import MagicMock + + @contextlib.contextmanager + def _conn(): + yield MagicMock() + + monkeypatch.setattr(sp_mod, "db_readonly", _conn) + + class TestConfigureSource: def test_agent_data_with_sources_list(self): @@ -385,9 +404,9 @@ class TestConfigureSource: assert sp.source == {} def test_request_active_docs_used(self, monkeypatch): - import application.api.answer.services.stream_processor as sp_mod StreamProcessor = sp_mod.StreamProcessor + _stub_db_readonly(monkeypatch, sp_mod) monkeypatch.setattr(sp_mod, "can_access", lambda *a, **k: True) sp = StreamProcessor({"active_docs": "abc"}, {"sub": "u"}) sp._configure_source() @@ -399,7 +418,6 @@ class TestConfigureSource: An unchecked id read another tenant's documents straight into the answer, while /api/sources//search correctly refused the same id. """ - import application.api.answer.services.stream_processor as sp_mod StreamProcessor = sp_mod.StreamProcessor monkeypatch.setattr(sp_mod, "can_access", lambda *a, **k: False) @@ -409,9 +427,9 @@ class TestConfigureSource: assert sp.all_sources == [] def test_mixed_access_keeps_only_the_readable_ids(self, monkeypatch): - import application.api.answer.services.stream_processor as sp_mod StreamProcessor = sp_mod.StreamProcessor + _stub_db_readonly(monkeypatch, sp_mod) monkeypatch.setattr( sp_mod, "can_access", lambda conn, kind, sid, user: sid == "mine" ) @@ -420,7 +438,6 @@ class TestConfigureSource: assert sp.source == {"active_docs": ["mine"]} def test_access_check_failure_fails_closed(self, monkeypatch): - import application.api.answer.services.stream_processor as sp_mod StreamProcessor = sp_mod.StreamProcessor def _boom(*a, **k): diff --git a/tests/api/answer/test_source_access_control.py b/tests/api/answer/test_source_access_control.py index 27426556..461dbd56 100644 --- a/tests/api/answer/test_source_access_control.py +++ b/tests/api/answer/test_source_access_control.py @@ -25,6 +25,21 @@ TEAM_SHARED = "src-team-shared" FOREIGN = "src-foreign" +@pytest.fixture(autouse=True) +def _stub_db(monkeypatch): + """``_load_request_sources`` opens a connection; it needn't be a real one.""" + import contextlib + from unittest.mock import MagicMock + + @contextlib.contextmanager + def _conn(): + yield MagicMock() + + # Patch the name bound in stream_processor, not the source module: it is + # imported at module load, so rebinding the origin has no effect. + monkeypatch.setattr(sp_mod, "db_readonly", _conn) + + @pytest.fixture def access_model(monkeypatch): """``can_access`` semantics: own it, or hold a direct team grant."""