ReMe/tests/test_tool.py

196 lines
5.9 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""Tests for tool operations including search and execution tools.
This module contains test functions for various tool operations such as
search tools (Dashscope, Mock, Tavily) and execution tools (Code, Shell).
"""
# pylint: disable=too-many-statements
import asyncio
from reme import ReMe
app = ReMe()
def test_search():
"""Test search tool operations.
Tests DashscopeSearch, MockSearch, and TavilySearch operations
with a sample query to verify they work correctly.
"""
from reme.tool.search import DashscopeSearch, MockSearch, TavilySearch
query = "美股DFDV是做什么的"
for op in [
DashscopeSearch(),
MockSearch(),
TavilySearch(),
]:
print("\n" + "=" * 60)
print(f"Testing {op.__class__.__name__}")
print("=" * 60)
print(f"Query: {query}")
output = asyncio.run(op.call(query=query, service_context=app.service_context))
print(f"Output:\n{output}")
def test_execute():
"""Test code and shell execution tool operations.
Tests ExecuteCode and ExecuteShell operations with various scenarios
including successful execution, syntax errors, runtime errors, and
invalid commands to verify error handling.
"""
from reme.tool.gallery import ExecuteCode, ExecuteShell
# Test ExecuteCode
print("\n" + "=" * 60)
print("Testing ExecuteCode")
print("=" * 60)
op = ExecuteCode()
code_to_execute = "print('hello world')"
print(f"Executing Python code: {code_to_execute}")
output = asyncio.run(op.call(code=code_to_execute))
print(f"Output:\n{output}")
# Test ExecuteCode with more complex code
print("\n" + "=" * 60)
print("Testing ExecuteCode with calculation")
print("=" * 60)
op = ExecuteCode()
code_to_execute = "result = sum(range(1, 11))\nprint(f'Sum of 1-10: {result}')"
print(f"Executing Python code:\n{code_to_execute}")
output = asyncio.run(op.call(code=code_to_execute))
print(f"Output:\n{output}")
# Test ExecuteShell
print("\n" + "=" * 60)
print("Testing ExecuteShell")
print("=" * 60)
op = ExecuteShell()
command = "ls"
print(f"Executing shell command: {command}")
output = asyncio.run(op.call(command=command))
print(f"Output:\n{output}")
# Test ExecuteShell with echo
print("\n" + "=" * 60)
print("Testing ExecuteShell with echo")
print("=" * 60)
op = ExecuteShell()
command = "echo 'Hello from shell!'"
print(f"Executing shell command: {command}")
output = asyncio.run(op.call(command=command))
print(f"Output:\n{output}")
# Test ExecuteCode with error (syntax error)
print("\n" + "=" * 60)
print("Testing ExecuteCode with syntax error (expected to fail)")
print("=" * 60)
op = ExecuteCode()
code_to_execute = "print('missing closing quote)"
print(f"Executing Python code with syntax error:\n{code_to_execute}")
output = asyncio.run(op.call(code=code_to_execute))
print(f"Output:\n{output}")
# Test ExecuteCode with runtime error
print("\n" + "=" * 60)
print("Testing ExecuteCode with runtime error (expected to fail)")
print("=" * 60)
op = ExecuteCode()
code_to_execute = "x = 1 / 0"
print(f"Executing Python code with runtime error:\n{code_to_execute}")
output = asyncio.run(op.call(code=code_to_execute))
print(f"Output:\n{output}")
# Test ExecuteCode with undefined variable
print("\n" + "=" * 60)
print("Testing ExecuteCode with undefined variable (expected to fail)")
print("=" * 60)
op = ExecuteCode()
code_to_execute = "print(undefined_variable)"
print(f"Executing Python code with undefined variable:\n{code_to_execute}")
output = asyncio.run(op.call(code=code_to_execute))
print(f"Output:\n{output}")
# Test ExecuteShell with invalid command
print("\n" + "=" * 60)
print("Testing ExecuteShell with invalid command (expected to fail)")
print("=" * 60)
op = ExecuteShell()
command = "this_command_does_not_exist"
print(f"Executing invalid shell command: {command}")
output = asyncio.run(op.call(command=command))
print(f"Output:\n{output}")
# Test ExecuteShell with command that returns non-zero exit code
print("\n" + "=" * 60)
print("Testing ExecuteShell with failing command (expected to fail)")
print("=" * 60)
op = ExecuteShell()
command = "ls /nonexistent_directory_12345"
print(f"Executing shell command that should fail: {command}")
output = asyncio.run(op.call(command=command))
print(f"Output:\n{output}")
print("\n" + "=" * 60)
print("All tests completed!")
print("=" * 60)
def test_simple_chat():
"""Test simple chat operation.
Tests the SimpleChat agent with a basic query to verify
it can process and respond to user input.
"""
from reme.agent.chat import SimpleChat
op = SimpleChat()
output = asyncio.run(op.call(query="你好", service_context=app.service_context))
print(output)
async def test_stream_chat():
"""Test streaming chat operation.
Tests the StreamChat agent with a query to verify it can
process and stream responses in real-time using async operations.
"""
from reme.agent.chat import StreamChat
from reme.core.utils import execute_stream_task
from reme.core.context import RuntimeContext
from asyncio import Queue
op = StreamChat()
context = RuntimeContext(query="你好,详细介绍一下自己", stream_queue=Queue(), service_context=app.service_context)
async def task():
await op.call(context)
await op.context.add_stream_done()
async for chunk in execute_stream_task(
stream_queue=context.stream_queue,
task=asyncio.create_task(task()),
task_name="test_stream_chat",
as_bytes=False,
):
print(chunk, end="")
if __name__ == "__main__":
test_search()
# test_execute()
# test_simple_chat()
# asyncio.run(test_stream_chat())