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
//! Test module for UI helpers.

use super::*;

/// What: Initialize details field translations for tests.
///
/// Inputs:
/// - `translations`: Mutable reference to translations map.
///
/// Output:
/// - Adds details field translations to the map.
fn init_details_translations(translations: &mut std::collections::HashMap<String, String>) {
    translations.insert(
        "app.details.fields.repository".to_string(),
        "Repository".to_string(),
    );
    translations.insert(
        "app.details.fields.package_name".to_string(),
        "Package Name".to_string(),
    );
    translations.insert(
        "app.details.fields.version".to_string(),
        "Version".to_string(),
    );
    translations.insert(
        "app.details.fields.description".to_string(),
        "Description".to_string(),
    );
    translations.insert(
        "app.details.fields.architecture".to_string(),
        "Architecture".to_string(),
    );
    translations.insert("app.details.fields.url".to_string(), "URL".to_string());
    translations.insert(
        "app.details.fields.licences".to_string(),
        "Licences".to_string(),
    );
    translations.insert(
        "app.details.fields.provides".to_string(),
        "Provides".to_string(),
    );
    translations.insert(
        "app.details.fields.depends_on".to_string(),
        "Depends on".to_string(),
    );
    translations.insert(
        "app.details.fields.optional_dependencies".to_string(),
        "Optional dependencies".to_string(),
    );
    translations.insert(
        "app.details.fields.required_by".to_string(),
        "Required by".to_string(),
    );
    translations.insert(
        "app.details.fields.optional_for".to_string(),
        "Optional for".to_string(),
    );
    translations.insert(
        "app.details.fields.conflicts_with".to_string(),
        "Conflicts with".to_string(),
    );
    translations.insert(
        "app.details.fields.replaces".to_string(),
        "Replaces".to_string(),
    );
    translations.insert(
        "app.details.fields.download_size".to_string(),
        "Download size".to_string(),
    );
    translations.insert(
        "app.details.fields.install_size".to_string(),
        "Install size".to_string(),
    );
    translations.insert(
        "app.details.fields.package_owner".to_string(),
        "Package Owner".to_string(),
    );
    translations.insert(
        "app.details.fields.build_date".to_string(),
        "Build date".to_string(),
    );
    translations.insert(
        "app.details.fields.not_available".to_string(),
        "N/A".to_string(),
    );
    translations.insert(
        "app.details.show_pkgbuild".to_string(),
        "Show PKGBUILD".to_string(),
    );
    translations.insert(
        "app.details.hide_pkgbuild".to_string(),
        "Hide PKGBUILD".to_string(),
    );
    translations.insert("app.details.url_label".to_string(), "URL:".to_string());
}

/// What: Initialize results translations for tests.
///
/// Inputs:
/// - `translations`: Mutable reference to translations map.
///
/// Output:
/// - Adds results translations to the map.
fn init_results_translations(translations: &mut std::collections::HashMap<String, String>) {
    translations.insert("app.results.title".to_string(), "Results".to_string());
    translations.insert("app.results.buttons.sort".to_string(), "Sort".to_string());
    translations.insert(
        "app.results.buttons.options".to_string(),
        "Options".to_string(),
    );
    translations.insert(
        "app.results.buttons.panels".to_string(),
        "Panels".to_string(),
    );
    translations.insert(
        "app.results.buttons.config_lists".to_string(),
        "Config/Lists".to_string(),
    );
    translations.insert("app.results.buttons.menu".to_string(), "Menu".to_string());
    translations.insert("app.results.filters.aur".to_string(), "AUR".to_string());
    translations.insert("app.results.filters.core".to_string(), "core".to_string());
    translations.insert("app.results.filters.extra".to_string(), "extra".to_string());
    translations.insert(
        "app.results.filters.multilib".to_string(),
        "multilib".to_string(),
    );
    translations.insert("app.results.filters.eos".to_string(), "EOS".to_string());
    translations.insert(
        "app.results.filters.cachyos".to_string(),
        "CachyOS".to_string(),
    );
    translations.insert("app.results.filters.artix".to_string(), "Artix".to_string());
    translations.insert(
        "app.results.filters.artix_omniverse".to_string(),
        "OMNI".to_string(),
    );
    translations.insert(
        "app.results.filters.artix_universe".to_string(),
        "UNI".to_string(),
    );
    translations.insert(
        "app.results.filters.artix_lib32".to_string(),
        "LIB32".to_string(),
    );
    translations.insert(
        "app.results.filters.artix_galaxy".to_string(),
        "GALAXY".to_string(),
    );
    translations.insert(
        "app.results.filters.artix_world".to_string(),
        "WORLD".to_string(),
    );
    translations.insert(
        "app.results.filters.artix_system".to_string(),
        "SYSTEM".to_string(),
    );
    translations.insert(
        "app.results.filters.blackarch".to_string(),
        "BlackArch".to_string(),
    );
    translations.insert(
        "app.results.filters.manjaro".to_string(),
        "Manjaro".to_string(),
    );
}

