alef 0.70.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
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
//! Java assertion rendering helpers.
//!
//! ~keep This file is already over the repo's 1,000-line file-modularization cap. The
//! `not_error_may_assert_presence` unification (routing `not_error` through
//! `not_error_presence::may_assert_presence`) added one parameter to `render_assertion`,
//! required at every call site, plus removed the old ad hoc `result_is_option && bare_field`
//! special case for `not_error` (now folded into the general arm) — a net small growth of
//! wiring and doc comments, not new unrelated functionality.

use crate::e2e::codegen::assertion_recipes::chunks_result_var;
use crate::e2e::codegen::assertion_type_skip::{
    streaming_assertion_type_skip_line, streaming_assertion_value_skip_line,
};
use crate::e2e::escape::escape_java;
use crate::e2e::field_access::FieldResolver;
use crate::e2e::fixture::Assertion;
use heck::ToLowerCamelCase;

use super::assertion_wildcard::render_wildcard_assertion;
use super::values::json_to_java;

#[allow(clippy::too_many_arguments)]
pub(super) fn render_assertion(
    out: &mut String,
    assertion: &Assertion,
    result_var: &str,
    class_name: &str,
    field_resolver: &FieldResolver,
    result_is_simple: bool,
    result_is_bytes: bool,
    result_is_option: bool,
    is_streaming: bool,
    streaming_item_type: Option<&str>,
    enum_fields: &std::collections::HashSet<String>,
    assert_enum_types: &std::collections::HashMap<String, String>,
    returns_void: bool,
    fractional_fields: &std::collections::HashSet<String>,
    not_error_may_assert_presence: bool,
) {
    // Bare-result is_empty / not_empty on Option<T> returns: the Java facade exposes
    // these as `@Nullable T` (via `.orElse(null)`) rather than `Optional<T>`, so the
    // template's `.isEmpty()` call would not compile for record types. Emit a
    // null-check instead — mirrors the kotlin / zig codegen behaviour.
    //
    // `not_error` is deliberately absent from this match: WHETHER it may assert presence is
    // decided once, centrally, by the caller via `not_error_presence::may_assert_presence`
    // (which already accounts for `result_is_option`) and handled in the general `not_error`
    // arm below, alongside every other backend's identical decision point. ~keep
    let bare_field = assertion.field.as_deref().is_none_or(str::is_empty);
    if result_is_option && bare_field {
        match assertion.assertion_type.as_str() {
            "is_empty" => {
                out.push_str(&format!(
                    "        assertNull({result_var}, \"expected empty value\");\n"
                ));
                return;
            }
            "not_empty" => {
                out.push_str(&format!(
                    "        assertNotNull({result_var}, \"expected non-empty value\");\n"
                ));
                return;
            }
            _ => {}
        }
    }

    // Byte-buffer returns: emit length-based assertions instead of struct-field
    // accessors. The result is `byte[]`, which has no `isEmpty()`/struct-field methods.
    // Field paths on byte-buffer results (e.g. `audio`, `content`) are pseudo-fields
    // referencing the buffer itself — treat them the same as no-field assertions.
    if result_is_bytes {
        match assertion.assertion_type.as_str() {
            "not_empty" => {
                out.push_str(&format!(
                    "        assertTrue({result_var}.length > 0, \"expected non-empty value\");\n"
                ));
                return;
            }
            "is_empty" => {
                out.push_str(&format!(
                    "        assertEquals(0, {result_var}.length, \"expected empty value\");\n"
                ));
                return;
            }
            "count_equals" | "length_equals" => {
                if let Some(n) = assertion.value.as_ref().and_then(|v| v.as_u64()) {
                    out.push_str(&format!("        assertEquals({n}, {result_var}.length);\n"));
                }
                return;
            }
            "count_min" | "length_min" => {
                if let Some(n) = assertion.value.as_ref().and_then(|v| v.as_u64()) {
                    out.push_str(&format!(
                        "        assertTrue({result_var}.length >= {n}, \"expected length >= {n}\");\n"
                    ));
                }
                return;
            }
            "not_error" => {
                // Use the statically-imported assertion (org.junit.jupiter.api.Assertions.*)
                // so we don't need a separate FQN import of the `Assertions` class.
                out.push_str(&format!(
                    "        assertNotNull({result_var}, \"expected non-null byte[] response\");\n"
                ));
                return;
            }
            _ => {
                out.push_str(&format!(
                    "        // skipped: assertion type '{}' not supported on byte[] result\n",
                    assertion.assertion_type
                ));
                return;
            }
        }
    }

    // `not_error` never carries a `field` and has no `java/assertion.jinja` branch —
    // that template's if/elif chain has no `else`, so before this the call silently
    // rendered nothing. An uncaught exception already fails the `@Test` method, but a
    // fixture whose only assertion is `not_error` must still leave a real, visible
    // assertion instead of a vacuous body. Mirrors the `assertNotNull` idiom the
    // byte[] branch above already uses. For streaming fixtures, assert on the
    // drained `chunks` list (bound by `collect_snippet` before this runs) rather
    // than the raw `result_var`, so a lazily-consumed stream that errors only on
    // iteration is still caught. `returns_void` calls bind no `result_var` at all
    // (`java/test_method.jinja`'s `{% if returns_void %}` branch calls without
    // assigning), so asserting on a variable here would not compile — that case is
    // handled at the call-emission site instead: `test_method.rs`'s `void_not_error`
    // flag wraps `call_expr` itself in `assertDoesNotThrow(() -> ...)`, so this arm
    // stays a no-op purely because the real assertion lives one level up, not because
    // nothing is asserted. WHETHER the plain (non-void, non-streaming) case below may
    // assert presence at all is decided once, centrally, by
    // `not_error_presence::may_assert_presence` — this arm only decides how. ~keep
    if assertion.assertion_type == "not_error" {
        if returns_void {
            // Handled by `test_method.rs`'s `void_not_error` wrapping the call in
            // assertDoesNotThrow — nothing to render into assertions_body here.
        } else if is_streaming {
            out.push_str("        assertNotNull(chunks, \"expected drained chunks list\");\n");
        } else if not_error_may_assert_presence {
            out.push_str(&format!(
                "        assertNotNull({result_var}, \"expected non-null response\");\n"
            ));
        }
        return;
    }

    // Handle synthetic/virtual fields that are computed rather than direct record accessors.
    if let Some(f) = &assertion.field {
        if let Some(reason) = crate::e2e::codegen::assertion_recipes::chunks_synthetic_skip_reason(f, field_resolver) {
            out.push_str(&format!("        // skipped: {reason}\n"));
            return;
        }

        match f.as_str() {
            // ---- ProcessingResult chunk-level computed predicates ----
            "chunks_have_content" => {
                let result_var = &chunks_result_var(field_resolver, "java", result_var);
                let pred = format!(
                    "java.util.Optional.ofNullable({result_var}.chunks()).orElse(java.util.List.of()).stream().allMatch(c -> c.content() != null && !c.content().isBlank())"
                );
                out.push_str(&crate::e2e::template_env::render(
                    "java/synthetic_assertion.jinja",
                    minijinja::context! {
                        assertion_kind => "chunks_content",
                        assertion_type => assertion.assertion_type.as_str(),
                        pred => pred,
                        field_name => f,
                    },
                ));
                return;
            }
            "chunks_have_heading_context" => {
                let result_var = &chunks_result_var(field_resolver, "java", result_var);
                let pred = format!(
                    "java.util.Optional.ofNullable({result_var}.chunks()).orElse(java.util.List.of()).stream().allMatch(c -> c.metadata().headingContext() != null)"
                );
                out.push_str(&crate::e2e::template_env::render(
                    "java/synthetic_assertion.jinja",
                    minijinja::context! {
                        assertion_kind => "chunks_heading_context",
                        assertion_type => assertion.assertion_type.as_str(),
                        pred => pred,
                        field_name => f,
                    },
                ));
                return;
            }
            "chunks_have_embeddings" => {
                let result_var = &chunks_result_var(field_resolver, "java", result_var);
                let pred = format!(
                    "java.util.Optional.ofNullable({result_var}.chunks()).orElse(java.util.List.of()).stream().allMatch(c -> c.embedding() != null && !c.embedding().isEmpty())"
                );
                out.push_str(&crate::e2e::template_env::render(
                    "java/synthetic_assertion.jinja",
                    minijinja::context! {
                        assertion_kind => "chunks_embeddings",
                        assertion_type => assertion.assertion_type.as_str(),
                        pred => pred,
                        field_name => f,
                    },
                ));
                return;
            }
            "first_chunk_starts_with_heading" => {
                let result_var = &chunks_result_var(field_resolver, "java", result_var);
                let pred = format!(
                    "java.util.Optional.ofNullable({result_var}.chunks()).orElse(java.util.List.of()).stream().findFirst().map(c -> c.metadata().headingContext() != null).orElse(false)"
                );
                out.push_str(&crate::e2e::template_env::render(
                    "java/synthetic_assertion.jinja",
                    minijinja::context! {
                        assertion_kind => "first_chunk_heading",
                        assertion_type => assertion.assertion_type.as_str(),
                        pred => pred,
                        field_name => f,
                    },
                ));
                return;
            }
            // ---- EmbedResponse virtual fields ----
            // When result_is_simple=true the result IS List<List<Float>> (the raw embeddings list).
            // When result_is_simple=false the result has an .embeddings() accessor.
            "embedding_dimensions" => {
                // Dimension = size of the first embedding vector in the list.
                let embed_list = if result_is_simple {
                    result_var.to_string()
                } else {
                    format!("{result_var}.embeddings()")
                };
                let expr = format!("({embed_list}.isEmpty() ? 0 : {embed_list}.get(0).size())");
                let java_val = assertion.value.as_ref().map(json_to_java).unwrap_or_default();
                out.push_str(&crate::e2e::template_env::render(
                    "java/synthetic_assertion.jinja",
                    minijinja::context! {
                        assertion_kind => "embedding_dimensions",
                        assertion_type => assertion.assertion_type.as_str(),
                        expr => expr,
                        java_val => java_val,
                        field_name => f,
                    },
                ));
                return;
            }
            "embeddings_valid" | "embeddings_finite" | "embeddings_non_zero" | "embeddings_normalized" => {
                // These are validation predicates that require iterating the embedding matrix.
                let embed_list = if result_is_simple {
                    result_var.to_string()
                } else {
                    format!("{result_var}.embeddings()")
                };
                let pred = match f.as_str() {
                    "embeddings_valid" => {
                        format!("{embed_list}.stream().allMatch(e -> e != null && !e.isEmpty())")
                    }
                    "embeddings_finite" => {
                        format!("{embed_list}.stream().flatMap(java.util.Collection::stream).allMatch(Float::isFinite)")
                    }
                    "embeddings_non_zero" => {
                        format!("{embed_list}.stream().allMatch(e -> e.stream().anyMatch(v -> v != 0.0f))")
                    }
                    "embeddings_normalized" => format!(
                        "{embed_list}.stream().allMatch(e -> {{ double n = e.stream().mapToDouble(v -> v * v).sum(); return Math.abs(n - 1.0) < 1e-3; }})"
                    ),
                    _ => unreachable!(),
                };
                let assertion_kind = format!("embeddings_{}", f.strip_prefix("embeddings_").unwrap_or(f));
                out.push_str(&crate::e2e::template_env::render(
                    "java/synthetic_assertion.jinja",
                    minijinja::context! {
                        assertion_kind => assertion_kind,
                        assertion_type => assertion.assertion_type.as_str(),
                        pred => pred,
                        field_name => f,
                    },
                ));
                return;
            }
            // ---- Fields not present on the Java ProcessingResult ----
            "keywords" | "keywords_count" => {
                out.push_str(&crate::e2e::template_env::render(
                    "java/synthetic_assertion.jinja",
                    minijinja::context! {
                        assertion_kind => "keywords",
                        field_name => f,
                    },
                ));
                return;
            }
            // ---- metadata not_empty / is_empty: Metadata is a required record, not Optional ----
            // Metadata has no .isEmpty() method; check that at least one optional field is present.
            "metadata" => {
                match assertion.assertion_type.as_str() {
                    "not_empty" | "is_empty" => {
                        out.push_str(&crate::e2e::template_env::render(
                            "java/synthetic_assertion.jinja",
                            minijinja::context! {
                                assertion_kind => "metadata",
                                assertion_type => assertion.assertion_type.as_str(),
                                result_var => result_var,
                            },
                        ));
                        return;
                    }
                    _ => {} // fall through to normal handling
                }
            }
            _ => {}
        }
    }

    // Streaming virtual fields: intercept before is_valid_for_result so they are
    // never skipped.  These fields resolve against the `chunks` collected-list variable.
    // Gate on `is_streaming` so non-streaming fixtures (e.g. consumers whose real
    // result struct has a literal `chunks` field) don't divert into the virtual
    // accessor path — they should fall through to the normal field resolver.
    if let Some(f) = &assertion.field
        && is_streaming
        && !f.is_empty()
        && crate::e2e::codegen::streaming_assertions::is_streaming_virtual_field(f)
    {
        if let Some(expr) =
            crate::e2e::codegen::streaming_assertions::StreamingFieldResolver::accessor_with_streaming_context(
                f,
                "java",
                "chunks",
                None,
                streaming_item_type,
            )
        {
            let line = match assertion.assertion_type.as_str() {
                "count_min" => {
                    if let Some(n) = assertion.value.as_ref().and_then(|v| v.as_u64()) {
                        format!("        assertTrue({expr}.size() >= {n}, \"expected >= {n} chunks\");\n")
                    } else {
                        streaming_assertion_value_skip_line("        ", "//", f, &assertion.assertion_type) + "\n"
                    }
                }
                "count_equals" => {
                    if let Some(n) = assertion.value.as_ref().and_then(|v| v.as_u64()) {
                        format!("        assertEquals({n}, {expr}.size());\n")
                    } else {
                        streaming_assertion_value_skip_line("        ", "//", f, &assertion.assertion_type) + "\n"
                    }
                }
                "equals" => {
                    if let Some(serde_json::Value::String(s)) = &assertion.value {
                        let literal = super::values::java_string_literal(s);
                        format!("        assertEquals({literal}, {expr});\n")
                    } else if let Some(n) = assertion.value.as_ref().and_then(|v| v.as_u64()) {
                        format!("        assertEquals({n}, {expr});\n")
                    } else {
                        streaming_assertion_value_skip_line("        ", "//", f, &assertion.assertion_type) + "\n"
                    }
                }
                "not_empty" => format!("        assertFalse({expr}.isEmpty(), \"expected non-empty\");\n"),
                "is_empty" => format!("        assertTrue({expr}.isEmpty(), \"expected empty\");\n"),
                "is_true" => format!("        assertTrue({expr}, \"expected true\");\n"),
                "is_false" => format!("        assertFalse({expr}, \"expected false\");\n"),
                "greater_than" => {
                    if let Some(n) = assertion.value.as_ref().and_then(|v| v.as_u64()) {
                        format!("        assertTrue({expr} > {n}, \"expected > {n}\");\n")
                    } else {
                        streaming_assertion_value_skip_line("        ", "//", f, &assertion.assertion_type) + "\n"
                    }
                }
                "greater_than_or_equal" => {
                    if let Some(n) = assertion.value.as_ref().and_then(|v| v.as_u64()) {
                        format!("        assertTrue({expr} >= {n}, \"expected >= {n}\");\n")
                    } else {
                        streaming_assertion_value_skip_line("        ", "//", f, &assertion.assertion_type) + "\n"
                    }
                }
                "contains" => {
                    if let Some(serde_json::Value::String(s)) = &assertion.value {
                        let escaped = crate::e2e::escape::escape_java(s);
                        format!(
                            "        assertTrue({expr}.contains(\"{escaped}\"), \"expected to contain: {escaped}\");\n"
                        )
                    } else {
                        streaming_assertion_value_skip_line("        ", "//", f, &assertion.assertion_type) + "\n"
                    }
                }
                _ => format!(
                    "{}\n",
                    streaming_assertion_type_skip_line("        ", "//", f, &assertion.assertion_type)
                ),
            };
            out.push_str(&line);
        } else {
            // ~keep The accessor returns `None` for reachable inputs (a `stream.has_*_event`
            // predicate whose item type this call never resolved, for one), and this branch used
            // to be absent: the assertion vanished with no line for
            // `fail_on_unavailable_field_markers` to see, so a clean strict-gate run was
            // indistinguishable from one that dropped it. alef's streaming adapter owns the gap,
            // so it is counted, never fatal.
            out.push_str(&format!(
                "        // skipped: {}\n",
                crate::e2e::codegen::field_skip::FieldSkip::StreamingAssertionOnUnsupportedField.message(f)
            ));
        }
        return;
    }

    // Skip assertions on fields that don't exist on the result type.
    if let Some(f) = &assertion.field
        && !f.is_empty()
        && !field_resolver.is_valid_for_result(f)
    {
        out.push_str(&crate::e2e::template_env::render(
            "java/synthetic_assertion.jinja",
            minijinja::context! {
                assertion_kind => "skipped",
                field_name => f,
            },
        ));
        return;
    }

    // Bracket-wildcard traversal (`links[].linkType`) means "every element". This must run
    // before `field_expr` is built below, since that path lowers the wildcard to index 0 and
    // would assert on a single element while reading as whole-array coverage. ~keep
    if let Some(f) = assertion.field.as_deref()
        && !f.is_empty()
        && let Some((array_part, elem_part)) = field_resolver.wildcard_split(f)
    {
        render_wildcard_assertion(out, assertion, result_var, field_resolver, f, &array_part, &elem_part);
        return;
    }

    // Determine if this field maps to a sealed-interface type declared in
    // `assert_enum_types`.  When `Some`, the value is the type name (e.g.
    // "FormatMetadata") and the corresponding `{TypeName}Display` helper will
    // be used to produce the display string for assertions.
    let sealed_display_type: Option<String> = assertion.field.as_deref().and_then(|f| {
        let resolved = field_resolver.resolve(f);
        assert_enum_types
            .get(f)
            .or_else(|| assert_enum_types.get(resolved))
            .cloned()
    });
    let is_sealed_display_field = sealed_display_type.is_some();

    // Determine if this field is an enum type (no `.contains()` on enums in Java).
    // Check both the raw fixture field path and the resolved (aliased) path so that
    // `fields_enum` entries can use either form (e.g., `"assets[].category"` or the
    // resolved `"assets[].asset_category"`). The hand-maintained `enum_fields` config is
    // checked first, but a field it never lists (e.g. a recursive struct's own enum field,
    // reached only through its parent's path — `data.kind` on a self-referential
    // `Option<Box<DataNode>>`) must still be rescued from the IR-derived classification, the
    // same way `field_resolver.is_enum` already backs every other backend's equivalent check
    // (csharp/kotlin/dart/gleam/swift/...). Without it, such a field silently falls through to
    // a plain `assertEquals(String, EnumType)` that can never pass, since a `String` is never
    // `.equals()` an enum constant. ~keep
    // NOTE: Sealed-interface types (those in assert_enum_types) are not Java enums
    // and do not have a .getValue() method — exclude them from enum field treatment.
    //
    // A third shape needs the same exclusion: an IR enum with data-carrying variants (e.g. a
    // `#[serde(untagged)]` union) is still an "enum" in the IR, but the Java binding backend
    // renders it as a wrapper class (`gen_java_tagged_union` / `gen_java_untagged_wrapper`),
    // neither of which declares `getValue()`. `java_enum_emits_get_value` answers from the
    // exact predicate the binding backend itself branches on
    // (`backends::java::gen_bindings::emits_get_value`), so this can never disagree with what
    // was actually emitted; it answers `None` when the IR doesn't resolve a concrete enum type
    // for the field (e.g. a `fields_enum`-only config entry), in which case the pre-existing
    // behaviour (assume `getValue()` is available) is kept. ~keep
    let field_is_enum = assertion.field.as_deref().is_some_and(|f| {
        let resolved = field_resolver.resolve(f);
        let in_enum_fields = enum_fields.get(f).is_some()
            || enum_fields.get(resolved).is_some()
            || field_resolver.is_enum(f)
            || field_resolver.is_enum(resolved);
        let emits_get_value = field_resolver
            .java_enum_emits_get_value(f)
            .or_else(|| field_resolver.java_enum_emits_get_value(resolved))
            .unwrap_or(true);
        in_enum_fields && !is_sealed_display_field && emits_get_value
    });

    // Determine if this field is an array (List<T>) — needed to choose .toString() for
    // contains assertions, since List.contains(Object) uses equals() which won't match
    // strings against complex record types like StructureItem.
    let field_is_array = assertion
        .field
        .as_deref()
        .is_some_and(|f| field_resolver.is_array(field_resolver.resolve(f)));

    // A fixture's `equals` assertion carrying a literal JSON `null` against a field the IR
    // proves is a genuine (non-`Option`) collection -- e.g. `Vec<T>` with `#[serde(default,
    // skip_serializing_if = "Vec::is_empty")]` -- can never pass: the generated binding's
    // Jackson deserializer and its `Builder`'s own default both materialize an absent/omitted
    // collection as an empty `List`/`Map`, never `null` -- see
    // `backends::java::gen_bindings::types::builders::gen_builder_nested_class`'s
    // `field_is_optional_in_binding` branch, which only ever defaults to `null` when the IR's
    // `field.optional` is true. Both `is_collection_root` and `is_optional` here read that
    // same IR fact (via `with_ir_collection_map`/`with_ir_fields`), so this is the assertion
    // side asking the binding side's own question, not a second independent guess at the
    // answer. Recognize the case and assert the same emptiness the binding actually produces
    // instead of a null-equality check that can never pass. ~keep
    let is_doomed_null_equals_on_required_collection = assertion.assertion_type == "equals"
        && matches!(assertion.value, Some(serde_json::Value::Null))
        && assertion.field.as_deref().is_some_and(|f| {
            !f.is_empty()
                && field_resolver.is_valid_for_result(f)
                && field_resolver.is_collection_root(field_resolver.resolve(f))
                && !field_resolver.is_optional(field_resolver.resolve(f))
        });
    if is_doomed_null_equals_on_required_collection {
        let accessor = field_resolver.accessor(assertion.field.as_deref().unwrap_or_default(), "java", result_var);
        out.push_str(&format!(
            "        assertTrue({accessor}.isEmpty(), \"expected empty (binding never returns null for a non-optional collection)\");\n"
        ));
        return;
    }

    let field_expr = if result_is_simple {
        result_var.to_string()
    } else {
        match &assertion.field {
            Some(f) if !f.is_empty() => {
                let accessor = field_resolver.accessor(f, "java", result_var);
                let resolved = field_resolver.resolve(f);
                // Unwrap Optional fields with a type-appropriate fallback.
                // Map.get() returns nullable, not Optional, so skip .orElse() for map access.
                // NOTE: is_optional() means the field is in optional_fields, but that doesn't
                // guarantee it returns Optional<T> in Java — nested fields like metadata.twitterCard
                // return @Nullable String, not Optional<String>. We detect this by checking
                // if the field path contains a dot (nested access).
                // Fields in `fields_display_as_text` have an `Option<T>` inner type
                // that is not a plain `String` (e.g. `AssistantContent`). Their Java
                // binding exposes a `.text()` accessor returning `String`. Using
                // `Objects::toString` on these would produce the class-name representation,
                // not the textual content.
                let field_is_display_as_text = field_resolver.is_display_as_text(f);
                if field_resolver.is_optional(resolved) && !field_resolver.has_map_access(f) {
                    // All nullable fields in the Java binding return @Nullable types, not Optional<T>.
                    // Wrap them in Optional.ofNullable() so e2e tests can use .orElse() fallbacks.
                    let optional_expr = format!("java.util.Optional.ofNullable({accessor})");
                    // Enum-typed optional fields need .map(v -> v.getValue()) to coerce to String
                    // before the orElse("") fallback can type-check (Optional<Enum>.orElse("") would
                    // be a type mismatch — Optional<String>.orElse("") is the only safe form).
                    if field_is_enum {
                        match assertion.assertion_type.as_str() {
                            // `is_true`/`is_false` on an Optional field mean "present"/"absent" --
                            // matching not_empty/is_empty, the raw Optional is returned so the
                            // template's presence switch (not a `.map(...).orElse(...)` string
                            // coercion, which produced a non-boolean `assertTrue` argument) decides.
                            "not_empty" | "is_empty" | "is_true" | "is_false" => optional_expr,
                            _ => {
                                // `field_is_enum` already excludes sealed-interface types
                                // (is_sealed_display_field), so any remaining enum type
                                // has .getValue() available.
                                format!("{optional_expr}.map(v -> v.getValue()).orElse(\"\")")
                            }
                        }
                    } else if field_is_display_as_text {
                        // Non-String content union (e.g. AssistantContent): call `.text()`
                        // to get the textual representation instead of `Objects::toString`
                        // which would return the class name.
                        match assertion.assertion_type.as_str() {
                            // `is_true`/`is_false` on an Optional field mean "present"/"absent" --
                            // matching not_empty/is_empty, the raw Optional is returned so the
                            // template's presence switch (not a `.map(...).orElse(...)` string
                            // coercion, which produced a non-boolean `assertTrue` argument) decides.
                            "not_empty" | "is_empty" | "is_true" | "is_false" => optional_expr,
                            _ => format!("{optional_expr}.map(v -> v.text()).orElse(\"\")"),
                        }
                    } else {
                        match assertion.assertion_type.as_str() {
                            // `not_empty`/`is_empty`/`is_true`/`is_false` on an Optional field all
                            // return the raw Optional so the template's presence switch decides --
                            // for is_true/is_false this replaces a `.map(...).orElse(...)` string
                            // coercion that produced a non-boolean `assertTrue` argument.
                            "not_empty" | "is_empty" | "is_true" | "is_false" => optional_expr,
                            // For size/count assertions on Optional<List<T>> fields, use List.of() fallback.
                            "count_min" | "count_equals" => {
                                format!("{optional_expr}.orElse(java.util.List.of())")
                            }
                            // For numeric comparisons on Optional<Long/Integer> fields, coerce
                            // the boxed numeric type to `long` via Number::longValue so the same
                            // code path compiles for both `Optional<Integer>` (e.g. mapped from
                            // Rust `Option<u32>`) and `Optional<Long>` fields.  Using a bare
                            // `.orElse(0L)` would fail for `Optional<Integer>` because the
                            // fallback type would not match the element type.
                            //
                            // Fractional fields (`f32`/`f64`, e.g. `Optional<Double>
                            // qualityScore`) must NOT go through `Number::longValue()` — that
                            // truncates the boxed value to zero before the comparison ever
                            // runs, turning a `[0.0, 1.0]` range assertion into a tautology
                            // (every legal value truncates to `0L`, so both bounds always
                            // hold). Route these through `Number::doubleValue()` instead so
                            // the comparison actually observes the fractional value. ~keep
                            "greater_than" | "less_than" | "greater_than_or_equal" | "less_than_or_equal" => {
                                if field_resolver.is_array(resolved) {
                                    format!("{optional_expr}.orElse(java.util.List.of())")
                                } else if is_fractional_field(fractional_fields, resolved) {
                                    format!("{optional_expr}.map(Number::doubleValue).orElse(0.0)")
                                } else {
                                    format!("{optional_expr}.map(Number::longValue).orElse(0L)")
                                }
                            }
                            // For equals on Optional fields, determine fallback based on whether value is numeric.
                            // If the fixture value is a number, coerce via Number::longValue so the
                            // comparison compiles for both Optional<Integer> and Optional<Long>.
                            // Sealed-display fields are handled via the {TypeName}Display helper in
                            // string_expr — keep as Optional here so the helper receives the unwrapped value.
                            "equals" => {
                                if is_sealed_display_field {
                                    // Sealed-interface Optional: keep, will be handled by string_expr path
                                    optional_expr
                                } else if let Some(expected) = &assertion.value {
                                    if expected.is_number() {
                                        format!("{optional_expr}.map(Number::longValue).orElse(0L)")
                                    } else {
                                        // `.map(Objects::toString)` collapses Optional<T> to
                                        // Optional<String> before `.orElse("")`, so the result
                                        // is unambiguously a String even when T is `Object`
                                        // (which is the Java mapping for free-form JSON values
                                        // like `Option<serde_json::Value>` — javac otherwise
                                        // infers LUB(Object, String) = Object and breaks
                                        // String-only method calls like .contains()).
                                        format!("{optional_expr}.map(java.util.Objects::toString).orElse(\"\")")
                                    }
                                } else {
                                    format!("{optional_expr}.map(java.util.Objects::toString).orElse(\"\")")
                                }
                            }
                            _ if field_resolver.is_array(resolved) => {
                                format!("{optional_expr}.orElse(java.util.List.of())")
                            }
                            _ => format!("{optional_expr}.map(java.util.Objects::toString).orElse(\"\")"),
                        }
                    }
                } else {
                    accessor
                }
            }
            _ => result_var.to_string(),
        }
    };

    // For enum fields, string-based assertions need .getValue() to convert the enum to
    // its serde-serialized lowercase string value (e.g., AssetCategory.Image -> "image").
    // All alef-generated Java enums expose a getValue() method annotated with @JsonValue.
    // Optional enum fields are already coerced to String via `.map(v -> v.getValue()).orElse("")`
    // upstream in field_expr; in that case the value is already a String and we must not
    // call .getValue() again. Detect by looking for `.map(v -> v.getValue())` in the expr.
    // Sealed-interface types (is_sealed_display_field) use a pattern-match helper instead.
    let string_expr = if field_is_enum && !field_expr.contains(".map(v -> v.getValue())") {
        format!("{field_expr}.getValue()")
    } else if let Some(ref stype) = sealed_display_type {
        // Sealed-interface type: convert via a generated `{TypeName}Display.toDisplayString`
        // helper that pattern-matches over all variants from the IR.
        // For Optional<T>, unwrap with orElse(null) so the helper can handle null safely.
        let inner_expr = if field_expr.contains("Optional.ofNullable") {
            format!("{field_expr}.orElse(null)")
        } else {
            field_expr.clone()
        };
        format!("{stype}Display.toDisplayString({inner_expr})")
    } else {
        field_expr.clone()
    };

    // Pre-compute context for template
    let assertion_type = assertion.assertion_type.as_str();
    let java_val = assertion.value.as_ref().map(json_to_java).unwrap_or_default();
    let is_string_val = assertion.value.as_ref().is_some_and(|v| v.is_string());
    let is_numeric_val = assertion.value.as_ref().is_some_and(|v| v.is_number());

    // values_java is consumed by `contains`, `contains_all`, `contains_any`, and
    // `not_contains` loops. Fall back to wrapping the singular `value` so single-entry
    // fixtures still emit one assertion call per value instead of an empty loop.
    let values_java: Vec<String> = assertion
        .values
        .as_ref()
        .map(|values| values.iter().map(json_to_java).collect::<Vec<_>>())
        .or_else(|| assertion.value.as_ref().map(|v| vec![json_to_java(v)]))
        .unwrap_or_default();

    let contains_any_expr = if !values_java.is_empty() {
        values_java
            .iter()
            .map(|v| format!("{string_expr}.contains({v})"))
            .collect::<Vec<_>>()
            .join(" || ")
    } else {
        String::new()
    };

    let length_expr = if result_is_bytes {
        format!("{field_expr}.length")
    } else {
        format!("{field_expr}.length()")
    };

    let n = assertion.value.as_ref().and_then(|v| v.as_u64()).unwrap_or(0);

    let call_expr = if let Some(method_name) = &assertion.method {
        build_java_method_call(result_var, method_name, assertion.args.as_ref(), class_name)
    } else {
        String::new()
    };

    let check = assertion.check.as_deref().unwrap_or("is_true");

    let java_check_val = assertion.value.as_ref().map(json_to_java).unwrap_or_default();

    let check_n = assertion.value.as_ref().and_then(|v| v.as_u64()).unwrap_or(0);

    let is_bool_val = assertion.value.as_ref().is_some_and(|v| v.is_boolean());
    let bool_is_true = assertion.value.as_ref().is_some_and(|v| v.as_bool() == Some(true));

    let method_returns_collection = assertion
        .method
        .as_ref()
        .is_some_and(|m| matches!(m.as_str(), "find_nodes_by_type" | "findNodesByType"));

    let rendered = crate::e2e::template_env::render(
        "java/assertion.jinja",
        minijinja::context! {
            assertion_type,
            java_val,
            string_expr,
            field_expr,
            field_is_enum,
            field_is_array,
            is_string_val,
            is_numeric_val,
            values_java => values_java,
            contains_any_expr,
            length_expr,
            n,
            call_expr,
            check,
            java_check_val,
            check_n,
            is_bool_val,
            bool_is_true,
            method_returns_collection,
        },
    );
    out.push_str(&rendered);
}

