"""Planner(任务拆解)单元测试。""" from router_system.classifier import RuleClassifier from router_system.knowledge import KnowledgeBase from router_system.planner import Planner def _plan(query: str): clf = RuleClassifier() c = clf.classify(query) return Planner(KnowledgeBase()).plan(query, c), c def test_simple_task_no_split(): graph, c = _plan("2 + 2 等于多少") # easy 难度 → 单节点不拆 assert c.domain == "math" assert c.difficulty == "easy" assert len(graph) == 1 n = graph.nodes()[0] assert n.id == "solve" assert n.kind == "solve" def test_code_task_split_four(): graph, c = _plan("用 Python 写一个快速排序函数,并解释时间复杂度") assert c.domain == "code" assert len(graph) == 4 ids = [n.id for n in graph.nodes()] assert ids == ["analyze", "design", "implement", "verify"] def test_math_task_split_three(): graph, c = _plan("求解方程 x^2 - 5x + 6 = 0") assert c.domain == "math" assert len(graph) == 3 ids = [n.id for n in graph.nodes()] assert ids == ["conditions", "solve", "verify"] def test_deps_wired(): graph, _ = _plan("用 Python 写一个快速排序函数,并解释时间复杂度") nodes = {n.id: n for n in graph.nodes()} assert nodes["design"].deps == ["analyze"] assert nodes["implement"].deps == ["design"] assert nodes["verify"].deps == ["implement"] def test_no_template_falls_back_to_single(): graph, c = _plan("你好呀") # general 无模板命中("你好"不在任何 pattern)→ 单节点 assert c.domain == "general" assert len(graph) == 1 n = graph.nodes()[0] assert n.kind == "explain" def test_explain_plan_trace(): graph, _ = _plan("求解方程 x^2 - 5x + 6 = 0") trace = Planner(KnowledgeBase()).explain_plan(graph) assert trace and "plan:multi[3]" in trace[0] single_graph, _ = _plan("1 + 1 = ?") st = Planner(KnowledgeBase()).explain_plan(single_graph) assert "plan:single" in st[0]