alef 0.71.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
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
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::codegen::field_skip::{FieldSkip, nested_wildcard_skip_line};
use crate::e2e::escape::escape_elixir;
use crate::e2e::field_access::FieldResolver;
use crate::e2e::fixture::Assertion;
use std::collections::{HashMap, HashSet};
use std::fmt::Write as FmtWrite;

use super::values::json_to_elixir;

/// Returns true if the field expression is a numeric/integer expression
/// (e.g., a `length(...)` call) rather than a string.
pub(super) fn is_numeric_expr(field_expr: &str) -> bool {
    field_expr.starts_with("length(")
}

/// Build a call to the generated enum module's `wire_value/1` function.
///
/// Enum submodules (`AppModule.EnumName`) live under the app's top-level Elixir module,
/// regardless of which (possibly nested) module a given NIF call is dispatched through --
/// see `enum_module_header.jinja` in the rustler backend, which always nests enum modules one
/// level under `app_module`. `module_path` here is the *call's* module (`E2eConfig`'s
/// `[crates.e2e.call] module`, e.g. `MyLib` or a nested `MyLib.Service`), so only its first
/// dot-segment is the app root the enum module actually lives under. ~keep
fn elixir_enum_wire_value_expr(module_path: &str, enum_type_name: &str, field_expr: &str) -> String {
    let app_root = module_path.split('.').next().unwrap_or(module_path);
    format!("{app_root}.{enum_type_name}.wire_value({field_expr})")
}