/// Leaf segment of a (possibly dotted / bracketed) resolved field path, e.g.
/// `"results[0].quality_score"` -> `"quality_score"`.
fn leaf_field_name(path: &str) -> &str {
    let last_dot = path.rsplit('.').next().unwrap_or(path);
    last_dot.split('[').next().unwrap_or(last_dot)
}

/// True when `resolved`'s leaf field name is known (via [`fractional_scalar_fields`]) to
/// carry an `f32`/`f64` Rust type — directly or through `Option<T>`.
fn is_fractional_field(fractional_fields: &std::collections::HashSet<String>, resolved: &str) -> bool {
    fractional_fields.contains(leaf_field_name(resolved))
}

/// Field names (bare leaf, e.g. `"quality_score"`) whose Rust type — or `Option<T>` inner
/// type — is `f32`/`f64` on at least one IR type in `type_defs`.
///
/// Consulted before defaulting an `Optional` numeric-range coercion to
/// `Number::longValue()`: that truncates a fractional value to zero before the comparison
/// runs, turning e.g. a `[0.0, 1.0]` range assertion on a `Double` `qualityScore` into a
/// tautology (every legal value truncates to `0L`, so both bounds always hold). ~keep
pub(super) fn fractional_scalar_fields(type_defs: &[crate::core::ir::TypeDef]) -> std::collections::HashSet<String> {
    use crate::core::ir::{PrimitiveType, TypeRef};
    let mut fractional = std::collections::HashSet::new();
    for type_def in type_defs {
        for field in &type_def.fields {
            let ty = match &field.ty {
                TypeRef::Optional(inner) => inner.as_ref(),
                other => other,
            };
            if matches!(
                ty,
                TypeRef::Primitive(PrimitiveType::F32) | TypeRef::Primitive(PrimitiveType::F64)
            ) {
                fractional.insert(field.name.clone());
            }
        }
    }
    fractional
}

