feat: add knowledge import preflight

This commit is contained in:
张胜宇
2026-09-10 20:22:53 +08:00
parent e9b3d272f0
commit 24f75636a0
3 changed files with 1488 additions and 0 deletions
@@ -0,0 +1,75 @@
import json
import pytest
from tools.knowledge_import_preflight import build_import_manifest
def vector_candidate() -> dict[str, object]:
return {
"qa_id": "RAG-PUB-001",
"title": "基金交易确认时间说明",
"question": "基金什么时候确认?",
"paraphrases": ["申购何时确认", "赎回多久确认"],
"answer": "交易确认时间以产品规则和实际交易日为准。",
"scope": "public",
"intent": "policy_explain",
"collection": "fin_policy_collection",
"execution_mode": "vector_search",
"retrieval_status": "approved_candidate",
"audience": ["visitor", "authenticated_user"],
"agent_data_access": "none",
"tags": ["交易规则", "确认"],
"phase": "phase_1",
"source_type": "qa_pair",
"source_file": "qa-v5.8.txt",
"source_url": None,
"source_version": "v5.8",
"review_status": "approved_candidate",
"status": "active",
"effective_date": None,
"expire_date": None,
}
def rule_only_record() -> dict[str, object]:
return {
"qa_id": "RAG-SEC-001",
"scope": "security_notice",
"collection": None,
"execution_mode": "fixed_route",
"retrieval_status": "rule_only",
}
def test_manifest_contains_only_eligible_public_records_pending_admin_review() -> None:
manifest = build_import_manifest(
[vector_candidate(), rule_only_record()], source_name="qa-v5.8.jsonl"
)
assert manifest["summary"] == {
"total_records": 2,
"eligible_records": 1,
"excluded_rule_records": 1,
"publication_state": "pending_review",
}
entry = manifest["records"][0]
assert entry["qa_id"] == "RAG-PUB-001"
assert entry["knowledge_type"] == "policy_explain"
assert entry["milvus_collection"] == "fin_policy_collection"
assert entry["review_status"] == "pending_review"
assert entry["status"] == "active"
assert entry["retrieval_text"] == (
"标准问题:基金什么时候确认?\n"
"相似问法:申购何时确认;赎回多久确认\n"
"标签:交易规则、确认"
)
assert json.loads(entry["content_text"])["answer"] == "交易确认时间以产品规则和实际交易日为准。"
def test_invalid_public_record_is_rejected_instead_of_silently_entering_manifest() -> None:
invalid = vector_candidate()
invalid["agent_data_access"] = "account"
with pytest.raises(ValueError, match="RAG-PUB-001"):
build_import_manifest([invalid], source_name="qa-v5.8.jsonl")