pacsea 0.8.2

A fast, friendly TUI for browsing and installing Arch and AUR packages with built-in news and security scanning
Documentation
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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
//! Tests for `AppState`.

use crate::state::app_state::AppState;
use crate::state::types::{
    AdvisorySeverity, NewsFeedItem, NewsFeedSource, NewsReadFilter, NewsSortMode,
};

#[test]
/// What: Verify `AppState::default` initialises UI flags and filesystem paths under the configured lists directory.
///
/// Inputs:
/// - No direct inputs; shims the `HOME` environment variable to a temporary directory before constructing `AppState`.
///
/// Output:
/// - Ensures selection indices reset to zero, result buffers start empty, and cached path values live under `lists_dir`.
///
/// Details:
/// - Uses a mutex guard to serialise environment mutations and restores `HOME` at the end to avoid cross-test interference.
fn app_state_default_initializes_paths_and_flags() {
    let _guard = crate::state::test_mutex()
        .lock()
        .expect("Test mutex poisoned");
    // Shim HOME so lists_dir() resolves under a temp dir
    let orig_home = std::env::var_os("HOME");
    let dir = std::env::temp_dir().join(format!(
        "pacsea_test_state_default_{}_{}",
        std::process::id(),
        std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .expect("System time is before UNIX epoch")
            .as_nanos()
    ));
    let _ = std::fs::create_dir_all(&dir);
    unsafe { std::env::set_var("HOME", dir.display().to_string()) };

    let app = AppState::default();
    assert_eq!(app.selected, 0);
    assert!(app.results.is_empty());
    assert!(app.all_results.is_empty());
    assert!(!app.loading_index);
    assert!(!app.dry_run);
    // Paths should point under lists_dir
    let lists = crate::theme::lists_dir();
    assert!(app.recent_path.starts_with(&lists));
    assert!(app.cache_path.starts_with(&lists));
    assert!(app.install_path.starts_with(&lists));
    assert!(app.official_index_path.starts_with(&lists));
    assert!(app.news_read_ids_path.starts_with(&lists));

    unsafe {
        if let Some(v) = orig_home {
            std::env::set_var("HOME", v);
        } else {
            std::env::remove_var("HOME");
        }
    }
}

#[cfg(test)]
#[test]
#[allow(clippy::field_reassign_with_default)]
/// What: Ensure news filtering respects per-source toggles for updates and comments.
///
/// Inputs:
/// - Five news items spanning Arch, advisory, official update, AUR update, and AUR comment.
/// - Filters that disable Arch/advisory/update sources while leaving AUR comments enabled.
///
/// Output:
/// - `news_results` retains only the enabled source after applying filters.
///
/// Details:
/// - Uses the global test mutex and HOME shim to avoid path collisions with other tests.
fn refresh_news_results_applies_all_source_filters() {
    let _guard = crate::state::test_mutex()
        .lock()
        .expect("Test mutex poisoned");
    let orig_home = std::env::var_os("HOME");
    let dir = std::env::temp_dir().join(format!(
        "pacsea_test_news_filters_{}_{}",
        std::process::id(),
        std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .expect("System time is before UNIX epoch")
            .as_nanos()
    ));
    let _ = std::fs::create_dir_all(&dir);
    unsafe { std::env::set_var("HOME", dir.display().to_string()) };

    let mut app = AppState::default();
    app.news_items = vec![
        NewsFeedItem {
            id: "arch".into(),
            date: "2025-01-01".into(),
            title: "Arch".into(),
            summary: None,
            url: None,
            source: NewsFeedSource::ArchNews,
            severity: None,
            packages: vec![],
        },
        NewsFeedItem {
            id: "adv".into(),
            date: "2025-01-01".into(),
            title: "ADV".into(),
            summary: None,
            url: None,
            source: NewsFeedSource::SecurityAdvisory,
            severity: Some(AdvisorySeverity::High),
            packages: vec!["openssl".into()],
        },
        NewsFeedItem {
            id: "upd-official".into(),
            date: "2025-01-01".into(),
            title: "Official update".into(),
            summary: None,
            url: None,
            source: NewsFeedSource::InstalledPackageUpdate,
            severity: None,
            packages: vec!["pacman".into()],
        },
        NewsFeedItem {
            id: "upd-aur".into(),
            date: "2025-01-01".into(),
            title: "AUR update".into(),
            summary: None,
            url: None,
            source: NewsFeedSource::AurPackageUpdate,
            severity: None,
            packages: vec!["yay".into()],
        },
        NewsFeedItem {
            id: "comment".into(),
            date: "2025-01-01".into(),
            title: "New comment".into(),
            summary: Some("hello".into()),
            url: None,
            source: NewsFeedSource::AurComment,
            severity: None,
            packages: vec!["yay".into()],
        },
    ];
    app.news_filter_show_arch_news = false;
    app.news_filter_show_advisories = false;
    app.news_filter_show_pkg_updates = false;
    app.news_filter_show_aur_updates = false;
    app.news_filter_show_aur_comments = true;
    app.news_filter_installed_only = false;
    app.news_max_age_days = None;

    app.refresh_news_results();
    assert_eq!(app.news_results.len(), 1);
    assert_eq!(app.news_results[0].id, "comment");

    unsafe {
        if let Some(v) = orig_home {
            std::env::set_var("HOME", v);
        } else {
            std::env::remove_var("HOME");
        }
    }
}

