"""Integration tests for file route handlers."""
import base64
from local_filesystem.api.file_routes import register_file_routes
class TestListFiles:
"""Integration tests for list_files route."""
def test_list_files_root(self, populated_temp_root, mock_context, capture_handlers):
"""Test listing files in root directory."""
handlers = capture_handlers(
populated_temp_root, register_file_routes, "local_filesystem.api.file_routes"
)
result = handlers["list_files"](path=".", pattern="*")
assert "files" in result
assert result["total_count"] >= 5
file_names = {f["name"] for f in result["files"]}
assert "file1.txt" in file_names
assert "file2.txt" in file_names
assert "script.py" in file_names
def test_list_files_with_pattern(self, populated_temp_root, mock_context, capture_handlers):
"""Test listing files with glob pattern."""
handlers = capture_handlers(
populated_temp_root, register_file_routes, "local_filesystem.api.file_routes"
)
result = handlers["list_files"](path=".", pattern="*.txt")
assert "files" in result
for file in result["files"]:
assert file["name"].endswith(".txt")
def test_list_files_subdirectory(self, populated_temp_root, mock_context, capture_handlers):
"""Test listing files in subdirectory."""
handlers = capture_handlers(
populated_temp_root, register_file_routes, "local_filesystem.api.file_routes"
)
result = handlers["list_files"](path="dir1", pattern="*")
assert "files" in result
file_names = {f["name"] for f in result["files"]}
assert "nested.txt" in file_names
class TestReadFile:
"""Integration tests for read_file route."""
def test_read_text_file(self, populated_temp_root, mock_context, capture_handlers):
"""Test reading a text file."""
handlers = capture_handlers(
populated_temp_root, register_file_routes, "local_filesystem.api.file_routes"
)
result = handlers["read_file"](path="file1.txt", offset=0, limit=None)
# Check that content contains expected text (normalizing line endings for cross-platform)
assert "Hello World" in result["content"]
assert "This is a test file." in result["content"]
assert "Python is awesome!" in result["content"]
assert result["is_binary"] is False
assert result["encoding"] == "utf-8"
def test_read_binary_file(self, populated_temp_root, mock_context, capture_handlers):
"""Test reading a binary file."""
handlers = capture_handlers(
populated_temp_root, register_file_routes, "local_filesystem.api.file_routes"
)
result = handlers["read_file"](path="binary.bin", offset=0, limit=None)
assert result["is_binary"] is True
# Binary content should be base64 encoded
decoded = base64.b64decode(result["content"])
assert decoded == b"\x00\x01\x02\x03\x04\x05\xff\xfe\xfd"
def test_read_file_with_offset_and_limit(
self, populated_temp_root, mock_context, capture_handlers
):
"""Test reading file with offset and limit."""
handlers = capture_handlers(
populated_temp_root, register_file_routes, "local_filesystem.api.file_routes"
)
# Read first 5 bytes
result = handlers["read_file"](path="file1.txt", offset=0, limit=5)
assert result["content"] == "Hello"
class TestWriteFile:
"""Integration tests for write_file route."""
def test_write_text_file(self, temp_root, mock_context, capture_handlers):
"""Test writing a text file."""
handlers = capture_handlers(
temp_root, register_file_routes, "local_filesystem.api.file_routes"
)
content = "Test content\nLine 2"
result = handlers["write_file"](path="test.txt", content=content, is_base64=False)
assert result["success"] is True
assert (temp_root / "test.txt").read_text() == content
def test_write_binary_file(self, temp_root, mock_context, capture_handlers):
"""Test writing a binary file."""
handlers = capture_handlers(
temp_root, register_file_routes, "local_filesystem.api.file_routes"
)
binary_data = b"\x00\x01\x02\x03"
content = base64.b64encode(binary_data).decode("utf-8")
result = handlers["write_file"](path="test.bin", content=content, is_base64=True)
assert result["success"] is True
assert (temp_root / "test.bin").read_bytes() == binary_data
class TestAppendFile:
"""Integration tests for append_file route."""
def test_append_to_file(self, populated_temp_root, mock_context, capture_handlers):
"""Test appending content to a file."""
handlers = capture_handlers(
populated_temp_root, register_file_routes, "local_filesystem.api.file_routes"
)
original_content = (populated_temp_root / "file1.txt").read_text()
append_content = "\nAppended line"
result = handlers["append_file"](path="file1.txt", content=append_content)
assert result["success"] is True
new_content = (populated_temp_root / "file1.txt").read_text()
assert new_content == original_content + append_content
class TestDeleteFile:
"""Integration tests for delete_file route."""
def test_delete_file(self, populated_temp_root, mock_context, capture_handlers):
"""Test deleting a file."""
handlers = capture_handlers(
populated_temp_root, register_file_routes, "local_filesystem.api.file_routes"
)
assert (populated_temp_root / "file1.txt").exists()
result = handlers["delete_file"](path="file1.txt")
assert result["success"] is True
assert not (populated_temp_root / "file1.txt").exists()
class TestMoveFile:
"""Integration tests for move_file route."""
def test_move_file(self, populated_temp_root, mock_context, capture_handlers):
"""Test moving a file."""
handlers = capture_handlers(
populated_temp_root, register_file_routes, "local_filesystem.api.file_routes"
)
original_content = (populated_temp_root / "file1.txt").read_text()
result = handlers["move_file"](source_path="file1.txt", destination_path="moved.txt")
assert result["success"] is True
assert not (populated_temp_root / "file1.txt").exists()
assert (populated_temp_root / "moved.txt").exists()
assert (populated_temp_root / "moved.txt").read_text() == original_content
def test_rename_file(self, populated_temp_root, mock_context, capture_handlers):
"""Test renaming a file."""
handlers = capture_handlers(
populated_temp_root, register_file_routes, "local_filesystem.api.file_routes"
)
result = handlers["move_file"](source_path="script.py", destination_path="renamed_script.py")
assert result["success"] is True
assert not (populated_temp_root / "script.py").exists()
assert (populated_temp_root / "renamed_script.py").exists()
class TestGetFileMetadata:
"""Integration tests for get_file_metadata route."""
def test_get_file_metadata_text(self, populated_temp_root, mock_context, capture_handlers):
"""Test getting metadata for a text file."""
handlers = capture_handlers(
populated_temp_root, register_file_routes, "local_filesystem.api.file_routes"
)
result = handlers["get_file_metadata"](path="file1.txt")
assert result["path"] == "file1.txt"
assert result["exists"] is True
assert result["is_binary"] is False
assert "mime_type" in result
assert "created" in result
assert "modified" in result
assert "accessed" in result
assert "permissions" in result
def test_get_file_metadata_binary(self, populated_temp_root, mock_context, capture_handlers):
"""Test getting metadata for a binary file."""
handlers = capture_handlers(
populated_temp_root, register_file_routes, "local_filesystem.api.file_routes"
)
result = handlers["get_file_metadata"](path="binary.bin")
assert result["path"] == "binary.bin"
assert result["exists"] is True
assert result["is_binary"] is True