|
| 1 | +# Copyright 2026 Dimensional Inc. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import os |
| 16 | +from pathlib import Path |
| 17 | +from threading import Event |
| 18 | + |
| 19 | +from dotenv import load_dotenv |
| 20 | +from langchain_core.messages.base import BaseMessage |
| 21 | +import pytest |
| 22 | + |
| 23 | +from dimos.agents.agent_test_runner import AgentTestRunner |
| 24 | +from dimos.agents.mcp.mcp_client import McpClient |
| 25 | +from dimos.agents.mcp.mcp_server import McpServer |
| 26 | +from dimos.core.blueprints import autoconnect |
| 27 | +from dimos.core.global_config import global_config |
| 28 | +from dimos.core.transport import pLCMTransport |
| 29 | + |
| 30 | +load_dotenv() |
| 31 | + |
| 32 | +FIXTURE_DIR = Path(__file__).parent / "fixtures" |
| 33 | + |
| 34 | + |
| 35 | +@pytest.fixture |
| 36 | +def agent_setup(request): |
| 37 | + coordinator = None |
| 38 | + transports: list[pLCMTransport] = [] |
| 39 | + unsubs: list = [] |
| 40 | + recording = bool(os.getenv("RECORD")) |
| 41 | + |
| 42 | + def fn( |
| 43 | + *, |
| 44 | + blueprints, |
| 45 | + messages: list[BaseMessage], |
| 46 | + dask: bool = False, |
| 47 | + system_prompt: str | None = None, |
| 48 | + fixture: str | None = None, |
| 49 | + ) -> list[BaseMessage]: |
| 50 | + history: list[BaseMessage] = [] |
| 51 | + finished_event = Event() |
| 52 | + |
| 53 | + agent_transport: pLCMTransport = pLCMTransport("/agent") |
| 54 | + finished_transport: pLCMTransport = pLCMTransport("/finished") |
| 55 | + transports.extend([agent_transport, finished_transport]) |
| 56 | + |
| 57 | + def on_message(msg: BaseMessage) -> None: |
| 58 | + history.append(msg) |
| 59 | + |
| 60 | + unsubs.append(agent_transport.subscribe(on_message)) |
| 61 | + unsubs.append(finished_transport.subscribe(lambda _: finished_event.set())) |
| 62 | + |
| 63 | + # Derive fixture path from test name if not explicitly provided. |
| 64 | + if fixture is not None: |
| 65 | + fixture_path = FIXTURE_DIR / fixture |
| 66 | + else: |
| 67 | + fixture_path = FIXTURE_DIR / f"{request.node.name}.json" |
| 68 | + |
| 69 | + client_kwargs: dict = {"system_prompt": system_prompt} |
| 70 | + |
| 71 | + if recording or fixture_path.exists(): |
| 72 | + client_kwargs["model_fixture"] = str(fixture_path) |
| 73 | + |
| 74 | + blueprint = autoconnect( |
| 75 | + *blueprints, |
| 76 | + McpServer.blueprint(), |
| 77 | + McpClient.blueprint(**client_kwargs), |
| 78 | + AgentTestRunner.blueprint(messages=messages), |
| 79 | + ) |
| 80 | + |
| 81 | + global_config.update( |
| 82 | + viewer_backend="none", |
| 83 | + dask=dask, |
| 84 | + ) |
| 85 | + |
| 86 | + nonlocal coordinator |
| 87 | + coordinator = blueprint.build() |
| 88 | + |
| 89 | + if not finished_event.wait(60): |
| 90 | + raise TimeoutError("Timed out waiting for agent to finish processing messages.") |
| 91 | + |
| 92 | + return history |
| 93 | + |
| 94 | + yield fn |
| 95 | + |
| 96 | + if coordinator is not None: |
| 97 | + coordinator.stop() |
| 98 | + |
| 99 | + for transport in transports: |
| 100 | + transport.stop() |
| 101 | + |
| 102 | + for unsub in unsubs: |
| 103 | + unsub() |
0 commit comments