mirror of
https://github.com/HKUDS/DeepTutor.git
synced 2026-12-02 19:29:36 +08:00
* 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
186 lines
6.9 KiB
Python
186 lines
6.9 KiB
Python
"""Tests for the schema-driven channels endpoint helpers.
|
|
|
|
Covers:
|
|
* ``resolve_config_model``: maps each ``XxxChannel`` to its ``XxxConfig``.
|
|
* ``inline_refs``: flattens nested model ``$ref``s (slack ``dm`` subtree).
|
|
* ``collect_secret_fields``: only flags **string-typed** secret-looking keys
|
|
(so e.g. ``user_token_read_only: bool`` is excluded).
|
|
* ``GET /api/v1/tutorbot/channels/schema`` integration: shape, snake_case
|
|
property names, and that telegram/slack/discord schemas survive the trip.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from fastapi.testclient import TestClient
|
|
import pytest
|
|
|
|
from deeptutor.api.routers._tutorbot_channel_schema import (
|
|
all_channel_schemas,
|
|
channel_schema_payload,
|
|
collect_secret_fields,
|
|
inline_refs,
|
|
resolve_config_model,
|
|
)
|
|
|
|
|
|
class TestResolveConfigModel:
|
|
def test_telegram_pairs_with_telegram_config(self) -> None:
|
|
from deeptutor.tutorbot.channels.telegram import TelegramChannel, TelegramConfig
|
|
|
|
assert resolve_config_model(TelegramChannel) is TelegramConfig
|
|
|
|
def test_slack_pairs_with_slack_config(self) -> None:
|
|
from deeptutor.tutorbot.channels.slack import SlackChannel, SlackConfig
|
|
|
|
assert resolve_config_model(SlackChannel) is SlackConfig
|
|
|
|
def test_discord_pairs_with_discord_config(self) -> None:
|
|
from deeptutor.tutorbot.channels.discord import DiscordChannel, DiscordConfig
|
|
|
|
assert resolve_config_model(DiscordChannel) is DiscordConfig
|
|
|
|
|
|
class TestInlineRefs:
|
|
def test_inlines_simple_def(self) -> None:
|
|
schema = {
|
|
"type": "object",
|
|
"properties": {"dm": {"$ref": "#/$defs/SlackDMConfig"}},
|
|
"$defs": {
|
|
"SlackDMConfig": {
|
|
"type": "object",
|
|
"properties": {"enabled": {"type": "boolean"}},
|
|
}
|
|
},
|
|
}
|
|
out = inline_refs(schema)
|
|
assert "$defs" not in out
|
|
assert out["properties"]["dm"]["type"] == "object"
|
|
assert out["properties"]["dm"]["properties"]["enabled"]["type"] == "boolean"
|
|
|
|
def test_per_field_overrides_take_precedence(self) -> None:
|
|
# Pydantic sometimes emits {"$ref": "...", "description": "..."}; the
|
|
# description should override the referenced model's description.
|
|
schema = {
|
|
"type": "object",
|
|
"properties": {
|
|
"child": {"$ref": "#/$defs/Foo", "description": "override"},
|
|
},
|
|
"$defs": {
|
|
"Foo": {"type": "object", "description": "original"},
|
|
},
|
|
}
|
|
out = inline_refs(schema)
|
|
assert out["properties"]["child"]["description"] == "override"
|
|
|
|
|
|
class TestCollectSecretFields:
|
|
def test_flags_string_token_field(self) -> None:
|
|
schema = {
|
|
"properties": {
|
|
"token": {"type": "string"},
|
|
"enabled": {"type": "boolean"},
|
|
}
|
|
}
|
|
assert collect_secret_fields(schema) == ["token"]
|
|
|
|
def test_skips_boolean_with_secret_substring(self) -> None:
|
|
# Slack's ``user_token_read_only`` is a flag, not a secret.
|
|
schema = {
|
|
"properties": {
|
|
"user_token_read_only": {"type": "boolean"},
|
|
"bot_token": {"type": "string"},
|
|
}
|
|
}
|
|
assert collect_secret_fields(schema) == ["bot_token"]
|
|
|
|
def test_walks_nested_objects(self) -> None:
|
|
schema = {
|
|
"properties": {
|
|
"dm": {
|
|
"type": "object",
|
|
"properties": {"webhook_secret": {"type": "string"}},
|
|
},
|
|
}
|
|
}
|
|
assert collect_secret_fields(schema) == ["dm.webhook_secret"]
|
|
|
|
def test_handles_nullable_strings(self) -> None:
|
|
# Pydantic's ``Optional[str]`` becomes ``anyOf: [{type: string}, {type: null}]``.
|
|
schema = {
|
|
"properties": {
|
|
"encrypt_key": {"anyOf": [{"type": "string"}, {"type": "null"}]},
|
|
}
|
|
}
|
|
assert collect_secret_fields(schema) == ["encrypt_key"]
|
|
|
|
|
|
class TestChannelSchemaPayload:
|
|
def test_telegram_payload_shape(self) -> None:
|
|
from deeptutor.tutorbot.channels.telegram import TelegramChannel
|
|
|
|
payload = channel_schema_payload(TelegramChannel)
|
|
assert payload is not None
|
|
assert payload["name"] == "telegram"
|
|
assert payload["display_name"] == "Telegram"
|
|
assert payload["secret_fields"] == ["token"]
|
|
# Snake_case wire format (matches the storage form).
|
|
props = payload["json_schema"]["properties"]
|
|
assert "allow_from" in props and "allowFrom" not in props
|
|
assert payload["default_config"]["enabled"] is False
|
|
|
|
def test_slack_dm_subtree_inlined(self) -> None:
|
|
from deeptutor.tutorbot.channels.slack import SlackChannel
|
|
|
|
payload = channel_schema_payload(SlackChannel)
|
|
assert payload is not None
|
|
dm = payload["json_schema"]["properties"]["dm"]
|
|
assert dm["type"] == "object"
|
|
assert "enabled" in dm["properties"]
|
|
# Bool flags whose names contain "token" must NOT be flagged secret.
|
|
assert "user_token_read_only" not in payload["secret_fields"]
|
|
assert "bot_token" in payload["secret_fields"]
|
|
|
|
|
|
class TestEndpoint:
|
|
@pytest.fixture
|
|
def client(self) -> TestClient:
|
|
# Build a minimal FastAPI app with just the tutorbot router; the
|
|
# ``/channels/schema`` endpoint doesn't touch the manager so no
|
|
# fixturing of ``get_tutorbot_manager`` is needed.
|
|
from fastapi import FastAPI
|
|
|
|
from deeptutor.api.routers import tutorbot as tutorbot_router
|
|
|
|
app = FastAPI()
|
|
app.include_router(tutorbot_router.router, prefix="/api/v1/tutorbot")
|
|
return TestClient(app)
|
|
|
|
def test_returns_channels_and_global(self, client: TestClient) -> None:
|
|
res = client.get("/api/v1/tutorbot/channels/schema")
|
|
assert res.status_code == 200
|
|
body = res.json()
|
|
assert set(body.keys()) >= {"channels", "global"}
|
|
# Telegram is always installed (no extra deps).
|
|
assert "telegram" in body["channels"]
|
|
|
|
def test_telegram_entry_has_secret_fields(self, client: TestClient) -> None:
|
|
res = client.get("/api/v1/tutorbot/channels/schema")
|
|
tg = res.json()["channels"]["telegram"]
|
|
assert tg["secret_fields"] == ["token"]
|
|
assert "token" in tg["json_schema"]["properties"]
|
|
|
|
def test_global_schema_uses_snake_case(self, client: TestClient) -> None:
|
|
res = client.get("/api/v1/tutorbot/channels/schema")
|
|
global_props = res.json()["global"]["json_schema"]["properties"]
|
|
assert "send_progress" in global_props
|
|
assert "send_tool_hints" in global_props
|
|
|
|
|
|
class TestAllChannelSchemas:
|
|
def test_returns_at_least_telegram(self) -> None:
|
|
out = all_channel_schemas()
|
|
assert "telegram" in out
|
|
# Every payload has the four documented keys.
|
|
for entry in out.values():
|
|
assert {"name", "display_name", "default_config", "secret_fields", "json_schema"} <= entry.keys()
|