minutes icon indicating copy to clipboard operation
minutes copied to clipboard

Integration test overwrites the developer's real ~/.minutes/search.db when using a temporary output_dir

Open rymalia opened this issue 4 months ago • 2 comments

Filing this as discussed in the #573 review.

cargo test -p minutes-core writes to the developer's real search index. A single integration test replaces its contents with two fixture rows pointing into a TempDir. The index self-repairs on the next ordinary search, so this is not data loss — but a test writing to real user state under $HOME is worth closing, and the window can be long if you don't search in between.

Reproduction

$ sqlite3 ~/.minutes/search.db "select count(*) from meetings;"
88

$ cargo test -p minutes-core --no-default-features --test integration search_filters_by_content_type
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 7 filtered out; finished in 0.34s

$ sqlite3 ~/.minutes/search.db "select count(*) from meetings;"
2
$ sqlite3 ~/.minutes/search.db "select path from meetings limit 2;"
/var/folders/nn/_m7qs4vx6w7br00bxr83vtcw0000gn/T/.tmpa6Fj0J/output/2026-07-27-meeting-one.md
/var/folders/nn/_m7qs4vx6w7br00bxr83vtcw0000gn/T/.tmpa6Fj0J/output/memos/2026-07-27-memo-one.md

$ minutes search "meeting" >/dev/null      # any ordinary search repairs it
$ sqlite3 ~/.minutes/search.db "select count(*) from meetings;"
88

I noticed it because my index had been sitting in the 2-row state — I hadn't run a search since the previous test run, so nothing had repaired it.

Root cause

SearchIndex::default_db_path() is global and independent of the config (search_index.rs:90):

pub fn default_db_path() -> PathBuf {
    dirs::home_dir()
        .unwrap_or_else(|| PathBuf::from("."))
        .join(".minutes")
        .join("search.db")
}

SearchIndex::open takes a &Config but uses that global path for the database, honouring the config only for output_dir. It then does the right thing for the wrong situation — detects the mismatch and rebuilds (search_index.rs:131-136):

if stored != output_dir_str {
    tracing::info!(old = %stored, new = %output_dir_str, "output_dir changed; rebuilding index");
    schema::rebuild(&mut conn)?;

Path: tests/integration/pipeline_test.rs:176 → search::search (search.rs:560-567) → SearchIndex::open (search.rs:597) → global DB.

Nothing in the test is wrong. test_config() isolates output_dir exactly as you'd expect; the isolation just doesn't reach the database path.

The interesting part: the fix pattern already exists, but integration tests can't reach it

Unit tests that touch global state already solve this by overriding HOME — e.g. copilot/battle_card.rs:289-293, which calls rebuild_index (global graph.db) but is safe because it sets up test_home_env_lock() + HomeOverride::set(temp) first.

That pattern isn't reachable from tests/:

  • test_home_env_lock is pub(crate) (lib.rs:166)
  • HomeOverride is a private struct defined separately inside two different unit-test modules (summarize.rs:3102, copilot/battle_card.rs:255)

So the one integration test that exercises a global-state path is precisely the one with no way to isolate it. That seems more like the actual gap than anything specific to the search index.

Worth noting the same exposure exists for graph.db (graph.rs:100, also global). rebuild_index has ~22 callers across core, CLI, and Tauri, and today the only test that reaches it is the battle_card one above, which is sandboxed. But nothing enforces that, and rebuild_index_at clears every graph row before repopulating (graph.rs:389-401), so an unsandboxed test that reaches it would blow away the real graph. Recovery there is minutes people --rebuild.

Suggested fix

Smallest version that matches existing convention: make the HOME-override guard reusable from integration tests — e.g. promote test_home_env_lock plus a single shared HomeOverride into a test-support surface both unit and integration tests can use, replacing the two private copies, and use it in pipeline_test.rs.

#573 already added a path-injection seam (SearchIndex::open_at, mirroring graph::rebuild_index_at), but on its own it does not fix this reproduction: the test goes through the high-level search::search, which calls SearchIndex::open unconditionally (search.rs:597), so the injection would have to reach the search entry point too. The HOME override fixes it without touching production signatures.

I'd skip anything that infers "this is a test" from the config (e.g. rejecting a temp output_dir) — indexing a temporary or unusual output directory is legitimate user configuration.

One CI note

CI runs this exact target on macOS (.github/workflows/ci.yml:154-158) with continue-on-error: true, so any regression test added to the integration target won't gate merges as things stand. The runner is ephemeral and ~/.minutes isn't cached (:81-88), so CI itself is unaffected — this only bites local development.

Environment

  • macOS 26.5 (Apple Silicon), reproduced against main at 89537c31 (v0.24.0)
  • No local modifications to the test or to search_index.rs

rymalia avatar Jul 28 '26 00:07 rymalia

Shipped in #594, now on main.

Thank you for filing this after the #573 review, and for the reframing. I would have fixed the symptom, which is one test writing to a real database. Your framing that the isolation pattern already existed but was structurally unreachable from tests/ is the actual bug, and a better description of the problem than the reproduction that found it. The one test that touched global state was the one with no way to isolate itself.

Landed:

  • a doc-hidden test_support module with one HomeOverride, the shared lock, and with_temp_home, compiled unconditionally because that is exactly what integration tests cannot see under #[cfg(test)]
  • both private copies deleted and the 14 callers of the old lock helper moved over, so there is one implementation instead of three
  • the offending test wrapped in with_temp_home

Verified in both directions with a sentinel row in a real index: unfixed, it is replaced by fixture rows pointing into /tmp; fixed, it survives untouched.

I also took your note about graph.db having the same shape with roughly 22 rebuild_index callers. This PR does not close that, but it makes the guard reachable, which was the blocker. And I skipped inferring test-ness from the config, per your point that indexing a temporary output_dir is legitimate configuration.

silverstein avatar Jul 29 '26 04:07 silverstein

Shipped in #594, now on main.

Thank you for filing this after the #573 review, and for the reframing. I would have fixed the symptom, which is one test writing to a real database. Your framing that the isolation pattern already existed but was structurally unreachable from tests/ is the actual bug, and it is a better description of the problem than the reproduction that found it. The one test that touched global state was the one with no way to isolate itself.

Landed:

  • a doc-hidden test_support module with one HomeOverride, the shared lock, and with_temp_home, compiled unconditionally because that is exactly what integration tests cannot see under #[cfg(test)]
  • both private copies deleted and the 14 callers of the old lock helper moved over, so there is one implementation instead of three
  • the offending test wrapped in with_temp_home

Verified in both directions with a sentinel row in a real index: unfixed, it is replaced by fixture rows pointing into /tmp; fixed, it survives untouched.

I also took your note about graph.db having the same shape with roughly 22 rebuild_index callers. This PR does not close that, but it makes the guard reachable, which was the blocker. Also skipped inferring test-ness from the config, per your point that indexing a temporary output_dir is legitimate configuration.

silverstein avatar Jul 29 '26 04:07 silverstein