autogen/python/packages/autogen-agentchat/tests/test_code_executor_agent.py
Victor Dibia b8b13935c9
Make FileSurfer and CodeExecAgent Declarative (#5765)
<!-- Thank you for your contribution! Please review
https://microsoft.github.io/autogen/docs/Contribute before opening a
pull request. -->

<!-- Please add a reviewer to the assignee section when you create a PR.
If you don't have the access to it, we will shortly find a reviewer and
assign them to your PR. -->

## Why are these changes needed?

Make FileSurfer and CodeExecAgent Declarative.
These agent presents are used as part of magentic one and having them
declarative is a precursor to their use in AGS.

<!-- Please give a short summary of the change and the problem this
solves. -->

## Related issue number

<!-- For example: "Closes #1234" -->
Closes #5607

## Checks

- [ ] I've included any doc changes needed for
<https://microsoft.github.io/autogen/>. See
<https://github.com/microsoft/autogen/blob/main/CONTRIBUTING.md> to
build and test documentation locally.
- [ ] I've added tests (if relevant) corresponding to the changes
introduced in this PR.
- [ ] I've made sure all auto checks have passed.
2025-03-01 15:46:30 +00:00

181 lines
4.9 KiB
Python

import pytest
from autogen_agentchat.agents import CodeExecutorAgent
from autogen_agentchat.base import Response
from autogen_agentchat.messages import TextMessage
from autogen_core import CancellationToken
from autogen_ext.code_executors.local import LocalCommandLineCodeExecutor
@pytest.mark.asyncio
async def test_basic_code_execution() -> None:
"""Test basic code execution"""
agent = CodeExecutorAgent(name="code_executor", code_executor=LocalCommandLineCodeExecutor())
messages = [
TextMessage(
content="""
```python
import math
number = 42
square_root = math.sqrt(number)
print("%0.3f" % (square_root,))
```
""".strip(),
source="assistant",
)
]
response = await agent.on_messages(messages, CancellationToken())
assert isinstance(response, Response)
assert isinstance(response.chat_message, TextMessage)
assert response.chat_message.content.strip() == "6.481"
assert response.chat_message.source == "code_executor"
@pytest.mark.asyncio
async def test_code_execution_error() -> None:
"""Test basic code execution"""
agent = CodeExecutorAgent(name="code_executor", code_executor=LocalCommandLineCodeExecutor())
messages = [
TextMessage(
content="""
```python
import math
number = -1.0
square_root = math.sqrt(number)
print("%0.3f" % (square_root,))
```
""".strip(),
source="assistant",
)
]
response = await agent.on_messages(messages, CancellationToken())
assert isinstance(response, Response)
assert isinstance(response.chat_message, TextMessage)
assert "The script ran, then exited with an error (POSIX exit code: 1)" in response.chat_message.content
assert "ValueError: math domain error" in response.chat_message.content
@pytest.mark.asyncio
async def test_code_execution_no_output() -> None:
"""Test basic code execution"""
agent = CodeExecutorAgent(name="code_executor", code_executor=LocalCommandLineCodeExecutor())
messages = [
TextMessage(
content="""
```python
import math
number = 42
square_root = math.sqrt(number)
```
""".strip(),
source="assistant",
)
]
response = await agent.on_messages(messages, CancellationToken())
assert isinstance(response, Response)
assert isinstance(response.chat_message, TextMessage)
assert (
"The script ran but produced no output to console. The POSIX exit code was: 0. If you were expecting output, consider revising the script to ensure content is printed to stdout."
in response.chat_message.content
)
@pytest.mark.asyncio
async def test_code_execution_no_block() -> None:
"""Test basic code execution"""
agent = CodeExecutorAgent(name="code_executor", code_executor=LocalCommandLineCodeExecutor())
messages = [
TextMessage(
content="""
import math
number = 42
square_root = math.sqrt(number)
""".strip(),
source="assistant",
)
]
response = await agent.on_messages(messages, CancellationToken())
assert isinstance(response, Response)
assert isinstance(response.chat_message, TextMessage)
assert (
"No code blocks found in the thread. Please provide at least one markdown-encoded code block"
in response.chat_message.content
)
@pytest.mark.asyncio
async def test_code_execution_multiple_blocks() -> None:
"""Test basic code execution"""
agent = CodeExecutorAgent(name="code_executor", code_executor=LocalCommandLineCodeExecutor())
messages = [
TextMessage(
content="""
```python
import math
number = 42
square_root = math.sqrt(number)
print("%0.3f" % (square_root,))
```
And also:
```python
import time
print(f"The current time is: {time.time()}")
```
And this should result in an error:
```python
import math
number = -1.0
square_root = math.sqrt(number)
print("%0.3f" % (square_root,))
```
""".strip(),
source="assistant",
)
]
response = await agent.on_messages(messages, CancellationToken())
assert isinstance(response, Response)
assert isinstance(response.chat_message, TextMessage)
assert "6.481" in response.chat_message.content
assert "The current time is:" in response.chat_message.content
assert "The script ran, then exited with an error (POSIX exit code: 1)" in response.chat_message.content
assert "ValueError: math domain error" in response.chat_message.content
@pytest.mark.asyncio
async def test_code_execution_agent_serialization() -> None:
"""Test agent config serialization"""
agent = CodeExecutorAgent(name="code_executor", code_executor=LocalCommandLineCodeExecutor())
# Serialize and deserialize the agent
serialized_agent = agent.dump_component()
deserialized_agent = CodeExecutorAgent.load_component(serialized_agent)
assert isinstance(deserialized_agent, CodeExecutorAgent)
assert deserialized_agent.name == "code_executor"