/// Build a Java call expression for a `method_result` assertion on a sample_language Tree.
///
/// Maps method names to the appropriate Java static/instance method calls.
pub(super) fn build_java_method_call(
    result_var: &str,
    method_name: &str,
    args: Option<&serde_json::Value>,
    class_name: &str,
) -> String {
    match method_name {
        "root_child_count" => format!("{result_var}.rootNode().childCount()"),
        "root_node_type" => format!("{result_var}.rootNode().kind()"),
        "named_children_count" => format!("{result_var}.rootNode().namedChildCount()"),
        "has_error_nodes" => format!("{class_name}.treeHasErrorNodes({result_var})"),
        "error_count" | "tree_error_count" => format!("{class_name}.treeErrorCount({result_var})"),
        "tree_to_sexp" => format!("{class_name}.treeToSexp({result_var})"),
        "contains_node_type" => {
            let node_type = args
                .and_then(|a| a.get("node_type"))
                .and_then(|v| v.as_str())
                .unwrap_or("");
            format!("{class_name}.treeContainsNodeType({result_var}, \"{node_type}\")")
        }
        "find_nodes_by_type" => {
            let node_type = args
                .and_then(|a| a.get("node_type"))
                .and_then(|v| v.as_str())
                .unwrap_or("");
            format!("{class_name}.findNodesByType({result_var}, \"{node_type}\")")
        }
        "run_query" => {
            let query_source = args
                .and_then(|a| a.get("query_source"))
                .and_then(|v| v.as_str())
                .unwrap_or("");
            let language = args
                .and_then(|a| a.get("language"))
                .and_then(|v| v.as_str())
                .unwrap_or("");
            let escaped_query = escape_java(query_source);
            format!("{class_name}.runQuery({result_var}, \"{language}\", \"{escaped_query}\", source)")
        }
        _ => {
            format!("{result_var}.{}()", method_name.to_lower_camel_case())
        }
    }
}

