Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions tests/integration/extension/test_reflection.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
"""Integration tests for the reflection tool."""

from unittest.mock import MagicMock

import pytest

from codegen.extensions.tools.reflection import perform_reflection
from codegen.sdk.core.codebase import Codebase


@pytest.fixture
def mock_codebase():
"""Create a mock codebase for testing."""
return MagicMock(spec=Codebase)


def test_perform_reflection(mock_codebase):
"""Test the reflection tool."""
# Test with basic inputs
result = perform_reflection(
context_summary="Testing the reflection tool",
findings_so_far="Found some interesting patterns",
current_challenges="Need to improve test coverage",
reflection_focus=None,
codebase=mock_codebase,
)

# Verify the result structure
assert result.status == "success"
assert "reflection" in result.content
assert "Testing the reflection tool" in result.content
assert "Found some interesting patterns" in result.content
assert "Need to improve test coverage" in result.content

# Test with a specific reflection focus
result = perform_reflection(
context_summary="Testing the reflection tool",
findings_so_far="Found some interesting patterns",
current_challenges="Need to improve test coverage",
reflection_focus="architecture",
codebase=mock_codebase,
)

# Verify the result includes the focus
assert result.status == "success"
assert "reflection" in result.content
assert "architecture" in result.content

# Test with minimal inputs
result = perform_reflection(
context_summary="Minimal test",
findings_so_far="Minimal findings",
codebase=mock_codebase,
)

# Verify the result with minimal inputs
assert result.status == "success"
assert "reflection" in result.content
assert "Minimal test" in result.content
assert "Minimal findings" in result.content
150 changes: 150 additions & 0 deletions tests/integration/extension/test_relace_edit.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
"""Integration tests for the relace_edit tool."""

import os
import tempfile
from pathlib import Path
from unittest.mock import MagicMock, patch

import pytest

from codegen.extensions.tools.relace_edit import relace_edit
from codegen.sdk.core.codebase import Codebase


@pytest.fixture
def temp_workspace():
"""Create a temporary workspace for testing."""
with tempfile.TemporaryDirectory() as temp_dir:
# Create a simple file structure for testing
base_dir = Path(temp_dir)

# Create a simple Python file
test_file = base_dir / "test_file.py"
test_file.write_text("""
def hello_world():
return 'Hello, World!'

def goodbye_world():
return 'Goodbye, World!'
""")

# Create a JavaScript file
js_file = base_dir / "test_file.js"
js_file.write_text("""
function helloWorld() {
return 'Hello, World!';
}

function goodbyeWorld() {
return 'Goodbye, World!';
}
""")

# Create a codebase from the temp directory
codebase = Codebase.from_directory(str(base_dir))

yield codebase


@pytest.mark.skipif(not os.getenv("RELACE_API"), reason="RELACE_API environment variable not set")
def test_relace_edit_real_api(temp_workspace):
"""Test relace_edit with the real API.

This test is skipped if the RELACE_API environment variable is not set.
"""
# Test editing a Python file
edit_snippet = """
# Keep existing imports

# Modify hello_world function
def hello_world():
print("Starting greeting")
return 'Hello, Modified World!'

# Keep goodbye_world function
"""

result = relace_edit(temp_workspace, "test_file.py", edit_snippet)
assert result.status == "success"

# Verify the file was modified correctly
from codegen.extensions.tools import view_file

file_content = view_file(temp_workspace, "test_file.py")
assert 'print("Starting greeting")' in file_content.content
assert "Hello, Modified World!" in file_content.content
assert "Goodbye, World!" in file_content.content # Should be preserved


@patch("codegen.extensions.tools.relace_edit.requests.post")
def test_relace_edit_mock_api(mock_post, temp_workspace):
"""Test relace_edit with a mocked API."""
# Mock the API response
mock_response = MagicMock()
mock_response.status_code = 200
mock_response.json.return_value = {
"status": "success",
"result": """
def hello_world():
print("Mocked greeting")
return 'Hello, Mocked World!'

def goodbye_world():
return 'Goodbye, World!'
""",
}
mock_post.return_value = mock_response

# Test editing a Python file
edit_snippet = """
# Modify hello_world function
def hello_world():
print("Mocked greeting")
return 'Hello, Mocked World!'

# Keep goodbye_world function
"""

result = relace_edit(temp_workspace, "test_file.py", edit_snippet)
assert result.status == "success"

