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
use tokio::sync::mpsc;

use crate::state::AppState;

/// What: Log detailed dependency information for `SandboxInfo` entries.
///
/// Inputs:
/// - `sandbox_info`: `SandboxInfo` resolution results to log
///
/// Output: None (side effect: logging)
///
/// Details:
/// - Logs summary and per-package dependency counts
fn log_sandbox_info_details(sandbox_info: &[crate::logic::sandbox::SandboxInfo]) {
    if sandbox_info.is_empty() {
        tracing::warn!("[Runtime] handle_sandbox_result: Received empty sandbox info");
        return;
    }

    tracing::info!(
        "[Runtime] handle_sandbox_result: Received {} sandbox info entries",
        sandbox_info.len()
    );

    for info in sandbox_info {
        let total_deps = info.depends.len()
            + info.makedepends.len()
            + info.checkdepends.len()
            + info.optdepends.len();
        let installed_deps = info.depends.iter().filter(|d| d.is_installed).count()
            + info.makedepends.iter().filter(|d| d.is_installed).count()
            + info.checkdepends.iter().filter(|d| d.is_installed).count()
            + info.optdepends.iter().filter(|d| d.is_installed).count();
        tracing::info!(
            "[Runtime] handle_sandbox_result: Package '{}' - total_deps={}, installed_deps={}, depends={}, makedepends={}, checkdepends={}, optdepends={}",
            info.package_name,
            total_deps,
            installed_deps,
            info.depends.len(),
            info.makedepends.len(),
            info.checkdepends.len(),
            info.optdepends.len()
        );
    }
}

/// What: Check if `SandboxInfo` is empty (all dependency vectors are empty).
///
/// Inputs:
/// - `info`: `SandboxInfo` to check
///
/// Output: `true` if all dependency vectors are empty, `false` otherwise
const fn is_empty_sandbox(info: &crate::logic::sandbox::SandboxInfo) -> bool {
    info.depends.is_empty()
        && info.makedepends.is_empty()
        && info.checkdepends.is_empty()
        && info.optdepends.is_empty()
}

/// What: Merge new `SandboxInfo` into existing cache, preserving valid entries.
///
/// Inputs:
/// - `current_cache`: Current cached `SandboxInfo`
/// - `new_info`: New sandbox resolution results
///
/// Output: Updated sandbox cache with merged entries
///
/// Details:
/// - Preserves entries for packages not in the new result
/// - Preserves existing valid entries if new entry is empty
/// - Replaces entries when new data is available
fn merge_sandbox_cache(
    current_cache: &[crate::logic::sandbox::SandboxInfo],
    new_info: &[crate::logic::sandbox::SandboxInfo],
) -> Vec<crate::logic::sandbox::SandboxInfo> {
    let mut updated_sandbox = current_cache.to_vec();
    let new_package_names: std::collections::HashSet<String> =
        new_info.iter().map(|s| s.package_name.clone()).collect();

    // Extract existing valid entries for packages that will be updated
    let mut existing_valid: std::collections::HashMap<String, crate::logic::sandbox::SandboxInfo> =
        updated_sandbox
            .iter()
            .filter(|s| new_package_names.contains(&s.package_name))
            .filter(|s| !is_empty_sandbox(s))
            .map(|s| (s.package_name.clone(), s.clone()))
            .collect();

    // Remove old entries for packages that are in the new result
    updated_sandbox.retain(|s| !new_package_names.contains(&s.package_name));

    // Add new entries, preserving existing valid data if new entry is empty
    for new_entry in new_info {
        if is_empty_sandbox(new_entry) {
            if let Some(existing) = existing_valid.remove(&new_entry.package_name) {
                tracing::debug!(
                    "[Runtime] handle_sandbox_result: Preserving existing valid sandbox info for '{}' (new entry is empty)",
                    new_entry.package_name
                );
                updated_sandbox.push(existing);
            } else {
                updated_sandbox.push(new_entry.clone());
            }
        } else {
            updated_sandbox.push(new_entry.clone());
        }
    }

    updated_sandbox
}

