|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import importlib |
| 4 | +import sys |
| 5 | +import types |
| 6 | +from pathlib import Path |
| 7 | +from types import SimpleNamespace |
| 8 | +from typing import Any |
| 9 | + |
| 10 | +import pytest |
| 11 | + |
| 12 | + |
| 13 | +class _FakeStreamlit(types.ModuleType): |
| 14 | + def __init__(self) -> None: |
| 15 | + super().__init__("streamlit") |
| 16 | + |
| 17 | + def cache_resource(self, **_: Any) -> Any: |
| 18 | + return lambda function: function |
| 19 | + |
| 20 | + |
| 21 | +@pytest.fixture |
| 22 | +def bootstrap_module(monkeypatch: pytest.MonkeyPatch) -> Any: |
| 23 | + monkeypatch.delitem(sys.modules, "app.bootstrap", raising=False) |
| 24 | + monkeypatch.setitem(sys.modules, "streamlit", _FakeStreamlit()) |
| 25 | + return importlib.import_module("app.bootstrap") |
| 26 | + |
| 27 | + |
| 28 | +def _settings(tmp_path: Path, **overrides: Any) -> SimpleNamespace: |
| 29 | + values = { |
| 30 | + "default_provider": "ollama", |
| 31 | + "mistral_api_key": "embed-key", |
| 32 | + "mistral_gen_model": "codestral-latest", |
| 33 | + "mistral_embed_model": "mistral-embed", |
| 34 | + "mistral_base_url": "https://api.mistral.ai/v1", |
| 35 | + "llm_cache_dir": tmp_path / "cache", |
| 36 | + "llm_cache_size_limit_gb": 1.0, |
| 37 | + } |
| 38 | + values.update(overrides) |
| 39 | + return SimpleNamespace(**values) |
| 40 | + |
| 41 | + |
| 42 | +def test_bootstrap_uses_configured_sql_provider( |
| 43 | + bootstrap_module: Any, |
| 44 | + monkeypatch: pytest.MonkeyPatch, |
| 45 | + tmp_path: Path, |
| 46 | +) -> None: |
| 47 | + settings = _settings(tmp_path) |
| 48 | + persist_dir = tmp_path / "repo" / "chroma_data" |
| 49 | + persist_dir.mkdir(parents=True) |
| 50 | + foreign_cwd = tmp_path / "foreign" |
| 51 | + foreign_cwd.mkdir() |
| 52 | + monkeypatch.chdir(foreign_cwd) |
| 53 | + |
| 54 | + registry = object() |
| 55 | + schema_index = object() |
| 56 | + sql_provider = object() |
| 57 | + provider_calls: list[tuple[str, object]] = [] |
| 58 | + client_paths: list[str] = [] |
| 59 | + |
| 60 | + monkeypatch.setattr(bootstrap_module, "get_settings", lambda: settings) |
| 61 | + monkeypatch.setattr( |
| 62 | + bootstrap_module, |
| 63 | + "under_root", |
| 64 | + lambda *parts: persist_dir.parent.joinpath(*parts), |
| 65 | + raising=False, |
| 66 | + ) |
| 67 | + monkeypatch.setattr(bootstrap_module, "get_default_registry", lambda: registry) |
| 68 | + monkeypatch.setattr( |
| 69 | + bootstrap_module.chromadb, |
| 70 | + "PersistentClient", |
| 71 | + lambda **kwargs: client_paths.append(kwargs["path"]) or object(), |
| 72 | + ) |
| 73 | + monkeypatch.setattr(bootstrap_module, "MistralProvider", lambda **_: object()) |
| 74 | + monkeypatch.setattr(bootstrap_module, "CachingEmbeddingProvider", lambda *_, **__: object()) |
| 75 | + monkeypatch.setattr(bootstrap_module, "SchemaIndex", lambda **_: schema_index) |
| 76 | + monkeypatch.setattr( |
| 77 | + bootstrap_module, |
| 78 | + "build_provider", |
| 79 | + lambda name, settings: provider_calls.append((name, settings)) or object(), |
| 80 | + ) |
| 81 | + monkeypatch.setattr( |
| 82 | + bootstrap_module, |
| 83 | + "CachingLLMProvider", |
| 84 | + lambda *_, **__: sql_provider, |
| 85 | + ) |
| 86 | + |
| 87 | + result = bootstrap_module.bootstrap() |
| 88 | + |
| 89 | + assert provider_calls == [("ollama", settings)] |
| 90 | + assert client_paths == [str(persist_dir)] |
| 91 | + assert result == (registry, schema_index, sql_provider, sql_provider) |
| 92 | + |
| 93 | + |
| 94 | +def test_bootstrap_reports_mistral_key_is_for_embeddings( |
| 95 | + bootstrap_module: Any, |
| 96 | + monkeypatch: pytest.MonkeyPatch, |
| 97 | + tmp_path: Path, |
| 98 | +) -> None: |
| 99 | + monkeypatch.setattr( |
| 100 | + bootstrap_module, |
| 101 | + "get_settings", |
| 102 | + lambda: _settings(tmp_path, mistral_api_key=""), |
| 103 | + ) |
| 104 | + |
| 105 | + with pytest.raises(RuntimeError, match="required for embeddings"): |
| 106 | + bootstrap_module.bootstrap() |
0 commit comments