#[cfg(test)]
mod tests {
    use std::collections::{HashMap, HashSet};

    use super::*;
    use crate::e2e::field_access::FieldResolver;
    use crate::e2e::fixture::Assertion;

    fn make_resolver(optional: HashSet<String>, dat: HashSet<String>) -> FieldResolver {
        FieldResolver::new(
            &HashMap::new(),
            &optional,
            &HashSet::new(),
            &HashSet::new(),
            &HashSet::new(),
        )
        .with_display_as_text_fields(dat)
    }

    fn make_equals_assertion(field: &str, value: &str) -> Assertion {
        Assertion {
            assertion_type: "equals".to_string(),
            field: Some(field.to_string()),
            value: Some(serde_json::Value::String(value.to_string())),
            ..Default::default()
        }
    }

    fn make_contains_assertion(field: &str, value: &str) -> Assertion {
        Assertion {
            assertion_type: "contains".to_string(),
            field: Some(field.to_string()),
            value: Some(serde_json::Value::String(value.to_string())),
            ..Default::default()
        }
    }

    fn render_bare(assertion: &Assertion) -> String {
        let resolver = make_resolver(HashSet::new(), HashSet::new());
        let mut out = String::new();
        render_assertion(
            &mut out,
            assertion,
            "result",
            "Result",
            &resolver,
            false,
            false,
            false,
            false,
            None,
            &HashSet::new(),
            &HashMap::new(),
            false,
            &HashSet::new(),
            true,
        );
        out
    }