/// What: Sync `SandboxInfo` to preflight modal if open.
///
/// Inputs:
/// - `modal`: Preflight modal state
/// - `sandbox_info`: `SandboxInfo` resolution results
///
/// Output: None (side effect: updates modal state)
///
/// Details:
/// - Filters `SandboxInfo` to match modal items
/// - Handles empty results and mismatches gracefully
/// - Sets appropriate error messages when needed
fn sync_sandbox_to_modal(
    modal: &mut crate::state::Modal,
    sandbox_info: &[crate::logic::sandbox::SandboxInfo],
) {
    let crate::state::Modal::Preflight {
        items,
        sandbox_info: modal_sandbox,
        sandbox_loaded,
        sandbox_error,
        ..
    } = modal
    else {
        tracing::debug!("[Runtime] handle_sandbox_result: Preflight modal not open, skipping sync");
        return;
    };

    let item_names: std::collections::HashSet<String> =
        items.iter().map(|i| i.name.clone()).collect();
    let aur_items: Vec<_> = items
        .iter()
        .filter(|p| matches!(p.source, crate::state::Source::Aur))
        .collect();
    let filtered_sandbox: Vec<_> = sandbox_info
        .iter()
        .filter(|sb| item_names.contains(&sb.package_name))
        .cloned()
        .collect();

    tracing::info!(
        "[Runtime] handle_sandbox_result: Modal open - items={}, aur_items={}, filtered_sandbox={}, modal_current={}",
        items.len(),
        aur_items.len(),
        filtered_sandbox.len(),
        modal_sandbox.len()
    );

    if !filtered_sandbox.is_empty() {
        sync_matching_sandbox(
            modal_sandbox,
            sandbox_loaded,
            sandbox_error,
            filtered_sandbox,
        );
        return;
    }

    sync_empty_or_mismatched_sandbox(
        sandbox_info,
        &item_names,
        aur_items.as_slice(),
        modal_sandbox,
        sandbox_loaded,
        sandbox_error,
    );
}

/// What: Sync matching `SandboxInfo` to modal.
///
/// Inputs:
/// - `modal_sandbox`: Modal `SandboxInfo` field to update
/// - `sandbox_loaded`: Modal loaded flag to update
/// - `sandbox_error`: Modal error field to update
/// - `filtered_sandbox`: Matching `SandboxInfo` to sync
///
/// Output: None (side effect: updates modal fields)
fn sync_matching_sandbox(
    modal_sandbox: &mut Vec<crate::logic::sandbox::SandboxInfo>,
    sandbox_loaded: &mut bool,
    sandbox_error: &mut Option<String>,
    filtered_sandbox: Vec<crate::logic::sandbox::SandboxInfo>,
) {
    tracing::info!(
        "[Runtime] handle_sandbox_result: Syncing {} sandbox infos to preflight modal",
        filtered_sandbox.len()
    );
    *modal_sandbox = filtered_sandbox;
    *sandbox_loaded = true;
    *sandbox_error = None;
    tracing::debug!(
        "[Runtime] handle_sandbox_result: Successfully synced sandbox info to modal, loaded={}",
        *sandbox_loaded
    );
}

/// What: Handle empty or mismatched `SandboxInfo` for modal sync.
///
/// Inputs:
/// - `sandbox_info`: All sandbox resolution results
/// - `item_names`: Names of items in the modal
/// - `aur_items`: AUR items in the modal
/// - `modal_sandbox`: Modal `SandboxInfo` field to update
/// - `sandbox_loaded`: Modal loaded flag to update
/// - `sandbox_error`: Modal error field to update
///
/// Output: None (side effect: updates modal fields)
///
/// Details:
/// - Handles cases where `SandboxInfo` doesn't match modal items
/// - Sets appropriate error messages for empty results
fn sync_empty_or_mismatched_sandbox(
    sandbox_info: &[crate::logic::sandbox::SandboxInfo],
    item_names: &std::collections::HashSet<String>,
    aur_items: &[&crate::state::PackageItem],
    modal_sandbox: &mut Vec<crate::logic::sandbox::SandboxInfo>,
    sandbox_loaded: &mut bool,
    sandbox_error: &mut Option<String>,
) {
    if aur_items.is_empty() {
        *sandbox_loaded = true;
        *sandbox_error = None;
        return;
    }

    if sandbox_info.is_empty() {
        handle_empty_sandbox_result(aur_items, sandbox_loaded, sandbox_error);
    } else {
        handle_partial_match(
            sandbox_info,
            item_names,
            modal_sandbox,
            sandbox_loaded,
            sandbox_error,
        );
    }
}

