revue 2.71.1

A Vue-style TUI framework for Rust with CSS styling
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
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
//! File picker widget
//!
//! Interactive file and directory browser with filtering and selection.
//!
//! # Example
//!
//! ```rust,ignore
//! use revue::widget::{FilePicker, FileFilter, file_picker};
//!
//! // Open file dialog
//! let picker = FilePicker::new()
//!     .title("Open File")
//!     .filter(FileFilter::extensions(&["rs", "toml"]))
//!     .show_hidden(false);
//!
//! // Save file dialog
//! let save = FilePicker::save()
//!     .default_name("untitled.rs");
//!
//! // Directory picker
//! let dir = FilePicker::directory();
//! ```

mod render;
mod types;
pub(crate) mod validation;

pub use types::{FileFilter, PickerEntry, PickerMode, PickerResult};
pub use validation::FilePickerError;

use crate::style::Color;
use crate::widget::theme::PLACEHOLDER_FG;
use crate::widget::WidgetProps;
use crate::{impl_props_builders, impl_styled_view};
use std::fs;
use std::path::{Path, PathBuf};
use validation::{validate_and_canonicalize, validate_path_no_traversal, validate_security_only};

/// File picker widget
#[derive(Clone, Debug)]
pub struct FilePicker {
    /// Current directory
    pub(crate) current_dir: PathBuf,
    /// Entries in current directory
    pub(crate) entries: Vec<PickerEntry>,
    /// Highlighted index
    pub(crate) highlighted: usize,
    /// Scroll offset
    pub(crate) scroll_offset: usize,
    /// Max visible items
    pub(crate) max_visible: usize,
    /// Picker mode
    pub(crate) mode: PickerMode,
    /// File filter
    pub(crate) filter: FileFilter,
    /// Show hidden files
    pub(crate) show_hidden: bool,
    /// Sort by name
    pub(crate) sort_by_name: bool,
    /// Directories first
    pub(crate) dirs_first: bool,
    /// Title
    pub(crate) title: Option<String>,
    /// Default filename (for save mode)
    pub(crate) default_name: Option<String>,
    /// Input filename (for save mode)
    pub(crate) input_name: String,
    /// Is inputting filename (for future save mode UI)
    pub(crate) _input_mode: bool,
    /// Confirm selection needed (for future save mode UI)
    pub(crate) _confirm_overwrite: bool,
    /// Width
    pub(crate) width: u16,
    /// History (visited directories)
    pub(crate) history: Vec<PathBuf>,
    /// History index
    pub(crate) history_idx: usize,
    /// Selected items (for multi-select)
    pub(crate) selected: Vec<PathBuf>,
    /// Foreground color
    pub(crate) fg: Option<Color>,
    /// Directory color
    pub(crate) dir_fg: Option<Color>,
    /// Hidden file color
    pub(crate) hidden_fg: Option<Color>,
    /// Widget properties
    pub(crate) props: WidgetProps,
}

impl FilePicker {
    /// Create a new file picker
    pub fn new() -> Self {
        let current_dir = std::env::current_dir().unwrap_or_else(|_| PathBuf::from("/"));

        let mut picker = Self {
            current_dir: current_dir.clone(),
            entries: Vec::new(),
            highlighted: 0,
            scroll_offset: 0,
            max_visible: 15,
            mode: PickerMode::Open,
            filter: FileFilter::All,
            show_hidden: false,
            sort_by_name: true,
            dirs_first: true,
            title: None,
            default_name: None,
            input_name: String::new(),
            _input_mode: false,
            _confirm_overwrite: true,
            width: 60,
            history: vec![current_dir],
            history_idx: 0,
            selected: Vec::new(),
            fg: None,
            dir_fg: Some(Color::BLUE),
            hidden_fg: Some(PLACEHOLDER_FG),
            props: WidgetProps::new(),
        };

        picker.refresh();
        picker
    }

    /// Create save file picker
    pub fn save() -> Self {
        Self::new().mode(PickerMode::Save).title("Save File")
    }

