fix: harden auth flow and sync browser support docs

This commit is contained in:
jackwener
2026-03-10 11:02:32 +08:00
parent db7d7e8874
commit 19ab11d6a4
8 changed files with 572 additions and 170 deletions

View File

@@ -1,6 +1,7 @@
from __future__ import annotations
from click.testing import CliRunner
import pytest
from twitter_cli.cli import cli
from twitter_cli.models import UserProfile
@@ -26,3 +27,64 @@ def test_cli_feed_json_input_path(tmp_path, tweet_factory) -> None:
result = runner.invoke(cli, ["feed", "--input", str(json_path), "--json"])
assert result.exit_code == 0
assert '"id": "1"' in result.output
@pytest.mark.parametrize(
"args",
[
["favorites"],
["bookmarks"],
["search", "x"],
["user-posts", "alice"],
["likes", "alice"],
["list", "123"],
],
)
def test_cli_commands_wrap_client_creation_errors(monkeypatch, args) -> None:
monkeypatch.setattr(
"twitter_cli.cli._get_client",
lambda config=None: (_ for _ in ()).throw(RuntimeError("boom")),
)
runner = CliRunner()
result = runner.invoke(cli, args)
assert result.exit_code == 1
assert "boom" in result.output
assert type(result.exception).__name__ == "SystemExit"
def test_cli_tweet_accepts_shared_url_with_query(monkeypatch) -> None:
class FakeClient:
def fetch_tweet_detail(self, tweet_id: str, max_count: int):
assert tweet_id == "12345"
assert max_count == 50
return []
monkeypatch.setattr("twitter_cli.cli._get_client", lambda config=None: FakeClient())
monkeypatch.setattr(
"twitter_cli.cli.load_config",
lambda: {"fetch": {"count": 50}, "filter": {}, "rateLimit": {}},
)
runner = CliRunner()
result = runner.invoke(cli, ["tweet", "https://x.com/user/status/12345?s=20"])
assert result.exit_code == 0
def test_cli_bookmark_alias_works(monkeypatch) -> None:
calls = []
class FakeClient:
def bookmark_tweet(self, tweet_id: str) -> bool:
calls.append(tweet_id)
return True
monkeypatch.setattr("twitter_cli.cli._get_client", lambda config=None: FakeClient())
runner = CliRunner()
result = runner.invoke(cli, ["bookmark", "123"])
assert result.exit_code == 0
assert calls == ["123"]