/// What: Handle partial match between `SandboxInfo` and modal items.
///
/// Inputs:
/// - `sandbox_info`: All sandbox resolution results
/// - `item_names`: Names of items in the modal
/// - `modal_sandbox`: Modal `SandboxInfo` field to update
/// - `sandbox_loaded`: Modal loaded flag to update
/// - `sandbox_error`: Modal error field to update
///
/// Output: None (side effect: updates modal fields)
fn handle_partial_match(
    sandbox_info: &[crate::logic::sandbox::SandboxInfo],
    item_names: &std::collections::HashSet<String>,
    modal_sandbox: &mut Vec<crate::logic::sandbox::SandboxInfo>,
    sandbox_loaded: &mut bool,
    sandbox_error: &mut Option<String>,
) {
    let partial_match: Vec<_> = sandbox_info
        .iter()
        .filter(|sb| item_names.contains(&sb.package_name))
        .cloned()
        .collect();

    if partial_match.is_empty() {
        tracing::warn!(
            "[Runtime] Sandbox info exists but doesn't match modal items. Modal items: {:?}, Sandbox packages: {:?}",
            item_names,
            sandbox_info
                .iter()
                .map(|s| &s.package_name)
                .collect::<Vec<_>>()
        );
    } else {
        tracing::debug!(
            "[Runtime] Partial sandbox sync: {} of {} packages matched",
            partial_match.len(),
            item_names.len()
        );
        *modal_sandbox = partial_match;
    }
    *sandbox_loaded = true;
    *sandbox_error = None;
}

/// What: Handle empty sandbox result when AUR packages are expected.
///
/// Inputs:
/// - `aur_items`: AUR items in the modal
/// - `sandbox_loaded`: Modal loaded flag to update
/// - `sandbox_error`: Modal error field to update
///
/// Output: None (side effect: updates modal fields)
fn handle_empty_sandbox_result(
    aur_items: &[&crate::state::PackageItem],
    sandbox_loaded: &mut bool,
    sandbox_error: &mut Option<String>,
) {
    tracing::warn!(
        "[Runtime] handle_sandbox_result: Sandbox resolution returned empty results for {} AUR packages (AUR may be down or network issues). Modal items: {:?}",
        aur_items.len(),
        aur_items.iter().map(|i| &i.name).collect::<Vec<_>>()
    );
    *sandbox_loaded = true;
    *sandbox_error = Some(format!(
        "Failed to fetch sandbox information for {} AUR package(s). AUR may be temporarily unavailable.",
        aur_items.len()
    ));
}

use crate::app::runtime::handlers::common::{HandlerConfig, handle_result};

/// What: Handler configuration for sandbox results.
struct SandboxHandlerConfig;

impl HandlerConfig for SandboxHandlerConfig {
    type Result = crate::logic::sandbox::SandboxInfo;

    fn get_resolving(&self, app: &AppState) -> bool {
        app.sandbox_resolving
    }

    fn set_resolving(&self, app: &mut AppState, value: bool) {
        app.sandbox_resolving = value;
    }

    fn get_preflight_resolving(&self, app: &AppState) -> bool {
        app.preflight_sandbox_resolving
    }

    fn set_preflight_resolving(&self, app: &mut AppState, value: bool) {
        app.preflight_sandbox_resolving = value;
    }

