"""M2 离线测试:用 5 个真实 capture 验证 parse_detail_response 字段映射。""" from __future__ import annotations import json from pathlib import Path import pytest from creation_knowledge.integrations.crawler import ( parse_content_id, parse_detail_response, ) FIXTURES = Path(__file__).parent / "fixtures" # content_id -> (期望作者, 标题里应出现的子串) EXPECTED = { "67e4bdf50000000006028a59": ("海狸教自媒体运营", "短视频脚本"), "698481e1000000000a02a7c1": ("Irvin是个编剧(接稿版)", "叙事结构"), "67e2e39b0000000003028ff0": ("拾意", "剧本创作"), "699308fa0000000016009697": ("方圆的增长飞轮", "选题"), "680659e8000000001a007a11": ("故事设计原理拆解学习"[:2], "故事设计"), } def _load(content_id: str) -> dict: return json.loads((FIXTURES / f"xhs_case_{content_id}.json").read_text("utf-8")) @pytest.mark.parametrize("content_id", list(EXPECTED)) def test_parse_detail_fields(content_id: str): # 重抓的 xhs_case_*.json 即原始响应 {code,msg,data},无 response 包裹 response = _load(content_id) post = parse_detail_response(response, fallback_content_id=content_id) assert post.id == f"xhs_{content_id}" assert post.platform == "xiaohongshu" assert post.content_id == content_id assert content_id in post.url assert post.title, "title 不应为空" _, title_sub = EXPECTED[content_id] assert title_sub in post.title assert post.raw.get("code") == 0 def test_authors(): for cid, (author, _) in EXPECTED.items(): post = parse_detail_response(_load(cid), fallback_content_id=cid) if cid in ("67e4bdf50000000006028a59", "698481e1000000000a02a7c1", "67e2e39b0000000003028ff0", "699308fa0000000016009697"): assert post.author_name == author def test_shiyi_posts_text_sparse(): """拾意两条正文几乎只有话题串——M3 多模态提取存在的理由。""" for cid in ("67e2e39b0000000003028ff0", "680659e8000000001a007a11"): post = parse_detail_response(_load(cid), fallback_content_id=cid) stripped = post.body_text.replace("#", "").replace("话题", "").strip() assert len(stripped) < 40, f"{cid} body_text 应很稀疏: {post.body_text!r}" def test_parse_content_id_from_url(): url = ("https://www.xiaohongshu.com/explore/67e4bdf50000000006028a59" "?xsec_token=ABC=&xsec_source=pc_like") assert parse_content_id(url) == "67e4bdf50000000006028a59" assert parse_content_id("67e4bdf50000000006028a59") == "67e4bdf50000000006028a59"