#[allow(clippy::too_many_arguments)]
pub(super) fn render_assertion(
    out: &mut String,
    assertion: &Assertion,
    result_var: &str,
    field_resolver: &FieldResolver,
    module_path: &str,
    fields_enum: &HashSet<String>,
    per_call_enum_fields: &HashMap<String, String>,
    result_is_simple: bool,
    is_streaming: bool,
    returns_void: bool,
    not_error_may_assert_presence: bool,
) {
    // Handle synthetic / derived fields before the is_valid_for_result check
    // so they are never treated as struct field accesses on the result.
    if let Some(f) = &assertion.field {
        if let Some(reason) = crate::e2e::codegen::assertion_recipes::chunks_synthetic_skip_reason(f, field_resolver) {
            let _ = writeln!(out, "      # skipped: {reason}");
            return;
        }

        match f.as_str() {
            "chunks_have_content" => {
                let result_var = &chunks_result_var(field_resolver, "elixir", result_var);
                let pred =
                    format!("Enum.all?({result_var}.chunks || [], fn c -> c.content != nil and c.content != \"\" end)");
                match assertion.assertion_type.as_str() {
                    "is_true" => {
                        let _ = writeln!(out, "      assert {pred}");
                    }
                    "is_false" => {
                        let _ = writeln!(out, "      refute {pred}");
                    }
                    other => {
                        panic!("Elixir e2e generator: unsupported assertion type '{other}' on synthetic field '{f}'");
                    }
                }
                return;
            }
            "chunks_have_embeddings" => {
                let result_var = &chunks_result_var(field_resolver, "elixir", result_var);
                let pred = format!(
                    "Enum.all?({result_var}.chunks || [], fn c -> c.embedding != nil and c.embedding != [] end)"
                );
                match assertion.assertion_type.as_str() {
                    "is_true" => {
                        let _ = writeln!(out, "      assert {pred}");
                    }
                    "is_false" => {
                        let _ = writeln!(out, "      refute {pred}");
                    }
                    other => {
                        panic!("Elixir e2e generator: unsupported assertion type '{other}' on synthetic field '{f}'");
                    }
                }
                return;
            }
            "chunks_have_heading_context" => {
                let result_var = &chunks_result_var(field_resolver, "elixir", result_var);
                let pred = format!(
                    "Enum.all?({result_var}.chunks || [], fn c -> c.metadata != nil and c.metadata.heading_context != nil end)"
                );
                match assertion.assertion_type.as_str() {
                    "is_true" => {
                        let _ = writeln!(out, "      assert {pred}");
                    }
                    "is_false" => {
                        let _ = writeln!(out, "      refute {pred}");
                    }
                    other => {
                        panic!("Elixir e2e generator: unsupported assertion type '{other}' on synthetic field '{f}'");
                    }
                }
                return;
            }
            "first_chunk_starts_with_heading" => {
                // Same real field as `chunks_have_heading_context` above (`c.metadata.heading_context`),
                // restricted to the first chunk -- not a `content`-prefix proxy. A chunk whose
                // content happens to start with "#" for an unrelated reason (a literal markdown
                // heading in the source, not `prepend_heading_context`) would pass the old proxy
                // and hide a genuine `heading_context` regression. ~keep
                let result_var = &chunks_result_var(field_resolver, "elixir", result_var);
                let expr = format!(
                    "case List.first({result_var}.chunks || []) do
        c when is_map(c) -> c.metadata != nil and c.metadata.heading_context != nil
        _ -> false
      end"
                );
                match assertion.assertion_type.as_str() {
                    "is_true" => {
                        let _ = writeln!(out, "      assert ({expr})");
                    }
                    "is_false" => {
                        let _ = writeln!(out, "      refute ({expr})");
                    }
                    other => {
                        panic!("Elixir e2e generator: unsupported assertion type '{other}' on synthetic field '{f}'");
                    }
                }
                return;
            }
            "embeddings" => {
                match assertion.assertion_type.as_str() {
                    "count_equals" => {
                        if let Some(val) = &assertion.value {
                            let ex_val = json_to_elixir(val);
                            let _ = writeln!(out, "      assert length({result_var}) == {ex_val}");
                        }
                    }
                    "count_min" => {
                        if let Some(val) = &assertion.value {
                            let ex_val = json_to_elixir(val);
                            let _ = writeln!(out, "      assert length({result_var}) >= {ex_val}");
                        }
                    }
                    "not_empty" => {
                        let _ = writeln!(out, "      assert {result_var} != []");
                    }
                    "is_empty" => {
                        let _ = writeln!(out, "      assert {result_var} == []");
                    }
                    other => {
                        panic!(
                            "Elixir e2e generator: unsupported assertion type '{other}' on synthetic field 'embeddings'"
                        );
                    }
                }
                return;
            }
            "embedding_dimensions" => {
                let expr = format!("(if {result_var} == [], do: 0, else: length(hd({result_var})))");
                match assertion.assertion_type.as_str() {
                    "equals" => {
                        if let Some(val) = &assertion.value {
                            let ex_val = json_to_elixir(val);
                            let _ = writeln!(out, "      assert {expr} == {ex_val}");
                        }
                    }
                    "greater_than" => {
                        if let Some(val) = &assertion.value {
                            let ex_val = json_to_elixir(val);
                            let _ = writeln!(out, "      assert {expr} > {ex_val}");
                        }
                    }
                    other => {
                        panic!(
                            "Elixir e2e generator: unsupported assertion type '{other}' on synthetic field 'embedding_dimensions'"
                        );
                    }
                }
                return;
            }
            "embeddings_valid" | "embeddings_finite" | "embeddings_non_zero" | "embeddings_normalized" => {
                let pred = match f.as_str() {
                    "embeddings_valid" => {
                        format!("Enum.all?({result_var}, fn e -> e != [] end)")
                    }
                    "embeddings_finite" => {
                        format!("Enum.all?({result_var}, fn e -> Enum.all?(e, fn v -> is_float(v) and v == v end) end)")
                    }
                    "embeddings_non_zero" => {
                        format!("Enum.all?({result_var}, fn e -> Enum.any?(e, fn v -> v != 0.0 end) end)")
                    }
                    "embeddings_normalized" => {
                        format!(
                            "Enum.all?({result_var}, fn e -> n = Enum.reduce(e, 0.0, fn v, acc -> acc + v * v end); abs(n - 1.0) < 1.0e-3 end)"
                        )
                    }
                    _ => unreachable!(),
                };
                match assertion.assertion_type.as_str() {
                    "is_true" => {
                        let _ = writeln!(out, "      assert {pred}");
                    }
                    "is_false" => {
                        let _ = writeln!(out, "      refute {pred}");
                    }
                    other => {
                        panic!("Elixir e2e generator: unsupported assertion type '{other}' on synthetic field '{f}'");
                    }
                }
                return;
            }
            "keywords" | "keywords_count" => {
                let _ = writeln!(
                    out,
                    "      # skipped: {}",
                    FieldSkip::NotAvailableOnElixirResultType.message(f)
                );
                return;
            }
            _ => {}
        }
    }

    if is_streaming
        && let Some(f) = &assertion.field
        && !f.is_empty()
        && crate::e2e::codegen::streaming_assertions::is_streaming_virtual_field(f)
    {
        if let Some(expr) =
            crate::e2e::codegen::streaming_assertions::StreamingFieldResolver::accessor(f, "elixir", result_var)
        {
            // ~keep Every value-narrowing arm below used to fall through to nothing when the
            // fixture's value did not survive `as_u64()` / the string pattern, so the assertion
            // disappeared with no line for any funnel to count.
            let value_skip = || streaming_assertion_value_skip_line("      ", "#", f, &assertion.assertion_type);
            match assertion.assertion_type.as_str() {
                "count_min" => {
                    if let Some(n) = assertion.value.as_ref().and_then(|v| v.as_u64()) {
                        let _ = writeln!(out, "      assert length({expr}) >= {n}");
                    } else {
                        let _ = writeln!(out, "{}", value_skip());
                    }
                }
                "count_equals" => {
                    if let Some(n) = assertion.value.as_ref().and_then(|v| v.as_u64()) {
                        let _ = writeln!(out, "      assert length({expr}) == {n}");
                    } else {
                        let _ = writeln!(out, "{}", value_skip());
                    }
                }
                "equals" => {
                    if let Some(serde_json::Value::String(s)) = &assertion.value {
                        let escaped = escape_elixir(s);
                        let _ = writeln!(out, "      assert {expr} == \"{escaped}\"");
                    } else if let Some(n) = assertion.value.as_ref().and_then(|v| v.as_u64()) {
                        let _ = writeln!(out, "      assert {expr} == {n}");
                    } else {
                        let _ = writeln!(out, "{}", value_skip());
                    }
                }
                "not_empty" => {
                    let _ = writeln!(out, "      assert {expr} not in [nil, \"\", [], %{{}}]");
                }
                "is_empty" => {
                    let _ = writeln!(out, "      assert {expr} == []");
                }
                "is_true" => {
                    let _ = writeln!(out, "      assert {expr}");
                }
                "is_false" => {
                    let _ = writeln!(out, "      refute {expr}");
                }
                "greater_than" => {
                    if let Some(n) = assertion.value.as_ref().and_then(|v| v.as_u64()) {
                        let _ = writeln!(out, "      assert {expr} > {n}");
                    } else {
                        let _ = writeln!(out, "{}", value_skip());
                    }
                }
                "greater_than_or_equal" => {
                    if let Some(n) = assertion.value.as_ref().and_then(|v| v.as_u64()) {
                        let _ = writeln!(out, "      assert {expr} >= {n}");
                    } else {
                        let _ = writeln!(out, "{}", value_skip());
                    }
                }
                "contains" => {
                    if let Some(serde_json::Value::String(s)) = &assertion.value {
                        let escaped = escape_elixir(s);
                        let _ = writeln!(out, "      assert String.contains?({expr}, \"{escaped}\")");
                    } else {
                        let _ = writeln!(out, "{}", value_skip());
                    }
                }
                _ => {
                    let _ = writeln!(
                        out,
                        "{}",
                        streaming_assertion_type_skip_line("      ", "#", f, &assertion.assertion_type)
                    );
                }
            }
        } else {
            // ~keep The accessor returns `None` for reachable inputs (a `stream.has_*_event`
            // predicate never resolves through `accessor`, which supplies no item type), and this
            // branch used to be absent: the assertion vanished with no line for
            // `fail_on_unavailable_field_markers` to see. alef's streaming adapter owns the gap,
            // so it is counted, never fatal.
            let _ = writeln!(
                out,
                "      # skipped: {}",
                FieldSkip::StreamingAssertionOnUnsupportedField.message(f)
            );
        }
        return;
    }

    if !result_is_simple
        && let Some(f) = &assertion.field
        && !f.is_empty()
        && !field_resolver.is_valid_for_result(f)
    {
        let _ = writeln!(
            out,
            "      # skipped: {}",
            FieldSkip::NotAvailableOnResultType.message(f)
        );
        return;
    }

    // A `foo[].bar` fixture path means "some element of foo satisfies this", but
    // `FieldResolver::accessor` lowers `[]` to index 0 (`Enum.at(result.foo, 0).bar`),
    // which silently checks only the first element and reads as coverage. Quantify over
    // every element instead. Explicit numeric indices (`foo[2].bar`) are a separate,
    // correct feature and keep their `Enum.at/2` lowering. ~keep
    if !result_is_simple
        && let Some(f) = assertion.field.as_deref()
        && let Some((array_part, elem_part)) = field_resolver.wildcard_split(f)
    {
        // `wildcard_split` consumes the first `[].` only, so a doubly-nested path leaves a
        // second wildcard in `elem_part` that the element accessor below lowers to index 0. ~keep
        if let Some(line) = nested_wildcard_skip_line("      ", "#", f, &elem_part) {
            let _ = writeln!(out, "{line}");
            return;
        }
        let array_accessor = if array_part.is_empty() {
            result_var.to_string()
        } else {
            field_resolver.accessor(&array_part, "elixir", result_var)
        };
        let elem_accessor = if elem_part.is_empty() {
            "e".to_string()
        } else {
            field_resolver.accessor(&elem_part, "elixir", "e")
        };
        render_wildcard_assertion(out, assertion, &array_accessor, &elem_accessor, f);
        return;
    }

    let field_expr = if result_is_simple {
        result_var.to_string()
    } else {
        match &assertion.field {
            Some(f) if !f.is_empty() => field_resolver.accessor(f, "elixir", result_var),
            _ => result_var.to_string(),
        }
    };

    let is_numeric = is_numeric_expr(&field_expr);
    // `fields_enum`/`per_call_enum_fields` carry the hand-maintained config. When neither
    // names the field, fall back to the IR-derived classification (`with_ir_enum_map`,
    // anchored at the call's declared Rust return type via `resolve_declared_result_type`) so
    // a consumer that never configured either still gets a correct classification instead of
    // the dynamically-typed default `to_string`-less comparison, which asserts the NIF's atom
    // (`:key_value`) against the fixture's wire string (`"key_value"`) and silently evaluates
    // to `false` rather than failing to compile. This is purely additive. ~keep
    let field_is_enum = assertion.field.as_deref().filter(|f| !f.is_empty()).is_some_and(|f| {
        let resolved = field_resolver.resolve(f);
        fields_enum.contains(f)
            || fields_enum.contains(resolved)
            || per_call_enum_fields.contains_key(f)
            || per_call_enum_fields.contains_key(resolved)
            || field_resolver.is_enum(f)
    });
    let field_is_format_metadata = assertion
        .field
        .as_deref()
        .filter(|f| !f.is_empty())
        .is_some_and(|f| f == "metadata.format" || f.ends_with(".metadata.format"));

    // Check if this field is configured as display_as_text (e.g., AssistantContent struct
    // with a .text field). When true, access the .text property and nil-guard to empty string.
    let field_is_display_as_text = assertion
        .field
        .as_deref()
        .filter(|f| !f.is_empty())
        .is_some_and(|f| field_resolver.is_display_as_text(f));

    let coerced_field_expr = if field_is_format_metadata {
        format!("alef_e2e_format_to_string({field_expr})")
    } else if field_is_display_as_text {
        // For display_as_text fields (e.g., content: AssistantContent), access .text
        // and provide empty string as fallback when nil
        format!("(({field_expr} && {field_expr}.text) || \"\")")
    } else if field_is_enum {
        // The binding exposes the exact serde wire value via `<Enum>.wire_value/1` (see
        // `gen_elixir_enum_module_with_known_types` in the rustler backend) rather than
        // `to_string/1`: `to_string(:key_value)` returns the atom's own Elixir spelling
        // ("key_value"), not the wire value ("KeyValue") the fixture literal carries, and a
        // data-carrying enum's flat-struct/tuple runtime shape has no `String.Chars` impl at
        // all. Compare the fixture literal verbatim -- no lowering on our side -- against
        // whatever `wire_value/1` returns. `ir_enum_type_name` only resolves when the IR
        // positively confirms the field's enum type (see `field_is_enum` above); a field
        // classified as enum only through the hand-maintained `fields_enum`/`enum_fields`
        // config (enum type name unknown) keeps the previous `to_string/1` behavior rather
        // than guessing a module path. ~keep
        match assertion
            .field
            .as_deref()
            .filter(|f| !f.is_empty())
            .and_then(|f| field_resolver.ir_enum_type_name(f))
        {
            Some(enum_type_name) => elixir_enum_wire_value_expr(module_path, &enum_type_name, &field_expr),
            None => format!("to_string({field_expr})"),
        }
    } else {
        field_expr.clone()
    };
    let field_is_array = assertion
        .field
        .as_deref()
        .filter(|f| !f.is_empty())
        .is_some_and(|f| field_resolver.is_array(field_resolver.resolve(f)));

    match assertion.assertion_type.as_str() {
        "equals" => {
            if let Some(expected) = &assertion.value {
                let elixir_val = json_to_elixir(expected);
                let is_string_expected = expected.is_string();
                if is_string_expected && !is_numeric {
                    let _ = writeln!(out, "      assert {coerced_field_expr} == {elixir_val}");
                } else if field_is_enum {
                    let _ = writeln!(out, "      assert {coerced_field_expr} == {elixir_val}");
                } else {
                    let _ = writeln!(out, "      assert {field_expr} == {elixir_val}");
                }
            }
        }
        "contains" => {
            if let Some(expected) = &assertion.value {
                let elixir_val = json_to_elixir(expected);
                if field_is_array && expected.is_string() {
                    let _ = writeln!(
                        out,
                        "      assert Enum.any?({field_expr}, fn item -> Enum.any?(alef_e2e_item_texts(item), &String.contains?(&1, {elixir_val})) end)"
                    );
                } else {
                    let _ = writeln!(
                        out,
                        "      assert String.contains?(to_string({field_expr}), {elixir_val})"
                    );
                }
            }
        }
        "contains_all" => {
            if let Some(values) = &assertion.values {
                for val in values {
                    let elixir_val = json_to_elixir(val);
                    if field_is_array && val.is_string() {
                        let _ = writeln!(
                            out,
                            "      assert Enum.any?({field_expr}, fn item -> Enum.any?(alef_e2e_item_texts(item), &String.contains?(&1, {elixir_val})) end)"
                        );
                    } else {
                        let _ = writeln!(
                            out,
                            "      assert String.contains?(to_string({field_expr}), {elixir_val})"
                        );
                    }
                }
            }
        }
        "not_contains" => {
            for expected in assertion.expected_values() {
                let elixir_val = json_to_elixir(expected);
                if field_is_array && expected.is_string() {
                    let _ = writeln!(
                        out,
                        "      refute Enum.any?({field_expr}, fn item -> Enum.any?(alef_e2e_item_texts(item), &String.contains?(&1, {elixir_val})) end)"
                    );
                } else {
                    let _ = writeln!(
                        out,
                        "      refute String.contains?(to_string({field_expr}), {elixir_val})"
                    );
                }
            }
        }
        "not_empty" => {
            let _ = writeln!(out, "      assert {field_expr} not in [nil, \"\", [], %{{}}]");
        }
        "is_empty" => {
            if is_numeric {
                let _ = writeln!(out, "      assert {field_expr} == 0");
            } else {
                let _ = writeln!(out, "      assert {coerced_field_expr} in [nil, \"\", [], %{{}}]");
            }
        }
        "contains_any" => {
            if let Some(values) = &assertion.values {
                let items: Vec<String> = values.iter().map(json_to_elixir).collect();
                let list_str = items.join(", ");
                let _ = writeln!(
                    out,
                    "      assert Enum.any?([{list_str}], fn v -> String.contains?(to_string({field_expr}), v) end)"
                );
            }
        }
        "greater_than" => {
            if let Some(val) = &assertion.value {
                let elixir_val = json_to_elixir(val);
                let _ = writeln!(out, "      assert {field_expr} > {elixir_val}");
            }
        }
        "less_than" => {
            if let Some(val) = &assertion.value {
                let elixir_val = json_to_elixir(val);
                let _ = writeln!(out, "      assert {field_expr} < {elixir_val}");
            }
        }
        "greater_than_or_equal" => {
            if let Some(val) = &assertion.value {
                let elixir_val = json_to_elixir(val);
                let _ = writeln!(out, "      assert {field_expr} >= {elixir_val}");
            }
        }
        "less_than_or_equal" => {
            if let Some(val) = &assertion.value {
                let elixir_val = json_to_elixir(val);
                let _ = writeln!(out, "      assert {field_expr} <= {elixir_val}");
            }
        }
        "starts_with" => {
            if let Some(expected) = &assertion.value {
                let elixir_val = json_to_elixir(expected);
                let _ = writeln!(out, "      assert String.starts_with?({field_expr}, {elixir_val})");
            }
        }
        "ends_with" => {
            if let Some(expected) = &assertion.value {
                let elixir_val = json_to_elixir(expected);
                let _ = writeln!(out, "      assert String.ends_with?({field_expr}, {elixir_val})");
            }
        }
        "min_length" => {
            if let Some(val) = &assertion.value
                && let Some(n) = val.as_u64()
            {
                let _ = writeln!(
                    out,
                    "      assert (is_binary({field_expr}) && byte_size({field_expr}) >= {n}) || (is_list({field_expr}) && length({field_expr}) >= {n})"
                );
            }
        }
        "max_length" => {
            if let Some(val) = &assertion.value
                && let Some(n) = val.as_u64()
            {
                let _ = writeln!(
                    out,
                    "      assert (is_binary({field_expr}) && byte_size({field_expr}) <= {n}) || (is_list({field_expr}) && length({field_expr}) <= {n})"
                );
            }
        }
        "count_min" => {
            if let Some(val) = &assertion.value
                && let Some(n) = val.as_u64()
            {
                let _ = writeln!(out, "      assert length({field_expr}) >= {n}");
            }
        }
        "count_equals" => {
            if let Some(val) = &assertion.value
                && let Some(n) = val.as_u64()
            {
                let _ = writeln!(out, "      assert length({field_expr}) == {n}");
            }
        }
        "is_true" => {
            let field_is_optional = assertion
                .field
                .as_ref()
                .is_some_and(|f| !f.is_empty() && field_resolver.is_optional(f));
            if field_is_optional {
                // Optional field: "is_true" means "present" -- `== true` never matches a
                // present non-boolean value (e.g. a map), so it always fails even when the
                // field is present. `refute is_nil(...)` is the interpretation that holds
                // for any value, matching the Rust `.is_some()` convention. ~keep
                let _ = writeln!(out, "      refute is_nil({field_expr})");
            } else {
                let _ = writeln!(out, "      assert {field_expr} == true");
            }
        }
        "is_false" => {
            let field_is_optional = assertion
                .field
                .as_ref()
                .is_some_and(|f| !f.is_empty() && field_resolver.is_optional(f));
            if field_is_optional {
                let _ = writeln!(out, "      assert is_nil({field_expr})");
            } else {
                let _ = writeln!(out, "      assert {field_expr} == false");
            }
        }
        "method_result" => {
            if let Some(method_name) = &assertion.method {
                let call_expr = build_elixir_method_call(result_var, method_name, assertion.args.as_ref(), module_path);
                let check = assertion.check.as_deref().unwrap_or("is_true");
                match check {
                    "equals" => {
                        if let Some(val) = &assertion.value {
                            let elixir_val = json_to_elixir(val);
                            let _ = writeln!(out, "      assert {call_expr} == {elixir_val}");
                        }
                    }
                    "is_true" => {
                        let _ = writeln!(out, "      assert {call_expr} == true");
                    }
                    "is_false" => {
                        let _ = writeln!(out, "      assert {call_expr} == false");
                    }
                    "greater_than_or_equal" => {
                        if let Some(val) = &assertion.value {
                            let n = val.as_u64().unwrap_or(0);
                            let _ = writeln!(out, "      assert {call_expr} >= {n}");
                        }
                    }
                    "count_min" => {
                        if let Some(val) = &assertion.value {
                            let n = val.as_u64().unwrap_or(0);
                            let _ = writeln!(out, "      assert length({call_expr}) >= {n}");
                        }
                    }
                    "contains" => {
                        if let Some(val) = &assertion.value {
                            let elixir_val = json_to_elixir(val);
                            let _ = writeln!(out, "      assert String.contains?({call_expr}, {elixir_val})");
                        }
                    }
                    "is_error" => {
                        let _ = writeln!(out, "      assert_raise RuntimeError, fn -> {call_expr} end");
                    }
                    other_check => {
                        panic!("Elixir e2e generator: unsupported method_result check type: {other_check}");
                    }
                }
            } else {
                panic!("Elixir e2e generator: method_result assertion missing 'method' field");
            }
        }
        "matches_regex" => {
            if let Some(expected) = &assertion.value {
                let elixir_val = json_to_elixir(expected);
                let _ = writeln!(out, "      assert Regex.match?(~r/{elixir_val}/, {field_expr})");
            }
        }
        "not_error" => {
            // `test_case.rs` already binds the call result via `{:ok, result} = call(...)`
            // for `returns_result` calls — a real, meaningful check: an `{:error, _}`
            // return fails the match with a `MatchError`, failing the test. But before
            // this fix, rendering nothing here left `result` bound and never
            // referenced. `actual_result_var` in `test_case.rs` only underscore-
            // prefixes the binding when `fixture.assertions.is_empty()` — a
            // `not_error`-only fixture has one (non-empty) assertion, so the binding
            // stays named and unreferenced, an "unused variable" warning that `mix
            // compile --warnings-as-errors` (used by downstream consumers of this
            // generator) promotes to a build failure. Emit a real, visible assertion
            // that also consumes the variable.
            //
            // `returns_void` calls are the one exception: rustler encodes a Rust `()`
            // success payload as the Elixir atom `nil` (`TypeRef::Unit => "nil"` in
            // `backends/rustler/gen_bindings`), so `result` is `nil` on every SUCCESSFUL
            // void call, not just a failed one. `refute is_nil(result)` there would fail
            // every passing call — a guaranteed-red test, worse than the vacuous check it
            // replaced. There is nothing left to assert on a `nil` payload; the `{:ok,
            // result} = call(...)` match above is already the real, visible `not_error`
            // check (an `{:error, _}` return raises `MatchError`, failing the test), the
            // same way Rust's `.expect()` and Gleam's `let assert Ok(...)` already are for
            // their own void calls. ~keep
            //
            // WHETHER `refute is_nil(...)` may render at all is decided once, centrally, by
            // `not_error_presence::may_assert_presence` — a sibling assertion or an
            // `Option<T>` result both make it unsafe (e.g. a bare `Option<T>`-returning call
            // whose success path legitimately returns `None` -> Elixir `nil` would make this
            // arm directly contradict a sibling `is_empty`'s `assert is_nil(...)` on the same
            // variable). The caller (`test_case.rs`) already underscore-prefixes
            // `actual_result_var` when this is `false` for a `not_error`-only fixture, so the
            // unused-variable warning this arm used to dodge by asserting is dodged there
            // instead. This arm only decides how. ~keep
            if !returns_void && not_error_may_assert_presence {
                let _ = writeln!(out, "      refute is_nil({result_var})");
            }
        }
        // ~keep Unreachable by construction: `expects_error` in test_case.rs is true
        // whenever any assertion is type "error", and every such fixture returns early
        // (validation_creation_failure or the plain expects_error branch) before the
        // assertions loop that calls render_assertion is ever reached — so `result_var`
        // here is always the already-unwrapped Ok value, never the error tuple this
        // assertion type names. The declared error value is preserved at the two call
        // sites in test_case.rs (`emit_error_assertion`), not here.
        "error" => {}
        other => {
            panic!("Elixir e2e generator: unsupported assertion type: {other}");
        }
    }
}

