| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141 |
- from fastapi.testclient import TestClient
- from content_agent import api
- from content_agent.integrations.mock_platform import MockPlatformClient
- from content_agent.run_service import RunService
- from tests.p1_helpers import FakeDemandSource, FakeQueryVariantClient
- def test_api_runs_and_queries_mock_chain(tmp_path, monkeypatch):
- monkeypatch.setattr(
- api,
- "service",
- RunService(
- runtime_root=tmp_path / "runtime" / "v1",
- demand_source=FakeDemandSource(),
- query_variant_client=FakeQueryVariantClient(),
- ),
- )
- client = TestClient(api.app)
- response = client.post("/runs", json={"platform": "douyin", "platform_mode": "mock"})
- assert response.status_code == 200
- payload = response.json()
- run_id = payload["run_id"]
- assert payload["platform_mode"] == "mock"
- assert payload["policy_run_id"].startswith("policy_run_")
- assert payload["policy_bundle_id"] == "douyin_policy_bundle_v1"
- assert payload["strategy_version"] == "V1"
- for path in [
- f"/runs/{run_id}",
- f"/runs/{run_id}/discovered-content-items",
- f"/runs/{run_id}/rule-decisions",
- f"/runs/{run_id}/source-path-records",
- f"/runs/{run_id}/final-output",
- f"/runs/{run_id}/strategy-review",
- f"/runs/{run_id}/validation",
- ]:
- get_response = client.get(path)
- assert get_response.status_code == 200, path
- review = client.get(f"/runs/{run_id}/strategy-review").json()["data"]
- assert review["summary"]["pooled_content_count"] == 1
- assert review["suggestions"]
- validation = client.get(f"/runs/{run_id}/validation").json()
- assert validation["status"] == "pass"
- summary = client.get(f"/runs/{run_id}").json()
- assert summary["validation_status"] == "pass"
- def test_api_defaults_to_real_platform_mode_but_can_select_mock(tmp_path, monkeypatch):
- selected_modes = []
- def fake_platform_client(self, platform, platform_mode):
- selected_modes.append((platform, platform_mode))
- return MockPlatformClient()
- monkeypatch.setattr(RunService, "_platform_client", fake_platform_client)
- monkeypatch.setattr(
- api,
- "service",
- RunService(
- runtime_root=tmp_path / "runtime" / "v1",
- demand_source=FakeDemandSource(),
- query_variant_client=FakeQueryVariantClient(),
- ),
- )
- client = TestClient(api.app)
- default_response = client.post("/runs", json={})
- mock_response = client.post("/runs", json={"platform_mode": "mock"})
- assert default_response.status_code == 200
- assert default_response.json()["platform_mode"] == "real"
- assert mock_response.status_code == 200
- assert mock_response.json()["platform_mode"] == "mock"
- assert selected_modes == [("douyin", "real"), ("douyin", "mock")]
- def test_api_rejects_non_douyin_real_platform(tmp_path, monkeypatch):
- monkeypatch.setattr(
- api,
- "service",
- RunService(
- runtime_root=tmp_path / "runtime" / "v1",
- demand_source=FakeDemandSource(),
- query_variant_client=FakeQueryVariantClient(),
- ),
- )
- client = TestClient(api.app)
- response = client.post(
- "/runs", json={"platform": "other_platform", "platform_mode": "real"}
- )
- assert response.status_code == 400
- assert response.json()["detail"]["error_code"] == "INVALID_REQUEST"
- def test_api_returns_partial_success_as_successful_response(tmp_path, monkeypatch):
- service = RunService(
- runtime_root=tmp_path / "runtime" / "v1",
- demand_source=FakeDemandSource(),
- query_variant_client=FakeQueryVariantClient(),
- )
- service._platform_client = lambda platform, platform_mode: _PartialFailurePlatformClient()
- monkeypatch.setattr(api, "service", service)
- client = TestClient(api.app)
- response = client.post("/runs", json={"platform": "douyin", "platform_mode": "real"})
- assert response.status_code == 200
- payload = response.json()
- assert payload["status"] == "partial_success"
- summary = client.get(f"/runs/{payload['run_id']}").json()
- assert summary["status"] == "partial_success"
- def test_api_rejects_legacy_run_identifier_field(tmp_path, monkeypatch):
- monkeypatch.setattr(api, "service", RunService(runtime_root=tmp_path / "runtime" / "v1"))
- client = TestClient(api.app)
- legacy_run_key = "tr" + "ace_id"
- response = client.post(
- "/runs",
- json={"platform": "douyin", "platform_mode": "mock", legacy_run_key: "legacy_run_001"},
- )
- assert response.status_code == 422
- class _PartialFailurePlatformClient:
- def __init__(self) -> None:
- self.mock = MockPlatformClient()
- def search(self, search_query: dict) -> list[dict]:
- if search_query["search_query_id"] == "q_002":
- raise RuntimeError("temporary platform failure")
- return self.mock.search(search_query)
|