alef 0.107.9

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
//! Table-driven coverage for the structural TS mapper in `ts_union.rs`.
//!
//! Each shape here mirrors a real consumer type (see the module doc comment on `ts_union.rs`):
//! `EmbeddingInput`/`ModerationInput` (scalar-or-vec), `RerankDocument` (newtype-or-struct),
//! `UserContent`/`AssistantContent` (scalar-or-vec-of-struct, forcing an interface), and
//! `ToolChoice` (newtype-of-struct plus newtype-of-unit-enum, forcing a string-literal union).

use super::*;
use crate::core::ir::{EnumDef, EnumVariant, FieldDef, PrimitiveType, TypeDef, TypeRef};
use ahash::{AHashMap, AHashSet};

pub(super) fn empty_api() -> ApiSurface {
    ApiSurface {
        unresolved_modules: Vec::new(),
        crate_name: "test-lib".to_string(),
        version: "0.1.0".to_string(),
        types: vec![],
        functions: vec![],
        enums: vec![],
        errors: vec![],
        excluded_type_paths: std::collections::BTreeMap::new(),
        excluded_trait_names: std::collections::HashSet::new(),
        services: vec![],
        handler_contracts: vec![],
        unsupported_public_items: Vec::new(),
    }
}

pub(super) fn tuple_variant(name: &str, ty: TypeRef) -> EnumVariant {
    EnumVariant {
        name: name.to_string(),
        fields: vec![FieldDef {
            name: "_0".to_string(),
            ty,
            ..Default::default()
        }],
        is_tuple: true,
        ..Default::default()
    }
}

/// Builds the plan for a single enum and returns its shared `custom_section` text — the
/// equivalent of what used to be the per-enum `declaration` field before untagged enums started
/// sharing one combined custom section (see `AllUntaggedEnumsTsPlan`).
pub(super) fn plan_for(enum_def: &EnumDef, api: &ApiSurface) -> String {
    let exclude_types = AHashSet::default();
    let opaque_types = AHashSet::default();
    build_untagged_enum_ts_plans(&[enum_def], api, &exclude_types, &opaque_types, "Alef").custom_section
}

// ---------------------------------------------------------------------------------------------
// Real consumer shapes
// ---------------------------------------------------------------------------------------------

