|
| 1 | +"""Test cases for append_text_file_contents handler.""" |
| 2 | + |
| 3 | +import os |
| 4 | +from typing import Any, Dict |
| 5 | + |
| 6 | +import pytest |
| 7 | + |
| 8 | +from mcp_text_editor.server import AppendTextFileContentsHandler |
| 9 | +from mcp_text_editor.text_editor import TextEditor |
| 10 | + |
| 11 | +# Initialize handler for tests |
| 12 | +append_handler = AppendTextFileContentsHandler() |
| 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_append_text_file_success(test_dir: str, cleanup_files: None) -> None: |
| 30 | + """Test successful appending to a file.""" |
| 31 | + test_file = os.path.join(test_dir, "append_test.txt") |
| 32 | + initial_content = "Initial content\n" |
| 33 | + append_content = "Appended content\n" |
| 34 | + |
| 35 | + # Create initial file |
| 36 | + with open(test_file, "w", encoding="utf-8") as f: |
| 37 | + f.write(initial_content) |
| 38 | + |
| 39 | + # Get file hash for append operation |
| 40 | + editor = TextEditor() |
| 41 | + _, _, _, file_hash, _, _ = await editor.read_file_contents(test_file) |
| 42 | + |
| 43 | + # Append content using handler |
| 44 | + arguments: Dict[str, Any] = { |
| 45 | + "path": test_file, |
| 46 | + "contents": append_content, |
| 47 | + "file_hash": file_hash, |
| 48 | + } |
| 49 | + response = await append_handler.run_tool(arguments) |
| 50 | + |
| 51 | + # Check if content was appended correctly |
| 52 | + with open(test_file, "r", encoding="utf-8") as f: |
| 53 | + content = f.read() |
| 54 | + assert content == initial_content + append_content |
| 55 | + |
| 56 | + # Parse response to check success |
| 57 | + assert len(response) == 1 |
| 58 | + result = response[0].text |
| 59 | + assert "\"result\": \"ok\"" in result |
| 60 | + |
| 61 | + |
| 62 | +@pytest.mark.asyncio |
| 63 | +async def test_append_text_file_not_exists(test_dir: str, cleanup_files: None) -> None: |
| 64 | + """Test attempting to append to a non-existent file.""" |
| 65 | + test_file = os.path.join(test_dir, "nonexistent.txt") |
| 66 | + |
| 67 | + # Try to append to non-existent file |
| 68 | + arguments: Dict[str, Any] = { |
| 69 | + "path": test_file, |
| 70 | + "contents": "Some content\n", |
| 71 | + "file_hash": "dummy_hash", |
| 72 | + } |
| 73 | + |
| 74 | + # Should raise error because file doesn't exist |
| 75 | + with pytest.raises(RuntimeError) as exc_info: |
| 76 | + await append_handler.run_tool(arguments) |
| 77 | + assert "File does not exist" in str(exc_info.value) |
| 78 | + |
| 79 | + |
| 80 | +@pytest.mark.asyncio |
| 81 | +async def test_append_text_file_hash_mismatch(test_dir: str, cleanup_files: None) -> None: |
| 82 | + """Test appending with incorrect file hash.""" |
| 83 | + test_file = os.path.join(test_dir, "hash_test.txt") |
| 84 | + initial_content = "Initial content\n" |
| 85 | + |
| 86 | + # Create initial file |
| 87 | + with open(test_file, "w", encoding="utf-8") as f: |
| 88 | + f.write(initial_content) |
| 89 | + |
| 90 | + # Try to append with incorrect hash |
| 91 | + arguments: Dict[str, Any] = { |
| 92 | + "path": test_file, |
| 93 | + "contents": "New content\n", |
| 94 | + "file_hash": "incorrect_hash", |
| 95 | + } |
| 96 | + |
| 97 | + # Should raise error because hash doesn't match |
| 98 | + with pytest.raises(RuntimeError) as exc_info: |
| 99 | + await append_handler.run_tool(arguments) |
| 100 | + assert "hash mismatch" in str(exc_info.value).lower() |
| 101 | + |
| 102 | + |
| 103 | +@pytest.mark.asyncio |
| 104 | +async def test_append_text_file_relative_path(test_dir: str, cleanup_files: None) -> None: |
| 105 | + """Test attempting to append using a relative path.""" |
| 106 | + arguments: Dict[str, Any] = { |
| 107 | + "path": "relative_path.txt", |
| 108 | + "contents": "Some content\n", |
| 109 | + "file_hash": "dummy_hash", |
| 110 | + } |
| 111 | + |
| 112 | + # Should raise error because path is not absolute |
| 113 | + with pytest.raises(RuntimeError) as exc_info: |
| 114 | + await append_handler.run_tool(arguments) |
| 115 | + assert "File path must be absolute" in str(exc_info.value) |
| 116 | + |
| 117 | + |
| 118 | +@pytest.mark.asyncio |
| 119 | +async def test_append_text_file_missing_args() -> None: |
| 120 | + """Test appending with missing arguments.""" |
| 121 | + # Test missing path |
| 122 | + with pytest.raises(RuntimeError) as exc_info: |
| 123 | + await append_handler.run_tool({"contents": "content\n", "file_hash": "hash"}) |
| 124 | + assert "Missing required argument: path" in str(exc_info.value) |
| 125 | + |
| 126 | + # Test missing contents |
| 127 | + with pytest.raises(RuntimeError) as exc_info: |
| 128 | + await append_handler.run_tool({"path": "/absolute/path.txt", "file_hash": "hash"}) |
| 129 | + assert "Missing required argument: contents" in str(exc_info.value) |
| 130 | + |
| 131 | + # Test missing file_hash |
| 132 | + with pytest.raises(RuntimeError) as exc_info: |
| 133 | + await append_handler.run_tool({"path": "/absolute/path.txt", "contents": "content\n"}) |
| 134 | + assert "Missing required argument: file_hash" in str(exc_info.value) |
| 135 | + |
| 136 | + |
| 137 | +@pytest.mark.asyncio |
| 138 | +async def test_append_text_file_custom_encoding(test_dir: str, cleanup_files: None) -> None: |
| 139 | + """Test appending with custom encoding.""" |
| 140 | + test_file = os.path.join(test_dir, "encode_test.txt") |
| 141 | + initial_content = "こんにちは\n" |
| 142 | + append_content = "さようなら\n" |
| 143 | + |
| 144 | + # Create initial file |
| 145 | + with open(test_file, "w", encoding="utf-8") as f: |
| 146 | + f.write(initial_content) |
| 147 | + |
| 148 | + # Get file hash for append operation |
| 149 | + editor = TextEditor() |
| 150 | + _, _, _, file_hash, _, _ = await editor.read_file_contents(test_file, encoding="utf-8") |
| 151 | + |
| 152 | + # Append content using handler with specified encoding |
| 153 | + arguments: Dict[str, Any] = { |
| 154 | + "path": test_file, |
| 155 | + "contents": append_content, |
| 156 | + "file_hash": file_hash, |
| 157 | + "encoding": "utf-8", |
| 158 | + } |
| 159 | + response = await append_handler.run_tool(arguments) |
| 160 | + |
| 161 | + # Check if content was appended correctly |
| 162 | + with open(test_file, "r", encoding="utf-8") as f: |
| 163 | + content = f.read() |
| 164 | + assert content == initial_content + append_content |
| 165 | + |
| 166 | + # Parse response to check success |
| 167 | + assert len(response) == 1 |
| 168 | + result = response[0].text |
| 169 | + assert "\"result\": \"ok\"" in result |
0 commit comments