    fn render_with_optional(assertion: &Assertion, optional_field: &str) -> String {
        let optional: HashSet<String> = [optional_field.to_string()].into_iter().collect();
        let resolver = make_resolver(optional, HashSet::new());
        let mut out = String::new();
        render_assertion(
            &mut out,
            assertion,
            "result",
            "Result",
            &resolver,
            false,
            false,
            false,
            false,
            None,
            &HashSet::new(),
            &HashMap::new(),
            false,
            &HashSet::new(),
            true,
        );
        out
    }

    fn is_true_assertion(field: &str) -> Assertion {
        Assertion {
            assertion_type: "is_true".to_string(),
            field: Some(field.to_string()),
            ..Default::default()
        }
    }

    /// `Option<DataNode>` presence: before the fix this fell through to the generic
    /// `.map(Objects::toString).orElse("")` string-coercion arm, so `assertTrue` received
    /// a `String` argument -- a compile error, since `assertTrue` requires `boolean`.
    #[test]
    fn is_true_on_optional_struct_field_checks_presence() {
        let out = render_with_optional(&is_true_assertion("data"), "data");
        assert_eq!(
            out,
            "        assertTrue(java.util.Optional.ofNullable(result.data()).isPresent(), \"expected true (present)\");\n"
        );
    }

