test_api.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. from fastapi.testclient import TestClient
  2. from content_agent import api
  3. from content_agent.integrations.mock_platform import MockPlatformClient
  4. from content_agent.run_service import RunService
  5. from tests.p1_helpers import FakeDemandSource, FakeQueryVariantClient
  6. def test_api_runs_and_queries_mock_chain(tmp_path, monkeypatch):
  7. monkeypatch.setattr(
  8. api,
  9. "service",
  10. RunService(
  11. runtime_root=tmp_path / "runtime" / "v1",
  12. demand_source=FakeDemandSource(),
  13. query_variant_client=FakeQueryVariantClient(),
  14. ),
  15. )
  16. client = TestClient(api.app)
  17. response = client.post("/runs", json={"platform": "douyin", "platform_mode": "mock"})
  18. assert response.status_code == 200
  19. payload = response.json()
  20. run_id = payload["run_id"]
  21. assert payload["platform_mode"] == "mock"
  22. assert payload["policy_run_id"].startswith("policy_run_")
  23. assert payload["policy_bundle_id"] == "douyin_policy_bundle_v1"
  24. assert payload["strategy_version"] == "V1"
  25. for path in [
  26. f"/runs/{run_id}",
  27. f"/runs/{run_id}/discovered-content-items",
  28. f"/runs/{run_id}/rule-decisions",
  29. f"/runs/{run_id}/source-path-records",
  30. f"/runs/{run_id}/final-output",
  31. f"/runs/{run_id}/strategy-review",
  32. f"/runs/{run_id}/validation",
  33. ]:
  34. get_response = client.get(path)
  35. assert get_response.status_code == 200, path
  36. review = client.get(f"/runs/{run_id}/strategy-review").json()["data"]
  37. # M3 受控变化: 画像门槛退役,改 Gemini 相关性 + 平台热度打分;mock 链热度不足,
  38. # 三条内容落复看带(原 1 进池)。
  39. assert review["summary"]["pooled_content_count"] == 0
  40. assert review["summary"]["review_content_count"] == 3
  41. assert review["suggestions"]
  42. validation = client.get(f"/runs/{run_id}/validation").json()
  43. assert validation["status"] == "pass"
  44. summary = client.get(f"/runs/{run_id}").json()
  45. assert summary["validation_status"] == "pass"
  46. run_list = client.get("/runs").json()
  47. assert run_list["total"] == 1
  48. assert run_list["items"][0]["run_id"] == run_id
  49. assert run_list["data_origin"] == "runtime_export"
  50. dashboard = client.get(f"/runs/{run_id}/dashboard").json()
  51. assert dashboard["run_id"] == run_id
  52. assert dashboard["data_origin"] == "runtime_export"
  53. assert dashboard["counts"]["queries"] >= 1
  54. assert dashboard["runtime_files"]
  55. assert dashboard["business_summary"]["query_count"] >= 1
  56. assert {stage["stage_id"] for stage in dashboard["stage_conclusions"]} >= {
  57. "source",
  58. "query",
  59. "platform",
  60. "judge",
  61. "walk",
  62. "asset",
  63. "learning",
  64. }
  65. query_stage = next(stage for stage in dashboard["stage_conclusions"] if stage["stage_id"] == "query")
  66. assert "生成成功" in query_stage["metric"]
  67. assert "llm_variant" not in query_stage["metric"]
  68. source_stage = next(stage for stage in dashboard["stage_conclusions"] if stage["stage_id"] == "source")
  69. assert source_stage["detail"] == "需求池 ID:1"
  70. assert isinstance(dashboard["rule_application_summary"], list)
  71. assert "nodes" in dashboard["walk_graph"]
  72. assert dashboard["technical_refs"]["runtime_files_url"].endswith("/runtime-files")
  73. queries = client.get(f"/runs/{run_id}/queries").json()
  74. assert queries["total"] >= 1
  75. assert queries["items"][0]["search_query_id"]
  76. content_items = client.get(f"/runs/{run_id}/content-items").json()
  77. assert content_items["total"] >= 1
  78. assert "rule_decision" in content_items["items"][0]
  79. timeline = client.get(f"/runs/{run_id}/timeline").json()
  80. assert timeline["total"] >= 1
  81. assert any(item["source"] == "run_events.jsonl" for item in timeline["items"])
  82. runtime_files = client.get(f"/runs/{run_id}/runtime-files").json()
  83. filenames = {item["filename"] for item in runtime_files["files"]}
  84. assert "search_queries.jsonl" in filenames
  85. runtime_file = client.get(f"/runs/{run_id}/runtime-files/search_queries.jsonl").json()
  86. assert runtime_file["records"]
  87. assert runtime_file["data_origin"] == "runtime_export"
  88. bad_runtime_file = client.get(f"/runs/{run_id}/runtime-files/not_allowed.jsonl")
  89. assert bad_runtime_file.status_code == 400
  90. assert bad_runtime_file.json()["detail"]["error_code"] == "INVALID_REQUEST"
  91. missing_dashboard = client.get("/runs/not-a-run/dashboard")
  92. assert missing_dashboard.status_code == 404
  93. assert missing_dashboard.json()["detail"]["error_code"] == "RUN_NOT_FOUND"
  94. def test_api_defaults_to_real_platform_mode_but_can_select_mock(tmp_path, monkeypatch):
  95. selected_modes = []
  96. def fake_platform_client(self, platform, platform_mode):
  97. selected_modes.append((platform, platform_mode))
  98. return MockPlatformClient()
  99. monkeypatch.setattr(RunService, "_platform_client", fake_platform_client)
  100. monkeypatch.setattr(
  101. api,
  102. "service",
  103. RunService(
  104. runtime_root=tmp_path / "runtime" / "v1",
  105. demand_source=FakeDemandSource(),
  106. query_variant_client=FakeQueryVariantClient(),
  107. ),
  108. )
  109. client = TestClient(api.app)
  110. default_response = client.post("/runs", json={})
  111. mock_response = client.post("/runs", json={"platform_mode": "mock"})
  112. assert default_response.status_code == 200
  113. assert default_response.json()["platform_mode"] == "real"
  114. assert mock_response.status_code == 200
  115. assert mock_response.json()["platform_mode"] == "mock"
  116. assert selected_modes == [("douyin", "real"), ("douyin", "mock")]
  117. def test_api_rejects_non_douyin_real_platform(tmp_path, monkeypatch):
  118. monkeypatch.setattr(
  119. api,
  120. "service",
  121. RunService(
  122. runtime_root=tmp_path / "runtime" / "v1",
  123. demand_source=FakeDemandSource(),
  124. query_variant_client=FakeQueryVariantClient(),
  125. ),
  126. )
  127. client = TestClient(api.app)
  128. response = client.post(
  129. "/runs", json={"platform": "other_platform", "platform_mode": "real"}
  130. )
  131. assert response.status_code == 400
  132. assert response.json()["detail"]["error_code"] == "INVALID_REQUEST"
  133. def test_api_returns_partial_success_as_successful_response(tmp_path, monkeypatch):
  134. service = RunService(
  135. runtime_root=tmp_path / "runtime" / "v1",
  136. demand_source=FakeDemandSource(),
  137. query_variant_client=FakeQueryVariantClient(),
  138. )
  139. service._platform_client = lambda platform, platform_mode: _PartialFailurePlatformClient()
  140. monkeypatch.setattr(api, "service", service)
  141. client = TestClient(api.app)
  142. response = client.post("/runs", json={"platform": "douyin", "platform_mode": "real"})
  143. assert response.status_code == 200
  144. payload = response.json()
  145. assert payload["status"] == "partial_success"
  146. summary = client.get(f"/runs/{payload['run_id']}").json()
  147. assert summary["status"] == "partial_success"
  148. def test_api_rejects_legacy_run_identifier_field(tmp_path, monkeypatch):
  149. monkeypatch.setattr(api, "service", RunService(runtime_root=tmp_path / "runtime" / "v1"))
  150. client = TestClient(api.app)
  151. legacy_run_key = "tr" + "ace_id"
  152. response = client.post(
  153. "/runs",
  154. json={"platform": "douyin", "platform_mode": "mock", legacy_run_key: "legacy_run_001"},
  155. )
  156. assert response.status_code == 422
  157. class _PartialFailurePlatformClient:
  158. def __init__(self) -> None:
  159. self.mock = MockPlatformClient()
  160. def search(self, search_query: dict) -> list[dict]:
  161. if search_query["search_query_id"] == "q_002":
  162. raise RuntimeError("temporary platform failure")
  163. return self.mock.search(search_query)
  164. def test_api_timeline_includes_summary(tmp_path, monkeypatch):
  165. monkeypatch.setattr(
  166. api,
  167. "service",
  168. RunService(
  169. runtime_root=tmp_path / "runtime" / "v1",
  170. demand_source=FakeDemandSource(),
  171. query_variant_client=FakeQueryVariantClient(),
  172. ),
  173. )
  174. client = TestClient(api.app)
  175. run_id = client.post("/runs", json={"platform": "douyin", "platform_mode": "mock"}).json()["run_id"]
  176. timeline = client.get(f"/runs/{run_id}/timeline").json()
  177. summary = timeline["summary"]
  178. assert set(summary) == {
  179. "total_duration_ms",
  180. "stage_duration_ms",
  181. "query_failure_count",
  182. "platform_rate_limited_count",
  183. "decode_status_counts",
  184. "error_counts",
  185. "walk_status_counts",
  186. }
  187. assert summary["stage_duration_ms"]
  188. assert "stalled" not in timeline and "is_blocked" not in timeline
  189. def test_api_config_readonly_endpoints():
  190. client = TestClient(api.app)
  191. rule_packs = client.get("/config/rule-packs").json()
  192. assert rule_packs["source_file"].endswith("douyin_rule_packs.v1.json")
  193. # M4 砍包受控变化:4 future dispatch/binding 已删,仅剩 content。
  194. assert len(rule_packs["data"]["rule_pack_dispatch"]) == 1
  195. assert len(rule_packs["data"]["effect_status_mapping"]) == 5
  196. walk = client.get("/config/walk-strategy").json()
  197. assert len(walk["data"]["walk_rule_pack_binding"]) == 1
  198. assert len(walk["data"]["walk_edge_catalog"]) == 10
  199. prompts = client.get("/config/query-prompts").json()
  200. assert "douyin/V1" in prompts["data"]["profiles"]
  201. policy = client.get("/config/walk-policy").json()
  202. assert policy["source_file"].endswith("walk_policy.json")
  203. assert "edge_permissions" in policy["data"]
  204. assert "edge_budgets" in policy["data"]
  205. def test_api_dashboard_rule_summary_effect_status_not_none(tmp_path, monkeypatch):
  206. monkeypatch.setattr(
  207. api,
  208. "service",
  209. RunService(
  210. runtime_root=tmp_path / "runtime" / "v1",
  211. demand_source=FakeDemandSource(),
  212. query_variant_client=FakeQueryVariantClient(),
  213. ),
  214. )
  215. client = TestClient(api.app)
  216. run_id = client.post("/runs", json={"platform": "douyin", "platform_mode": "mock"}).json()["run_id"]
  217. dashboard = client.get(f"/runs/{run_id}/dashboard").json()
  218. summaries = dashboard["rule_application_summary"]
  219. assert summaries
  220. # 修复前 content_effect_status 恒为 None(decision 记录无该字段)。
  221. assert all(row["content_effect_status"] is not None for row in summaries)
  222. # walk_graph edge 带归属/执行分离字段。
  223. walk_edges = [e for e in dashboard["walk_graph"]["edges"] if e.get("rule_pack")]
  224. assert any("rule_pack_executed" in e for e in walk_edges)