test_policy_dispatch.py 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. from __future__ import annotations
  2. import json
  3. import shutil
  4. from pathlib import Path
  5. import pytest
  6. from content_agent.integrations.policy_json import JsonPolicyBundleStore
  7. def test_policy_bundle_uses_content_dispatch_and_exports_runtime_contracts():
  8. bundle = JsonPolicyBundleStore().load_policy_bundle("V1")
  9. assert bundle["dispatch_id"] == "dispatch_content"
  10. assert bundle["runtime_stage"] == "V1.0"
  11. assert bundle["target_entity"] == "Content"
  12. assert bundle["content_format"] == "video"
  13. assert bundle["rule_pack_id"] == "douyin_content_discovery_rule_pack_v1"
  14. assert bundle["effect_status_mapping"]
  15. assert bundle["query_effect_aggregation"]
  16. assert bundle["runtime_status_contract"]["query_effect_status"] == [
  17. "success",
  18. "pending",
  19. "failed",
  20. "rule_blocked",
  21. ]
  22. assert bundle["policy_bundle_hash"]
  23. def test_policy_bundle_fails_when_content_dispatch_is_missing(tmp_path):
  24. root = _copy_policy_files(tmp_path)
  25. path = root / "product_documents/规则包/douyin_rule_packs.v1.json"
  26. data = json.loads(path.read_text(encoding="utf-8"))
  27. for dispatch in data["rule_pack_dispatch"]:
  28. if dispatch["dispatch_id"] == "dispatch_content":
  29. dispatch["dispatch_enabled"] = False
  30. path.write_text(json.dumps(data, ensure_ascii=False), encoding="utf-8")
  31. with pytest.raises(ValueError, match="expected exactly one Content dispatch"):
  32. JsonPolicyBundleStore(root).load_policy_bundle("V1")
  33. def test_policy_bundle_fails_when_content_dispatch_is_ambiguous(tmp_path):
  34. root = _copy_policy_files(tmp_path)
  35. path = root / "product_documents/规则包/douyin_rule_packs.v1.json"
  36. data = json.loads(path.read_text(encoding="utf-8"))
  37. duplicate = dict(data["rule_pack_dispatch"][0])
  38. duplicate["dispatch_id"] = "dispatch_content_duplicate"
  39. duplicate["priority"] = 2
  40. data["rule_pack_dispatch"].append(duplicate)
  41. path.write_text(json.dumps(data, ensure_ascii=False), encoding="utf-8")
  42. with pytest.raises(ValueError, match="expected exactly one Content dispatch"):
  43. JsonPolicyBundleStore(root).load_policy_bundle("V1")
  44. def test_policy_bundle_fails_when_dispatch_points_to_missing_rule_pack(tmp_path):
  45. root = _copy_policy_files(tmp_path)
  46. path = root / "product_documents/规则包/douyin_rule_packs.v1.json"
  47. data = json.loads(path.read_text(encoding="utf-8"))
  48. for dispatch in data["rule_pack_dispatch"]:
  49. if dispatch["dispatch_id"] == "dispatch_content":
  50. dispatch["rule_pack_id"] = "missing_rule_pack"
  51. path.write_text(json.dumps(data, ensure_ascii=False), encoding="utf-8")
  52. with pytest.raises(ValueError, match="dispatch dispatch_content matched 0 enabled rule packs"):
  53. JsonPolicyBundleStore(root).load_policy_bundle("V1")
  54. def test_policy_bundle_fails_when_dispatch_points_to_disabled_rule_pack(tmp_path):
  55. root = _copy_policy_files(tmp_path)
  56. path = root / "product_documents/规则包/douyin_rule_packs.v1.json"
  57. data = json.loads(path.read_text(encoding="utf-8"))
  58. for rule_pack in data["rule_packs"]:
  59. if rule_pack["rule_pack_id"] == "douyin_content_discovery_rule_pack_v1":
  60. rule_pack["enabled"] = False
  61. path.write_text(json.dumps(data, ensure_ascii=False), encoding="utf-8")
  62. with pytest.raises(ValueError, match="dispatch dispatch_content matched 0 enabled rule packs"):
  63. JsonPolicyBundleStore(root).load_policy_bundle("V1")
  64. def _copy_policy_files(tmp_path: Path) -> Path:
  65. root = tmp_path / "repo"
  66. (root / "product_documents/规则包").mkdir(parents=True)
  67. shutil.copy(
  68. "product_documents/规则包/douyin_rule_packs.v1.json",
  69. root / "product_documents/规则包/douyin_rule_packs.v1.json",
  70. )
  71. return root