    #[test]
    fn is_false_on_optional_struct_field_checks_absence() {
        let out = render_with_optional(
            &Assertion {
                assertion_type: "is_false".to_string(),
                field: Some("data".to_string()),
                ..Default::default()
            },
            "data",
        );
        assert_eq!(
            out,
            "        assertTrue(java.util.Optional.ofNullable(result.data()).isEmpty(), \"expected false (absent)\");\n"
        );
    }

    /// A follow-on member access through the same optional field must still compile: the
    /// leaf (`equals` on `data.kind`) is unaffected by the `is_true` fix, so it continues to
    /// route through the existing `Optional.ofNullable(...).map(Objects::toString).orElse("")`
    /// coercion rather than needing an unwrap of its own -- Java's binding returns `@Nullable`
    /// types, not `Optional<T>`, so `result.data().kind()` already compiles regardless of
    /// nullability. ~keep
    #[test]
    fn equals_on_nested_field_through_optional_parent_is_unchanged() {
        let out = render_with_optional(&make_equals_assertion("data.kind", "KeyValue"), "data");
        assert!(out.contains("result.data().kind()"), "got: {out}");
    }

    #[test]
    fn is_true_on_non_optional_field_is_unchanged() {
        let out = render_bare(&is_true_assertion("active"));
        assert_eq!(out, "        assertTrue(result.active(), \"expected true\");\n");
    }
    #[cfg(test)]
    #[path = "wildcard_tests.rs"]
    mod wildcard_tests;

