srz 0.2.0

Fast, modular CLI to summarize webpages, documents, books, and live news using local LLMs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
use clap::Parser;
use srz::cli::{
    clean_news_query, is_news_query, CacheCommand, Cli, NewsTimeframe, ResolvedAction, SummaryStyle,
};

#[test]
fn test_cache_subcommands() {
    let dir_cli = Cli::parse_from(["srz", "cache", "dir"]);
    assert_eq!(
        dir_cli.resolve_action().unwrap(),
        ResolvedAction::Cache(CacheCommand::Dir)
    );

    let size_cli = Cli::parse_from(["srz", "cache", "size"]);
    assert_eq!(
        size_cli.resolve_action().unwrap(),
        ResolvedAction::Cache(CacheCommand::Size)
    );

    let clean_cli = Cli::parse_from(["srz", "cache", "clean"]);
    assert_eq!(
        clean_cli.resolve_action().unwrap(),
        ResolvedAction::Cache(CacheCommand::Clean)
    );
}

#[test]
fn test_history_stats_find_subcommands() {
    let hist_cli = Cli::parse_from(["srz", "history", "--limit", "20"]);
    assert_eq!(
        hist_cli.resolve_action().unwrap(),
        ResolvedAction::History { limit: 20 }
    );

    let stats_cli = Cli::parse_from(["srz", "stats"]);
    assert_eq!(stats_cli.resolve_action().unwrap(), ResolvedAction::Stats);

    let find_cli = Cli::parse_from(["srz", "find", "Melange"]);
    assert_eq!(
        find_cli.resolve_action().unwrap(),
        ResolvedAction::Find { query: "Melange" }
    );

    let models_cli = Cli::parse_from(["srz", "models"]);
    assert_eq!(models_cli.resolve_action().unwrap(), ResolvedAction::Models);

    let doctor_cli = Cli::parse_from(["srz", "doctor"]);
    assert_eq!(doctor_cli.resolve_action().unwrap(), ResolvedAction::Doctor);

    let index_cli = Cli::parse_from(["srz", "index"]);
    assert_eq!(index_cli.resolve_action().unwrap(), ResolvedAction::Index);
}

#[test]
fn test_completions_subcommand() {
    let bash_cli = Cli::parse_from(["srz", "completions", "bash"]);
    assert_eq!(
        bash_cli.resolve_action().unwrap(),
        ResolvedAction::Completions {
            shell: clap_complete::Shell::Bash
        }
    );

    let zsh_cli = Cli::parse_from(["srz", "completions", "zsh"]);
    assert_eq!(
        zsh_cli.resolve_action().unwrap(),
        ResolvedAction::Completions {
            shell: clap_complete::Shell::Zsh
        }
    );
}

#[test]
fn test_default_style_is_concise() {
    let cli = Cli::parse_from(["srz", "Dune"]);
    assert_eq!(cli.style, SummaryStyle::Concise);
}

#[test]
fn test_new_summary_styles() {
    let tldr_cli = Cli::parse_from(["srz", "Dune", "--style", "tldr"]);
    assert_eq!(tldr_cli.style, SummaryStyle::Tldr);
    assert_eq!(tldr_cli.style.as_str(), "tldr");

    let critique_cli = Cli::parse_from(["srz", "Dune", "--style", "critique"]);
    assert_eq!(critique_cli.style, SummaryStyle::Critique);
    assert_eq!(critique_cli.style.as_str(), "critique");

    let eli5_cli = Cli::parse_from(["srz", "Dune", "--style", "eli5"]);
    assert_eq!(eli5_cli.style, SummaryStyle::Eli5);
    assert_eq!(eli5_cli.style.as_str(), "eli5");

    let flashcards_cli = Cli::parse_from(["srz", "Dune", "--style", "flashcards"]);
    assert_eq!(flashcards_cli.style, SummaryStyle::Flashcards);
    assert_eq!(flashcards_cli.style.as_str(), "flashcards");
}

#[test]
fn test_auto_detect_url() {
    let cli = Cli::parse_from(["srz", "https://example.com/article"]);

    match cli.resolve_action().unwrap() {
        ResolvedAction::Web { url } => assert_eq!(url, "https://example.com/article"),
        _ => panic!("Expected Web action"),
    }
}

#[test]
fn test_auto_detect_stdin_and_file() {
    let stdin_cli = Cli::parse_from(["srz", "-"]);
    assert_eq!(stdin_cli.resolve_action().unwrap(), ResolvedAction::Stdin);

    let stdin_subcmd = Cli::parse_from(["srz", "file", "-"]);
    assert_eq!(stdin_subcmd.resolve_action().unwrap(), ResolvedAction::Stdin);

    // Existing local file (Cargo.toml)
    let file_cli = Cli::parse_from(["srz", "Cargo.toml"]);
    match file_cli.resolve_action().unwrap() {
        ResolvedAction::File { path } => {
            assert_eq!(path, std::path::PathBuf::from("Cargo.toml"));
        }
        _ => panic!("Expected File action for Cargo.toml"),
    }

    let file_subcmd = Cli::parse_from(["srz", "file", "README.md"]);
    match file_subcmd.resolve_action().unwrap() {
        ResolvedAction::File { path } => {
            assert_eq!(path, std::path::PathBuf::from("README.md"));
        }
        _ => panic!("Expected File action for README.md"),
    }
}

