Coverage for gco_mcp/resources/tests.py: 88.89%

46 statements  

« prev     ^ index     » next       coverage.py v7.15.2, created at 2026-07-30 21:22 +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 cli_runner import PROJECT_ROOT # runtime-resolved checkout root (uvx-safe) 

8from server import mcp 

9 

10TESTS_DIR = PROJECT_ROOT / "tests" 

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

12 

13 

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

15def tests_index() -> str: 

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

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

18 readme = TESTS_DIR / "README.md" 

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

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

21 

22 # Categorize test files 

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

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

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

26 

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

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

29 for f in config_files: 

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

31 for f in helper_files: 

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

33 lines.append("") 

34 

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

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

37 for f in test_files: 

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

39 lines.append("") 

40 

41 # BATS tests 

42 bats_dir = TESTS_DIR / "BATS" 

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

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

45 bats_readme = bats_dir / "README.md" 

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

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

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

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

50 for f in bats_files: 

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

52 

53 return "\n".join(lines) 

54 

55 

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

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

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

59 

60 Args: 

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

62 """ 

63 path = TESTS_DIR / filepath 

64 if not path.is_file(): 

65 # Try with .md extension for README 

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

67 if not path.is_file(): 

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

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

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

71 return path.read_text()