Files
twitter-cli-cookiefile/tests/test_serialization.py
jackwener 49d3e237c4 feat: add whoami, reply, quote, follow/unfollow commands and --compact mode
- whoami: fetch current authenticated user profile
- reply <id> <text>: standalone reply command
- quote <id> <text>: quote-tweet command
- follow/unfollow <handle>: follow/unfollow users
- --compact/-c: global flag for LLM-friendly minimal JSON output
- client.py: add fetch_me, quote_tweet, follow_user, unfollow_user
- serialization.py: add tweet_to_compact_dict, tweets_to_compact_json
- 7 new tests (82 total, all passing)
2026-03-10 20:09:08 +08:00

52 lines
1.6 KiB
Python

from __future__ import annotations
from twitter_cli.serialization import tweet_from_dict, tweet_to_dict, tweets_from_json, tweets_to_json
def test_tweet_roundtrip_dict(tweet_factory) -> None:
tweet = tweet_factory("42")
payload = tweet_to_dict(tweet)
restored = tweet_from_dict(payload)
assert restored.id == tweet.id
assert restored.author.screen_name == tweet.author.screen_name
assert restored.metrics.likes == tweet.metrics.likes
def test_tweets_json_roundtrip(tweet_factory) -> None:
tweets = [tweet_factory("1"), tweet_factory("2", lang="zh")]
raw = tweets_to_json(tweets)
restored = tweets_from_json(raw)
assert [tweet.id for tweet in restored] == ["1", "2"]
assert restored[1].lang == "zh"
def test_compact_serialization(tweet_factory) -> None:
from twitter_cli.serialization import tweet_to_compact_dict, tweets_to_compact_json
import json
tweet = tweet_factory(
"42",
created_at="Sat Mar 07 05:51:02 +0000 2026",
text="A" * 200,
)
compact = tweet_to_compact_dict(tweet)
assert compact["id"] == "42"
assert compact["author"] == "@alice"
assert compact["time"] == "Mar 07 05:51"
assert len(compact["text"]) <= 140
assert compact["text"].endswith("...")
assert compact["likes"] == 10
assert compact["rts"] == 2
# Should only have 6 keys
assert set(compact.keys()) == {"id", "author", "text", "likes", "rts", "time"}
# Test batch serialization
raw = tweets_to_compact_json([tweet])
parsed = json.loads(raw)
assert len(parsed) == 1
assert parsed[0]["author"] == "@alice"