|
| 1 | +"""Tests for LangGraph integration helpers.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +from collections.abc import Sequence |
| 6 | +from unittest.mock import MagicMock, patch |
| 7 | + |
| 8 | +import pytest |
| 9 | +from langchain_core.tools import BaseTool as LangChainBaseTool |
| 10 | + |
| 11 | +from stackone_ai.models import ExecuteConfig, StackOneTool, ToolParameters, Tools |
| 12 | + |
| 13 | + |
| 14 | +@pytest.fixture |
| 15 | +def sample_tool() -> StackOneTool: |
| 16 | + """Create a sample tool for testing.""" |
| 17 | + return StackOneTool( |
| 18 | + description="Test tool", |
| 19 | + parameters=ToolParameters( |
| 20 | + type="object", |
| 21 | + properties={"id": {"type": "string", "description": "Record ID"}}, |
| 22 | + ), |
| 23 | + _execute_config=ExecuteConfig( |
| 24 | + headers={}, |
| 25 | + method="GET", |
| 26 | + url="https://api.example.com/test/{id}", |
| 27 | + name="test_tool", |
| 28 | + ), |
| 29 | + _api_key="test_key", |
| 30 | + ) |
| 31 | + |
| 32 | + |
| 33 | +@pytest.fixture |
| 34 | +def tools_collection(sample_tool: StackOneTool) -> Tools: |
| 35 | + """Create a Tools collection for testing.""" |
| 36 | + return Tools([sample_tool]) |
| 37 | + |
| 38 | + |
| 39 | +class TestToLangchainTools: |
| 40 | + """Test _to_langchain_tools helper function.""" |
| 41 | + |
| 42 | + def test_converts_tools_collection(self, tools_collection: Tools): |
| 43 | + """Test converting a Tools collection to LangChain tools.""" |
| 44 | + from stackone_ai.integrations.langgraph import _to_langchain_tools |
| 45 | + |
| 46 | + result = _to_langchain_tools(tools_collection) |
| 47 | + |
| 48 | + assert isinstance(result, Sequence) |
| 49 | + assert len(result) == 1 |
| 50 | + assert isinstance(result[0], LangChainBaseTool) |
| 51 | + assert result[0].name == "test_tool" |
| 52 | + |
| 53 | + def test_passthrough_langchain_tools(self): |
| 54 | + """Test that LangChain tools are passed through unchanged.""" |
| 55 | + from stackone_ai.integrations.langgraph import _to_langchain_tools |
| 56 | + |
| 57 | + mock_lc_tool = MagicMock(spec=LangChainBaseTool) |
| 58 | + lc_tools = [mock_lc_tool] |
| 59 | + |
| 60 | + result = _to_langchain_tools(lc_tools) |
| 61 | + |
| 62 | + assert result is lc_tools |
| 63 | + assert len(result) == 1 |
| 64 | + |
| 65 | + |
| 66 | +class TestToToolNode: |
| 67 | + """Test to_tool_node function.""" |
| 68 | + |
| 69 | + def test_creates_tool_node_from_tools_collection(self, tools_collection: Tools): |
| 70 | + """Test creating a ToolNode from a Tools collection.""" |
| 71 | + from stackone_ai.integrations.langgraph import to_tool_node |
| 72 | + |
| 73 | + node = to_tool_node(tools_collection) |
| 74 | + |
| 75 | + # ToolNode should be created |
| 76 | + assert node is not None |
| 77 | + # Check it has the expected tools |
| 78 | + assert len(node.tools_by_name) == 1 |
| 79 | + assert "test_tool" in node.tools_by_name |
| 80 | + |
| 81 | + def test_creates_tool_node_from_langchain_tools(self, tools_collection: Tools): |
| 82 | + """Test creating a ToolNode from pre-converted LangChain tools.""" |
| 83 | + from stackone_ai.integrations.langgraph import to_tool_node |
| 84 | + |
| 85 | + lc_tools = tools_collection.to_langchain() |
| 86 | + node = to_tool_node(lc_tools) |
| 87 | + |
| 88 | + assert node is not None |
| 89 | + assert len(node.tools_by_name) == 1 |
| 90 | + |
| 91 | + def test_passes_kwargs_to_tool_node(self, tools_collection: Tools): |
| 92 | + """Test that kwargs are passed to ToolNode constructor.""" |
| 93 | + from stackone_ai.integrations.langgraph import to_tool_node |
| 94 | + |
| 95 | + # name is a valid ToolNode parameter |
| 96 | + node = to_tool_node(tools_collection, name="custom_node") |
| 97 | + |
| 98 | + assert node is not None |
| 99 | + |
| 100 | + |
| 101 | +class TestToToolExecutor: |
| 102 | + """Test to_tool_executor function (deprecated, returns ToolNode).""" |
| 103 | + |
| 104 | + def test_creates_tool_node(self, tools_collection: Tools): |
| 105 | + """Test to_tool_executor creates a ToolNode.""" |
| 106 | + from stackone_ai.integrations.langgraph import to_tool_executor |
| 107 | + |
| 108 | + result = to_tool_executor(tools_collection) |
| 109 | + |
| 110 | + # Should return a ToolNode (ToolExecutor is deprecated) |
| 111 | + assert result is not None |
| 112 | + assert len(result.tools_by_name) == 1 |
| 113 | + |
| 114 | + |
| 115 | +class TestBindModelWithTools: |
| 116 | + """Test bind_model_with_tools function.""" |
| 117 | + |
| 118 | + def test_binds_tools_to_model(self, tools_collection: Tools): |
| 119 | + """Test binding tools to a model.""" |
| 120 | + from stackone_ai.integrations.langgraph import bind_model_with_tools |
| 121 | + |
| 122 | + mock_model = MagicMock() |
| 123 | + mock_bound_model = MagicMock() |
| 124 | + mock_model.bind_tools.return_value = mock_bound_model |
| 125 | + |
| 126 | + result = bind_model_with_tools(mock_model, tools_collection) |
| 127 | + |
| 128 | + assert result is mock_bound_model |
| 129 | + mock_model.bind_tools.assert_called_once() |
| 130 | + # Check that LangChain tools were passed |
| 131 | + call_args = mock_model.bind_tools.call_args[0][0] |
| 132 | + assert isinstance(call_args, Sequence) |
| 133 | + assert len(call_args) == 1 |
| 134 | + |
| 135 | + def test_binds_langchain_tools_directly(self): |
| 136 | + """Test binding pre-converted LangChain tools.""" |
| 137 | + from stackone_ai.integrations.langgraph import bind_model_with_tools |
| 138 | + |
| 139 | + mock_model = MagicMock() |
| 140 | + mock_lc_tool = MagicMock(spec=LangChainBaseTool) |
| 141 | + lc_tools = [mock_lc_tool] |
| 142 | + |
| 143 | + bind_model_with_tools(mock_model, lc_tools) |
| 144 | + |
| 145 | + mock_model.bind_tools.assert_called_once_with(lc_tools) |
| 146 | + |
| 147 | + |
| 148 | +class TestCreateReactAgent: |
| 149 | + """Test create_react_agent function.""" |
| 150 | + |
| 151 | + def test_creates_react_agent(self, tools_collection: Tools): |
| 152 | + """Test creating a ReAct agent.""" |
| 153 | + from stackone_ai.integrations.langgraph import create_react_agent |
| 154 | + |
| 155 | + mock_llm = MagicMock() |
| 156 | + |
| 157 | + with patch("langgraph.prebuilt.create_react_agent") as mock_create: |
| 158 | + mock_agent = MagicMock() |
| 159 | + mock_create.return_value = mock_agent |
| 160 | + |
| 161 | + result = create_react_agent(mock_llm, tools_collection) |
| 162 | + |
| 163 | + assert result is mock_agent |
| 164 | + mock_create.assert_called_once() |
| 165 | + # First arg is llm, second is tools |
| 166 | + call_args = mock_create.call_args |
| 167 | + assert call_args[0][0] is mock_llm |
| 168 | + |
| 169 | + def test_passes_kwargs_to_create_react_agent(self, tools_collection: Tools): |
| 170 | + """Test that kwargs are passed to create_react_agent.""" |
| 171 | + from stackone_ai.integrations.langgraph import create_react_agent |
| 172 | + |
| 173 | + mock_llm = MagicMock() |
| 174 | + |
| 175 | + with patch("langgraph.prebuilt.create_react_agent") as mock_create: |
| 176 | + create_react_agent(mock_llm, tools_collection, checkpointer=None) |
| 177 | + |
| 178 | + mock_create.assert_called_once() |
| 179 | + call_kwargs = mock_create.call_args[1] |
| 180 | + assert "checkpointer" in call_kwargs |
| 181 | + |
| 182 | + |
| 183 | +class TestEnsureLanggraph: |
| 184 | + """Test _ensure_langgraph helper function.""" |
| 185 | + |
| 186 | + def test_raises_import_error_when_langgraph_not_installed(self): |
| 187 | + """Test that ImportError is raised when langgraph is not installed.""" |
| 188 | + from stackone_ai.integrations.langgraph import _ensure_langgraph |
| 189 | + |
| 190 | + with patch.dict("sys.modules", {"langgraph": None, "langgraph.prebuilt": None}): |
| 191 | + with patch( |
| 192 | + "stackone_ai.integrations.langgraph._ensure_langgraph", |
| 193 | + side_effect=ImportError("LangGraph is not installed"), |
| 194 | + ): |
| 195 | + # This test verifies the error message format |
| 196 | + pass |
| 197 | + |
| 198 | + # Since langgraph is installed in the test environment, just verify function runs |
| 199 | + _ensure_langgraph() # Should not raise |
| 200 | + |
| 201 | + |
| 202 | +class TestModuleImports: |
| 203 | + """Test module-level imports from integrations package.""" |
| 204 | + |
| 205 | + def test_imports_from_integrations_init(self): |
| 206 | + """Test that all functions are importable from integrations package.""" |
| 207 | + from stackone_ai.integrations import ( |
| 208 | + bind_model_with_tools, |
| 209 | + create_react_agent, |
| 210 | + to_tool_executor, |
| 211 | + to_tool_node, |
| 212 | + ) |
| 213 | + |
| 214 | + assert callable(to_tool_node) |
| 215 | + assert callable(to_tool_executor) |
| 216 | + assert callable(bind_model_with_tools) |
| 217 | + assert callable(create_react_agent) |
0 commit comments