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
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
use crate::state::{AppState, PackageItem};

/// What: Filter cached dependencies for current items.
///
/// Inputs:
/// - `app`: Application state with cached dependencies.
/// - `item_names`: Set of current package names.
///
/// Output:
/// - Vector of matching cached dependencies.
fn filter_cached_dependencies(
    app: &AppState,
    item_names: &std::collections::HashSet<String>,
) -> Vec<crate::state::modal::DependencyInfo> {
    app.install_list_deps
        .iter()
        .filter(|dep| {
            dep.required_by
                .iter()
                .any(|req_by| item_names.contains(req_by))
        })
        .cloned()
        .collect()
}

/// What: Filter cached files for current items.
///
/// Inputs:
/// - `app`: Application state with cached files.
/// - `item_names`: Set of current package names.
///
/// Output:
/// - Vector of matching cached files.
fn filter_cached_files(
    app: &AppState,
    item_names: &std::collections::HashSet<String>,
) -> Vec<crate::state::modal::PackageFileInfo> {
    app.install_list_files
        .iter()
        .filter(|file_info| item_names.contains(&file_info.name))
        .cloned()
        .collect()
}

/// What: Trigger background resolution for preflight data.
///
/// Inputs:
/// - `app`: Application state.
/// - `items`: Packages to resolve.
/// - `dependency_info`: Current dependency info (empty triggers resolution).
/// - `cached_files`: Current cached files (empty triggers resolution).
///
/// Output:
/// - Updates app state with resolution flags and items.
fn trigger_background_resolution(
    app: &mut AppState,
    items: &[PackageItem],
    dependency_info: &[crate::state::modal::DependencyInfo],
    cached_files: &[crate::state::modal::PackageFileInfo],
) {
    if dependency_info.is_empty() {
        app.preflight_deps_items = Some((
            items.to_vec(),
            crate::state::modal::PreflightAction::Install,
        ));
        app.preflight_deps_resolving = true;
    }
    if cached_files.is_empty() {
        app.preflight_files_items = Some(items.to_vec());
        app.preflight_files_resolving = true;
    }
    app.preflight_services_items = Some(items.to_vec());
    app.preflight_services_resolving = true;
    let aur_items: Vec<_> = items
        .iter()
        .filter(|p| matches!(p.source, crate::state::Source::Aur))
        .cloned()
        .collect();
    if !aur_items.is_empty() {
        app.preflight_sandbox_items = Some(aur_items);
        app.preflight_sandbox_resolving = true;
    }
}

/// What: Create preflight modal with cached data.
///
/// Inputs:
/// - `app`: Application state.
/// - `items`: Packages under review.
/// - `summary`: Preflight summary data.
/// - `header`: Header chips data.
/// - `dependency_info`: Dependency information.
/// - `cached_files`: Cached file information.
///
/// Output:
/// - Creates and sets the Preflight modal in app state.
fn create_preflight_modal_with_cache(
    app: &mut AppState,
    items: Vec<PackageItem>,
    summary: crate::state::modal::PreflightSummaryData,
    header: crate::state::modal::PreflightHeaderChips,
    dependency_info: Vec<crate::state::modal::DependencyInfo>,
    cached_files: Vec<crate::state::modal::PackageFileInfo>,
) {
    app.modal = crate::state::Modal::Preflight {
        items,
        action: crate::state::PreflightAction::Install,
        tab: crate::state::PreflightTab::Deps,
        summary: Some(Box::new(summary)),
        summary_scroll: 0,
        header_chips: header,
        dependency_info,
        dep_selected: 0,
        dep_tree_expanded: std::collections::HashSet::new(),
        deps_error: None,
        file_info: cached_files,
        file_selected: 0,
        file_tree_expanded: std::collections::HashSet::new(),
        files_error: None,
        service_info: Vec::new(),
        service_selected: 0,
        services_loaded: false,
        services_error: None,
        sandbox_info: Vec::new(),
        sandbox_selected: 0,
        sandbox_tree_expanded: std::collections::HashSet::new(),
        sandbox_loaded: false,
        sandbox_error: None,
        selected_optdepends: std::collections::HashMap::new(),
        cascade_mode: app.remove_cascade_mode,
        cached_reverse_deps_report: None,
    };
}

