from unittest.mock import MagicMock, patch
import pytest
from src.rag_engine import RAGEngine
from src.result_types import Result, ErrorKind, ErrorInfo, NilRAGState


def test_init_vector_store_returns_result_not_raises() -> None:
 config = MagicMock()
 config.vector_store.provider = "chroma"
 config.vector_store.collection_name = "test"
 config.embedding_provider = "gemini"
 engine = RAGEngine(base_dir="/tmp", config=config)
 with patch("src.rag_engine._get_chromadb", return_value=None):
  r = engine._init_vector_store_result()
  assert isinstance(r, Result)
  assert not r.ok
  assert len(r.errors) >= 1
  assert r.errors[0].kind == ErrorKind.CONFIG


def test_init_vector_store_unknown_provider_returns_error_info() -> None:
 config = MagicMock()
 config.vector_store.provider = "unknown_provider_xyz"
 config.embedding_provider = "gemini"
 engine = RAGEngine(base_dir="/tmp", config=config)
 r = engine._init_vector_store_result()
 assert not r.ok
 assert r.errors[0].kind == ErrorKind.CONFIG
 assert "unknown_provider_xyz" in r.errors[0].message


def test_validate_collection_dim_returns_result() -> None:
 config = MagicMock()
 config.vector_store.provider = "chroma"
 config.vector_store.collection_name = "test"
 config.embedding_provider = "gemini"
 engine = RAGEngine(base_dir="/tmp", config=config)
 engine.collection = None
 engine.embedding_provider = None
 r = engine._validate_collection_dim_result()
 assert isinstance(r, Result)
 assert r.ok


def test_is_empty_uses_nil_rag_state_when_not_configured() -> None:
 config = MagicMock()
 config.enabled = False
 engine = RAGEngine(base_dir="/tmp", config=config)
 state = engine._get_state()
 assert isinstance(state, NilRAGState)
 assert state.enabled is False
 assert state.is_empty_result is True
 assert state.errors == []
