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
//! Quality Analysis Testing
//!
//! Comprehensive test suite for quality validation including:
//! - Quality metric calculations
//! - Blur detection algorithms
//! - Exposure analysis
//! - Quality validator accuracy
//! - Parameter validation and boundary conditions
//! - Performance testing for compute-intensive analysis

use crabcamera::commands::quality::{
    analyze_frame_blur, analyze_frame_exposure, analyze_quality_trends, auto_capture_with_quality,
    capture_best_quality_frame, get_quality_config, update_quality_config, validate_frame_quality,
    validate_provided_frame, ValidationConfigDto,
};
use crabcamera::quality::{
    BlurDetector, BlurLevel, ExposureAnalyzer, ExposureLevel, QualityValidator, ValidationConfig,
};
use crabcamera::types::{CameraFormat, CameraFrame};
use std::time::Instant;
use tokio;

/// Mock device ID for testing
const TEST_DEVICE_ID: &str = "test_camera_quality";

/// Helper function to create test frames with specific characteristics
fn create_test_frame_with_pattern(width: u32, height: u32, pattern: &str) -> CameraFrame {
    let size = (width * height * 3) as usize;
    let mut data = vec![0u8; size];

    match pattern {
        "solid_gray" => {
            // Solid gray pattern
            for i in (0..size).step_by(3) {
                data[i] = 128; // R
                data[i + 1] = 128; // G
                data[i + 2] = 128; // B
            }
        }
        "checkboard" => {
            // Checkboard pattern for sharpness testing
            let check_size = 8;
            for y in 0..height {
                for x in 0..width {
                    let is_white = ((x / check_size) + (y / check_size)) % 2 == 0;
                    let color = if is_white { 255 } else { 0 };
                    let idx = ((y * width + x) * 3) as usize;
                    if idx + 2 < size {
                        data[idx] = color;
                        data[idx + 1] = color;
                        data[idx + 2] = color;
                    }
                }
            }
        }
        "gradient" => {
            // Horizontal gradient for exposure testing
            for y in 0..height {
                for x in 0..width {
                    let intensity = (x * 255 / width) as u8;
                    let idx = ((y * width + x) * 3) as usize;
                    if idx + 2 < size {
                        data[idx] = intensity;
                        data[idx + 1] = intensity;
                        data[idx + 2] = intensity;
                    }
                }
            }
        }
        "dark" => {
            // Very dark image
            for i in (0..size).step_by(3) {
                data[i] = 20;
                data[i + 1] = 20;
                data[i + 2] = 20;
            }
        }
        "bright" => {
            // Very bright image
            for i in (0..size).step_by(3) {
                data[i] = 240;
                data[i + 1] = 240;
                data[i + 2] = 240;
            }
        }
        "noisy" => {
            // Noisy pattern for blur testing
            for i in (0..size).step_by(3) {
                let noise = (i % 50) as u8;
                data[i] = 128 + noise;
                data[i + 1] = 128 + noise;
                data[i + 2] = 128 + noise;
            }
        }
        _ => {
            // Default solid pattern
            for i in (0..size).step_by(3) {
                data[i] = 128;
                data[i + 1] = 128;
                data[i + 2] = 128;
            }
        }
    }

    CameraFrame::new(data, width, height, "test".to_string())
}

/// Test blur detection with various image patterns
#[test]
fn test_blur_detector_patterns() {
    let detector = BlurDetector::default();

    // Test sharp checkboard pattern
    let sharp_frame = create_test_frame_with_pattern(100, 100, "checkboard");
    let sharp_metrics = detector.analyze_frame(&sharp_frame);

    println!("Sharp checkboard metrics:");
    println!("  Variance: {:.2}", sharp_metrics.variance);
    println!("  Gradient: {:.2}", sharp_metrics.gradient_magnitude);
    println!("  Edge density: {:.4}", sharp_metrics.edge_density);
    println!("  Quality score: {:.3}", sharp_metrics.quality_score);

    // Checkboard should have high variance and be considered sharp
    assert!(sharp_metrics.variance > 100.0);
    assert!(matches!(
        sharp_metrics.blur_level,
        BlurLevel::Sharp | BlurLevel::Good
    ));

    // Test blurry solid pattern
    let blurry_frame = create_test_frame_with_pattern(100, 100, "solid_gray");
    let blurry_metrics = detector.analyze_frame(&blurry_frame);

    println!("Solid gray metrics:");
    println!("  Variance: {:.2}", blurry_metrics.variance);
    println!("  Quality score: {:.3}", blurry_metrics.quality_score);

    // Solid pattern should have low variance
    assert!(blurry_metrics.variance < 50.0);
    assert!(matches!(
        blurry_metrics.blur_level,
        BlurLevel::Blurry | BlurLevel::VeryBlurry
    ));
}

