crabcamera 0.8.3

Advanced cross-platform camera integration for Tauri applications
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
#[cfg(test)]
mod platform_tests {
    use crabcamera::errors::CameraError;
    use crabcamera::platform::{MockCamera, PlatformCamera};
    use crabcamera::tests::{set_mock_camera_mode, MockCaptureMode};
    use crabcamera::types::{CameraControls, CameraFormat, CameraInitParams};

    fn create_test_params() -> CameraInitParams {
        CameraInitParams::new("test_camera_0".to_string())
            .with_format(CameraFormat::new(640, 480, 30.0))
    }

    #[test]
    fn test_mock_camera_creation() {
        let format = CameraFormat::new(1920, 1080, 30.0);
        let mock_camera = MockCamera::new("test_device".to_string(), format.clone());

        assert_eq!(mock_camera.get_device_id(), "test_device");
        assert!(mock_camera.is_available());
    }

    #[test]
    fn test_mock_camera_stream_control() {
        let format = CameraFormat::new(640, 480, 30.0);
        let mock_camera = MockCamera::new("test_stream".to_string(), format);

        // Test starting stream
        let result = mock_camera.start_stream();
        assert!(result.is_ok(), "Starting stream should succeed");

        // Test stopping stream
        let result = mock_camera.stop_stream();
        assert!(result.is_ok(), "Stopping stream should succeed");
    }

    #[test]
    fn test_mock_camera_capture_success() {
        let format = CameraFormat::new(640, 480, 30.0);
        let mut mock_camera = MockCamera::new("test_capture".to_string(), format);

        // Set success mode
        mock_camera.set_capture_mode(MockCaptureMode::Success);
        set_mock_camera_mode("test_capture", MockCaptureMode::Success);

        let result = mock_camera.capture_frame();
        assert!(result.is_ok(), "Capture should succeed in success mode");

        let frame = result.unwrap();
        assert!(frame.width > 0, "Frame width should be positive");
        assert!(frame.height > 0, "Frame height should be positive");
        assert!(!frame.data.is_empty(), "Frame data should not be empty");
        assert_eq!(frame.device_id, "test_capture");
    }

    #[test]
    fn test_mock_camera_capture_failure() {
        let format = CameraFormat::new(640, 480, 30.0);
        let mut mock_camera = MockCamera::new("test_fail".to_string(), format);

        // Set failure mode
        mock_camera.set_capture_mode(MockCaptureMode::Failure);
        set_mock_camera_mode("test_fail", MockCaptureMode::Failure);

        let result = mock_camera.capture_frame();
        assert!(result.is_err(), "Capture should fail in failure mode");

        match result {
            Err(CameraError::CaptureError(msg)) => {
                assert!(msg.contains("Mock capture failure"));
            }
            _ => panic!("Expected CaptureError"),
        }
    }

    #[test]
    fn test_mock_camera_slow_capture() {
        let format = CameraFormat::new(640, 480, 30.0);
        let mut mock_camera = MockCamera::new("test_slow".to_string(), format);

        // Set slow capture mode
        mock_camera.set_capture_mode(MockCaptureMode::SlowCapture);
        set_mock_camera_mode("test_slow", MockCaptureMode::SlowCapture);

        let start = std::time::Instant::now();
        let result = mock_camera.capture_frame();
        let duration = start.elapsed();

        assert!(result.is_ok(), "Slow capture should succeed");
        assert!(
            duration.as_millis() >= 100,
            "Slow capture should take at least 100ms"
        );
    }