/// `enum EmbeddingInput { Single(String), Multiple(Vec<String>) }` -> `string | string[]`.
#[test]
fn embedding_input_maps_to_string_or_string_array() {
    let enum_def = EnumDef {
        name: "EmbeddingInput".to_string(),
        rust_path: "test_lib::EmbeddingInput".to_string(),
        variants: vec![
            tuple_variant("Single", TypeRef::String),
            tuple_variant("Multiple", TypeRef::Vec(Box::new(TypeRef::String))),
        ],
        serde_untagged: true,
        ..Default::default()
    };
    let api = empty_api();
    let all_plans =
        build_untagged_enum_ts_plans(&[&enum_def], &api, &AHashSet::default(), &AHashSet::default(), "Alef");
    assert!(
        all_plans
            .custom_section
            .contains("export type AlefEmbeddingInput = string | string[];"),
        "actual:\n{}",
        all_plans.custom_section
    );
    let plan = all_plans.plans.get("EmbeddingInput").expect("plan for EmbeddingInput");
    assert!(
        plan.extern_type_declaration
            .contains(r#"typescript_type = "AlefEmbeddingInput""#)
    );
    assert!(
        plan.extern_type_declaration
            .contains("pub type AlefEmbeddingInputValue;")
    );
}

/// `ModerationInput` has the identical scalar-or-vec shape as `EmbeddingInput`.
#[test]
fn moderation_input_maps_to_string_or_string_array() {
    let enum_def = EnumDef {
        name: "ModerationInput".to_string(),
        rust_path: "test_lib::ModerationInput".to_string(),
        variants: vec![
            tuple_variant("Single", TypeRef::String),
            tuple_variant("Multiple", TypeRef::Vec(Box::new(TypeRef::String))),
        ],
        serde_untagged: true,
        ..Default::default()
    };
    let plan = plan_for(&enum_def, &empty_api());
    assert!(
        plan.contains("export type AlefModerationInput = string | string[];"),
        "actual:\n{}",
        plan
    );
}

/// `enum RerankDocument { Text(String), Object { text: String } }` ->
/// `string | { text: string }` — a newtype variant next to a struct variant.
#[test]
fn rerank_document_maps_newtype_and_struct_variant() {
    let enum_def = EnumDef {
        name: "RerankDocument".to_string(),
        rust_path: "test_lib::RerankDocument".to_string(),
        variants: vec![
            tuple_variant("Text", TypeRef::String),
            EnumVariant {
                name: "Object".to_string(),
                fields: vec![FieldDef {
                    name: "text".to_string(),
                    ty: TypeRef::String,
                    ..Default::default()
                }],
                is_tuple: false,
                ..Default::default()
            },
        ],
        serde_untagged: true,
        ..Default::default()
    };
    let plan = plan_for(&enum_def, &empty_api());
    assert!(
        plan.contains("export type AlefRerankDocument = string | { text: string; };"),
        "actual:\n{}",
        plan
    );
}

pub(super) fn content_part_type() -> TypeDef {
    TypeDef {
        name: "ContentPart".to_string(),
        rust_path: "test_lib::ContentPart".to_string(),
        fields: vec![
            FieldDef {
                name: "text".to_string(),
                ty: TypeRef::String,
                ..Default::default()
            },
            FieldDef {
                name: "kind".to_string(),
                ty: TypeRef::String,
                ..Default::default()
            },
        ],
        ..Default::default()
    }
}

/// `enum UserContent { Text(String), Parts(Vec<ContentPart>) }` ->
/// `string | AlefContentPartWire[]` plus an emitted `AlefContentPartWire` interface.
#[test]
fn user_content_maps_to_string_or_content_part_array() {
    let enum_def = EnumDef {
        name: "UserContent".to_string(),
        rust_path: "test_lib::UserContent".to_string(),
        variants: vec![
            tuple_variant("Text", TypeRef::String),
            tuple_variant(
                "Parts",
                TypeRef::Vec(Box::new(TypeRef::Named("ContentPart".to_string()))),
            ),
        ],
        serde_untagged: true,
        ..Default::default()
    };
    let mut api = empty_api();
    api.types = vec![content_part_type()];
    let plan = plan_for(&enum_def, &api);

    assert!(
        plan.contains("export type AlefUserContent = string | AlefContentPartWire[];"),
        "actual:\n{}",
        plan
    );
    assert!(
        plan.contains("export interface AlefContentPartWire {\n    text: string;\n    kind: string;\n}"),
        "actual:\n{}",
        plan
    );
}

/// `AssistantContent` has the identical scalar-or-vec-of-struct shape, over `AssistantPart`.
#[test]
fn assistant_content_maps_to_string_or_assistant_part_array() {
    let enum_def = EnumDef {
        name: "AssistantContent".to_string(),
        rust_path: "test_lib::AssistantContent".to_string(),
        variants: vec![
            tuple_variant("Text", TypeRef::String),
            tuple_variant(
                "Parts",
                TypeRef::Vec(Box::new(TypeRef::Named("AssistantPart".to_string()))),
            ),
        ],
        serde_untagged: true,
        ..Default::default()
    };
    let mut api = empty_api();
    api.types = vec![TypeDef {
        name: "AssistantPart".to_string(),
        rust_path: "test_lib::AssistantPart".to_string(),
        fields: vec![FieldDef {
            name: "text".to_string(),
            ty: TypeRef::String,
            ..Default::default()
        }],
        ..Default::default()
    }];
    let plan = plan_for(&enum_def, &api);
    assert!(
        plan.contains("export type AlefAssistantContent = string | AlefAssistantPartWire[];"),
        "actual:\n{}",
        plan
    );
}

/// `enum ToolChoice { Mode(ToolChoiceMode), Specific(SpecificToolChoice) }` where
/// `ToolChoiceMode` is a fieldless enum -> a string-literal union, and `SpecificToolChoice` is a
/// struct -> an interface. Covers both auxiliary-declaration kinds in one union.
#[test]
fn tool_choice_maps_unit_enum_and_struct_newtype_variants() {
    let mode_enum = EnumDef {
        name: "ToolChoiceMode".to_string(),
        rust_path: "test_lib::ToolChoiceMode".to_string(),
        variants: vec![
            EnumVariant {
                name: "Auto".to_string(),
                ..Default::default()
            },
            EnumVariant {
                name: "Required".to_string(),
                ..Default::default()
            },
            EnumVariant {
                name: "None".to_string(),
                ..Default::default()
            },
        ],
        serde_rename_all: Some("snake_case".to_string()),
        is_copy: true,
        ..Default::default()
    };
    let enum_def = EnumDef {
        name: "ToolChoice".to_string(),
        rust_path: "test_lib::ToolChoice".to_string(),
        variants: vec![
            tuple_variant("Mode", TypeRef::Named("ToolChoiceMode".to_string())),
            tuple_variant("Specific", TypeRef::Named("SpecificToolChoice".to_string())),
        ],
        serde_untagged: true,
        ..Default::default()
    };
    let mut api = empty_api();
    api.enums = vec![mode_enum];
    api.types = vec![TypeDef {
        name: "SpecificToolChoice".to_string(),
        rust_path: "test_lib::SpecificToolChoice".to_string(),
        fields: vec![FieldDef {
            name: "name".to_string(),
            ty: TypeRef::String,
            ..Default::default()
        }],
        ..Default::default()
    }];
    let plan = plan_for(&enum_def, &api);

    assert!(
        // `ToolChoiceMode` gets a `Wire` suffix here: the bare name is already claimed by the
        // real wasm-bindgen `enum WasmToolChoiceMode` (numeric ABI discriminant) emitted
        // unconditionally for every fieldless enum — a different, incompatible runtime shape
        // from this union member's serde wire string. See `map_named_enum`'s `~keep` note.
        plan.contains("export type AlefToolChoice = AlefToolChoiceModeWire | AlefSpecificToolChoiceWire;"),
        "actual:\n{}",
        plan
    );
    assert!(
        plan.contains(r#"export type AlefToolChoiceModeWire = "auto" | "required" | "none";"#),
        "actual:\n{}",
        plan
    );
    assert!(
        plan.contains("export interface AlefSpecificToolChoiceWire {\n    name: string;\n}"),
        "actual:\n{}",
        plan
    );
}

// ---------------------------------------------------------------------------------------------
// Primitive / container mapping
// ---------------------------------------------------------------------------------------------

#[test]
fn primitive_types_map_to_expected_ts() {
    let cases: &[(TypeRef, &str)] = &[
        (TypeRef::Primitive(PrimitiveType::Bool), "boolean"),
        (TypeRef::Primitive(PrimitiveType::U32), "number"),
        (TypeRef::Primitive(PrimitiveType::F64), "number"),
        (TypeRef::Primitive(PrimitiveType::U64), "bigint"),
        (TypeRef::Primitive(PrimitiveType::I64), "bigint"),
        (TypeRef::String, "string"),
        (TypeRef::Char, "string"),
        (TypeRef::Path, "string"),
        (TypeRef::Bytes, "Uint8Array"),
        (TypeRef::Unit, "null"),
        (TypeRef::Json, "any"),
        (TypeRef::Duration, "number"),
    ];
    let api = empty_api();
    for (ty, expected) in cases {
        let mut ctx = TsMapContext {
            api: &api,
            exclude_types: &AHashSet::default(),
            opaque_type_names: &AHashSet::default(),
            prefix: "Alef",
            in_progress: AHashMap::default(),
            resolved_names: ahash::AHashMap::default(),
            decls: Vec::new(),
            casing: super::WireCasing::Serde,
        };
        assert_eq!(ctx.map_type(ty), *expected, "mapping {ty:?}");
    }
}

#[test]
fn vec_maps_to_element_array() {
    let api = empty_api();
    let mut ctx = TsMapContext {
        api: &api,
        exclude_types: &AHashSet::default(),
        opaque_type_names: &AHashSet::default(),
        prefix: "Alef",
        in_progress: AHashMap::default(),
        resolved_names: ahash::AHashMap::default(),
        decls: Vec::new(),
        casing: super::WireCasing::Serde,
    };
    let ty = TypeRef::Vec(Box::new(TypeRef::String));
    assert_eq!(ctx.map_type(&ty), "string[]");
}

/// `Vec<Option<String>>` must render as `(string | undefined)[]`, not `string | undefined[]`.
/// TypeScript's `[]` suffix binds tighter than `|`, so an unparenthesized union followed by `[]`
/// only arrays the union's last operand — `string | undefined[]` parses as
/// `string | (undefined[])`, describing a completely different (and wrong) type: a value that is
/// either a bare string or an array of `undefined`s, never an array containing possibly-absent
/// strings.
///
/// Revert symptom: reverting the `TypeRef::Vec` match arm back to the unconditional
/// `format!("{}[]", ...)` makes this assert `"(string | undefined)[]"` fail because `map_type`
/// instead returns the unparenthesized `"string | undefined[]"`.
#[test]
fn vec_of_optional_wraps_union_before_appending_array_suffix() {
    let api = empty_api();
    let mut ctx = TsMapContext {
        api: &api,
        exclude_types: &AHashSet::default(),
        opaque_type_names: &AHashSet::default(),
        prefix: "Alef",
        in_progress: AHashMap::default(),
        resolved_names: ahash::AHashMap::default(),
        decls: Vec::new(),
        casing: super::WireCasing::Serde,
    };
    let ty = TypeRef::Vec(Box::new(TypeRef::Optional(Box::new(TypeRef::String))));
    assert_eq!(ctx.map_type(&ty), "(string | undefined)[]");
}

/// The same nested shape one level deeper (`Vec<Vec<Option<String>>>`) must parenthesize at the
/// level the union actually occurs, not just the outermost `[]`.
///
/// Revert symptom: reverting the fix makes this assert `"(string | undefined)[][]"` fail because
/// the inner recursive call returns the unparenthesized `"string | undefined[]"`, which the outer
/// `Vec` arm then suffixes as `"string | undefined[][]"`.
#[test]
fn nested_vec_of_optional_parenthesizes_at_the_right_level() {
    let api = empty_api();
    let mut ctx = TsMapContext {
        api: &api,
        exclude_types: &AHashSet::default(),
        opaque_type_names: &AHashSet::default(),
        prefix: "Alef",
        in_progress: AHashMap::default(),
        resolved_names: ahash::AHashMap::default(),
        decls: Vec::new(),
        casing: super::WireCasing::Serde,
    };
    let ty = TypeRef::Vec(Box::new(TypeRef::Vec(Box::new(TypeRef::Optional(Box::new(
        TypeRef::String,
    ))))));
    assert_eq!(ctx.map_type(&ty), "(string | undefined)[][]");
}

#[test]
fn optional_appends_undefined() {
    let api = empty_api();
    let mut ctx = TsMapContext {
        api: &api,
        exclude_types: &AHashSet::default(),
        opaque_type_names: &AHashSet::default(),
        prefix: "Alef",
        in_progress: AHashMap::default(),
        resolved_names: ahash::AHashMap::default(),
        decls: Vec::new(),
        casing: super::WireCasing::Serde,
    };
    let ty = TypeRef::Optional(Box::new(TypeRef::String));
    assert_eq!(ctx.map_type(&ty), "string | undefined");
}

#[test]
fn string_keyed_map_becomes_record() {
    let api = empty_api();
    let mut ctx = TsMapContext {
        api: &api,
        exclude_types: &AHashSet::default(),
        opaque_type_names: &AHashSet::default(),
        prefix: "Alef",
        in_progress: AHashMap::default(),
        resolved_names: ahash::AHashMap::default(),
        decls: Vec::new(),
        casing: super::WireCasing::Serde,
    };
    let ty = TypeRef::Map(
        Box::new(TypeRef::String),
        Box::new(TypeRef::Primitive(PrimitiveType::U32)),
    );
    assert_eq!(ctx.map_type(&ty), "Record<string, number>");
}

#[test]
fn non_string_keyed_map_falls_back_to_any() {
    let api = empty_api();
    let mut ctx = TsMapContext {
        api: &api,
        exclude_types: &AHashSet::default(),
        opaque_type_names: &AHashSet::default(),
        prefix: "Alef",
        in_progress: AHashMap::default(),
        resolved_names: ahash::AHashMap::default(),
        decls: Vec::new(),
        casing: super::WireCasing::Serde,
    };
    let ty = TypeRef::Map(
        Box::new(TypeRef::Primitive(PrimitiveType::U32)),
        Box::new(TypeRef::String),
    );
    assert_eq!(ctx.map_type(&ty), "any");
}

// ---------------------------------------------------------------------------------------------
// Fallback: unmappable payload degrades ONLY that variant, not the whole union
// ---------------------------------------------------------------------------------------------

/// An excluded named type in one variant falls back to `any` for that variant only — the
/// sibling variant keeps its real structural type.
#[test]
fn excluded_type_falls_back_to_any_for_that_variant_only() {
    let enum_def = EnumDef {
        name: "Mixed".to_string(),
        rust_path: "test_lib::Mixed".to_string(),
        variants: vec![
            tuple_variant("Plain", TypeRef::String),
            tuple_variant("Opaque", TypeRef::Named("SecretHandle".to_string())),
        ],
        serde_untagged: true,
        ..Default::default()
    };
    let mut api = empty_api();
    api.types = vec![TypeDef {
        name: "SecretHandle".to_string(),
        rust_path: "test_lib::SecretHandle".to_string(),
        ..Default::default()
    }];
    let exclude_types: AHashSet<String> = ["SecretHandle".to_string()].into_iter().collect();
    let plan =
        build_untagged_enum_ts_plans(&[&enum_def], &api, &exclude_types, &AHashSet::default(), "Alef").custom_section;
    assert!(
        plan.contains("export type AlefMixed = string | any;"),
        "excluding one variant's type must not collapse the whole union to any;\nactual:\n{}",
        plan
    );
}

/// An opaque (handle) type used as a payload has no public fields to describe structurally, so
/// it falls back to `any` for that variant only.
#[test]
fn opaque_type_falls_back_to_any_for_that_variant_only() {
    let enum_def = EnumDef {
        name: "Mixed".to_string(),
        rust_path: "test_lib::Mixed".to_string(),
        variants: vec![
            tuple_variant("Plain", TypeRef::String),
            tuple_variant("Handle", TypeRef::Named("ClientHandle".to_string())),
        ],
        serde_untagged: true,
        ..Default::default()
    };
    let mut api = empty_api();
    api.types = vec![TypeDef {
        name: "ClientHandle".to_string(),
        rust_path: "test_lib::ClientHandle".to_string(),
        is_opaque: true,
        ..Default::default()
    }];
    let opaque_types: AHashSet<String> = ["ClientHandle".to_string()].into_iter().collect();
    let plan =
        build_untagged_enum_ts_plans(&[&enum_def], &api, &AHashSet::default(), &opaque_types, "Alef").custom_section;
    assert!(
        plan.contains("export type AlefMixed = string | any;"),
        "actual:\n{}",
        plan
    );
}

/// A named type that resolves to neither a struct nor an enum (e.g. an unresolved generic
/// parameter) falls back to `any` rather than panicking.
#[test]
fn unresolvable_named_type_falls_back_to_any() {
    let enum_def = EnumDef {
        name: "Mixed".to_string(),
        rust_path: "test_lib::Mixed".to_string(),
        variants: vec![tuple_variant("Generic", TypeRef::Named("T".to_string()))],
        serde_untagged: true,
        ..Default::default()
    };
    let plan = plan_for(&enum_def, &empty_api());
    assert!(plan.contains("export type AlefMixed = any;"), "actual:\n{}", plan);
}

// ---------------------------------------------------------------------------------------------
// Recursion guard
// ---------------------------------------------------------------------------------------------

/// A struct that (transitively) contains itself must not blow the stack, and must reference its
/// own interface name for the recursive field rather than re-expanding it.
#[test]
fn self_referential_struct_terminates_and_reuses_its_own_name() {
    let enum_def = EnumDef {
        name: "TreeInput".to_string(),
        rust_path: "test_lib::TreeInput".to_string(),
        variants: vec![tuple_variant("Node", TypeRef::Named("TreeNode".to_string()))],
        serde_untagged: true,
        ..Default::default()
    };
    let mut api = empty_api();
    api.types = vec![TypeDef {
        name: "TreeNode".to_string(),
        rust_path: "test_lib::TreeNode".to_string(),
        fields: vec![
            FieldDef {
                name: "value".to_string(),
                ty: TypeRef::String,
                ..Default::default()
            },
            FieldDef {
                name: "children".to_string(),
                ty: TypeRef::Vec(Box::new(TypeRef::Named("TreeNode".to_string()))),
                ..Default::default()
            },
        ],
        ..Default::default()
    }];
    let plan = plan_for(&enum_def, &api);

    assert!(
        plan.contains("export interface AlefTreeNodeWire {\n    value: string;\n    children: AlefTreeNodeWire[];\n}"),
        "the self-reference must resolve to the interface's own name, not re-expand or fall back to any;\nactual:\n{}",
        plan
    );
    // The interface must be emitted exactly once, not once per reference.
    assert_eq!(
        plan.matches("export interface AlefTreeNodeWire").count(),
        1,
        "actual:\n{}",
        plan
    );
}

/// A mutually-recursive pair (A references B, B references A) must also terminate and de-dupe.
#[test]
fn mutually_recursive_structs_terminate_and_dedupe() {
    let enum_def = EnumDef {
        name: "PairInput".to_string(),
        rust_path: "test_lib::PairInput".to_string(),
        variants: vec![tuple_variant("A", TypeRef::Named("NodeA".to_string()))],
        serde_untagged: true,
        ..Default::default()
    };
    let mut api = empty_api();
    api.types = vec![
        TypeDef {
            name: "NodeA".to_string(),
            rust_path: "test_lib::NodeA".to_string(),
            fields: vec![FieldDef {
                name: "b".to_string(),
                ty: TypeRef::Named("NodeB".to_string()),
                ..Default::default()
            }],
            ..Default::default()
        },
        TypeDef {
            name: "NodeB".to_string(),
            rust_path: "test_lib::NodeB".to_string(),
            fields: vec![FieldDef {
                name: "a".to_string(),
                ty: TypeRef::Named("NodeA".to_string()),
                ..Default::default()
            }],
            ..Default::default()
        },
    ];
    let plan = plan_for(&enum_def, &api);

    assert_eq!(
        plan.matches("export interface AlefNodeAWire").count(),
        1,
        "actual:\n{}",
        plan
    );
    assert_eq!(
        plan.matches("export interface AlefNodeBWire").count(),
        1,
        "actual:\n{}",
        plan
    );
}

// ---------------------------------------------------------------------------------------------
// Optional / shared-reference field handling
// ---------------------------------------------------------------------------------------------