|
| 1 | +"""Test cases for create_text_file handler.""" |
| 2 | + |
| 3 | +import os |
| 4 | +from typing import Any, Dict |
| 5 | + |
| 6 | +import pytest |
| 7 | + |
| 8 | +from mcp_text_editor.server import CreateTextFileHandler |
| 9 | +from mcp_text_editor.text_editor import TextEditor |
| 10 | + |
| 11 | +# Initialize handlers for tests |
| 12 | +create_file_handler = CreateTextFileHandler() |
| 13 | + |
| 14 | + |
| 15 | +@pytest.fixture |
| 16 | +def test_dir(tmp_path: str) -> str: |
| 17 | + """Create a temporary directory for test files.""" |
| 18 | + return str(tmp_path) |
| 19 | + |
| 20 | + |
| 21 | +@pytest.fixture |
| 22 | +def cleanup_files() -> None: |
| 23 | + """Clean up any test files after each test.""" |
| 24 | + yield |
| 25 | + # Add cleanup code if needed |
| 26 | + |
| 27 | + |
| 28 | +@pytest.mark.asyncio |
| 29 | +async def test_create_text_file_success(test_dir: str, cleanup_files: None) -> None: |
| 30 | + """Test successful creation of a new text file.""" |
| 31 | + test_file = os.path.join(test_dir, "new_file.txt") |
| 32 | + content = "Hello, World!\n" |
| 33 | + |
| 34 | + # Create file using handler |
| 35 | + arguments: Dict[str, Any] = { |
| 36 | + "path": test_file, |
| 37 | + "contents": content, |
| 38 | + } |
| 39 | + response = await create_file_handler.run_tool(arguments) |
| 40 | + |
| 41 | + # Check if file was created with correct content |
| 42 | + assert os.path.exists(test_file) |
| 43 | + with open(test_file, "r", encoding="utf-8") as f: |
| 44 | + assert f.read() == content |
| 45 | + |
| 46 | + # Parse response to check success |
| 47 | + assert len(response) == 1 |
| 48 | + result = response[0].text |
| 49 | + assert "\"result\": \"ok\"" in result |
| 50 | + |
| 51 | + |
| 52 | +@pytest.mark.asyncio |
| 53 | +async def test_create_text_file_exists(test_dir: str, cleanup_files: None) -> None: |
| 54 | + """Test attempting to create a file that already exists.""" |
| 55 | + test_file = os.path.join(test_dir, "existing_file.txt") |
| 56 | + |
| 57 | + # Create file first |
| 58 | + with open(test_file, "w", encoding="utf-8") as f: |
| 59 | + f.write("Existing content\n") |
| 60 | + |
| 61 | + # Try to create file using handler |
| 62 | + arguments: Dict[str, Any] = { |
| 63 | + "path": test_file, |
| 64 | + "contents": "New content\n", |
| 65 | + } |
| 66 | + |
| 67 | + # Should raise error because file exists |
| 68 | + with pytest.raises(RuntimeError): |
| 69 | + await create_file_handler.run_tool(arguments) |
| 70 | + |
| 71 | + |
| 72 | +@pytest.mark.asyncio |
| 73 | +async def test_create_text_file_relative_path(test_dir: str, cleanup_files: None) -> None: |
| 74 | + """Test attempting to create a file with a relative path.""" |
| 75 | + # Try to create file using relative path |
| 76 | + arguments: Dict[str, Any] = { |
| 77 | + "path": "relative_path.txt", |
| 78 | + "contents": "Some content\n", |
| 79 | + } |
| 80 | + |
| 81 | + # Should raise error because path is not absolute |
| 82 | + with pytest.raises(RuntimeError) as exc_info: |
| 83 | + await create_file_handler.run_tool(arguments) |
| 84 | + assert "File path must be absolute" in str(exc_info.value) |
| 85 | + |
| 86 | + |
| 87 | +@pytest.mark.asyncio |
| 88 | +async def test_create_text_file_missing_args() -> None: |
| 89 | + """Test creating a file with missing arguments.""" |
| 90 | + # Test missing path |
| 91 | + with pytest.raises(RuntimeError) as exc_info: |
| 92 | + await create_file_handler.run_tool({"contents": "content\n"}) |
| 93 | + assert "Missing required argument: path" in str(exc_info.value) |
| 94 | + |
| 95 | + # Test missing contents |
| 96 | + with pytest.raises(RuntimeError) as exc_info: |
| 97 | + await create_file_handler.run_tool({"path": "/absolute/path.txt"}) |
| 98 | + assert "Missing required argument: contents" in str(exc_info.value) |
| 99 | + |
| 100 | + |
| 101 | +@pytest.mark.asyncio |
| 102 | +async def test_create_text_file_custom_encoding(test_dir: str, cleanup_files: None) -> None: |
| 103 | + """Test creating a file with custom encoding.""" |
| 104 | + test_file = os.path.join(test_dir, "encoded_file.txt") |
| 105 | + content = "こんにちは\n" # Japanese text |
| 106 | + |
| 107 | + # Create file using handler with specified encoding |
| 108 | + arguments: Dict[str, Any] = { |
| 109 | + "path": test_file, |
| 110 | + "contents": content, |
| 111 | + "encoding": "utf-8", |
| 112 | + } |
| 113 | + response = await create_file_handler.run_tool(arguments) |
| 114 | + |
| 115 | + # Check if file was created with correct content |
| 116 | + assert os.path.exists(test_file) |
| 117 | + with open(test_file, "r", encoding="utf-8") as f: |
| 118 | + assert f.read() == content |
| 119 | + |
| 120 | + # Parse response to check success |
| 121 | + assert len(response) == 1 |
| 122 | + result = response[0].text |
| 123 | + assert "\"result\": \"ok\"" in result |
0 commit comments