mirror of
https://github.com/headroomlabs-ai/headroom.git
synced 2026-11-22 13:29:19 +08:00
test(integrations): drop duplicated LangChain and LiteLLM tests
test_extended.py repeated tests for HeadroomChatMessageHistory, HeadroomDocumentCompressor, HeadroomToolWrapper and the streaming tracker that the per-module LangChain test files already cover. Line and branch coverage of headroom/integrations/langchain is identical with and without them (1150 statements, 161 missed, 52 partial in both runs). Kept: test_call_compresses_large_json is the only LangChain test that runs compress_tool_result unmocked, and the only one in that suite that reaches the non-JSON fallback in mcp/server.py. Moved into test_streaming.py and test_memory.py: the output-token and below-threshold tests, the only ones that run memory and streaming with the real default provider. The LiteLLM tests removed only asserted hasattr, callable or not-None; the remaining tests call and await the same hooks. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
a9815bbb5f
commit
fc66c0bc78
@@ -2,11 +2,9 @@
|
||||
|
||||
Tests cover:
|
||||
1. langchain_providers - Provider auto-detection
|
||||
2. langchain_memory - HeadroomChatMessageHistory
|
||||
3. langchain_retriever - HeadroomDocumentCompressor
|
||||
4. langchain_agents - HeadroomToolWrapper
|
||||
5. langchain_langsmith - LangSmith integration
|
||||
6. langchain_streaming - Streaming metrics
|
||||
2. langchain_agents - HeadroomToolWrapper end-to-end compression
|
||||
3. langchain_langsmith - LangSmith integration
|
||||
4. HeadroomChatModel provider auto-detection
|
||||
"""
|
||||
|
||||
import json
|
||||
@@ -16,9 +14,7 @@ import pytest
|
||||
|
||||
# Check if LangChain is available
|
||||
try:
|
||||
from langchain_core.documents import Document
|
||||
from langchain_core.messages import AIMessage, HumanMessage
|
||||
from langchain_core.tools import StructuredTool
|
||||
import langchain_core # noqa: F401
|
||||
|
||||
LANGCHAIN_AVAILABLE = True
|
||||
except ImportError:
|
||||
@@ -132,225 +128,8 @@ class TestProviderDetection:
|
||||
assert name == "gpt-4o" # Default for OpenAI
|
||||
|
||||
|
||||
class TestHeadroomChatMessageHistory:
|
||||
"""Tests for HeadroomChatMessageHistory memory wrapper."""
|
||||
|
||||
def test_init(self):
|
||||
"""Initialize with base history."""
|
||||
from headroom.integrations.langchain.memory import HeadroomChatMessageHistory
|
||||
|
||||
mock_history = MagicMock()
|
||||
mock_history.messages = []
|
||||
|
||||
wrapper = HeadroomChatMessageHistory(
|
||||
mock_history,
|
||||
compress_threshold_tokens=4000,
|
||||
keep_recent_turns=5,
|
||||
)
|
||||
|
||||
assert wrapper._base is mock_history
|
||||
assert wrapper._threshold == 4000
|
||||
assert wrapper._keep_recent_turns == 5
|
||||
|
||||
def test_messages_passthrough_under_threshold(self):
|
||||
"""Messages pass through when under threshold."""
|
||||
from headroom.integrations.langchain.memory import HeadroomChatMessageHistory
|
||||
|
||||
mock_history = MagicMock()
|
||||
mock_history.messages = [
|
||||
HumanMessage(content="Hello"),
|
||||
AIMessage(content="Hi there!"),
|
||||
]
|
||||
|
||||
wrapper = HeadroomChatMessageHistory(
|
||||
mock_history,
|
||||
compress_threshold_tokens=10000, # High threshold
|
||||
)
|
||||
|
||||
messages = wrapper.messages
|
||||
assert len(messages) == 2
|
||||
assert messages[0].content == "Hello"
|
||||
|
||||
def test_add_message_delegates(self):
|
||||
"""add_message delegates to base history."""
|
||||
from headroom.integrations.langchain.memory import HeadroomChatMessageHistory
|
||||
|
||||
mock_history = MagicMock()
|
||||
mock_history.messages = []
|
||||
|
||||
wrapper = HeadroomChatMessageHistory(mock_history)
|
||||
message = HumanMessage(content="Test")
|
||||
wrapper.add_message(message)
|
||||
|
||||
mock_history.add_message.assert_called_once_with(message)
|
||||
|
||||
def test_clear_delegates(self):
|
||||
"""clear delegates to base history."""
|
||||
from headroom.integrations.langchain.memory import HeadroomChatMessageHistory
|
||||
|
||||
mock_history = MagicMock()
|
||||
mock_history.messages = []
|
||||
|
||||
wrapper = HeadroomChatMessageHistory(mock_history)
|
||||
wrapper.clear()
|
||||
|
||||
mock_history.clear.assert_called_once()
|
||||
|
||||
def test_get_compression_stats(self):
|
||||
"""Get compression statistics."""
|
||||
from headroom.integrations.langchain.memory import HeadroomChatMessageHistory
|
||||
|
||||
mock_history = MagicMock()
|
||||
mock_history.messages = []
|
||||
|
||||
wrapper = HeadroomChatMessageHistory(mock_history)
|
||||
stats = wrapper.get_compression_stats()
|
||||
|
||||
assert "compression_count" in stats
|
||||
assert "total_tokens_saved" in stats
|
||||
assert stats["compression_count"] == 0
|
||||
|
||||
|
||||
class TestHeadroomDocumentCompressor:
|
||||
"""Tests for HeadroomDocumentCompressor retriever integration."""
|
||||
|
||||
def test_init(self):
|
||||
"""Initialize with defaults."""
|
||||
from headroom.integrations.langchain.retriever import HeadroomDocumentCompressor
|
||||
|
||||
compressor = HeadroomDocumentCompressor()
|
||||
|
||||
assert compressor.max_documents == 10
|
||||
assert compressor.min_relevance == 0.0
|
||||
assert compressor.prefer_diverse is False
|
||||
|
||||
def test_init_custom(self):
|
||||
"""Initialize with custom settings."""
|
||||
from headroom.integrations.langchain.retriever import HeadroomDocumentCompressor
|
||||
|
||||
compressor = HeadroomDocumentCompressor(
|
||||
max_documents=5,
|
||||
min_relevance=0.5,
|
||||
prefer_diverse=True,
|
||||
)
|
||||
|
||||
assert compressor.max_documents == 5
|
||||
assert compressor.min_relevance == 0.5
|
||||
assert compressor.prefer_diverse is True
|
||||
|
||||
def test_compress_passthrough_under_limit(self):
|
||||
"""Pass through when under max_documents."""
|
||||
from headroom.integrations.langchain.retriever import HeadroomDocumentCompressor
|
||||
|
||||
compressor = HeadroomDocumentCompressor(max_documents=10)
|
||||
|
||||
docs = [
|
||||
Document(page_content="Python is a programming language."),
|
||||
Document(page_content="JavaScript runs in browsers."),
|
||||
]
|
||||
|
||||
result = compressor.compress_documents(docs, "What is Python?")
|
||||
|
||||
assert len(result) == 2
|
||||
|
||||
def test_compress_reduces_to_max(self):
|
||||
"""Compress when over max_documents."""
|
||||
from headroom.integrations.langchain.retriever import HeadroomDocumentCompressor
|
||||
|
||||
compressor = HeadroomDocumentCompressor(max_documents=2)
|
||||
|
||||
docs = [
|
||||
Document(page_content="Python is a programming language."),
|
||||
Document(page_content="Java is also a language."),
|
||||
Document(page_content="Weather today is sunny."),
|
||||
Document(page_content="Cats are cute animals."),
|
||||
]
|
||||
|
||||
result = compressor.compress_documents(docs, "programming language")
|
||||
|
||||
assert len(result) == 2
|
||||
|
||||
def test_compress_prefers_relevant(self):
|
||||
"""Keep most relevant documents."""
|
||||
from headroom.integrations.langchain.retriever import HeadroomDocumentCompressor
|
||||
|
||||
compressor = HeadroomDocumentCompressor(max_documents=1)
|
||||
|
||||
docs = [
|
||||
Document(page_content="Weather today is sunny."),
|
||||
Document(page_content="Python programming tutorial basics."),
|
||||
Document(page_content="Cats are cute animals."),
|
||||
]
|
||||
|
||||
result = compressor.compress_documents(docs, "Python tutorial")
|
||||
|
||||
assert len(result) == 1
|
||||
assert "Python" in result[0].page_content
|
||||
|
||||
def test_metrics_tracked(self):
|
||||
"""Compression metrics are tracked."""
|
||||
from headroom.integrations.langchain.retriever import HeadroomDocumentCompressor
|
||||
|
||||
compressor = HeadroomDocumentCompressor(max_documents=2)
|
||||
|
||||
docs = [
|
||||
Document(page_content="Doc 1"),
|
||||
Document(page_content="Doc 2"),
|
||||
Document(page_content="Doc 3"),
|
||||
]
|
||||
|
||||
compressor.compress_documents(docs, "query")
|
||||
|
||||
metrics = compressor.last_metrics
|
||||
assert metrics is not None
|
||||
assert metrics.documents_before == 3
|
||||
assert metrics.documents_after == 2
|
||||
assert metrics.documents_removed == 1
|
||||
|
||||
def test_get_compression_stats(self):
|
||||
"""Get compression statistics."""
|
||||
from headroom.integrations.langchain.retriever import HeadroomDocumentCompressor
|
||||
|
||||
compressor = HeadroomDocumentCompressor(max_documents=1)
|
||||
docs = [Document(page_content="A"), Document(page_content="B")]
|
||||
|
||||
compressor.compress_documents(docs, "A")
|
||||
stats = compressor.get_compression_stats()
|
||||
|
||||
assert "documents_before" in stats
|
||||
assert "documents_after" in stats
|
||||
assert "average_relevance" in stats
|
||||
|
||||
|
||||
class TestHeadroomToolWrapper:
|
||||
"""Tests for HeadroomToolWrapper agent integration."""
|
||||
|
||||
def test_init(self):
|
||||
"""Initialize wrapper."""
|
||||
from headroom.integrations.langchain.agents import HeadroomToolWrapper
|
||||
|
||||
mock_tool = MagicMock()
|
||||
mock_tool.name = "test_tool"
|
||||
mock_tool.description = "A test tool"
|
||||
|
||||
wrapper = HeadroomToolWrapper(mock_tool)
|
||||
|
||||
assert wrapper.name == "test_tool"
|
||||
assert wrapper.description == "A test tool"
|
||||
|
||||
def test_call_passthrough_small_output(self):
|
||||
"""Small outputs pass through without compression."""
|
||||
from headroom.integrations.langchain.agents import HeadroomToolWrapper
|
||||
|
||||
mock_tool = MagicMock()
|
||||
mock_tool.name = "test"
|
||||
mock_tool.description = "test"
|
||||
mock_tool.invoke.return_value = "small result"
|
||||
|
||||
wrapper = HeadroomToolWrapper(mock_tool, min_chars_to_compress=1000)
|
||||
result = wrapper("query")
|
||||
|
||||
assert result == "small result"
|
||||
"""End-to-end HeadroomToolWrapper compression (compress_tool_result not mocked)."""
|
||||
|
||||
def test_call_compresses_large_json(self):
|
||||
"""Large JSON outputs get compressed."""
|
||||
@@ -370,58 +149,6 @@ class TestHeadroomToolWrapper:
|
||||
# Should be smaller after compression
|
||||
assert len(result) <= len(large_output)
|
||||
|
||||
def test_as_langchain_tool(self):
|
||||
"""Convert to LangChain tool."""
|
||||
from headroom.integrations.langchain.agents import HeadroomToolWrapper
|
||||
|
||||
mock_tool = MagicMock()
|
||||
mock_tool.name = "test"
|
||||
mock_tool.description = "test tool"
|
||||
mock_tool.invoke.return_value = "result"
|
||||
|
||||
wrapper = HeadroomToolWrapper(mock_tool)
|
||||
lc_tool = wrapper.as_langchain_tool()
|
||||
|
||||
assert isinstance(lc_tool, StructuredTool)
|
||||
assert lc_tool.name == "test"
|
||||
|
||||
def test_wrap_tools_with_headroom(self):
|
||||
"""Wrap multiple tools at once."""
|
||||
from headroom.integrations.langchain.agents import wrap_tools_with_headroom
|
||||
|
||||
tools = []
|
||||
for i in range(3):
|
||||
mock = MagicMock()
|
||||
mock.name = f"tool_{i}"
|
||||
mock.description = f"Tool {i}"
|
||||
mock.invoke.return_value = "result"
|
||||
tools.append(mock)
|
||||
|
||||
wrapped = wrap_tools_with_headroom(tools)
|
||||
|
||||
assert len(wrapped) == 3
|
||||
assert all(isinstance(t, StructuredTool) for t in wrapped)
|
||||
|
||||
def test_metrics_collector(self):
|
||||
"""Tool metrics are collected."""
|
||||
from headroom.integrations.langchain.agents import (
|
||||
HeadroomToolWrapper,
|
||||
ToolMetricsCollector,
|
||||
)
|
||||
|
||||
collector = ToolMetricsCollector()
|
||||
|
||||
mock_tool = MagicMock()
|
||||
mock_tool.name = "test"
|
||||
mock_tool.description = "test"
|
||||
mock_tool.invoke.return_value = "result"
|
||||
|
||||
wrapper = HeadroomToolWrapper(mock_tool, metrics_collector=collector)
|
||||
wrapper("query")
|
||||
|
||||
assert len(collector.metrics) == 1
|
||||
assert collector.metrics[0].tool_name == "test"
|
||||
|
||||
|
||||
class TestHeadroomLangSmithCallbackHandler:
|
||||
"""Tests for LangSmith integration."""
|
||||
@@ -504,104 +231,6 @@ class TestHeadroomLangSmithCallbackHandler:
|
||||
assert handler._pending_metrics == {}
|
||||
|
||||
|
||||
class TestStreamingMetricsTracker:
|
||||
"""Tests for streaming metrics tracking."""
|
||||
|
||||
def test_init(self):
|
||||
"""Initialize tracker."""
|
||||
from headroom.integrations.langchain.streaming import StreamingMetricsTracker
|
||||
|
||||
tracker = StreamingMetricsTracker(model="gpt-4o")
|
||||
|
||||
assert tracker._model == "gpt-4o"
|
||||
assert tracker._content == ""
|
||||
assert tracker._chunk_count == 0
|
||||
|
||||
def test_add_chunk_string(self):
|
||||
"""Add string chunks."""
|
||||
from headroom.integrations.langchain.streaming import StreamingMetricsTracker
|
||||
|
||||
tracker = StreamingMetricsTracker()
|
||||
tracker.add_chunk("Hello ")
|
||||
tracker.add_chunk("world!")
|
||||
|
||||
assert tracker.content == "Hello world!"
|
||||
assert tracker.chunk_count == 2
|
||||
|
||||
def test_add_chunk_with_content_attr(self):
|
||||
"""Add chunks with content attribute."""
|
||||
from headroom.integrations.langchain.streaming import StreamingMetricsTracker
|
||||
|
||||
tracker = StreamingMetricsTracker()
|
||||
|
||||
chunk1 = MagicMock()
|
||||
chunk1.content = "Hello "
|
||||
chunk2 = MagicMock()
|
||||
chunk2.content = "world!"
|
||||
|
||||
tracker.add_chunk(chunk1)
|
||||
tracker.add_chunk(chunk2)
|
||||
|
||||
assert tracker.content == "Hello world!"
|
||||
|
||||
def test_output_tokens(self):
|
||||
"""Count output tokens."""
|
||||
from headroom.integrations.langchain.streaming import StreamingMetricsTracker
|
||||
|
||||
tracker = StreamingMetricsTracker(model="gpt-4o")
|
||||
tracker.add_chunk("Hello world, this is a test message.")
|
||||
|
||||
tokens = tracker.output_tokens
|
||||
assert tokens > 0
|
||||
|
||||
def test_finish(self):
|
||||
"""Finish tracking and get metrics."""
|
||||
from headroom.integrations.langchain.streaming import StreamingMetricsTracker
|
||||
|
||||
tracker = StreamingMetricsTracker()
|
||||
tracker.add_chunk("Test content")
|
||||
metrics = tracker.finish()
|
||||
|
||||
assert metrics.chunk_count == 1
|
||||
assert metrics.content_length == len("Test content")
|
||||
assert metrics.duration_ms is not None
|
||||
assert metrics.end_time is not None
|
||||
|
||||
def test_reset(self):
|
||||
"""Reset tracker for reuse."""
|
||||
from headroom.integrations.langchain.streaming import StreamingMetricsTracker
|
||||
|
||||
tracker = StreamingMetricsTracker()
|
||||
tracker.add_chunk("Content")
|
||||
tracker.finish()
|
||||
|
||||
tracker.reset()
|
||||
|
||||
assert tracker.content == ""
|
||||
assert tracker.chunk_count == 0
|
||||
|
||||
def test_streaming_metrics_callback(self):
|
||||
"""Test context manager interface."""
|
||||
from headroom.integrations.langchain.streaming import StreamingMetricsCallback
|
||||
|
||||
with StreamingMetricsCallback(model="gpt-4o") as tracker:
|
||||
tracker.add_chunk("Hello")
|
||||
tracker.add_chunk(" world")
|
||||
|
||||
# After context exit, metrics should be available
|
||||
# (accessed via the callback object, not the tracker)
|
||||
|
||||
def test_track_streaming_response(self):
|
||||
"""Track a complete streaming response."""
|
||||
from headroom.integrations.langchain.streaming import track_streaming_response
|
||||
|
||||
chunks = ["Hello ", "world", "!"]
|
||||
content, metrics = track_streaming_response(iter(chunks), model="gpt-4o")
|
||||
|
||||
assert content == "Hello world!"
|
||||
assert metrics.chunk_count == 3
|
||||
|
||||
|
||||
class TestAutoDetectProviderInChatModel:
|
||||
"""Tests for auto_detect_provider in HeadroomChatModel."""
|
||||
|
||||
|
||||
@@ -129,6 +129,20 @@ class TestHeadroomChatMessageHistoryMessages:
|
||||
assert len(messages) == len(sample_langchain_messages)
|
||||
assert history._compression_count == 0
|
||||
|
||||
def test_messages_below_threshold_with_default_provider(self):
|
||||
"""With no provider passed, the default provider counts tokens and
|
||||
short histories pass through."""
|
||||
from headroom.integrations.langchain.memory import HeadroomChatMessageHistory
|
||||
|
||||
base = MagicMock()
|
||||
base.messages = [HumanMessage(content="Hello"), AIMessage(content="Hi there!")]
|
||||
|
||||
history = HeadroomChatMessageHistory(base, compress_threshold_tokens=10000)
|
||||
messages = history.messages
|
||||
|
||||
assert [m.content for m in messages] == ["Hello", "Hi there!"]
|
||||
assert history._compression_count == 0
|
||||
|
||||
def test_messages_compresses_when_over_threshold(self, mock_base_history, mock_provider):
|
||||
"""messages applies compression when over token threshold."""
|
||||
from headroom.integrations.langchain.memory import HeadroomChatMessageHistory
|
||||
|
||||
@@ -356,6 +356,15 @@ class TestStreamingMetricsTrackerProperties:
|
||||
assert tokens == 2
|
||||
mock_provider.get_token_counter.assert_called_with("gpt-4o")
|
||||
|
||||
def test_output_tokens_with_default_provider(self):
|
||||
"""With no provider passed, the default provider's real counter is used."""
|
||||
from headroom.integrations.langchain.streaming import StreamingMetricsTracker
|
||||
|
||||
tracker = StreamingMetricsTracker(model="gpt-4o")
|
||||
tracker.add_chunk("Hello world, this is a test message.")
|
||||
|
||||
assert tracker.output_tokens > 0
|
||||
|
||||
def test_chunk_count_property(self, mock_provider, sample_chunks):
|
||||
"""chunk_count property returns number of chunks."""
|
||||
from headroom.integrations.langchain.streaming import StreamingMetricsTracker
|
||||
|
||||
@@ -3,7 +3,6 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import importlib
|
||||
import inspect
|
||||
from pathlib import Path
|
||||
from unittest.mock import AsyncMock, MagicMock
|
||||
|
||||
@@ -32,17 +31,6 @@ HeadroomCallback = _import_callback()
|
||||
class TestHeadroomCallbackPostCallSuccessHook:
|
||||
"""async_post_call_success_hook must exist and return response unchanged."""
|
||||
|
||||
def test_method_exists(self) -> None:
|
||||
cb = HeadroomCallback()
|
||||
assert hasattr(cb, "async_post_call_success_hook"), (
|
||||
"HeadroomCallback must define async_post_call_success_hook "
|
||||
"for LiteLLM proxy compatibility"
|
||||
)
|
||||
|
||||
def test_method_is_coroutine(self) -> None:
|
||||
cb = HeadroomCallback()
|
||||
assert inspect.iscoroutinefunction(cb.async_post_call_success_hook)
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_returns_response_unchanged(self) -> None:
|
||||
cb = HeadroomCallback()
|
||||
|
||||
@@ -17,23 +17,6 @@ from headroom.integrations.litellm_callback import HeadroomCallback # noqa: E40
|
||||
|
||||
|
||||
class TestHeadroomCallbackCustomLoggerInheritance:
|
||||
def test_instantiates_without_error(self) -> None:
|
||||
cb = HeadroomCallback()
|
||||
assert cb is not None
|
||||
|
||||
def test_has_async_post_call_success_hook(self) -> None:
|
||||
"""Regression: AttributeError: 'HeadroomCallback' has no attr 'async_post_call_success_hook'."""
|
||||
cb = HeadroomCallback()
|
||||
assert hasattr(cb, "async_post_call_success_hook"), (
|
||||
"async_post_call_success_hook must exist (added in litellm 1.89.x)"
|
||||
)
|
||||
|
||||
def test_async_post_call_success_hook_is_callable(self) -> None:
|
||||
"""LiteLLM must be able to await the hook without exception."""
|
||||
cb = HeadroomCallback()
|
||||
hook = cb.async_post_call_success_hook
|
||||
assert callable(hook)
|
||||
|
||||
def test_async_post_call_success_hook_does_not_raise(self) -> None:
|
||||
"""Calling the hook (no-op from CustomLogger) must not raise."""
|
||||
cb = HeadroomCallback()
|
||||
@@ -59,12 +42,6 @@ class TestHeadroomCallbackCustomLoggerInheritance:
|
||||
]
|
||||
assert not missing, f"Missing CustomLogger hooks: {missing}"
|
||||
|
||||
def test_async_pre_call_hook_still_works(self) -> None:
|
||||
"""Inheritance must not break the existing compression hook."""
|
||||
cb = HeadroomCallback()
|
||||
assert hasattr(cb, "async_pre_call_hook")
|
||||
assert callable(cb.async_pre_call_hook)
|
||||
|
||||
def test_total_tokens_saved_property(self) -> None:
|
||||
cb = HeadroomCallback()
|
||||
assert cb.total_tokens_saved == 0
|
||||
|
||||
Reference in New Issue
Block a user