返回 last30days-skill
test_x_lane_parity.py
根目录 / tests / test_x_lane_parity.py
1 """x_lane_parity: compare the X results of two saved report JSON files."""
2
3 from __future__ import annotations
4
5 import json
6 from pathlib import Path
7
8 import pytest
9
10 import x_lane_parity as parity
11
12
13 def _item(post_id: int, handle: str, likes: int | None, **engagement) -> dict:
14 eng = {"likes": likes, "reposts": 1, "replies": 0, "quotes": 0, **engagement}
15 if likes is None:
16 eng.pop("likes")
17 return {
18 "source": "x",
19 "url": f"https://x.com/{handle}/status/{post_id}",
20 "author": handle,
21 "engagement": eng,
22 }
23
24
25 def _report(tmp_path: Path, name: str, items: list[dict], topic: str = "t") -> str:
26 path = tmp_path / name
27 path.write_text(json.dumps({
28 "topic": topic, "range_from": "2026-09-07", "range_to": "2026-10-07",
29 "items_by_source": {"x": items},
30 }), encoding="utf-8")
31 return str(path)
32
33
34 def test_counts_ratio_and_top_liked_recall(tmp_path):
35 baseline = [_item(100 + i, f"b{i}", likes=100 - i) for i in range(12)]
36 # The candidate holds 7 of the baseline's 10 most-liked posts plus 3 others.
37 candidate = [_item(100 + i, f"b{i}", likes=100 - i) for i in (0, 1, 2, 3, 4, 5, 6)]
38 candidate += [_item(900 + i, "c", likes=5) for i in range(3)]
39 result = parity.compare(_report(tmp_path, "a.json", baseline), _report(tmp_path, "b.json", candidate))
40 assert result["baseline_count"] == 12
41 assert result["candidate_count"] == 10
42 assert result["count_ratio"] == round(10 / 12, 3)
43 assert result["top_liked_recall"] == 0.7
44 assert result["baseline_authors"] == 12
45 assert result["candidate_authors"] == 8
46
47
48 def test_recall_also_counts_posts_present_only_in_the_envelope(tmp_path):
49 baseline = [_item(100 + i, f"b{i}", likes=50 - i) for i in range(10)]
50 candidate = [_item(100, "b0", likes=50)]
51 envelope = tmp_path / "env.json"
52 envelope.write_text(json.dumps({"topic": "t", "calls": [{"lane": "topic", "posts": [
53 {"id": str(100 + i), "author_handle": f"b{i}"} for i in range(1, 5)
54 ]}]}), encoding="utf-8")
55 result = parity.compare(
56 _report(tmp_path, "a.json", baseline), _report(tmp_path, "b.json", candidate), envelope=str(envelope),
57 )
58 assert result["top_liked_recall"] == 0.5
59
60
61 def test_field_completeness_counts_missing_metrics(tmp_path):
62 candidate = [_item(1, "a", likes=3), _item(2, "b", likes=None)]
63 result = parity.compare(_report(tmp_path, "a.json", [_item(1, "a", likes=3)]), _report(tmp_path, "b.json", candidate))
64 assert result["candidate_field_completeness"] == 0.5
65
66
67 def test_empty_reports_do_not_crash(tmp_path):
68 result = parity.compare(_report(tmp_path, "a.json", []), _report(tmp_path, "b.json", []))
69 assert result["baseline_count"] == 0
70 assert result["count_ratio"] == 0.0
71 assert result["top_liked_recall"] == 0.0
72
73
74 def test_reports_for_different_topics_are_refused(tmp_path):
75 with pytest.raises(ValueError, match="topic"):
76 parity.compare(_report(tmp_path, "a.json", [], topic="t"), _report(tmp_path, "b.json", [], topic="other"))
77
78
79 def test_envelope_for_another_topic_is_refused(tmp_path):
80 envelope = tmp_path / "env.json"
81 envelope.write_text(json.dumps({"topic": "other", "calls": []}), encoding="utf-8")
82 with pytest.raises(ValueError, match="envelope topic"):
83 parity.compare(_report(tmp_path, "a.json", []), _report(tmp_path, "b.json", []), envelope=str(envelope))
84
84 lines PYTHON