#[test]
fn test_auto_detect_book_with_global_flags() {
    let cli = Cli::parse_from([
        "srz",
        "The Great Gatsby",
        "--author",
        "F. Scott Fitzgerald",
        "--style",
        "concise",
        "--no-cache",
    ]);

    assert!(cli.no_cache);
    assert_eq!(cli.style, SummaryStyle::Concise);

    match cli.resolve_action().unwrap() {
        ResolvedAction::Book {
            title,
            author,
            no_search,
        } => {
            assert_eq!(title, "The Great Gatsby");
            assert_eq!(author, Some("F. Scott Fitzgerald"));
            assert!(!no_search);
        }
        _ => panic!("Expected Book action"),
    }
}

#[test]
fn test_book_subcommand_flags() {
    let cli = Cli::parse_from([
        "srz",
        "book",
        "Dune",
        "--author",
        "Frank Herbert",
        "--no-search",
        "--max-tokens",
        "4096",
    ]);

    assert_eq!(cli.max_tokens, 4096);

    match cli.resolve_action().unwrap() {
        ResolvedAction::Book {
            title,
            author,
            no_search,
        } => {
            assert_eq!(title, "Dune");
            assert_eq!(author, Some("Frank Herbert"));
            assert!(no_search);
        }
        _ => panic!("Expected Book action"),
    }
}

#[test]
fn test_missing_target_returns_error() {
    let cli = Cli::parse_from(["srz"]);
    assert!(cli.resolve_action().is_err());
}

#[test]
fn test_quiet_and_verbose_flags() {
    let quiet_cli = Cli::parse_from(["srz", "Dune", "-q"]);
    assert!(quiet_cli.quiet);
    assert!(!quiet_cli.verbose);

    let verbose_cli = Cli::parse_from(["srz", "web", "https://example.com", "--verbose"]);
    assert!(!verbose_cli.quiet);
    assert!(verbose_cli.verbose);
}

#[test]
fn test_json_flag() {
    let json_cli = Cli::parse_from(["srz", "history", "--json"]);
    assert!(json_cli.json);

    let json_summary_cli = Cli::parse_from(["srz", "Dune", "--json"]);
    assert!(json_summary_cli.json);
}

#[test]
fn test_yes_flag() {
    let yes_cli = Cli::parse_from(["srz", "book", "The Hobbit", "-y"]);
    assert!(yes_cli.yes);

    let long_yes_cli = Cli::parse_from(["srz", "The Hobbit", "--yes"]);
    assert!(long_yes_cli.yes);

    let open_cli = Cli::parse_from(["srz", "The Hobbit", "-b"]);
    assert!(open_cli.open);

    let long_open_cli = Cli::parse_from(["srz", "The Hobbit", "--open"]);
    assert!(long_open_cli.open);

    let concurrency_cli = Cli::parse_from(["srz", "The Hobbit", "--concurrency", "4"]);
    assert_eq!(concurrency_cli.concurrency, 4);
}

#[test]
fn test_news_subcommand_resolution() {
    let default_news = Cli::parse_from(["srz", "news", "javascript"]);
    assert_eq!(
        default_news.resolve_action().unwrap(),
        ResolvedAction::News {
            topic: "javascript".to_string(),
            timeframe: NewsTimeframe::Day,
            limit: 4,
        }
    );

    let custom_news = Cli::parse_from([
        "srz", "news", "artificial intelligence", "-t", "week", "-l", "8",
    ]);
    assert_eq!(
        custom_news.resolve_action().unwrap(),
        ResolvedAction::News {
            topic: "artificial intelligence".to_string(),
            timeframe: NewsTimeframe::Week,
            limit: 8,
        }
    );
}

#[test]
fn test_news_natural_query_auto_detection() {
    let js_news = Cli::parse_from(["srz", "current javascript news"]);
    assert_eq!(
        js_news.resolve_action().unwrap(),
        ResolvedAction::News {
            topic: "javascript".to_string(),
            timeframe: NewsTimeframe::Day,
            limit: 4,
        }
    );

    let latest_rust = Cli::parse_from(["srz", "latest rust news"]);
    assert_eq!(
        latest_rust.resolve_action().unwrap(),
        ResolvedAction::News {
            topic: "rust".to_string(),
            timeframe: NewsTimeframe::Day,
            limit: 4,
        }
    );

    let news_about_tech = Cli::parse_from(["srz", "news about quantum computing"]);
    assert_eq!(
        news_about_tech.resolve_action().unwrap(),
        ResolvedAction::News {
            topic: "quantum computing".to_string(),
            timeframe: NewsTimeframe::Day,
            limit: 4,
        }
    );

    let general_news = Cli::parse_from(["srz", "current news"]);
    assert_eq!(
        general_news.resolve_action().unwrap(),
        ResolvedAction::News {
            topic: "top stories".to_string(),
            timeframe: NewsTimeframe::Day,
            limit: 4,
        }
    );
}

