Skip to content

Codebase intelligence for AI and humans: code health scores, auto-generated docs, git analytics, dead code detection, and architectural decisions via MCP.

test(savings): stop the test suite banking savings in the real repository (#2442)

Raghav Chamadiya90648f3d8bc5
diff --git a/tests/unit/server/mcp/conftest.py b/tests/unit/server/mcp/conftest.py
index 188ea1a45..534aa4d1d 100644
--- a/tests/unit/server/mcp/conftest.py
+++ b/tests/unit/server/mcp/conftest.py
@@ -692,3 +692,26 @@ async def health_data(session: AsyncSession, populated_db: str) -> str:
)
await session.commit()
return rid
+
+
[email protected](autouse=True)
+def _no_savings_writes_outside_a_test_repo(monkeypatch):
+ """Keep a tool call in these tests from banking a saving in the real repo.
+
+ Clearing the process-global repo path is not enough: the budget layer falls
+ back to the current working directory, which under pytest is the checkout
+ itself. So a test driving ``tool_middleware`` wrote canonical events into
+ the developer's own ledger, where they look like live traffic and are
+ recognisable only by their synthetic tool names.
+
+ Neutralised at the write rather than at the resolver, because the same
+ resolver decides where omission refs are stored and the budget tests depend
+ on that still working. A test that wants a real event drives the recorder
+ itself.
+ """
+ from repowise.server.mcp_server import _state
+
+ monkeypatch.setattr(_state, "_repo_path", None, raising=False)
+ from repowise.core.savings import recorder
+
+ monkeypatch.setattr(recorder, "record_event", lambda repo_root, payload: False)
diff --git a/tests/unit/server/mcp/test_savings_isolation.py b/tests/unit/server/mcp/test_savings_isolation.py
new file mode 100644
index 000000000..d6d3f1d20
--- /dev/null
+++ b/tests/unit/server/mcp/test_savings_isolation.py
@@ -0,0 +1,34 @@
+"""A test must not bank a saving in the developer's own ledger.
+
+The canonical savings event goes to whatever repository the budget layer
+resolved, and that resolution falls back to the current working directory,
+which under pytest is the checkout itself. Tests driving ``tool_middleware``
+wrote rows into the real repository's sidecar, where they look like live
+traffic and are recognisable only by their synthetic tool names.
+"""
+
+from __future__ import annotations
+
+import pytest
+
+from repowise.server.mcp_server import tool_middleware
+
+
+async def test_a_tool_call_in_a_test_banks_nothing(monkeypatch: pytest.MonkeyPatch) -> None:
+ written: list[object] = []
+ from repowise.core.savings import repository
+
+ monkeypatch.setattr(
+ repository.SavingsRepository,
+ "record_event",
+ lambda self, event: written.append(event) or True,
+ )
+
+ async def odd_tool(x: int) -> dict:
+ return {"value": x, "_meta": {}}
+
+ result = await tool_middleware(odd_tool)(1)
+
+ assert result["value"] == 1
+ assert written == []