|
1 | 1 | import pytest |
| 2 | + |
2 | 3 | from agentlow.agent import OllamaAgent |
3 | 4 | from agentlow.tools import ToolExecutor |
4 | | -from pathlib import Path |
| 5 | + |
5 | 6 |
|
6 | 7 | @pytest.fixture |
7 | | -def agent(tmp_path): |
8 | | - return OllamaAgent( |
9 | | - model="qwen2.5:7b", # Asume Ollama corriendo en tests |
| 8 | +def agent(tmp_path, monkeypatch): |
| 9 | + """Agente en modo unit-test. |
| 10 | +
|
| 11 | + En CI normalmente NO hay un servidor Ollama ejecutándose. |
| 12 | + Para que los tests sean deterministas y no dependan de red/servicios, |
| 13 | + mockeamos la llamada a Ollama devolviendo un "echo" del prompt. |
| 14 | + """ |
| 15 | + |
| 16 | + a = OllamaAgent( |
| 17 | + model="qwen2.5:7b", |
10 | 18 | work_dir=str(tmp_path), |
11 | | - temperature=0.0 |
| 19 | + temperature=0.0, |
12 | 20 | ) |
13 | 21 |
|
| 22 | + def _fake_call_ollama(messages, tools=None): |
| 23 | + last_user = "" |
| 24 | + for m in reversed(messages): |
| 25 | + if m.get("role") == "user": |
| 26 | + last_user = str(m.get("content", "")) |
| 27 | + break |
| 28 | + |
| 29 | + original = last_user.split("\n\n", 1)[1] if "\n\n" in last_user else last_user |
| 30 | + return {"message": {"content": f"Echo: {original}"}} |
| 31 | + |
| 32 | + monkeypatch.setattr(a, "_call_ollama", _fake_call_ollama) |
| 33 | + return a |
| 34 | + |
| 35 | + |
14 | 36 | def test_agent_init(agent): |
15 | 37 | assert agent.model == "qwen2.5:7b" |
16 | 38 | assert len(agent.messages) == 0 |
17 | 39 |
|
| 40 | + |
18 | 41 | def test_tool_executor(tmp_path): |
19 | 42 | executor = ToolExecutor(work_dir=str(tmp_path)) |
20 | | - # Test read/write |
21 | 43 | write_result = executor.execute_tool("write_file", {"path": "test.txt", "content": "Hello"}) |
22 | 44 | assert write_result["success"] |
23 | 45 | read_result = executor.execute_tool("read_file", {"path": "test.txt"}) |
24 | 46 | assert read_result["content"] == "Hello" |
25 | 47 |
|
| 48 | + |
26 | 49 | def test_agent_run_simple(agent): |
27 | 50 | response = agent.run("Echo test", verbose=False) |
28 | | - assert "test" in response.lower() # Asume modelo responde echo |
| 51 | + assert "test" in response.lower() |
29 | 52 |
|
30 | | -def test_cache(agent): |
31 | | - # Asume caché implementado |
32 | | - first = agent.run("Lista archivos") |
33 | | - second = agent.run("Lista archivos") |
34 | | - assert first == second # Con caché hit |
35 | 53 |
|
36 | | -# Más tests para herramientas Pro... |
| 54 | +def test_cache(agent): |
| 55 | + # Si luego implementas caché real, este test sigue válido |
| 56 | + # mientras el modelo/mock sea determinista. |
| 57 | + first = agent.run("Lista archivos", verbose=False) |
| 58 | + second = agent.run("Lista archivos", verbose=False) |
| 59 | + assert first == second |
0 commit comments