test_api.py 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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. assert review["summary"]["pooled_content_count"] == 1
  38. assert review["suggestions"]
  39. validation = client.get(f"/runs/{run_id}/validation").json()
  40. assert validation["status"] == "pass"
  41. summary = client.get(f"/runs/{run_id}").json()
  42. assert summary["validation_status"] == "pass"
  43. run_list = client.get("/runs").json()
  44. assert run_list["total"] == 1
  45. assert run_list["items"][0]["run_id"] == run_id
  46. assert run_list["data_origin"] == "runtime_export"
  47. dashboard = client.get(f"/runs/{run_id}/dashboard").json()
  48. assert dashboard["run_id"] == run_id
  49. assert dashboard["data_origin"] == "runtime_export"
  50. assert dashboard["counts"]["queries"] >= 1
  51. assert dashboard["runtime_files"]
  52. assert dashboard["business_summary"]["query_count"] >= 1
  53. assert {stage["stage_id"] for stage in dashboard["stage_conclusions"]} >= {
  54. "source",
  55. "query",
  56. "platform",
  57. "judge",
  58. "walk",
  59. "asset",
  60. "learning",
  61. }
  62. query_stage = next(stage for stage in dashboard["stage_conclusions"] if stage["stage_id"] == "query")
  63. assert "生成成功" in query_stage["metric"]
  64. assert "llm_variant" not in query_stage["metric"]
  65. source_stage = next(stage for stage in dashboard["stage_conclusions"] if stage["stage_id"] == "source")
  66. assert source_stage["detail"] == "需求池 ID:1"
  67. assert isinstance(dashboard["rule_application_summary"], list)
  68. assert "nodes" in dashboard["walk_graph"]
  69. assert dashboard["technical_refs"]["runtime_files_url"].endswith("/runtime-files")
  70. queries = client.get(f"/runs/{run_id}/queries").json()
  71. assert queries["total"] >= 1
  72. assert queries["items"][0]["search_query_id"]
  73. content_items = client.get(f"/runs/{run_id}/content-items").json()
  74. assert content_items["total"] >= 1
  75. assert "rule_decision" in content_items["items"][0]
  76. timeline = client.get(f"/runs/{run_id}/timeline").json()
  77. assert timeline["total"] >= 1
  78. assert any(item["source"] == "run_events.jsonl" for item in timeline["items"])
  79. runtime_files = client.get(f"/runs/{run_id}/runtime-files").json()
  80. filenames = {item["filename"] for item in runtime_files["files"]}
  81. assert "search_queries.jsonl" in filenames
  82. runtime_file = client.get(f"/runs/{run_id}/runtime-files/search_queries.jsonl").json()
  83. assert runtime_file["records"]
  84. assert runtime_file["data_origin"] == "runtime_export"
  85. bad_runtime_file = client.get(f"/runs/{run_id}/runtime-files/not_allowed.jsonl")
  86. assert bad_runtime_file.status_code == 400
  87. assert bad_runtime_file.json()["detail"]["error_code"] == "INVALID_REQUEST"
  88. missing_dashboard = client.get("/runs/not-a-run/dashboard")
  89. assert missing_dashboard.status_code == 404
  90. assert missing_dashboard.json()["detail"]["error_code"] == "RUN_NOT_FOUND"
  91. def test_api_defaults_to_real_platform_mode_but_can_select_mock(tmp_path, monkeypatch):
  92. selected_modes = []
  93. def fake_platform_client(self, platform, platform_mode):
  94. selected_modes.append((platform, platform_mode))
  95. return MockPlatformClient()
  96. monkeypatch.setattr(RunService, "_platform_client", fake_platform_client)
  97. monkeypatch.setattr(
  98. api,
  99. "service",
  100. RunService(
  101. runtime_root=tmp_path / "runtime" / "v1",
  102. demand_source=FakeDemandSource(),
  103. query_variant_client=FakeQueryVariantClient(),
  104. ),
  105. )
  106. client = TestClient(api.app)
  107. default_response = client.post("/runs", json={})
  108. mock_response = client.post("/runs", json={"platform_mode": "mock"})
  109. assert default_response.status_code == 200
  110. assert default_response.json()["platform_mode"] == "real"
  111. assert mock_response.status_code == 200
  112. assert mock_response.json()["platform_mode"] == "mock"
  113. assert selected_modes == [("douyin", "real"), ("douyin", "mock")]
  114. def test_api_rejects_non_douyin_real_platform(tmp_path, monkeypatch):
  115. monkeypatch.setattr(
  116. api,
  117. "service",
  118. RunService(
  119. runtime_root=tmp_path / "runtime" / "v1",
  120. demand_source=FakeDemandSource(),
  121. query_variant_client=FakeQueryVariantClient(),
  122. ),
  123. )
  124. client = TestClient(api.app)
  125. response = client.post(
  126. "/runs", json={"platform": "other_platform", "platform_mode": "real"}
  127. )
  128. assert response.status_code == 400
  129. assert response.json()["detail"]["error_code"] == "INVALID_REQUEST"
  130. def test_api_returns_partial_success_as_successful_response(tmp_path, monkeypatch):
  131. service = RunService(
  132. runtime_root=tmp_path / "runtime" / "v1",
  133. demand_source=FakeDemandSource(),
  134. query_variant_client=FakeQueryVariantClient(),
  135. )
  136. service._platform_client = lambda platform, platform_mode: _PartialFailurePlatformClient()
  137. monkeypatch.setattr(api, "service", service)
  138. client = TestClient(api.app)
  139. response = client.post("/runs", json={"platform": "douyin", "platform_mode": "real"})
  140. assert response.status_code == 200
  141. payload = response.json()
  142. assert payload["status"] == "partial_success"
  143. summary = client.get(f"/runs/{payload['run_id']}").json()
  144. assert summary["status"] == "partial_success"
  145. def test_api_rejects_legacy_run_identifier_field(tmp_path, monkeypatch):
  146. monkeypatch.setattr(api, "service", RunService(runtime_root=tmp_path / "runtime" / "v1"))
  147. client = TestClient(api.app)
  148. legacy_run_key = "tr" + "ace_id"
  149. response = client.post(
  150. "/runs",
  151. json={"platform": "douyin", "platform_mode": "mock", legacy_run_key: "legacy_run_001"},
  152. )
  153. assert response.status_code == 422
  154. class _PartialFailurePlatformClient:
  155. def __init__(self) -> None:
  156. self.mock = MockPlatformClient()
  157. def search(self, search_query: dict) -> list[dict]:
  158. if search_query["search_query_id"] == "q_002":
  159. raise RuntimeError("temporary platform failure")
  160. return self.mock.search(search_query)
  161. def test_api_timeline_includes_summary(tmp_path, monkeypatch):
  162. monkeypatch.setattr(
  163. api,
  164. "service",
  165. RunService(
  166. runtime_root=tmp_path / "runtime" / "v1",
  167. demand_source=FakeDemandSource(),
  168. query_variant_client=FakeQueryVariantClient(),
  169. ),
  170. )
  171. client = TestClient(api.app)
  172. run_id = client.post("/runs", json={"platform": "douyin", "platform_mode": "mock"}).json()["run_id"]
  173. timeline = client.get(f"/runs/{run_id}/timeline").json()
  174. summary = timeline["summary"]
  175. assert set(summary) == {
  176. "total_duration_ms",
  177. "stage_duration_ms",
  178. "query_failure_count",
  179. "platform_rate_limited_count",
  180. "decode_status_counts",
  181. "error_counts",
  182. "walk_status_counts",
  183. }
  184. assert summary["stage_duration_ms"]
  185. assert "stalled" not in timeline and "is_blocked" not in timeline
  186. def test_api_config_readonly_endpoints():
  187. client = TestClient(api.app)
  188. rule_packs = client.get("/config/rule-packs").json()
  189. assert rule_packs["source_file"].endswith("douyin_rule_packs.v1.json")
  190. assert len(rule_packs["data"]["rule_pack_dispatch"]) == 5
  191. assert len(rule_packs["data"]["effect_status_mapping"]) == 5
  192. walk = client.get("/config/walk-strategy").json()
  193. assert len(walk["data"]["walk_rule_pack_binding"]) == 8
  194. assert len(walk["data"]["walk_edge_catalog"]) == 10
  195. prompts = client.get("/config/query-prompts").json()
  196. assert "douyin/V1" in prompts["data"]["profiles"]
  197. def test_api_dashboard_rule_summary_effect_status_not_none(tmp_path, monkeypatch):
  198. monkeypatch.setattr(
  199. api,
  200. "service",
  201. RunService(
  202. runtime_root=tmp_path / "runtime" / "v1",
  203. demand_source=FakeDemandSource(),
  204. query_variant_client=FakeQueryVariantClient(),
  205. ),
  206. )
  207. client = TestClient(api.app)
  208. run_id = client.post("/runs", json={"platform": "douyin", "platform_mode": "mock"}).json()["run_id"]
  209. dashboard = client.get(f"/runs/{run_id}/dashboard").json()
  210. summaries = dashboard["rule_application_summary"]
  211. assert summaries
  212. # 修复前 content_effect_status 恒为 None(decision 记录无该字段)。
  213. assert all(row["content_effect_status"] is not None for row in summaries)
  214. # walk_graph edge 带归属/执行分离字段。
  215. walk_edges = [e for e in dashboard["walk_graph"]["edges"] if e.get("rule_pack")]
  216. assert any("rule_pack_executed" in e for e in walk_edges)