#[cfg(test)]
#[test]
#[allow(clippy::field_reassign_with_default)]
/// What: Ensure news read filter respects read/unread selections.
///
/// Inputs:
/// - Two news items with distinct IDs and URLs.
/// - `news_read_ids` containing one of the items.
///
/// Output:
/// - `news_results` reflect the selected read filter (All/Unread/Read).
///
/// Details:
/// - Uses HOME shim to avoid collisions with persisted paths.
fn refresh_news_results_applies_read_filter() {
    let _guard = crate::state::test_mutex()
        .lock()
        .expect("Test mutex poisoned");
    let orig_home = std::env::var_os("HOME");
    let dir = std::env::temp_dir().join(format!(
        "pacsea_test_news_read_filter_{}_{}",
        std::process::id(),
        std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .expect("System time is before UNIX epoch")
            .as_nanos()
    ));
    let _ = std::fs::create_dir_all(&dir);
    unsafe { std::env::set_var("HOME", dir.display().to_string()) };

    let mut app = AppState::default();
    app.news_items = vec![
        NewsFeedItem {
            id: "read".into(),
            date: "2025-01-01".into(),
            title: "Read item".into(),
            summary: None,
            url: Some("https://example.com/read".into()),
            source: NewsFeedSource::ArchNews,
            severity: None,
            packages: vec![],
        },
        NewsFeedItem {
            id: "unread".into(),
            date: "2025-01-02".into(),
            title: "Unread item".into(),
            summary: None,
            url: Some("https://example.com/unread".into()),
            source: NewsFeedSource::ArchNews,
            severity: None,
            packages: vec![],
        },
    ];
    app.news_read_ids.insert("read".into());
    app.news_filter_read_status = NewsReadFilter::Unread;
    app.news_max_age_days = None;

    app.refresh_news_results();
    assert_eq!(app.news_results.len(), 1);
    assert_eq!(app.news_results[0].id, "unread");

    app.news_filter_read_status = NewsReadFilter::Read;
    app.refresh_news_results();
    assert_eq!(app.news_results.len(), 1);
    assert_eq!(app.news_results[0].id, "read");

    app.news_filter_read_status = NewsReadFilter::All;
    app.refresh_news_results();
    assert_eq!(app.news_results.len(), 2);

    unsafe {
        if let Some(v) = orig_home {
            std::env::set_var("HOME", v);
        } else {
            std::env::remove_var("HOME");
        }
    }
}

