twitter-cli 0.8.3__tar.gz → 0.8.5__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- twitter_cli-0.8.5/AGENTS.md +70 -0
- {twitter_cli-0.8.3 → twitter_cli-0.8.5}/PKG-INFO +25 -1
- {twitter_cli-0.8.3 → twitter_cli-0.8.5}/README.md +24 -0
- {twitter_cli-0.8.3 → twitter_cli-0.8.5}/SKILL.md +1 -0
- {twitter_cli-0.8.3 → twitter_cli-0.8.5}/pyproject.toml +1 -1
- {twitter_cli-0.8.3 → twitter_cli-0.8.5}/tests/test_auth.py +17 -1
- twitter_cli-0.8.5/tests/test_cache.py +101 -0
- {twitter_cli-0.8.3 → twitter_cli-0.8.5}/tests/test_cli.py +111 -33
- twitter_cli-0.8.5/tests/test_filter.py +88 -0
- twitter_cli-0.8.5/tests/test_output.py +147 -0
- twitter_cli-0.8.5/tests/test_timeutil.py +73 -0
- {twitter_cli-0.8.3 → twitter_cli-0.8.5}/twitter_cli/auth.py +50 -18
- {twitter_cli-0.8.3 → twitter_cli-0.8.5}/twitter_cli/cli.py +230 -100
- {twitter_cli-0.8.3 → twitter_cli-0.8.5}/twitter_cli/client.py +54 -1
- twitter_cli-0.8.5/twitter_cli/commands/__init__.py +7 -0
- {twitter_cli-0.8.3 → twitter_cli-0.8.5}/twitter_cli/constants.py +0 -2
- {twitter_cli-0.8.3 → twitter_cli-0.8.5}/twitter_cli/exceptions.py +30 -1
- {twitter_cli-0.8.3 → twitter_cli-0.8.5}/twitter_cli/formatter.py +19 -6
- {twitter_cli-0.8.3 → twitter_cli-0.8.5}/twitter_cli/graphql.py +2 -0
- {twitter_cli-0.8.3 → twitter_cli-0.8.5}/twitter_cli/models.py +6 -0
- {twitter_cli-0.8.3 → twitter_cli-0.8.5}/twitter_cli/output.py +22 -0
- {twitter_cli-0.8.3 → twitter_cli-0.8.5}/twitter_cli/serialization.py +17 -2
- {twitter_cli-0.8.3 → twitter_cli-0.8.5}/twitter_cli/timeutil.py +11 -0
- {twitter_cli-0.8.3 → twitter_cli-0.8.5}/uv.lock +1 -1
- twitter_cli-0.8.3/.github/workflows/publish-clawhub.yml +0 -127
- twitter_cli-0.8.3/tests/test_filter.py +0 -31
- {twitter_cli-0.8.3 → twitter_cli-0.8.5}/.github/ISSUE_TEMPLATE/bug_report.yml +0 -0
- {twitter_cli-0.8.3 → twitter_cli-0.8.5}/.github/ISSUE_TEMPLATE/feature_request.yml +0 -0
- {twitter_cli-0.8.3 → twitter_cli-0.8.5}/.github/workflows/ci.yml +0 -0
- {twitter_cli-0.8.3 → twitter_cli-0.8.5}/.github/workflows/publish.yml +0 -0
- {twitter_cli-0.8.3 → twitter_cli-0.8.5}/.gitignore +0 -0
- {twitter_cli-0.8.3 → twitter_cli-0.8.5}/LICENSE +0 -0
- {twitter_cli-0.8.3 → twitter_cli-0.8.5}/SCHEMA.md +0 -0
- {twitter_cli-0.8.3 → twitter_cli-0.8.5}/config.yaml +0 -0
- {twitter_cli-0.8.3 → twitter_cli-0.8.5}/tests/conftest.py +0 -0
- {twitter_cli-0.8.3 → twitter_cli-0.8.5}/tests/fixtures/followers_page.json +0 -0
- {twitter_cli-0.8.3 → twitter_cli-0.8.5}/tests/fixtures/home_timeline.json +0 -0
- {twitter_cli-0.8.3 → twitter_cli-0.8.5}/tests/fixtures/list_timeline.json +0 -0
- {twitter_cli-0.8.3 → twitter_cli-0.8.5}/tests/fixtures/search_timeline.json +0 -0
- {twitter_cli-0.8.3 → twitter_cli-0.8.5}/tests/fixtures/tweet_detail.json +0 -0
- {twitter_cli-0.8.3 → twitter_cli-0.8.5}/tests/test_client.py +0 -0
- {twitter_cli-0.8.3 → twitter_cli-0.8.5}/tests/test_config.py +0 -0
- {twitter_cli-0.8.3 → twitter_cli-0.8.5}/tests/test_config_normalization.py +0 -0
- {twitter_cli-0.8.3 → twitter_cli-0.8.5}/tests/test_parser_fixtures.py +0 -0
- {twitter_cli-0.8.3 → twitter_cli-0.8.5}/tests/test_search.py +0 -0
- {twitter_cli-0.8.3 → twitter_cli-0.8.5}/tests/test_serialization.py +0 -0
- {twitter_cli-0.8.3 → twitter_cli-0.8.5}/tests/test_smoke.py +0 -0
- {twitter_cli-0.8.3 → twitter_cli-0.8.5}/twitter_cli/__init__.py +0 -0
- {twitter_cli-0.8.3 → twitter_cli-0.8.5}/twitter_cli/cache.py +0 -0
- {twitter_cli-0.8.3 → twitter_cli-0.8.5}/twitter_cli/config.py +0 -0
- {twitter_cli-0.8.3 → twitter_cli-0.8.5}/twitter_cli/filter.py +0 -0
- {twitter_cli-0.8.3 → twitter_cli-0.8.5}/twitter_cli/parser.py +0 -0
- {twitter_cli-0.8.3 → twitter_cli-0.8.5}/twitter_cli/search.py +0 -0
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
# AGENTS.md — Agent Developer Guide for twitter-cli
|
|
2
|
+
|
|
3
|
+
This file provides context for AI agents working in this repository.
|
|
4
|
+
|
|
5
|
+
## Project Overview
|
|
6
|
+
|
|
7
|
+
- **Project**: twitter-cli — A CLI for Twitter/X (read timelines, bookmarks, search, post, reply, etc.)
|
|
8
|
+
- **Language**: Python 3.10+
|
|
9
|
+
- **Package Manager**: uv (recommended) / pip
|
|
10
|
+
- **Repository**: https://github.com/jackwener/twitter-cli
|
|
11
|
+
|
|
12
|
+
## Build, Lint, and Test Commands
|
|
13
|
+
|
|
14
|
+
```bash
|
|
15
|
+
# Install all dependencies (including dev)
|
|
16
|
+
uv sync --extra dev
|
|
17
|
+
|
|
18
|
+
# Run ruff linter
|
|
19
|
+
uv run ruff check .
|
|
20
|
+
|
|
21
|
+
# Run mypy type checker
|
|
22
|
+
uv run mypy twitter_cli
|
|
23
|
+
|
|
24
|
+
# Run all tests (excludes smoke tests by default)
|
|
25
|
+
uv run pytest -q
|
|
26
|
+
|
|
27
|
+
# Run a single test
|
|
28
|
+
uv run pytest tests/test_cli.py::test_feed_command -v
|
|
29
|
+
|
|
30
|
+
# Run tests matching pattern
|
|
31
|
+
uv run pytest -k "test_parse" -v
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
## Code Style
|
|
35
|
+
|
|
36
|
+
- **Line length**: 100 characters
|
|
37
|
+
- **Python version**: 3.10+
|
|
38
|
+
- Use `from __future__ import annotations` at top of all .py files
|
|
39
|
+
- **Functions/variables**: `snake_case`, **Classes**: `PascalCase`, **Constants**: `UPPER_SNAKE_CASE`
|
|
40
|
+
- Private functions: prefix with `_`
|
|
41
|
+
- Use `@dataclass` for data models (in `models.py`)
|
|
42
|
+
- Use Click framework for CLI commands
|
|
43
|
+
- Custom exceptions in `exceptions.py`, base: `TwitterError(RuntimeError)`
|
|
44
|
+
|
|
45
|
+
## Project Structure
|
|
46
|
+
|
|
47
|
+
```
|
|
48
|
+
twitter_cli/
|
|
49
|
+
├── cli.py # Click CLI entry point
|
|
50
|
+
├── client.py # Twitter API client (HTTP)
|
|
51
|
+
├── auth.py # Cookie extraction & auth
|
|
52
|
+
├── graphql.py # GraphQL query IDs
|
|
53
|
+
├── parser.py # Tweet/User parsing
|
|
54
|
+
├── models.py # Dataclass models
|
|
55
|
+
├── formatter.py # Rich table formatting
|
|
56
|
+
├── serialization.py # YAML/JSON output
|
|
57
|
+
├── output.py # Structured output helpers
|
|
58
|
+
├── config.py # Config loading
|
|
59
|
+
├── filter.py # Tweet ranking/scoring
|
|
60
|
+
├── constants.py # Constants
|
|
61
|
+
├── exceptions.py # Custom exceptions
|
|
62
|
+
├── cache.py # Tweet caching
|
|
63
|
+
├── search.py # Search utilities
|
|
64
|
+
└── timeutil.py # Time utilities
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
## CI
|
|
68
|
+
|
|
69
|
+
- GitHub Actions: Python 3.10, 3.11, 3.12
|
|
70
|
+
- CI validates: ruff check + mypy + pytest
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: twitter-cli
|
|
3
|
-
Version: 0.8.
|
|
3
|
+
Version: 0.8.5
|
|
4
4
|
Summary: A CLI for Twitter/X — feed, bookmarks, and user timeline in terminal
|
|
5
5
|
Project-URL: Homepage, https://github.com/jackwener/twitter-cli
|
|
6
6
|
Project-URL: Repository, https://github.com/jackwener/twitter-cli
|
|
@@ -214,6 +214,12 @@ Browser extraction is recommended — it forwards ALL Twitter cookies (not just
|
|
|
214
214
|
TWITTER_CHROME_PROFILE="Profile 2" twitter feed
|
|
215
215
|
```
|
|
216
216
|
|
|
217
|
+
**Browser priority:** If you have multiple browsers, set `TWITTER_BROWSER` to try a specific browser first:
|
|
218
|
+
|
|
219
|
+
```bash
|
|
220
|
+
TWITTER_BROWSER=chrome twitter feed # Supported: arc, chrome, edge, firefox, brave
|
|
221
|
+
```
|
|
222
|
+
|
|
217
223
|
After loading cookies, the CLI performs lightweight verification. Commands that require account access fail fast on clear auth errors (`401/403`).
|
|
218
224
|
|
|
219
225
|
### Proxy Support
|
|
@@ -324,6 +330,12 @@ Mode behavior:
|
|
|
324
330
|
- `Invalid tweet JSON file`
|
|
325
331
|
- Regenerate input using `twitter feed --json > tweets.json`.
|
|
326
332
|
|
|
333
|
+
- **Windows: no output captured by pipe/subprocess** (AI agent integration)
|
|
334
|
+
- This is a **ConPTY** issue, not a twitter-cli bug. Windows Terminal's ConPTY pseudo-terminal can intercept pipe output from commands with network latency.
|
|
335
|
+
- **Fix**: Use **Git Bash** as your terminal shell and set `"windowsEnableConpty": false` in your terminal settings.
|
|
336
|
+
- If disabling ConPTY with PowerShell, emoji output may fail with `UnicodeEncodeError: 'gbk'`. Git Bash handles UTF-8 natively.
|
|
337
|
+
- Standard `subprocess.run(capture_output=True)` and file redirection (`> file 2>&1`) work correctly regardless of ConPTY.
|
|
338
|
+
|
|
327
339
|
|
|
328
340
|
|
|
329
341
|
Structured error codes commonly include `not_authenticated`, `not_found`, `invalid_input`, `rate_limited`, and `api_error`.
|
|
@@ -523,6 +535,12 @@ twitter follow elonmusk --json
|
|
|
523
535
|
TWITTER_CHROME_PROFILE="Profile 2" twitter feed
|
|
524
536
|
```
|
|
525
537
|
|
|
538
|
+
**浏览器优先级**:如果有多个浏览器,可通过 `TWITTER_BROWSER` 指定优先尝试的浏览器:
|
|
539
|
+
|
|
540
|
+
```bash
|
|
541
|
+
TWITTER_BROWSER=chrome twitter feed # 支持: arc, chrome, edge, firefox, brave
|
|
542
|
+
```
|
|
543
|
+
|
|
526
544
|
### 代理支持
|
|
527
545
|
|
|
528
546
|
设置 `TWITTER_PROXY` 环境变量即可:
|
|
@@ -573,6 +591,12 @@ score = likes_w * likes
|
|
|
573
591
|
- 或在弹出 Keychain 授权时点击 **"始终允许"**。
|
|
574
592
|
- 报错 `Twitter API error 404`:通常是 queryId 轮换,重试即可。
|
|
575
593
|
|
|
594
|
+
- **Windows 下 pipe/subprocess 无法捕获输出**(AI agent 集成场景)
|
|
595
|
+
- 这是 **ConPTY** 伪终端的问题,不是 twitter-cli 的 bug。Windows Terminal 的 ConPTY 可能拦截有网络延迟的命令的管道输出。
|
|
596
|
+
- **解决方案**:使用 **Git Bash** 并在终端设置中设置 `"windowsEnableConpty": false`。
|
|
597
|
+
- ConPTY 关闭后如用 PowerShell,emoji 可能会报 `UnicodeEncodeError: 'gbk'`。Git Bash 原生支持 UTF-8。
|
|
598
|
+
- 标准 `subprocess.run(capture_output=True)` 和文件重定向 (`> file 2>&1`) 不受此问题影响。
|
|
599
|
+
|
|
576
600
|
|
|
577
601
|
|
|
578
602
|
- 结构化错误码通常会区分 `not_authenticated`、`not_found`、`invalid_input`、`rate_limited`、`api_error`。
|
|
@@ -182,6 +182,12 @@ Browser extraction is recommended — it forwards ALL Twitter cookies (not just
|
|
|
182
182
|
TWITTER_CHROME_PROFILE="Profile 2" twitter feed
|
|
183
183
|
```
|
|
184
184
|
|
|
185
|
+
**Browser priority:** If you have multiple browsers, set `TWITTER_BROWSER` to try a specific browser first:
|
|
186
|
+
|
|
187
|
+
```bash
|
|
188
|
+
TWITTER_BROWSER=chrome twitter feed # Supported: arc, chrome, edge, firefox, brave
|
|
189
|
+
```
|
|
190
|
+
|
|
185
191
|
After loading cookies, the CLI performs lightweight verification. Commands that require account access fail fast on clear auth errors (`401/403`).
|
|
186
192
|
|
|
187
193
|
### Proxy Support
|
|
@@ -292,6 +298,12 @@ Mode behavior:
|
|
|
292
298
|
- `Invalid tweet JSON file`
|
|
293
299
|
- Regenerate input using `twitter feed --json > tweets.json`.
|
|
294
300
|
|
|
301
|
+
- **Windows: no output captured by pipe/subprocess** (AI agent integration)
|
|
302
|
+
- This is a **ConPTY** issue, not a twitter-cli bug. Windows Terminal's ConPTY pseudo-terminal can intercept pipe output from commands with network latency.
|
|
303
|
+
- **Fix**: Use **Git Bash** as your terminal shell and set `"windowsEnableConpty": false` in your terminal settings.
|
|
304
|
+
- If disabling ConPTY with PowerShell, emoji output may fail with `UnicodeEncodeError: 'gbk'`. Git Bash handles UTF-8 natively.
|
|
305
|
+
- Standard `subprocess.run(capture_output=True)` and file redirection (`> file 2>&1`) work correctly regardless of ConPTY.
|
|
306
|
+
|
|
295
307
|
|
|
296
308
|
|
|
297
309
|
Structured error codes commonly include `not_authenticated`, `not_found`, `invalid_input`, `rate_limited`, and `api_error`.
|
|
@@ -491,6 +503,12 @@ twitter follow elonmusk --json
|
|
|
491
503
|
TWITTER_CHROME_PROFILE="Profile 2" twitter feed
|
|
492
504
|
```
|
|
493
505
|
|
|
506
|
+
**浏览器优先级**:如果有多个浏览器,可通过 `TWITTER_BROWSER` 指定优先尝试的浏览器:
|
|
507
|
+
|
|
508
|
+
```bash
|
|
509
|
+
TWITTER_BROWSER=chrome twitter feed # 支持: arc, chrome, edge, firefox, brave
|
|
510
|
+
```
|
|
511
|
+
|
|
494
512
|
### 代理支持
|
|
495
513
|
|
|
496
514
|
设置 `TWITTER_PROXY` 环境变量即可:
|
|
@@ -541,6 +559,12 @@ score = likes_w * likes
|
|
|
541
559
|
- 或在弹出 Keychain 授权时点击 **"始终允许"**。
|
|
542
560
|
- 报错 `Twitter API error 404`:通常是 queryId 轮换,重试即可。
|
|
543
561
|
|
|
562
|
+
- **Windows 下 pipe/subprocess 无法捕获输出**(AI agent 集成场景)
|
|
563
|
+
- 这是 **ConPTY** 伪终端的问题,不是 twitter-cli 的 bug。Windows Terminal 的 ConPTY 可能拦截有网络延迟的命令的管道输出。
|
|
564
|
+
- **解决方案**:使用 **Git Bash** 并在终端设置中设置 `"windowsEnableConpty": false`。
|
|
565
|
+
- ConPTY 关闭后如用 PowerShell,emoji 可能会报 `UnicodeEncodeError: 'gbk'`。Git Bash 原生支持 UTF-8。
|
|
566
|
+
- 标准 `subprocess.run(capture_output=True)` 和文件重定向 (`> file 2>&1`) 不受此问题影响。
|
|
567
|
+
|
|
544
568
|
|
|
545
569
|
|
|
546
570
|
- 结构化错误码通常会区分 `not_authenticated`、`not_found`、`invalid_input`、`rate_limited`、`api_error`。
|
|
@@ -49,6 +49,7 @@ If `AUTH_NEEDED`, proceed to guide the user:
|
|
|
49
49
|
|
|
50
50
|
Ensure user is logged into x.com in one of: Arc, Chrome, Edge, Firefox, Brave. twitter-cli auto-extracts cookies.
|
|
51
51
|
All Chrome profiles are scanned automatically. To specify a profile: `TWITTER_CHROME_PROFILE="Profile 2" twitter feed`.
|
|
52
|
+
To prioritize a specific browser: `TWITTER_BROWSER=chrome twitter feed` (supported: arc, chrome, edge, firefox, brave).
|
|
52
53
|
|
|
53
54
|
```bash
|
|
54
55
|
twitter whoami
|
|
@@ -135,7 +135,7 @@ def test_extract_via_subprocess_script_includes_arc(monkeypatch) -> None:
|
|
|
135
135
|
cookies, diagnostics = auth._extract_via_subprocess()
|
|
136
136
|
|
|
137
137
|
assert cookies is None
|
|
138
|
-
assert '
|
|
138
|
+
assert '"arc": browser_cookie3.arc' in seen["script"]
|
|
139
139
|
|
|
140
140
|
|
|
141
141
|
def test_extract_via_subprocess_retries_uv_when_current_env_has_no_output(monkeypatch) -> None:
|
|
@@ -349,6 +349,22 @@ def test_diagnose_keychain_issues_ssh_hint(monkeypatch) -> None:
|
|
|
349
349
|
assert "security unlock-keychain" in hint
|
|
350
350
|
|
|
351
351
|
|
|
352
|
+
def test_diagnose_keychain_issues_windows_hint(monkeypatch) -> None:
|
|
353
|
+
"""On Windows, hint should mention DPAPI and environment variable workaround."""
|
|
354
|
+
monkeypatch.setattr("sys.platform", "win32")
|
|
355
|
+
monkeypatch.delenv("SSH_CLIENT", raising=False)
|
|
356
|
+
monkeypatch.delenv("SSH_TTY", raising=False)
|
|
357
|
+
monkeypatch.delenv("SSH_CONNECTION", raising=False)
|
|
358
|
+
|
|
359
|
+
diagnostics = ["chrome: Unable to get key for cookie decryption"]
|
|
360
|
+
hint = auth._diagnose_keychain_issues(diagnostics)
|
|
361
|
+
|
|
362
|
+
assert hint is not None
|
|
363
|
+
assert "DPAPI" in hint
|
|
364
|
+
assert "TWITTER_AUTH_TOKEN" in hint
|
|
365
|
+
assert "shadowcopy" in hint
|
|
366
|
+
|
|
367
|
+
|
|
352
368
|
def test_diagnose_keychain_issues_returns_none_for_unrelated_errors() -> None:
|
|
353
369
|
"""Should return None when diagnostics don't mention Keychain."""
|
|
354
370
|
diagnostics = ["chrome[Default]=no-cookies", "firefox: profile not found"]
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
"""Tests for twitter_cli.cache module."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import json
|
|
6
|
+
import time
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
from twitter_cli.cache import resolve_cached_tweet, save_tweet_cache
|
|
10
|
+
from twitter_cli.models import Author, Metrics, Tweet
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def _make_tweet(tweet_id: str, text: str = "hello") -> Tweet:
|
|
14
|
+
return Tweet(
|
|
15
|
+
id=tweet_id,
|
|
16
|
+
text=text,
|
|
17
|
+
author=Author(id="u1", name="Alice", screen_name="alice"),
|
|
18
|
+
metrics=Metrics(likes=1),
|
|
19
|
+
created_at="2025-01-01",
|
|
20
|
+
)
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
class TestSaveAndResolve:
|
|
24
|
+
"""save_tweet_cache → resolve_cached_tweet round-trip."""
|
|
25
|
+
|
|
26
|
+
def test_round_trip(self, tmp_path, monkeypatch) -> None:
|
|
27
|
+
cache_file = tmp_path / "last_results.json"
|
|
28
|
+
monkeypatch.setattr("twitter_cli.cache._CACHE_FILE", cache_file)
|
|
29
|
+
monkeypatch.setattr("twitter_cli.cache._CACHE_DIR", tmp_path)
|
|
30
|
+
|
|
31
|
+
tweets = [_make_tweet("100"), _make_tweet("200"), _make_tweet("300")]
|
|
32
|
+
save_tweet_cache(tweets)
|
|
33
|
+
|
|
34
|
+
assert cache_file.exists()
|
|
35
|
+
tweet_id, size = resolve_cached_tweet(1)
|
|
36
|
+
assert tweet_id == "100"
|
|
37
|
+
assert size == 3
|
|
38
|
+
|
|
39
|
+
tweet_id, size = resolve_cached_tweet(3)
|
|
40
|
+
assert tweet_id == "300"
|
|
41
|
+
assert size == 3
|
|
42
|
+
|
|
43
|
+
def test_out_of_range_returns_none(self, tmp_path, monkeypatch) -> None:
|
|
44
|
+
cache_file = tmp_path / "last_results.json"
|
|
45
|
+
monkeypatch.setattr("twitter_cli.cache._CACHE_FILE", cache_file)
|
|
46
|
+
monkeypatch.setattr("twitter_cli.cache._CACHE_DIR", tmp_path)
|
|
47
|
+
|
|
48
|
+
save_tweet_cache([_make_tweet("100")])
|
|
49
|
+
|
|
50
|
+
tweet_id, size = resolve_cached_tweet(99)
|
|
51
|
+
assert tweet_id is None
|
|
52
|
+
assert size == 1
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
class TestCacheExpiry:
|
|
56
|
+
"""TTL expiration behavior."""
|
|
57
|
+
|
|
58
|
+
def test_expired_cache_returns_none(self, tmp_path, monkeypatch) -> None:
|
|
59
|
+
cache_file = tmp_path / "last_results.json"
|
|
60
|
+
monkeypatch.setattr("twitter_cli.cache._CACHE_FILE", cache_file)
|
|
61
|
+
|
|
62
|
+
# Write cache with old timestamp
|
|
63
|
+
payload = {
|
|
64
|
+
"created_at": time.time() - 7200, # 2 hours ago
|
|
65
|
+
"tweets": [{"index": 1, "id": "100", "author": "alice", "text": "hi"}],
|
|
66
|
+
}
|
|
67
|
+
cache_file.write_text(json.dumps(payload), encoding="utf-8")
|
|
68
|
+
|
|
69
|
+
tweet_id, size = resolve_cached_tweet(1)
|
|
70
|
+
assert tweet_id is None
|
|
71
|
+
assert size == 0
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
class TestCacheEdgeCases:
|
|
75
|
+
"""Corrupted and missing cache files."""
|
|
76
|
+
|
|
77
|
+
def test_missing_file(self, tmp_path, monkeypatch) -> None:
|
|
78
|
+
cache_file = tmp_path / "does_not_exist.json"
|
|
79
|
+
monkeypatch.setattr("twitter_cli.cache._CACHE_FILE", cache_file)
|
|
80
|
+
|
|
81
|
+
tweet_id, size = resolve_cached_tweet(1)
|
|
82
|
+
assert tweet_id is None
|
|
83
|
+
assert size == 0
|
|
84
|
+
|
|
85
|
+
def test_corrupted_json(self, tmp_path, monkeypatch) -> None:
|
|
86
|
+
cache_file = tmp_path / "last_results.json"
|
|
87
|
+
cache_file.write_text("{{invalid json", encoding="utf-8")
|
|
88
|
+
monkeypatch.setattr("twitter_cli.cache._CACHE_FILE", cache_file)
|
|
89
|
+
|
|
90
|
+
tweet_id, size = resolve_cached_tweet(1)
|
|
91
|
+
assert tweet_id is None
|
|
92
|
+
assert size == 0
|
|
93
|
+
|
|
94
|
+
def test_wrong_structure(self, tmp_path, monkeypatch) -> None:
|
|
95
|
+
cache_file = tmp_path / "last_results.json"
|
|
96
|
+
cache_file.write_text('"just a string"', encoding="utf-8")
|
|
97
|
+
monkeypatch.setattr("twitter_cli.cache._CACHE_FILE", cache_file)
|
|
98
|
+
|
|
99
|
+
tweet_id, size = resolve_cached_tweet(1)
|
|
100
|
+
assert tweet_id is None
|
|
101
|
+
assert size == 0
|
|
@@ -10,7 +10,7 @@ import yaml
|
|
|
10
10
|
|
|
11
11
|
from twitter_cli.cli import cli
|
|
12
12
|
from twitter_cli.formatter import article_to_markdown, print_tweet_table
|
|
13
|
-
from twitter_cli.models import Author, Metrics, Tweet, UserProfile
|
|
13
|
+
from twitter_cli.models import Author, BookmarkFolder, Metrics, Tweet, UserProfile
|
|
14
14
|
from twitter_cli.serialization import tweets_to_json
|
|
15
15
|
|
|
16
16
|
|
|
@@ -19,22 +19,12 @@ def test_cli_user_command_works_with_client_factory(monkeypatch) -> None:
|
|
|
19
19
|
def fetch_user(self, screen_name: str) -> UserProfile:
|
|
20
20
|
return UserProfile(id="1", name="Alice", screen_name=screen_name)
|
|
21
21
|
|
|
22
|
-
monkeypatch.setattr("twitter_cli.cli._get_client", lambda config=None: FakeClient())
|
|
22
|
+
monkeypatch.setattr("twitter_cli.cli._get_client", lambda config=None, quiet=False: FakeClient())
|
|
23
23
|
runner = CliRunner()
|
|
24
24
|
result = runner.invoke(cli, ["user", "alice"])
|
|
25
25
|
assert result.exit_code == 0
|
|
26
26
|
|
|
27
27
|
|
|
28
|
-
def test_get_client_for_output_does_not_swallow_real_type_error(monkeypatch) -> None:
|
|
29
|
-
def _broken_get_client(config=None, quiet=False):
|
|
30
|
-
raise TypeError("real bug")
|
|
31
|
-
|
|
32
|
-
monkeypatch.setattr("twitter_cli.cli._get_client", _broken_get_client)
|
|
33
|
-
|
|
34
|
-
with pytest.raises(TypeError, match="real bug"):
|
|
35
|
-
from twitter_cli.cli import _get_client_for_output
|
|
36
|
-
|
|
37
|
-
_get_client_for_output({})
|
|
38
28
|
|
|
39
29
|
|
|
40
30
|
def test_cli_feed_json_input_path(tmp_path, tweet_factory) -> None:
|
|
@@ -102,7 +92,7 @@ def test_print_tweet_table_full_text_shows_untruncated_text(tweet_factory) -> No
|
|
|
102
92
|
def test_cli_commands_wrap_client_creation_errors(monkeypatch, args) -> None:
|
|
103
93
|
monkeypatch.setattr(
|
|
104
94
|
"twitter_cli.cli._get_client",
|
|
105
|
-
lambda config=None: (_ for _ in ()).throw(RuntimeError("boom")),
|
|
95
|
+
lambda config=None, quiet=False: (_ for _ in ()).throw(RuntimeError("boom")),
|
|
106
96
|
)
|
|
107
97
|
runner = CliRunner()
|
|
108
98
|
|
|
@@ -114,10 +104,11 @@ def test_cli_commands_wrap_client_creation_errors(monkeypatch, args) -> None:
|
|
|
114
104
|
|
|
115
105
|
|
|
116
106
|
def test_cli_user_error_yaml(monkeypatch) -> None:
|
|
107
|
+
from twitter_cli.exceptions import NotFoundError
|
|
117
108
|
monkeypatch.setenv("OUTPUT", "auto")
|
|
118
109
|
monkeypatch.setattr(
|
|
119
110
|
"twitter_cli.cli._get_client",
|
|
120
|
-
lambda config=None: (_ for _ in ()).throw(
|
|
111
|
+
lambda config=None, quiet=False: (_ for _ in ()).throw(NotFoundError("User not found")),
|
|
121
112
|
)
|
|
122
113
|
runner = CliRunner()
|
|
123
114
|
|
|
@@ -136,7 +127,7 @@ def test_cli_tweet_accepts_shared_url_with_query(monkeypatch) -> None:
|
|
|
136
127
|
assert max_count == 50
|
|
137
128
|
return []
|
|
138
129
|
|
|
139
|
-
monkeypatch.setattr("twitter_cli.cli._get_client", lambda config=None: FakeClient())
|
|
130
|
+
monkeypatch.setattr("twitter_cli.cli._get_client", lambda config=None, quiet=False: FakeClient())
|
|
140
131
|
monkeypatch.setattr(
|
|
141
132
|
"twitter_cli.cli.load_config",
|
|
142
133
|
lambda: {"fetch": {"count": 50}, "filter": {}, "rateLimit": {}},
|
|
@@ -162,7 +153,7 @@ def test_cli_article_accepts_article_url_and_json(monkeypatch) -> None:
|
|
|
162
153
|
article_text="Hello\n\n## Section",
|
|
163
154
|
)
|
|
164
155
|
|
|
165
|
-
monkeypatch.setattr("twitter_cli.cli._get_client", lambda config=None: FakeClient())
|
|
156
|
+
monkeypatch.setattr("twitter_cli.cli._get_client", lambda config=None, quiet=False: FakeClient())
|
|
166
157
|
monkeypatch.setattr(
|
|
167
158
|
"twitter_cli.cli.load_config",
|
|
168
159
|
lambda: {"fetch": {"count": 50}, "filter": {}, "rateLimit": {}},
|
|
@@ -195,7 +186,7 @@ def test_cli_article_markdown_output_and_save(monkeypatch, tmp_path) -> None:
|
|
|
195
186
|
assert tweet_id == "12345"
|
|
196
187
|
return article
|
|
197
188
|
|
|
198
|
-
monkeypatch.setattr("twitter_cli.cli._get_client", lambda config=None: FakeClient())
|
|
189
|
+
monkeypatch.setattr("twitter_cli.cli._get_client", lambda config=None, quiet=False: FakeClient())
|
|
199
190
|
monkeypatch.setattr("twitter_cli.cli.load_config", lambda: {})
|
|
200
191
|
output_path = tmp_path / "article.md"
|
|
201
192
|
runner = CliRunner()
|
|
@@ -227,7 +218,7 @@ def test_cli_article_markdown_overrides_auto_structured_output(monkeypatch) -> N
|
|
|
227
218
|
return article
|
|
228
219
|
|
|
229
220
|
monkeypatch.setenv("OUTPUT", "auto")
|
|
230
|
-
monkeypatch.setattr("twitter_cli.cli._get_client", lambda config=None: FakeClient())
|
|
221
|
+
monkeypatch.setattr("twitter_cli.cli._get_client", lambda config=None, quiet=False: FakeClient())
|
|
231
222
|
monkeypatch.setattr("twitter_cli.cli.load_config", lambda: {})
|
|
232
223
|
runner = CliRunner()
|
|
233
224
|
|
|
@@ -254,7 +245,7 @@ def test_cli_bookmark_alias_works(monkeypatch) -> None:
|
|
|
254
245
|
calls.append(tweet_id)
|
|
255
246
|
return True
|
|
256
247
|
|
|
257
|
-
monkeypatch.setattr("twitter_cli.cli._get_client", lambda config=None: FakeClient())
|
|
248
|
+
monkeypatch.setattr("twitter_cli.cli._get_client", lambda config=None, quiet=False: FakeClient())
|
|
258
249
|
runner = CliRunner()
|
|
259
250
|
|
|
260
251
|
result = runner.invoke(cli, ["bookmark", "123"])
|
|
@@ -263,6 +254,93 @@ def test_cli_bookmark_alias_works(monkeypatch) -> None:
|
|
|
263
254
|
assert calls == ["123"]
|
|
264
255
|
|
|
265
256
|
|
|
257
|
+
def test_cli_bookmarks_folders_inherits_parent_options(monkeypatch) -> None:
|
|
258
|
+
calls = []
|
|
259
|
+
|
|
260
|
+
def fake_folder_timeline(
|
|
261
|
+
folder_id: str,
|
|
262
|
+
max_count: int | None,
|
|
263
|
+
since: str | None,
|
|
264
|
+
as_json: bool,
|
|
265
|
+
as_yaml: bool,
|
|
266
|
+
output_file: str | None,
|
|
267
|
+
do_filter: bool,
|
|
268
|
+
compact: bool,
|
|
269
|
+
full_text: bool,
|
|
270
|
+
) -> None:
|
|
271
|
+
calls.append(
|
|
272
|
+
(
|
|
273
|
+
folder_id,
|
|
274
|
+
max_count,
|
|
275
|
+
since,
|
|
276
|
+
as_json,
|
|
277
|
+
as_yaml,
|
|
278
|
+
output_file,
|
|
279
|
+
do_filter,
|
|
280
|
+
compact,
|
|
281
|
+
full_text,
|
|
282
|
+
)
|
|
283
|
+
)
|
|
284
|
+
|
|
285
|
+
monkeypatch.setattr("twitter_cli.cli._run_bookmark_folder_timeline", fake_folder_timeline)
|
|
286
|
+
runner = CliRunner()
|
|
287
|
+
|
|
288
|
+
result = runner.invoke(
|
|
289
|
+
cli,
|
|
290
|
+
["bookmarks", "--json", "--full-text", "-n", "7", "-o", "root.json", "--filter", "folders", "123"],
|
|
291
|
+
)
|
|
292
|
+
|
|
293
|
+
assert result.exit_code == 0
|
|
294
|
+
assert calls == [("123", 7, None, True, False, "root.json", True, False, True)]
|
|
295
|
+
|
|
296
|
+
|
|
297
|
+
def test_cli_bookmarks_folders_list_inherits_parent_output_options(monkeypatch) -> None:
|
|
298
|
+
calls = []
|
|
299
|
+
|
|
300
|
+
def fake_list_bookmark_folders(
|
|
301
|
+
as_json: bool,
|
|
302
|
+
as_yaml: bool,
|
|
303
|
+
compact: bool,
|
|
304
|
+
output_file: str | None,
|
|
305
|
+
) -> None:
|
|
306
|
+
calls.append((as_json, as_yaml, compact, output_file))
|
|
307
|
+
|
|
308
|
+
monkeypatch.setattr("twitter_cli.cli._run_list_bookmark_folders", fake_list_bookmark_folders)
|
|
309
|
+
runner = CliRunner()
|
|
310
|
+
|
|
311
|
+
result = runner.invoke(cli, ["bookmarks", "--json", "-o", "folders.json", "folders"])
|
|
312
|
+
|
|
313
|
+
assert result.exit_code == 0
|
|
314
|
+
assert calls == [(True, False, False, "folders.json")]
|
|
315
|
+
|
|
316
|
+
|
|
317
|
+
def test_cli_bookmarks_folders_list_writes_output_file(monkeypatch, tmp_path) -> None:
|
|
318
|
+
class FakeClient:
|
|
319
|
+
def fetch_bookmark_folders(self) -> list[BookmarkFolder]:
|
|
320
|
+
return [BookmarkFolder(id="f1", name="Reading"), BookmarkFolder(id="f2", name="Research")]
|
|
321
|
+
|
|
322
|
+
monkeypatch.setattr("twitter_cli.cli._get_client", lambda config=None, quiet=False: FakeClient())
|
|
323
|
+
monkeypatch.setattr("twitter_cli.cli.load_config", lambda: {})
|
|
324
|
+
output_path = tmp_path / "folders.json"
|
|
325
|
+
runner = CliRunner()
|
|
326
|
+
|
|
327
|
+
result = runner.invoke(
|
|
328
|
+
cli,
|
|
329
|
+
["bookmarks", "folders", "--json", "--output", str(output_path)],
|
|
330
|
+
)
|
|
331
|
+
|
|
332
|
+
assert result.exit_code == 0
|
|
333
|
+
payload = json.loads(result.output)
|
|
334
|
+
assert payload["ok"] is True
|
|
335
|
+
assert payload["data"][0]["id"] == "f1"
|
|
336
|
+
|
|
337
|
+
saved = json.loads(output_path.read_text(encoding="utf-8"))
|
|
338
|
+
assert saved == [
|
|
339
|
+
{"id": "f1", "name": "Reading"},
|
|
340
|
+
{"id": "f2", "name": "Research"},
|
|
341
|
+
]
|
|
342
|
+
|
|
343
|
+
|
|
266
344
|
def test_cli_whoami_command(monkeypatch) -> None:
|
|
267
345
|
from twitter_cli.models import UserProfile
|
|
268
346
|
|
|
@@ -270,7 +348,7 @@ def test_cli_whoami_command(monkeypatch) -> None:
|
|
|
270
348
|
def fetch_me(self) -> UserProfile:
|
|
271
349
|
return UserProfile(id="42", name="Test User", screen_name="testuser")
|
|
272
350
|
|
|
273
|
-
monkeypatch.setattr("twitter_cli.cli._get_client", lambda config=None: FakeClient())
|
|
351
|
+
monkeypatch.setattr("twitter_cli.cli._get_client", lambda config=None, quiet=False: FakeClient())
|
|
274
352
|
runner = CliRunner()
|
|
275
353
|
|
|
276
354
|
result = runner.invoke(cli, ["whoami"])
|
|
@@ -289,7 +367,7 @@ def test_cli_whoami_auto_yaml(monkeypatch) -> None:
|
|
|
289
367
|
return UserProfile(id="42", name="Test User", screen_name="testuser")
|
|
290
368
|
|
|
291
369
|
monkeypatch.setenv("OUTPUT", "auto")
|
|
292
|
-
monkeypatch.setattr("twitter_cli.cli._get_client", lambda config=None: FakeClient())
|
|
370
|
+
monkeypatch.setattr("twitter_cli.cli._get_client", lambda config=None, quiet=False: FakeClient())
|
|
293
371
|
runner = CliRunner()
|
|
294
372
|
|
|
295
373
|
result = runner.invoke(cli, ["whoami"])
|
|
@@ -307,7 +385,7 @@ def test_cli_status_auto_yaml(monkeypatch) -> None:
|
|
|
307
385
|
return UserProfile(id="42", name="Test User", screen_name="testuser")
|
|
308
386
|
|
|
309
387
|
monkeypatch.setenv("OUTPUT", "auto")
|
|
310
|
-
monkeypatch.setattr("twitter_cli.cli._get_client", lambda config=None: FakeClient())
|
|
388
|
+
monkeypatch.setattr("twitter_cli.cli._get_client", lambda config=None, quiet=False: FakeClient())
|
|
311
389
|
runner = CliRunner()
|
|
312
390
|
|
|
313
391
|
result = runner.invoke(cli, ["status"])
|
|
@@ -328,7 +406,7 @@ def test_cli_reply_command(monkeypatch) -> None:
|
|
|
328
406
|
calls.append({"text": text, "reply_to_id": reply_to_id})
|
|
329
407
|
return "999"
|
|
330
408
|
|
|
331
|
-
monkeypatch.setattr("twitter_cli.cli._get_client", lambda config=None: FakeClient())
|
|
409
|
+
monkeypatch.setattr("twitter_cli.cli._get_client", lambda config=None, quiet=False: FakeClient())
|
|
332
410
|
runner = CliRunner()
|
|
333
411
|
|
|
334
412
|
result = runner.invoke(cli, ["reply", "12345", "Nice tweet!"])
|
|
@@ -345,7 +423,7 @@ def test_cli_quote_command(monkeypatch) -> None:
|
|
|
345
423
|
calls.append({"tweet_id": tweet_id, "text": text})
|
|
346
424
|
return "888"
|
|
347
425
|
|
|
348
|
-
monkeypatch.setattr("twitter_cli.cli._get_client", lambda config=None: FakeClient())
|
|
426
|
+
monkeypatch.setattr("twitter_cli.cli._get_client", lambda config=None, quiet=False: FakeClient())
|
|
349
427
|
runner = CliRunner()
|
|
350
428
|
|
|
351
429
|
result = runner.invoke(cli, ["quote", "12345", "Interesting!"])
|
|
@@ -361,7 +439,7 @@ def test_cli_post_json_output(monkeypatch) -> None:
|
|
|
361
439
|
assert reply_to_id is None
|
|
362
440
|
return "999"
|
|
363
441
|
|
|
364
|
-
monkeypatch.setattr("twitter_cli.cli._get_client", lambda config=None: FakeClient())
|
|
442
|
+
monkeypatch.setattr("twitter_cli.cli._get_client", lambda config=None, quiet=False: FakeClient())
|
|
365
443
|
runner = CliRunner()
|
|
366
444
|
|
|
367
445
|
result = runner.invoke(cli, ["post", "hello", "--json"])
|
|
@@ -378,7 +456,7 @@ def test_cli_like_yaml_output(monkeypatch) -> None:
|
|
|
378
456
|
assert tweet_id == "123"
|
|
379
457
|
return True
|
|
380
458
|
|
|
381
|
-
monkeypatch.setattr("twitter_cli.cli._get_client", lambda config=None: FakeClient())
|
|
459
|
+
monkeypatch.setattr("twitter_cli.cli._get_client", lambda config=None, quiet=False: FakeClient())
|
|
382
460
|
runner = CliRunner()
|
|
383
461
|
|
|
384
462
|
result = runner.invoke(cli, ["like", "123", "--yaml"])
|
|
@@ -399,7 +477,7 @@ def test_cli_follow_json_output(monkeypatch) -> None:
|
|
|
399
477
|
assert user_id == "42"
|
|
400
478
|
return True
|
|
401
479
|
|
|
402
|
-
monkeypatch.setattr("twitter_cli.cli._get_client", lambda config=None: FakeClient())
|
|
480
|
+
monkeypatch.setattr("twitter_cli.cli._get_client", lambda config=None, quiet=False: FakeClient())
|
|
403
481
|
runner = CliRunner()
|
|
404
482
|
|
|
405
483
|
result = runner.invoke(cli, ["follow", "alice", "--json"])
|
|
@@ -421,7 +499,7 @@ def test_cli_follow_command(monkeypatch) -> None:
|
|
|
421
499
|
actions.append(("follow", user_id))
|
|
422
500
|
return True
|
|
423
501
|
|
|
424
|
-
monkeypatch.setattr("twitter_cli.cli._get_client", lambda config=None: FakeClient())
|
|
502
|
+
monkeypatch.setattr("twitter_cli.cli._get_client", lambda config=None, quiet=False: FakeClient())
|
|
425
503
|
runner = CliRunner()
|
|
426
504
|
|
|
427
505
|
result = runner.invoke(cli, ["follow", "alice"])
|
|
@@ -440,7 +518,7 @@ def test_cli_unfollow_command(monkeypatch) -> None:
|
|
|
440
518
|
actions.append(("unfollow", user_id))
|
|
441
519
|
return True
|
|
442
520
|
|
|
443
|
-
monkeypatch.setattr("twitter_cli.cli._get_client", lambda config=None: FakeClient())
|
|
521
|
+
monkeypatch.setattr("twitter_cli.cli._get_client", lambda config=None, quiet=False: FakeClient())
|
|
444
522
|
runner = CliRunner()
|
|
445
523
|
|
|
446
524
|
result = runner.invoke(cli, ["unfollow", "alice"])
|
|
@@ -457,7 +535,7 @@ def test_cli_search_advanced_options(monkeypatch) -> None:
|
|
|
457
535
|
captured["product"] = product
|
|
458
536
|
return []
|
|
459
537
|
|
|
460
|
-
monkeypatch.setattr("twitter_cli.cli._get_client", lambda config=None: FakeClient())
|
|
538
|
+
monkeypatch.setattr("twitter_cli.cli._get_client", lambda config=None, quiet=False: FakeClient())
|
|
461
539
|
monkeypatch.setattr(
|
|
462
540
|
"twitter_cli.cli.load_config",
|
|
463
541
|
lambda: {"fetch": {"count": 50}, "filter": {}, "rateLimit": {}},
|
|
@@ -492,7 +570,7 @@ def test_cli_search_operators_only_no_query(monkeypatch) -> None:
|
|
|
492
570
|
captured["query"] = query
|
|
493
571
|
return []
|
|
494
572
|
|
|
495
|
-
monkeypatch.setattr("twitter_cli.cli._get_client", lambda config=None: FakeClient())
|
|
573
|
+
monkeypatch.setattr("twitter_cli.cli._get_client", lambda config=None, quiet=False: FakeClient())
|
|
496
574
|
monkeypatch.setattr(
|
|
497
575
|
"twitter_cli.cli.load_config",
|
|
498
576
|
lambda: {"fetch": {"count": 50}, "filter": {}, "rateLimit": {}},
|
|
@@ -513,7 +591,7 @@ def test_cli_search_empty_query_no_options() -> None:
|
|
|
513
591
|
|
|
514
592
|
|
|
515
593
|
def test_cli_search_invalid_date_rejected(monkeypatch) -> None:
|
|
516
|
-
monkeypatch.setattr("twitter_cli.cli._get_client", lambda config=None: None)
|
|
594
|
+
monkeypatch.setattr("twitter_cli.cli._get_client", lambda config=None, quiet=False: None)
|
|
517
595
|
runner = CliRunner()
|
|
518
596
|
|
|
519
597
|
result = runner.invoke(cli, ["search", "python", "--since", "not-a-date"])
|
|
@@ -522,7 +600,7 @@ def test_cli_search_invalid_date_rejected(monkeypatch) -> None:
|
|
|
522
600
|
|
|
523
601
|
|
|
524
602
|
def test_cli_search_rejects_reversed_date_range(monkeypatch) -> None:
|
|
525
|
-
monkeypatch.setattr("twitter_cli.cli._get_client", lambda config=None: None)
|
|
603
|
+
monkeypatch.setattr("twitter_cli.cli._get_client", lambda config=None, quiet=False: None)
|
|
526
604
|
runner = CliRunner()
|
|
527
605
|
|
|
528
606
|
result = runner.invoke(cli, ["search", "python", "--since", "2026-03-02", "--until", "2026-03-01"])
|