    /// Create directory picker
    pub fn directory() -> Self {
        Self::new()
            .mode(PickerMode::Directory)
            .filter(FileFilter::DirectoriesOnly)
            .title("Select Directory")
    }

    /// Create multi-select picker
    pub fn multi_select() -> Self {
        Self::new()
            .mode(PickerMode::MultiSelect)
            .title("Select Files")
    }

    /// Set mode
    pub fn mode(mut self, mode: PickerMode) -> Self {
        self.mode = mode;
        self
    }

    /// Set filter
    pub fn filter(mut self, filter: FileFilter) -> Self {
        self.filter = filter;
        self.refresh();
        self
    }

    /// Show/hide hidden files
    pub fn show_hidden(mut self, show: bool) -> Self {
        self.show_hidden = show;
        self.refresh();
        self
    }

    /// Set title
    pub fn title(mut self, title: impl Into<String>) -> Self {
        self.title = Some(title.into());
        self
    }

    /// Set default filename (for save mode)
    pub fn default_name(mut self, name: impl Into<String>) -> Self {
        let name = name.into();
        self.input_name = name.clone();
        self.default_name = Some(name);
        self
    }

    /// Set starting directory
    ///
    /// # Panics
    ///
    /// Panics if the path contains traversal patterns (../) or is invalid.
    /// Use `try_set_start_dir()` for a non-panicking version.
    pub fn start_dir(mut self, dir: impl AsRef<Path>) -> Self {
        let path = dir.as_ref();
        match validate_security_only(path) {
            Ok(validated) => {
                self.current_dir = validated;
                self.refresh();
            }
            Err(e) => {
                panic!("Invalid starting directory: {}", e);
            }
        }
        self
    }

    /// Set starting directory (non-panicking version)
    ///
    /// Returns error if the path contains traversal patterns or is invalid.
    pub fn try_set_start_dir(mut self, dir: impl AsRef<Path>) -> Result<Self, FilePickerError> {
        let path = dir.as_ref();
        let validated = validate_and_canonicalize(path, &self.current_dir)?;
        self.current_dir = validated;
        self.refresh();
        Ok(self)
    }

    /// Set width
    pub fn width(mut self, width: u16) -> Self {
        self.width = width;
        self
    }

    /// Set max visible items
    pub fn max_visible(mut self, max: usize) -> Self {
        self.max_visible = max;
        self
    }

    /// Refresh directory listing
    pub fn refresh(&mut self) {
        self.entries.clear();

        if let Ok(entries) = fs::read_dir(&self.current_dir) {
            for entry in entries.flatten() {
                let path = entry.path();

                // Skip symlinks for security (they could escape the allowed directory)
                // We check if the path is a symlink by comparing metadata
                if let Ok(metadata) = fs::symlink_metadata(&path) {
                    if metadata.file_type().is_symlink() {
                        // Resolve the symlink and check if it stays within current directory
                        match path.canonicalize() {
                            Ok(resolved) => {
                                // Only allow symlinks that stay within current directory
                                if !resolved.starts_with(&self.current_dir) {
                                    continue; // Skip symlinks that escape
                                }
                                // Use the resolved path for further processing
                            }
                            Err(_) => continue, // Skip broken symlinks
                        }
                    }
                }

                // Skip hidden if not showing
                let name = path.file_name().and_then(|n| n.to_str()).unwrap_or("");

                if !self.show_hidden && name.starts_with('.') {
                    continue;
                }

                // Apply filter (but always show directories in Open mode)
                let is_dir = path.is_dir();
                if !is_dir && !self.filter.matches(&path) {
                    continue;
                }

                if let Some(entry) = PickerEntry::from_path(&path) {
                    self.entries.push(entry);
                }
            }
        }

        // Sort
        self.entries.sort_by(|a, b| {
            if self.dirs_first {
                match (a.is_dir, b.is_dir) {
                    (true, false) => return std::cmp::Ordering::Less,
                    (false, true) => return std::cmp::Ordering::Greater,
                    _ => {}
                }
            }

            if self.sort_by_name {
                a.name.to_lowercase().cmp(&b.name.to_lowercase())
            } else {
                std::cmp::Ordering::Equal
            }
        });

        // Reset selection
        self.highlighted = 0;
        self.scroll_offset = 0;
    }

