test_decode_payloads.py 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. from __future__ import annotations
  2. import json
  3. from core.models import Post
  4. from decode_content.payloads import build_payload, build_payloads
  5. def _post() -> Post:
  6. return Post(
  7. id="xhs_1",
  8. platform="xiaohongshu",
  9. url="https://xhs/1",
  10. content_id="1",
  11. title="脚本教程",
  12. author_name="作者",
  13. )
  14. def test_build_how_payload_uses_formal_dim_attribute_and_scope_union():
  15. knowledge = {
  16. "id": "k1",
  17. "type": "how",
  18. "title": "先定受众再写脚本",
  19. "purpose": "写出稳定脚本",
  20. "业务阶段": ["脚本"],
  21. "steps": [
  22. {
  23. "input": "选题",
  24. "directive": "收窄受众",
  25. "output": "受众画像",
  26. "创作阶段": "定向",
  27. "动作": "收窄",
  28. "作用域": [
  29. {"scope_type": "intent", "value": "吸引注意", "link": "复用"},
  30. {"scope_type": "intent", "value": "吸引注意", "link": "复用"},
  31. ],
  32. }
  33. ],
  34. }
  35. payload = build_payload(_post(), knowledge)
  36. assert payload["dim_attributes"] == ["how工序"]
  37. assert "输入:选题" in payload["content"]
  38. assert "指引:收窄受众" in payload["content"]
  39. assert payload["scopes"] == [{"scope_type": "intent", "value": "吸引注意"}]
  40. assert {"key": "创作阶段", "type": "str", "value": "定向"} in payload["custom_ext"]
  41. def test_build_what_and_why_payload_content_is_string_contract():
  42. what = {
  43. "id": "w1",
  44. "type": "what",
  45. "title": "脚本三要素",
  46. "kind": "子集",
  47. "概要": "脚本由开头中段结尾构成",
  48. "维度拆分规则": "按表达顺序",
  49. "body": [{"item_name": "开头", "item_desc": "抓注意"}],
  50. "业务阶段": ["脚本"],
  51. "作用域": [{"scope_type": "form", "value": "脚本结构", "top": []}],
  52. }
  53. why = {
  54. "id": "y1",
  55. "type": "why",
  56. "title": "冲突带来停留",
  57. "阐述": "冲突能制造期待,所以提升停留。",
  58. "业务阶段": ["选题"],
  59. "作用域": [{"scope_type": "effect", "value": "提升停留", "score": 0.95}],
  60. }
  61. what_payload, why_payload = build_payloads(_post(), [what, why])
  62. assert what_payload["dim_attributes"] == ["what构成"]
  63. assert json.loads(what_payload["content"])["body"][0]["item_name"] == "开头"
  64. assert {"key": "概要", "type": "str", "value": "脚本由开头中段结尾构成"} in what_payload["custom_ext"]
  65. assert why_payload["dim_attributes"] == ["why原理"]
  66. assert why_payload["content"] == "冲突能制造期待,所以提升停留。"
  67. assert "score" not in why_payload["scopes"][0]