/// What: Create preflight modal for insert mode (background computation).
///
/// Inputs:
/// - `app`: Application state.
/// - `items`: Packages under review.
///
/// Output:
/// - Creates and sets the Preflight modal with background computation queued.
fn create_preflight_modal_insert_mode(app: &mut AppState, items: Vec<PackageItem>) {
    let items_clone = items.clone();
    app.preflight_cancelled
        .store(false, std::sync::atomic::Ordering::Relaxed);
    app.preflight_summary_items = Some((items_clone, crate::state::PreflightAction::Install));
    app.preflight_summary_resolving = true;
    app.pending_service_plan.clear();
    app.modal = crate::state::Modal::Preflight {
        items,
        action: crate::state::PreflightAction::Install,
        tab: crate::state::PreflightTab::Summary,
        summary: None,
        summary_scroll: 0,
        header_chips: crate::state::modal::PreflightHeaderChips {
            package_count: 1,
            download_bytes: 0,
            install_delta_bytes: 0,
            aur_count: 0,
            risk_score: 0,
            risk_level: crate::state::modal::RiskLevel::Low,
        },
        dependency_info: Vec::new(),
        dep_selected: 0,
        dep_tree_expanded: std::collections::HashSet::new(),
        deps_error: None,
        file_info: Vec::new(),
        file_selected: 0,
        file_tree_expanded: std::collections::HashSet::new(),
        files_error: None,
        service_info: Vec::new(),
        service_selected: 0,
        services_loaded: false,
        services_error: None,
        sandbox_info: Vec::new(),
        sandbox_selected: 0,
        sandbox_tree_expanded: std::collections::HashSet::new(),
        sandbox_loaded: false,
        sandbox_error: None,
        selected_optdepends: std::collections::HashMap::new(),
        cascade_mode: app.remove_cascade_mode,
        cached_reverse_deps_report: None,
    };
}

/// What: Open preflight modal with cached dependencies and files, or trigger background resolution.
///
/// Inputs:
/// - `app`: Mutable application state
/// - `items`: Packages to open preflight for
/// - `use_cache`: Whether to use cached dependencies/files or trigger background resolution
///
/// Output:
/// - None (modifies app state directly)
///
/// Details:
/// - If `use_cache` is true, checks cache and uses cached data if available, otherwise triggers background resolution.
/// - If `use_cache` is false, always triggers background resolution (used in insert mode).
/// - Sets up all preflight resolution flags and initializes the modal state.
pub fn open_preflight_modal(app: &mut AppState, items: Vec<PackageItem>, use_cache: bool) {
    if crate::theme::settings().skip_preflight {
        if crate::events::install::try_open_warn_aur_repo_duplicate_modal(
            app,
            &items,
            crate::state::modal::PreflightHeaderChips::default(),
        ) {
            return;
        }
        // Direct install - check for reinstalls first, then batch updates
        // First, check if we're installing packages that are already installed (reinstall scenario)
        // BUT exclude packages that have updates available (those should go through normal update flow)
        let installed_set = crate::logic::deps::get_installed_packages();
        let provided_set = crate::logic::deps::get_provided_packages(&installed_set);
        let upgradable_set = crate::logic::deps::get_upgradable_packages();

        let installed_packages: Vec<crate::state::PackageItem> = items
            .iter()
            .filter(|item| {
                // Check if package is installed or provided by an installed package
                let is_installed = crate::logic::deps::is_package_installed_or_provided(
                    &item.name,
                    &installed_set,
                    &provided_set,
                );

                if !is_installed {
                    return false;
                }

                // Check if package has an update available
                // For official packages: check if it's in upgradable_set OR version differs from installed
                // For AUR packages: check if target version is different from installed version
                let has_update = if upgradable_set.contains(&item.name) {
                    // Package is in upgradable set (pacman -Qu)
                    true
                } else if !item.version.is_empty() {
                    // Normalize target version by removing revision suffix (same as installed version normalization)
                    let normalized_target_version =
                        item.version.split('-').next().unwrap_or(&item.version);
                    // Compare normalized target version with normalized installed version
                    // This works for both official and AUR packages
                    crate::logic::deps::get_installed_version(&item.name).is_ok_and(
                        |installed_version| normalized_target_version != installed_version,
                    )
                } else {
                    // No version info available, no update
                    false
                };

                // Only show reinstall confirmation if installed AND no update available
                // If update is available, it should go through normal update flow
                !has_update
            })
            .cloned()
            .collect();

        if !installed_packages.is_empty() {
            // Show reinstall confirmation modal
            // Store both installed packages (for display) and all packages (for installation)
            app.modal = crate::state::Modal::ConfirmReinstall {
                items: installed_packages,
                all_items: items,
                header_chips: crate::state::modal::PreflightHeaderChips::default(),
            };
            return;
        }

        // Check if this is a batch update scenario requiring confirmation
        // Only show if there's actually an update available (package is upgradable)
        // AND the package has installed packages in its "Required By" field (dependency risk)
        let has_versions = items.iter().any(|item| {
            matches!(item.source, crate::state::Source::Official { .. }) && !item.version.is_empty()
        });
        let has_upgrade_available = items.iter().any(|item| {
            matches!(item.source, crate::state::Source::Official { .. })
                && upgradable_set.contains(&item.name)
        });

        // Only show warning if package has installed packages in "Required By" (dependency risk)
        let has_installed_required_by = items.iter().any(|item| {
            matches!(item.source, crate::state::Source::Official { .. })
                && crate::index::is_installed(&item.name)
                && crate::logic::deps::has_installed_required_by(&item.name)
        });

        if has_versions && has_upgrade_available && has_installed_required_by {
            // Show confirmation modal for batch updates (only if update is actually available
            // AND package has installed dependents that could be affected)
            app.modal = crate::state::Modal::ConfirmBatchUpdate {
                items,
                dry_run: app.dry_run,
            };
            return;
        }

        crate::install::start_integrated_install_all(app, &items, app.dry_run);
        app.toast_message = Some(crate::i18n::t(app, "app.toasts.installing_skipped"));
        app.toast_expires_at = Some(std::time::Instant::now() + std::time::Duration::from_secs(3));
        return;
    }

    if use_cache {
        // Reset cancellation flag when opening new preflight
        app.preflight_cancelled
            .store(false, std::sync::atomic::Ordering::Relaxed);

        let crate::logic::preflight::PreflightSummaryOutcome {
            summary,
            header,
            reverse_deps_report: _,
        } = crate::logic::preflight::compute_preflight_summary(
            &items,
            crate::state::PreflightAction::Install,
        );
        app.pending_service_plan.clear();

        let item_names: std::collections::HashSet<String> =
            items.iter().map(|i| i.name.clone()).collect();
        let cached_deps = filter_cached_dependencies(app, &item_names);
        let cached_files = filter_cached_files(app, &item_names);

        let dependency_info = if cached_deps.is_empty() {
            tracing::debug!(
                "[Preflight] Cache empty, will trigger background dependency resolution for {} packages",
                items.len()
            );
            Vec::new()
        } else {
            cached_deps
        };

        trigger_background_resolution(app, &items, &dependency_info, &cached_files);
        create_preflight_modal_with_cache(
            app,
            items,
            summary,
            header,
            dependency_info,
            cached_files,
        );
    } else {
        create_preflight_modal_insert_mode(app, items);
    }
    app.toast_message = Some(if use_cache {
        crate::i18n::t(app, "app.toasts.preflight_opened")
    } else {
        "Preflight opened".to_string()
    });
    app.toast_expires_at = Some(std::time::Instant::now() + std::time::Duration::from_secs(2));
}

