decy-oracle 2.2.0

CITL (Compiler-in-the-Loop) oracle for C-to-Rust transpilation pattern mining
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
//! Coverage tests for config.rs
//!
//! These tests target all uncovered lines and branches in OracleConfig,
//! including from_env with auto_fix variants, from_file edge cases,
//! serde round-trip paths, and the default_patterns_path fallback logic.

use crate::config::OracleConfig;
use std::io::Write;
use std::path::PathBuf;
use tempfile::NamedTempFile;

// Mutex for serializing env var tests to prevent race conditions.
// Use unwrap_or_else to recover from poison (a prior test panicking).
static ENV_MUTEX: std::sync::Mutex<()> = std::sync::Mutex::new(());

fn lock_env() -> std::sync::MutexGuard<'static, ()> {
    ENV_MUTEX.lock().unwrap_or_else(|e| e.into_inner())
}

// ============================================================================
// FROM_ENV: AUTO_FIX BRANCH COVERAGE (replaces ignored tests)
// ============================================================================

#[test]
fn config_coverage_from_env_auto_fix_true_lowercase() {
    let _lock = lock_env();
    // Clean slate
    std::env::remove_var("DECY_ORACLE_PATTERNS");
    std::env::remove_var("DECY_ORACLE_THRESHOLD");
    std::env::remove_var("DECY_ORACLE_AUTO_FIX");

    std::env::set_var("DECY_ORACLE_AUTO_FIX", "true");
    let config = OracleConfig::from_env();
    assert!(config.auto_fix, "auto_fix should be true for 'true'");

    std::env::remove_var("DECY_ORACLE_AUTO_FIX");
}

#[test]
fn config_coverage_from_env_auto_fix_true_uppercase() {
    let _lock = lock_env();
    std::env::remove_var("DECY_ORACLE_PATTERNS");
    std::env::remove_var("DECY_ORACLE_THRESHOLD");
    std::env::remove_var("DECY_ORACLE_AUTO_FIX");

    std::env::set_var("DECY_ORACLE_AUTO_FIX", "TRUE");
    let config = OracleConfig::from_env();
    assert!(config.auto_fix, "auto_fix should be true for 'TRUE'");

    std::env::remove_var("DECY_ORACLE_AUTO_FIX");
}

#[test]
fn config_coverage_from_env_auto_fix_true_mixed_case() {
    let _lock = lock_env();
    std::env::remove_var("DECY_ORACLE_PATTERNS");
    std::env::remove_var("DECY_ORACLE_THRESHOLD");
    std::env::remove_var("DECY_ORACLE_AUTO_FIX");

    std::env::set_var("DECY_ORACLE_AUTO_FIX", "True");
    let config = OracleConfig::from_env();
    assert!(config.auto_fix, "auto_fix should be true for 'True'");

    std::env::remove_var("DECY_ORACLE_AUTO_FIX");
}

#[test]
fn config_coverage_from_env_auto_fix_false_explicit() {
    let _lock = lock_env();
    std::env::remove_var("DECY_ORACLE_PATTERNS");
    std::env::remove_var("DECY_ORACLE_THRESHOLD");
    std::env::remove_var("DECY_ORACLE_AUTO_FIX");

    std::env::set_var("DECY_ORACLE_AUTO_FIX", "false");
    let config = OracleConfig::from_env();
    assert!(!config.auto_fix, "auto_fix should be false for 'false'");

    std::env::remove_var("DECY_ORACLE_AUTO_FIX");
}

#[test]
fn config_coverage_from_env_auto_fix_arbitrary_string_is_false() {
    let _lock = lock_env();
    std::env::remove_var("DECY_ORACLE_PATTERNS");
    std::env::remove_var("DECY_ORACLE_THRESHOLD");
    std::env::remove_var("DECY_ORACLE_AUTO_FIX");

    std::env::set_var("DECY_ORACLE_AUTO_FIX", "yes");
    let config = OracleConfig::from_env();
    assert!(!config.auto_fix, "'yes' != 'true', should be false");

    std::env::remove_var("DECY_ORACLE_AUTO_FIX");
}

