diff --git a/api/routers/monitor.py b/api/routers/monitor.py index 26c9e51..fb40058 100644 --- a/api/routers/monitor.py +++ b/api/routers/monitor.py @@ -172,6 +172,12 @@ async def list_comments( "note_title": comment["note_title"], "note_cover": comment["note_cover"], "note_url": comment["note_url"], + # 作品所属的创作者。评论流按 博主 → 作品 → 评论 三级展开时,最外层 + # 就是按这两个字段分组的 —— 少了它们,前端拿到的是 undefined, + # 于是所有博主塌成同一个分组、标签回退成「未知博主」。 + # 同一个桶里的评论必然同属一个作品,所以取哪一条都一样。 + "creator_hash": comment["note_creator_hash"], + "creator_name": comment["note_creator_name"], "comments": [], }, ) diff --git a/tests/test_monitor_comments.py b/tests/test_monitor_comments.py index 6ca7152..60cbd89 100644 --- a/tests/test_monitor_comments.py +++ b/tests/test_monitor_comments.py @@ -49,12 +49,17 @@ async def _seed(): session.add(task) await session.flush() - for note_id, title in (("note-a", "作品甲"), ("note-b", "作品乙")): + # 两个作品**属于不同的博主** —— 评论流最外层按创作者分组,同一个人就没得测了。 + for note_id, title, creator_hash, creator_name in ( + ("note-a", "作品甲", "hash-a", "博主甲"), + ("note-b", "作品乙", "hash-b", "博主乙"), + ): session.add( MonitorNote( task_id=task.id, note_id=note_id, title=title, note_url=f"https://www.xiaohongshu.com/explore/{note_id}", - cover=f"https://img/{note_id}.jpg", creator_hash="h", + cover=f"https://img/{note_id}.jpg", creator_hash=creator_hash, + creator_name=creator_name, source_kind="video", published_at=None, first_seen_run_id=1, first_seen_at=1_700_000_000_000, last_seen_run_id=1, last_seen_at=1_700_000_000_000, @@ -139,6 +144,27 @@ class TestGroupByNote: # note-b's only comment is the most recent overall. assert groups[0]["note_id"] == "note-b" + @pytest.mark.asyncio + async def test_each_bucket_carries_its_creator(self, client): + """桶上必须带作品的创作者 —— 评论流最外层就是按它分组的。 + + 少了这两个字段,前端拿到的 creator_hash / creator_name 都是 undefined, + 于是所有博主塌成同一个分组、标签回退成「未知博主」:一个人都分不出来。 + """ + groups = { + group["note_id"]: group + for group in ( + await client.get("/api/monitor/comments", params={"group_by": "note"}) + ).json()["groups"] + } + + assert groups["note-a"]["creator_hash"] == "hash-a" + assert groups["note-a"]["creator_name"] == "博主甲" + assert groups["note-b"]["creator_hash"] == "hash-b" + assert groups["note-b"]["creator_name"] == "博主乙" + # 两个作品的创作者必须真的不同,否则界面上照样分不出来。 + assert groups["note-a"]["creator_hash"] != groups["note-b"]["creator_hash"] + @pytest.mark.asyncio async def test_flat_shape_is_unchanged_without_the_flag(self, client): body = (await client.get("/api/monitor/comments")).json()