/// What: Initialize minimal English translations for tests.
///
/// Inputs:
/// - `app`: `AppState` to populate with translations
///
/// Output:
/// - Populates `app.translations` and `app.translations_fallback` with minimal English translations
///
/// Details:
/// - Sets up only the translations needed for tests to pass
fn init_test_translations(app: &mut crate::state::AppState) {
    use std::collections::HashMap;
    let mut translations = HashMap::new();
    init_details_translations(&mut translations);
    init_results_translations(&mut translations);
    app.translations = translations.clone();
    app.translations_fallback = translations;
}

/// What: Create a test `PackageItem` for an official repository package.
///
/// Inputs:
/// - `name`: Package name
/// - `repo`: Repository name (e.g., "extra", "core")
///
/// Output:
/// - Returns a `PackageItem` with default test values for an official package.
///
/// Details:
/// - Sets version to "1.0", creates a default description, and sets architecture to `x86_64`.
/// - Used in tests to create mock official packages.
fn item_official(name: &str, repo: &str) -> crate::state::PackageItem {
    crate::state::PackageItem {
        name: name.to_string(),
        version: "1.0".to_string(),
        description: format!("{name} desc"),
        source: crate::state::Source::Official {
            repo: repo.to_string(),
            arch: "x86_64".to_string(),
        },
        popularity: None,
        out_of_date: None,
        orphaned: false,
    }
}

#[test]
/// What: Validate helper functions that filter recent/install indices and toggle details labels.
///
/// Inputs:
/// - Recent list with pane find queries, install list search term, and details populated with metadata.
///
/// Output:
/// - Filtered indices match expectations and the details footer alternates between `Show`/`Hide PKGBUILD` labels.
///
/// Details:
/// - Covers the case-insensitive dedupe path plus button label toggling when PKGBUILD visibility flips.
fn filtered_indices_and_details_lines() {
    let mut app = crate::state::AppState::default();
    init_test_translations(&mut app);
    app.load_recent_items(&[
        "alpha".to_string(),
        "bravo".to_string(),
        "charlie".to_string(),
    ]);
    assert_eq!(filtered_recent_indices(&app), vec![0, 1, 2]);
    app.focus = crate::state::Focus::Recent;
    app.pane_find = Some("a".into());
    let inds = filtered_recent_indices(&app);
    assert_eq!(inds, vec![0, 1, 2]);

    app.install_list = vec![
        item_official("ripgrep", "extra"),
        crate::state::PackageItem {
            name: "fd".into(),
            version: "1".into(),
            description: String::new(),
            source: crate::state::Source::Aur,
            popularity: None,
            out_of_date: None,
            orphaned: false,
        },
    ];
    app.focus = crate::state::Focus::Install;
    app.pane_find = Some("rip".into());
    let inds2 = filtered_install_indices(&app);
    assert_eq!(inds2, vec![0]);

    app.details = crate::state::PackageDetails {
        repository: "extra".into(),
        name: "ripgrep".into(),
        version: "14".into(),
        description: "desc".into(),
        architecture: "x86_64".into(),
        url: "https://example.com".into(),
        licenses: vec!["MIT".into()],
        groups: vec![],
        provides: vec![],
        depends: vec![],
        opt_depends: vec![],
        required_by: vec![],
        optional_for: vec![],
        conflicts: vec![],
        replaces: vec![],
        download_size: None,
        install_size: None,
        owner: "owner".into(),
        build_date: "date".into(),
        popularity: None,
        out_of_date: None,
        orphaned: false,
    };
    let th = crate::theme::theme();
    let lines = format_details_lines(&app, 80, &th);
    assert!(
        lines
            .last()
            .expect("lines should not be empty in test")
            .spans[0]
            .content
            .contains("Show PKGBUILD")
    );
    let mut app2 = crate::state::AppState::default();
    init_test_translations(&mut app2);
    app2.details = app.details.clone();
    app2.pkgb_visible = true;
    let lines2 = format_details_lines(&app2, 80, &th);
    assert!(
        lines2
            .last()
            .expect("lines2 should not be empty in test")
            .spans[0]
            .content
            .contains("Hide PKGBUILD")
    );
}