#[test]
fn config_coverage_from_env_auto_fix_empty_string_is_false() {
    let _lock = lock_env();
    std::env::remove_var("DECY_ORACLE_PATTERNS");
    std::env::remove_var("DECY_ORACLE_THRESHOLD");
    std::env::remove_var("DECY_ORACLE_AUTO_FIX");

    std::env::set_var("DECY_ORACLE_AUTO_FIX", "");
    let config = OracleConfig::from_env();
    assert!(!config.auto_fix, "empty string != 'true', should be false");

    std::env::remove_var("DECY_ORACLE_AUTO_FIX");
}

// ============================================================================
// FROM_ENV: ALL VARIABLES SET SIMULTANEOUSLY
// ============================================================================

#[test]
fn config_coverage_from_env_all_vars_set() {
    let _lock = lock_env();
    std::env::set_var("DECY_ORACLE_PATTERNS", "/tmp/test_oracle.apr");
    std::env::set_var("DECY_ORACLE_THRESHOLD", "0.95");
    std::env::set_var("DECY_ORACLE_AUTO_FIX", "true");

    let config = OracleConfig::from_env();
    assert_eq!(config.patterns_path, PathBuf::from("/tmp/test_oracle.apr"));
    assert!(
        (config.confidence_threshold - 0.95_f32).abs() < 0.01,
        "threshold: {}",
        config.confidence_threshold
    );
    assert!(config.auto_fix);
    // max_suggestions and max_retries should be defaults
    assert_eq!(config.max_suggestions, 5);
    assert_eq!(config.max_retries, 3);

    std::env::remove_var("DECY_ORACLE_PATTERNS");
    std::env::remove_var("DECY_ORACLE_THRESHOLD");
    std::env::remove_var("DECY_ORACLE_AUTO_FIX");
}

#[test]
fn config_coverage_from_env_threshold_zero() {
    let _lock = lock_env();
    std::env::remove_var("DECY_ORACLE_PATTERNS");
    std::env::remove_var("DECY_ORACLE_AUTO_FIX");

    std::env::set_var("DECY_ORACLE_THRESHOLD", "0.0");
    let config = OracleConfig::from_env();
    assert!(
        config.confidence_threshold.abs() < f32::EPSILON,
        "threshold should be 0.0, got {}",
        config.confidence_threshold
    );

    std::env::remove_var("DECY_ORACLE_THRESHOLD");
}

#[test]
fn config_coverage_from_env_threshold_one() {
    let _lock = lock_env();
    std::env::remove_var("DECY_ORACLE_PATTERNS");
    std::env::remove_var("DECY_ORACLE_AUTO_FIX");

    std::env::set_var("DECY_ORACLE_THRESHOLD", "1.0");
    let config = OracleConfig::from_env();
    assert!(
        (config.confidence_threshold - 1.0_f32).abs() < f32::EPSILON,
        "threshold should be 1.0, got {}",
        config.confidence_threshold
    );

    std::env::remove_var("DECY_ORACLE_THRESHOLD");
}

#[test]
fn config_coverage_from_env_threshold_negative_parses() {
    let _lock = lock_env();
    std::env::remove_var("DECY_ORACLE_PATTERNS");
    std::env::remove_var("DECY_ORACLE_AUTO_FIX");

    std::env::set_var("DECY_ORACLE_THRESHOLD", "-0.5");
    let config = OracleConfig::from_env();
    assert!(
        (config.confidence_threshold - (-0.5_f32)).abs() < f32::EPSILON,
        "threshold should be -0.5, got {}",
        config.confidence_threshold
    );

    std::env::remove_var("DECY_ORACLE_THRESHOLD");
}

