test_app_api.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  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_latest_query_result_list(self, query_id):
  127. assert query_id == self.query_id
  128. return {
  129. "query": {
  130. "id": query_id,
  131. "batch_id": self.batch_id,
  132. "query_text": "短视频脚本 开头 怎么写",
  133. "axes": {},
  134. "keep": True,
  135. "filter_reason": None,
  136. "status": "ready",
  137. "sort_order": 0,
  138. },
  139. "run": {
  140. "id": self.run_id,
  141. "batch_id": self.batch_id,
  142. "run_key": "run-1",
  143. "status": "running",
  144. },
  145. "jobs": [{"id": uuid4(), "platform": "xiaohongshu", "status": "done"}],
  146. "items": [
  147. {
  148. "id": self.item_id,
  149. "platform": "xiaohongshu",
  150. "title": "脚本开头",
  151. "raw_summary": "先定受众",
  152. "status": "candidate",
  153. "content_mode": "image_post",
  154. "metadata": {
  155. "page_index": 1,
  156. "matched_candidate": {"raw": "large"},
  157. "source_payload": {"raw": "large"},
  158. },
  159. }
  160. ],
  161. "media_assets": [
  162. {
  163. "id": self.media_id,
  164. "item_id": self.item_id,
  165. "media_type": "image",
  166. "source_url": "https://origin.test/1.jpg",
  167. "oss_url": "https://oss.test/1.jpg",
  168. "cdn_url": "https://cdn.test/1.jpg",
  169. "position": 1,
  170. "status": "done",
  171. }
  172. ],
  173. "classifications": [
  174. {
  175. "id": self.classification_id,
  176. "item_id": self.item_id,
  177. "is_creation_knowledge": True,
  178. "label": "creation",
  179. "confidence": 0.98,
  180. "status": "done",
  181. "error_message": None,
  182. }
  183. ],
  184. "decode_summaries": [
  185. {
  186. "item_id": self.item_id,
  187. "decode_status": "decoded",
  188. "particle_count": 1,
  189. "payload_count": 1,
  190. }
  191. ],
  192. }
  193. def get_candidate_item(self, item_id):
  194. assert item_id == self.item_id
  195. return self.get_query_detail(run_id=self.run_id, query_id=self.query_id)["items"][0]
  196. def list_media_assets_for_item(self, item_id):
  197. assert item_id == self.item_id
  198. return self.get_query_detail(run_id=self.run_id, query_id=self.query_id)["media_assets"]
  199. class FakeDecodeRepo:
  200. def __init__(self):
  201. self.decode_result_id = uuid4()
  202. self.decode_job_id = uuid4()
  203. self.payload_id = uuid4()
  204. def get_decode_result_for_item(self, item_id):
  205. return DecodeResult(
  206. id=self.decode_result_id,
  207. item_id=item_id,
  208. status="decoded",
  209. read_result={"text": "读懂后的帖子内容"},
  210. gate_result={"passed": True, "reason": "具备可复用创作方法"},
  211. framing_result={"particles": []},
  212. )
  213. def create_decode_job(self, *, item_id, status="pending", metadata=None):
  214. return DecodeJob(
  215. id=self.decode_job_id,
  216. item_id=item_id,
  217. status=status,
  218. metadata=metadata or {},
  219. )
  220. def list_payload_drafts(self, item_id=None):
  221. return [
  222. PayloadDraft(
  223. id=self.payload_id,
  224. item_id=item_id or uuid4(),
  225. payload={"title": "可审核 payload"},
  226. review_status="pending",
  227. ingest_ready=False,
  228. status="draft",
  229. )
  230. ]
  231. def list_knowledge_particles(self, item_id=None):
  232. return [
  233. KnowledgeParticle(
  234. id=uuid4(),
  235. item_id=item_id or uuid4(),
  236. particle_type="what",
  237. title="What I know",
  238. content={"content": "一个可复用知识点"},
  239. status="draft",
  240. )
  241. ]
  242. def list_scope_results(self, item_id=None):
  243. return [
  244. ScopeResult(
  245. id=uuid4(),
  246. item_id=item_id or uuid4(),
  247. scope_type="intent",
  248. scope_value="吸引注意",
  249. status="draft",
  250. )
  251. ]
  252. def list_ingest_records(self, item_id=None):
  253. return [
  254. IngestRecord(
  255. id=uuid4(),
  256. payload_draft_id=self.payload_id,
  257. target_system="dry-run",
  258. status="ingested",
  259. )
  260. ]
  261. class FakePipelineRepo:
  262. def get_pipeline_run(self, run_id):
  263. return PipelineRun(
  264. id=run_id,
  265. run_key="pipeline-run-1",
  266. status="running",
  267. current_stage="decode",
  268. )
  269. def _client(repo: FakeAcquisitionRepo):
  270. app.dependency_overrides[get_acquisition_repository] = lambda: repo
  271. return TestClient(app)
  272. def test_app_health_and_formal_acquisition_routes():
  273. repo = FakeAcquisitionRepo()
  274. client = _client(repo)
  275. try:
  276. assert client.get("/health").json()["entrypoint"] == "app.api:app"
  277. batch = client.get(f"/api/query-batches/{repo.batch_id}").json()
  278. assert batch["batch"]["name"] == "正式 query 批次"
  279. assert batch["queries"][0]["id"] == str(repo.query_id)
  280. summary = client.get(f"/api/acquisition/runs/{repo.run_id}/summary").json()
  281. assert summary["creation_hit_count"] == 2
  282. assert summary["queries"][0]["query_id"] == str(repo.query_id)
  283. detail = client.get(f"/api/acquisition/runs/{repo.run_id}/queries/{repo.query_id}").json()
  284. item = detail["platforms"]["xiaohongshu"]["items"][0]
  285. assert item["content_mode"] == "image_post"
  286. assert item["body_text"] == "先定受众,再写开头钩子"
  287. assert item["media_assets"][0]["cdn_url"] == "https://cdn.test/1.jpg"
  288. assert item["classification"]["is_creation_knowledge"] is True
  289. latest = client.get(f"/api/query-generation/latest/queries/{repo.query_id}").json()
  290. lightweight_item = latest["items"][0]
  291. assert "body_text" not in lightweight_item
  292. assert "source_payload" not in lightweight_item
  293. assert "matched_candidate" not in lightweight_item["metadata"]
  294. assert "source_payload" not in lightweight_item["metadata"]
  295. assert lightweight_item["raw_summary"] == "先定受众"
  296. assert len(lightweight_item["media_assets"]) == 1
  297. assert lightweight_item["decode_summary"]["particle_count"] == 1
  298. assert latest["platforms"]["xiaohongshu"]["item_ids"] == [str(repo.item_id)]
  299. assert "items" not in latest["platforms"]["xiaohongshu"]
  300. finally:
  301. app.dependency_overrides.clear()
  302. def test_app_compresses_large_json_responses():
  303. repo = FakeAcquisitionRepo()
  304. client = _client(repo)
  305. try:
  306. response = client.get(
  307. f"/api/query-generation/latest/queries/{repo.query_id}",
  308. headers={"Accept-Encoding": "gzip"},
  309. )
  310. assert response.status_code == 200
  311. assert response.headers.get("content-encoding") == "gzip"
  312. finally:
  313. app.dependency_overrides.clear()
  314. def test_query_generation_preview_defaults_to_first_two_families():
  315. client = TestClient(app)
  316. data = client.get("/api/query-generation/preview?per=2").json()
  317. assert data["summary"]["family_keys"] == ["f1", "f2"]
  318. assert [family["key"] for family in data["families"]] == ["f1", "f2"]
  319. assert data["summary"]["query_count"] == 4
  320. assert data["metadata"]["active_family_keys"] == ["f1", "f2"]
  321. def test_query_generation_preview_can_return_full_cartesian_combinations():
  322. client = TestClient(app)
  323. data = client.get("/api/query-generation/preview?per=0&batch_n=1").json()
  324. assert data["summary"]["family_keys"] == ["f1", "f2"]
  325. assert data["summary"]["query_count"] == 36
  326. assert [len(family["items"]) for family in data["families"]] == [18, 18]
  327. def test_formal_app_does_not_expose_legacy_static_mounts():
  328. paths = {route.path for route in app.routes if hasattr(route, "path")}
  329. assert "/data" not in paths
  330. assert "/frames" not in paths
  331. assert "/api/creation-search/summary" not in paths
  332. assert "/api/creation-search/query" not in paths
  333. def test_pipeline_route_returns_501_instead_of_legacy_data_when_not_wired():
  334. client = TestClient(app)
  335. run_id = uuid4()
  336. assert client.get(f"/api/pipeline/runs/{run_id}").status_code == 501
  337. def test_formal_decode_payload_pipeline_routes_have_override_success_paths():
  338. acquisition_repo = FakeAcquisitionRepo()
  339. decode_repo = FakeDecodeRepo()
  340. pipeline_repo = FakePipelineRepo()
  341. item_id = acquisition_repo.item_id
  342. run_id = uuid4()
  343. app.dependency_overrides[get_acquisition_repository] = lambda: acquisition_repo
  344. app.dependency_overrides[get_decode_repository] = lambda: decode_repo
  345. app.dependency_overrides[get_pipeline_repository] = lambda: pipeline_repo
  346. client = TestClient(app)
  347. try:
  348. result = client.get(f"/api/decode/items/{item_id}/result").json()
  349. assert result["item_id"] == str(item_id)
  350. assert result["read_result"]["text"] == "读懂后的帖子内容"
  351. job = client.post(f"/api/decode/items/{item_id}/jobs").json()["job"]
  352. assert job["item_id"] == str(item_id)
  353. assert job["status"] == "pending"
  354. drafts = client.get(f"/api/payloads/drafts?item_id={item_id}").json()["items"]
  355. assert drafts[0]["item_id"] == str(item_id)
  356. assert drafts[0]["payload"]["title"] == "可审核 payload"
  357. detail = client.get(f"/api/decode/items/{item_id}/detail").json()
  358. assert detail["item"]["id"] == str(item_id)
  359. assert detail["knowledge_particles"][0]["title"] == "What I know"
  360. assert detail["ingest_records"][0]["status"] == "ingested"
  361. run = client.get(f"/api/pipeline/runs/{run_id}").json()["run"]
  362. assert run["id"] == str(run_id)
  363. assert run["current_stage"] == "decode"
  364. finally:
  365. app.dependency_overrides.clear()
  366. def test_no_legacy_api_dependencies_in_formal_app_sources():
  367. roots = [Path("app"), Path("pipeline")]
  368. text = "\n".join(
  369. path.read_text(encoding="utf-8")
  370. for root in roots
  371. for path in root.rglob("*")
  372. if path.is_file() and path.suffix in {".py", ".jsx", ".js"}
  373. )
  374. assert "acquisition.store" not in text
  375. assert "CK_SQLITE_PATH" not in text
  376. assert "/data/queries" not in text
  377. assert "/api/creation-search" not in text
  378. assert "creation_knowledge.api" not in text
  379. def test_legacy_top_level_package_is_archived():
  380. assert not Path("creation_knowledge").exists()
  381. assert Path("archive/2026-06-30-step6/legacy_creation_knowledge/creation_knowledge").exists()
  382. def test_active_sources_do_not_import_legacy_store_or_package():
  383. roots = [Path("app"), Path("pipeline"), Path("acquisition"), Path("decode_content"), Path("scripts")]
  384. text = "\n".join(
  385. path.read_text(encoding="utf-8")
  386. for root in roots
  387. for path in root.rglob("*")
  388. if path.is_file() and path.suffix in {".py", ".jsx", ".js"}
  389. )
  390. assert "from acquisition import store" not in text
  391. assert "import acquisition.store" not in text
  392. assert "from creation_knowledge" not in text
  393. assert "import creation_knowledge" not in text