/// Test exposure analysis with different lighting conditions
#[test]
fn test_exposure_analyzer_patterns() {
    let analyzer = ExposureAnalyzer::default();

    // Test well-exposed gradient
    let gradient_frame = create_test_frame_with_pattern(100, 100, "gradient");
    let gradient_metrics = analyzer.analyze_frame(&gradient_frame);

    println!("Gradient exposure metrics:");
    println!("  Mean brightness: {:.3}", gradient_metrics.mean_brightness);
    println!("  Std dev: {:.3}", gradient_metrics.brightness_std);
    println!("  Dynamic range: {:.3}", gradient_metrics.dynamic_range);
    println!("  Quality score: {:.3}", gradient_metrics.quality_score);

    // Gradient should have good dynamic range
    assert!(gradient_metrics.dynamic_range > 0.8);
    assert!(gradient_metrics.brightness_std > 0.2);

    // Test dark image
    let dark_frame = create_test_frame_with_pattern(100, 100, "dark");
    let dark_metrics = analyzer.analyze_frame(&dark_frame);

    assert!(dark_metrics.mean_brightness < 0.2);
    assert_eq!(dark_metrics.exposure_level, ExposureLevel::Underexposed);
    assert!(dark_metrics.dark_pixel_ratio > 0.8);

    // Test bright image
    let bright_frame = create_test_frame_with_pattern(100, 100, "bright");
    let bright_metrics = analyzer.analyze_frame(&bright_frame);

    assert!(bright_metrics.mean_brightness > 0.8);
    assert_eq!(bright_metrics.exposure_level, ExposureLevel::Overexposed);
    assert!(bright_metrics.bright_pixel_ratio > 0.8);
}

/// Test quality validator with different frame types
#[test]
fn test_quality_validator() {
    let validator = QualityValidator::default();

    // Test high quality frame (sharp checkboard)
    let high_quality = create_test_frame_with_pattern(640, 480, "checkboard");
    let hq_report = validator.validate_frame(&high_quality);

    println!("High quality frame report:");
    println!("  Overall score: {:.3}", hq_report.score.overall);
    println!("  Blur score: {:.3}", hq_report.score.blur);
    println!("  Exposure score: {:.3}", hq_report.score.exposure);
    println!("  Is acceptable: {}", hq_report.is_acceptable);

    assert!(hq_report.score.overall > 0.5);
    assert!(hq_report.score.blur > 0.5);

    // Test low quality frame (solid color)
    let low_quality = create_test_frame_with_pattern(320, 240, "solid_gray");
    let lq_report = validator.validate_frame(&low_quality);

    println!("Low quality frame report:");
    println!("  Overall score: {:.3}", lq_report.score.overall);
    println!("  Technical score: {:.3}", lq_report.score.technical);

    // Solid frame should have low quality
    assert!(lq_report.score.overall < 0.7);
    assert!(lq_report.score.blur < 0.5);
}

/// Test blur level thresholds and scoring
#[test]
fn test_blur_level_boundaries() {
    // Test variance to blur level mapping
    assert_eq!(BlurLevel::from_variance(1500.0), BlurLevel::Sharp);
    assert_eq!(BlurLevel::from_variance(800.0), BlurLevel::Good);
    assert_eq!(BlurLevel::from_variance(300.0), BlurLevel::Moderate);
    assert_eq!(BlurLevel::from_variance(100.0), BlurLevel::Blurry);
    assert_eq!(BlurLevel::from_variance(10.0), BlurLevel::VeryBlurry);

    // Test quality scores
    assert_eq!(BlurLevel::Sharp.quality_score(), 1.0);
    assert_eq!(BlurLevel::Good.quality_score(), 0.8);
    assert_eq!(BlurLevel::Moderate.quality_score(), 0.6);
    assert_eq!(BlurLevel::Blurry.quality_score(), 0.3);
    assert_eq!(BlurLevel::VeryBlurry.quality_score(), 0.1);
}