#[test]
/// What: Ensure filtered recent indices use news history when in news mode.
///
/// Inputs:
/// - App mode set to News with dedicated news recent entries and pane find.
///
/// Output:
/// - Indices reflect the news history and respect pane find filtering.
fn filtered_recent_indices_use_news_history() {
    let mut app = crate::state::AppState::default();
    init_test_translations(&mut app);
    app.app_mode = crate::state::types::AppMode::News;
    app.load_news_recent_items(&["alpha".to_string(), "beta".to_string(), "gamma".to_string()]);
    assert_eq!(filtered_recent_indices(&app), vec![0, 1, 2]);
    app.focus = crate::state::Focus::Recent;
    app.pane_find = Some("be".into());
    let inds = filtered_recent_indices(&app);
    assert_eq!(inds, vec![1]);
}

#[test]
/// What: Ensure details rendering formats lists and byte sizes into human-friendly strings.
///
/// Inputs:
/// - `PackageDetails` with empty license list, multiple provides, and a non-zero install size.
///
/// Output:
/// - Renders `N/A` for missing values, formats bytes into `1.5 KiB`, and joins lists with commas.
///
/// Details:
/// - Confirms string composition matches UI expectations for optional fields.
fn details_lines_sizes_and_lists() {
    let mut app = crate::state::AppState::default();
    init_test_translations(&mut app);
    app.details = crate::state::PackageDetails {
        repository: "extra".into(),
        name: "ripgrep".into(),
        version: "14".into(),
        description: "desc".into(),
        architecture: "x86_64".into(),
        url: String::new(),
        licenses: vec![],
        groups: vec![],
        provides: vec!["prov1".into(), "prov2".into()],
        depends: vec![],
        opt_depends: vec![],
        required_by: vec![],
        optional_for: vec![],
        conflicts: vec![],
        replaces: vec![],
        download_size: None,
        install_size: Some(1536),
        owner: String::new(),
        build_date: String::new(),
        popularity: None,
        out_of_date: None,
        orphaned: false,
    };
    let th = crate::theme::theme();
    let lines = format_details_lines(&app, 80, &th);
    assert!(
        lines
            .iter()
            .any(|l| l.spans.iter().any(|s| s.content.contains("N/A")))
    );
    assert!(
        lines
            .iter()
            .any(|l| l.spans.iter().any(|s| s.content.contains("1.5 KiB")))
    );
    assert!(lines.iter().any(|l| {
        l.spans
            .iter()
            .any(|s| s.content.contains("Licences") || s.content.contains('-'))
    }));
    assert!(
        lines
            .iter()
            .any(|l| l.spans.iter().any(|s| s.content.contains("prov1, prov2")))
    );
}

#[tokio::test]
/// What: Ensure the recent preview trigger becomes a no-op when focus or selection is invalid.
///
/// Inputs:
/// - `app`: Focus initially outside Recent, then Recent with no selection, then Recent with a filtered-out entry.
/// - `tx`: Preview channel observed for emitted messages across each scenario.
///
/// Output:
/// - Each invocation leaves the channel empty, showing no preview requests were issued.
///
/// Details:
/// - Applies a short timeout for each check to guard against unexpected asynchronous sends.
async fn trigger_recent_preview_noop_when_not_recent_or_invalid() {
    let mut app = crate::state::AppState::default();
    let (tx, mut rx) = tokio::sync::mpsc::unbounded_channel();
    app.focus = crate::state::Focus::Search;
    trigger_recent_preview(&app, &tx);
    let none1 = tokio::time::timeout(std::time::Duration::from_millis(30), rx.recv())
        .await
        .ok()
        .flatten();
    assert!(none1.is_none());

    app.focus = crate::state::Focus::Recent;
    app.load_recent_items(&["abc".to_string()]);
    app.history_state.select(None);
    trigger_recent_preview(&app, &tx);
    let none2 = tokio::time::timeout(std::time::Duration::from_millis(30), rx.recv())
        .await
        .ok()
        .flatten();
    assert!(none2.is_none());

    app.history_state.select(Some(0));
    app.pane_find = Some("zzz".into());
    trigger_recent_preview(&app, &tx);
    let none3 = tokio::time::timeout(std::time::Duration::from_millis(30), rx.recv())
        .await
        .ok()
        .flatten();
    assert!(none3.is_none());
}