feat(scenarios): 兼容非法配置并展示错误摘要
This commit is contained in:
@@ -3,6 +3,7 @@ import pytest
|
||||
from apps.scenarios.services import (
|
||||
ScenarioNotFound,
|
||||
get_scenario,
|
||||
list_scenario_issues,
|
||||
list_scenarios,
|
||||
)
|
||||
|
||||
@@ -38,3 +39,91 @@ def test_home_page_shows_applicable_questions(client):
|
||||
assert response.status_code == 200
|
||||
assert "适用题型" in content
|
||||
assert "SOP 问答" in content
|
||||
|
||||
|
||||
def test_list_scenarios_skips_invalid_config_and_collects_issues(settings, tmp_path):
|
||||
valid_file = tmp_path / "valid.yaml"
|
||||
invalid_file = tmp_path / "invalid.yaml"
|
||||
valid_file.write_text(
|
||||
"""
|
||||
id: demo_valid
|
||||
name: 有效场景
|
||||
description: 用于测试
|
||||
agent:
|
||||
role: 测试助手
|
||||
goal: 正常返回
|
||||
instructions:
|
||||
- 输出结果
|
||||
rag:
|
||||
enabled: false
|
||||
tools: []
|
||||
output:
|
||||
type: general_answer
|
||||
audit:
|
||||
enabled: true
|
||||
""".strip(),
|
||||
encoding="utf-8",
|
||||
)
|
||||
invalid_file.write_text(
|
||||
"""
|
||||
id: broken
|
||||
name: 非法场景
|
||||
description: 缺少 agent.goal
|
||||
agent:
|
||||
role: 测试助手
|
||||
instructions:
|
||||
- 输出结果
|
||||
rag:
|
||||
enabled: true
|
||||
tools: []
|
||||
output:
|
||||
type: general_answer
|
||||
audit:
|
||||
enabled: true
|
||||
""".strip(),
|
||||
encoding="utf-8",
|
||||
)
|
||||
settings.SCENARIO_CONFIG_DIR = tmp_path
|
||||
|
||||
scenarios = list_scenarios()
|
||||
issues = list_scenario_issues()
|
||||
|
||||
assert [scenario["id"] for scenario in scenarios] == ["demo_valid"]
|
||||
assert len(issues) == 1
|
||||
assert issues[0]["file_name"] == "invalid.yaml"
|
||||
assert "agent.goal" in issues[0]["message"]
|
||||
|
||||
|
||||
def test_home_page_shows_invalid_scenario_issues_instead_of_500(client, settings, tmp_path):
|
||||
valid_file = tmp_path / "valid.yaml"
|
||||
invalid_file = tmp_path / "invalid.yaml"
|
||||
valid_file.write_text(
|
||||
"""
|
||||
id: demo_valid
|
||||
name: 有效场景
|
||||
description: 用于测试
|
||||
agent:
|
||||
role: 测试助手
|
||||
goal: 正常返回
|
||||
instructions:
|
||||
- 输出结果
|
||||
rag:
|
||||
enabled: false
|
||||
tools: []
|
||||
output:
|
||||
type: general_answer
|
||||
audit:
|
||||
enabled: true
|
||||
""".strip(),
|
||||
encoding="utf-8",
|
||||
)
|
||||
invalid_file.write_text("id: broken\nname: 缺失结构", encoding="utf-8")
|
||||
settings.SCENARIO_CONFIG_DIR = tmp_path
|
||||
|
||||
response = client.get("/")
|
||||
|
||||
content = response.content.decode("utf-8")
|
||||
assert response.status_code == 200
|
||||
assert "有效场景" in content
|
||||
assert "配置异常" in content
|
||||
assert "invalid.yaml" in content
|
||||
|
||||
Reference in New Issue
Block a user