#[cfg(test)]
mod tests {
    use super::*;

    /// What: Provide a baseline `AppState` for preflight helper tests.
    ///
    /// Inputs: None
    ///
    /// Output: Fresh `AppState` with default values.
    fn new_app() -> AppState {
        AppState::default()
    }

    /// What: Create a test package item for testing.
    ///
    /// Inputs:
    /// - `name`: Package name
    ///
    /// Output: A test `PackageItem` with official source.
    fn test_package(name: &str) -> PackageItem {
        PackageItem {
            name: name.to_string(),
            version: "1.0.0".to_string(),
            description: "Test package".to_string(),
            source: crate::state::Source::Official {
                repo: "extra".to_string(),
                arch: "x86_64".to_string(),
            },
            popularity: None,
            out_of_date: None,
            orphaned: false,
        }
    }

    #[test]
    /// What: Verify that `trigger_background_resolution` sets resolution flags correctly.
    ///
    /// Inputs:
    /// - App state
    /// - Empty `dependency_info` and `cached_files`
    ///
    /// Output:
    /// - Resolution flags and items are set
    ///
    /// Details:
    /// - Tests that background resolution is properly triggered when caches are empty.
    fn trigger_background_resolution_sets_flags_when_cache_empty() {
        let mut app = new_app();
        let items = vec![test_package("test-pkg")];

        trigger_background_resolution(&mut app, &items, &[], &[]);

        // Flags should be set
        assert!(app.preflight_deps_resolving);
        assert!(app.preflight_files_resolving);
        assert!(app.preflight_services_resolving);
        // Items should be queued
        assert!(app.preflight_deps_items.is_some());
        assert!(app.preflight_files_items.is_some());
        assert!(app.preflight_services_items.is_some());
    }

    #[test]
    /// What: Verify that `trigger_background_resolution` does not set deps flag when cache has deps.
    ///
    /// Inputs:
    /// - App state
    /// - Non-empty `dependency_info`
    ///
    /// Output:
    /// - Deps resolution flag and items are not set
    ///
    /// Details:
    /// - Tests that existing cached deps prevent re-resolution.
    fn trigger_background_resolution_skips_deps_when_cached() {
        let mut app = new_app();
        let items = vec![test_package("test-pkg")];
        let cached_deps = vec![crate::state::modal::DependencyInfo {
            name: "cached-dep".to_string(),
            version: "1.0".to_string(),
            status: crate::state::modal::DependencyStatus::ToInstall,
            source: crate::state::modal::DependencySource::Official {
                repo: "extra".to_string(),
            },
            required_by: vec!["test-pkg".to_string()],
            depends_on: Vec::new(),
            is_core: false,
            is_system: false,
        }];

        trigger_background_resolution(&mut app, &items, &cached_deps, &[]);

        // Deps should not be triggered (cache has data)
        assert!(!app.preflight_deps_resolving);
        assert!(app.preflight_deps_items.is_none());
        // Files should still be triggered (cache empty)
        assert!(app.preflight_files_resolving);
        assert!(app.preflight_files_items.is_some());
    }

