Add list cursor pagination (#56)

This commit is contained in:
kshift
2026-05-07 23:53:22 +08:00
committed by GitHub
parent 1f3a9ee535
commit 7c634e0d39
6 changed files with 107 additions and 8 deletions

View File

@@ -119,6 +119,37 @@ def test_cli_feed_accepts_cursor_and_emits_pagination(monkeypatch) -> None:
assert payload["pagination"]["nextCursor"] == "cursor-next"
def test_cli_list_accepts_cursor_and_emits_pagination(monkeypatch, tweet_factory) -> None:
class FakeClient:
def fetch_list_timeline(
self,
list_id: str,
count: int,
cursor: str | None = None,
return_cursor: bool = False,
):
assert list_id == "123"
assert count == 20
assert cursor == "cursor-prev"
assert return_cursor is True
return [tweet_factory("1")], "cursor-next"
monkeypatch.setattr("twitter_cli.cli._get_client", lambda config=None, quiet=False: FakeClient())
monkeypatch.setattr(
"twitter_cli.cli.load_config",
lambda: {"fetch": {"count": 20}, "filter": {}, "rateLimit": {}},
)
runner = CliRunner()
result = runner.invoke(cli, ["list", "123", "--cursor", "cursor-prev", "--json"])
assert result.exit_code == 0
payload = json.loads(result.output)
assert payload["ok"] is True
assert payload["data"][0]["id"] == "1"
assert payload["pagination"]["nextCursor"] == "cursor-next"
def test_print_tweet_table_truncates_text_by_default(tweet_factory) -> None:
long_text = "A" * 140
console = Console(record=True, width=400)

View File

@@ -424,6 +424,34 @@ class TestPaginationBehavior:
assert cursor == "cursor-next"
assert calls[0]["cursor"] == "cursor-prev"
def test_fetch_list_timeline_accepts_cursor_and_returns_cursor(self):
client = TwitterClient.__new__(TwitterClient)
client._request_delay = 0.0
client._max_count = 200
calls = []
def _graphql_get(operation_name, variables, features, field_toggles=None):
calls.append((operation_name, variables.copy()))
return {"page": 1}
client._graphql_get = _graphql_get
tweet = MagicMock(id="tweet-1")
with patch('twitter_cli.client.parse_timeline_response', return_value=([tweet], "cursor-next")):
tweets, cursor = client.fetch_list_timeline(
"list-1",
1,
cursor="cursor-prev",
return_cursor=True,
)
assert [item.id for item in tweets] == ["tweet-1"]
assert cursor == "cursor-next"
assert calls[0][0] == "ListLatestTweetsTimeline"
assert calls[0][1]["listId"] == "list-1"
assert calls[0][1]["cursor"] == "cursor-prev"
def test_user_list_continues_when_cursor_advances_without_new_users(self):
client = TwitterClient.__new__(TwitterClient)
client._request_delay = 0.0