    #[test]
    fn test_mock_camera_controls() {
        let format = CameraFormat::new(640, 480, 30.0);
        let mut mock_camera = MockCamera::new("test_controls".to_string(), format);

        let controls = CameraControls {
            brightness: Some(0.7),
            contrast: Some(0.8),
            saturation: Some(0.6),
            exposure_time: Some(0.5),
            focus_distance: Some(0.9),
            white_balance: Some(crabcamera::types::WhiteBalance::Custom(6500)),
            iso_sensitivity: Some(800),
            zoom: Some(2.0),
            auto_focus: Some(true),
            auto_exposure: Some(true),
            aperture: None,
            image_stabilization: Some(true),
            noise_reduction: Some(false),
            sharpness: Some(0.5),
        };

        // Apply controls
        let result = mock_camera.apply_controls(&controls);
        assert!(result.is_ok(), "Applying controls should succeed");

        // Get controls back
        let result = mock_camera.get_controls();
        assert!(result.is_ok(), "Getting controls should succeed");

        let retrieved_controls = result.unwrap();
        assert_eq!(retrieved_controls.brightness, controls.brightness);
        assert_eq!(retrieved_controls.contrast, controls.contrast);
        assert_eq!(retrieved_controls.saturation, controls.saturation);
    }

    #[test]
    fn test_mock_camera_capabilities() {
        let format = CameraFormat::new(640, 480, 30.0);
        let mock_camera = MockCamera::new("test_capabilities".to_string(), format);

        let result = mock_camera.test_capabilities();
        assert!(result.is_ok(), "Getting capabilities should succeed");

        let capabilities = result.unwrap();
        assert!(
            capabilities.supports_auto_focus,
            "Should support auto focus"
        );
        assert!(
            capabilities.supports_manual_focus,
            "Should support manual focus"
        );
        assert!(
            capabilities.supports_auto_exposure,
            "Should support auto exposure"
        );
        assert!(
            capabilities.supports_white_balance,
            "Should support white balance"
        );
        assert_eq!(capabilities.max_resolution, (1920, 1080));
        assert_eq!(capabilities.max_fps, 60.0);
    }

    #[test]
    fn test_mock_camera_performance_metrics() {
        let format = CameraFormat::new(640, 480, 30.0);
        let mock_camera = MockCamera::new("test_metrics".to_string(), format);

        let result = mock_camera.get_performance_metrics();
        assert!(result.is_ok(), "Getting performance metrics should succeed");

        let metrics = result.unwrap();
        assert!(
            metrics.capture_latency_ms > 0.0,
            "Capture latency should be positive"
        );
        assert!(
            metrics.processing_time_ms > 0.0,
            "Processing time should be positive"
        );
        assert!(
            metrics.memory_usage_mb > 0.0,
            "Memory usage should be positive"
        );
        assert!(metrics.fps_actual > 0.0, "Actual FPS should be positive");
        assert!(
            metrics.quality_score > 0.0 && metrics.quality_score <= 1.0,
            "Quality score should be 0-1"
        );
    }

    #[test]
    fn test_platform_camera_creation_in_test_environment() {
        let params = create_test_params();

        let result = PlatformCamera::new(params);
        assert!(
            result.is_ok(),
            "Creating platform camera should succeed in test environment"
        );

        match result.unwrap() {
            PlatformCamera::Mock(_) => {
                // Expected in test environment
            }
            _ => panic!("Expected Mock camera in test environment"),
        }
    }

    #[test]
    fn test_platform_camera_capture_frame() {
        let params = create_test_params();
        let mut camera = PlatformCamera::new(params).unwrap();

        // Set up for successful capture
        set_mock_camera_mode("test_camera_0", MockCaptureMode::Success);

        let result = camera.capture_frame();
        assert!(result.is_ok(), "Capturing frame should succeed");

        let frame = result.unwrap();
        assert_eq!(frame.device_id, "test_camera_0");
        assert!(frame.width > 0 && frame.height > 0);
        assert!(!frame.data.is_empty());
    }

    #[test]
    fn test_platform_camera_stream_control() {
        let params = create_test_params();
        let mut camera = PlatformCamera::new(params).unwrap();

        // Test start stream
        let result = camera.start_stream();
        assert!(result.is_ok(), "Starting stream should succeed");

        // Test stop stream
        let result = camera.stop_stream();
        assert!(result.is_ok(), "Stopping stream should succeed");
    }

    #[test]
    fn test_platform_camera_availability() {
        let params = create_test_params();
        let camera = PlatformCamera::new(params).unwrap();

        let is_available = camera.is_available();
        assert!(is_available, "Mock camera should be available");
    }

