alef 0.83.0

Opinionated polyglot binding generator for Rust libraries
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
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
use super::*;
use crate::core::ir::{ApiSurface, EnumDef, EnumVariant, TypeDef};
use tracing_test::traced_test;

mod drift;
mod enabled_features;
mod feature_forwarding;

#[test]
fn combine_gates_drops_an_owner_the_member_already_requires() {
    assert_eq!(
        combine_gates(
            r#"feature = "client""#,
            r#"all(feature = "client", feature = "streaming")"#
        ),
        r#"all(feature = "client", feature = "streaming")"#
    );
}

#[test]
fn combine_gates_keeps_both_operands_when_the_member_does_not_imply_the_owner() {
    assert_eq!(
        combine_gates(r#"feature = "client""#, r#"feature = "streaming""#),
        r#"all(feature = "client", feature = "streaming")"#
    );
}

#[test]
fn combine_gates_does_not_collapse_two_predicates_the_parser_cannot_read() {
    // Both sides parse to `CfgPredicate::Other`, which compares equal without being the same
    // condition. Collapsing here would compile the member in on a target the owner excludes.
    assert_eq!(
        combine_gates("target_os = \"macos\"", "target_os = \"linux\""),
        "all(target_os = \"macos\", target_os = \"linux\")"
    );
}

#[test]
fn combine_gates_does_not_treat_a_disjunct_as_implying_the_owner() {
    // `any(a, b)` holding does not guarantee `a`; only conjunction licenses the drop.
    assert_eq!(
        combine_gates(
            r#"feature = "client""#,
            r#"any(feature = "client", feature = "server")"#
        ),
        r#"all(feature = "client", any(feature = "client", feature = "server"))"#
    );
}

#[test]
fn collect_cfg_feature_names_simple_feature() {
    let mut out = BTreeSet::new();
    collect_cfg_feature_names(r#"feature = "pdf""#, &mut out);
    assert_eq!(out, BTreeSet::from(["pdf".to_string()]));
}

#[test]
fn collect_cfg_feature_names_any_compound() {
    let mut out = BTreeSet::new();
    collect_cfg_feature_names(r#"any(feature = "html", feature = "xml")"#, &mut out);
    let want: BTreeSet<String> = ["html", "xml"].into_iter().map(String::from).collect();
    assert_eq!(out, want);
}

#[test]
fn collect_cfg_feature_names_all_compound() {
    let mut out = BTreeSet::new();
    collect_cfg_feature_names(
        r#"all(feature = "layout-types", not(feature = "wasm-target"))"#,
        &mut out,
    );
    let want: BTreeSet<String> = ["layout-types", "wasm-target"].into_iter().map(String::from).collect();
    assert_eq!(out, want);
}

#[test]
fn parse_cfg_predicate_simple_feature() {
    assert_eq!(
        parse_cfg_predicate(r#"feature = "tokenizer""#),
        CfgPredicate::Feature("tokenizer".to_string())
    );
}

#[test]
fn parse_cfg_predicate_any_preserves_arms() {
    assert_eq!(
        parse_cfg_predicate(r#"any(feature = "native-http", feature = "wasm-http")"#),
        CfgPredicate::Any(vec![
            CfgPredicate::Feature("native-http".to_string()),
            CfgPredicate::Feature("wasm-http".to_string()),
        ])
    );
}

#[test]
fn parse_cfg_predicate_all_preserves_arms() {
    assert_eq!(
        parse_cfg_predicate(r#"all(feature = "layout-types", not(feature = "wasm-target"))"#),
        CfgPredicate::All(vec![
            CfgPredicate::Feature("layout-types".to_string()),
            CfgPredicate::Not(Box::new(CfgPredicate::Feature("wasm-target".to_string()))),
        ])
    );
}

#[test]
fn parse_cfg_predicate_not() {
    assert_eq!(
        parse_cfg_predicate(r#"not(feature = "wasm-target")"#),
        CfgPredicate::Not(Box::new(CfgPredicate::Feature("wasm-target".to_string())))
    );
}

#[test]
fn parse_cfg_predicate_unrecognised_is_other() {
    assert_eq!(parse_cfg_predicate(r#"target_arch = "wasm32""#), CfgPredicate::Other);
}

#[test]
fn collect_cfg_feature_names_ignores_non_feature_cfg() {
    let mut out = BTreeSet::new();
    collect_cfg_feature_names(r#"target_arch = "wasm32""#, &mut out);
    assert!(out.is_empty());
}

#[test]
fn collect_cfg_feature_names_whitespace_normalisation() {
    let mut out = BTreeSet::new();
    collect_cfg_feature_names(r#"any (feature = "a" , feature = "b")"#, &mut out);
    let want: BTreeSet<String> = ["a", "b"].into_iter().map(String::from).collect();
    assert_eq!(out, want);
}

#[test]
fn collect_cfg_features_walks_types_enums_functions() {
    let mut out = BTreeSet::new();
    collect_cfg_feature_names(r#"feature = "pdf""#, &mut out);
    collect_cfg_feature_names(r#"any(feature = "html", feature = "xml")"#, &mut out);
    collect_cfg_feature_names(
        r#"all(feature = "layout-types", not(feature = "wasm-target"))"#,
        &mut out,
    );
    collect_cfg_feature_names(r#"target_arch = "wasm32""#, &mut out);
    let want: BTreeSet<String> = ["html", "layout-types", "pdf", "wasm-target", "xml"]
        .into_iter()
        .map(String::from)
        .collect();
    assert_eq!(out, want);
}

#[test]
fn any_group_feature_names_reads_a_two_arm_any_gate() {
    let want: BTreeSet<String> = ["engine-native", "engine-portable"]
        .into_iter()
        .map(String::from)
        .collect();
    assert_eq!(
        any_group_feature_names(r#"any(feature = "engine-native", feature = "engine-portable")"#),
        Some(want)
    );
}

#[test]
fn any_group_feature_names_normalises_stringified_whitespace() {
    let want: BTreeSet<String> = ["a", "b"].into_iter().map(String::from).collect();
    assert_eq!(
        any_group_feature_names(r#"any (feature = "a" , feature = "b")"#),
        Some(want)
    );
}

#[test]
fn any_group_feature_names_rejects_a_single_feature_gate() {
    assert_eq!(any_group_feature_names(r#"feature = "engine-native""#), None);
}

#[test]
fn any_group_feature_names_rejects_an_all_gate() {
    assert_eq!(
        any_group_feature_names(r#"all(feature = "engine-native", feature = "engine-portable")"#),
        None
    );
}

#[test]
fn any_group_feature_names_ignores_a_nested_non_feature_arm() {
    // Only two bare `feature = "..."` arms are extracted; a third arm that is not a bare feature
    // (here nested inside `not(...)`) does not count toward the two-arm minimum on its own, but
    // does not block the two arms that do qualify either.
    let want: BTreeSet<String> = ["engine-native", "engine-portable"]
        .into_iter()
        .map(String::from)
        .collect();
    assert_eq!(
        any_group_feature_names(
            r#"any(feature = "engine-native", feature = "engine-portable", not(target_arch = "wasm32"))"#
        ),
        Some(want)
    );
}

/// The relationship [`collect_cfg_feature_alternatives`] exists to expose: a source crate gates
/// one capability behind either of two sibling features, and a caller widening a binding's
/// literal Cargo feature list needs to discover that pairing from the parsed cfg strings instead
/// of a hard-coded feature name.
#[test]
fn collect_cfg_feature_alternatives_finds_an_any_gated_pair() {
    let api = ApiSurface {
        crate_name: "mylib".to_string(),
        types: vec![TypeDef {
            name: "Engine".to_string(),
            rust_path: "mylib::Engine".to_string(),
            cfg: Some(r#"any(feature = "engine-native", feature = "engine-portable")"#.to_string()),
            ..Default::default()
        }],
        ..Default::default()
    };

    let groups = collect_cfg_feature_alternatives(&api);
    let want: BTreeSet<String> = ["engine-native", "engine-portable"]
        .into_iter()
        .map(String::from)
        .collect();
    assert_eq!(groups, vec![want]);
}

#[test]
fn collect_cfg_feature_alternatives_ignores_a_single_feature_gate() {
    let api = ApiSurface {
        crate_name: "mylib".to_string(),
        types: vec![TypeDef {
            name: "Engine".to_string(),
            rust_path: "mylib::Engine".to_string(),
            cfg: Some(r#"feature = "engine-native""#.to_string()),
            ..Default::default()
        }],
        ..Default::default()
    };

    assert!(collect_cfg_feature_alternatives(&api).is_empty());
}

#[test]
fn collect_cfg_features_full_surface_walk() {
    let api = ApiSurface {
        types: vec![TypeDef {
            name: "PdfDoc".to_string(),
            rust_path: "mylib::PdfDoc".to_string(),
            cfg: Some(r#"feature = "pdf""#.to_string()),
            ..Default::default()
        }],
        enums: vec![EnumDef {
            name: "ImageOutputFormat".to_string(),
            variants: vec![
                EnumVariant {
                    name: "Native".to_string(),
                    cfg: None,
                    ..Default::default()
                },
                EnumVariant {
                    name: "Heic".to_string(),
                    cfg: Some(r#"feature = "heic""#.to_string()),
                    ..Default::default()
                },
            ],
            ..Default::default()
        }],
        ..Default::default()
    };
    let features = collect_cfg_features(&api);
    let want: BTreeSet<String> = ["heic", "pdf"].into_iter().map(String::from).collect();
    assert_eq!(features, want);
}

#[test]
fn collect_cfg_features_includes_method_gates() {
    // A Rust-emitting backend re-emits a gated method's `#[cfg(feature = "X")]` into its own
    // crate, so `X` must reach that crate's `[features]` table or the build fails with
    // `unexpected cfg condition value`. ~keep
    use crate::core::ir::MethodDef;

    let api = ApiSurface {
        crate_name: "mylib".to_string(),
        types: vec![TypeDef {
            name: "Client".to_string(),
            rust_path: "mylib::Client".to_string(),
            methods: vec![
                MethodDef {
                    name: "ping".to_string(),
                    ..Default::default()
                },
                MethodDef {
                    name: "stream".to_string(),
                    cfg: Some(r#"feature = "streaming""#.to_string()),
                    ..Default::default()
                },
            ],
            ..Default::default()
        }],
        enums: vec![EnumDef {
            name: "Format".to_string(),
            rust_path: "mylib::Format".to_string(),
            methods: vec![MethodDef {
                name: "from_mime".to_string(),
                cfg: Some(r#"all(feature = "mime", feature = "sniff")"#.to_string()),
                ..Default::default()
            }],
            ..Default::default()
        }],
        ..Default::default()
    };

    let want: BTreeSet<String> = ["mime", "sniff", "streaming"].into_iter().map(String::from).collect();
    assert_eq!(collect_cfg_features(&api), want);
}

/// A Rust-emitting backend that re-emits a gated service's constructor or configurator gate
/// (mirroring how it already re-emits a gated method's) needs `X` declared in the manifest for
/// the same `unexpected cfg condition value` reason `collect_cfg_features_includes_method_gates`
/// covers for methods. `ServiceDef` and its `constructor`/`configurators` `MethodDef`s were
/// previously not walked at all, unlike `backends::ffi::gen_bindings::helpers::cbindgen_feature_defines`,
/// which already reads `ServiceDef::cfg` for the FFI header's `#if` guards — this test pins the
/// shared helper to the same coverage. ~keep
#[test]
fn collect_cfg_features_includes_service_and_configurator_gates() {
    use crate::core::ir::{MethodDef, ServiceDef};

    let api = ApiSurface {
        crate_name: "mylib".to_string(),
        services: vec![ServiceDef {
            name: "ClientConfig".to_string(),
            rust_path: "mylib::client::ClientConfig".to_string(),
            constructor: MethodDef {
                name: "new".to_string(),
                ..Default::default()
            },
            configurators: vec![
                MethodDef {
                    name: "with_timeout".to_string(),
                    ..Default::default()
                },
                MethodDef {
                    name: "with_tower_layer".to_string(),
                    cfg: Some(r#"feature = "tower""#.to_string()),
                    ..Default::default()
                },
            ],
            registrations: vec![],
            entrypoints: vec![],
            doc: String::new(),
            cfg: None,
        }],
        ..Default::default()
    };

    let want: BTreeSet<String> = ["tower".to_string()].into_iter().collect();
    assert_eq!(collect_cfg_features(&api), want);
}

/// A service merged from a foreign `[[crates.source_crates]]` crate must not forward its cfg
/// to the host crate's `[features]` table, for the same reason a merged type/enum doesn't
/// (see `collect_cfg_features_excludes_external_source_crate_cfgs`) — forwarding would
/// reference a feature the host crate does not define.
#[test]
fn collect_cfg_features_excludes_external_source_crate_service_cfgs() {
    use crate::core::ir::{MethodDef, ServiceDef};

    let api = ApiSurface {
        crate_name: "hostlib".to_string(),
        services: vec![ServiceDef {
            name: "OtherService".to_string(),
            rust_path: "otherlib::OtherService".to_string(),
            constructor: MethodDef::default(),
            configurators: vec![],
            registrations: vec![],
            entrypoints: vec![],
            doc: String::new(),
            cfg: Some(r#"feature = "foreign-only""#.to_string()),
        }],
        ..Default::default()
    };

    assert!(
        collect_cfg_features(&api).is_empty(),
        "a foreign-owned service's cfg must not forward to the host crate"
    );
}

#[test]
fn collect_cfg_features_excludes_external_source_crate_cfgs() {
    // A type/enum merged from `[[crates.source_crates]]` carries the foreign crate's rust_path ~keep
    // and cfg gates. Its features must NOT be forwarded to the host crate (they'd map to ~keep
    // `<host>/<feat>` for a feature the host does not define, breaking cargo resolution). ~keep
    let api = ApiSurface {
        crate_name: "hostlib".to_string(),
        types: vec![TypeDef {
            name: "HostDoc".to_string(),
            rust_path: "hostlib::HostDoc".to_string(),
            cfg: Some(r#"feature = "pdf""#.to_string()),
            ..Default::default()
        }],
        enums: vec![EnumDef {
            name: "Strategy".to_string(),
            rust_path: "otherlib::Strategy".to_string(),
            variants: vec![
                EnumVariant {
                    name: "Auto".to_string(),
                    cfg: None,
                    ..Default::default()
                },
                EnumVariant {
                    name: "Advanced".to_string(),
                    cfg: Some(r#"any(test, feature = "foreign-only")"#.to_string()),
                    ..Default::default()
                },
            ],
            ..Default::default()
        }],
        ..Default::default()
    };
    let features = collect_cfg_features(&api);
    assert_eq!(
        features,
        BTreeSet::from(["pdf".to_string()]),
        "host `pdf` must forward; the foreign `foreign-only` feature must not leak into host passthrough"
    );
}

/// [`is_host_owned_rust_path`] is the single authority `collect_cfg_gates` and backend arm/
/// variant emitters (`gen_enum_from_i32_rs_helper`, the Swift enum wrapper) both call to decide
/// whether a cfg-gated item's cfg is safe to re-emit verbatim. Pinning its own behavior here
/// keeps `collect_cfg_features_excludes_external_source_crate_cfgs`'s coverage of the
/// feature-collection side from silently diverging from the emitter side.
#[test]
fn is_host_owned_rust_path_matches_the_leading_path_segment() {
    assert!(is_host_owned_rust_path("hostlib", "hostlib::Strategy"));
    assert!(!is_host_owned_rust_path("hostlib", "otherlib::Strategy"));
}

#[test]
fn is_host_owned_rust_path_normalises_dashes_in_the_host_crate_name() {
    assert!(is_host_owned_rust_path("host-lib", "host_lib::Strategy"));
}

#[test]
fn is_host_owned_rust_path_is_permissive_for_an_unknown_host_crate_name() {
    assert!(
        is_host_owned_rust_path("", "otherlib::Strategy"),
        "an unknown (empty) host crate name must not falsely mark everything foreign"
    );
}

/// An unqualified `rust_path` (no `::`) is only permissive by accident of comparing an empty
/// first segment; a non-empty single segment is compared against the host crate name like any
/// other leading segment, so it reads as foreign unless it happens to equal the host name.
#[test]
fn is_host_owned_rust_path_compares_a_bare_name_like_any_leading_segment() {
    assert!(is_host_owned_rust_path("hostlib", "hostlib"));
    assert!(!is_host_owned_rust_path("hostlib", "Strategy"));
}

fn api_with_gated_functions(names_and_cfgs: &[(&str, Option<&str>)]) -> ApiSurface {
    use crate::core::ir::FunctionDef;
    ApiSurface {
        crate_name: "test_lib".to_string(),
        functions: names_and_cfgs
            .iter()
            .map(|(name, cfg)| FunctionDef {
                name: (*name).to_string(),
                rust_path: format!("test_lib::{name}"),
                cfg: cfg.map(|s| s.to_string()),
                ..Default::default()
            })
            .collect(),
        ..Default::default()
    }
}

#[test]
fn undeclared_cfg_features_returns_only_missing_names() {
    let api = api_with_gated_functions(&[
        ("count_tokens", Some(r#"feature = "tokenizer""#)),
        ("with_tower_layer", Some(r#"feature = "tower""#)),
        ("completion_cost", None),
    ]);
    let declared: BTreeSet<String> = ["tower".to_string()].into_iter().collect();
    assert_eq!(
        undeclared_cfg_features(&api, &declared),
        BTreeSet::from(["tokenizer".to_string()]),
        "only the feature missing from `declared` should be reported"
    );
}

#[test]
fn undeclared_cfg_features_empty_when_everything_declared() {
    let api = api_with_gated_functions(&[("count_tokens", Some(r#"feature = "tokenizer""#))]);
    let declared: BTreeSet<String> = ["tokenizer".to_string()].into_iter().collect();
    assert!(undeclared_cfg_features(&api, &declared).is_empty());
}

#[test]
fn read_declared_cargo_features_none_when_file_missing() {
    let dir = tempfile::tempdir().expect("tempdir");
    let manifest = dir.path().join("Cargo.toml");
    assert!(
        read_declared_cargo_features(&manifest).is_none(),
        "an unscaffolded crate has nothing to verify against yet"
    );
}

#[test]
fn read_declared_cargo_features_reads_features_table() {
    let dir = tempfile::tempdir().expect("tempdir");
    let manifest = dir.path().join("Cargo.toml");
    std::fs::write(
        &manifest,
        "[package]\nname = \"x\"\n\n[features]\ndefault = [\"native-http\"]\nnative-http = [\"core/native-http\"]\n",
    )
    .expect("write manifest");
    assert_eq!(
        read_declared_cargo_features(&manifest),
        Some(BTreeSet::from(["default".to_string(), "native-http".to_string()]))
    );
}

#[test]
fn read_declared_cargo_features_empty_set_when_no_features_table() {
    let dir = tempfile::tempdir().expect("tempdir");
    let manifest = dir.path().join("Cargo.toml");
    std::fs::write(&manifest, "[package]\nname = \"x\"\n").expect("write manifest");
    assert_eq!(
        read_declared_cargo_features(&manifest),
        Some(BTreeSet::new()),
        "a manifest scaffolded before any cfg-gated symbol existed has no [features] table at \
         all, which must still be distinguishable from a missing file"
    );
}

/// Reproduces the sample-core incident: the binding crate's Cargo.toml declares only the
/// features it had at scaffold time (`native-http`), but the generated source now references
/// `tokenizer` because the core crate gained a cfg-gated function since then.
#[traced_test]
#[test]
fn warn_on_undeclared_binding_cfg_features_warns_when_feature_is_undeclared() {
    let dir = tempfile::tempdir().expect("tempdir");
    let manifest = dir.path().join("Cargo.toml");
    std::fs::write(
        &manifest,
        "[package]\nname = \"x\"\n\n[features]\ndefault = [\"native-http\"]\nnative-http = [\"core/native-http\"]\n",
    )
    .expect("write manifest");
    let api = api_with_gated_functions(&[("count_tokens", Some(r#"feature = "tokenizer""#))]);

    warn_on_undeclared_binding_cfg_features(&api, Language::Ruby, &manifest);

    assert!(
        logs_contain("does not enable by default"),
        "a stale manifest missing a referenced feature must produce a warning"
    );
}

/// Reproduces the actual liter-llm-style incident this fix targets: an earlier repair pass (or a
/// hand-edit) added the forwarding row (`tokenizer = ["core/tokenizer"]`) but never added
/// `tokenizer` to `default`. A manifest in exactly this shape must still warn -- declaring a
/// feature is not the same as turning it on -- which is the whole reason the check now keys on
/// [`read_default_enabled_cargo_features`] instead of [`read_declared_cargo_features`].
#[traced_test]
#[test]
fn warn_on_undeclared_binding_cfg_features_warns_when_declared_but_not_in_default() {
    let dir = tempfile::tempdir().expect("tempdir");
    let manifest = dir.path().join("Cargo.toml");
    std::fs::write(
        &manifest,
        "[package]\nname = \"x\"\n\n[features]\ndefault = [\"native-http\"]\n\
         native-http = [\"core/native-http\"]\ntokenizer = [\"core/tokenizer\"]\n",
    )
    .expect("write manifest");
    let api = api_with_gated_functions(&[("count_tokens", Some(r#"feature = "tokenizer""#))]);

    warn_on_undeclared_binding_cfg_features(&api, Language::Ruby, &manifest);

    assert!(
        logs_contain("does not enable by default"),
        "a feature that is declared but unreachable from `default` must still warn -- \
         `cargo rustc --print cfg` proves it is not actually on"
    );
}

#[traced_test]
#[test]
fn warn_on_undeclared_binding_cfg_features_silent_when_manifest_declares_everything() {
    let dir = tempfile::tempdir().expect("tempdir");
    let manifest = dir.path().join("Cargo.toml");
    std::fs::write(
        &manifest,
        "[package]\nname = \"x\"\n\n[features]\ndefault = [\"tokenizer\"]\ntokenizer = [\"core/tokenizer\"]\n",
    )
    .expect("write manifest");
    let api = api_with_gated_functions(&[("count_tokens", Some(r#"feature = "tokenizer""#))]);

    warn_on_undeclared_binding_cfg_features(&api, Language::Ruby, &manifest);

    assert!(
        !logs_contain("does not enable by default"),
        "a fully up-to-date manifest must not warn"
    );
}

#[traced_test]
#[test]
fn warn_on_undeclared_binding_cfg_features_silent_when_manifest_missing() {
    let dir = tempfile::tempdir().expect("tempdir");
    let manifest = dir.path().join("Cargo.toml");
    let api = api_with_gated_functions(&[("count_tokens", Some(r#"feature = "tokenizer""#))]);

    warn_on_undeclared_binding_cfg_features(&api, Language::Ruby, &manifest);

    assert!(
        !logs_contain("does not enable by default"),
        "an unscaffolded crate (no manifest on disk yet) must not warn -- there is nothing to \
         verify against"
    );
}

fn resolved_config(toml_text: &str) -> ResolvedCrateConfig {
    let cfg: crate::core::config::NewAlefConfig = toml::from_str(toml_text).expect("valid config");
    cfg.resolve().expect("resolve").remove(0)
}

/// [`effective_ffi_default_features`] must union the FFI language's own configured feature list
/// with every feature name discovered by walking the API surface's cfg gates, and it must
/// exclude `extra_features` (declare-only, never defaulted) from that union even when the same
/// name is also discovered from a gate.
#[test]
fn effective_ffi_default_features_unions_configured_and_cfg_discovered_names() {
    let config = resolved_config(
        r#"
[workspace]
languages = ["ffi"]
[[crates]]
name = "sample-core"
sources = []
[crates.ffi]
features = ["shared"]
extra_features = ["wasm-http"]
"#,
    );
    let api = api_with_gated_functions(&[
        ("configured_only", None),
        ("discovered_gate", Some(r#"feature = "sample-gate""#)),
        ("declare_only_gate", Some(r#"feature = "wasm-http""#)),
    ]);

    let effective: BTreeSet<String> = effective_ffi_default_features(&api, &config).into_iter().collect();

    assert_eq!(
        effective,
        BTreeSet::from(["shared".to_string(), "sample-gate".to_string()]),
        "must include the configured FFI feature and the discovered gate, but exclude the \
         declare-only extra_features entry even though it is also gated on"
    );
}

/// Table-driven coverage for [`merge_missing_cfg_features`]: whether it patches an existing
/// binding manifest's `[features]` table, and what it must never touch while doing so.
struct MergeCase {
    name: &'static str,
    existing: &'static str,
    gated_functions: &'static [(&'static str, Option<&'static str>)],
    core_declared_features: &'static [&'static str],
    /// Substrings the patched output must contain. Empty when [`Self::expect_none`] is `true`.
    expect_contains: &'static [&'static str],
    /// Substrings that must survive verbatim -- proof the merge is additive-only, not a
    /// regeneration. Checked against `existing` when [`Self::expect_none`] is `true`.
    expect_preserved: &'static [&'static str],
    /// `true` when nothing referenced is missing (or nothing missing may be invented), so the
    /// function must return `Ok(None)` rather than rewrite the file.
    expect_none: bool,
}

#[test]
fn merge_missing_cfg_features_table() {
    let cases = [
        MergeCase {
            name: "a manifest missing one gated feature gets it added",
            existing: "[package]\nname = \"x\"\n\n[features]\n\
                       default = [\"native-http\"]\n\
                       native-http = [\"core/native-http\"]\n\n\
                       [dependencies]\nserde = \"1\"\n",
            gated_functions: &[("count_tokens", Some(r#"feature = "tokenizer""#))],
            core_declared_features: &["native-http", "tokenizer"],
            expect_contains: &[
                r#"tokenizer = ["core/tokenizer"]"#,
                r#"default = ["native-http", "tokenizer"]"#,
            ],
            expect_preserved: &[
                r#"native-http = ["core/native-http"]"#,
                "[dependencies]",
                "serde = \"1\"",
            ],
            expect_none: false,
        },
        MergeCase {
            name: "a manifest already declaring and defaulting the feature is left unchanged",
            existing: "[package]\nname = \"x\"\n\n[features]\n\
                       default = [\"tokenizer\"]\n\
                       tokenizer = [\"core/tokenizer\"]\n",
            gated_functions: &[("count_tokens", Some(r#"feature = "tokenizer""#))],
            core_declared_features: &["tokenizer"],
            expect_contains: &[],
            expect_preserved: &[r#"tokenizer = ["core/tokenizer"]"#],
            expect_none: true,
        },
        MergeCase {
            name: "a feature declared but excluded from default is added to default, not redeclared",
            existing: "[package]\nname = \"x\"\n\n[features]\n\
                       default = [\"native-http\"]\n\
                       native-http = [\"core/native-http\"]\n\
                       tokenizer = [\"core/tokenizer\"]\n",
            gated_functions: &[("count_tokens", Some(r#"feature = "tokenizer""#))],
            core_declared_features: &["native-http", "tokenizer"],
            expect_contains: &[r#"default = ["native-http", "tokenizer"]"#],
            expect_preserved: &[
                r#"tokenizer = ["core/tokenizer"]"#,
                r#"native-http = ["core/native-http"]"#,
            ],
            expect_none: false,
        },
        MergeCase {
            name: "a manifest with no [features] table at all gets one created, feature enabled by default",
            existing: "[package]\nname = \"x\"\nversion = \"0.1.0\"\n\n[dependencies]\nserde = \"1\"\n",
            gated_functions: &[("count_tokens", Some(r#"feature = "tokenizer""#))],
            core_declared_features: &["tokenizer"],
            expect_contains: &[
                "[features]",
                r#"tokenizer = ["core/tokenizer"]"#,
                r#"default = ["tokenizer"]"#,
            ],
            expect_preserved: &["[dependencies]", "serde = \"1\""],
            expect_none: false,
        },
        MergeCase {
            name: "a gated feature the core crate does not declare is never invented or defaulted",
            existing: "[package]\nname = \"x\"\n\n[features]\ndefault = []\n",
            gated_functions: &[("count_tokens", Some(r#"feature = "tokenizer""#))],
            core_declared_features: &[],
            expect_contains: &[],
            expect_preserved: &["default = []"],
            expect_none: true,
        },
    ];

    for case in cases {
        let api = api_with_gated_functions(case.gated_functions);
        let core_declared: BTreeSet<String> = case.core_declared_features.iter().map(|s| s.to_string()).collect();

        let result = merge_missing_cfg_features(case.existing, &api, "core", &core_declared)
            .unwrap_or_else(|error| panic!("case `{}`: merge failed: {error}", case.name));

        if case.expect_none {
            assert!(
                result.is_none(),
                "case `{}`: expected no change, got:\n{:?}",
                case.name,
                result
            );
            for needle in case.expect_preserved {
                assert!(
                    case.existing.contains(needle),
                    "case `{}`: control assertion failed -- `{needle}` must be in the original fixture",
                    case.name
                );
            }
            continue;
        }

        let patched = result.unwrap_or_else(|| panic!("case `{}`: expected a patch, got None", case.name));
        for needle in case.expect_contains {
            assert!(
                patched.contains(needle),
                "case `{}`: patched manifest missing `{needle}`, got:\n{patched}",
                case.name
            );
        }
        for needle in case.expect_preserved {
            assert!(
                patched.contains(needle),
                "case `{}`: patch dropped or reformatted `{needle}`, got:\n{patched}",
                case.name
            );
        }
    }
}

/// Beyond substring checks: every line of a manifest untouched by the merge must survive
/// byte-for-byte, including comments and a section the merge has no knowledge of -- proof this
/// is a surgical insert, not a parse/reserialize that happens to look similar.
#[test]
fn merge_missing_cfg_features_preserves_untouched_lines_verbatim() {
    let existing = "# top-of-file comment, hand-written\n\
                     [package]\n\
                     name = \"x\"\n\
                     version = \"0.1.0\"\n\n\
                     [features]\n\
                     # a feature comment the user wrote\n\
                     default = [\"native-http\"]\n\
                     native-http = [\"core/native-http\"]\n\n\
                     [package.metadata.cargo-machete]\n\
                     ignored = [\"rb-sys\"]\n";
    let api = api_with_gated_functions(&[("count_tokens", Some(r#"feature = "tokenizer""#))]);
    let core_declared: BTreeSet<String> = ["native-http", "tokenizer"].into_iter().map(String::from).collect();

    let patched = merge_missing_cfg_features(existing, &api, "core", &core_declared)
        .expect("merge must succeed")
        .expect("a missing feature must produce a patch");

    // The `default` line is the one deliberate exception: enabling the backfilled feature is the
    // point of this merge, so its own value array is expected to change. ~keep
    for line in existing.lines().filter(|line| !line.starts_with("default = ")) {
        assert!(
            patched.contains(line),
            "line `{line}` from the original manifest did not survive the merge verbatim, got:\n{patched}"
        );
    }
    assert!(
        patched.contains(r#"tokenizer = ["core/tokenizer"]"#),
        "the missing feature must still be declared, got:\n{patched}"
    );
    assert!(
        patched.contains(r#"default = ["native-http", "tokenizer"]"#),
        "the backfilled feature must also be enabled by default, not merely declared, got:\n{patched}"
    );
}

/// [`core_crate_declared_features`] must read the core crate's own `[features]` table off disk,
/// resolved the same way [`crate::scaffold::core_crate_manifest_path`] locates it, and must fall
/// back to an empty set (never a panic or a guess) when the manifest cannot be found.
#[test]
fn core_crate_declared_features_reads_the_core_manifest() {
    let dir = tempfile::tempdir().expect("tempdir");
    let ws_root = dir.path();
    let core_dir = ws_root.join("crates").join("my-lib");
    std::fs::create_dir_all(&core_dir).expect("create core crate dir");
    std::fs::write(
        core_dir.join("Cargo.toml"),
        "[package]\nname = \"my-lib\"\n\n[features]\ndefault = []\ntokenizer = []\ntower = []\n",
    )
    .expect("write core Cargo.toml");

    let config = crate::core::config::ResolvedCrateConfig {
        workspace_root: Some(ws_root.to_path_buf()),
        name: "my-lib".to_string(),
        sources: vec![std::path::PathBuf::from("crates/my-lib/src/lib.rs")],
        ..Default::default()
    };

    assert_eq!(
        core_crate_declared_features(&config),
        BTreeSet::from(["default".to_string(), "tokenizer".to_string(), "tower".to_string()])
    );
}

#[test]
fn core_crate_declared_features_empty_when_workspace_root_unset() {
    let config = crate::core::config::ResolvedCrateConfig::default();
    assert!(core_crate_declared_features(&config).is_empty());
}

/// [`expand_configured_features`] must follow the core crate's own `[features]` table so a
/// configured aggregate name also stands for every member it enables -- transitively, through a
/// nested aggregate -- while leaving unrelated names alone.
///
/// Without this, `cfg_feature_satisfied` (which matches literally and hard-codes only `full` as a
/// universal umbrella) reports every `#[cfg(feature = "<member>")]` gate unsatisfied for a
/// binding configured with the aggregate, and the gated items vanish from that surface with no
/// diagnostic even though cargo compiles them.
#[test]
fn expand_configured_features_follows_the_core_crates_feature_graph() {
    let dir = tempfile::tempdir().expect("tempdir");
    let core_dir = dir.path().join("crates").join("my-lib");
    std::fs::create_dir_all(&core_dir).expect("create core crate dir");
    std::fs::write(
        core_dir.join("Cargo.toml"),
        "[package]\nname = \"my-lib\"\n\n[features]\ndefault = []\n\
         mobile-target = [\"tokenizer\", \"text\"]\ntext = [\"markup\"]\nmarkup = []\n\
         tokenizer = [\"dep:tok\", \"other-crate/tok\"]\nunrelated = []\n",
    )
    .expect("write core Cargo.toml");
    let config = crate::core::config::ResolvedCrateConfig {
        workspace_root: Some(dir.path().to_path_buf()),
        name: "my-lib".to_string(),
        sources: vec![std::path::PathBuf::from("crates/my-lib/src/lib.rs")],
        ..Default::default()
    };

    let expanded: BTreeSet<String> = expand_configured_features(&config, &["mobile-target".to_string()])
        .into_iter()
        .collect();

    assert_eq!(
        expanded,
        BTreeSet::from([
            "mobile-target".to_string(),
            "tokenizer".to_string(),
            "text".to_string(),
            "markup".to_string(),
        ]),
        "the aggregate, its direct members, and the nested aggregate's members must all count as \
         enabled; `dep:` and `crate/feature` tokens are not local feature names, and an unrelated \
         feature must not be swept in"
    );
}

/// A manifest that cannot be located must leave the requested list exactly as it was: widening
/// the enabled set is only safe when the manifest proves the members are on.
#[test]
fn expand_configured_features_passes_through_when_the_manifest_is_unreachable() {
    let config = crate::core::config::ResolvedCrateConfig::default();
    assert_eq!(
        expand_configured_features(&config, &["tokenizer".to_string()]),
        vec!["tokenizer".to_string()],
        "no workspace_root means no manifest to read, so the list must pass through untouched"
    );
}