test_api.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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"] == "V4"
  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. # V4-M3: mock 链路用 query relevance + 平台可观测表现 50/50 打分。
  38. assert review["summary"]["pooled_content_count"] == 0
  39. assert review["summary"]["review_content_count"] == 0
  40. assert review["summary"]["rejected_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. v4_rule_rows = [
  72. row
  73. for row in dashboard["rule_application_summary"]
  74. if row.get("scorecard_schema_version") == "v4_scorecard.v1"
  75. ]
  76. assert v4_rule_rows
  77. assert all(row["v4_explanation"]["scorecard_schema_version"] == "v4_scorecard.v1" for row in v4_rule_rows)
  78. assert "nodes" in dashboard["walk_graph"]
  79. v4_gate_edges = [
  80. edge for edge in dashboard["walk_graph"]["edges"] if edge.get("v4_gate")
  81. ]
  82. assert all(isinstance(edge["v4_gate"], dict) for edge in v4_gate_edges)
  83. assert dashboard["technical_refs"]["runtime_files_url"].endswith("/runtime-files")
  84. queries = client.get(f"/runs/{run_id}/queries").json()
  85. assert queries["total"] >= 1
  86. assert queries["items"][0]["search_query_id"]
  87. content_items = client.get(f"/runs/{run_id}/content-items").json()
  88. assert content_items["total"] >= 1
  89. assert "rule_decision" in content_items["items"][0]
  90. timeline = client.get(f"/runs/{run_id}/timeline").json()
  91. assert timeline["total"] >= 1
  92. assert any(item["source"] == "run_events.jsonl" for item in timeline["items"])
  93. runtime_files = client.get(f"/runs/{run_id}/runtime-files").json()
  94. filenames = {item["filename"] for item in runtime_files["files"]}
  95. assert "search_queries.jsonl" in filenames
  96. runtime_file = client.get(f"/runs/{run_id}/runtime-files/search_queries.jsonl").json()
  97. assert runtime_file["records"]
  98. assert runtime_file["data_origin"] == "runtime_export"
  99. bad_runtime_file = client.get(f"/runs/{run_id}/runtime-files/not_allowed.jsonl")
  100. assert bad_runtime_file.status_code == 400
  101. assert bad_runtime_file.json()["detail"]["error_code"] == "INVALID_REQUEST"
  102. missing_dashboard = client.get("/runs/not-a-run/dashboard")
  103. assert missing_dashboard.status_code == 404
  104. assert missing_dashboard.json()["detail"]["error_code"] == "RUN_NOT_FOUND"
  105. def test_api_defaults_to_real_platform_mode_but_can_select_mock(tmp_path, monkeypatch):
  106. selected_modes = []
  107. def fake_platform_client(self, platform, platform_mode):
  108. selected_modes.append((platform, platform_mode))
  109. return MockPlatformClient()
  110. monkeypatch.setattr(RunService, "_platform_client", fake_platform_client)
  111. monkeypatch.setattr(
  112. api,
  113. "service",
  114. RunService(
  115. runtime_root=tmp_path / "runtime" / "v1",
  116. demand_source=FakeDemandSource(),
  117. query_variant_client=FakeQueryVariantClient(),
  118. ),
  119. )
  120. client = TestClient(api.app)
  121. default_response = client.post("/runs", json={})
  122. mock_response = client.post("/runs", json={"platform_mode": "mock"})
  123. assert default_response.status_code == 200
  124. assert default_response.json()["platform_mode"] == "real"
  125. assert mock_response.status_code == 200
  126. assert mock_response.json()["platform_mode"] == "mock"
  127. assert selected_modes == [("douyin", "real"), ("douyin", "mock")]
  128. def test_api_rejects_non_douyin_real_platform(tmp_path, monkeypatch):
  129. monkeypatch.setattr(
  130. api,
  131. "service",
  132. RunService(
  133. runtime_root=tmp_path / "runtime" / "v1",
  134. demand_source=FakeDemandSource(),
  135. query_variant_client=FakeQueryVariantClient(),
  136. ),
  137. )
  138. client = TestClient(api.app)
  139. response = client.post(
  140. "/runs", json={"platform": "other_platform", "platform_mode": "real"}
  141. )
  142. assert response.status_code == 400
  143. assert response.json()["detail"]["error_code"] == "INVALID_REQUEST"
  144. def test_api_returns_partial_success_as_successful_response(tmp_path, monkeypatch):
  145. service = RunService(
  146. runtime_root=tmp_path / "runtime" / "v1",
  147. demand_source=FakeDemandSource(),
  148. query_variant_client=FakeQueryVariantClient(),
  149. )
  150. service._platform_client = lambda platform, platform_mode: _PartialFailurePlatformClient()
  151. monkeypatch.setattr(api, "service", service)
  152. client = TestClient(api.app)
  153. response = client.post("/runs", json={"platform": "douyin", "platform_mode": "real"})
  154. assert response.status_code == 200
  155. payload = response.json()
  156. assert payload["status"] == "partial_success"
  157. summary = client.get(f"/runs/{payload['run_id']}").json()
  158. assert summary["status"] == "partial_success"
  159. def test_api_rejects_legacy_run_identifier_field(tmp_path, monkeypatch):
  160. monkeypatch.setattr(api, "service", RunService(runtime_root=tmp_path / "runtime" / "v1"))
  161. client = TestClient(api.app)
  162. legacy_run_key = "tr" + "ace_id"
  163. response = client.post(
  164. "/runs",
  165. json={"platform": "douyin", "platform_mode": "mock", legacy_run_key: "legacy_run_001"},
  166. )
  167. assert response.status_code == 422
  168. class _PartialFailurePlatformClient:
  169. def __init__(self) -> None:
  170. self.mock = MockPlatformClient()
  171. def search(self, search_query: dict) -> list[dict]:
  172. if search_query["search_query_id"] == "q_002":
  173. raise RuntimeError("temporary platform failure")
  174. return self.mock.search(search_query)
  175. def test_api_timeline_includes_summary(tmp_path, monkeypatch):
  176. monkeypatch.setattr(
  177. api,
  178. "service",
  179. RunService(
  180. runtime_root=tmp_path / "runtime" / "v1",
  181. demand_source=FakeDemandSource(),
  182. query_variant_client=FakeQueryVariantClient(),
  183. ),
  184. )
  185. client = TestClient(api.app)
  186. run_id = client.post("/runs", json={"platform": "douyin", "platform_mode": "mock"}).json()["run_id"]
  187. timeline = client.get(f"/runs/{run_id}/timeline").json()
  188. summary = timeline["summary"]
  189. assert set(summary) == {
  190. "total_duration_ms",
  191. "stage_duration_ms",
  192. "query_failure_count",
  193. "platform_rate_limited_count",
  194. "decode_status_counts",
  195. "error_counts",
  196. "walk_status_counts",
  197. }
  198. assert summary["stage_duration_ms"]
  199. assert "stalled" not in timeline and "is_blocked" not in timeline
  200. def test_api_config_readonly_endpoints():
  201. client = TestClient(api.app)
  202. rule_packs = client.get("/config/rule-packs").json()
  203. assert rule_packs["source_file"].endswith("douyin_rule_packs.v1.json")
  204. # M4 砍包受控变化:4 future dispatch/binding 已删,仅剩 content。
  205. assert len(rule_packs["data"]["rule_pack_dispatch"]) == 1
  206. assert len(rule_packs["data"]["effect_status_mapping"]) == 7
  207. walk = client.get("/config/walk-strategy").json()
  208. assert len(walk["data"]["walk_rule_pack_binding"]) == 1
  209. assert len(walk["data"]["walk_edge_catalog"]) == 9
  210. prompts = client.get("/config/query-prompts").json()
  211. assert "douyin/V1" in prompts["data"]["profiles"]
  212. policy = client.get("/config/walk-policy").json()
  213. assert policy["source_file"].endswith("walk_policy.json")
  214. assert "edge_permissions" in policy["data"]
  215. assert "edge_budgets" in policy["data"]
  216. def test_api_dashboard_rule_summary_effect_status_not_none(tmp_path, monkeypatch):
  217. monkeypatch.setattr(
  218. api,
  219. "service",
  220. RunService(
  221. runtime_root=tmp_path / "runtime" / "v1",
  222. demand_source=FakeDemandSource(),
  223. query_variant_client=FakeQueryVariantClient(),
  224. ),
  225. )
  226. client = TestClient(api.app)
  227. run_id = client.post("/runs", json={"platform": "douyin", "platform_mode": "mock"}).json()["run_id"]
  228. dashboard = client.get(f"/runs/{run_id}/dashboard").json()
  229. summaries = dashboard["rule_application_summary"]
  230. assert summaries
  231. # 修复前 content_effect_status 恒为 None(decision 记录无该字段)。
  232. assert all(row["content_effect_status"] is not None for row in summaries)
  233. # walk_graph edge 带归属/执行分离字段。
  234. walk_edges = [e for e in dashboard["walk_graph"]["edges"] if e.get("rule_pack")]
  235. assert any("rule_pack_executed" in e for e in walk_edges)