#[test]
fn test_news_query_helpers() {
    assert!(is_news_query("current javascript news"));
    assert!(is_news_query("latest AI news"));
    assert!(is_news_query("tech news"));
    assert!(is_news_query("news"));
    assert!(is_news_query("current headlines"));
    assert!(!is_news_query("The Great Gatsby"));
    assert!(!is_news_query("Designing Data-Intensive Applications"));

    assert_eq!(clean_news_query("current javascript news"), "javascript");
    assert_eq!(clean_news_query("latest rust news"), "rust");
    assert_eq!(clean_news_query("news about astrophysics"), "astrophysics");
    assert_eq!(clean_news_query("current news"), "top stories");
    assert_eq!(clean_news_query("news"), "top stories");
}

#[test]
fn test_google_news_url_extraction() {
    use srz::news::{extract_article_id, extract_attr_value, parse_batchexecute_destination_url};

    let url = "https://news.google.com/rss/articles/CBMiWmh0dHBzOi8vd3d3LnNpdGVwb2ludC5jb20vamF2YXNjcmlwdC1uZXdzLw?oc=5";
    assert_eq!(
        extract_article_id(url),
        Some("CBMiWmh0dHBzOi8vd3d3LnNpdGVwb2ludC5jb20vamF2YXNjcmlwdC1uZXdzLw")
    );

    let html = r#"<div data-n-a-sg="ABC123xyz" data-n-a-ts="1714200000"></div>"#;
    assert_eq!(extract_attr_value(html, "data-n-a-sg="), Some("ABC123xyz"));
    assert_eq!(extract_attr_value(html, "data-n-a-ts="), Some("1714200000"));

    let rpc_text = ")]}'\n\n[[\"wrb.fr\",\"Fbv4je\",\"[\\\"garturlreq\\\",\\\"https://www.sitepoint.com/javascript-news/\\\"]\"]]";
    assert_eq!(
        parse_batchexecute_destination_url(rpc_text),
        Some("https://www.sitepoint.com/javascript-news/".to_string())
    );
}

#[test]
fn test_stream_flag_and_mode_defaults() {
    use srz::llm::StreamMode;

    // Default: stream is false, stream_mode is Buffered
    let default_cli = Cli::parse_from(["srz", "Dune"]);
    assert!(!default_cli.stream);
    assert!(matches!(
        default_cli.stream_mode("Dune"),
        StreamMode::Buffered
    ));

    // When --stream is provided: stream is true, stream_mode is Stdout
    let stream_cli = Cli::parse_from(["srz", "Dune", "--stream"]);
    assert!(stream_cli.stream);
    match stream_cli.stream_mode("Dune") {
        StreamMode::Stdout { header_title } => assert_eq!(header_title, "Dune"),
        _ => panic!("Expected StreamMode::Stdout"),
    }

    // When --stream is provided but with -o, stream_mode is Buffered
    let file_cli = Cli::parse_from(["srz", "Dune", "--stream", "-o", "out.md"]);
    assert!(file_cli.stream);
    assert!(matches!(file_cli.stream_mode("Dune"), StreamMode::Buffered));

    // When --stream is provided but with --json, stream_mode is Buffered
    let json_cli = Cli::parse_from(["srz", "Dune", "--stream", "--json"]);
    assert!(json_cli.stream);
    assert!(matches!(json_cli.stream_mode("Dune"), StreamMode::Buffered));
}

#[test]
fn test_upgrade_subcommand_flags() {
    // Default srz upgrade
    let upgrade_cli = Cli::parse_from(["srz", "upgrade"]);
    assert_eq!(
        upgrade_cli.resolve_action().unwrap(),
        ResolvedAction::Upgrade {
            check: false,
            force: false,
            version: None,
            dry_run: false,
        }
    );

    // srz upgrade --check
    let check_cli = Cli::parse_from(["srz", "upgrade", "-c"]);
    assert_eq!(
        check_cli.resolve_action().unwrap(),
        ResolvedAction::Upgrade {
            check: true,
            force: false,
            version: None,
            dry_run: false,
        }
    );

    // srz upgrade --force --dry-run
    let force_dry_cli = Cli::parse_from(["srz", "upgrade", "--force", "--dry-run"]);
    assert_eq!(
        force_dry_cli.resolve_action().unwrap(),
        ResolvedAction::Upgrade {
            check: false,
            force: true,
            version: None,
            dry_run: true,
        }
    );

    // srz upgrade --version 0.2.0
    let ver_cli = Cli::parse_from(["srz", "upgrade", "--version", "0.2.0"]);
    assert_eq!(
        ver_cli.resolve_action().unwrap(),
        ResolvedAction::Upgrade {
            check: false,
            force: false,
            version: Some("0.2.0".to_string()),
            dry_run: false,
        }
    );
}