    #[test]
    fn test_platform_camera_device_id() {
        let params = create_test_params();
        let camera = PlatformCamera::new(params).unwrap();

        let device_id = camera.get_device_id();
        assert!(device_id.is_some(), "Mock camera should have device ID");
        assert_eq!(device_id.unwrap(), "test_camera_0");
    }

    #[test]
    fn test_platform_camera_apply_controls() {
        let params = create_test_params();
        let mut camera = PlatformCamera::new(params).unwrap();

        let controls = CameraControls {
            brightness: Some(0.5),
            contrast: Some(0.6),
            saturation: Some(0.7),
            exposure_time: Some(0.8),
            focus_distance: Some(0.9),
            white_balance: Some(crabcamera::types::WhiteBalance::Custom(5500)),
            iso_sensitivity: Some(400),
            zoom: Some(1.5),
            auto_focus: Some(false),
            auto_exposure: Some(false),
            aperture: None,
            image_stabilization: Some(false),
            noise_reduction: Some(true),
            sharpness: Some(0.3),
        };

        let result = camera.apply_controls(&controls);
        assert!(result.is_ok(), "Applying controls should succeed");
    }

    #[test]
    fn test_platform_camera_error_propagation() {
        let params = create_test_params();
        let mut camera = PlatformCamera::new(params).unwrap();

        // Set up for capture failure
        set_mock_camera_mode("test_camera_0", MockCaptureMode::Failure);

        let result = camera.capture_frame();
        assert!(
            result.is_err(),
            "Capture should fail when set to failure mode"
        );

        match result {
            Err(CameraError::CaptureError(_)) => {
                // Expected error type
            }
            _ => panic!("Expected CaptureError"),
        }
    }

    #[test]
    fn test_multiple_camera_instances() {
        let params1 = CameraInitParams::new("test_multi_1".to_string())
            .with_format(CameraFormat::new(640, 480, 30.0));

        let params2 = CameraInitParams::new("test_multi_2".to_string())
            .with_format(CameraFormat::new(1280, 720, 30.0));

        let camera1 = PlatformCamera::new(params1);
        let camera2 = PlatformCamera::new(params2);

        assert!(
            camera1.is_ok(),
            "First camera should be created successfully"
        );
        assert!(
            camera2.is_ok(),
            "Second camera should be created successfully"
        );

        let camera1 = camera1.unwrap();
        let camera2 = camera2.unwrap();

        assert_eq!(camera1.get_device_id(), Some("test_multi_1"));
        assert_eq!(camera2.get_device_id(), Some("test_multi_2"));
    }

    #[test]
    fn test_concurrent_camera_operations() {
        let params = create_test_params();
        let camera = PlatformCamera::new(params).unwrap();

        // Test concurrent stream operations
        let camera_arc = std::sync::Arc::new(std::sync::Mutex::new(camera));
        let mut handles = vec![];

        for i in 0..5 {
            let camera_clone = camera_arc.clone();
            let handle = std::thread::spawn(move || {
                if let Ok(mut camera) = camera_clone.lock() {
                    let _ = camera.start_stream();
                    let _ = camera.stop_stream();
                    let _ = camera.is_available();
                    i // Return thread ID for verification
                } else {
                    panic!("Failed to acquire camera lock");
                }
            });
            handles.push(handle);
        }

        // Wait for all threads to complete
        for (i, handle) in handles.into_iter().enumerate() {
            let result = handle.join().unwrap();
            assert_eq!(result, i, "Thread should complete successfully");
        }
    }

    #[test]
    fn test_error_handling_comprehensive() {
        // Test various error conditions
        let params = create_test_params();
        let mut camera = PlatformCamera::new(params).unwrap();

        // Test different failure modes
        set_mock_camera_mode("test_camera_0", MockCaptureMode::Failure);
        let result = camera.capture_frame();
        assert!(result.is_err());

        // Switch back to success
        set_mock_camera_mode("test_camera_0", MockCaptureMode::Success);
        let result = camera.capture_frame();
        assert!(result.is_ok());

        // Test slow capture doesn't cause errors
        set_mock_camera_mode("test_camera_0", MockCaptureMode::SlowCapture);
        let result = camera.capture_frame();
        assert!(result.is_ok());
    }