#[test]
fn config_coverage_from_env_threshold_empty_string_uses_default() {
    let _lock = lock_env();
    std::env::remove_var("DECY_ORACLE_PATTERNS");
    std::env::remove_var("DECY_ORACLE_AUTO_FIX");

    std::env::set_var("DECY_ORACLE_THRESHOLD", "");
    let config = OracleConfig::from_env();
    // Empty string fails to parse as f32, so default is used
    assert!(
        (config.confidence_threshold - 0.7_f32).abs() < f32::EPSILON,
        "empty string should fallback to default 0.7, got {}",
        config.confidence_threshold
    );

    std::env::remove_var("DECY_ORACLE_THRESHOLD");
}

#[test]
fn config_coverage_from_env_patterns_relative_path() {
    let _lock = lock_env();
    std::env::remove_var("DECY_ORACLE_THRESHOLD");
    std::env::remove_var("DECY_ORACLE_AUTO_FIX");

    std::env::set_var("DECY_ORACLE_PATTERNS", "relative/path.apr");
    let config = OracleConfig::from_env();
    assert_eq!(config.patterns_path, PathBuf::from("relative/path.apr"));

    std::env::remove_var("DECY_ORACLE_PATTERNS");
}

#[test]
fn config_coverage_from_env_patterns_empty_string() {
    let _lock = lock_env();
    std::env::remove_var("DECY_ORACLE_THRESHOLD");
    std::env::remove_var("DECY_ORACLE_AUTO_FIX");

    std::env::set_var("DECY_ORACLE_PATTERNS", "");
    let config = OracleConfig::from_env();
    assert_eq!(config.patterns_path, PathBuf::from(""));

    std::env::remove_var("DECY_ORACLE_PATTERNS");
}

// ============================================================================
// FROM_FILE: ADDITIONAL BRANCH COVERAGE
// ============================================================================

#[test]
fn config_coverage_from_file_with_patterns_path() {
    let mut file = NamedTempFile::new().unwrap();
    writeln!(
        file,
        r#"
patterns_path = "/custom/oracle/patterns.apr"
confidence_threshold = 0.6
max_suggestions = 3
auto_fix = false
max_retries = 1
"#
    )
    .unwrap();

    let config = OracleConfig::from_file(file.path()).unwrap();
    assert_eq!(config.patterns_path, PathBuf::from("/custom/oracle/patterns.apr"));
    assert!((config.confidence_threshold - 0.6_f32).abs() < f32::EPSILON);
    assert_eq!(config.max_suggestions, 3);
    assert!(!config.auto_fix);
    assert_eq!(config.max_retries, 1);
}

#[test]
fn config_coverage_from_file_only_patterns_path() {
    let mut file = NamedTempFile::new().unwrap();
    writeln!(
        file,
        r#"
patterns_path = "/specific/path.apr"
"#
    )
    .unwrap();

    let config = OracleConfig::from_file(file.path()).unwrap();
    assert_eq!(config.patterns_path, PathBuf::from("/specific/path.apr"));
    // Defaults for everything else
    assert!((config.confidence_threshold - 0.7_f32).abs() < f32::EPSILON);
    assert_eq!(config.max_suggestions, 5);
    assert!(!config.auto_fix);
    assert_eq!(config.max_retries, 3);
}

#[test]
fn config_coverage_from_file_only_auto_fix() {
    let mut file = NamedTempFile::new().unwrap();
    writeln!(file, "auto_fix = true").unwrap();

    let config = OracleConfig::from_file(file.path()).unwrap();
    assert!(config.auto_fix);
    // Other defaults preserved
    assert!((config.confidence_threshold - 0.7_f32).abs() < f32::EPSILON);
    assert_eq!(config.max_suggestions, 5);
    assert_eq!(config.max_retries, 3);
}

