From 7fa4d12e3044d09df17a9125ae916e60509189ef Mon Sep 17 00:00:00 2001 From: aserrador Date: Wed, 10 Jun 2026 15:37:37 -0500 Subject: [PATCH] fix: test --- tests/test_agent.py | 89 +++++++++++++++++++++------------------------ 1 file changed, 42 insertions(+), 47 deletions(-) diff --git a/tests/test_agent.py b/tests/test_agent.py index fa19296..5cb3eba 100644 --- a/tests/test_agent.py +++ b/tests/test_agent.py @@ -4,7 +4,6 @@ from src.naliiabot.bot.agent.agent import Agent from langchain_core.messages import HumanMessage, AIMessage, ToolMessage from langgraph.graph import END - """ Tests unitarios robustos para el Agente. @@ -32,7 +31,7 @@ class TestAgentConstruction: agent = Agent(model=mock_model) - assert agent.config.max_iterations == 10 + assert agent.config.max_iterations == 100 assert "helpful assistant" in agent.config.system_prompt assert agent.tools == [] @@ -113,6 +112,7 @@ class TestGraphStructure: # TESTS DE COMPORTAMIENTO DE NODOS # ============================================================================ + class TestLLMNode: """Tests del nodo de llamada al LLM.""" @@ -126,10 +126,7 @@ class TestLLMNode: mock_response = AIMessage(content="Hola") agent._model.invoke = Mock(return_value=mock_response) - state = { - "messages": [HumanMessage(content="Hi")], - "llm_calls": 2 - } + state = {"messages": [HumanMessage(content="Hi")], "llm_calls": 2} result = agent._llm_call_node(state) @@ -166,7 +163,7 @@ class TestLLMNode: state = { "messages": [HumanMessage(content="Hi")], - "llm_calls": 5 # Ya en límite + "llm_calls": 5, # Ya en límite } with pytest.raises(RuntimeError, match="Max iterations"): @@ -203,11 +200,13 @@ class TestToolNode: ai_msg = AIMessage( content="", - tool_calls=[{ - "name": "calculator", - "args": {"x": 1, "y": 2}, - "id": "call_123" - }] + tool_calls=[ + { + "name": "calculator", + "args": {"x": 1, "y": 2}, + "id": "call_123", + } + ], ) state = {"messages": [HumanMessage(content="Calc"), ai_msg]} @@ -227,11 +226,9 @@ class TestToolNode: ai_msg = AIMessage( content="", - tool_calls=[{ - "name": "unknown_tool", - "args": {}, - "id": "call_123" - }] + tool_calls=[ + {"name": "unknown_tool", "args": {}, "id": "call_123"} + ], ) state = {"messages": [ai_msg]} @@ -254,7 +251,9 @@ class TestToolNode: ai_msg = AIMessage( content="", - tool_calls=[{"name": "calculator", "args": {}, "id": "call_123"}] + tool_calls=[ + {"name": "calculator", "args": {}, "id": "call_123"} + ], ) state = {"messages": [ai_msg]} @@ -276,11 +275,9 @@ class TestToolNode: ai_msg = AIMessage( content="", - tool_calls=[{ - "name": "calculator", - "args": {}, - "id": "call_123" - }] + tool_calls=[ + {"name": "calculator", "args": {}, "id": "call_123"} + ], ) state = {"messages": [ai_msg]} @@ -314,7 +311,7 @@ class TestShouldContinue: """ ai_msg = AIMessage( content="", - tool_calls=[{"name": "calc", "args": {}, "id": "1"}] + tool_calls=[{"name": "calc", "args": {}, "id": "1"}], ) state = {"messages": [ai_msg]} @@ -354,6 +351,7 @@ class TestShouldContinue: # TESTS DE INTEGRACIÓN CON FLUJO COMPLETO # ============================================================================ + class TestIntegrationFlows: """Tests de flujos completos con mocks controlados.""" @@ -363,12 +361,12 @@ class TestIntegrationFlows: CUANDO: Usuario pregunta algo simple ENTONCES: Flujo: START → llm_call → END """ - final_response = AIMessage(content="¡Hola! ¿En qué puedo ayudarte?") + final_response = AIMessage( + content="¡Hola! ¿En qué puedo ayudarte?" + ) agent._model.invoke = Mock(return_value=final_response) - initial_state = { - "messages": [HumanMessage(content="Hola")] - } + initial_state = {"messages": [HumanMessage(content="Hola")]} result = agent.invoke(initial_state) @@ -377,7 +375,8 @@ class TestIntegrationFlows: assert result["llm_calls"] == 1 assert len(result["messages"]) == 2 assert result["messages"][-1].content == ( - "¡Hola! ¿En qué puedo ayudarte?") + "¡Hola! ¿En qué puedo ayudarte?" + ) def test_tool_use_flow(self, agent_with_tool): """ @@ -388,23 +387,22 @@ class TestIntegrationFlows: # Primera llamada: LLM decide usar tool first_response = AIMessage( content="", - tool_calls=[{ - "name": "calculator", - "args": {"expr": "2+2"}, - "id": "calc_1" - }] + tool_calls=[ + { + "name": "calculator", + "args": {"expr": "2+2"}, + "id": "calc_1", + } + ], ) # Segunda llamada: LLM responde con resultado second_response = AIMessage(content="El resultado es 42") - agent_with_tool._model.invoke = Mock(side_effect=[ - first_response, - second_response - ]) + agent_with_tool._model.invoke = Mock( + side_effect=[first_response, second_response] + ) - initial_state = { - "messages": [HumanMessage(content="Calcula 2+2")] - } + initial_state = {"messages": [HumanMessage(content="Calcula 2+2")]} result = agent_with_tool.invoke(initial_state) @@ -431,6 +429,7 @@ class TestIntegrationFlows: # TESTS DE EJECUCIÓN DE TOOLS (UNITARIO DETALLADO) # ============================================================================ + class TestToolExecution: """Tests específicos del método _execute_tool.""" @@ -443,7 +442,7 @@ class TestToolExecution: tool_call = { "name": "calculator", "args": {"x": 10}, - "id": "call_123" + "id": "call_123", } result = agent_with_tool._execute_tool(tool_call) @@ -459,11 +458,7 @@ class TestToolExecution: CUANDO: Ejecuto _execute_tool ENTONCES: Retorna ToolMessage con error """ - tool_call = { - "name": "nonexistent", - "args": {}, - "id": "call_404" - } + tool_call = {"name": "nonexistent", "args": {}, "id": "call_404"} result = agent._execute_tool(tool_call)