Coverage for mcp/resources/tests.py: 89%

47 statements  

« prev     ^ index     » next       coverage.py v7.14.1, created at 2026-06-15 15:07 +0000

1"""Test suite resources (tests:// scheme) for the GCO MCP server. 

2 

3Exposes the test README, test configuration, and individual test files 

4so the LLM can understand testing patterns, fixtures, and coverage. 

5""" 

6 

7from pathlib import Path 

8 

9from server import mcp 

10 

11PROJECT_ROOT = Path(__file__).parent.parent.parent 

12TESTS_DIR = PROJECT_ROOT / "tests" 

13_TEST_EXTENSIONS = {".py", ".md"} 

14 

15 

16@mcp.resource("tests://gco/index") 

17def tests_index() -> str: 

18 """List test suite documentation, configuration, and test files.""" 

19 lines = ["# GCO Test Suite\n"] 

20 readme = TESTS_DIR / "README.md" 

21 if readme.is_file(): 21 ↛ 25line 21 didn't jump to line 25 because the condition on line 21 was always true

22 lines.append("- `tests://gco/README` — Test suite overview, patterns, and mocking guide\n") 

23 

24 # Categorize test files 

25 test_files = sorted(f for f in TESTS_DIR.glob("test_*.py")) 

26 helper_files = sorted(f for f in TESTS_DIR.glob("_*.py")) 

27 config_files = sorted(f for f in TESTS_DIR.iterdir() if f.name in ("conftest.py", "pytest.ini")) 

28 

29 if config_files or helper_files: 29 ↛ 37line 29 didn't jump to line 37 because the condition on line 29 was always true

30 lines.append("## Test Infrastructure") 

31 for f in config_files: 

32 lines.append(f"- `tests://gco/{f.name}` — {f.stem}") 

33 for f in helper_files: 

34 lines.append(f"- `tests://gco/{f.name}` — {f.stem}") 

35 lines.append("") 

36 

37 if test_files: 37 ↛ 44line 37 didn't jump to line 44 because the condition on line 37 was always true

38 lines.append(f"## Test Files ({len(test_files)} files)") 

39 for f in test_files: 

40 lines.append(f"- `tests://gco/{f.name}` — {f.stem}") 

41 lines.append("") 

42 

43 # BATS tests 

44 bats_dir = TESTS_DIR / "BATS" 

45 if bats_dir.is_dir(): 45 ↛ 55line 45 didn't jump to line 55 because the condition on line 45 was always true

46 bats_files = sorted(bats_dir.glob("*.bats")) 

47 bats_readme = bats_dir / "README.md" 

48 if bats_files or bats_readme.is_file(): 48 ↛ 55line 48 didn't jump to line 55 because the condition on line 48 was always true

49 lines.append("## BATS Shell Tests") 

50 if bats_readme.is_file(): 50 ↛ 52line 50 didn't jump to line 52 because the condition on line 50 was always true

51 lines.append("- `tests://gco/BATS/README.md` — BATS test overview") 

52 for f in bats_files: 

53 lines.append(f"- `tests://gco/BATS/{f.name}` — {f.stem}") 

54 

55 return "\n".join(lines) 

56 

57 

58@mcp.resource("tests://gco/{filepath*}") 

59def test_file_resource(filepath: str) -> str: 

60 """Read a test file, helper, or configuration file. 

61 

62 Args: 

63 filepath: Path relative to tests/ (e.g. test_mcp_server.py, conftest.py, BATS/README.md). 

64 """ 

65 path = TESTS_DIR / filepath 

66 if not path.is_file(): 

67 # Try with .md extension for README 

68 path = TESTS_DIR / f"{filepath}.md" 

69 if not path.is_file(): 

70 return f"Test file '{filepath}' not found." 

71 if path.suffix not in (_TEST_EXTENSIONS | {".bats"}): 71 ↛ 72line 71 didn't jump to line 72 because the condition on line 71 was never true

72 return f"File type '{path.suffix}' not served." 

73 return path.read_text()