    /// IR-oracle wiring regression (alef task #64): a field that is IR-reachable
    /// (present, non-`binding_excluded`, on some IR type) but missing from the
    /// hand-maintained `result_fields` config must still render a real assertion,
    /// not a "skipped: field not available" stub — `java/test_method.rs` now
    /// threads `FieldResolver::ir_field_sets(type_defs)` into `with_ir_fields`. ~keep
    #[test]
    fn java_ir_reachable_field_absent_from_result_fields_is_not_skipped() {
        let reachable: HashSet<String> = ["data".to_string()].into_iter().collect();
        let resolver = FieldResolver::new(
            &HashMap::new(),
            &HashSet::new(),
            &HashSet::new(),
            &HashSet::new(),
            &HashSet::new(),
        )
        .with_ir_fields(reachable, HashSet::new(), HashSet::new());
        let assertion = make_equals_assertion("data", "hello");
        let mut out = String::new();
        render_assertion(
            &mut out,
            &assertion,
            "result",
            "SampleClass",
            &resolver,
            false,
            false,
            false,
            false,
            None,
            &HashSet::new(),
            &HashMap::new(),
            false,
            &HashSet::new(),
            true,
        );
        assert!(!out.contains("skipped"), "got: {out}");
    }

    /// The negative-control half of the same regression: `internal_diagnostics`
    /// represents a field carrying `#[doc(hidden)]` or `#[cfg_attr(alef,
    /// alef(skip))]` in the real struct (a genuine `binding_excluded` field) —
    /// NOT `#[serde(skip)]`, which alone does not exclude a field from the
    /// binding surface. Even though it is listed in `result_fields` (a stale/
    /// wrong config entry), the IR must still win and reject it. ~keep
    #[test]
    fn java_ir_excluded_field_present_in_result_fields_is_still_skipped() {
        let result_fields: HashSet<String> = ["internal_diagnostics".to_string()].into_iter().collect();
        let excluded: HashSet<String> = ["internal_diagnostics".to_string()].into_iter().collect();
        let resolver = FieldResolver::new(
            &HashMap::new(),
            &HashSet::new(),
            &result_fields,
            &HashSet::new(),
            &HashSet::new(),
        )
        .with_ir_fields(HashSet::new(), excluded, HashSet::new());
        let assertion = make_equals_assertion("internal_diagnostics", "hello");
        let mut out = String::new();
        render_assertion(
            &mut out,
            &assertion,
            "result",
            "SampleClass",
            &resolver,
            false,
            false,
            false,
            false,
            None,
            &HashSet::new(),
            &HashMap::new(),
            false,
            &HashSet::new(),
            true,
        );
        assert!(out.contains("skipped"), "got: {out}");
    }

    /// A plain `Option<String>` field should use `Objects::toString` in the
    /// Java equals expression — NOT `.text()`. Guards against DAT path bleeding
    /// into regular optional string fields.
    #[test]
    fn java_plain_optional_string_uses_objects_to_string() {
        let mut optional = HashSet::new();
        optional.insert("content".to_string());
        let resolver = make_resolver(optional, HashSet::new());
        let assertion = make_equals_assertion("content", "hello");
        let mut out = String::new();
        render_assertion(
            &mut out,
            &assertion,
            "result",
            "SampleClass",
            &resolver,
            false,
            false,
            false,
            false,
            None,
            &HashSet::new(),
            &HashMap::new(),
            false,
            &HashSet::new(),
            true,
        );
        assert!(
            out.contains("Objects::toString"),
            "plain optional string field must use Objects::toString; got: {out}"
        );
        assert!(
            !out.contains(".text()"),
            "plain optional string must NOT use .text(); got: {out}"
        );
    }

    /// A `display_as_text` field (e.g. `Option<AssistantContent>`) should use
    /// `.map(v -> v.text()).orElse("")` so the Java assertion sees the textual
    /// representation, not the class-name string from `Objects::toString`.
    #[test]
    fn java_display_as_text_optional_uses_text_accessor() {
        let mut optional = HashSet::new();
        optional.insert("content".to_string());
        let mut dat = HashSet::new();
        dat.insert("content".to_string());
        let resolver = make_resolver(optional, dat);
        let assertion = make_equals_assertion("content", "Hello, world!");
        let mut out = String::new();
        render_assertion(
            &mut out,
            &assertion,
            "result",
            "SampleClass",
            &resolver,
            false,
            false,
            false,
            false,
            None,
            &HashSet::new(),
            &HashMap::new(),
            false,
            &HashSet::new(),
            true,
        );
        assert!(
            out.contains(".map(v -> v.text()).orElse(\"\")"),
            "display_as_text field must use .map(v -> v.text()).orElse(\"\"); got: {out}"
        );
        assert!(
            !out.contains("Objects::toString"),
            "display_as_text field must NOT use Objects::toString; got: {out}"
        );
    }

    fn make_not_error_assertion() -> Assertion {
        Assertion {
            assertion_type: "not_error".to_string(),
            ..Default::default()
        }
    }