/// Build an Elixir call expression for a `method_result` assertion on a sample_language result.
/// Maps method names to the appropriate `module_path` function calls.
pub(super) fn build_elixir_method_call(
    result_var: &str,
    method_name: &str,
    args: Option<&serde_json::Value>,
    module_path: &str,
) -> String {
    match method_name {
        "root_child_count" => format!("{module_path}.root_child_count({result_var})"),
        "has_error_nodes" => format!("{module_path}.tree_has_error_nodes({result_var})"),
        "error_count" | "tree_error_count" => format!("{module_path}.tree_error_count({result_var})"),
        "tree_to_sexp" => format!("{module_path}.tree_to_sexp({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!("{module_path}.tree_contains_node_type({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!("{module_path}.find_nodes_by_type({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("");
            format!("{module_path}.run_query({result_var}, \"{language}\", \"{query_source}\", source)")
        }
        _ => format!("{module_path}.{method_name}({result_var})"),
    }
}

/// Render the `foo[].bar` wildcard forms as an `Enum.any?/2` quantifier over every
/// element of the array, rather than an index-0 lookup. The array expression is
/// nil-guarded with `|| []` because an absent optional list surfaces as `nil` and
/// `Enum.any?/2` would raise on it.
fn render_wildcard_assertion(
    out: &mut String,
    assertion: &Assertion,
    array_accessor: &str,
    elem_accessor: &str,
    field: &str,
) {
    let guarded = format!("({array_accessor} || [])");
    let any_expr = |elixir_val: &str| {
        format!("Enum.any?({guarded}, fn e -> String.contains?(to_string({elem_accessor}), {elixir_val}) end)")
    };
    match assertion.assertion_type.as_str() {
        "contains" => {
            if let Some(val) = &assertion.value {
                let elixir_val = json_to_elixir(val);
                let _ = writeln!(out, "      assert {}", any_expr(&elixir_val));
            }
        }
        "contains_all" => {
            if let Some(values) = &assertion.values {
                for val in values {
                    let elixir_val = json_to_elixir(val);
                    let _ = writeln!(out, "      assert {}", any_expr(&elixir_val));
                }
            }
        }
        "not_contains" => {
            for val in assertion.expected_values() {
                let elixir_val = json_to_elixir(val);
                let _ = writeln!(out, "      refute {}", any_expr(&elixir_val));
            }
        }
        "not_empty" => {
            let _ = writeln!(
                out,
                "      assert Enum.any?({guarded}, fn e -> to_string({elem_accessor}) != \"\" end)"
            );
        }
        other => {
            let _ = writeln!(
                out,
                "      # skipped: unsupported traversal assertion '{other}' on '{field}'"
            );
        }
    }
}

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

    use super::render_assertion;
    use crate::e2e::codegen::assertion_type_skip::{AssertionTypeSkip, streaming_assertion_type_skip_line};
    use crate::e2e::fixture::Assertion;

    fn empty_resolver() -> FieldResolver {
        FieldResolver::new(
            &HashMap::new(),
            &HashSet::new(),
            &HashSet::new(),
            &HashSet::new(),
            &HashSet::new(),
        )
    }

    /// 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" comment — `elixir/test_case.rs` now
    /// threads `FieldResolver::ir_field_sets(type_defs)` into `with_ir_fields`. ~keep
    #[test]
    fn elixir_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 = Assertion {
            assertion_type: "equals".to_string(),
            field: Some("data".to_string()),
            value: Some(serde_json::Value::String("hello".to_string())),
            ..Default::default()
        };
        let mut out = String::new();
        render_assertion(
            &mut out,
            &assertion,
            "result",
            &resolver,
            "Sample",
            &HashSet::new(),
            &HashMap::new(),
            false,
            false,
            false,
            false,
        );
        assert!(!out.contains("skipped"), "got: {out}");
    }

    /// `first_chunk_starts_with_heading` must assert the real `metadata.heading_context`
    /// field, matching `chunks_have_heading_context` a few lines above it in the generator —
    /// not a `content`-prefix proxy (`String.starts_with?(c.content, "#")`), which can pass on
    /// a chunk whose heading metadata was never attached (a literal "#" in unrelated source
    /// content) and would not catch a regression that dropped `heading_context` entirely.
    #[test]
    fn first_chunk_starts_with_heading_asserts_the_real_field_not_a_content_proxy() {
        let assertion = Assertion {
            assertion_type: "is_true".to_string(),
            field: Some("first_chunk_starts_with_heading".to_string()),
            ..Default::default()
        };
        let mut out = String::new();
        render_assertion(
            &mut out,
            &assertion,
            "result",
            &empty_resolver(),
            "Sample",
            &HashSet::new(),
            &HashMap::new(),
            false,
            false,
            false,
            false,
        );
        assert!(
            out.contains("c.metadata.heading_context != nil"),
            "must read the real field, got: {out}"
        );
        assert!(
            !out.contains("starts_with?") && !out.contains("\"#\""),
            "must not fall back to a content-prefix proxy, 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 elixir_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 = Assertion {
            assertion_type: "equals".to_string(),
            field: Some("internal_diagnostics".to_string()),
            value: Some(serde_json::Value::String("hello".to_string())),
            ..Default::default()
        };
        let mut out = String::new();
        render_assertion(
            &mut out,
            &assertion,
            "result",
            &resolver,
            "Sample",
            &HashSet::new(),
            &HashMap::new(),
            false,
            false,
            false,
            false,
        );
        assert!(out.contains("skipped"), "got: {out}");
    }

    /// Regression test for a one-sided-trim bug: `String.trim/1` wrapped the actual value
    /// while the fixture `expected` literal was emitted verbatim. Fixture expectations may
    /// legitimately end in `\n`, so trimming only one side made those assertions impossible
    /// to satisfy — and trimming both would silently mask real trailing-whitespace
    /// regressions. Equals is exact: neither side is normalized.
    #[test]
    fn render_assertion_equals_string_compares_exactly_without_trim() {
        let resolver = empty_resolver();
        let assertion = Assertion {
            assertion_type: "equals".to_string(),
            field: None,
            value: Some(serde_json::Value::String("hello\n".into())),
            ..Default::default()
        };
        let mut out = String::new();
        render_assertion(
            &mut out,
            &assertion,
            "result",
            &resolver,
            "Sample",
            &HashSet::new(),
            &HashMap::new(),
            true,
            false,
            false,
            false,
        );
        assert!(
            !out.contains("String.trim("),
            "equals must not trim either side; got: {out}"
        );
        assert!(out.contains("assert result =="), "got: {out}");
    }

    /// Control for the trim fix: the tightened contract must still DISCRIMINATE values that
    /// differ only in trailing whitespace. If either side were normalized, the emitted
    /// assertion for "hello\n" and for "hello" would be identical and a real trailing-newline
    /// regression would pass unnoticed.
    /// Control for the `is_empty` leniency: Elixir used to emit `String.trim(actual) == ""`,
    /// which accepts a whitespace-only value like "  \n". Every other backend rejects it
    /// (python emits a falsy check, typescript a `.length` check), so the same fixture
    /// passed in Elixir and failed elsewhere. The emitted check must compare the real value.
    #[test]
    fn render_assertion_is_empty_does_not_trim_actual_value() {
        let resolver = empty_resolver();
        let assertion = Assertion {
            assertion_type: "is_empty".to_string(),
            field: None,
            value: None,
            ..Default::default()
        };
        let mut out = String::new();
        render_assertion(
            &mut out,
            &assertion,
            "result",
            &resolver,
            "Sample",
            &HashSet::new(),
            &HashMap::new(),
            true,
            false,
            false,
            false,
        );
        assert!(
            !out.contains("String.trim("),
            "is_empty must not trim the actual value; got: {out}"
        );
        assert_eq!(
            out, "      assert result in [nil, \"\", [], %{}]\n",
            "emitted is_empty check drifted: {out}"
        );
    }

    #[test]
    fn render_assertion_equals_still_discriminates_trailing_whitespace() {
        let render_for = |value: &str| {
            let resolver = empty_resolver();
            let assertion = Assertion {
                assertion_type: "equals".to_string(),
                field: None,
                value: Some(serde_json::Value::String(value.into())),
                ..Default::default()
            };
            let mut out = String::new();
            render_assertion(
                &mut out,
                &assertion,
                "result",
                &resolver,
                "Sample",
                &HashSet::new(),
                &HashMap::new(),
                true,
                false,
                false,
                false,
            );
            out
        };
        let emitted = render_for("hello\n");
        // The actual side must be the bare expression: any normalizing call (trim/strip/
        // case-folding) wrapped around it would silently accept a mismatched value.
        assert_eq!(
            emitted, "      assert result == \"hello\\n\"\n",
            "emitted assertion drifted: {emitted}"
        );
        // And a value differing only by the trailing newline must still produce a
        // different expectation, proving trailing whitespace is discriminated.
        assert_ne!(
            emitted,
            render_for("hello"),
            "trailing newline must still change the emitted assertion"
        );
    }

    fn render_not_empty(field: Option<&str>, is_streaming: bool) -> String {
        let assertion = Assertion {
            assertion_type: "not_empty".to_string(),
            field: field.map(str::to_string),
            ..Default::default()
        };
        let mut out = String::new();
        render_assertion(
            &mut out,
            &assertion,
            "result",
            &empty_resolver(),
            "Sample",
            &HashSet::new(),
            &HashMap::new(),
            false,
            is_streaming,
            false,
            false,
        );
        out
    }

    #[test]
    fn not_empty_rejects_every_empty_shape_for_values_and_streams() {
        assert_eq!(
            render_not_empty(None, false).trim(),
            "assert result not in [nil, \"\", [], %{}]"
        );
        assert_eq!(
            render_not_empty(Some("chunks"), true).trim(),
            "assert result not in [nil, \"\", [], %{}]"
        );
    }

    #[test]
    fn display_as_text_field_accessor_emits_text_property_access() {
        // Build a field resolver with 'content' configured as display_as_text
        let mut display_fields = HashSet::new();
        display_fields.insert("content".to_string());

        let resolver = FieldResolver::new(
            &HashMap::new(),
            &HashSet::new(),
            &HashSet::new(),
            &HashSet::new(),
            &HashSet::new(),
        )
        .with_display_as_text_fields(display_fields);

        // Test that is_display_as_text recognizes the field
        assert!(
            resolver.is_display_as_text("content"),
            "resolver should recognize 'content' as display_as_text"
        );

        // Build the coerced_field_expr as the assertions code does
        let field_expr = "result.content";
        let field_is_display_as_text = resolver.is_display_as_text("content");

        assert!(
            field_is_display_as_text,
            "field_is_display_as_text should be true for 'content'"
        );

        let coerced = if field_is_display_as_text {
            format!("(({field_expr} && {field_expr}.text) || \"\")")
        } else {
            field_expr.to_string()
        };

        // Verify the coerced expression accesses .text with nil-guard
        assert_eq!(
            coerced, "((result.content && result.content.text) || \"\")",
            "display_as_text field should emit nil-guarded .text accessor"
        );
    }

    #[test]
    fn non_display_as_text_field_accessor_uses_bare_expression() {
        let resolver = FieldResolver::new(
            &HashMap::new(),
            &HashSet::new(),
            &HashSet::new(),
            &HashSet::new(),
            &HashSet::new(),
        )
        .with_display_as_text_fields(HashSet::new());

        let field_expr = "result.content";
        let field_is_display_as_text = resolver.is_display_as_text("content");

        assert!(
            !field_is_display_as_text,
            "field_is_display_as_text should be false when not in display_as_text set"
        );

        let coerced = if field_is_display_as_text {
            format!("(({field_expr} && {field_expr}.text) || \"\")")
        } else {
            field_expr.to_string()
        };

        // Verify the coerced expression does NOT access .text
        assert_eq!(
            coerced, "result.content",
            "non-display_as_text field should use bare expression"
        );
    }

    /// Regression test for the not_error vacuous/unused-variable defect: before this
    /// fix, `not_error` rendered nothing, leaving `test_case.rs`'s `{:ok, result} =
    /// call(...)` binding referenced nowhere — an "unused variable" warning that
    /// `mix compile --warnings-as-errors` promotes to a build failure downstream.
    /// Must emit a real, variable-consuming assertion instead.
    #[test]
    fn not_error_emits_a_real_refute_is_nil_and_consumes_the_binding() {
        let resolver = empty_resolver();
        let assertion = Assertion {
            assertion_type: "not_error".to_string(),
            field: None,
            value: None,
            ..Default::default()
        };
        let mut out = String::new();
        render_assertion(
            &mut out,
            &assertion,
            "result",
            &resolver,
            "Sample",
            &HashSet::new(),
            &HashMap::new(),
            false,
            false,
            false,
            true,
        );
        assert_eq!(out, "      refute is_nil(result)\n");
    }

    /// The caller (`test_case.rs`) already substitutes the collected `chunks`
    /// variable for `result_var` on streaming fixtures before calling
    /// `render_assertion` — confirm the not_error arm asserts on whatever
    /// variable it's given rather than hardcoding "result".
    #[test]
    fn not_error_asserts_on_whatever_variable_the_caller_passes() {
        let resolver = empty_resolver();
        let assertion = Assertion {
            assertion_type: "not_error".to_string(),
            field: None,
            value: None,
            ..Default::default()
        };
        let mut out = String::new();
        render_assertion(
            &mut out,
            &assertion,
            "chunks",
            &resolver,
            "Sample",
            &HashSet::new(),
            &HashMap::new(),
            false,
            true,
            false,
            true,
        );
        assert_eq!(out, "      refute is_nil(chunks)\n");
    }

    /// Regression test for the void `not_error` defect: before this fix, a `returns_void`
    /// fixture whose only assertion was `not_error` still fell into the `refute is_nil(result)`
    /// branch above — but rustler encodes a Rust `()` success payload as the atom `nil`, so
    /// that assertion FAILED on every successful call, not just an unsuccessful one. The
    /// `{:ok, result} = call(...)` binding `test_case.rs` already emits is the real check for a
    /// void call: an `{:error, _}` return raises `MatchError`, failing the test on its own.
    #[test]
    fn void_not_error_emits_nothing_relying_on_the_ok_match_above() {
        let resolver = empty_resolver();
        let assertion = Assertion {
            assertion_type: "not_error".to_string(),
            field: None,
            value: None,
            ..Default::default()
        };
        let mut out = String::new();
        render_assertion(
            &mut out,
            &assertion,
            "result",
            &resolver,
            "Sample",
            &HashSet::new(),
            &HashMap::new(),
            false,
            false,
            true,
            false,
        );
        assert!(
            out.is_empty(),
            "a void call's result is always nil; asserting non-nil would fail every successful \
             call, got: {out}"
        );
    }

    #[test]
    #[should_panic(expected = "unsupported assertion type 'bogus_type' on synthetic field 'chunks_have_content'")]
    fn elixir_synthetic_field_unsupported_type_fails_loudly() {
        let resolver = empty_resolver();
        let assertion = Assertion {
            assertion_type: "bogus_type".to_string(),
            field: Some("chunks_have_content".to_string()),
            ..Default::default()
        };
        let mut out = String::new();
        render_assertion(
            &mut out,
            &assertion,
            "result",
            &resolver,
            "Sample",
            &HashSet::new(),
            &HashMap::new(),
            false,
            false,
            false,
            false,
        );
    }

    #[test]
    fn elixir_synthetic_chunks_have_content_supported_type_renders_assertion() {
        let resolver = empty_resolver();
        let assertion = Assertion {
            assertion_type: "is_true".to_string(),
            field: Some("chunks_have_content".to_string()),
            ..Default::default()
        };
        let mut out = String::new();
        render_assertion(
            &mut out,
            &assertion,
            "result",
            &resolver,
            "Sample",
            &HashSet::new(),
            &HashMap::new(),
            false,
            false,
            false,
            false,
        );
        assert_eq!(
            out,
            "      assert Enum.all?(result.chunks || [], fn c -> c.content != nil and c.content != \"\" end)\n"
        );
    }

    #[test]
    #[should_panic(expected = "unsupported assertion type 'bogus_type' on synthetic field 'embeddings'")]
    fn elixir_synthetic_embeddings_unsupported_type_fails_loudly() {
        let resolver = empty_resolver();
        let assertion = Assertion {
            assertion_type: "bogus_type".to_string(),
            field: Some("embeddings".to_string()),
            ..Default::default()
        };
        let mut out = String::new();
        render_assertion(
            &mut out,
            &assertion,
            "result",
            &resolver,
            "Sample",
            &HashSet::new(),
            &HashMap::new(),
            false,
            false,
            false,
            false,
        );
    }

    #[test]
    fn elixir_synthetic_embeddings_supported_type_renders_assertion() {
        let resolver = empty_resolver();
        let assertion = Assertion {
            assertion_type: "not_empty".to_string(),
            field: Some("embeddings".to_string()),
            ..Default::default()
        };
        let mut out = String::new();
        render_assertion(
            &mut out,
            &assertion,
            "result",
            &resolver,
            "Sample",
            &HashSet::new(),
            &HashMap::new(),
            false,
            false,
            false,
            false,
        );
        assert_eq!(out, "      assert result != []\n");
    }

    /// This asserted a panic until the streaming type gap was routed through the skip funnel.
    /// A panic here aborts the whole consumer regen over one unrenderable assertion, which is a
    /// worse signal than a registered marker: the marker is counted by the census, is attributable
    /// to a named field and type, and the strict gate can still escalate it to a failure. Loud and
    /// fatal are not the same property, and only the first one was wanted. ~keep
    #[test]
    fn elixir_streaming_virtual_field_unsupported_type_is_counted_not_fatal() {
        let resolver = empty_resolver();
        let assertion = Assertion {
            assertion_type: "bogus_type".to_string(),
            field: Some("chunks".to_string()),
            ..Default::default()
        };
        let mut out = String::new();
        render_assertion(
            &mut out,
            &assertion,
            "result",
            &resolver,
            "Sample",
            &HashSet::new(),
            &HashMap::new(),
            false,
            true,
            false,
            false,
        );
        assert_eq!(
            out.trim_end(),
            streaming_assertion_type_skip_line("      ", "#", "chunks", "bogus_type"),
            "the gap must be recorded on the registered wording, not dropped: {out}"
        );
        assert_eq!(
            AssertionTypeSkip::extract_classified(out.trim_end()),
            Some(("bogus_type", AssertionTypeSkip::StreamingAssertionTypeNotSupported)),
            "an emitted marker the type funnel cannot recognise is uncounted, which is the \
             silent drop wearing a comment: {out}"
        );
    }

    #[test]
    fn elixir_streaming_virtual_field_supported_type_renders_assertion() {
        let resolver = empty_resolver();
        let assertion = Assertion {
            assertion_type: "count_min".to_string(),
            field: Some("chunks".to_string()),
            value: Some(serde_json::Value::from(1)),
            ..Default::default()
        };
        let mut out = String::new();
        render_assertion(
            &mut out,
            &assertion,
            "result",
            &resolver,
            "Sample",
            &HashSet::new(),
            &HashMap::new(),
            false,
            true,
            false,
            false,
        );
        assert_eq!(out, "      assert length(result) >= 1\n");
    }
}

#[cfg(test)]
#[path = "assertions/skip_marker_tests.rs"]
mod skip_marker_tests;
#[cfg(test)]
#[path = "assertions/wildcard_tests.rs"]
mod wildcard_tests;