    /// Navigate to directory
    ///
    /// # Security
    ///
    /// The path is validated to prevent path traversal attacks.
    /// Returns error if the path contains traversal patterns or is outside allowed directory.
    pub fn navigate_to(&mut self, path: &Path) -> Result<(), FilePickerError> {
        // Always validate for path traversal, even if path doesn't exist
        validate_path_no_traversal(path)?;

        if !path.is_dir() {
            return Ok(()); // Not a directory, silently ignore
        }

        let validated = validate_and_canonicalize(path, &self.current_dir)?;
        self.current_dir = validated.clone();
        self.history.truncate(self.history_idx + 1);
        self.history.push(self.current_dir.clone());
        self.history_idx = self.history.len() - 1;
        self.refresh();
        Ok(())
    }

    /// Go to parent directory
    pub fn go_up(&mut self) {
        if let Some(parent) = self.current_dir.parent().map(Path::to_path_buf) {
            let _ = self.navigate_to(&parent); // Ignore errors, parent should be valid
        }
    }

    /// Go back in history
    pub fn go_back(&mut self) {
        if self.history_idx > 0 {
            self.history_idx -= 1;
            self.current_dir = self.history[self.history_idx].clone();
            self.refresh();
        }
    }

    /// Go forward in history
    pub fn go_forward(&mut self) {
        if self.history_idx < self.history.len() - 1 {
            self.history_idx += 1;
            self.current_dir = self.history[self.history_idx].clone();
            self.refresh();
        }
    }

    /// Move highlight up
    pub fn highlight_previous(&mut self) {
        if self.highlighted > 0 {
            self.highlighted -= 1;
            self.ensure_visible();
        }
    }

    /// Move highlight down
    pub fn highlight_next(&mut self) {
        if self.highlighted < self.entries.len().saturating_sub(1) {
            self.highlighted += 1;
            self.ensure_visible();
        }
    }

    /// Ensure highlighted item is visible
    fn ensure_visible(&mut self) {
        if self.highlighted < self.scroll_offset {
            self.scroll_offset = self.highlighted;
        } else if self.highlighted >= self.scroll_offset + self.max_visible {
            self.scroll_offset = self.highlighted - self.max_visible + 1;
        }
    }

    /// Enter selected item (directory or file)
    pub fn enter(&mut self) -> Option<PickerResult> {
        if let Some(entry) = self.entries.get(self.highlighted) {
            if entry.is_dir {
                let _ = self.navigate_to(&entry.path.clone()); // Ignore errors for valid entries
                None
            } else {
                match self.mode {
                    PickerMode::Open => Some(PickerResult::Selected(entry.path.clone())),
                    PickerMode::MultiSelect => {
                        self.toggle_selection();
                        None
                    }
                    _ => None,
                }
            }
        } else {
            None
        }
    }

    /// Toggle selection (for multi-select)
    pub fn toggle_selection(&mut self) {
        if let Some(entry) = self.entries.get_mut(self.highlighted) {
            if !entry.is_dir || self.mode == PickerMode::Directory {
                entry.selected = !entry.selected;

                if entry.selected {
                    self.selected.push(entry.path.clone());
                } else {
                    self.selected.retain(|p| p != &entry.path);
                }
            }
        }
    }

