Skip to content

test(mcp): isolate CspMcpServer tests from the real ~/.csp/index cache #103

Description

@amondnet

Summary

The three CspMcpServer tests in crates/csp/src/bin/csp/mcp_server.rs (search_tool_call_returns_json_payload, find_related_tool_call_reports_missing_chunk, and search_tool_call_honors_per_call_content) build real indexes for a random tempfile directory through the production DiskLoadOrBuild seam. load_or_build_index runs with LoadOrBuildOptions { base_dir: None, .. }, so every run writes into the developer's real ~/.csp/index/.

The cache key is hashed from the random temp path, so the entries can never be reused and are never removed. Measured with an isolated HOME: one cargo test run leaves 5 orphan index directories (~100 KB) — 2 from the pre-existing tests, 3 more since #99 added the per-call content test (one per content variant). Repeated local or CI runs accumulate them indefinitely; csp clear index is currently the only remedy, and it wipes the whole cache.

The tests already apply the module's one existing isolation hook (server.stats_file = None keeps telemetry out of ~/.csp/savings.jsonl); there is no equivalent hook for the index cache because CspMcpServer.cache is the monomorphic IndexCache<DiskLoadOrBuild> and DiskLoadOrBuild exposes no base_dir.

Surfaced by the gpt review engine on #99; the validator classified it as pre-existing on main rather than introduced by that PR, so it was deferred here.

Proposed fix

Give DiskLoadOrBuild an optional cache home (e.g. a base_dir: Option<PathBuf> field threaded into LoadOrBuildOptions.base_dir and source_fingerprint), and expose a #[cfg(test)]-gated constructor or field on CspMcpServer so all three tests point the cache at a second tempdir() that is deleted with the fixture. This matches the crate's existing seams (IndexCache::with_seam(Stub) in the lib tests, run_clear_at(&CacheLocation, ..) in the CLI) rather than adding a public production constructor that exists only for tests.

Acceptance

  • All three CspMcpServer tests run with HOME/~/.csp untouched (verify by running the test binary with an isolated HOME and asserting ~/.csp/index/ stays absent).
  • No new public API on CspMcpServer outside #[cfg(test)].
  • cargo fmt --all && cargo clippy --all-targets --all-features -- -D warnings && cargo test --workspace pass.

Refs

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    status:proposalProposal for discussiontype:testTesting improvements or additions

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions