test_decode_gates_framing_scoping.py 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. from __future__ import annotations
  2. from decode_content import gates
  3. from decode_content.framing import normalize_knowledge_shapes
  4. from decode_content.scoping import apply_scopes, split_scope_items, strip_verb_tail
  5. def test_creation_gate_uses_admit_refute_tiebreak():
  6. calls = []
  7. def fake_chat(system, user, timeout=None):
  8. calls.append(system)
  9. if system == gates.GATE_ADMIT:
  10. return {"in_scope": True}
  11. if system == gates.GATE_REFUTE:
  12. return {"out_of_scope": True}
  13. if system == gates.GATE_TIEBREAK:
  14. return {"in_scope": False}
  15. raise AssertionError(system)
  16. result = gates.creation_gate("读懂内容", chat_json_fn=fake_chat)
  17. assert result.passed is False
  18. assert "裁决=False" in result.reason
  19. assert calls == [gates.GATE_ADMIT, gates.GATE_REFUTE, gates.GATE_TIEBREAK]
  20. def test_normalize_knowledge_shapes_guards_stage_kind_and_parent():
  21. knowledges = [
  22. {
  23. "id": "w1",
  24. "type": "what",
  25. "role": "组件",
  26. "parent": {"how_id": "missing", "step": 1},
  27. "title": "构成",
  28. "kind": "集合",
  29. "业务阶段": ["脚本", "错误"],
  30. },
  31. {
  32. "id": "h1",
  33. "type": "how",
  34. "title": "流程",
  35. "业务阶段": ["选题"],
  36. "steps": [{"创作阶段": "定稿"}],
  37. },
  38. ]
  39. out = normalize_knowledge_shapes(knowledges)
  40. assert out[0]["kind"] == "子集"
  41. assert out[0]["业务阶段"] == ["脚本"]
  42. assert out[0]["role"] == "主"
  43. assert out[0]["parent"] is None
  44. assert out[1]["steps"][0]["创作阶段"] is None
  45. def test_scoping_split_strip_and_apply_without_linker():
  46. assert strip_verb_tail("寻找爆款选题") == "爆款选题"
  47. assert split_scope_items([{"scope_type": "form", "value": "脚本结构与标题结构"}]) == [
  48. {"scope_type": "form", "value": "脚本结构"},
  49. {"scope_type": "form", "value": "标题结构"},
  50. ]
  51. knowledges = [{"id": "h1", "type": "how", "steps": [{"id": "s1"}]}]
  52. apply_scopes(
  53. knowledges,
  54. [
  55. {
  56. "knowledge_id": "h1",
  57. "step_id": "s1",
  58. "items": [{"scope_type": "intent", "value": "吸引注意"}],
  59. }
  60. ],
  61. linker=None,
  62. )
  63. assert knowledges[0]["steps"][0]["作用域"][0]["link"] == "候选"