#[cfg(test)]
#[test]
#[allow(clippy::field_reassign_with_default)]
/// What: Ensure "[Advisories All]" filter shows all advisories regardless of installed status.
///
/// Inputs:
/// - Advisories for both installed and non-installed packages.
/// - `news_filter_show_advisories = true` and `news_filter_installed_only = false`.
///
/// Output:
/// - All advisories are shown in `news_results`.
///
/// Details:
/// - Verifies that "[Advisories All]" behaves as if [Installed only] filter was off
///   and [Advisories] filter was on.
/// - When `news_filter_installed_only = false`, the installed-only filtering block
///   (lines 914-923) should not run, allowing all advisories to pass through.
/// - Uses HOME shim to avoid collisions with persisted paths.
fn refresh_news_results_advisories_all_shows_all() {
    let _guard = crate::state::test_mutex()
        .lock()
        .expect("Test mutex poisoned");
    let orig_home = std::env::var_os("HOME");
    let dir = std::env::temp_dir().join(format!(
        "pacsea_test_advisories_all_{}_{}",
        std::process::id(),
        std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .expect("System time is before UNIX epoch")
            .as_nanos()
    ));
    let _ = std::fs::create_dir_all(&dir);
    unsafe { std::env::set_var("HOME", dir.display().to_string()) };

    let mut app = AppState::default();

    app.news_items = vec![
        NewsFeedItem {
            id: "adv-1".into(),
            date: "2025-01-01".into(),
            title: "Advisory 1".into(),
            summary: None,
            url: None,
            source: NewsFeedSource::SecurityAdvisory,
            severity: Some(AdvisorySeverity::High),
            packages: vec!["package1".into()],
        },
        NewsFeedItem {
            id: "adv-2".into(),
            date: "2025-01-02".into(),
            title: "Advisory 2".into(),
            summary: None,
            url: None,
            source: NewsFeedSource::SecurityAdvisory,
            severity: Some(AdvisorySeverity::Medium),
            packages: vec!["package2".into()],
        },
        NewsFeedItem {
            id: "adv-3".into(),
            date: "2025-01-03".into(),
            title: "Advisory 3".into(),
            summary: None,
            url: None,
            source: NewsFeedSource::SecurityAdvisory,
            severity: Some(AdvisorySeverity::Critical),
            packages: vec!["package3".into(), "package4".into()],
        },
    ];

    // Set up "[Advisories All]" state: advisories on, installed_only off
    // This should show all advisories regardless of whether packages are installed
    app.news_filter_show_advisories = true;
    app.news_filter_installed_only = false;
    app.news_filter_show_arch_news = false;
    app.news_filter_show_pkg_updates = false;
    app.news_filter_show_aur_updates = false;
    app.news_filter_show_aur_comments = false;
    app.news_max_age_days = None;

    app.refresh_news_results();

    // All advisories should be shown when [Advisories All] is active
    // (news_filter_show_advisories = true, news_filter_installed_only = false)
    assert_eq!(
        app.news_results.len(),
        3,
        "All advisories should be shown when [Advisories All] is active (advisories on, installed_only off)"
    );
    assert!(app.news_results.iter().any(|it| it.id == "adv-1"));
    assert!(app.news_results.iter().any(|it| it.id == "adv-2"));
    assert!(app.news_results.iter().any(|it| it.id == "adv-3"));

    unsafe {
        if let Some(v) = orig_home {
            std::env::set_var("HOME", v);
        } else {
            std::env::remove_var("HOME");
        }
    }
}

#[cfg(test)]
#[test]
#[allow(clippy::field_reassign_with_default)]
/// What: Verify severity-first news sort orders higher severities before date and title tiebreaks.
///
/// Inputs:
/// - Mixed advisory severities with overlapping dates.
///
/// Output:
/// - `news_results` starts with Critical, then High (newest first), then Medium/Unknown.
///
/// Details:
/// - Uses HOME shim to avoid touching real persisted files.
fn refresh_news_results_sorts_by_severity_then_date() {
    let _guard = crate::state::test_mutex()
        .lock()
        .expect("Test mutex poisoned");
    let orig_home = std::env::var_os("HOME");
    let dir = std::env::temp_dir().join(format!(
        "pacsea_test_news_sort_severity_{}_{}",
        std::process::id(),
        std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .expect("System time is before UNIX epoch")
            .as_nanos()
    ));
    let _ = std::fs::create_dir_all(&dir);
    unsafe { std::env::set_var("HOME", dir.display().to_string()) };

    let mut app = AppState::default();
    app.news_items = vec![
        NewsFeedItem {
            id: "crit".into(),
            date: "2025-01-01".into(),
            title: "critical".into(),
            summary: None,
            url: None,
            source: NewsFeedSource::SecurityAdvisory,
            severity: Some(AdvisorySeverity::Critical),
            packages: vec![],
        },
        NewsFeedItem {
            id: "high-new".into(),
            date: "2025-01-03".into(),
            title: "high-new".into(),
            summary: None,
            url: None,
            source: NewsFeedSource::SecurityAdvisory,
            severity: Some(AdvisorySeverity::High),
            packages: vec![],
        },
        NewsFeedItem {
            id: "high-old".into(),
            date: "2025-01-02".into(),
            title: "high-old".into(),
            summary: None,
            url: None,
            source: NewsFeedSource::SecurityAdvisory,
            severity: Some(AdvisorySeverity::High),
            packages: vec![],
        },
        NewsFeedItem {
            id: "unknown".into(),
            date: "2025-01-04".into(),
            title: "unknown".into(),
            summary: None,
            url: None,
            source: NewsFeedSource::SecurityAdvisory,
            severity: Some(AdvisorySeverity::Unknown),
            packages: vec![],
        },
    ];
    app.news_filter_show_advisories = true;
    app.news_filter_installed_only = false;
    app.news_filter_show_arch_news = false;
    app.news_filter_show_pkg_updates = false;
    app.news_filter_show_aur_updates = false;
    app.news_filter_show_aur_comments = false;
    app.news_max_age_days = None;
    app.news_sort_mode = NewsSortMode::SeverityThenDate;
    app.refresh_news_results();
    let ids: Vec<String> = app.news_results.iter().map(|it| it.id.clone()).collect();
    assert_eq!(ids, vec!["crit", "high-new", "high-old", "unknown"]);

    unsafe {
        if let Some(v) = orig_home {
            std::env::set_var("HOME", v);
        } else {
            std::env::remove_var("HOME");
        }
    }
}