    #[test]
    fn test_platform_camera_drop_cleanup() {
        // Test that Drop implementation properly cleans up
        let params = create_test_params();
        let mut camera = PlatformCamera::new(params).unwrap();

        // Camera should start stream successfully
        let start_result = camera.start_stream();
        assert!(start_result.is_ok(), "Stream should start successfully");

        // When camera goes out of scope, Drop should be called
        // This is automatic and we can't test it directly, but we can verify
        // that the camera is properly constructed for cleanup
        assert!(
            camera.is_available(),
            "Camera should be available before drop"
        );
    }

    #[test]
    fn test_camera_system_operations() {
        use crabcamera::platform::CameraSystem;

        // Test system initialization
        let init_result = CameraSystem::initialize();
        match init_result {
            Ok(message) => {
                assert!(!message.is_empty(), "Init message should not be empty");
                assert!(message.len() > 10, "Init message should be descriptive");
            }
            Err(_) => {
                // Initialization failure is acceptable in test environment
            }
        }

        // Test platform info
        let platform_result = CameraSystem::get_platform_info();
        match platform_result {
            Ok(info) => {
                assert!(!info.backend.is_empty(), "Backend should be specified");
                assert!(!info.features.is_empty(), "Should have some features");

                // Verify platform enum is valid
                let platform_str = info.platform.as_str();
                let valid_platforms = ["windows", "linux", "macos", "unknown"];
                assert!(
                    valid_platforms.contains(&platform_str),
                    "Platform should be valid: {}",
                    platform_str
                );
            }
            Err(_) => {
                // Platform info failure is acceptable in test environment
            }
        }

        // Test camera listing
        let cameras_result = CameraSystem::list_cameras();
        match cameras_result {
            Ok(cameras) => {
                // Cameras can be empty in test environment
                for camera in cameras {
                    assert!(!camera.id.is_empty(), "Camera ID should not be empty");
                    assert!(!camera.name.is_empty(), "Camera name should not be empty");
                    // is_available can be true or false - both are valid
                }
            }
            Err(_) => {
                // Camera listing failure is acceptable in test environment
            }
        }

        // Test system testing
        let test_result = CameraSystem::test_system();
        match test_result {
            Ok(result) => {
                // cameras_found is u32, always >= 0

                // Test results should be valid
                for (camera_id, test_result) in result.test_results {
                    assert!(!camera_id.is_empty(), "Camera ID should not be empty");
                    // All test result variants are valid - just verify the match works
                    match test_result {
                        crabcamera::platform::CameraTestResult::Success => {}
                        crabcamera::platform::CameraTestResult::InitError(_) => {}
                        crabcamera::platform::CameraTestResult::CaptureError(_) => {}
                        crabcamera::platform::CameraTestResult::NotAvailable => {}
                    }
                }
            }
            Err(_) => {
                // System test failure is acceptable in test environment
            }
        }
    }

    #[test]
    fn test_platform_optimizations() {
        use crabcamera::platform::optimizations;

        // Test photography format recommendation
        let photo_format = optimizations::get_photography_format();
        assert!(
            photo_format.width > 0,
            "Photography format should have positive width"
        );
        assert!(
            photo_format.height > 0,
            "Photography format should have positive height"
        );
        assert!(
            photo_format.fps > 0.0,
            "Photography format should have positive FPS"
        );

        // Should be a reasonable photography resolution
        assert!(
            photo_format.width >= 1280,
            "Photography format should be at least 720p width"
        );
        assert!(
            photo_format.height >= 720,
            "Photography format should be at least 720p height"
        );

        // Test optimal settings
        let optimal_settings = optimizations::get_optimal_settings();
        assert!(
            !optimal_settings.device_id.is_empty(),
            "Device ID should not be empty"
        );
        assert!(
            optimal_settings.format.width > 0,
            "Format width should be positive"
        );
        assert!(
            optimal_settings.format.height > 0,
            "Format height should be positive"
        );
        assert!(
            optimal_settings.format.fps > 0.0,
            "Format FPS should be positive"
        );
    }