/// Test exposure level thresholds and scoring
#[test]
fn test_exposure_level_boundaries() {
    // Test brightness to exposure level mapping
    assert_eq!(
        ExposureLevel::from_brightness(0.1),
        ExposureLevel::Underexposed
    );
    assert_eq!(
        ExposureLevel::from_brightness(0.3),
        ExposureLevel::SlightlyDark
    );
    assert_eq!(
        ExposureLevel::from_brightness(0.5),
        ExposureLevel::WellExposed
    );
    assert_eq!(
        ExposureLevel::from_brightness(0.7),
        ExposureLevel::SlightlyBright
    );
    assert_eq!(
        ExposureLevel::from_brightness(0.9),
        ExposureLevel::Overexposed
    );

    // Test quality scores
    assert_eq!(ExposureLevel::WellExposed.quality_score(), 1.0);
    assert_eq!(ExposureLevel::SlightlyDark.quality_score(), 0.8);
    assert_eq!(ExposureLevel::SlightlyBright.quality_score(), 0.8);
    assert_eq!(ExposureLevel::Underexposed.quality_score(), 0.3);
    assert_eq!(ExposureLevel::Overexposed.quality_score(), 0.3);
}

/// Test quality validation commands with provided frames
#[tokio::test]
async fn test_validate_provided_frame_command() {
    // Test with high quality frame
    let hq_frame = create_test_frame_with_pattern(1280, 720, "checkboard");
    let result = validate_provided_frame(hq_frame).await;

    assert!(result.is_ok());
    let report = result.unwrap();
    assert!(report.score.overall >= 0.0 && report.score.overall <= 1.0);
    assert!(report.score.blur >= 0.0 && report.score.blur <= 1.0);
    assert!(report.score.exposure >= 0.0 && report.score.exposure <= 1.0);

    // Test with low quality frame
    let lq_frame = create_test_frame_with_pattern(160, 120, "solid_gray");
    let result = validate_provided_frame(lq_frame).await;

    assert!(result.is_ok());
    let report = result.unwrap();
    assert!(report.score.overall < 0.8); // Should be lower quality
}

/// Test quality configuration update and retrieval
#[tokio::test]
async fn test_quality_config_management() {
    // Get default configuration
    let default_config = get_quality_config().await;
    assert!(default_config.is_ok());
    let config = default_config.unwrap();

    println!("Default quality config:");
    println!("  Blur threshold: {}", config.blur_threshold);
    println!("  Exposure threshold: {}", config.exposure_threshold);
    println!("  Overall threshold: {}", config.overall_threshold);

    // Verify defaults are reasonable
    assert!(config.blur_threshold > 0.0 && config.blur_threshold <= 1.0);
    assert!(config.exposure_threshold > 0.0 && config.exposure_threshold <= 1.0);
    assert!(config.overall_threshold > 0.0 && config.overall_threshold <= 1.0);

    // Update configuration
    let new_config = ValidationConfigDto {
        blur_threshold: 0.8,
        exposure_threshold: 0.9,
        overall_threshold: 0.85,
        min_width: 1920,
        min_height: 1080,
        max_noise_level: 0.1,
    };

    let update_result = update_quality_config(new_config.clone()).await;
    assert!(update_result.is_ok());
}

/// Test frame quality validation with camera capture
#[tokio::test]
async fn test_validate_frame_quality_command() {
    let device_id = Some(TEST_DEVICE_ID.to_string());
    let format = Some(CameraFormat::standard());

    let result = validate_frame_quality(device_id, format).await;
    match result {
        Ok(report) => {
            println!("Captured frame quality:");
            println!("  Overall score: {:.3}", report.score.overall);
            println!("  Blur score: {:.3}", report.score.blur);
            println!("  Exposure score: {:.3}", report.score.exposure);
            println!("  Technical score: {:.3}", report.score.technical);

            // Verify report structure
            assert!(report.score.overall >= 0.0 && report.score.overall <= 1.0);
            assert!(report.blur_metrics.unwrap().quality_score >= 0.0);
            assert!(report.exposure_metrics.unwrap().quality_score >= 0.0);
        }
        Err(e) if e.contains("mutex") || e.contains("camera") => {
            println!(
                "Warning: Quality validation test skipped (no camera): {}",
                e
            );
        }
        Err(e) => {
            println!("Unexpected quality validation error: {}", e);
        }
    }
}

