Files
DeepTutor/tests/tools/test_rag_tool.py
Bingxi Zhao (Frank) e1c669f02a feat(knowledge): add Tencent IMA as a connected knowledge engine
IMA (ima.qq.com) is where a lot of users already keep their library, and
its OpenAPI exposes retrieval over it. Adding it as a sixth engine follows
the `lightrag-server` shape exactly: a KB bound to `ima` is a connection
pointer, not an index. Nothing is copied and nothing is stored locally —
documents are added in IMA, retrieval calls IMA, and DeepTutor's chat loop
still writes the answer.

The wire is plain HTTP POST to `/openapi/wiki/v1/<method>` with two auth
headers and a `{code,msg,data}` envelope; `search_knowledge` returns
cursor-paginated items carrying a `highlight_content` snippet. Only the two
read-only calls we need are wrapped, and pagination is bounded so a server
that never reports the end cannot spin.

Credentials (`client_id` + `api_key`) and the library id live per-KB in
`kb_config.json` rather than as one global account key. IMA credentials
identify a person, so per-KB means each user on a shared deployment
retrieves through their own account, and connecting stays self-contained
with no separate settings surface to fill in first. Both metadata paths
already work off an allowlist, so the credentials cannot leak into
surfaced metadata; only the library id is exposed, for the UI to show.

`connect-ima` re-probes server-side and never trusts the client's verdict,
matching the LightRAG-server route. IMA KBs are registered as a connected
type — no folder under `base_dir`, no embedding reconcile, deletion drops
only our pointer — and are reported as non-enumerable in the manifest,
since claiming "0 documents" for a library held behind an API would be a
lie.

Backend only; the connect flow in the knowledge center UI is not wired up
yet.
2026-07-29 00:38:10 +08:00

124 lines
3.9 KiB
Python

"""Factory + tool-wrapper layer tests (llamaindex-only)."""
from __future__ import annotations
import pytest
from deeptutor.services.rag.factory import (
DEFAULT_PROVIDER,
get_pipeline,
list_pipelines,
normalize_provider_name,
)
from deeptutor.tools.rag_tool import (
RAGService,
get_available_providers,
get_current_provider,
)
class TestNormalizeProviderName:
"""`normalize_provider_name` is now a stub: every input collapses to llamaindex."""
@pytest.mark.parametrize(
"value",
[
None,
"",
" ",
"llamaindex",
"LlamaIndex",
"raganything",
"raganything_docling",
"totally_unknown_xyz",
],
)
def test_collapses_to_default(self, value) -> None:
assert normalize_provider_name(value) == DEFAULT_PROVIDER
class TestPipelineFactory:
def test_list_pipelines_includes_pageindex(self) -> None:
pipelines = list_pipelines()
assert isinstance(pipelines, list)
assert {p["id"] for p in pipelines} == {
DEFAULT_PROVIDER,
"pageindex",
"graphrag",
"lightrag",
"lightrag-server",
"ima",
}
def test_get_pipeline_returns_singleton(self) -> None:
try:
first = get_pipeline()
second = get_pipeline()
except (ValueError, ImportError) as exc:
pytest.skip(f"LlamaIndex optional dependency missing: {exc}")
assert first is second
def test_get_pipeline_collapses_unknown_to_default_singleton(self) -> None:
"""Unknown / legacy provider names resolve to the default pipeline."""
try:
a = get_pipeline("llamaindex")
b = get_pipeline("raganything") # legacy string → default
c = get_pipeline("nonexistent_xyz")
except (ValueError, ImportError) as exc:
pytest.skip(f"LlamaIndex optional dependency missing: {exc}")
assert a is b is c
def test_get_pipeline_dispatches_known_provider(self) -> None:
"""A real provider name builds its own pipeline (not the default)."""
pipe = get_pipeline("lightrag", kb_base_dir="/tmp/kb-test")
assert type(pipe).__name__ == "LightRagPipeline"
class TestRAGServiceClassHelpers:
def test_list_providers_includes_pageindex(self) -> None:
providers = RAGService.list_providers()
assert {p["id"] for p in providers} == {
DEFAULT_PROVIDER,
"pageindex",
"graphrag",
"lightrag",
"lightrag-server",
"ima",
}
def test_has_provider_default_true(self) -> None:
assert RAGService.has_provider(DEFAULT_PROVIDER) is True
def test_has_provider_unknown_false(self) -> None:
assert RAGService.has_provider("nonexistent") is False
assert RAGService.has_provider("") is False
def test_get_current_provider_collapses_unknown_env(
self, monkeypatch: pytest.MonkeyPatch
) -> None:
monkeypatch.setenv("RAG_PROVIDER", "raganything") # unknown → default
assert get_current_provider() == DEFAULT_PROVIDER
monkeypatch.delenv("RAG_PROVIDER", raising=False)
assert get_current_provider() == DEFAULT_PROVIDER
class TestToolLayerExports:
def test_get_available_providers_matches_class_method(self) -> None:
assert get_available_providers() == RAGService.list_providers()
def test_rag_search_requires_kb_name(self) -> None:
import asyncio
from deeptutor.tools.rag_tool import rag_search
with pytest.raises(ValueError, match="kb_name"):
asyncio.run(rag_search(query="hi", kb_name=""))
def test_rag_search_requires_query(self) -> None:
import asyncio
from deeptutor.tools.rag_tool import rag_search
with pytest.raises(ValueError, match="non-empty"):
asyncio.run(rag_search(query="", kb_name="any"))