Spaces:
Running
Running
Jeremiah Lowin commited on
Commit ·
d7fad77
1
Parent(s): 7ea3e33
Update tests
Browse files
tests/servers/test_file_browser.py
DELETED
|
@@ -1,47 +0,0 @@
|
|
| 1 |
-
import json
|
| 2 |
-
from fastmcp import FastMCP
|
| 3 |
-
import pytest
|
| 4 |
-
from pathlib import Path
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
@pytest.fixture(scope="session")
|
| 8 |
-
def test_dir(tmp_path_factory) -> Path:
|
| 9 |
-
"""Create a temporary directory with test files."""
|
| 10 |
-
tmp = tmp_path_factory.mktemp("test_files")
|
| 11 |
-
|
| 12 |
-
# Create test files
|
| 13 |
-
(tmp / "example.py").write_text("print('hello world')")
|
| 14 |
-
(tmp / "readme.md").write_text("# Test Directory\nThis is a test.")
|
| 15 |
-
(tmp / "config.json").write_text('{"test": true}')
|
| 16 |
-
|
| 17 |
-
return tmp
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
@pytest.fixture
|
| 21 |
-
def mcp(test_dir: Path) -> FastMCP:
|
| 22 |
-
mcp = FastMCP()
|
| 23 |
-
|
| 24 |
-
@mcp.resource("fs://test_dir")
|
| 25 |
-
def list_files() -> list[str]:
|
| 26 |
-
"""List the files in the test directory"""
|
| 27 |
-
return [str(f) for f in test_dir.iterdir()]
|
| 28 |
-
|
| 29 |
-
return mcp
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
async def test_list_resources(mcp: FastMCP):
|
| 33 |
-
resources = await mcp.list_resources()
|
| 34 |
-
assert len(resources) == 1
|
| 35 |
-
assert str(resources[0].uri) == "fs://test_dir"
|
| 36 |
-
assert resources[0].name == "test_dir"
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
async def test_read_resource(mcp: FastMCP):
|
| 40 |
-
files = await mcp.read_resource("fs://test_dir")
|
| 41 |
-
files = json.loads(files)
|
| 42 |
-
|
| 43 |
-
assert isinstance(files, list)
|
| 44 |
-
assert len(files) == 3
|
| 45 |
-
assert any("example.py" in f for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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
from fastmcp import FastMCP
|
| 3 |
+
import pytest
|
| 4 |
+
from pathlib import Path
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
@pytest.fixture()
|
| 8 |
+
def test_dir(tmp_path_factory) -> Path:
|
| 9 |
+
"""Create a temporary directory with test files."""
|
| 10 |
+
tmp = tmp_path_factory.mktemp("test_files")
|
| 11 |
+
|
| 12 |
+
# Create test files
|
| 13 |
+
(tmp / "example.py").write_text("print('hello world')")
|
| 14 |
+
(tmp / "readme.md").write_text("# Test Directory\nThis is a test.")
|
| 15 |
+
(tmp / "config.json").write_text('{"test": true}')
|
| 16 |
+
|
| 17 |
+
return tmp
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
@pytest.fixture
|
| 21 |
+
def mcp() -> FastMCP:
|
| 22 |
+
mcp = FastMCP()
|
| 23 |
+
|
| 24 |
+
return mcp
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
@pytest.fixture(autouse=True)
|
| 28 |
+
def resources(mcp: FastMCP, test_dir: Path) -> None:
|
| 29 |
+
@mcp.resource("dir://test_dir")
|
| 30 |
+
def list_test_dir() -> list[str]:
|
| 31 |
+
"""List the files in the test directory"""
|
| 32 |
+
return [str(f) for f in test_dir.iterdir()]
|
| 33 |
+
|
| 34 |
+
@mcp.resource("file://test_dir/example.py")
|
| 35 |
+
def read_example_py() -> str:
|
| 36 |
+
"""Read the example.py file"""
|
| 37 |
+
try:
|
| 38 |
+
return (test_dir / "example.py").read_text()
|
| 39 |
+
except FileNotFoundError:
|
| 40 |
+
return "File not found"
|
| 41 |
+
|
| 42 |
+
@mcp.resource("file://test_dir/readme.md")
|
| 43 |
+
def read_readme_md() -> str:
|
| 44 |
+
"""Read the readme.md file"""
|
| 45 |
+
try:
|
| 46 |
+
return (test_dir / "readme.md").read_text()
|
| 47 |
+
except FileNotFoundError:
|
| 48 |
+
return "File not found"
|
| 49 |
+
|
| 50 |
+
@mcp.resource("file://test_dir/config.json")
|
| 51 |
+
def read_config_json() -> str:
|
| 52 |
+
"""Read the config.json file"""
|
| 53 |
+
try:
|
| 54 |
+
return (test_dir / "config.json").read_text()
|
| 55 |
+
except FileNotFoundError:
|
| 56 |
+
return "File not found"
|
| 57 |
+
|
| 58 |
+
return mcp
|
| 59 |
+
|
| 60 |
+
|
| 61 |
+
@pytest.fixture(autouse=True)
|
| 62 |
+
def tools(mcp: FastMCP, test_dir: Path) -> None:
|
| 63 |
+
@mcp.tool()
|
| 64 |
+
def delete_file(path: str) -> bool:
|
| 65 |
+
# ensure path is in test_dir
|
| 66 |
+
if Path(path).resolve().parent != test_dir:
|
| 67 |
+
raise ValueError(f"Path must be in test_dir: {path}")
|
| 68 |
+
Path(path).unlink()
|
| 69 |
+
return True
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
async def test_list_resources(mcp: FastMCP):
|
| 73 |
+
resources = await mcp.list_resources()
|
| 74 |
+
assert len(resources) == 4
|
| 75 |
+
|
| 76 |
+
assert [str(r.uri) for r in resources] == [
|
| 77 |
+
"dir://test_dir",
|
| 78 |
+
"file://test_dir/example.py",
|
| 79 |
+
"file://test_dir/readme.md",
|
| 80 |
+
"file://test_dir/config.json",
|
| 81 |
+
]
|
| 82 |
+
|
| 83 |
+
|
| 84 |
+
async def test_read_resource_dir(mcp: FastMCP):
|
| 85 |
+
files = await mcp.read_resource("dir://test_dir")
|
| 86 |
+
files = json.loads(files)
|
| 87 |
+
|
| 88 |
+
assert sorted([Path(f).name for f in files]) == [
|
| 89 |
+
"config.json",
|
| 90 |
+
"example.py",
|
| 91 |
+
"readme.md",
|
| 92 |
+
]
|
| 93 |
+
|
| 94 |
+
|
| 95 |
+
async def test_read_resource_file(mcp: FastMCP):
|
| 96 |
+
result = await mcp.read_resource("file://test_dir/example.py")
|
| 97 |
+
assert result == "print('hello world')"
|
| 98 |
+
|
| 99 |
+
|
| 100 |
+
async def test_delete_file(mcp: FastMCP, test_dir: Path):
|
| 101 |
+
await mcp.call_tool(
|
| 102 |
+
"delete_file", arguments=dict(path=str(test_dir / "example.py"))
|
| 103 |
+
)
|
| 104 |
+
assert not (test_dir / "example.py").exists()
|
| 105 |
+
|
| 106 |
+
|
| 107 |
+
async def test_delete_file_and_check_resources(mcp: FastMCP, test_dir: Path):
|
| 108 |
+
await mcp.call_tool(
|
| 109 |
+
"delete_file", arguments=dict(path=str(test_dir / "example.py"))
|
| 110 |
+
)
|
| 111 |
+
result = await mcp.read_resource("file://test_dir/example.py")
|
| 112 |
+
assert result == "File not found"
|