/// Test blur analysis command
#[tokio::test]
async fn test_analyze_frame_blur_command() {
    let device_id = Some(TEST_DEVICE_ID.to_string());
    let format = Some(CameraFormat::standard());

    let result = analyze_frame_blur(device_id, format).await;
    match result {
        Ok(metrics) => {
            println!("Blur analysis metrics:");
            println!("  Variance: {:.2}", metrics.variance);
            println!("  Gradient magnitude: {:.2}", metrics.gradient_magnitude);
            println!("  Edge density: {:.4}", metrics.edge_density);
            println!("  Blur level: {:?}", metrics.blur_level);

            // Verify metrics structure
            assert!(metrics.variance >= 0.0);
            assert!(metrics.gradient_magnitude >= 0.0);
            assert!(metrics.edge_density >= 0.0 && metrics.edge_density <= 1.0);
            assert!(metrics.quality_score >= 0.0 && metrics.quality_score <= 1.0);
        }
        Err(e) if e.contains("mutex") || e.contains("camera") => {
            println!("Warning: Blur analysis test skipped (no camera): {}", e);
        }
        Err(e) => {
            println!("Unexpected blur analysis error: {}", e);
        }
    }
}

/// Test exposure analysis command
#[tokio::test]
async fn test_analyze_frame_exposure_command() {
    let device_id = Some(TEST_DEVICE_ID.to_string());
    let format = Some(CameraFormat::standard());

    let result = analyze_frame_exposure(device_id, format).await;
    match result {
        Ok(metrics) => {
            println!("Exposure analysis metrics:");
            println!("  Mean brightness: {:.3}", metrics.mean_brightness);
            println!("  Brightness std: {:.3}", metrics.brightness_std);
            println!("  Dark pixel ratio: {:.3}", metrics.dark_pixel_ratio);
            println!("  Bright pixel ratio: {:.3}", metrics.bright_pixel_ratio);
            println!("  Dynamic range: {:.3}", metrics.dynamic_range);
            println!("  Exposure level: {:?}", metrics.exposure_level);

            // Verify metrics structure
            assert!(metrics.mean_brightness >= 0.0 && metrics.mean_brightness <= 1.0);
            assert!(metrics.brightness_std >= 0.0);
            assert!(metrics.dark_pixel_ratio >= 0.0 && metrics.dark_pixel_ratio <= 1.0);
            assert!(metrics.bright_pixel_ratio >= 0.0 && metrics.bright_pixel_ratio <= 1.0);
            assert!(metrics.dynamic_range >= 0.0 && metrics.dynamic_range <= 1.0);
            assert!(metrics.histogram.len() == 256);
        }
        Err(e) if e.contains("mutex") || e.contains("camera") => {
            println!("Warning: Exposure analysis test skipped (no camera): {}", e);
        }
        Err(e) => {
            println!("Unexpected exposure analysis error: {}", e);
        }
    }
}

/// Test best quality frame capture
#[tokio::test]
async fn test_capture_best_quality_frame() {
    let device_id = Some(TEST_DEVICE_ID.to_string());
    let format = Some(CameraFormat::standard());

    let result = capture_best_quality_frame(device_id, format, Some(3)).await;
    match result {
        Ok(capture_result) => {
            println!("Best quality capture result:");
            println!("  Attempts used: {}", capture_result.attempts_used);
            println!(
                "  Quality score: {:.3}",
                capture_result.quality_report.score.overall
            );

            assert!(capture_result.attempts_used <= 3);
            assert!(capture_result.frame.is_valid());
            assert!(capture_result.quality_report.score.overall >= 0.0);
        }
        Err(e) if e.contains("mutex") || e.contains("camera") => {
            println!("Warning: Best quality test skipped (no camera): {}", e);
        }
        Err(e) => {
            println!("Unexpected best quality error: {}", e);
        }
    }
}

/// Test auto-capture with quality threshold
#[tokio::test]
async fn test_auto_capture_with_quality() {
    let device_id = Some(TEST_DEVICE_ID.to_string());
    let format = Some(CameraFormat::standard());

    // Test with reasonable quality threshold
    let result = auto_capture_with_quality(
        device_id,
        format,
        Some(0.5), // 50% quality threshold
        Some(5),   // Max 5 attempts
        Some(10),  // 10 second timeout
    )
    .await;

    match result {
        Ok(capture_result) => {
            println!("Auto capture result:");
            println!("  Attempts used: {}", capture_result.attempts_used);
            println!(
                "  Quality achieved: {:.3}",
                capture_result.quality_report.score.overall
            );

            assert!(capture_result.attempts_used <= 5);
            assert!(capture_result.quality_report.score.overall >= 0.5);
            assert!(capture_result.frame.is_valid());
        }
        Err(e) if e.contains("mutex") || e.contains("camera") || e.contains("timeout") => {
            println!("Warning: Auto capture test result: {}", e);
        }
        Err(e) => {
            println!("Unexpected auto capture error: {}", e);
        }
    }
}

