#[cfg(test)]
mod tests {
    use std::collections::HashSet;
    use std::fs;
    use tempfile::tempdir;
    use std::sync::Arc;
    use arc_swap::ArcSwap;

    use crate::views::file_selector::FileSelector;
    use crate::file_index::SharedFileIndex;
    use crate::views::View;

    #[test]
    fn test_file_selector_reveal_flow() -> anyhow::Result<()> {
        let td = tempdir()?;
        let root = td.path().join("project");
        fs::create_dir_all(root.join("a/b/c"))?;
        fs::write(root.join("a/b/c/file.rs"), "fn main() {}")?;

        // Build a minimal SharedFileIndex (None — index not built)
        let shared: SharedFileIndex = Arc::new(ArcSwap::from_pointee(None));

        // Create FileSelector with initial_path set to the deep file
        let initial = Some(root.join("a/b/c/file.rs"));
        let mut fs_view = FileSelector::new(root.clone(), &[], shared, HashSet::new(), initial.clone());

        // Ensure pending_load is set (top-level 'a' needs loading)
        assert!(fs_view.pending_load.is_some());

        // Prepare settings for handle_operation (not used heavily by FileSelector)
        let settings = crate::settings::Settings::new(&root.join(".oo/config.yaml"))?;

        // Loop: while pending_load set, simulate app reading directory and delivering DirLoaded
        while let Some(dir) = fs_view.pending_load.take() {
            // Read entries for dir as (abs_path, is_dir)
            let mut entries = vec![];
            for entry in fs::read_dir(&dir)? {
                let e = entry?;
                let p = e.path();
                entries.push((p.clone(), p.is_dir()));
            }
            // Apply DirLoaded operation
            let op = crate::operation::FileSelectorOp::DirLoaded { path: dir.clone(), entries };
            let _ = fs_view.handle_operation(&crate::operation::Operation::FileSelectorLocal(op), &settings);
        }

        // After all lazy loads, the tree should have the target selected internally (focus remains on filter)
        let sel = fs_view.tree_selected_file();
        assert_eq!(sel, initial);
        Ok(())
    }
}
