test_app_api.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. from __future__ import annotations
  2. from pathlib import Path
  3. from uuid import uuid4
  4. from fastapi.testclient import TestClient
  5. from acquisition.domain import Query, QueryBatch
  6. from app.api import app
  7. from app.dependencies import (
  8. get_acquisition_repository,
  9. get_decode_repository,
  10. get_pipeline_repository,
  11. )
  12. from decode_content.models import (
  13. DecodeJob,
  14. DecodeResult,
  15. IngestRecord,
  16. KnowledgeParticle,
  17. PayloadDraft,
  18. ScopeResult,
  19. )
  20. from pipeline.models import PipelineRun
  21. class FakeAcquisitionRepo:
  22. def __init__(self):
  23. self.batch_id = uuid4()
  24. self.run_id = uuid4()
  25. self.query_id = uuid4()
  26. self.item_id = uuid4()
  27. self.media_id = uuid4()
  28. self.classification_id = uuid4()
  29. def get_query_batch(self, batch_id):
  30. assert batch_id == self.batch_id
  31. return QueryBatch(
  32. id=batch_id,
  33. name="正式 query 批次",
  34. source_type="manual",
  35. target_platforms=["xiaohongshu", "weixin", "douyin"],
  36. status="ready",
  37. )
  38. def list_queries_for_batch(self, batch_id, *, keep=None):
  39. assert batch_id == self.batch_id
  40. return [
  41. Query(
  42. id=self.query_id,
  43. batch_id=batch_id,
  44. query_text="短视频脚本 开头 怎么写",
  45. keep=True,
  46. status="ready",
  47. )
  48. ]
  49. def get_run_summary(self, run_id):
  50. assert run_id == self.run_id
  51. return {
  52. "id": run_id,
  53. "run_key": "run-1",
  54. "batch_id": self.batch_id,
  55. "status": "running",
  56. "query_count": 1,
  57. "job_count": 3,
  58. "candidate_count": 5,
  59. "creation_hit_count": 2,
  60. "queries": [
  61. {
  62. "query_id": self.query_id,
  63. "query_text": "短视频脚本 开头 怎么写",
  64. "candidate_count": 5,
  65. "creation_hit_count": 2,
  66. "platforms": {"xiaohongshu": {"status": "done"}},
  67. }
  68. ],
  69. }
  70. def get_query_detail(self, *, run_id, query_id):
  71. assert run_id == self.run_id
  72. assert query_id == self.query_id
  73. return {
  74. "query": {
  75. "id": query_id,
  76. "batch_id": self.batch_id,
  77. "query_text": "短视频脚本 开头 怎么写",
  78. "axes": {},
  79. "keep": True,
  80. "filter_reason": None,
  81. "status": "ready",
  82. "sort_order": 0,
  83. },
  84. "jobs": [{"id": uuid4(), "platform": "xiaohongshu", "status": "done"}],
  85. "items": [
  86. {
  87. "id": self.item_id,
  88. "platform": "xiaohongshu",
  89. "platform_item_id": "xhs1",
  90. "unique_key": "xhs:xhs1",
  91. "canonical_url": "https://xhs.test/1",
  92. "content_type": "图文",
  93. "content_mode": "image_post",
  94. "title": "脚本开头",
  95. "author_name": "作者",
  96. "body_text": "先定受众,再写开头钩子",
  97. "raw_summary": "先定受众",
  98. "status": "candidate",
  99. }
  100. ],
  101. "media_assets": [
  102. {
  103. "id": self.media_id,
  104. "item_id": self.item_id,
  105. "media_type": "image",
  106. "source_url": "https://origin.test/1.jpg",
  107. "oss_url": "https://oss.test/1.jpg",
  108. "cdn_url": "https://cdn.test/1.jpg",
  109. "position": 1,
  110. "status": "done",
  111. }
  112. ],
  113. "classifications": [
  114. {
  115. "id": self.classification_id,
  116. "item_id": self.item_id,
  117. "is_creation_knowledge": True,
  118. "label": "creation",
  119. "confidence": 0.98,
  120. "reason": "可复用",
  121. "status": "done",
  122. "error_message": None,
  123. }
  124. ],
  125. }
  126. def get_candidate_item(self, item_id):
  127. assert item_id == self.item_id
  128. return self.get_query_detail(run_id=self.run_id, query_id=self.query_id)["items"][0]
  129. def list_media_assets_for_item(self, item_id):
  130. assert item_id == self.item_id
  131. return self.get_query_detail(run_id=self.run_id, query_id=self.query_id)["media_assets"]
  132. class FakeDecodeRepo:
  133. def __init__(self):
  134. self.decode_result_id = uuid4()
  135. self.decode_job_id = uuid4()
  136. self.payload_id = uuid4()
  137. def get_decode_result_for_item(self, item_id):
  138. return DecodeResult(
  139. id=self.decode_result_id,
  140. item_id=item_id,
  141. status="decoded",
  142. read_result={"text": "读懂后的帖子内容"},
  143. gate_result={"passed": True, "reason": "具备可复用创作方法"},
  144. framing_result={"particles": []},
  145. )
  146. def create_decode_job(self, *, item_id, status="pending", metadata=None):
  147. return DecodeJob(
  148. id=self.decode_job_id,
  149. item_id=item_id,
  150. status=status,
  151. metadata=metadata or {},
  152. )
  153. def list_payload_drafts(self, item_id=None):
  154. return [
  155. PayloadDraft(
  156. id=self.payload_id,
  157. item_id=item_id or uuid4(),
  158. payload={"title": "可审核 payload"},
  159. review_status="pending",
  160. ingest_ready=False,
  161. status="draft",
  162. )
  163. ]
  164. def list_knowledge_particles(self, item_id=None):
  165. return [
  166. KnowledgeParticle(
  167. id=uuid4(),
  168. item_id=item_id or uuid4(),
  169. particle_type="what",
  170. title="What I know",
  171. content={"content": "一个可复用知识点"},
  172. status="draft",
  173. )
  174. ]
  175. def list_scope_results(self, item_id=None):
  176. return [
  177. ScopeResult(
  178. id=uuid4(),
  179. item_id=item_id or uuid4(),
  180. scope_type="intent",
  181. scope_value="吸引注意",
  182. status="draft",
  183. )
  184. ]
  185. def list_ingest_records(self, item_id=None):
  186. return [
  187. IngestRecord(
  188. id=uuid4(),
  189. payload_draft_id=self.payload_id,
  190. target_system="dry-run",
  191. status="ingested",
  192. )
  193. ]
  194. class FakePipelineRepo:
  195. def get_pipeline_run(self, run_id):
  196. return PipelineRun(
  197. id=run_id,
  198. run_key="pipeline-run-1",
  199. status="running",
  200. current_stage="decode",
  201. )
  202. def _client(repo: FakeAcquisitionRepo):
  203. app.dependency_overrides[get_acquisition_repository] = lambda: repo
  204. return TestClient(app)
  205. def test_app_health_and_formal_acquisition_routes():
  206. repo = FakeAcquisitionRepo()
  207. client = _client(repo)
  208. try:
  209. assert client.get("/health").json()["entrypoint"] == "app.api:app"
  210. batch = client.get(f"/api/query-batches/{repo.batch_id}").json()
  211. assert batch["batch"]["name"] == "正式 query 批次"
  212. assert batch["queries"][0]["id"] == str(repo.query_id)
  213. summary = client.get(f"/api/acquisition/runs/{repo.run_id}/summary").json()
  214. assert summary["creation_hit_count"] == 2
  215. assert summary["queries"][0]["query_id"] == str(repo.query_id)
  216. detail = client.get(f"/api/acquisition/runs/{repo.run_id}/queries/{repo.query_id}").json()
  217. item = detail["platforms"]["xiaohongshu"]["items"][0]
  218. assert item["content_mode"] == "image_post"
  219. assert item["body_text"] == "先定受众,再写开头钩子"
  220. assert item["media_assets"][0]["cdn_url"] == "https://cdn.test/1.jpg"
  221. assert item["classification"]["is_creation_knowledge"] is True
  222. finally:
  223. app.dependency_overrides.clear()
  224. def test_query_generation_preview_defaults_to_first_two_families():
  225. client = TestClient(app)
  226. data = client.get("/api/query-generation/preview?per=2").json()
  227. assert data["summary"]["family_keys"] == ["f1", "f2"]
  228. assert [family["key"] for family in data["families"]] == ["f1", "f2"]
  229. assert data["summary"]["query_count"] == 4
  230. assert data["metadata"]["active_family_keys"] == ["f1", "f2"]
  231. def test_query_generation_preview_can_return_full_cartesian_combinations():
  232. client = TestClient(app)
  233. data = client.get("/api/query-generation/preview?per=0&batch_n=1").json()
  234. assert data["summary"]["family_keys"] == ["f1", "f2"]
  235. assert data["summary"]["query_count"] == 36
  236. assert [len(family["items"]) for family in data["families"]] == [18, 18]
  237. def test_formal_app_does_not_expose_legacy_static_mounts():
  238. paths = {route.path for route in app.routes if hasattr(route, "path")}
  239. assert "/data" not in paths
  240. assert "/frames" not in paths
  241. assert "/api/creation-search/summary" not in paths
  242. assert "/api/creation-search/query" not in paths
  243. def test_pipeline_route_returns_501_instead_of_legacy_data_when_not_wired():
  244. client = TestClient(app)
  245. run_id = uuid4()
  246. assert client.get(f"/api/pipeline/runs/{run_id}").status_code == 501
  247. def test_formal_decode_payload_pipeline_routes_have_override_success_paths():
  248. acquisition_repo = FakeAcquisitionRepo()
  249. decode_repo = FakeDecodeRepo()
  250. pipeline_repo = FakePipelineRepo()
  251. item_id = acquisition_repo.item_id
  252. run_id = uuid4()
  253. app.dependency_overrides[get_acquisition_repository] = lambda: acquisition_repo
  254. app.dependency_overrides[get_decode_repository] = lambda: decode_repo
  255. app.dependency_overrides[get_pipeline_repository] = lambda: pipeline_repo
  256. client = TestClient(app)
  257. try:
  258. result = client.get(f"/api/decode/items/{item_id}/result").json()
  259. assert result["item_id"] == str(item_id)
  260. assert result["read_result"]["text"] == "读懂后的帖子内容"
  261. job = client.post(f"/api/decode/items/{item_id}/jobs").json()["job"]
  262. assert job["item_id"] == str(item_id)
  263. assert job["status"] == "pending"
  264. drafts = client.get(f"/api/payloads/drafts?item_id={item_id}").json()["items"]
  265. assert drafts[0]["item_id"] == str(item_id)
  266. assert drafts[0]["payload"]["title"] == "可审核 payload"
  267. detail = client.get(f"/api/decode/items/{item_id}/detail").json()
  268. assert detail["item"]["id"] == str(item_id)
  269. assert detail["knowledge_particles"][0]["title"] == "What I know"
  270. assert detail["ingest_records"][0]["status"] == "ingested"
  271. run = client.get(f"/api/pipeline/runs/{run_id}").json()["run"]
  272. assert run["id"] == str(run_id)
  273. assert run["current_stage"] == "decode"
  274. finally:
  275. app.dependency_overrides.clear()
  276. def test_no_legacy_api_dependencies_in_formal_app_sources():
  277. roots = [Path("app"), Path("pipeline")]
  278. text = "\n".join(
  279. path.read_text(encoding="utf-8")
  280. for root in roots
  281. for path in root.rglob("*")
  282. if path.is_file() and path.suffix in {".py", ".jsx", ".js"}
  283. )
  284. assert "acquisition.store" not in text
  285. assert "CK_SQLITE_PATH" not in text
  286. assert "/data/queries" not in text
  287. assert "/api/creation-search" not in text
  288. assert "creation_knowledge.api" not in text
  289. def test_legacy_top_level_package_is_archived():
  290. assert not Path("creation_knowledge").exists()
  291. assert Path("archive/2026-06-30-step6/legacy_creation_knowledge/creation_knowledge").exists()
  292. def test_active_sources_do_not_import_legacy_store_or_package():
  293. roots = [Path("app"), Path("pipeline"), Path("acquisition"), Path("decode_content"), Path("scripts")]
  294. text = "\n".join(
  295. path.read_text(encoding="utf-8")
  296. for root in roots
  297. for path in root.rglob("*")
  298. if path.is_file() and path.suffix in {".py", ".jsx", ".js"}
  299. )
  300. assert "from acquisition import store" not in text
  301. assert "import acquisition.store" not in text
  302. assert "from creation_knowledge" not in text
  303. assert "import creation_knowledge" not in text