Add testg

This commit is contained in:
luijait 2025-05-26 08:43:15 +00:00
parent 9b2fb71927
commit 9af18a6e34
1 changed files with 50 additions and 17 deletions

View File

@ -1,51 +1,77 @@
import os
import pytest
import json
import asyncio
from unittest.mock import MagicMock
# Set test environment variables to avoid OpenAI client initialization errors
os.environ["OPENAI_API_KEY"] = "test_key_for_ci_environment"
from cai.tools.reconnaissance.generic_linux_command import generic_linux_command
from cai.sdk.agents import RunContextWrapper
def test_generic_linux_command_echo():
@pytest.mark.asyncio
async def test_generic_linux_command_echo():
"""Test the execution of echo command using generic_linux_command."""
result = generic_linux_command(command="echo 'hello'")
args = {"command": "echo 'hello'"}
result = await generic_linux_command.on_invoke_tool(
RunContextWrapper(None), json.dumps(args)
)
assert result.strip() == 'hello'
def test_generic_linux_command_ls():
@pytest.mark.asyncio
async def test_generic_linux_command_ls():
"""Test the execution of ls command using generic_linux_command."""
result = generic_linux_command(command="ls -l")
args = {"command": "ls -l"}
result = await generic_linux_command.on_invoke_tool(
RunContextWrapper(None), json.dumps(args)
)
# Check that the output contains typical ls -l indicators
assert "total" in result or "drwx" in result or "-rw" in result
def test_generic_linux_command_invalid_command():
@pytest.mark.asyncio
async def test_generic_linux_command_invalid_command():
"""Test handling of invalid command using generic_linux_command."""
result = generic_linux_command(command="invalid_command_xyz123")
args = {"command": "invalid_command_xyz123"}
result = await generic_linux_command.on_invoke_tool(
RunContextWrapper(None), json.dumps(args)
)
# Check for common error indicators
assert ("not found" in result.lower() or
"command not found" in result.lower() or
"no such file" in result.lower())
def test_generic_linux_command_empty_command():
@pytest.mark.asyncio
async def test_generic_linux_command_empty_command():
"""Test handling of empty command using generic_linux_command."""
result = generic_linux_command(command="")
args = {"command": ""}
result = await generic_linux_command.on_invoke_tool(
RunContextWrapper(None), json.dumps(args)
)
assert "Error: No command provided" in result
def test_generic_linux_command_session_list():
@pytest.mark.asyncio
async def test_generic_linux_command_session_list():
"""Test session list functionality using generic_linux_command."""
result = generic_linux_command(command="session list")
args = {"command": "session list"}
result = await generic_linux_command.on_invoke_tool(
RunContextWrapper(None), json.dumps(args)
)
assert "No active sessions" in result or "Active sessions:" in result
def test_generic_linux_command_env_info():
@pytest.mark.asyncio
async def test_generic_linux_command_env_info():
"""Test environment info functionality using generic_linux_command."""
result = generic_linux_command(command="env info")
args = {"command": "env info"}
result = await generic_linux_command.on_invoke_tool(
RunContextWrapper(None), json.dumps(args)
)
assert "Current Environment:" in result
assert "CTF Environment:" in result
assert "Container:" in result
@ -53,18 +79,25 @@ def test_generic_linux_command_env_info():
assert "Workspace:" in result
def test_generic_linux_command_interactive_flag():
@pytest.mark.asyncio
async def test_generic_linux_command_interactive_flag():
"""Test interactive flag functionality using generic_linux_command."""
# Test with interactive=True but a simple command
result = generic_linux_command(command="echo 'test'", interactive=True)
args = {"command": "echo 'test'", "interactive": True}
result = await generic_linux_command.on_invoke_tool(
RunContextWrapper(None), json.dumps(args)
)
# Should still work, just might have different session handling
assert "test" in result
def test_generic_linux_command_with_session_id():
@pytest.mark.asyncio
async def test_generic_linux_command_with_session_id():
"""Test session_id parameter using generic_linux_command."""
# Test with a non-existent session_id
result = generic_linux_command(command="echo 'test'",
session_id="nonexistent123")
args = {"command": "echo 'test'", "session_id": "nonexistent123"}
result = await generic_linux_command.on_invoke_tool(
RunContextWrapper(None), json.dumps(args)
)
# Should handle gracefully - either execute or give session error
assert isinstance(result, str)