    /// Regression test for the not_error vacuous-test defect: `java/assertion.jinja`'s
    /// if/elif chain has no `not_error` branch and no final `else`, so before this fix
    /// a fixture whose only assertion was `not_error` rendered nothing at all — not
    /// even a comment. Must emit a real `assertNotNull` instead.
    #[test]
    fn not_error_emits_a_real_assert_not_null_on_the_result() {
        let resolver = make_resolver(HashSet::new(), HashSet::new());
        let assertion = make_not_error_assertion();
        let mut out = String::new();
        render_assertion(
            &mut out,
            &assertion,
            "result",
            "SampleClass",
            &resolver,
            false,
            false,
            false,
            false,
            None,
            &HashSet::new(),
            &HashMap::new(),
            false,
            &HashSet::new(),
            true,
        );
        assert_eq!(out, "        assertNotNull(result, \"expected non-null response\");\n");
    }

    #[test]
    fn not_error_on_a_streaming_fixture_asserts_on_drained_chunks_not_result() {
        let resolver = make_resolver(HashSet::new(), HashSet::new());
        let assertion = make_not_error_assertion();
        let mut out = String::new();
        render_assertion(
            &mut out,
            &assertion,
            "result",
            "SampleClass",
            &resolver,
            false,
            false,
            false,
            true,
            None,
            &HashSet::new(),
            &HashMap::new(),
            false,
            &HashSet::new(),
            true,
        );
        assert_eq!(
            out,
            "        assertNotNull(chunks, \"expected drained chunks list\");\n"
        );
    }

    /// A `returns_void` call binds no `result_var` at all (see
    /// `java/test_method.jinja`'s `{% if returns_void %}` branch) — asserting on it
    /// would not compile. The real assertion for this case lives one level up: see
    /// `test_method.rs`'s `void_not_error_call_wraps_call_expr_in_assert_does_not_throw`,
    /// which wraps `call_expr` in `assertDoesNotThrow` at the call-emission site instead.
    #[test]
    fn not_error_on_a_returns_void_call_emits_nothing() {
        let resolver = make_resolver(HashSet::new(), HashSet::new());
        let assertion = make_not_error_assertion();
        let mut out = String::new();
        render_assertion(
            &mut out,
            &assertion,
            "result",
            "SampleClass",
            &resolver,
            false,
            false,
            false,
            false,
            None,
            &HashSet::new(),
            &HashMap::new(),
            true,
            &HashSet::new(),
            true,
        );
        assert!(
            out.is_empty(),
            "a returns_void call must not reference an unbound result_var, got: {out}"
        );
    }

    fn make_range_assertion(assertion_type: &str, field: &str, value: f64) -> Assertion {
        Assertion {
            assertion_type: assertion_type.to_string(),
            field: Some(field.to_string()),
            value: serde_json::Number::from_f64(value).map(serde_json::Value::Number),
            ..Default::default()
        }
    }

    /// Regression test for the `qualityScore` range-assertion defect: an
    /// `Optional<Double>` field's range comparators must NOT coerce through
    /// `Number::longValue()` — that truncates every legal fractional value to `0L`
    /// before the comparison runs, so a `[0.0, 1.0]` range check on a `Double` becomes
    /// a tautology that can never fail. With the field registered in
    /// `fractional_fields`, the emitted comparison must use `Number::doubleValue()`
    /// instead, so it can actually observe (and fail on) an out-of-range value. ~keep
    #[test]
    fn fractional_optional_field_range_assertion_uses_double_value_not_long_value() {
        let mut optional = HashSet::new();
        optional.insert("quality_score".to_string());
        let resolver = make_resolver(optional, HashSet::new());
        let fractional: HashSet<String> = ["quality_score".to_string()].into_iter().collect();
        let assertion = make_range_assertion("greater_than_or_equal", "quality_score", 0.0);
        let mut out = String::new();
        render_assertion(
            &mut out,
            &assertion,
            "result",
            "SampleClass",
            &resolver,
            false,
            false,
            false,
            false,
            None,
            &HashSet::new(),
            &HashMap::new(),
            false,
            &fractional,
            true,
        );
        assert!(
            out.contains("Number::doubleValue"),
            "fractional Optional field must coerce via Number::doubleValue, got: {out}"
        );
        assert!(
            !out.contains("Number::longValue"),
            "fractional Optional field must NOT truncate via Number::longValue, got: {out}"
        );
    }

    /// Negative control: an integer `Optional` field (e.g. `sheetCount`, correctly
    /// handled at `SmokeTest.java:149`) is absent from `fractional_fields` and must
    /// keep using `Number::longValue()` — the fractional-type fix must not regress
    /// the already-correct integer path.
    #[test]
    fn integer_optional_field_range_assertion_still_uses_long_value() {
        let mut optional = HashSet::new();
        optional.insert("sheet_count".to_string());
        let resolver = make_resolver(optional, HashSet::new());
        let assertion = make_range_assertion("greater_than_or_equal", "sheet_count", 1.0);
        let mut out = String::new();
        render_assertion(
            &mut out,
            &assertion,
            "result",
            "SampleClass",
            &resolver,
            false,
            false,
            false,
            false,
            None,
            &HashSet::new(),
            &HashMap::new(),
            false,
            &HashSet::new(),
            true,
        );
        assert!(
            out.contains("Number::longValue"),
            "integer Optional field must keep Number::longValue, got: {out}"
        );
        assert!(
            !out.contains("Number::doubleValue"),
            "integer Optional field must not use Number::doubleValue, got: {out}"
        );
    }

    /// `fractional_scalar_fields` must recognize `f64`/`f32` fields, including
    /// through `Option<T>`, and must NOT flag integer fields.
    #[test]
    fn fractional_scalar_fields_detects_float_types_through_optional() {
        use crate::core::ir::{FieldDef, PrimitiveType, TypeDef, TypeRef};

        let type_defs = vec![TypeDef {
            name: "SampleResult".to_string(),
            fields: vec![
                FieldDef {
                    name: "quality_score".to_string(),
                    ty: TypeRef::Optional(Box::new(TypeRef::Primitive(PrimitiveType::F64))),
                    ..Default::default()
                },
                FieldDef {
                    name: "ratio".to_string(),
                    ty: TypeRef::Primitive(PrimitiveType::F32),
                    ..Default::default()
                },
                FieldDef {
                    name: "sheet_count".to_string(),
                    ty: TypeRef::Optional(Box::new(TypeRef::Primitive(PrimitiveType::U32))),
                    ..Default::default()
                },
            ],
            ..Default::default()
        }];

        let fractional = fractional_scalar_fields(&type_defs);
        assert!(fractional.contains("quality_score"), "got: {fractional:?}");
        assert!(fractional.contains("ratio"), "got: {fractional:?}");
        assert!(
            !fractional.contains("sheet_count"),
            "integer field must not be classified as fractional, got: {fractional:?}"
        );
    }
}