|
| 1 | +import pytest |
| 2 | +from autogen_agentchat.agents import CodeExecutorAgent |
| 3 | +from autogen_agentchat.base import Response |
| 4 | +from autogen_agentchat.messages import TextMessage |
| 5 | +from autogen_core import CancellationToken |
| 6 | +from autogen_core.components.code_executor import LocalCommandLineCodeExecutor |
| 7 | + |
| 8 | + |
| 9 | +@pytest.mark.asyncio |
| 10 | +async def test_basic_code_execution() -> None: |
| 11 | + """Test basic code execution""" |
| 12 | + |
| 13 | + agent = CodeExecutorAgent(name="code_executor", code_executor=LocalCommandLineCodeExecutor()) |
| 14 | + |
| 15 | + messages = [ |
| 16 | + TextMessage( |
| 17 | + content=""" |
| 18 | +```python |
| 19 | +import math |
| 20 | +
|
| 21 | +number = 42 |
| 22 | +square_root = math.sqrt(number) |
| 23 | +print("%0.3f" % (square_root,)) |
| 24 | +``` |
| 25 | +""".strip(), |
| 26 | + source="assistant", |
| 27 | + ) |
| 28 | + ] |
| 29 | + response = await agent.on_messages(messages, CancellationToken()) |
| 30 | + |
| 31 | + assert isinstance(response, Response) |
| 32 | + assert isinstance(response.chat_message, TextMessage) |
| 33 | + assert response.chat_message.content.strip() == "6.481" |
| 34 | + assert response.chat_message.source == "code_executor" |
| 35 | + |
| 36 | + |
| 37 | +@pytest.mark.asyncio |
| 38 | +async def test_code_execution_error() -> None: |
| 39 | + """Test basic code execution""" |
| 40 | + |
| 41 | + agent = CodeExecutorAgent(name="code_executor", code_executor=LocalCommandLineCodeExecutor()) |
| 42 | + |
| 43 | + messages = [ |
| 44 | + TextMessage( |
| 45 | + content=""" |
| 46 | +```python |
| 47 | +import math |
| 48 | +
|
| 49 | +number = -1.0 |
| 50 | +square_root = math.sqrt(number) |
| 51 | +print("%0.3f" % (square_root,)) |
| 52 | +``` |
| 53 | +""".strip(), |
| 54 | + source="assistant", |
| 55 | + ) |
| 56 | + ] |
| 57 | + response = await agent.on_messages(messages, CancellationToken()) |
| 58 | + |
| 59 | + assert isinstance(response, Response) |
| 60 | + assert isinstance(response.chat_message, TextMessage) |
| 61 | + assert "The script ran, then exited with an error (POSIX exit code: 1)" in response.chat_message.content |
| 62 | + assert "ValueError: math domain error" in response.chat_message.content |
| 63 | + |
| 64 | + |
| 65 | +@pytest.mark.asyncio |
| 66 | +async def test_code_execution_no_output() -> None: |
| 67 | + """Test basic code execution""" |
| 68 | + |
| 69 | + agent = CodeExecutorAgent(name="code_executor", code_executor=LocalCommandLineCodeExecutor()) |
| 70 | + |
| 71 | + messages = [ |
| 72 | + TextMessage( |
| 73 | + content=""" |
| 74 | +```python |
| 75 | +import math |
| 76 | +
|
| 77 | +number = 42 |
| 78 | +square_root = math.sqrt(number) |
| 79 | +``` |
| 80 | +""".strip(), |
| 81 | + source="assistant", |
| 82 | + ) |
| 83 | + ] |
| 84 | + response = await agent.on_messages(messages, CancellationToken()) |
| 85 | + |
| 86 | + assert isinstance(response, Response) |
| 87 | + assert isinstance(response.chat_message, TextMessage) |
| 88 | + assert ( |
| 89 | + "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." |
| 90 | + in response.chat_message.content |
| 91 | + ) |
| 92 | + |
| 93 | + |
| 94 | +@pytest.mark.asyncio |
| 95 | +async def test_code_execution_no_block() -> None: |
| 96 | + """Test basic code execution""" |
| 97 | + |
| 98 | + agent = CodeExecutorAgent(name="code_executor", code_executor=LocalCommandLineCodeExecutor()) |
| 99 | + |
| 100 | + messages = [ |
| 101 | + TextMessage( |
| 102 | + content=""" |
| 103 | +import math |
| 104 | +
|
| 105 | +number = 42 |
| 106 | +square_root = math.sqrt(number) |
| 107 | +""".strip(), |
| 108 | + source="assistant", |
| 109 | + ) |
| 110 | + ] |
| 111 | + response = await agent.on_messages(messages, CancellationToken()) |
| 112 | + |
| 113 | + assert isinstance(response, Response) |
| 114 | + assert isinstance(response.chat_message, TextMessage) |
| 115 | + assert ( |
| 116 | + "No code blocks found in the thread. Please provide at least one markdown-encoded code block" |
| 117 | + in response.chat_message.content |
| 118 | + ) |
| 119 | + |
| 120 | + |
| 121 | +@pytest.mark.asyncio |
| 122 | +async def test_code_execution_multiple_blocks() -> None: |
| 123 | + """Test basic code execution""" |
| 124 | + |
| 125 | + agent = CodeExecutorAgent(name="code_executor", code_executor=LocalCommandLineCodeExecutor()) |
| 126 | + |
| 127 | + messages = [ |
| 128 | + TextMessage( |
| 129 | + content=""" |
| 130 | +```python |
| 131 | +import math |
| 132 | +
|
| 133 | +number = 42 |
| 134 | +square_root = math.sqrt(number) |
| 135 | +print("%0.3f" % (square_root,)) |
| 136 | +``` |
| 137 | +
|
| 138 | +And also: |
| 139 | +
|
| 140 | +```python |
| 141 | +import time |
| 142 | +print(f"The current time is: {time.time()}") |
| 143 | +
|
| 144 | +``` |
| 145 | +
|
| 146 | +And this should result in an error: |
| 147 | +```python |
| 148 | +import math |
| 149 | +
|
| 150 | +number = -1.0 |
| 151 | +square_root = math.sqrt(number) |
| 152 | +print("%0.3f" % (square_root,)) |
| 153 | +``` |
| 154 | +
|
| 155 | +""".strip(), |
| 156 | + source="assistant", |
| 157 | + ) |
| 158 | + ] |
| 159 | + response = await agent.on_messages(messages, CancellationToken()) |
| 160 | + |
| 161 | + assert isinstance(response, Response) |
| 162 | + assert isinstance(response.chat_message, TextMessage) |
| 163 | + assert "6.481" in response.chat_message.content |
| 164 | + assert "The current time is:" in response.chat_message.content |
| 165 | + assert "The script ran, then exited with an error (POSIX exit code: 1)" in response.chat_message.content |
| 166 | + assert "ValueError: math domain error" in response.chat_message.content |
0 commit comments