    /// Confirm selection
    ///
    /// Returns a canonicalized absolute path for the selected file(s).
    /// This ensures platform-independent consistent behavior.
    pub fn confirm(&self) -> PickerResult {
        match self.mode {
            PickerMode::Open | PickerMode::Directory => {
                if let Some(entry) = self.entries.get(self.highlighted) {
                    let valid_selection = (self.mode == PickerMode::Directory && entry.is_dir)
                        || (self.mode == PickerMode::Open && !entry.is_dir);
                    if valid_selection {
                        // Return canonicalized absolute path for consistent cross-platform behavior
                        let path = entry
                            .path
                            .canonicalize()
                            .unwrap_or_else(|_| entry.path.clone());
                        PickerResult::Selected(path)
                    } else {
                        PickerResult::None
                    }
                } else {
                    PickerResult::None
                }
            }
            PickerMode::Save => {
                if !self.input_name.is_empty() {
                    let path = self.current_dir.join(&self.input_name);
                    // For new files, try to canonicalize but fall back to joined path
                    let canonical = path.canonicalize().unwrap_or(path);
                    PickerResult::Selected(canonical)
                } else {
                    PickerResult::None
                }
            }
            PickerMode::MultiSelect => {
                if self.selected.is_empty() {
                    PickerResult::None
                } else {
                    // Canonicalize paths where possible, keep original on failure
                    // This prevents silent dropping of valid selections that just can't be canonicalized
                    let paths: Vec<PathBuf> = self
                        .selected
                        .iter()
                        .map(|p| p.canonicalize().unwrap_or_else(|_| p.clone()))
                        .collect();
                    if paths.is_empty() {
                        PickerResult::None
                    } else {
                        PickerResult::Multiple(paths)
                    }
                }
            }
        }
    }

    /// Get current directory
    pub fn current_dir(&self) -> &Path {
        &self.current_dir
    }

    /// Get highlighted entry
    pub fn highlighted_entry(&self) -> Option<&PickerEntry> {
        self.entries.get(self.highlighted)
    }

    /// Input character (for save mode)
    pub fn input_char(&mut self, c: char) {
        if self.mode == PickerMode::Save {
            self.input_name.push(c);
        }
    }

    /// Delete input character
    pub fn input_backspace(&mut self) {
        if self.mode == PickerMode::Save {
            self.input_name.pop();
        }
    }

    /// Toggle hidden files
    pub fn toggle_hidden(&mut self) {
        self.show_hidden = !self.show_hidden;
        self.refresh();
    }
}

impl Default for FilePicker {
    fn default() -> Self {
        Self::new()
    }
}

impl_styled_view!(FilePicker);
impl_props_builders!(FilePicker);

/// Create a file picker
pub fn file_picker() -> FilePicker {
    FilePicker::new()
}

/// Create a save file picker
pub fn save_picker() -> FilePicker {
    FilePicker::save()
}

/// Create a directory picker
pub fn dir_picker() -> FilePicker {
    FilePicker::directory()
}