#[test]
fn config_coverage_from_file_only_max_retries() {
    let mut file = NamedTempFile::new().unwrap();
    writeln!(file, "max_retries = 100").unwrap();

    let config = OracleConfig::from_file(file.path()).unwrap();
    assert_eq!(config.max_retries, 100);
    // Other defaults preserved
    assert!((config.confidence_threshold - 0.7_f32).abs() < f32::EPSILON);
    assert_eq!(config.max_suggestions, 5);
    assert!(!config.auto_fix);
}

#[test]
fn config_coverage_from_file_only_max_suggestions() {
    let mut file = NamedTempFile::new().unwrap();
    writeln!(file, "max_suggestions = 0").unwrap();

    let config = OracleConfig::from_file(file.path()).unwrap();
    assert_eq!(config.max_suggestions, 0);
}

#[test]
fn config_coverage_from_file_invalid_field_type_returns_error() {
    let mut file = NamedTempFile::new().unwrap();
    writeln!(file, r#"confidence_threshold = "not_a_float""#).unwrap();

    let result = OracleConfig::from_file(file.path());
    assert!(result.is_err());
}

#[test]
fn config_coverage_from_file_unknown_field_ignored() {
    let mut file = NamedTempFile::new().unwrap();
    writeln!(
        file,
        r#"
confidence_threshold = 0.5
unknown_field = "ignored"
"#
    )
    .unwrap();

    // serde(default) with deny_unknown_fields not set should either ignore or error
    // depending on the derive. Let's test what actually happens.
    let result = OracleConfig::from_file(file.path());
    // toml deserialization by default ignores unknown fields
    if let Ok(config) = result {
        assert!((config.confidence_threshold - 0.5_f32).abs() < f32::EPSILON);
    }
    // If it errors, that's also fine -- just exercising the code path
}

// ============================================================================
// SERDE ROUND-TRIP: FULL COVERAGE OF SERIALIZE/DESERIALIZE
// ============================================================================

#[test]
fn config_coverage_serde_json_round_trip() {
    let config = OracleConfig {
        patterns_path: PathBuf::from("/test/serde.apr"),
        confidence_threshold: 0.42,
        max_suggestions: 7,
        auto_fix: true,
        max_retries: 2,
    };

    let json = serde_json::to_string(&config).unwrap();
    let deserialized: OracleConfig = serde_json::from_str(&json).unwrap();

    assert_eq!(deserialized.patterns_path, config.patterns_path);
    assert!((deserialized.confidence_threshold - config.confidence_threshold).abs() < f32::EPSILON);
    assert_eq!(deserialized.max_suggestions, config.max_suggestions);
    assert_eq!(deserialized.auto_fix, config.auto_fix);
    assert_eq!(deserialized.max_retries, config.max_retries);
}

#[test]
fn config_coverage_serde_json_round_trip_defaults() {
    let config = OracleConfig::default();
    let json = serde_json::to_string(&config).unwrap();
    let deserialized: OracleConfig = serde_json::from_str(&json).unwrap();

    assert!((deserialized.confidence_threshold - 0.7_f32).abs() < f32::EPSILON);
    assert_eq!(deserialized.max_suggestions, 5);
    assert!(!deserialized.auto_fix);
    assert_eq!(deserialized.max_retries, 3);
}

#[test]
fn config_coverage_toml_round_trip_all_fields() {
    let config = OracleConfig {
        patterns_path: PathBuf::from("/round/trip.apr"),
        confidence_threshold: 0.33,
        max_suggestions: 42,
        auto_fix: false,
        max_retries: 99,
    };

    let toml_str = toml::to_string(&config).unwrap();
    let deserialized: OracleConfig = toml::from_str(&toml_str).unwrap();

    assert_eq!(deserialized.patterns_path, config.patterns_path);
    assert!((deserialized.confidence_threshold - config.confidence_threshold).abs() < 0.01);
    assert_eq!(deserialized.max_suggestions, config.max_suggestions);
    assert_eq!(deserialized.auto_fix, config.auto_fix);
    assert_eq!(deserialized.max_retries, config.max_retries);
}

#[test]
fn config_coverage_serde_json_from_partial() {
    let json = r#"{"confidence_threshold": 0.99}"#;
    let config: OracleConfig = serde_json::from_str(json).unwrap();
    assert!((config.confidence_threshold - 0.99_f32).abs() < 0.01);
    // Defaults for missing fields
    assert_eq!(config.max_suggestions, 5);
    assert!(!config.auto_fix);
    assert_eq!(config.max_retries, 3);
}

#[test]
fn config_coverage_serde_json_from_empty_object() {
    let json = "{}";
    let config: OracleConfig = serde_json::from_str(json).unwrap();
    assert!((config.confidence_threshold - 0.7_f32).abs() < f32::EPSILON);
    assert_eq!(config.max_suggestions, 5);
    assert!(!config.auto_fix);
    assert_eq!(config.max_retries, 3);
}

// ============================================================================
// DEFAULT IMPLEMENTATION EDGE CASES
// ============================================================================

#[test]
fn config_coverage_default_patterns_path_parent_is_decy_dir() {
    let config = OracleConfig::default();
    let parent = config.patterns_path.parent().unwrap();
    assert!(
        parent.to_string_lossy().ends_with(".decy"),
        "parent dir should be .decy, got: {:?}",
        parent
    );
}

#[test]
fn config_coverage_default_confidence_threshold_in_range() {
    let config = OracleConfig::default();
    assert!(
        config.confidence_threshold >= 0.0 && config.confidence_threshold <= 1.0,
        "default threshold {} should be in [0.0, 1.0]",
        config.confidence_threshold
    );
}

#[test]
fn config_coverage_default_max_suggestions_nonzero() {
    let config = OracleConfig::default();
    assert!(config.max_suggestions > 0);
}

#[test]
fn config_coverage_default_max_retries_nonzero() {
    let config = OracleConfig::default();
    assert!(config.max_retries > 0);
}

#[test]
fn config_coverage_default_auto_fix_disabled() {
    let config = OracleConfig::default();
    assert!(!config.auto_fix);
}

// ============================================================================
// CLONE AND DEBUG TRAIT COVERAGE
// ============================================================================

#[test]
fn config_coverage_clone_preserves_all_fields() {
    let config = OracleConfig {
        patterns_path: PathBuf::from("/clone/test.apr"),
        confidence_threshold: 0.123,
        max_suggestions: 77,
        auto_fix: true,
        max_retries: 11,
    };
    let cloned = config.clone();

    assert_eq!(cloned.patterns_path, PathBuf::from("/clone/test.apr"));
    assert!((cloned.confidence_threshold - 0.123_f32).abs() < f32::EPSILON);
    assert_eq!(cloned.max_suggestions, 77);
    assert!(cloned.auto_fix);
    assert_eq!(cloned.max_retries, 11);
}

#[test]
fn config_coverage_debug_contains_all_field_names() {
    let config = OracleConfig::default();
    let debug = format!("{:?}", config);
    assert!(debug.contains("patterns_path"));
    assert!(debug.contains("confidence_threshold"));
    assert!(debug.contains("max_suggestions"));
    assert!(debug.contains("auto_fix"));
    assert!(debug.contains("max_retries"));
}

#[test]
fn config_coverage_debug_format_with_custom_values() {
    let config = OracleConfig {
        patterns_path: PathBuf::from("/debug/format.apr"),
        confidence_threshold: 0.5,
        max_suggestions: 1,
        auto_fix: true,
        max_retries: 0,
    };
    let debug = format!("{config:?}");
    assert!(debug.contains("/debug/format.apr"));
    assert!(debug.contains("true"));
}

// ============================================================================
// STRUCT FIELD DIRECT ACCESS COVERAGE
// ============================================================================

#[test]
fn config_coverage_field_mutation() {
    let mut config = OracleConfig::default();
    config.patterns_path = PathBuf::from("/mutated.apr");
    config.confidence_threshold = 0.1;
    config.max_suggestions = 100;
    config.auto_fix = true;
    config.max_retries = 50;

    assert_eq!(config.patterns_path, PathBuf::from("/mutated.apr"));
    assert!((config.confidence_threshold - 0.1_f32).abs() < f32::EPSILON);
    assert_eq!(config.max_suggestions, 100);
    assert!(config.auto_fix);
    assert_eq!(config.max_retries, 50);
}

// ============================================================================
// FROM_ENV: PATTERNS_PATH VARIANTS
// ============================================================================

#[test]
fn config_coverage_from_env_patterns_absolute_path() {
    let _lock = lock_env();
    std::env::remove_var("DECY_ORACLE_THRESHOLD");
    std::env::remove_var("DECY_ORACLE_AUTO_FIX");

    std::env::set_var("DECY_ORACLE_PATTERNS", "/absolute/path/to/patterns.apr");
    let config = OracleConfig::from_env();
    assert_eq!(config.patterns_path, PathBuf::from("/absolute/path/to/patterns.apr"));
    assert!(config.patterns_path.is_absolute());

    std::env::remove_var("DECY_ORACLE_PATTERNS");
}

#[test]
fn config_coverage_from_env_patterns_with_spaces() {
    let _lock = lock_env();
    std::env::remove_var("DECY_ORACLE_THRESHOLD");
    std::env::remove_var("DECY_ORACLE_AUTO_FIX");

    std::env::set_var("DECY_ORACLE_PATTERNS", "/path with spaces/patterns.apr");
    let config = OracleConfig::from_env();
    assert_eq!(config.patterns_path, PathBuf::from("/path with spaces/patterns.apr"));

    std::env::remove_var("DECY_ORACLE_PATTERNS");
}

// ============================================================================
// FROM_FILE: TOML CONTENT VARIATIONS
// ============================================================================

#[test]
fn config_coverage_from_file_whitespace_only_uses_defaults() {
    let mut file = NamedTempFile::new().unwrap();
    writeln!(file, "   \n\n   ").unwrap();

    let config = OracleConfig::from_file(file.path()).unwrap();
    assert!((config.confidence_threshold - 0.7_f32).abs() < f32::EPSILON);
    assert_eq!(config.max_suggestions, 5);
}

#[test]
fn config_coverage_from_file_comments_only_uses_defaults() {
    let mut file = NamedTempFile::new().unwrap();
    writeln!(file, "# This is a comment\n# Another comment").unwrap();

    let config = OracleConfig::from_file(file.path()).unwrap();
    assert!((config.confidence_threshold - 0.7_f32).abs() < f32::EPSILON);
}

#[test]
fn config_coverage_from_file_all_fields_with_comments() {
    let mut file = NamedTempFile::new().unwrap();
    writeln!(
        file,
        r#"
# Oracle patterns location
patterns_path = "/commented/path.apr"

# Confidence threshold
confidence_threshold = 0.75

# Max suggestions
max_suggestions = 8

# Auto-fix
auto_fix = true

# Max retries
max_retries = 4
"#
    )
    .unwrap();

    let config = OracleConfig::from_file(file.path()).unwrap();
    assert_eq!(config.patterns_path, PathBuf::from("/commented/path.apr"));
    assert!((config.confidence_threshold - 0.75_f32).abs() < f32::EPSILON);
    assert_eq!(config.max_suggestions, 8);
    assert!(config.auto_fix);
    assert_eq!(config.max_retries, 4);
}

#[test]
fn config_coverage_from_file_extreme_values() {
    let mut file = NamedTempFile::new().unwrap();
    writeln!(
        file,
        r#"
confidence_threshold = 0.0
max_suggestions = 0
auto_fix = false
max_retries = 0
"#
    )
    .unwrap();

    let config = OracleConfig::from_file(file.path()).unwrap();
    assert!(config.confidence_threshold.abs() < f32::EPSILON);
    assert_eq!(config.max_suggestions, 0);
    assert!(!config.auto_fix);
    assert_eq!(config.max_retries, 0);
}

#[test]
fn config_coverage_from_file_large_values() {
    let mut file = NamedTempFile::new().unwrap();
    writeln!(
        file,
        r#"
confidence_threshold = 1.0
max_suggestions = 999999
max_retries = 999999
"#
    )
    .unwrap();

    let config = OracleConfig::from_file(file.path()).unwrap();
    assert!((config.confidence_threshold - 1.0_f32).abs() < f32::EPSILON);
    assert_eq!(config.max_suggestions, 999_999);
    assert_eq!(config.max_retries, 999_999);
}

// ============================================================================
// FROM_ENV + FROM_FILE INTERACTION
// ============================================================================

#[test]
fn config_coverage_env_overrides_are_independent_of_file() {
    let _lock = lock_env();
    // Set env var
    std::env::set_var("DECY_ORACLE_PATTERNS", "/env/path.apr");
    std::env::remove_var("DECY_ORACLE_THRESHOLD");
    std::env::remove_var("DECY_ORACLE_AUTO_FIX");

    // from_env uses env var
    let env_config = OracleConfig::from_env();
    assert_eq!(env_config.patterns_path, PathBuf::from("/env/path.apr"));

    // from_file uses file content (unaffected by env)
    let mut file = NamedTempFile::new().unwrap();
    writeln!(file, r#"patterns_path = "/file/path.apr""#).unwrap();
    let file_config = OracleConfig::from_file(file.path()).unwrap();
    assert_eq!(file_config.patterns_path, PathBuf::from("/file/path.apr"));

    std::env::remove_var("DECY_ORACLE_PATTERNS");
}

// ============================================================================
// TOML DESERIALIZATION ERROR PATHS
// ============================================================================

#[test]
fn config_coverage_from_file_invalid_toml_syntax() {
    let mut file = NamedTempFile::new().unwrap();
    writeln!(file, "[[[nested").unwrap();

    let result = OracleConfig::from_file(file.path());
    assert!(result.is_err());
}

#[test]
fn config_coverage_from_file_wrong_value_type_bool_as_string() {
    let mut file = NamedTempFile::new().unwrap();
    writeln!(file, r#"auto_fix = "yes""#).unwrap();

    let result = OracleConfig::from_file(file.path());
    assert!(result.is_err());
}

#[test]
fn config_coverage_from_file_wrong_value_type_int_as_string() {
    let mut file = NamedTempFile::new().unwrap();
    writeln!(file, r#"max_retries = "ten""#).unwrap();

    let result = OracleConfig::from_file(file.path());
    assert!(result.is_err());
}

// ============================================================================
// TOML SERIALIZATION: ENSURE ALL FIELDS PRESENT
// ============================================================================

#[test]
fn config_coverage_toml_serialization_default() {
    let config = OracleConfig::default();
    let toml_str = toml::to_string(&config).unwrap();

    // All field names must appear in serialized output
    assert!(toml_str.contains("patterns_path"));
    assert!(toml_str.contains("confidence_threshold"));
    assert!(toml_str.contains("max_suggestions"));
    assert!(toml_str.contains("auto_fix"));
    assert!(toml_str.contains("max_retries"));
}

#[test]
fn config_coverage_toml_serialization_custom() {
    let config = OracleConfig {
        patterns_path: PathBuf::from("/ser/test.apr"),
        confidence_threshold: 0.55,
        max_suggestions: 3,
        auto_fix: true,
        max_retries: 7,
    };
    let toml_str = toml::to_string(&config).unwrap();

    assert!(toml_str.contains("/ser/test.apr"));
    assert!(toml_str.contains("auto_fix = true"));
    assert!(toml_str.contains("max_suggestions = 3"));
    assert!(toml_str.contains("max_retries = 7"));
}