Files
DeepTutor/tests/services/config/test_knowledge_base_config.py
Bingxi Zhao (Frank) 100576da69 release: v1.1.2 — schema-driven channels, secret masking, RAG simplification, KB-consistency hardening
* TutorBot Channels tab is now schema-driven across every built-in & plugin channel
  (GET /api/v1/tutorbot/channels/schema), with per-field secret reveal toggles.
* Channel secrets are masked end-to-end; raw values only surface via the explicit
  ?include_secrets=true edit-form route. PATCH validates ChannelsConfig at the API
  boundary and returns 422 on bad payloads. reload_channels is serialised by an
  asyncio.Lock and records last_reload_error for the UI.
* Collapsed services/rag/ to a single LlamaIndex pipeline: removed ~2.6k lines of
  unused components/* + pipeline.py + types.py + utils/ + parsers/. rag_provider
  is now a stub; legacy values silently coerce to llamaindex.
* Centralised file routing into services/rag/file_routing.py (FileTypeRouter /
  DocumentType / FileClassification); dropped per-provider extension helpers.
* Defensive RAG/KB consistency: deep_solve / deep_research strip rag/kb when no
  KB is attached; SolveToolRuntime + ResearchPipeline + DecomposeAgent
  short-circuit instead of falling back to non-existent default KBs (DE-all,
  ai_textbook).
* Externalised AgenticChatPipeline prompts into agentic_chat.yaml (zh + en).
* Added Thai README (#337).
* New test modules: file_routing, knowledge_base_config, channel_schema,
  channel_secrets, rag_consistency, research_pipeline_rag.

Full notes: assets/releases/ver1-1-2.md

Made-with: Cursor
2026-04-18 11:33:53 +08:00

148 lines
5.7 KiB
Python

"""Tests for KnowledgeBaseConfigService stub + legacy-config migration."""
from __future__ import annotations
import json
from pathlib import Path
import pytest
from deeptutor.services.config.knowledge_base_config import (
KnowledgeBaseConfigService,
)
def _write_kb_config(path: Path, payload: dict) -> None:
path.parent.mkdir(parents=True, exist_ok=True)
path.write_text(json.dumps(payload), encoding="utf-8")
@pytest.fixture
def fresh_service(tmp_path: Path) -> KnowledgeBaseConfigService:
"""A KBConfigService bound to a tmp config path, bypassing the singleton."""
return KnowledgeBaseConfigService(config_path=tmp_path / "kb_config.json")
class TestStubProviderApi:
def test_get_rag_provider_always_llamaindex(self, fresh_service) -> None:
assert fresh_service.get_rag_provider("any-kb") == "llamaindex"
def test_set_rag_provider_coerces_legacy_to_llamaindex(self, fresh_service) -> None:
fresh_service.set_rag_provider("kb-1", "lightrag")
cfg = fresh_service.get_kb_config("kb-1")
assert cfg["rag_provider"] == "llamaindex"
def test_set_rag_provider_coerces_unknown_to_llamaindex(self, fresh_service) -> None:
fresh_service.set_rag_provider("kb-2", "totally-unknown")
cfg = fresh_service.get_kb_config("kb-2")
assert cfg["rag_provider"] == "llamaindex"
def test_get_kb_config_overrides_provider_field(self, fresh_service) -> None:
"""Even if a stale entry leaks `rag_provider: lightrag`, reads return llamaindex."""
fresh_service._config.setdefault("knowledge_bases", {})["kb-3"] = {
"path": "kb-3",
"rag_provider": "lightrag",
}
cfg = fresh_service.get_kb_config("kb-3")
assert cfg["rag_provider"] == "llamaindex"
class TestPayloadNormalizationOnLoad:
def test_legacy_provider_in_file_is_rewritten_and_marks_reindex(self, tmp_path: Path) -> None:
config_path = tmp_path / "kb_config.json"
_write_kb_config(
config_path,
{
"defaults": {"rag_provider": "lightrag", "search_mode": "hybrid"},
"knowledge_bases": {
"old-kb": {"path": "old-kb", "rag_provider": "lightrag"},
},
},
)
service = KnowledgeBaseConfigService(config_path=config_path)
kb = service._config["knowledge_bases"]["old-kb"]
assert kb["rag_provider"] == "llamaindex"
assert kb["needs_reindex"] is True
defaults = service._config["defaults"]
assert defaults["rag_provider"] == "llamaindex"
def test_existing_llamaindex_kb_is_left_alone(self, tmp_path: Path) -> None:
config_path = tmp_path / "kb_config.json"
_write_kb_config(
config_path,
{
"defaults": {"rag_provider": "llamaindex"},
"knowledge_bases": {
"fresh-kb": {"path": "fresh-kb", "rag_provider": "llamaindex"},
},
},
)
service = KnowledgeBaseConfigService(config_path=config_path)
kb = service._config["knowledge_bases"]["fresh-kb"]
assert kb["rag_provider"] == "llamaindex"
assert kb.get("needs_reindex", False) is False
def test_legacy_storage_dir_marks_reindex(self, tmp_path: Path) -> None:
"""If the on-disk storage uses the old layout, force a reindex flag."""
config_path = tmp_path / "kb_config.json"
kb_dir = tmp_path / "stale-kb"
(kb_dir / "rag_storage").mkdir(parents=True)
_write_kb_config(
config_path,
{
"defaults": {"rag_provider": "llamaindex"},
"knowledge_bases": {
"stale-kb": {"path": "stale-kb", "rag_provider": "llamaindex"},
},
},
)
service = KnowledgeBaseConfigService(config_path=config_path)
kb = service._config["knowledge_bases"]["stale-kb"]
assert kb["needs_reindex"] is True
def test_modern_storage_dir_does_not_trigger_reindex(self, tmp_path: Path) -> None:
config_path = tmp_path / "kb_config.json"
kb_dir = tmp_path / "modern-kb"
(kb_dir / "llamaindex_storage").mkdir(parents=True)
(kb_dir / "rag_storage").mkdir(parents=True) # legacy dir co-existing
_write_kb_config(
config_path,
{
"knowledge_bases": {
"modern-kb": {"path": "modern-kb", "rag_provider": "llamaindex"},
},
},
)
service = KnowledgeBaseConfigService(config_path=config_path)
kb = service._config["knowledge_bases"]["modern-kb"]
# legacy dir present + modern dir present => no forced reindex
assert kb.get("needs_reindex", False) is False
class TestPersistence:
def test_set_kb_config_normalizes_on_save(self, tmp_path: Path) -> None:
config_path = tmp_path / "kb_config.json"
service = KnowledgeBaseConfigService(config_path=config_path)
service.set_kb_config("new-kb", {"rag_provider": "raganything", "description": "x"})
on_disk = json.loads(config_path.read_text(encoding="utf-8"))
assert on_disk["knowledge_bases"]["new-kb"]["rag_provider"] == "llamaindex"
assert on_disk["knowledge_bases"]["new-kb"]["description"] == "x"
def test_search_mode_is_preserved(self, fresh_service) -> None:
fresh_service.set_search_mode("kb", "naive")
assert fresh_service.get_search_mode("kb") == "naive"
def test_set_default_kb(self, fresh_service) -> None:
fresh_service.set_default_kb("primary")
assert fresh_service.get_default_kb() == "primary"
fresh_service.set_default_kb(None)
assert fresh_service.get_default_kb() is None