    fn stage_name(&self) -> &'static str {
        "sandbox"
    }

    fn update_cache(&self, app: &mut AppState, results: &[Self::Result]) {
        log_sandbox_info_details(results);

        tracing::debug!(
            "[Runtime] handle_sandbox_result: Updating install_list_sandbox with {} entries (current cache has {})",
            results.len(),
            app.install_list_sandbox.len()
        );

        app.install_list_sandbox = merge_sandbox_cache(&app.install_list_sandbox, results);

        tracing::debug!(
            "[Runtime] handle_sandbox_result: install_list_sandbox now has {} entries: {:?}",
            app.install_list_sandbox.len(),
            app.install_list_sandbox
                .iter()
                .map(|s| &s.package_name)
                .collect::<Vec<_>>()
        );
    }

    fn set_cache_dirty(&self, app: &mut AppState) {
        app.sandbox_cache_dirty = true;
        tracing::debug!(
            "[Runtime] handle_sandbox_result: Marked sandbox_cache_dirty=true, install_list_sandbox has {} entries: {:?}",
            app.install_list_sandbox.len(),
            app.install_list_sandbox
                .iter()
                .map(|s| &s.package_name)
                .collect::<Vec<_>>()
        );
    }

    fn clear_preflight_items(&self, app: &mut AppState) {
        app.preflight_sandbox_items = None;
    }

    fn sync_to_modal(&self, app: &mut AppState, results: &[Self::Result], _was_preflight: bool) {
        sync_sandbox_to_modal(&mut app.modal, results);
    }

    fn log_flag_clear(&self, app: &AppState, was_preflight: bool, cancelled: bool) {
        tracing::debug!(
            "[Runtime] handle_sandbox_result: Clearing flags - was_preflight={}, sandbox_resolving={}, preflight_sandbox_resolving={}, cancelled={}",
            was_preflight,
            self.get_resolving(app),
            app.preflight_sandbox_resolving,
            cancelled
        );
    }

    fn is_resolution_complete(&self, app: &AppState, results: &[Self::Result]) -> bool {
        // Check if preflight modal is open
        if let crate::state::Modal::Preflight { items, .. } = &app.modal {
            // Only AUR packages need sandbox data
            let aur_items: std::collections::HashSet<String> = items
                .iter()
                .filter(|p| matches!(p.source, crate::state::Source::Aur))
                .map(|i| i.name.clone())
                .collect();

            if aur_items.is_empty() {
                // No AUR packages, resolution is complete
                return true;
            }

            let result_names: std::collections::HashSet<String> =
                results.iter().map(|s| s.package_name.clone()).collect();
            let cache_names: std::collections::HashSet<String> = app
                .install_list_sandbox
                .iter()
                .map(|s| s.package_name.clone())
                .collect();

            let all_have_data = aur_items
                .iter()
                .all(|name| result_names.contains(name) || cache_names.contains(name));

            if !all_have_data {
                let missing: Vec<String> = aur_items
                    .iter()
                    .filter(|name| !result_names.contains(*name) && !cache_names.contains(*name))
                    .cloned()
                    .collect();
                tracing::debug!(
                    "[Runtime] handle_sandbox_result: Resolution incomplete - missing sandbox for: {:?}",
                    missing
                );
            }

            return all_have_data;
        }

        // If no preflight modal, check preflight_sandbox_items
        if let Some(ref install_items) = app.preflight_sandbox_items {
            // Only AUR packages need sandbox data
            let aur_items: std::collections::HashSet<String> = install_items
                .iter()
                .filter(|p| matches!(p.source, crate::state::Source::Aur))
                .map(|i| i.name.clone())
                .collect();

            if aur_items.is_empty() {
                // No AUR packages, resolution is complete
                return true;
            }

            let result_names: std::collections::HashSet<String> =
                results.iter().map(|s| s.package_name.clone()).collect();
            let cache_names: std::collections::HashSet<String> = app
                .install_list_sandbox
                .iter()
                .map(|s| s.package_name.clone())
                .collect();

            let all_have_data = aur_items
                .iter()
                .all(|name| result_names.contains(name) || cache_names.contains(name));

            if !all_have_data {
                let missing: Vec<String> = aur_items
                    .iter()
                    .filter(|name| !result_names.contains(*name) && !cache_names.contains(*name))
                    .cloned()
                    .collect();
                tracing::debug!(
                    "[Runtime] handle_sandbox_result: Resolution incomplete - missing sandbox for: {:?}",
                    missing
                );
            }

            return all_have_data;
        }

        // No items to check, resolution is complete
        true
    }
}

/// What: Handle sandbox resolution result event.
///
/// Inputs:
/// - `app`: Application state
/// - `sandbox_info`: `SandboxInfo` resolution results
/// - `tick_tx`: Channel sender for tick events
///
/// Details:
/// - Updates cached `SandboxInfo`
/// - Syncs `SandboxInfo` to preflight modal if open
/// - Handles empty results and errors gracefully
/// - Respects cancellation flag
pub fn handle_sandbox_result(
    app: &mut AppState,
    sandbox_info: &[crate::logic::sandbox::SandboxInfo],
    tick_tx: &mpsc::UnboundedSender<()>,
) {
    handle_result(app, sandbox_info, tick_tx, &SandboxHandlerConfig);
}

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

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

    #[test]
    /// What: Verify that `handle_sandbox_result` updates cache correctly.
    ///
    /// Inputs:
    /// - App state
    /// - Sandbox resolution results
    ///
    /// Output:
    /// - `SandboxInfo` is cached
    /// - Flags are reset
    ///
    /// Details:
    /// - Tests that sandbox results are properly processed
    fn handle_sandbox_result_updates_cache() {
        let mut app = new_app();
        app.sandbox_resolving = true;

        let (tick_tx, _tick_rx) = mpsc::unbounded_channel();

        let sandbox_info = vec![crate::logic::sandbox::SandboxInfo {
            package_name: "test-package".to_string(),
            depends: vec![],
            makedepends: vec![],
            checkdepends: vec![],
            optdepends: vec![],
        }];

        handle_sandbox_result(&mut app, &sandbox_info, &tick_tx);

        // `SandboxInfo` should be cached
        assert_eq!(app.install_list_sandbox.len(), 1);
        // Flags should be reset
        assert!(!app.sandbox_resolving);
        assert!(!app.preflight_sandbox_resolving);
        // Cache dirty flag should be set
        assert!(app.sandbox_cache_dirty);
    }
}