/// Test quality trend analysis
#[tokio::test]
async fn test_analyze_quality_trends() {
    let device_id = Some(TEST_DEVICE_ID.to_string());
    let format = Some(CameraFormat::standard());

    let result = analyze_quality_trends(device_id, format, Some(5)).await;
    match result {
        Ok(analysis) => {
            println!("Quality trend analysis:");
            println!("  Samples analyzed: {}", analysis.samples_analyzed);
            println!("  Average quality: {:.3}", analysis.average_quality);
            println!("  Quality variance: {:.6}", analysis.quality_variance);
            println!("  Stability score: {:.3}", analysis.stability_score);
            println!("  Best score: {:.3}", analysis.best_score);
            println!("  Worst score: {:.3}", analysis.worst_score);
            println!("  Acceptable ratio: {:.3}", analysis.acceptable_ratio);

            assert!(analysis.samples_analyzed <= 5);
            assert!(analysis.average_quality >= 0.0 && analysis.average_quality <= 1.0);
            assert!(analysis.stability_score >= 0.0 && analysis.stability_score <= 1.0);
            assert!(analysis.best_score >= analysis.worst_score);
            assert!(analysis.acceptable_ratio >= 0.0 && analysis.acceptable_ratio <= 1.0);
        }
        Err(e) if e.contains("mutex") || e.contains("camera") => {
            println!("Warning: Quality trends test skipped (no camera): {}", e);
        }
        Err(e) => {
            println!("Unexpected quality trends error: {}", e);
        }
    }
}

/// Performance benchmark for quality analysis operations
#[test]
fn test_quality_analysis_performance() {
    let detector = BlurDetector::default();
    let analyzer = ExposureAnalyzer::default();
    let validator = QualityValidator::default();

    // Test various frame sizes
    let sizes = [(320, 240), (640, 480), (1280, 720), (1920, 1080)];

    for (width, height) in sizes.iter() {
        let frame = create_test_frame_with_pattern(*width, *height, "checkboard");

        // Benchmark blur detection
        let start = Instant::now();
        let blur_metrics = detector.analyze_frame(&frame);
        let blur_time = start.elapsed();

        // Benchmark exposure analysis
        let start = Instant::now();
        let exposure_metrics = analyzer.analyze_frame(&frame);
        let exposure_time = start.elapsed();

        // Benchmark full validation
        let start = Instant::now();
        let quality_report = validator.validate_frame(&frame);
        let validation_time = start.elapsed();

        println!("Performance for {}x{} frame:", width, height);
        println!("  Blur detection: {:?}", blur_time);
        println!("  Exposure analysis: {:?}", exposure_time);
        println!("  Full validation: {:?}", validation_time);
        println!("  Quality score: {:.3}", quality_report.score.overall);

        // Verify reasonable performance (under 2000ms for large frames - quality analysis is compute intensive)
        assert!(validation_time.as_millis() < 2000);

        // Verify results are valid
        assert!(blur_metrics.quality_score >= 0.0);
        assert!(exposure_metrics.quality_score >= 0.0);
        assert!(quality_report.score.overall >= 0.0);
    }
}

/// Test mathematical correctness of quality algorithms
#[test]
fn test_algorithm_mathematical_correctness() {
    let detector = BlurDetector::default();
    let analyzer = ExposureAnalyzer::default();

    // Test with known pattern - white/black squares should have high variance
    let sharp_frame = create_test_frame_with_pattern(100, 100, "checkboard");
    let sharp_metrics = detector.analyze_frame(&sharp_frame);

    // Checkboard should have very high Laplacian variance
    assert!(sharp_metrics.variance > 1000.0);
    assert!(sharp_metrics.gradient_magnitude > 50.0);
    assert!(sharp_metrics.edge_density > 0.3);

    // Test exposure with known gradient
    let gradient_frame = create_test_frame_with_pattern(256, 1, "gradient");
    let gradient_metrics = analyzer.analyze_frame(&gradient_frame);

    // Gradient should have approximately 50% mean brightness
    assert!(gradient_metrics.mean_brightness > 0.45);
    assert!(gradient_metrics.mean_brightness < 0.55);

    // Should have near-perfect dynamic range
    assert!(gradient_metrics.dynamic_range > 0.95);

    // Standard deviation should be significant for uniform gradient
    assert!(gradient_metrics.brightness_std > 0.25);
}