// KEEP HERE - Private implementation tests (all tests access private fields: current_dir, mode, PickerEntry fields, etc.)

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

    #[test]
    fn test_file_picker_new() {
        let picker = FilePicker::new();
        assert!(picker.current_dir.exists());
        assert_eq!(picker.mode, PickerMode::Open);
    }

    #[test]
    fn test_file_picker_modes() {
        let open = FilePicker::new();
        assert_eq!(open.mode, PickerMode::Open);

        let save = FilePicker::save();
        assert_eq!(save.mode, PickerMode::Save);

        let dir = FilePicker::directory();
        assert_eq!(dir.mode, PickerMode::Directory);

        let multi = FilePicker::multi_select();
        assert_eq!(multi.mode, PickerMode::MultiSelect);
    }

    #[test]
    fn test_file_filter_extensions() {
        let filter = FileFilter::extensions(&["rs", "toml"]);

        assert!(filter.matches(Path::new("main.rs")));
        assert!(filter.matches(Path::new("Cargo.toml")));
        assert!(!filter.matches(Path::new("readme.md")));
    }

    #[test]
    fn test_file_filter_pattern() {
        let filter = FileFilter::pattern("*.rs");
        assert!(filter.matches(Path::new("main.rs")));
        assert!(!filter.matches(Path::new("main.py")));

        let filter2 = FileFilter::pattern("test*");
        assert!(filter2.matches(Path::new("test_main.rs")));
        assert!(!filter2.matches(Path::new("main_test.rs")));
    }

    #[test]
    fn test_picker_entry_format_size() {
        let mut entry = PickerEntry {
            path: PathBuf::from("test.txt"),
            name: "test.txt".to_string(),
            is_dir: false,
            is_hidden: false,
            size: 1024,
            selected: false,
        };

        assert_eq!(entry.format_size(), "1.0 KB");

        entry.size = 1024 * 1024;
        assert_eq!(entry.format_size(), "1.0 MB");

        entry.is_dir = true;
        assert_eq!(entry.format_size(), "<DIR>");
    }

    #[test]
    fn test_navigation() {
        let mut picker = FilePicker::new();
        let _initial_dir = picker.current_dir.clone();

        // These tests depend on filesystem, so just check basic operations
        picker.highlight_next();
        picker.highlight_previous();

        assert!(picker.history.len() >= 1);
    }

    #[test]
    fn test_save_mode_input() {
        let mut picker = FilePicker::save();
        picker.input_char('t');
        picker.input_char('e');
        picker.input_char('s');
        picker.input_char('t');
        assert_eq!(picker.input_name, "test");

        picker.input_backspace();
        assert_eq!(picker.input_name, "tes");
    }

    #[test]
    fn test_helper_functions() {
        let fp = file_picker();
        assert_eq!(fp.mode, PickerMode::Open);

        let sp = save_picker();
        assert_eq!(sp.mode, PickerMode::Save);

        let dp = dir_picker();
        assert_eq!(dp.mode, PickerMode::Directory);
    }

    // Security tests for path traversal

    #[test]
    fn test_reject_double_dot_slash() {
        let picker = FilePicker::new();
        let result = picker.try_set_start_dir("../../../etc/passwd");
        assert!(result.is_err());
        if let Err(FilePickerError::PathTraversal(_)) = result {
            // Expected
        } else {
            panic!("Expected PathTraversal error");
        }
    }

    #[test]
    fn test_reject_double_dot_backslash() {
        let picker = FilePicker::new();
        // On Unix, backslash is a valid filename character, not a path separator
        // On Windows, this would be detected as path traversal
        let result = picker.try_set_start_dir("..\\..\\system32");
        // On Unix: this is a valid path with weird backslashes, might not exist
        // On Windows: should be rejected as traversal
        #[cfg(windows)]
        {
            assert!(result.is_err());
            if let Err(FilePickerError::PathTraversal(_)) = result {
                // Expected
            } else {
                panic!("Expected PathTraversal error");
            }
        }
        #[cfg(not(windows))]
        {
            // On Unix, backslash is just a character - the path validation
            // will catch it via component check (.. is a ParentDir component)
            // We just check the function doesn't panic
            let _ = result;
        }
    }

    #[test]
    fn test_reject_parent_dir_component() {
        let picker = FilePicker::new();
        let mut picker = picker;
        let path = PathBuf::from("..").join("etc");
        let result = picker.navigate_to(&path);
        assert!(result.is_err());
    }

    #[test]
    fn test_navigate_to_rejects_traversal() {
        let mut picker = FilePicker::new();
        let traversal_path = Path::new("../../../etc");
        let result = picker.navigate_to(traversal_path);
        assert!(result.is_err());
    }

    #[test]
    fn test_allow_valid_paths() {
        let picker = FilePicker::new();
        // Valid absolute path should work (if it exists)
        if let Ok(current) = std::env::current_dir() {
            let result = picker.try_set_start_dir(&current);
            assert!(result.is_ok());
        }
    }

    #[test]
    fn test_allow_current_directory() {
        let picker = FilePicker::new();
        let result = picker.try_set_start_dir(".");
        assert!(result.is_ok());
    }

    #[test]
    fn test_go_up_stays_safe() {
        let mut picker = FilePicker::new();
        let _initial_dir = picker.current_dir().to_path_buf();
        picker.go_up();
        // go_up should always navigate to parent, which should be valid
        // (may or may not change directory depending on where we started)
    }

    #[test]
    fn test_enter_directory_safe() {
        let mut picker = FilePicker::new();
        // Navigate within the picker should be safe
        picker.highlight_next();
        if let Some(entry) = picker.highlighted_entry() {
            if entry.is_dir {
                let _initial_dir = picker.current_dir().to_path_buf();
                picker.enter();
                // Either we navigated or stayed, but shouldn't panic
            }
        }
    }

    #[test]
    fn test_path_traversal_error_message() {
        let picker = FilePicker::new();
        let result = picker.try_set_start_dir("../../../etc/passwd");
        if let Err(e) = result {
            let msg = format!("{}", e);
            assert!(
                msg.contains("traversal") || msg.contains("parent"),
                "Error message should mention traversal: {}",
                msg
            );
        } else {
            panic!("Expected error for path traversal");
        }
    }

    #[test]
    fn test_outside_directory_error() {
        let mut picker = FilePicker::new();
        // Try to navigate to a path outside the current directory tree
        // This test is platform-dependent, so we just check the function doesn't panic
        let outside_path = Path::new("/tmp/revue_test_nonexistent_outside");
        let _result = picker.navigate_to(outside_path);
        // Should either succeed (if path exists) or fail with appropriate error
        // but should not panic
    }

    // Additional security hardening tests

    #[test]
    fn test_reject_null_byte_in_path() {
        let picker = FilePicker::new();
        // Path with null byte should be rejected
        let null_path = Path::new("test.txt\0malicious.exe");
        let result = picker.try_set_start_dir(null_path);
        assert!(result.is_err());
        if let Err(FilePickerError::InvalidCharacters) = result {
            // Expected
        } else {
            panic!("Expected InvalidCharacters error for null byte");
        }
    }

    #[test]
    fn test_reject_windows_reserved_names() {
        // Test Windows reserved device names
        #[cfg(windows)]
        {
            let reserved_names = ["CON", "PRN", "AUX", "NUL", "COM1", "LPT1"];
            for name in reserved_names {
                let picker = FilePicker::new();
                let path = PathBuf::from("/tmp").join(name);
                // try_set_start_dir returns Result - just verify it doesn't panic
                // The validation should handle reserved names gracefully
                let _ = picker.try_set_start_dir(&path);
            }
        }
        #[cfg(not(windows))]
        {
            // On non-Windows, this test is not applicable
            // Just verify that FilePicker creation doesn't panic
            let _picker = FilePicker::new();
        }
    }

    #[test]
    fn test_truncate_string_safe_utf8() {
        use validation::truncate_string_safe;

        // Test UTF-8 safe truncation
        let ascii = "hello_world_test.txt";
        let truncated = truncate_string_safe(ascii, 10);
        // truncate_string_safe adds "..." so result will be at most max_len + 3
        assert!(truncated.ends_with("..."));
        assert!(truncated.len() <= 13); // 10 + "..."
    }

    #[test]
    fn test_truncate_string_single_emoji() {
        use validation::truncate_string_safe;

        // Emoji is 4 bytes in UTF-8
        let emoji = "😀😀😀😀😀";
        let truncated = truncate_string_safe(emoji, 10);
        // Should handle gracefully without panic
        assert!(truncated.ends_with("..."));
    }

    #[test]
    fn test_multiselect_preserves_paths_on_canonicalize_failure() {
        let mut picker = FilePicker::multi_select();
        picker
            .selected
            .push(PathBuf::from("/nonexistent/path/file1.txt"));
        picker
            .selected
            .push(PathBuf::from("/nonexistent/path/file2.txt"));

        let result = picker.confirm();
        match result {
            PickerResult::Multiple(paths) => {
                // Should return the original paths even if canonicalize fails
                assert_eq!(paths.len(), 2);
            }
            _ => panic!("Expected Multiple with preserved paths"),
        }
    }
}