- Added new modules for advisor compliance, KYC sessions, and script templates, enhancing the advisor agent's capabilities. - Implemented a comprehensive API structure under the `/api/advisor-agent` prefix, ensuring clear organization and access to new features. - Established database models and repositories for compliance rules and KYC sessions, facilitating robust data management. - Integrated exception handling and response models to improve error management and user feedback. - Updated settings to include new configurations for compliance and KYC features, ensuring flexibility and adaptability. This update significantly expands the advisor agent's functionality, providing essential tools for compliance and customer interaction while maintaining a structured API design.
91 lines
3.3 KiB
Python
91 lines
3.3 KiB
Python
import subprocess
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
from fastapi.testclient import TestClient
|
|
|
|
from app.advisor_db import AgentSessionLocal
|
|
from app.main import app
|
|
from app.model.entities_advisor import ScriptTemplate
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
DATASET = ROOT / "docs" / "开发文档" / "29-Sprint2首批话术模板数据集.md"
|
|
SCRIPT = ROOT / "scripts" / "seed" / "import_script_templates.py"
|
|
client = TestClient(app)
|
|
|
|
def token_for(username: str, password: str) -> str:
|
|
response = client.post(
|
|
"/api/v1/auth/login",
|
|
json={"username": username, "password": password},
|
|
)
|
|
assert response.status_code == 200
|
|
return response.json()["data"]["access_token"]
|
|
|
|
def count_seed_templates() -> int:
|
|
with AgentSessionLocal() as session:
|
|
return session.query(ScriptTemplate).filter(ScriptTemplate.created_by == "seed:template_test_data").count()
|
|
|
|
def test_template_dataset_loads_at_least_20_test_templates():
|
|
assert DATASET.exists()
|
|
|
|
from scripts.seed.import_script_templates import load_templates_from_markdown
|
|
|
|
templates = load_templates_from_markdown(DATASET)
|
|
scenes = {template.scene for template in templates}
|
|
statuses = {template.review_status for template in templates}
|
|
|
|
assert len(templates) >= 20
|
|
assert {"loss_comfort", "product_recommend", "dual_record", "market_comment"}.issubset(scenes)
|
|
assert statuses <= {"test_data", "pending"}
|
|
assert all(template.is_approved is False for template in templates)
|
|
assert all(template.is_active is True for template in templates)
|
|
|
|
def test_template_import_is_idempotent_and_keeps_test_templates_unapproved():
|
|
from scripts.seed.import_compliance_rules import import_rules_from_markdown
|
|
from scripts.seed.import_script_templates import import_templates_from_markdown
|
|
|
|
import_rules_from_markdown()
|
|
first = import_templates_from_markdown(DATASET)
|
|
second = import_templates_from_markdown(DATASET)
|
|
|
|
assert first.total >= 20
|
|
assert second.total == first.total
|
|
assert count_seed_templates() == first.total
|
|
|
|
with AgentSessionLocal() as session:
|
|
rows = session.query(ScriptTemplate).filter(ScriptTemplate.created_by == "seed:template_test_data").all()
|
|
assert rows
|
|
assert all(row.is_approved is False for row in rows)
|
|
assert all(row.approved_by is None for row in rows)
|
|
assert all(row.is_active is True for row in rows)
|
|
|
|
def test_advisor_search_does_not_return_unapproved_seed_templates():
|
|
from scripts.seed.import_script_templates import import_templates_from_markdown
|
|
|
|
import_templates_from_markdown(DATASET)
|
|
advisor_token = token_for("advisor_test", "advisor_test")
|
|
|
|
response = client.get(
|
|
"/api/advisor-agent/script-templates/search",
|
|
params={"q": "DEV-TOP"},
|
|
headers={"Authorization": f"Bearer {advisor_token}"},
|
|
)
|
|
|
|
assert response.status_code == 200
|
|
assert response.json()["data"]["items"] == []
|
|
|
|
def test_template_import_script_prints_import_summary():
|
|
from scripts.seed.import_compliance_rules import import_rules_from_markdown
|
|
|
|
import_rules_from_markdown()
|
|
completed = subprocess.run(
|
|
[sys.executable, str(SCRIPT), "--dataset", str(DATASET)],
|
|
check=True,
|
|
capture_output=True,
|
|
text=True,
|
|
cwd=ROOT,
|
|
)
|
|
|
|
assert "Imported script templates:" in completed.stdout
|
|
assert "total=" in completed.stdout
|