#[cfg(test)]
#[test]
#[allow(clippy::field_reassign_with_default)]
/// What: Verify unread-first sorting promotes unread items ahead of read ones, then newest-first.
///
/// Inputs:
/// - Mixed read/unread items with different dates.
///
/// Output:
/// - Unread entries appear before read entries; newest unread first.
///
/// Details:
/// - Uses URL-based read markers to ensure both id/url markers are honoured.
fn refresh_news_results_sorts_unread_first_then_date() {
    let _guard = crate::state::test_mutex()
        .lock()
        .expect("Test mutex poisoned");
    let orig_home = std::env::var_os("HOME");
    let dir = std::env::temp_dir().join(format!(
        "pacsea_test_news_sort_unread_{}_{}",
        std::process::id(),
        std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .expect("System time is before UNIX epoch")
            .as_nanos()
    ));
    let _ = std::fs::create_dir_all(&dir);
    unsafe { std::env::set_var("HOME", dir.display().to_string()) };

    let mut app = AppState::default();
    app.news_items = vec![
        NewsFeedItem {
            id: "read-old".into(),
            date: "2025-01-01".into(),
            title: "read-old".into(),
            summary: None,
            url: Some("https://example.com/read-old".into()),
            source: NewsFeedSource::ArchNews,
            severity: None,
            packages: vec![],
        },
        NewsFeedItem {
            id: "read-new".into(),
            date: "2025-01-04".into(),
            title: "read-new".into(),
            summary: None,
            url: Some("https://example.com/read-new".into()),
            source: NewsFeedSource::ArchNews,
            severity: None,
            packages: vec![],
        },
        NewsFeedItem {
            id: "unread-old".into(),
            date: "2025-01-02".into(),
            title: "unread-old".into(),
            summary: None,
            url: Some("https://example.com/unread-old".into()),
            source: NewsFeedSource::ArchNews,
            severity: None,
            packages: vec![],
        },
        NewsFeedItem {
            id: "unread-new".into(),
            date: "2025-01-05".into(),
            title: "unread-new".into(),
            summary: None,
            url: Some("https://example.com/unread-new".into()),
            source: NewsFeedSource::ArchNews,
            severity: None,
            packages: vec![],
        },
    ];
    app.news_filter_show_arch_news = true;
    app.news_filter_show_advisories = false;
    app.news_filter_show_pkg_updates = false;
    app.news_filter_show_aur_updates = false;
    app.news_filter_show_aur_comments = false;
    app.news_filter_installed_only = false;
    app.news_max_age_days = None;
    app.news_read_urls
        .insert("https://example.com/read-old".into());
    app.news_read_ids.insert("read-new".into());
    app.news_sort_mode = NewsSortMode::UnreadThenDate;

    app.refresh_news_results();
    let ids: Vec<String> = app.news_results.iter().map(|it| it.id.clone()).collect();
    assert_eq!(
        ids,
        vec![
            "unread-new".to_string(),
            "unread-old".to_string(),
            "read-new".to_string(),
            "read-old".to_string()
        ]
    );

    unsafe {
        if let Some(v) = orig_home {
            std::env::set_var("HOME", v);
        } else {
            std::env::remove_var("HOME");
        }
    }
}