    #[test]
    fn test_platform_info_serialization() {
        use crabcamera::platform::PlatformInfo;
        use crabcamera::types::Platform;

        let platform_info = PlatformInfo {
            platform: Platform::Windows,
            backend: "Test Backend".to_string(),
            features: vec!["Feature 1".to_string(), "Feature 2".to_string()],
        };

        // Test serialization
        let serialized = serde_json::to_string(&platform_info);
        assert!(
            serialized.is_ok(),
            "PlatformInfo should serialize successfully"
        );

        // Test deserialization
        let json = serialized.unwrap();
        let deserialized: Result<PlatformInfo, _> = serde_json::from_str(&json);
        assert!(
            deserialized.is_ok(),
            "PlatformInfo should deserialize successfully"
        );

        let restored_info = deserialized.unwrap();
        assert_eq!(restored_info.platform.as_str(), "windows");
        assert_eq!(restored_info.backend, "Test Backend");
        assert_eq!(restored_info.features.len(), 2);
    }

    #[test]
    fn test_system_test_result_serialization() {
        use crabcamera::platform::{CameraTestResult, SystemTestResult};
        use crabcamera::types::Platform;

        let test_result = SystemTestResult {
            platform: Platform::Linux,
            cameras_found: 2,
            test_results: vec![
                ("camera1".to_string(), CameraTestResult::Success),
                (
                    "camera2".to_string(),
                    CameraTestResult::InitError("Test error".to_string()),
                ),
                ("camera3".to_string(), CameraTestResult::NotAvailable),
            ],
        };

        // Test serialization
        let serialized = serde_json::to_string(&test_result);
        assert!(
            serialized.is_ok(),
            "SystemTestResult should serialize successfully"
        );

        // Test deserialization
        let json = serialized.unwrap();
        let deserialized: Result<SystemTestResult, _> = serde_json::from_str(&json);
        assert!(
            deserialized.is_ok(),
            "SystemTestResult should deserialize successfully"
        );

        let restored_result = deserialized.unwrap();
        assert_eq!(restored_result.platform.as_str(), "linux");
        assert_eq!(restored_result.cameras_found, 2);
        assert_eq!(restored_result.test_results.len(), 3);
    }

    #[test]
    fn test_camera_test_result_variants() {
        use crabcamera::platform::CameraTestResult;

        let test_results = vec![
            CameraTestResult::Success,
            CameraTestResult::InitError("Init failed".to_string()),
            CameraTestResult::CaptureError("Capture failed".to_string()),
            CameraTestResult::NotAvailable,
        ];

        for result in test_results {
            // Test that all variants can be created and matched
            match result {
                CameraTestResult::Success => {
                    // Success variant works
                }
                CameraTestResult::InitError(msg) => {
                    assert_eq!(msg, "Init failed");
                }
                CameraTestResult::CaptureError(msg) => {
                    assert_eq!(msg, "Capture failed");
                }
                CameraTestResult::NotAvailable => {
                    // NotAvailable variant works
                }
            }
        }
    }

