mirror of
https://github.com/HKUDS/DeepTutor.git
synced 2026-12-02 03:09:49 +08:00
A focused cut on top of v1.4.3:
- Skill hub: install community skills from ClawHub (and any compatible
registry via settings/skill_hubs.json) — provider abstraction in
services/skill/hub.py, new `deeptutor skill search/install/list/remove`
CLI, admin-only install endpoint for multi-user (install ≠ assign).
Every import runs a security gate: hub verdict check, zip-slip/zip-bomb
guards, text/script suffix whitelist, `always:` frontmatter stripped,
provenance recorded in .hub-lock.json.
- KB previews: new /knowledge/{kb}/file-preview-text/{filename} endpoint
serving extracted plain text; KB Files tab renders real in-browser
DOCX/XLSX previews; DOCX previewer scales pages to fit narrow panels.
- Markdown previews allow safe raster data:image URLs on <img src>
(react-markdown strips all data: URLs by default); script-capable
schemes stay blocked.
- All ten localized READMEs re-synced with the English README.
- Docs (deeptutor.info): `deeptutor skill` reference, hub-install notes on
Space and multi-user pages, KB file routes in the server API table
(en + zh).
- CLI docs contract test updated for partner/skill subcommands.
Release notes: assets/releases/ver1-4-4.md
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
530 lines
20 KiB
Python
530 lines
20 KiB
Python
from __future__ import annotations
|
|
|
|
import importlib
|
|
import json
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
|
|
try:
|
|
from fastapi import FastAPI
|
|
from fastapi.testclient import TestClient
|
|
except Exception: # pragma: no cover - optional dependency in lightweight envs
|
|
FastAPI = None
|
|
TestClient = None
|
|
|
|
pytestmark = pytest.mark.skipif(
|
|
FastAPI is None or TestClient is None, reason="fastapi not installed"
|
|
)
|
|
|
|
if FastAPI is not None and TestClient is not None:
|
|
knowledge_router_module = importlib.import_module("deeptutor.api.routers.knowledge")
|
|
router = knowledge_router_module.router
|
|
else: # pragma: no cover - optional dependency in lightweight envs
|
|
knowledge_router_module = None
|
|
router = None
|
|
|
|
|
|
def _build_app() -> FastAPI:
|
|
if FastAPI is None or router is None: # pragma: no cover - guarded by pytestmark
|
|
raise RuntimeError("fastapi is not installed")
|
|
app = FastAPI()
|
|
app.include_router(router, prefix="/api/v1/knowledge")
|
|
return app
|
|
|
|
|
|
class _FakeKBManager:
|
|
def __init__(self, base_dir: Path) -> None:
|
|
self.base_dir = base_dir
|
|
self.base_dir.mkdir(parents=True, exist_ok=True)
|
|
self.config_file = self.base_dir / "kb_config.json"
|
|
self.config: dict[str, dict] = {"knowledge_bases": {}}
|
|
|
|
def _load_config(self) -> dict:
|
|
return self.config
|
|
|
|
def _save_config(self) -> None:
|
|
pass
|
|
|
|
def list_knowledge_bases(self) -> list[str]:
|
|
return sorted(self.config.get("knowledge_bases", {}).keys())
|
|
|
|
def update_kb_status(self, name: str, status: str, progress: dict | None = None) -> None:
|
|
entry = self.config.setdefault("knowledge_bases", {}).setdefault(name, {"path": name})
|
|
entry["status"] = status
|
|
entry["progress"] = progress or {}
|
|
|
|
def get_default(self) -> str | None:
|
|
names = self.list_knowledge_bases()
|
|
return names[0] if names else None
|
|
|
|
def get_knowledge_base_path(self, name: str) -> Path:
|
|
kb_dir = self.base_dir / name
|
|
kb_dir.mkdir(parents=True, exist_ok=True)
|
|
return kb_dir
|
|
|
|
|
|
class _FakeInitializer:
|
|
def __init__(self, kb_name: str, base_dir: str, **_kwargs) -> None:
|
|
self.kb_name = kb_name
|
|
self.base_dir = base_dir
|
|
self.kb_dir = Path(base_dir) / kb_name
|
|
self.raw_dir = self.kb_dir / "raw"
|
|
self.progress_tracker = _kwargs.get("progress_tracker")
|
|
|
|
def create_directory_structure(self) -> None:
|
|
self.raw_dir.mkdir(parents=True, exist_ok=True)
|
|
|
|
def _register_to_config(self) -> None:
|
|
pass
|
|
|
|
|
|
def _upload_payload() -> list[tuple[str, tuple[str, bytes, str]]]:
|
|
return [("files", ("demo.txt", b"hello", "text/plain"))]
|
|
|
|
|
|
def _invalid_upload_payload() -> list[tuple[str, tuple[str, bytes, str]]]:
|
|
return [("files", ("archive.zip", b"PK\x03\x04", "application/zip"))]
|
|
|
|
|
|
def _uppercase_upload_payload() -> list[tuple[str, tuple[str, bytes, str]]]:
|
|
return [("files", ("报告.PDF", b"%PDF-1.4\n", "application/pdf"))]
|
|
|
|
|
|
def test_rag_providers_returns_llamaindex_only() -> None:
|
|
with TestClient(_build_app()) as client:
|
|
response = client.get("/api/v1/knowledge/rag-providers")
|
|
|
|
assert response.status_code == 200
|
|
payload = response.json()
|
|
assert payload == {
|
|
"providers": [
|
|
{
|
|
"id": "llamaindex",
|
|
"name": "LlamaIndex",
|
|
"description": "LlamaIndex retrieval with hybrid BM25/vector fusion when available.",
|
|
}
|
|
]
|
|
}
|
|
|
|
|
|
def test_supported_file_types_returns_upload_policy() -> None:
|
|
with TestClient(_build_app()) as client:
|
|
response = client.get("/api/v1/knowledge/supported-file-types")
|
|
|
|
assert response.status_code == 200
|
|
payload = response.json()
|
|
assert ".pdf" in payload["extensions"]
|
|
assert ".docx" in payload["extensions"]
|
|
assert ".xlsx" in payload["extensions"]
|
|
assert ".pptx" in payload["extensions"]
|
|
assert ".md" in payload["extensions"]
|
|
assert ".png" in payload["extensions"]
|
|
assert payload["max_file_size_bytes"] > payload["max_pdf_size_bytes"] > 0
|
|
assert ".pdf" in payload["accept"]
|
|
assert ".docx" in payload["accept"]
|
|
assert ".png" in payload["accept"]
|
|
assert "image/png" in payload["accept"]
|
|
|
|
|
|
def test_create_kb_does_not_require_llm_precheck(monkeypatch, tmp_path: Path) -> None:
|
|
manager = _FakeKBManager(tmp_path / "knowledge_bases")
|
|
monkeypatch.setattr(knowledge_router_module, "get_kb_manager", lambda: manager)
|
|
monkeypatch.setattr(knowledge_router_module, "KnowledgeBaseInitializer", _FakeInitializer)
|
|
monkeypatch.setattr(
|
|
knowledge_router_module,
|
|
"get_llm_config",
|
|
lambda: (_ for _ in ()).throw(RuntimeError("should not be called")),
|
|
raising=False,
|
|
)
|
|
|
|
async def _noop_init_task(*_args, **_kwargs):
|
|
return None
|
|
|
|
monkeypatch.setattr(knowledge_router_module, "run_initialization_task", _noop_init_task)
|
|
monkeypatch.setattr(knowledge_router_module, "_kb_base_dir", tmp_path / "knowledge_bases")
|
|
|
|
with TestClient(_build_app()) as client:
|
|
response = client.post(
|
|
"/api/v1/knowledge/create",
|
|
data={"name": "kb-new", "rag_provider": "llamaindex"},
|
|
files=_upload_payload(),
|
|
)
|
|
|
|
assert response.status_code == 200
|
|
body = response.json()
|
|
assert body["name"] == "kb-new"
|
|
assert isinstance(body.get("task_id"), str) and body["task_id"]
|
|
assert manager.config["knowledge_bases"]["kb-new"]["rag_provider"] == "llamaindex"
|
|
assert manager.config["knowledge_bases"]["kb-new"]["needs_reindex"] is False
|
|
|
|
|
|
def test_create_coerces_legacy_provider_to_llamaindex(monkeypatch, tmp_path: Path) -> None:
|
|
"""`rag_provider` is now a stub: legacy values silently normalize to llamaindex."""
|
|
manager = _FakeKBManager(tmp_path / "knowledge_bases")
|
|
monkeypatch.setattr(knowledge_router_module, "get_kb_manager", lambda: manager)
|
|
|
|
async def _noop_init_task(*_args, **_kwargs):
|
|
return None
|
|
|
|
monkeypatch.setattr(knowledge_router_module, "run_initialization_task", _noop_init_task)
|
|
monkeypatch.setattr(knowledge_router_module, "_kb_base_dir", tmp_path / "knowledge_bases")
|
|
|
|
with TestClient(_build_app()) as client:
|
|
response = client.post(
|
|
"/api/v1/knowledge/create",
|
|
data={"name": "kb-legacy", "rag_provider": "lightrag"},
|
|
files=_upload_payload(),
|
|
)
|
|
|
|
assert response.status_code == 200
|
|
assert manager.config["knowledge_bases"]["kb-legacy"]["rag_provider"] == "llamaindex"
|
|
|
|
|
|
def test_create_rejects_invalid_files_before_registering_kb(monkeypatch, tmp_path: Path) -> None:
|
|
manager = _FakeKBManager(tmp_path / "knowledge_bases")
|
|
monkeypatch.setattr(knowledge_router_module, "get_kb_manager", lambda: manager)
|
|
monkeypatch.setattr(knowledge_router_module, "_kb_base_dir", tmp_path / "knowledge_bases")
|
|
|
|
with TestClient(_build_app()) as client:
|
|
response = client.post(
|
|
"/api/v1/knowledge/create",
|
|
data={"name": "kb-invalid", "rag_provider": "llamaindex"},
|
|
files=_invalid_upload_payload(),
|
|
)
|
|
|
|
assert response.status_code == 400
|
|
assert "unsupported file type" in response.json()["detail"].lower()
|
|
assert "kb-invalid" not in manager.config["knowledge_bases"]
|
|
|
|
|
|
def test_create_rejects_invalid_kb_name_before_registering_kb(monkeypatch, tmp_path: Path) -> None:
|
|
manager = _FakeKBManager(tmp_path / "knowledge_bases")
|
|
monkeypatch.setattr(knowledge_router_module, "get_kb_manager", lambda: manager)
|
|
monkeypatch.setattr(knowledge_router_module, "_kb_base_dir", tmp_path / "knowledge_bases")
|
|
|
|
with TestClient(_build_app()) as client:
|
|
response = client.post(
|
|
"/api/v1/knowledge/create",
|
|
data={"name": "bad/name", "rag_provider": "llamaindex"},
|
|
files=_upload_payload(),
|
|
)
|
|
|
|
assert response.status_code == 400
|
|
assert "reserved characters" in response.json()["detail"].lower()
|
|
assert manager.config["knowledge_bases"] == {}
|
|
|
|
|
|
def test_create_normalizes_uploaded_extension_to_lowercase(monkeypatch, tmp_path: Path) -> None:
|
|
manager = _FakeKBManager(tmp_path / "knowledge_bases")
|
|
monkeypatch.setattr(knowledge_router_module, "get_kb_manager", lambda: manager)
|
|
monkeypatch.setattr(knowledge_router_module, "KnowledgeBaseInitializer", _FakeInitializer)
|
|
monkeypatch.setattr(knowledge_router_module, "_kb_base_dir", tmp_path / "knowledge_bases")
|
|
|
|
async def _noop_init_task(*_args, **_kwargs):
|
|
return None
|
|
|
|
monkeypatch.setattr(knowledge_router_module, "run_initialization_task", _noop_init_task)
|
|
|
|
with TestClient(_build_app()) as client:
|
|
response = client.post(
|
|
"/api/v1/knowledge/create",
|
|
data={"name": "kb-uppercase", "rag_provider": "llamaindex"},
|
|
files=_uppercase_upload_payload(),
|
|
)
|
|
|
|
assert response.status_code == 200
|
|
assert response.json()["files"] == ["报告.pdf"]
|
|
assert (tmp_path / "knowledge_bases" / "kb-uppercase" / "raw" / "报告.pdf").exists()
|
|
|
|
|
|
def test_upload_returns_409_when_kb_needs_reindex(monkeypatch, tmp_path: Path) -> None:
|
|
manager = _FakeKBManager(tmp_path / "knowledge_bases")
|
|
manager.config["knowledge_bases"]["legacy-kb"] = {
|
|
"path": "legacy-kb",
|
|
"rag_provider": "llamaindex",
|
|
"needs_reindex": True,
|
|
"status": "needs_reindex",
|
|
}
|
|
monkeypatch.setattr(knowledge_router_module, "get_kb_manager", lambda: manager)
|
|
|
|
with TestClient(_build_app()) as client:
|
|
response = client.post("/api/v1/knowledge/legacy-kb/upload", files=_upload_payload())
|
|
|
|
assert response.status_code == 409
|
|
assert "needs reindex" in response.json()["detail"].lower()
|
|
|
|
|
|
def test_upload_ready_kb_returns_task_id(monkeypatch, tmp_path: Path) -> None:
|
|
manager = _FakeKBManager(tmp_path / "knowledge_bases")
|
|
manager.config["knowledge_bases"]["ready-kb"] = {
|
|
"path": "ready-kb",
|
|
"rag_provider": "llamaindex",
|
|
"needs_reindex": False,
|
|
"status": "ready",
|
|
}
|
|
monkeypatch.setattr(knowledge_router_module, "get_kb_manager", lambda: manager)
|
|
monkeypatch.setattr(knowledge_router_module, "_kb_base_dir", tmp_path / "knowledge_bases")
|
|
|
|
async def _noop_upload_task(*_args, **_kwargs):
|
|
return None
|
|
|
|
monkeypatch.setattr(knowledge_router_module, "run_upload_processing_task", _noop_upload_task)
|
|
|
|
with TestClient(_build_app()) as client:
|
|
response = client.post("/api/v1/knowledge/ready-kb/upload", files=_upload_payload())
|
|
|
|
assert response.status_code == 200
|
|
body = response.json()
|
|
assert isinstance(body.get("task_id"), str) and body["task_id"]
|
|
|
|
|
|
def test_list_files_accepts_default_alias(monkeypatch, tmp_path: Path) -> None:
|
|
manager = _FakeKBManager(tmp_path / "knowledge_bases")
|
|
manager.config["knowledge_bases"]["actual-kb"] = {
|
|
"path": "actual-kb",
|
|
"status": "ready",
|
|
}
|
|
raw_dir = manager.base_dir / "actual-kb" / "raw"
|
|
raw_dir.mkdir(parents=True)
|
|
(raw_dir / "demo.txt").write_text("hello", encoding="utf-8")
|
|
monkeypatch.setattr(knowledge_router_module, "get_kb_manager", lambda: manager)
|
|
|
|
with TestClient(_build_app()) as client:
|
|
response = client.get("/api/v1/knowledge/default/files")
|
|
|
|
assert response.status_code == 200
|
|
assert response.json()["files"][0]["name"] == "demo.txt"
|
|
|
|
|
|
def test_list_files_preserves_kb_named_default(monkeypatch, tmp_path: Path) -> None:
|
|
manager = _FakeKBManager(tmp_path / "knowledge_bases")
|
|
manager.config["knowledge_bases"]["actual-kb"] = {
|
|
"path": "actual-kb",
|
|
"status": "ready",
|
|
}
|
|
manager.config["knowledge_bases"]["default"] = {
|
|
"path": "default",
|
|
"status": "ready",
|
|
}
|
|
actual_raw = manager.base_dir / "actual-kb" / "raw"
|
|
actual_raw.mkdir(parents=True)
|
|
(actual_raw / "actual.txt").write_text("hello", encoding="utf-8")
|
|
default_raw = manager.base_dir / "default" / "raw"
|
|
default_raw.mkdir(parents=True)
|
|
(default_raw / "default.txt").write_text("hello", encoding="utf-8")
|
|
monkeypatch.setattr(knowledge_router_module, "get_kb_manager", lambda: manager)
|
|
|
|
with TestClient(_build_app()) as client:
|
|
response = client.get("/api/v1/knowledge/default/files")
|
|
|
|
assert response.status_code == 200
|
|
assert response.json()["files"][0]["name"] == "default.txt"
|
|
|
|
|
|
def test_file_preview_text_accepts_default_alias(monkeypatch, tmp_path: Path) -> None:
|
|
manager = _FakeKBManager(tmp_path / "knowledge_bases")
|
|
manager.config["knowledge_bases"]["actual-kb"] = {
|
|
"path": "actual-kb",
|
|
"status": "ready",
|
|
}
|
|
raw_dir = manager.base_dir / "actual-kb" / "raw"
|
|
raw_dir.mkdir(parents=True)
|
|
target = raw_dir / "slides.pptx"
|
|
target.write_bytes(b"PK\x03\x04")
|
|
calls: dict[str, object] = {}
|
|
|
|
def _fake_extract(path: Path, **kwargs) -> str:
|
|
calls["path"] = path
|
|
calls["kwargs"] = kwargs
|
|
return "--- Slide 1 ---\nTitle"
|
|
|
|
monkeypatch.setattr(knowledge_router_module, "get_kb_manager", lambda: manager)
|
|
monkeypatch.setattr(knowledge_router_module, "extract_text_from_path", _fake_extract)
|
|
|
|
with TestClient(_build_app()) as client:
|
|
response = client.get("/api/v1/knowledge/default/file-preview-text/slides.pptx")
|
|
|
|
assert response.status_code == 200
|
|
assert response.text == "--- Slide 1 ---\nTitle"
|
|
assert calls["path"] == target
|
|
assert calls["kwargs"]["max_chars"] == 200_000
|
|
|
|
|
|
def test_file_preview_text_returns_422_for_extraction_errors(monkeypatch, tmp_path: Path) -> None:
|
|
manager = _FakeKBManager(tmp_path / "knowledge_bases")
|
|
manager.config["knowledge_bases"]["actual-kb"] = {
|
|
"path": "actual-kb",
|
|
"status": "ready",
|
|
}
|
|
raw_dir = manager.base_dir / "actual-kb" / "raw"
|
|
raw_dir.mkdir(parents=True)
|
|
(raw_dir / "slides.pptx").write_bytes(b"PK\x03\x04")
|
|
extraction_error = knowledge_router_module.DocumentExtractionError
|
|
|
|
def _fake_extract(*_args, **_kwargs) -> str:
|
|
raise extraction_error("slides.pptx: no extractable text")
|
|
|
|
monkeypatch.setattr(knowledge_router_module, "get_kb_manager", lambda: manager)
|
|
monkeypatch.setattr(knowledge_router_module, "extract_text_from_path", _fake_extract)
|
|
|
|
with TestClient(_build_app()) as client:
|
|
response = client.get("/api/v1/knowledge/actual-kb/file-preview-text/slides.pptx")
|
|
|
|
assert response.status_code == 422
|
|
assert "no extractable text" in response.json()["detail"]
|
|
|
|
|
|
def test_reindex_accepts_default_alias(monkeypatch, tmp_path: Path) -> None:
|
|
manager = _FakeKBManager(tmp_path / "knowledge_bases")
|
|
manager.config["knowledge_bases"]["actual-kb"] = {
|
|
"path": "actual-kb",
|
|
"status": "ready",
|
|
}
|
|
monkeypatch.setattr(knowledge_router_module, "get_kb_manager", lambda: manager)
|
|
monkeypatch.setattr(knowledge_router_module, "_kb_base_dir", manager.base_dir)
|
|
|
|
class _Signature:
|
|
def hash(self) -> str:
|
|
return "sig"
|
|
|
|
embedding_signature = importlib.import_module("deeptutor.services.rag.embedding_signature")
|
|
index_versioning = importlib.import_module("deeptutor.services.rag.index_versioning")
|
|
monkeypatch.setattr(
|
|
embedding_signature, "signature_from_embedding_config", lambda: _Signature()
|
|
)
|
|
monkeypatch.setattr(index_versioning, "find_matching_version", lambda *_args, **_kwargs: None)
|
|
|
|
async def _noop_reindex_task(*_args, **_kwargs):
|
|
return None
|
|
|
|
monkeypatch.setattr(knowledge_router_module, "run_reindex_task", _noop_reindex_task)
|
|
|
|
with TestClient(_build_app()) as client:
|
|
response = client.post("/api/v1/knowledge/default/reindex")
|
|
|
|
assert response.status_code == 200
|
|
body = response.json()
|
|
assert body["noop"] is False
|
|
assert isinstance(body.get("task_id"), str) and body["task_id"]
|
|
assert manager.config["knowledge_bases"]["actual-kb"]["status"] == "initializing"
|
|
|
|
|
|
def test_reindex_error_status_bypasses_existing_match_noop(monkeypatch, tmp_path: Path) -> None:
|
|
manager = _FakeKBManager(tmp_path / "knowledge_bases")
|
|
manager.config["knowledge_bases"]["failed-kb"] = {
|
|
"path": "failed-kb",
|
|
"status": "error",
|
|
"progress": {"stage": "error", "message": "previous indexing failed"},
|
|
}
|
|
monkeypatch.setattr(knowledge_router_module, "get_kb_manager", lambda: manager)
|
|
monkeypatch.setattr(knowledge_router_module, "_kb_base_dir", manager.base_dir)
|
|
|
|
class _Signature:
|
|
def hash(self) -> str:
|
|
return "sig"
|
|
|
|
embedding_signature = importlib.import_module("deeptutor.services.rag.embedding_signature")
|
|
index_versioning = importlib.import_module("deeptutor.services.rag.index_versioning")
|
|
monkeypatch.setattr(
|
|
embedding_signature, "signature_from_embedding_config", lambda: _Signature()
|
|
)
|
|
monkeypatch.setattr(
|
|
index_versioning,
|
|
"find_matching_version",
|
|
lambda *_args, **_kwargs: {"layout": "flat", "ready": True},
|
|
)
|
|
|
|
async def _noop_reindex_task(*_args, **_kwargs):
|
|
return None
|
|
|
|
monkeypatch.setattr(knowledge_router_module, "run_reindex_task", _noop_reindex_task)
|
|
|
|
with TestClient(_build_app()) as client:
|
|
response = client.post("/api/v1/knowledge/failed-kb/reindex")
|
|
|
|
assert response.status_code == 200
|
|
body = response.json()
|
|
assert body["noop"] is False
|
|
assert isinstance(body.get("task_id"), str) and body["task_id"]
|
|
assert manager.config["knowledge_bases"]["failed-kb"]["status"] == "initializing"
|
|
|
|
|
|
def test_reindex_bypasses_existing_match_when_vectors_are_invalid(
|
|
monkeypatch, tmp_path: Path
|
|
) -> None:
|
|
manager = _FakeKBManager(tmp_path / "knowledge_bases")
|
|
manager.config["knowledge_bases"]["bad-index-kb"] = {
|
|
"path": "bad-index-kb",
|
|
"status": "ready",
|
|
}
|
|
monkeypatch.setattr(knowledge_router_module, "get_kb_manager", lambda: manager)
|
|
monkeypatch.setattr(knowledge_router_module, "_kb_base_dir", manager.base_dir)
|
|
|
|
class _Signature:
|
|
def hash(self) -> str:
|
|
return "sig"
|
|
|
|
kb_dir = manager.base_dir / "bad-index-kb"
|
|
version_dir = kb_dir / "version-1"
|
|
version_dir.mkdir(parents=True)
|
|
(version_dir / "docstore.json").write_text("{}", encoding="utf-8")
|
|
(version_dir / "meta.json").write_text(
|
|
json.dumps({"signature": "sig", "version": "version-1"}),
|
|
encoding="utf-8",
|
|
)
|
|
(version_dir / "default__vector_store.json").write_text(
|
|
json.dumps({"embedding_dict": {"bad-node": [0.1, None, 0.3]}}),
|
|
encoding="utf-8",
|
|
)
|
|
|
|
embedding_signature = importlib.import_module("deeptutor.services.rag.embedding_signature")
|
|
monkeypatch.setattr(
|
|
embedding_signature, "signature_from_embedding_config", lambda: _Signature()
|
|
)
|
|
|
|
async def _noop_reindex_task(*_args, **_kwargs):
|
|
return None
|
|
|
|
monkeypatch.setattr(knowledge_router_module, "run_reindex_task", _noop_reindex_task)
|
|
|
|
with TestClient(_build_app()) as client:
|
|
response = client.post("/api/v1/knowledge/bad-index-kb/reindex")
|
|
|
|
assert response.status_code == 200
|
|
body = response.json()
|
|
assert body["noop"] is False
|
|
assert isinstance(body.get("task_id"), str) and body["task_id"]
|
|
assert manager.config["knowledge_bases"]["bad-index-kb"]["status"] == "initializing"
|
|
|
|
|
|
def test_update_config_coerces_legacy_provider_to_llamaindex() -> None:
|
|
"""Legacy `rag_provider` values are accepted and normalized to llamaindex."""
|
|
|
|
class _FakeConfigService:
|
|
def __init__(self) -> None:
|
|
self.config: dict = {}
|
|
|
|
def set_kb_config(self, kb_name: str, config: dict) -> None:
|
|
self.kb_name = kb_name
|
|
self.config = config
|
|
|
|
def get_kb_config(self, _kb_name: str) -> dict:
|
|
return {"rag_provider": "llamaindex"}
|
|
|
|
fake_service = _FakeConfigService()
|
|
|
|
config_module = importlib.import_module("deeptutor.services.config")
|
|
app = _build_app()
|
|
|
|
with pytest.MonkeyPatch.context() as monkeypatch:
|
|
monkeypatch.setattr(config_module, "get_kb_config_service", lambda: fake_service)
|
|
with TestClient(app) as client:
|
|
response = client.put(
|
|
"/api/v1/knowledge/demo/config",
|
|
json={"rag_provider": "raganything"},
|
|
)
|
|
|
|
assert response.status_code in {200, 204}
|
|
assert fake_service.config.get("rag_provider") == "llamaindex"
|