|
| 1 | +"""Regression tests: the KB-metadata SQLite must honour `database.path`. |
| 2 | +
|
| 3 | +The vector store has always read `database.chroma_path`, while the session |
| 4 | +store was hardcoded to a working-directory-relative `./data/perspicacite.db`. |
| 5 | +A second instance therefore listed the main hub's knowledge bases while writing |
| 6 | +its vectors elsewhere, so every KB it created "existed" but retrieved nothing. |
| 7 | +""" |
| 8 | + |
| 9 | +from pathlib import Path |
| 10 | + |
| 11 | +import pytest |
| 12 | + |
| 13 | +from perspicacite.config.paths import LEGACY_SESSION_DB_PATH, resolve_session_db_path |
| 14 | +from perspicacite.config.schema import Config |
| 15 | + |
| 16 | + |
| 17 | +def test_unset_path_keeps_the_legacy_location(): |
| 18 | + """Existing deployments keep their metadata where it already lives.""" |
| 19 | + assert resolve_session_db_path(Config()) == LEGACY_SESSION_DB_PATH |
| 20 | + |
| 21 | + |
| 22 | +def test_setting_only_chroma_path_keeps_the_legacy_location(): |
| 23 | + """Touching the vector-store path must not move the metadata store.""" |
| 24 | + config = Config(database={"chroma_path": "/tmp/isolated-chroma"}) |
| 25 | + assert resolve_session_db_path(config) == LEGACY_SESSION_DB_PATH |
| 26 | + |
| 27 | + |
| 28 | +def test_explicit_path_is_honoured_and_home_is_expanded(): |
| 29 | + """`~` must be expanded; SessionStore would otherwise create a `~` directory.""" |
| 30 | + resolved = resolve_session_db_path(Config(database={"path": "~/isolated/memory.db"})) |
| 31 | + assert resolved == Path.home() / "isolated" / "memory.db" |
| 32 | + assert "~" not in str(resolved) |
| 33 | + |
| 34 | + |
| 35 | +def test_env_var_override_reaches_the_resolver(monkeypatch, tmp_path): |
| 36 | + """PERSPICACITE_DB_PATH is documented as the isolation switch.""" |
| 37 | + from perspicacite.config.loader import load_config |
| 38 | + |
| 39 | + target = tmp_path / "instance" / "memory.db" |
| 40 | + monkeypatch.setenv("PERSPICACITE_DB_PATH", str(target)) |
| 41 | + assert resolve_session_db_path(load_config(None)) == target |
| 42 | + |
| 43 | + |
| 44 | +@pytest.mark.asyncio |
| 45 | +async def test_app_state_opens_the_configured_database(monkeypatch, tmp_path): |
| 46 | + """AppState.initialize must hand the configured path to SessionStore.""" |
| 47 | + from perspicacite.web import state as state_module |
| 48 | + |
| 49 | + target = tmp_path / "instance" / "memory.db" |
| 50 | + monkeypatch.setenv("PERSPICACITE_DB_PATH", str(target)) |
| 51 | + monkeypatch.setenv("PERSPICACITE_ALLOW_MISSING_LLM_KEYS", "1") |
| 52 | + monkeypatch.setenv("PERSPICACITE_DB_CHROMA_PATH", str(tmp_path / "chroma")) |
| 53 | + |
| 54 | + opened: list[Path] = [] |
| 55 | + |
| 56 | + class _RecordingSessionStore: |
| 57 | + def __init__(self, db_path): |
| 58 | + opened.append(Path(db_path)) |
| 59 | + self.db_path = Path(db_path) |
| 60 | + |
| 61 | + async def init_db(self): |
| 62 | + return None |
| 63 | + |
| 64 | + monkeypatch.setattr(state_module, "SessionStore", _RecordingSessionStore) |
| 65 | + |
| 66 | + app_state = state_module.AppState() |
| 67 | + await app_state.initialize() |
| 68 | + |
| 69 | + assert opened == [target] |
| 70 | + assert opened[0] != LEGACY_SESSION_DB_PATH |
0 commit comments