|
| 1 | +"""Tests for the get_usage_stats session metrics.""" |
| 2 | + |
| 3 | +import time |
| 4 | + |
| 5 | +import pytest |
| 6 | + |
| 7 | + |
| 8 | +@pytest.fixture(autouse=True) |
| 9 | +def _reset_server_state(): |
| 10 | + """Reset server module-level state before each test.""" |
| 11 | + import mcp_codebase_index.server as srv |
| 12 | + |
| 13 | + srv._session_start = time.time() |
| 14 | + srv._tool_call_counts.clear() |
| 15 | + srv._total_chars_returned = 0 |
| 16 | + srv._indexer = None |
| 17 | + srv._query_fns = None |
| 18 | + yield |
| 19 | + srv._tool_call_counts.clear() |
| 20 | + srv._total_chars_returned = 0 |
| 21 | + |
| 22 | + |
| 23 | +class TestFormatDuration: |
| 24 | + def test_seconds(self): |
| 25 | + from mcp_codebase_index.server import _format_duration |
| 26 | + |
| 27 | + assert _format_duration(45) == "45s" |
| 28 | + |
| 29 | + def test_minutes(self): |
| 30 | + from mcp_codebase_index.server import _format_duration |
| 31 | + |
| 32 | + assert _format_duration(125) == "2m 5s" |
| 33 | + |
| 34 | + def test_hours(self): |
| 35 | + from mcp_codebase_index.server import _format_duration |
| 36 | + |
| 37 | + assert _format_duration(3725) == "1h 2m" |
| 38 | + |
| 39 | + |
| 40 | +class TestFormatUsageStats: |
| 41 | + def test_empty_session(self): |
| 42 | + from mcp_codebase_index.server import _format_usage_stats |
| 43 | + |
| 44 | + result = _format_usage_stats() |
| 45 | + assert "Total queries: 0" in result |
| 46 | + assert "Total chars returned: 0" in result |
| 47 | + |
| 48 | + def test_with_tool_calls(self): |
| 49 | + import mcp_codebase_index.server as srv |
| 50 | + |
| 51 | + srv._tool_call_counts["find_symbol"] = 5 |
| 52 | + srv._tool_call_counts["get_function_source"] = 3 |
| 53 | + srv._total_chars_returned = 1234 |
| 54 | + |
| 55 | + result = srv._format_usage_stats() |
| 56 | + assert "Total queries: 8" in result |
| 57 | + assert "find_symbol: 5" in result |
| 58 | + assert "get_function_source: 3" in result |
| 59 | + assert "Total chars returned: 1,234" in result |
| 60 | + |
| 61 | + def test_usage_stats_call_excluded_from_query_count(self): |
| 62 | + import mcp_codebase_index.server as srv |
| 63 | + |
| 64 | + srv._tool_call_counts["find_symbol"] = 3 |
| 65 | + srv._tool_call_counts["get_usage_stats"] = 2 |
| 66 | + |
| 67 | + result = srv._format_usage_stats() |
| 68 | + assert "Total queries: 3" in result |
| 69 | + # get_usage_stats should not appear in the per-tool breakdown |
| 70 | + assert "get_usage_stats" not in result |
| 71 | + |
| 72 | + def test_with_indexed_project(self, tmp_path): |
| 73 | + import mcp_codebase_index.server as srv |
| 74 | + from mcp_codebase_index.project_indexer import ProjectIndexer |
| 75 | + |
| 76 | + # Create a project with enough source to exceed returned chars |
| 77 | + (tmp_path / "main.py").write_text("def hello():\n return 'world'\n" * 100) |
| 78 | + (tmp_path / "utils.py").write_text("def helper():\n return 42\n" * 100) |
| 79 | + |
| 80 | + indexer = ProjectIndexer(str(tmp_path), include_patterns=["**/*.py"]) |
| 81 | + indexer.index() |
| 82 | + srv._indexer = indexer |
| 83 | + |
| 84 | + srv._tool_call_counts["find_symbol"] = 5 |
| 85 | + srv._total_chars_returned = 200 |
| 86 | + |
| 87 | + result = srv._format_usage_stats() |
| 88 | + assert "Total source in index:" in result |
| 89 | + assert "Estimated token savings:" in result |
| 90 | + |
| 91 | + def test_token_savings_calculation(self, tmp_path): |
| 92 | + import mcp_codebase_index.server as srv |
| 93 | + from mcp_codebase_index.project_indexer import ProjectIndexer |
| 94 | + |
| 95 | + # Create a project with known size |
| 96 | + (tmp_path / "big.py").write_text("x = 1\n" * 1000) # ~6000 chars |
| 97 | + |
| 98 | + indexer = ProjectIndexer(str(tmp_path), include_patterns=["**/*.py"]) |
| 99 | + indexer.index() |
| 100 | + srv._indexer = indexer |
| 101 | + |
| 102 | + srv._tool_call_counts["find_symbol"] = 10 |
| 103 | + srv._total_chars_returned = 500 |
| 104 | + |
| 105 | + result = srv._format_usage_stats() |
| 106 | + assert "Estimated without indexer:" in result |
| 107 | + assert "Estimated with indexer:" in result |
| 108 | + # 500 chars returned vs 6000 * 10 = 60000 naive |
| 109 | + assert "tokens" in result |
| 110 | + |
| 111 | + def test_no_savings_section_without_index(self): |
| 112 | + import mcp_codebase_index.server as srv |
| 113 | + |
| 114 | + srv._tool_call_counts["find_symbol"] = 3 |
| 115 | + srv._total_chars_returned = 100 |
| 116 | + |
| 117 | + result = srv._format_usage_stats() |
| 118 | + assert "Estimated token savings:" not in result |
0 commit comments