# Verify the API was called with the correct parameters
mock_post.assert_called_once()
args, kwargs = mock_post.call_args
assert "content" in kwargs["json"]
assert "edit_snippet" in kwargs["json"]

# Verify the file was modified correctly
from codegen.extensions.tools import view_file

file_content = view_file(temp_workspace, "test_file.py")
assert 'print("Mocked greeting")' in file_content.content
assert "Hello, Mocked World!" in file_content.content


@patch("codegen.extensions.tools.relace_edit.requests.post")
def test_relace_edit_api_error(mock_post, temp_workspace):
"""Test relace_edit with API errors."""
# Mock an API error response
mock_response = MagicMock()
mock_response.status_code = 400
mock_response.json.return_value = {"status": "error", "error": "Invalid request"}
mock_post.return_value = mock_response

# Test with API error
result = relace_edit(temp_workspace, "test_file.py", "# Invalid edit")
assert result.status == "error"
assert "API error" in result.error

# Mock a network error
mock_post.side_effect = Exception("Network error")

# Test with network error
result = relace_edit(temp_workspace, "test_file.py", "# Invalid edit")
assert result.status == "error"
assert "Network error" in result.error

# Test with non-existent file
result = relace_edit(temp_workspace, "nonexistent_file.py", "# Edit")
assert result.status == "error"
assert "not found" in result.error
99 changes: 99 additions & 0 deletions tests/integration/extension/test_slack.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
"""Integration tests for Slack tools."""

from unittest.mock import MagicMock

import pytest

from codegen.extensions.tools.link_annotation import add_links_to_message
from codegen.sdk.core.codebase import Codebase


@pytest.fixture
def mock_codebase():
"""Create a mock codebase for testing."""
codebase = MagicMock(spec=Codebase)

# Mock the symbol lookup functionality
def mock_get_symbol(symbol_name):
if symbol_name in ["test_function", "TestClass"]:
mock_symbol = MagicMock()
mock_symbol.filepath = f"src/{symbol_name.lower()}.py"
return mock_symbol
return None

codebase.get_symbol.side_effect = mock_get_symbol

# Mock the file exists functionality
def mock_file_exists(filepath):
return filepath in ["src/file1.py", "src/file2.py", "src/test_function.py", "src/testclass.py"]

codebase.file_exists.side_effect = mock_file_exists

return codebase


def test_add_links_to_message(mock_codebase):
"""Test adding links to a Slack message."""
# Test with symbol names
message = "Check the `test_function` and `TestClass` implementations."
result = add_links_to_message(message, mock_codebase)

# Verify links were added for symbols
assert "`test_function`" not in result # Should be replaced with a link
assert "`TestClass`" not in result # Should be replaced with a link
assert "src/test_function.py" in result
assert "src/testclass.py" in result

# Test with file paths
message = "Look at `src/file1.py` and `src/file2.py`."
result = add_links_to_message(message, mock_codebase)

# Verify links were added for files
assert "`src/file1.py`" not in result # Should be replaced with a link
assert "`src/file2.py`" not in result # Should be replaced with a link
assert "src/file1.py" in result
assert "src/file2.py" in result

# Test with non-existent symbols and files
message = "Check `nonexistent_function` and `src/nonexistent.py`."
result = add_links_to_message(message, mock_codebase)

# Verify no links were added for non-existent items
assert "`nonexistent_function`" in result # Should remain as is
assert "`src/nonexistent.py`" in result # Should remain as is

# Test with mixed content
message = "Check `test_function` and regular text with `src/file1.py` and *markdown*."
result = add_links_to_message(message, mock_codebase)

# Verify links were added only for valid symbols and files
assert "`test_function`" not in result
assert "`src/file1.py`" not in result
assert "src/test_function.py" in result
assert "src/file1.py" in result
assert "*markdown*" in result # Markdown should be preserved


def test_send_slack_message():
"""Test sending a message to Slack.

Note: This is a mock test since we can't actually send messages in the test environment.
"""
# Create a mock say function
mock_say = MagicMock()

# Create a mock codebase
mock_codebase = MagicMock(spec=Codebase)

# Import the function directly to test
from codegen.extensions.langchain.tools import SlackSendMessageTool

# Create the tool
tool = SlackSendMessageTool(codebase=mock_codebase, say=mock_say)

# Test sending a message
result = tool._run("Test message")

# Verify the message was sent
mock_say.assert_called_once()
assert "✅ Message sent successfully" in result
Loading
Loading