/// Test edge cases and boundary conditions
#[test]
fn test_quality_analysis_edge_cases() {
    let detector = BlurDetector::default();
    let analyzer = ExposureAnalyzer::default();

    // Test with minimal frame (1x1)
    let tiny_frame = create_test_frame_with_pattern(1, 1, "solid_gray");
    let tiny_blur = detector.analyze_frame(&tiny_frame);
    let tiny_exposure = analyzer.analyze_frame(&tiny_frame);

    // Should handle gracefully
    assert!(tiny_blur.quality_score >= 0.0);
    assert!(tiny_exposure.quality_score >= 0.0);

    // Test with very large frame
    let large_frame = create_test_frame_with_pattern(2048, 2048, "noisy");
    let large_blur = detector.analyze_frame(&large_frame);

    // Should complete without crashing
    assert!(large_blur.variance >= 0.0);

    // Test with extreme values
    let mut extreme_data = vec![0u8; 300]; // 100 pixels RGB
                                           // Fill with extreme values
    for i in (0..300).step_by(3) {
        extreme_data[i] = if i % 6 == 0 { 255 } else { 0 };
        extreme_data[i + 1] = if i % 6 == 3 { 255 } else { 0 };
        extreme_data[i + 2] = 0;
    }

    let extreme_frame = CameraFrame::new(extreme_data, 10, 10, "test".to_string());
    let extreme_metrics = analyzer.analyze_frame(&extreme_frame);

    // Should handle extreme contrast (expecting ~0.5 due to luminance weighting)
    assert!(extreme_metrics.dynamic_range > 0.4);
}

/// Test custom detector and analyzer configurations
#[test]
fn test_custom_configurations() {
    // Test custom blur detector thresholds
    let strict_detector = BlurDetector::new(500.0, 100.0); // Stricter thresholds
    let lenient_detector = BlurDetector::new(50.0, 10.0); // More lenient

    let test_frame = create_test_frame_with_pattern(200, 200, "noisy");

    let strict_metrics = strict_detector.analyze_frame(&test_frame);
    let lenient_metrics = lenient_detector.analyze_frame(&test_frame);

    // Strict detector should be harder to please
    assert!(
        strict_detector.is_acceptable_quality(&strict_metrics)
            <= lenient_detector.is_acceptable_quality(&lenient_metrics)
    );

    // Test custom exposure analyzer thresholds
    let custom_analyzer = ExposureAnalyzer::new(40, 200); // Custom dark/bright thresholds
    let metrics = custom_analyzer.analyze_frame(&test_frame);

    assert!(metrics.quality_score >= 0.0 && metrics.quality_score <= 1.0);
}

/// Test quality validator with custom configuration
#[test]
fn test_custom_quality_validator() {
    let custom_config = ValidationConfig {
        blur_threshold: 0.8,
        exposure_threshold: 0.9,
        overall_threshold: 0.85,
        min_resolution: (1920, 1080),
        max_noise_level: 0.1,
    };

    let validator = QualityValidator::new(custom_config);

    // Test with high-resolution sharp frame
    let hd_frame = create_test_frame_with_pattern(1920, 1080, "checkboard");
    let hd_report = validator.validate_frame(&hd_frame);

    println!("HD frame with strict validation:");
    println!("  Overall score: {:.3}", hd_report.score.overall);
    println!("  Technical score: {:.3}", hd_report.score.technical);
    println!("  Is acceptable: {}", hd_report.is_acceptable);

    // HD sharp frame should pass technical requirement
    assert!(hd_report.score.technical >= 0.5);

    // Test with low-resolution frame
    let ld_frame = create_test_frame_with_pattern(640, 480, "checkboard");
    let ld_report = validator.validate_frame(&ld_frame);

    // Should have lower technical score due to lower resolution
    assert!(ld_report.score.technical <= hd_report.score.technical);
}