background-picker 0.1.0

Allow user to select a background from a directory hiearchy
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
use background_picker::{Args, BackgroundPickerApp};
use clap::Parser;
use std::fs;
use tempfile::TempDir;
use serial_test::serial;

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

    fn create_comprehensive_test_structure(base_dir: &std::path::Path) -> std::io::Result<()> {
        // Create multiple directories with various image types
        fs::create_dir_all(base_dir.join("Photos/Vacation"))?;
        fs::create_dir_all(base_dir.join("Photos/Family"))?;
        fs::create_dir_all(base_dir.join("Wallpapers/Nature"))?;
        fs::create_dir_all(base_dir.join("Wallpapers/Abstract"))?;
        fs::create_dir_all(base_dir.join("Screenshots"))?;
        
        // Create root level images
        fs::write(base_dir.join("desktop_bg.jpg"), create_fake_jpeg_data())?;
        fs::write(base_dir.join("logo.png"), create_fake_png_data())?;
        
        // Create vacation photos
        fs::write(base_dir.join("Photos/Vacation/beach1.jpg"), create_fake_jpeg_data())?;
        fs::write(base_dir.join("Photos/Vacation/beach2.jpeg"), create_fake_jpeg_data())?;
        fs::write(base_dir.join("Photos/Vacation/sunset.png"), create_fake_png_data())?;
        
        // Create family photos
        fs::write(base_dir.join("Photos/Family/portrait.jpg"), create_fake_jpeg_data())?;
        fs::write(base_dir.join("Photos/Family/group.gif"), b"GIF89a fake gif data")?;
        
        // Create wallpapers
        fs::write(base_dir.join("Wallpapers/Nature/forest.jpg"), create_fake_jpeg_data())?;
        fs::write(base_dir.join("Wallpapers/Nature/mountains.png"), create_fake_png_data())?;
        fs::write(base_dir.join("Wallpapers/Abstract/geometric.webp"), b"RIFF fake webp")?;
        fs::write(base_dir.join("Wallpapers/Abstract/colors.bmp"), create_fake_bmp_data())?;
        
        // Create screenshots
        fs::write(base_dir.join("Screenshots/screen1.png"), create_fake_png_data())?;
        fs::write(base_dir.join("Screenshots/screen2.jpg"), create_fake_jpeg_data())?;
        
        // Create non-image files to test filtering
        fs::write(base_dir.join("readme.txt"), b"This is not an image")?;
        fs::write(base_dir.join("Photos/video.mp4"), b"fake video data")?;
        fs::write(base_dir.join("config.yaml"), b"configuration: data")?;
        
        Ok(())
    }

    fn create_fake_jpeg_data() -> Vec<u8> {
        // Minimal JPEG header
        vec![0xFF, 0xD8, 0xFF, 0xE0, 0x00, 0x10, 0x4A, 0x46, 0x49, 0x46]
    }

    fn create_fake_png_data() -> Vec<u8> {
        // PNG signature
        vec![0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A]
    }

    fn create_fake_bmp_data() -> Vec<u8> {
        // BMP header
        vec![0x42, 0x4D] // "BM"
    }

    #[test]
    #[serial]
    fn test_end_to_end_image_scanning_and_organization() {
        let temp_dir = TempDir::new().unwrap();
        create_comprehensive_test_structure(temp_dir.path()).unwrap();
        
        let mut args = Args::try_parse_from(["background-picker"]).unwrap();
        args.directory = temp_dir.path().to_path_buf();
        args.debug = true;
        args.thumbnail_size = 128;
        
        // Create a minimal app for testing
        let (sender, _receiver) = std::sync::mpsc::channel();
        let thread_pool = rayon::ThreadPoolBuilder::new()
            .num_threads(2)
            .build()
            .unwrap();
        
        let mut app = BackgroundPickerApp {
            args: args.clone(),
            images: std::sync::Arc::new(std::sync::RwLock::new(Vec::new())),
            folder_tree: std::collections::HashMap::new(),
            loading: true,
            thumbnail_sender: sender,
            thumbnail_receiver: _receiver,
            thread_pool,
            cache_dir: temp_dir.path().join("cache"),
        };
        
        let _ = app.scan_images();
        
        let images = app.images.read().unwrap();
        
        // Should find 13 image files (excluding txt, mp4, yaml)
        assert_eq!(images.len(), 13);
        
        // Verify folder structure
        assert_eq!(app.folder_tree.len(), 6); // Root + 5 subdirectories
        
        // Check root directory
        if let Some(root_images) = app.folder_tree.get(".") {
            assert_eq!(root_images.len(), 2); // desktop_bg.jpg, logo.png
        }
        
        // Check Photos/Vacation
        let vacation_images = app.folder_tree.get("Photos/Vacation").unwrap();
        assert_eq!(vacation_images.len(), 3); // beach1.jpg, beach2.jpeg, sunset.png
        
        // Check Photos/Family
        let family_images = app.folder_tree.get("Photos/Family").unwrap();
        assert_eq!(family_images.len(), 2); // portrait.jpg, group.gif
        
        // Check Wallpapers/Nature
        let nature_images = app.folder_tree.get("Wallpapers/Nature").unwrap();
        assert_eq!(nature_images.len(), 2); // forest.jpg, mountains.png
        
        // Check Wallpapers/Abstract
        let abstract_images = app.folder_tree.get("Wallpapers/Abstract").unwrap();
        assert_eq!(abstract_images.len(), 2); // geometric.webp, colors.bmp
        
        // Check Screenshots
        let screenshot_images = app.folder_tree.get("Screenshots").unwrap();
        assert_eq!(screenshot_images.len(), 2); // screen1.png, screen2.jpg
        
        // Verify all images have correct relative paths
        for (i, image) in images.iter().enumerate() {
            assert!(!image.relative_path.is_empty());
            assert!(image.path.exists());
            assert!(!image.loading); // Should not be loading after scan
            assert!(image.thumbnail.is_none()); // Thumbnails not loaded yet
            
            println!("Image {}: {} -> {}", i, image.relative_path, image.path.display());
        }
        
        assert!(!app.loading);
    }

    #[test]
    #[serial]
    fn test_selected_image_persistence_workflow() {
        let temp_dir = TempDir::new().unwrap();
        let selected_file = temp_dir.path().join("selected_image.txt");
        
        let mut args = Args::try_parse_from(["background-picker"]).unwrap();
        args.selected_image_file = selected_file.clone();
        
        let (sender, receiver) = std::sync::mpsc::channel();
        let thread_pool = rayon::ThreadPoolBuilder::new()
            .num_threads(1)
            .build()
            .unwrap();
        
        let app = BackgroundPickerApp {
            args,
            images: std::sync::Arc::new(std::sync::RwLock::new(Vec::new())),
            folder_tree: std::collections::HashMap::new(),
            loading: false,
            thumbnail_sender: sender,
            thumbnail_receiver: receiver,
            thread_pool,
            cache_dir: temp_dir.path().join("cache"),
        };
        
        // Save a selected image
        let first_image = std::path::PathBuf::from("/path/to/vacation/beach.jpg");
        app.save_selected_image(&first_image).unwrap();
        
        // Verify the file was created and contains the correct path
        assert!(selected_file.exists());
        let content = fs::read_to_string(&selected_file).unwrap();
        assert_eq!(content.trim(), first_image.to_string_lossy());
        
        // Update with a new selection
        let second_image = std::path::PathBuf::from("/path/to/nature/forest.jpg");
        app.save_selected_image(&second_image).unwrap();
        
        // Verify the file was updated
        let updated_content = fs::read_to_string(&selected_file).unwrap();
        assert_eq!(updated_content.trim(), second_image.to_string_lossy());
    }

    #[test]
    #[serial]
    fn test_thumbnail_caching_workflow() {
        let temp_dir = TempDir::new().unwrap();
        create_comprehensive_test_structure(temp_dir.path()).unwrap();
        
        let cache_dir = temp_dir.path().join("thumbnail_cache");
        let test_image = temp_dir.path().join("desktop_bg.jpg");
        
        // Test initial thumbnail generation
        let thumbnail_size = 150;
        let result1 = BackgroundPickerApp::load_or_generate_thumbnail(
            &test_image,
            thumbnail_size,
            &cache_dir,
            false
        );
        
        // The fake JPEG data might not be processable, so we'll handle both cases
        if result1.is_none() {
            println!("Thumbnail generation failed for fake image data - this is expected");
            return; // Skip rest of test if we can't generate thumbnails from fake data
        }
        
        assert!(result1.is_some());
        let thumbnail1 = result1.unwrap();
        assert_eq!(thumbnail1.size[0], thumbnail_size as usize);
        assert_eq!(thumbnail1.size[1], thumbnail_size as usize);
        
        // Verify cache file was created
        let cache_path = BackgroundPickerApp::get_cached_thumbnail_path_static(&test_image, &cache_dir);
        assert!(cache_path.is_some());
        let cache_file = cache_path.unwrap();
        assert!(cache_file.exists(), "Cache file should exist at {:?}", cache_file);
        
        // Test loading from cache
        let result2 = BackgroundPickerApp::load_or_generate_thumbnail(
            &test_image,
            thumbnail_size,
            &cache_dir,
            false
        );
        
        assert!(result2.is_some());
        let thumbnail2 = result2.unwrap();
        assert_eq!(thumbnail2.size[0], thumbnail_size as usize);
        assert_eq!(thumbnail2.size[1], thumbnail_size as usize);
        
        // Test cache validation
        assert!(BackgroundPickerApp::is_thumbnail_cache_valid_static(&test_image, &cache_file));
        
        // Test different thumbnail size
        let different_size = 64;
        let result3 = BackgroundPickerApp::load_or_generate_thumbnail(
            &test_image,
            different_size,
            &cache_dir,
            false
        );
        
        assert!(result3.is_some());
        let thumbnail3 = result3.unwrap();
        assert_eq!(thumbnail3.size[0], different_size as usize);
        assert_eq!(thumbnail3.size[1], different_size as usize);
    }

    #[test]
    #[serial]
    fn test_command_execution_workflow() {
        let temp_dir = TempDir::new().unwrap();
        let test_image = temp_dir.path().join("test.jpg");
        fs::write(&test_image, create_fake_jpeg_data()).unwrap();
        
        // Test with a simple command that should succeed
        let mut args = Args::try_parse_from(["background-picker"]).unwrap();
        args.command = "echo test".to_string();
        
        let (sender, _receiver) = std::sync::mpsc::channel();
        let thread_pool = rayon::ThreadPoolBuilder::new()
            .num_threads(1)
            .build()
            .unwrap();
        
        let app = BackgroundPickerApp {
            args,
            images: std::sync::Arc::new(std::sync::RwLock::new(Vec::new())),
            folder_tree: std::collections::HashMap::new(),
            loading: false,
            thumbnail_sender: sender,
            thumbnail_receiver: _receiver,
            thread_pool,
            cache_dir: temp_dir.path().join("cache"),
        };
        
        let result = app.set_background(&test_image);
        assert!(result.is_ok(), "Command execution should succeed");
        
        // Test with feh-like command structure
        let mut args2 = Args::try_parse_from(["background-picker"]).unwrap();
        args2.command = "echo --bg-max".to_string(); // Simulate feh --bg-max
        
        let app2 = BackgroundPickerApp {
            args: args2,
            images: std::sync::Arc::new(std::sync::RwLock::new(Vec::new())),
            folder_tree: std::collections::HashMap::new(),
            loading: false,
            thumbnail_sender: app.thumbnail_sender.clone(),
            thumbnail_receiver: app.thumbnail_receiver,
            thread_pool: rayon::ThreadPoolBuilder::new().num_threads(1).build().unwrap(),
            cache_dir: temp_dir.path().join("cache"),
        };
        
        let result2 = app2.set_background(&test_image);
        assert!(result2.is_ok(), "Feh-like command should succeed");
    }

    #[test]
    #[serial]
    fn test_pregeneration_workflow() {
        let temp_dir = TempDir::new().unwrap();
        create_comprehensive_test_structure(temp_dir.path()).unwrap();
        
        let mut args = Args::try_parse_from(["background-picker"]).unwrap();
        args.directory = temp_dir.path().to_path_buf();
        args.thumbnail_size = 100;
        args.debug = false; // Turn off debug for cleaner output in tests
        
        let (sender, _receiver) = std::sync::mpsc::channel();
        let thread_pool = rayon::ThreadPoolBuilder::new()
            .num_threads(2)
            .build()
            .unwrap();
        
        let mut app = BackgroundPickerApp {
            args: args.clone(),
            images: std::sync::Arc::new(std::sync::RwLock::new(Vec::new())),
            folder_tree: std::collections::HashMap::new(),
            loading: true,
            thumbnail_sender: sender,
            thumbnail_receiver: _receiver,
            thread_pool,
            cache_dir: temp_dir.path().join("thumbnails"),
        };
        
        // First scan for images
        let _ = app.scan_images();
        
        let image_count = app.images.read().unwrap().len();
        assert_eq!(image_count, 13); // Should find 13 image files
        
        // Test pregeneration (this doesn't exit in test environment)
        // We'll test the pregeneration logic without the exit
        let start_time = std::time::Instant::now();
        let _ = app.pregenerate_all_thumbnails();
        let elapsed = start_time.elapsed();
        
        // Pregeneration should complete relatively quickly for small test images
        assert!(elapsed.as_secs() < 30, "Pregeneration took too long: {:?}", elapsed);
        
        // Verify cache files were created
        let cache_dir = &app.cache_dir;
        let mut cache_files_found = 0;
        
        if cache_dir.exists() {
            for entry in fs::read_dir(cache_dir).unwrap() {
                let entry = entry.unwrap();
                if entry.path().extension().is_some_and(|ext| ext == "png") {
                    cache_files_found += 1;
                }
            }
        }
        
        // Should have generated cache files for processable images
        // (Some might fail due to fake image data, but some should succeed)
        println!("Cache files found: {}", cache_files_found);
        assert!(cache_files_found <= image_count, "Should not create more cache files than images");
    }

    #[test]
    #[serial]
    fn test_mixed_file_types_handling() {
        let temp_dir = TempDir::new().unwrap();
        
        // Create a mix of valid images, invalid images, and non-images
        fs::write(temp_dir.path().join("valid.jpg"), create_fake_jpeg_data()).unwrap();
        fs::write(temp_dir.path().join("valid.png"), create_fake_png_data()).unwrap();
        fs::write(temp_dir.path().join("invalid.jpg"), b"not actually jpeg data").unwrap();
        fs::write(temp_dir.path().join("document.txt"), b"text file").unwrap();
        fs::write(temp_dir.path().join("no_extension"), b"file without extension").unwrap();
        fs::write(temp_dir.path().join("empty.jpg"), b"").unwrap(); // Empty file
        
        let mut args = Args::try_parse_from(["background-picker"]).unwrap();
        args.directory = temp_dir.path().to_path_buf();
        args.debug = false;
        
        let (sender, _receiver) = std::sync::mpsc::channel();
        let thread_pool = rayon::ThreadPoolBuilder::new()
            .num_threads(1)
            .build()
            .unwrap();
        
        let mut app = BackgroundPickerApp {
            args: args.clone(),
            images: std::sync::Arc::new(std::sync::RwLock::new(Vec::new())),
            folder_tree: std::collections::HashMap::new(),
            loading: true,
            thumbnail_sender: sender,
            thumbnail_receiver: _receiver,
            thread_pool,
            cache_dir: temp_dir.path().join("cache"),
        };
        
        let _ = app.scan_images();
        
        let images = app.images.read().unwrap();
        
        // Should find 4 files with image extensions (valid.jpg, valid.png, invalid.jpg, empty.jpg)
        // Scanner only looks at extensions, not file content
        assert_eq!(images.len(), 4);
        
        // Test thumbnail generation for each discovered image
        for image in images.iter() {
            let result = BackgroundPickerApp::fast_thumbnail_generation(&image.path, 100);
            // Some will succeed (valid images) and some will fail (invalid/empty)
            // This is expected behavior
            println!("Thumbnail generation for {:?}: {}", 
                image.path.file_name().unwrap(), 
                if result.is_some() { "SUCCESS" } else { "FAILED" }
            );
        }
        
        assert!(!app.loading);
    }
}