We provide all the information about MCP servers via our MCP API.
curl -X GET 'https://glama.ai/api/mcp/v1/servers/mcp-bridge/local-filesystem'
If you have feedback or need assistance with the MCP directory API, please join our Discord server
"""Tests for SearchService."""
import pytest
class TestSearchByName:
"""Tests for search_by_name method."""
def test_search_by_name_exact_match(self, populated_temp_root, search_service):
"""Test searching by exact filename."""
result = search_service.search_by_name("file1.txt", ".")
assert result.total_matches == 1
assert len(result.matches) == 1
assert result.matches[0].name == "file1.txt"
def test_search_by_name_case_insensitive(self, populated_temp_root, search_service):
"""Test case-insensitive search."""
result = search_service.search_by_name("FILE1.TXT", ".", case_sensitive=False)
assert result.total_matches == 1
assert result.matches[0].name == "file1.txt"
def test_search_by_name_case_sensitive(self, populated_temp_root, search_service):
"""Test case-sensitive search."""
result = search_service.search_by_name("FILE1.TXT", ".", case_sensitive=True)
assert result.total_matches == 0
def test_search_by_name_recursive(self, populated_temp_root, search_service):
"""Test recursive search."""
result = search_service.search_by_name("nested.txt", ".", recursive=True)
assert result.total_matches == 1
assert "dir1" in result.matches[0].path
def test_search_by_name_non_recursive(self, populated_temp_root, search_service):
"""Test non-recursive search."""
result = search_service.search_by_name("nested.txt", ".", recursive=False)
assert result.total_matches == 0
def test_search_by_name_directory(self, populated_temp_root, search_service):
"""Test searching for directory by name."""
result = search_service.search_by_name("dir1", ".")
assert result.total_matches == 1
assert result.matches[0].is_directory is True
assert result.matches[0].name == "dir1"
def test_search_by_name_no_matches(self, populated_temp_root, search_service):
"""Test search with no matches."""
result = search_service.search_by_name("nonexistent.txt", ".")
assert result.total_matches == 0
assert len(result.matches) == 0
class TestSearchByGlob:
"""Tests for search_by_glob method."""
def test_search_by_glob_simple_pattern(self, populated_temp_root, search_service):
"""Test glob search with simple pattern."""
result = search_service.search_by_glob("*.txt", ".")
assert result.total_matches >= 2
for match in result.matches:
assert match.name.endswith(".txt")
def test_search_by_glob_recursive_pattern(self, populated_temp_root, search_service):
"""Test recursive glob pattern."""
result = search_service.search_by_glob("*.txt", ".", recursive=True)
assert result.total_matches >= 4
# Should include nested.txt, deep.txt, etc.
def test_search_by_glob_specific_extension(self, populated_temp_root, search_service):
"""Test searching for specific file extensions."""
result = search_service.search_by_glob("*.py", ".")
assert result.total_matches >= 1
for match in result.matches:
assert match.name.endswith(".py")
def test_search_by_glob_question_mark(self, populated_temp_root, search_service):
"""Test glob with ? wildcard."""
# Create a file that matches pattern
(populated_temp_root / "test1.txt").write_text("content")
(populated_temp_root / "test2.txt").write_text("content")
result = search_service.search_by_glob("test?.txt", ".")
assert result.total_matches >= 2
def test_search_by_glob_no_matches(self, populated_temp_root, search_service):
"""Test glob with no matches."""
result = search_service.search_by_glob("*.xyz", ".")
assert result.total_matches == 0
class TestSearchByFilenameRegex:
"""Tests for search_by_filename_regex method."""
def test_search_by_filename_regex_simple(self, populated_temp_root, search_service):
"""Test regex search on filename."""
result = search_service.search_by_filename_regex(r"file\d+\.txt", ".")
assert result.total_matches >= 2
for match in result.matches:
assert "file" in match.name
assert ".txt" in match.name
def test_search_by_filename_regex_case_insensitive(self, populated_temp_root, search_service):
"""Test case-insensitive regex search."""
result = search_service.search_by_filename_regex(r"FILE\d+", ".", case_sensitive=False)
assert result.total_matches >= 2
def test_search_by_filename_regex_case_sensitive(self, populated_temp_root, search_service):
"""Test case-sensitive regex search."""
result = search_service.search_by_filename_regex(r"FILE\d+", ".", case_sensitive=True)
assert result.total_matches == 0
def test_search_by_filename_regex_complex_pattern(self, populated_temp_root, search_service):
"""Test complex regex pattern."""
result = search_service.search_by_filename_regex(
r"^(file|data|config)\.", ".", recursive=True
)
# Should find file1.txt, file2.txt, data.json, config.yml
assert result.total_matches >= 2 # At least data.json and config.yml
def test_search_by_filename_regex_invalid_pattern(self, populated_temp_root, search_service):
"""Test invalid regex pattern."""
with pytest.raises(ValueError):
search_service.search_by_filename_regex(r"[invalid(", ".")
class TestSearchByContentText:
"""Tests for search_by_content_text method."""
def test_search_by_content_text_simple(self, populated_temp_root, search_service):
"""Test searching for text in file content."""
result = search_service.search_by_content_text("Hello World", ".")
assert result.total_matches >= 1
assert any("file1.txt" in match.path for match in result.matches)
def test_search_by_content_text_case_insensitive(self, populated_temp_root, search_service):
"""Test case-insensitive content search."""
result = search_service.search_by_content_text("HELLO WORLD", ".", case_sensitive=False)
assert result.total_matches >= 1
def test_search_by_content_text_case_sensitive(self, populated_temp_root, search_service):
"""Test case-sensitive content search."""
result = search_service.search_by_content_text("HELLO WORLD", ".", case_sensitive=True)
assert result.total_matches == 0
def test_search_by_content_text_with_context(self, populated_temp_root, search_service):
"""Test that search results include context."""
result = search_service.search_by_content_text("SEARCH", ".", case_sensitive=False)
assert result.total_matches >= 1
for match in result.matches:
if match.match_context:
assert "SEARCH" in match.match_context.upper()
assert match.line_number is not None
assert match.line_number > 0
def test_search_by_content_text_max_results(self, populated_temp_root, search_service):
"""Test max_results limit."""
# Create many files with the search term
for i in range(20):
(populated_temp_root / f"test_{i}.txt").write_text("common_word appears here")
result = search_service.search_by_content_text("common_word", ".", max_results=5)
assert result.total_matches == 5
assert result.truncated is True
def test_search_by_content_text_skips_binary(self, populated_temp_root, search_service):
"""Test that binary files are skipped."""
# Binary file should not be searched even if it contains the bytes
result = search_service.search_by_content_text("binary", ".")
# Should not find matches in binary.bin
for match in result.matches:
assert not match.name.endswith(".bin")
def test_search_by_content_text_recursive(self, populated_temp_root, search_service):
"""Test recursive content search."""
result = search_service.search_by_content_text("nested", ".", recursive=True)
assert result.total_matches >= 1
assert any(
"nested.txt" in match.path or "deep.txt" in match.path for match in result.matches
)
class TestSearchByContentRegex:
"""Tests for search_by_content_regex method."""
def test_search_by_content_regex_simple(self, populated_temp_root, search_service):
"""Test regex search in file content."""
result = search_service.search_by_content_regex(r"Hello \w+", ".")
assert result.total_matches >= 1
for match in result.matches:
if match.match_context:
assert "Hello" in match.match_context
def test_search_by_content_regex_case_insensitive(self, populated_temp_root, search_service):
"""Test case-insensitive regex content search."""
result = search_service.search_by_content_regex(r"HELLO \w+", ".", case_sensitive=False)
assert result.total_matches >= 1
def test_search_by_content_regex_case_sensitive(self, populated_temp_root, search_service):
"""Test case-sensitive regex content search."""
result = search_service.search_by_content_regex(r"HELLO \w+", ".", case_sensitive=True)
assert result.total_matches == 0
def test_search_by_content_regex_complex_pattern(self, populated_temp_root, search_service):
"""Test complex regex pattern."""
result = search_service.search_by_content_regex(r"\b[A-Z][a-z]+\b", ".")
assert result.total_matches >= 1
def test_search_by_content_regex_with_context(self, populated_temp_root, search_service):
"""Test regex results include context and line numbers."""
result = search_service.search_by_content_regex(r"Python", ".")
assert result.total_matches >= 1
for match in result.matches:
if match.match_context:
assert match.line_number is not None
assert match.line_number > 0
def test_search_by_content_regex_max_results(self, populated_temp_root, search_service):
"""Test max_results limit with regex."""
# Create many files
for i in range(15):
(populated_temp_root / f"regex_test_{i}.txt").write_text("pattern123 is here")
result = search_service.search_by_content_regex(r"pattern\d+", ".", max_results=10)
assert result.total_matches == 10
assert result.truncated is True
def test_search_by_content_regex_invalid_pattern(self, populated_temp_root, search_service):
"""Test invalid regex pattern."""
with pytest.raises(ValueError):
search_service.search_by_content_regex(r"[invalid(", ".")
class TestSearchDepthControl:
"""Tests for search depth control."""
def test_search_respects_max_depth(self, temp_root, search_service):
"""Test that search respects max_search_depth."""
# Create deep directory structure
deep_path = temp_root
for i in range(15):
deep_path = deep_path / f"level{i}"
deep_path.mkdir()
(deep_path / "test.txt").write_text("content")
# Search with limited depth (should stop at max_depth)
result = search_service.search_by_name("test.txt", ".")
# Should find some but not all due to depth limit
assert result.search_depth_reached <= search_service.max_search_depth
def test_search_depth_tracking(self, populated_temp_root, search_service):
"""Test that search tracks depth correctly."""
result = search_service.search_by_name("deep.txt", ".")
assert result.search_depth_reached >= 2 # dir1/subdir1/deep.txt
class TestSearchEdgeCases:
"""Tests for search edge cases."""
def test_search_empty_directory(self, temp_root, search_service):
"""Test searching in empty directory."""
(temp_root / "empty").mkdir()
result = search_service.search_by_name("anything", "empty")
assert result.total_matches == 0
assert not result.truncated
def test_search_nonexistent_path(self, search_service):
"""Test searching in non-existent path."""
with pytest.raises(FileNotFoundError):
search_service.search_by_name("file.txt", "nonexistent")
def test_search_with_special_characters(self, temp_root, search_service):
"""Test searching for files with special characters."""
(temp_root / "file-with-dashes.txt").write_text("content")
(temp_root / "file_with_underscores.txt").write_text("content")
result = search_service.search_by_glob("*with*", ".")
assert result.total_matches >= 2