    #[test]
    fn test_platform_camera_capabilities_comprehensive() {
        let params = create_test_params();
        let camera = PlatformCamera::new(params).unwrap();

        // Test capabilities
        let capabilities_result = camera.test_capabilities();
        assert!(
            capabilities_result.is_ok(),
            "Getting capabilities should succeed"
        );

        let capabilities = capabilities_result.unwrap();

        // Verify capability fields are reasonable
        assert!(
            capabilities.max_resolution.0 > 0,
            "Max width should be positive"
        );
        assert!(
            capabilities.max_resolution.1 > 0,
            "Max height should be positive"
        );
        assert!(capabilities.max_fps > 0.0, "Max FPS should be positive");

        // Boolean capabilities should be present (can be true or false)
        let _ = capabilities.supports_auto_focus;
        let _ = capabilities.supports_manual_focus;
        let _ = capabilities.supports_auto_exposure;
        let _ = capabilities.supports_manual_exposure;
        let _ = capabilities.supports_white_balance;
        let _ = capabilities.supports_zoom;
        let _ = capabilities.supports_flash;
        let _ = capabilities.supports_burst_mode;
        let _ = capabilities.supports_hdr;

        // Optional ranges can be None or Some - both are valid
        if let Some((min_exp, max_exp)) = capabilities.exposure_range {
            assert!(min_exp < max_exp, "Exposure range should be valid");
            assert!(min_exp > 0.0, "Min exposure should be positive");
        }

        if let Some((min_iso, max_iso)) = capabilities.iso_range {
            assert!(min_iso < max_iso, "ISO range should be valid");
            assert!(min_iso > 0, "Min ISO should be positive");
        }

        if let Some((min_focus, max_focus)) = capabilities.focus_range {
            assert!(min_focus <= max_focus, "Focus range should be valid");
            assert!(
                min_focus >= 0.0 && max_focus <= 1.0,
                "Focus range should be 0-1"
            );
        }
    }

    #[test]
    fn test_platform_camera_performance_metrics() {
        let params = create_test_params();
        let camera = PlatformCamera::new(params).unwrap();

        // Test performance metrics
        let metrics_result = camera.get_performance_metrics();
        assert!(
            metrics_result.is_ok(),
            "Getting performance metrics should succeed"
        );

        let metrics = metrics_result.unwrap();

        // Verify all metrics are reasonable
        assert!(
            metrics.capture_latency_ms > 0.0,
            "Capture latency should be positive"
        );
        assert!(
            metrics.processing_time_ms >= 0.0,
            "Processing time should be non-negative"
        );
        assert!(
            metrics.memory_usage_mb > 0.0,
            "Memory usage should be positive"
        );
        assert!(metrics.fps_actual > 0.0, "Actual FPS should be positive");
        // dropped_frames is u32, always >= 0
        // buffer_overruns is u32, always >= 0
        assert!(
            metrics.quality_score >= 0.0 && metrics.quality_score <= 1.0,
            "Quality score should be 0-1, got: {}",
            metrics.quality_score
        );
    }

    #[test]
    fn test_mock_camera_frame_callback() {
        use std::sync::{Arc, Mutex};

        let format = CameraFormat::new(640, 480, 30.0);
        let mut mock_camera = MockCamera::new("test_callback".to_string(), format);

        // Set up callback tracking
        let callback_called = Arc::new(Mutex::new(false));
        let callback_frame = Arc::new(Mutex::new(None));
        let callback_called_clone = callback_called.clone();
        let callback_frame_clone = callback_frame.clone();

        // Set the callback
        let result = mock_camera.frame_callback(move |frame| {
            *callback_called_clone.lock().unwrap() = true;
            *callback_frame_clone.lock().unwrap() = Some(frame);
        });
        assert!(result.is_ok(), "Setting callback should succeed");

        // Set success mode and capture a frame
        mock_camera.set_capture_mode(MockCaptureMode::Success);
        set_mock_camera_mode("test_callback", MockCaptureMode::Success);

        let capture_result = mock_camera.capture_frame();
        assert!(capture_result.is_ok(), "Capture should succeed");

        // Verify callback was called
        assert!(*callback_called.lock().unwrap(), "Callback should have been called");

        // Verify callback received the correct frame
        let callback_frame_data = callback_frame.lock().unwrap().take();
        assert!(callback_frame_data.is_some(), "Callback should have received a frame");

        let callback_frame = callback_frame_data.unwrap();
        assert_eq!(callback_frame.device_id, "test_callback");
        assert!(callback_frame.width > 0);
        assert!(callback_frame.height > 0);
        assert!(!callback_frame.data.is_empty());
    }
}