    #[test]
    /// What: Verify `create_preflight_modal_insert_mode` resets `preflight_cancelled` flag.
    ///
    /// Inputs:
    /// - App state with `preflight_cancelled` set to true
    ///
    /// Output:
    /// - `preflight_cancelled` is reset to false
    ///
    /// Details:
    /// - Tests the insert mode path resets the cancellation flag.
    fn create_preflight_modal_insert_mode_resets_cancelled() {
        let mut app = new_app();
        app.preflight_cancelled
            .store(true, std::sync::atomic::Ordering::Relaxed);
        let items = vec![test_package("test-pkg")];

        create_preflight_modal_insert_mode(&mut app, items);

        // Cancelled flag should be reset
        assert!(
            !app.preflight_cancelled
                .load(std::sync::atomic::Ordering::Relaxed)
        );
    }

    #[test]
    /// What: Verify `filter_cached_dependencies` returns only matching deps.
    ///
    /// Inputs:
    /// - App state with cached dependencies
    /// - Set of item names to filter by
    ///
    /// Output:
    /// - Only dependencies matching the item names are returned
    ///
    /// Details:
    /// - Tests that dependency filtering works correctly.
    fn filter_cached_dependencies_returns_matching() {
        let mut app = new_app();
        app.install_list_deps = vec![
            crate::state::modal::DependencyInfo {
                name: "dep-a".to_string(),
                version: "1.0".to_string(),
                status: crate::state::modal::DependencyStatus::ToInstall,
                source: crate::state::modal::DependencySource::Official {
                    repo: "extra".to_string(),
                },
                required_by: vec!["pkg-a".to_string()],
                depends_on: Vec::new(),
                is_core: false,
                is_system: false,
            },
            crate::state::modal::DependencyInfo {
                name: "dep-b".to_string(),
                version: "1.0".to_string(),
                status: crate::state::modal::DependencyStatus::ToInstall,
                source: crate::state::modal::DependencySource::Official {
                    repo: "extra".to_string(),
                },
                required_by: vec!["pkg-b".to_string()],
                depends_on: Vec::new(),
                is_core: false,
                is_system: false,
            },
        ];

        let mut item_names = std::collections::HashSet::new();
        item_names.insert("pkg-a".to_string());

        let result = filter_cached_dependencies(&app, &item_names);

        assert_eq!(result.len(), 1);
        assert_eq!(result[0].name, "dep-a");
    }

    #[test]
    /// What: Verify `filter_cached_files` returns only matching files.
    ///
    /// Inputs:
    /// - App state with cached file info
    /// - Set of item names to filter by
    ///
    /// Output:
    /// - Only file info matching the item names are returned
    ///
    /// Details:
    /// - Tests that file filtering works correctly.
    fn filter_cached_files_returns_matching() {
        let mut app = new_app();
        app.install_list_files = vec![
            crate::state::modal::PackageFileInfo {
                name: "pkg-a".to_string(),
                files: vec![crate::state::modal::FileChange {
                    path: "/usr/bin/a".to_string(),
                    change_type: crate::state::modal::FileChangeType::New,
                    package: "pkg-a".to_string(),
                    is_config: false,
                    predicted_pacnew: false,
                    predicted_pacsave: false,
                }],
                total_count: 1,
                new_count: 1,
                changed_count: 0,
                removed_count: 0,
                config_count: 0,
                pacnew_candidates: 0,
                pacsave_candidates: 0,
            },
            crate::state::modal::PackageFileInfo {
                name: "pkg-b".to_string(),
                files: vec![crate::state::modal::FileChange {
                    path: "/usr/bin/b".to_string(),
                    change_type: crate::state::modal::FileChangeType::New,
                    package: "pkg-b".to_string(),
                    is_config: false,
                    predicted_pacnew: false,
                    predicted_pacsave: false,
                }],
                total_count: 1,
                new_count: 1,
                changed_count: 0,
                removed_count: 0,
                config_count: 0,
                pacnew_candidates: 0,
                pacsave_candidates: 0,
            },
        ];

        let mut item_names = std::collections::HashSet::new();
        item_names.insert("pkg-a".to_string());

        let result = filter_cached_files(&app, &item_names);

        assert_eq!(result.len(), 1);
        assert_eq!(result[0].name, "pkg-a");
    }
}