assay-core 5.0.0

High-performance evaluation framework for LLM agents (Core)
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
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
use super::*;

#[test]
fn test_string_input_deserialize() {
    let yaml = r#"
            id: test1
            input: "simple string"
            expected:
              type: must_contain
              must_contain: ["foo"]
        "#;
    let tc: TestCase = serde_yaml::from_str(yaml).expect("failed to parse");
    assert_eq!(tc.input.prompt, "simple string");
}

#[test]
fn test_legacy_list_expected_single_entry() {
    let yaml = r#"
            id: test1
            input: "test"
            expected:
              - must_contain: "Paris"
        "#;
    let tc: TestCase = serde_yaml::from_str(yaml).expect("failed to parse");
    if let Expected::MustContain { must_contain } = tc.expected {
        assert_eq!(must_contain, vec!["Paris"]);
    } else {
        panic!("Expected MustContain, got {:?}", tc.expected);
    }
}

/// A multi-element `expected:` list used to keep element 0 and drop the rest in
/// silence, so a two-assertion block enforced half of what it claimed.
#[test]
fn test_multi_element_expected_list_is_rejected() {
    let yaml = r#"
            id: test1
            input: "test"
            expected:
              - must_contain: "Paris"
              - must_not_contain: "London"
        "#;
    let err = serde_yaml::from_str::<TestCase>(yaml)
        .expect_err("multi-element expected list must not parse");
    let msg = err.to_string();
    assert!(msg.contains("test1"), "message must name the test: {}", msg);
    assert!(
        msg.contains("2 entries"),
        "message must name the entry count: {}",
        msg
    );
}

#[test]
fn test_empty_expected_list_is_rejected() {
    let yaml = r#"
            id: test1
            input: "test"
            expected: []
        "#;
    let err =
        serde_yaml::from_str::<TestCase>(yaml).expect_err("empty expected list must not parse");
    assert!(err.to_string().contains("empty list"), "{}", err);
}

#[test]
fn test_explicit_null_expected_is_rejected_instead_of_treated_as_omitted() {
    let yaml = r#"
            id: explicit_null
            input: "test"
            expected: null
        "#;
    let err = serde_yaml::from_str::<TestCase>(yaml)
        .expect_err("an explicit null expected block must not become the omitted sentinel");
    assert!(err.to_string().contains("expected"), "{err}");
}

/// The headline regression: a typo in a key used to fall back to
/// `Expected::default()` (an empty `must_contain`), which passes unconditionally.
#[test]
fn test_unparsable_expected_object_is_hard_error() {
    let yaml = r#"
            id: typo_test
            input: "test"
            expected:
              must_contains: ["Paris"]
        "#;
    let err = serde_yaml::from_str::<TestCase>(yaml)
        .expect_err("unrecognized expected block must not parse");
    let msg = err.to_string();
    assert!(
        msg.contains("typo_test"),
        "message must name the test: {}",
        msg
    );
    assert!(
        msg.contains("must_contains"),
        "message must name the offending key: {}",
        msg
    );
}

/// Same typo, but inside a list entry: the other silent path to the default.
#[test]
fn test_unrecognized_expected_list_entry_is_hard_error() {
    let yaml = r#"
            id: typo_list
            input: "test"
            expected:
              - must_contains: ["Paris"]
        "#;
    let err = serde_yaml::from_str::<TestCase>(yaml)
        .expect_err("unrecognized expected list entry must not parse");
    let msg = err.to_string();
    assert!(
        msg.contains("typo_list") && msg.contains("must_contains"),
        "message must name test and key: {}",
        msg
    );
}

/// A tagged block whose VALUE shape is legacy must still parse. The strict parse
/// fails (a scalar is not a list), but the legacy heuristics understand it, and
/// rejecting it would turn working suites into config errors.
#[test]
fn test_tagged_block_with_legacy_scalar_value_still_parses() {
    let yaml = r#"
            id: tagged_scalar
            input: "test"
            expected:
              - type: must_contain
                must_contain: "hello"
        "#;
    let tc: TestCase = serde_yaml::from_str(yaml).expect("tagged block with scalar must parse");
    match tc.expected {
        Expected::MustContain { must_contain } => assert_eq!(must_contain, vec!["hello"]),
        other => panic!("Expected MustContain, got {:?}", other),
    }
}

/// `type: sequence` is not an `Expected` variant (the variant is `sequence_valid`),
/// but it is the shape documented in the migration guide, and the legacy `sequence`
/// key resolves it. It must keep working.
#[test]
fn test_legacy_type_sequence_still_parses() {
    let yaml = r#"
            id: legacy_seq
            input: "test"
            expected:
              - type: sequence
                sequence: ["Search", "Create"]
        "#;
    let tc: TestCase = serde_yaml::from_str(yaml).expect("legacy type: sequence must parse");
    match tc.expected {
        Expected::SequenceValid { sequence, .. } => {
            assert_eq!(
                sequence,
                Some(vec!["Search".to_string(), "Create".to_string()])
            );
        }
        other => panic!("Expected SequenceValid, got {:?}", other),
    }
}

/// An unparsable `sequence` value used to become `sequence: None` via `.ok()`, and
/// `sequence_valid` passes unconditionally with neither sequence nor rules — an
/// always-green test that no validate rule caught.
#[test]
fn test_unparsable_sequence_value_is_hard_error() {
    let yaml = r#"
            id: bad_seq
            input: "test"
            expected:
              sequence: 42
        "#;
    let err = serde_yaml::from_str::<TestCase>(yaml).expect_err("bad sequence must not parse");
    assert!(
        err.to_string().contains("`sequence` must be a list"),
        "{}",
        err
    );
}

/// An unparsable `must_contain` value used to collapse to an empty vec via
/// `unwrap_or_default()`, which passes for any response.
#[test]
fn test_unparsable_must_contain_value_is_hard_error() {
    let yaml = r#"
            id: bad_mc
            input: "test"
            expected:
              must_contain: {oops: 1}
        "#;
    let err = serde_yaml::from_str::<TestCase>(yaml).expect_err("bad must_contain must not parse");
    assert!(
        err.to_string().contains("`must_contain` must be a string"),
        "{}",
        err
    );
}

/// An assertion written out as empty passes for any response. Rejecting it at parse
/// time means every command that loads a config catches it, including `run` and `ci`.
#[test]
fn test_explicit_empty_must_contain_is_hard_error() {
    let yaml = r#"
            id: vacuous
            input: "test"
            expected:
              type: must_contain
              must_contain: []
        "#;
    let err =
        serde_yaml::from_str::<TestCase>(yaml).expect_err("empty must_contain must not parse");
    assert!(
        err.to_string().contains("would pass for any response"),
        "{}",
        err
    );
}

#[test]
fn test_explicit_empty_must_not_contain_is_hard_error() {
    let yaml = r#"
            id: vacuous
            input: "test"
            expected:
              type: must_not_contain
              must_not_contain: []
        "#;
    let err =
        serde_yaml::from_str::<TestCase>(yaml).expect_err("empty must_not_contain must not parse");
    assert!(
        err.to_string().contains("would pass for any response"),
        "{}",
        err
    );
}

#[test]
fn test_tagged_args_valid_without_policy_or_schema_is_hard_error() {
    let yaml = r#"
            id: vacuous_args
            input: "test"
            expected:
              type: args_valid
        "#;
    let err = serde_yaml::from_str::<TestCase>(yaml)
        .expect_err("args_valid without policy or schema must not parse");
    assert!(err.to_string().contains("asserts nothing"), "{}", err);
}

#[test]
fn test_tagged_sequence_valid_without_constraint_is_hard_error() {
    let yaml = r#"
            id: vacuous_sequence
            input: "test"
            expected:
              type: sequence_valid
        "#;
    let err = serde_yaml::from_str::<TestCase>(yaml)
        .expect_err("sequence_valid without a constraint must not parse");
    assert!(err.to_string().contains("asserts nothing"), "{}", err);
}

#[test]
fn test_tagged_sequence_valid_with_empty_sequence_is_an_exact_constraint() {
    let yaml = r#"
            id: vacuous_tagged_sequence
            input: "test"
            expected:
              type: sequence_valid
              sequence: []
        "#;
    let tc: TestCase = serde_yaml::from_str(yaml)
        .expect("an empty exact sequence requires the trace to contain no tool calls");
    assert!(matches!(
        tc.expected,
        Expected::SequenceValid {
            sequence: Some(ref sequence),
            ..
        } if sequence.is_empty()
    ));
}

#[test]
fn test_legacy_empty_sequence_is_an_exact_constraint() {
    let yaml = r#"
            id: vacuous_legacy_sequence
            input: "test"
            expected:
              sequence: []
        "#;
    serde_yaml::from_str::<TestCase>(yaml)
        .expect("legacy empty sequence still requires a trace with no tool calls");
}

#[test]
fn test_empty_inline_rules_cannot_erase_a_referenced_policy() {
    let yaml = r#"
            id: erased_policy
            input: "test"
            expected:
              type: sequence_valid
              policy: checks.yaml
              rules: []
        "#;
    let err = serde_yaml::from_str::<TestCase>(yaml)
        .expect_err("empty inline rules override the referenced policy and assert nothing");
    assert!(err.to_string().contains("asserts nothing"), "{err}");
}

#[test]
fn test_empty_sequence_with_nonempty_rules_still_parses() {
    let yaml = r#"
            id: rule_constrained_sequence
            input: "test"
            expected:
              type: sequence_valid
              sequence: []
              rules:
                - type: require
                  tool: Search
        "#;
    let tc: TestCase = serde_yaml::from_str(yaml).expect("nonempty rules assert a constraint");
    match tc.expected {
        Expected::SequenceValid { rules, .. } => {
            assert_eq!(rules.expect("rules").len(), 1);
        }
        other => panic!("Expected SequenceValid, got {:?}", other),
    }
}

#[test]
fn test_tagged_must_contain_with_only_empty_strings_is_hard_error() {
    let yaml = r#"
            id: vacuous_tagged_must_contain
            input: "test"
            expected:
              type: must_contain
              must_contain: [""]
        "#;
    let err = serde_yaml::from_str::<TestCase>(yaml)
        .expect_err("must_contain with only empty strings must not parse");
    assert!(err.to_string().contains("asserts nothing"), "{}", err);
}

#[test]
fn test_legacy_scalar_empty_must_contain_is_hard_error() {
    let yaml = r#"
            id: vacuous_legacy_must_contain
            input: "test"
            expected:
              must_contain: ""
        "#;
    let err = serde_yaml::from_str::<TestCase>(yaml)
        .expect_err("legacy empty must_contain must not parse");
    assert!(err.to_string().contains("asserts nothing"), "{}", err);
}

#[test]
fn test_tagged_empty_regex_match_is_hard_error() {
    let yaml = r#"
            id: vacuous_regex
            input: "test"
            expected:
              type: regex_match
              pattern: ""
        "#;
    let err =
        serde_yaml::from_str::<TestCase>(yaml).expect_err("an empty positive regex must not parse");
    assert!(err.to_string().contains("asserts nothing"), "{}", err);
}

#[test]
fn test_tagged_empty_tool_blocklist_is_hard_error() {
    let yaml = r#"
            id: vacuous_blocklist
            input: "test"
            expected:
              type: tool_blocklist
              blocked: []
        "#;
    let err =
        serde_yaml::from_str::<TestCase>(yaml).expect_err("an empty tool blocklist must not parse");
    assert!(err.to_string().contains("asserts nothing"), "{}", err);
}

#[test]
fn test_nonempty_semantic_constraints_still_parse() {
    let cases = [
        r#"
            id: constrained_must_contain
            input: "test"
            expected:
              type: must_contain
              must_contain: ["needle", ""]
        "#,
        r#"
            id: constrained_regex
            input: "test"
            expected:
              type: regex_match
              pattern: "needle"
        "#,
        r#"
            id: constrained_blocklist
            input: "test"
            expected:
              type: tool_blocklist
              blocked: ["exec"]
        "#,
    ];

    for yaml in cases {
        serde_yaml::from_str::<TestCase>(yaml).expect("a nonempty constraint must parse");
    }
}

#[test]
fn test_semantic_similarity_at_cosine_floor_is_hard_error() {
    let yaml = r#"
            id: vacuous_similarity
            input: "test"
            expected:
              type: semantic_similarity_to
              semantic_similarity_to: "reference"
              min_score: -1.0
        "#;
    let err = serde_yaml::from_str::<TestCase>(yaml)
        .expect_err("the cosine floor cannot reject a valid similarity score");
    assert!(err.to_string().contains("asserts nothing"), "{}", err);

    let epsilon_floor = yaml.replace("-1.0", "-0.9999995");
    let err = serde_yaml::from_str::<TestCase>(&epsilon_floor)
        .expect_err("the evaluator epsilon makes this threshold universally passing");
    assert!(err.to_string().contains("asserts nothing"), "{err}");

    let constrained = yaml.replace("-1.0", "-0.99");
    serde_yaml::from_str::<TestCase>(&constrained)
        .expect("a threshold above the cosine floor must parse");
}

#[test]
fn test_judge_criteria_without_an_evaluator_is_hard_error() {
    let yaml = r#"
            id: unsupported_judge
            input: "test"
            expected:
              type: judge_criteria
              judge_criteria:
                rubric: "be concise"
        "#;
    let err = serde_yaml::from_str::<TestCase>(yaml)
        .expect_err("an Expected variant with no evaluator must not parse");
    assert!(err.to_string().contains("not executable"), "{}", err);
}

#[test]
fn test_unimplemented_sequence_rules_are_hard_errors() {
    let rules = [
        "eventually\n                  tool: Search\n                  within: 2",
        "max_calls\n                  tool: Search\n                  max: 2",
        "after\n                  trigger: Search\n                  then: Create\n                  within: 2",
        "never_after\n                  trigger: Delete\n                  forbidden: Export",
        "sequence\n                  tools: [Search, Create]\n                  strict: true",
    ];

    for rule in rules {
        let yaml = format!(
            r#"
            id: unsupported_sequence_rule
            input: "test"
            expected:
              type: sequence_valid
              rules:
                - type: {rule}
        "#
        );
        let err = serde_yaml::from_str::<TestCase>(&yaml)
            .expect_err("a sequence rule ignored by the evaluator must not parse");
        assert!(err.to_string().contains("not executable"), "{}", err);
    }
}

#[test]
fn test_tautological_before_rule_is_hard_error() {
    let yaml = r#"
            id: tautological_before
            input: "test"
            expected:
              type: sequence_valid
              rules:
                - type: before
                  first: Search
                  then: Search
        "#;
    let err = serde_yaml::from_str::<TestCase>(yaml)
        .expect_err("before with identical operands passes every trace");
    assert!(err.to_string().contains("cannot constrain"), "{}", err);
}

#[test]
fn test_supported_sequence_rules_still_parse() {
    let yaml = r#"
            id: supported_sequence_rules
            input: "test"
            expected:
              type: sequence_valid
              rules:
                - type: require
                  tool: Search
                - type: before
                  first: Search
                  then: Create
                - type: blocklist
                  pattern: Delete
        "#;
    serde_yaml::from_str::<TestCase>(yaml).expect("implemented sequence rules must parse");
}

#[test]
fn test_obviously_universal_args_schemas_are_hard_errors() {
    let schemas = ["{}", "{Search: {}}", "{Search: true, Create: {}}"];

    for schema in schemas {
        let yaml = format!(
            r#"
            id: vacuous_args_schema
            input: "test"
            expected:
              type: args_valid
              policy: ignored-by-inline-schema.yaml
              schema: {schema}
        "#
        );
        let err = serde_yaml::from_str::<TestCase>(&yaml)
            .expect_err("an inline schema map that accepts everything must not parse");
        assert!(err.to_string().contains("asserts nothing"), "{}", err);
    }
}

#[test]
fn test_obviously_universal_output_schemas_are_hard_errors() {
    let schemas = ["{}", "{Search: {}}", "{Search: true, Create: {}}"];

    for schema in schemas {
        let yaml = format!(
            r#"
            id: vacuous_output_schema
            input: "test"
            expected:
              type: tool_output_valid
              schemas: {schema}
        "#
        );
        let err = serde_yaml::from_str::<TestCase>(&yaml)
            .expect_err("an output schema map that accepts everything must not parse");
        assert!(err.to_string().contains("asserts nothing"), "{}", err);
    }
}

#[test]
fn test_constraining_schema_maps_still_parse() {
    let cases = [
        r#"
            id: constrained_args_schema
            input: "test"
            expected:
              type: args_valid
              schema:
                Search:
                  type: object
                  required: [query]
        "#,
        r#"
            id: constrained_output_schema
            input: "test"
            expected:
              type: tool_output_valid
              schemas:
                Search:
                  type: object
                  required: [results]
        "#,
    ];

    for yaml in cases {
        serde_yaml::from_str::<TestCase>(yaml).expect("a constraining schema map must parse");
    }
}

#[test]
fn test_structured_policy_combines_trivial_schemas_with_effective_enforcement() {
    let yaml = r#"
            id: structured_allowlist
            input: "test"
            expected:
              type: args_valid
              schema:
                version: "2.0"
                enforcement:
                  unconstrained_tools: deny
                schemas:
                  Search: true
        "#;

    let test = serde_yaml::from_str::<TestCase>(yaml)
        .expect("trivial schemas participate in an effective structured allowlist");
    crate::model::validate_test_case_for_execution(&test)
        .expect("combined structured constraints must be validated in context");
}

#[test]
fn test_explicit_schema_containers_preserve_keyword_tool_names() {
    let cases = [
        r#"
            id: structured_keyword_tool
            input: "test"
            expected:
              type: args_valid
              schema:
                version: "2.0"
                schemas:
                  properties:
                    type: object
                    required: [query]
        "#,
        r#"
            id: output_keyword_tool
            input: "test"
            expected:
              type: tool_output_valid
              schemas:
                type:
                  type: object
                  required: [result]
        "#,
        r#"
            id: bare_metadata_named_tool
            input: "test"
            expected:
              type: args_valid
              schema:
                allow:
                  type: object
                  required: [query]
        "#,
    ];

    for yaml in cases {
        let test = serde_yaml::from_str::<TestCase>(yaml)
            .expect("an explicit schema container must not reserve valid tool names");
        crate::model::validate_test_case_for_execution(&test)
            .expect("explicit containers remove root-schema ambiguity");
    }
}

#[test]
fn schemas_only_policy_shape_is_rejected_as_ambiguous() {
    let ambiguous = serde_json::json!({
        "schemas": {
            "properties": {
                "query": {"type": "string"}
            }
        }
    });
    let err = crate::model::validate_args_policy_value(&ambiguous)
        .expect_err("schemas-only input has two valid interpretations");
    assert!(err.to_string().contains("ambiguous"), "{err:#}");

    let explicit = serde_json::json!({
        "version": "2.0",
        "schemas": {
            "schemas": {
                "properties": {
                    "query": {"type": "string"}
                }
            }
        }
    });
    crate::model::validate_args_policy_value(&explicit)
        .expect("a versioned container disambiguates the tool named schemas");
}

#[test]
fn structured_args_policy_rejects_unenforced_nested_tool_controls() {
    let policy = serde_json::json!({
        "version": "2.0",
        "tools": {
            "deny": ["never_called"],
            "restrict_scope": ["exec"]
        }
    });

    let err = crate::model::validate_args_policy_value(&policy)
        .expect_err("nested controls outside the args_valid evaluator must not be ignored");
    assert!(err.to_string().contains("tools.restrict_scope"), "{err:#}");
}

#[test]
fn structured_args_policy_supports_shared_defs_but_defs_alone_are_vacuous() {
    let shared_defs = serde_json::json!({
        "safe_path": {
            "type": "string",
            "pattern": "^/workspace/.*"
        }
    });
    let policy = serde_json::json!({
        "version": "2.0",
        "schemas": {
            "$defs": shared_defs.clone(),
            "read_file": {
                "type": "object",
                "properties": {
                    "path": {"$ref": "#/$defs/safe_path"}
                },
                "required": ["path"]
            }
        }
    });
    crate::model::validate_args_policy_value(&policy)
        .expect("documented shared definitions must compile for each tool schema");

    let defs_only = serde_json::json!({
        "version": "2.0",
        "schemas": {"$defs": shared_defs}
    });
    let err = crate::model::validate_args_policy_value(&defs_only)
        .expect_err("shared definitions without a tool schema assert nothing");
    assert!(err.to_string().contains("asserts nothing"), "{err:#}");
}

#[test]
fn malformed_structured_version_never_falls_back_to_a_legacy_tool_map() {
    let malformed = serde_json::json!({
        "version": {},
        "schemas": {
            "read_file": {"type": "object"}
        }
    });
    let err = crate::model::validate_args_policy_value(&malformed)
        .expect_err("a present structured discriminator must be validated");
    assert!(
        err.to_string().contains("version must be a string"),
        "{err:#}"
    );

    let explicit_tool = serde_json::json!({
        "version": "2.0",
        "schemas": {
            "version": {"type": "object"}
        }
    });
    crate::model::validate_args_policy_value(&explicit_tool)
        .expect("the structured container still permits a tool named version");
}

#[test]
fn shared_defs_must_be_a_mapping_even_for_boolean_tool_schemas() {
    let malformed = serde_json::json!({
        "version": "2.0",
        "schemas": {
            "$defs": ["not", "a", "mapping"],
            "deny_all": false
        }
    });
    let err = crate::model::validate_args_policy_value(&malformed)
        .expect_err("malformed shared definitions must not disappear beside a boolean schema");
    assert!(
        err.to_string().contains("$defs must be a mapping"),
        "{err:#}"
    );
}

#[test]
fn shared_defs_are_validated_even_when_all_tool_schemas_are_boolean() {
    let malformed = serde_json::json!({
        "version": "2.0",
        "schemas": {
            "$defs": {
                "invalid": {"type": "not-a-json-schema-type"}
            },
            "deny_all": false
        }
    });
    let err = crate::model::validate_args_policy_value(&malformed)
        .expect_err("shared definitions are policy input even when no object schema uses them");
    assert!(
        err.to_string().contains("shared $defs failed to compile"),
        "{err:#}"
    );
}

#[test]
fn tool_local_defs_without_assertions_are_vacuous() {
    let policy = serde_json::json!({
        "Search": {
            "$defs": {
                "query": {"type": "string"}
            }
        }
    });
    let err = crate::model::validate_args_policy_value(&policy)
        .expect_err("definitions do not constrain a tool call unless a schema references them");
    assert!(err.to_string().contains("asserts nothing"), "{err:#}");
}

#[test]
fn tool_schema_annotations_do_not_make_shared_defs_effective() {
    let annotations = [
        ("$comment", serde_json::json!("implementation note")),
        ("$id", serde_json::json!("urn:assay:test-schema")),
        (
            "$schema",
            serde_json::json!("https://json-schema.org/draft/2020-12/schema"),
        ),
        ("title", serde_json::json!("Search arguments")),
        ("description", serde_json::json!("Shared query definitions")),
        ("default", serde_json::json!({})),
        ("deprecated", serde_json::json!(false)),
        ("readOnly", serde_json::json!(true)),
        ("writeOnly", serde_json::json!(true)),
        ("examples", serde_json::json!([])),
        ("id", serde_json::json!("legacy-schema-id")),
        (
            "definitions",
            serde_json::json!({"unused": {"type": "string"}}),
        ),
        ("$recursiveAnchor", serde_json::json!(true)),
        ("x-note", serde_json::json!("extension annotation")),
    ];

    for (keyword, annotation) in annotations {
        let mut schema = serde_json::json!({
            "$defs": {"query": {"type": "string"}}
        });
        schema
            .as_object_mut()
            .expect("schema object")
            .insert(keyword.to_string(), annotation);
        let policy = serde_json::json!({"Search": schema});
        let err = crate::model::validate_args_policy_value(&policy)
            .expect_err("annotations do not constrain any instance");
        assert!(
            err.to_string().contains("asserts nothing"),
            "{keyword}: {err:#}"
        );
    }
}

#[test]
fn empty_required_does_not_make_a_schema_effective() {
    let no_ops = [
        serde_json::json!({"required": []}),
        serde_json::json!({"dependentRequired": {"kind": []}}),
        serde_json::json!({"properties": {"query": true}}),
        serde_json::json!({"allOf": [true]}),
        serde_json::json!({"anyOf": [true]}),
        serde_json::json!({"oneOf": [true]}),
        serde_json::json!({"if": {"type": "string"}}),
        serde_json::json!({"minLength": 0}),
        serde_json::json!({"uniqueItems": false}),
        serde_json::json!({"$defs": {"noop": true}, "$ref": "#/$defs/noop"}),
        serde_json::json!({"pattern": ""}),
        serde_json::json!({"oneOf": [true, false]}),
        serde_json::json!({"contains": false, "minContains": 0}),
        serde_json::json!({"if": false, "then": false}),
        serde_json::json!({
            "$schema": "https://json-schema.org/draft/2020-12/schema",
            "additionalItems": false
        }),
        serde_json::json!({
            "$schema": "http://json-schema.org/draft-04/schema#",
            "const": 5
        }),
        serde_json::json!({
            "$schema": "http://json-schema.org/draft-07/schema#",
            "dependentSchemas": {"credit_card": false}
        }),
    ];
    for schema in no_ops {
        let policy = serde_json::json!({"Search": schema});
        let err = crate::model::validate_args_policy_value(&policy)
            .expect_err("a no-op JSON Schema must constrain no instance");
        assert!(err.to_string().contains("asserts nothing"), "{err:#}");
    }

    for schema in [
        serde_json::json!({"required": ["query"]}),
        serde_json::json!({"properties": {"query": false}}),
        serde_json::json!({"oneOf": [true, true]}),
        serde_json::json!({"if": true, "then": false}),
        serde_json::json!({"minLength": 1}),
        serde_json::json!({"uniqueItems": true}),
        serde_json::json!({"$defs": {"deny": false}, "$ref": "#/$defs/deny"}),
        serde_json::json!({"contains": true}),
        serde_json::json!({"if": false, "else": false}),
        serde_json::json!({
            "$schema": "http://json-schema.org/draft-07/schema#",
            "dependencies": {"credit_card": ["billing_address"]}
        }),
        serde_json::json!({
            "$schema": "http://json-schema.org/draft-07/schema#",
            "format": "email"
        }),
        serde_json::json!({
            "$schema": "http://json-schema.org/draft-07/schema#",
            "items": [true],
            "additionalItems": false
        }),
        serde_json::json!({
            "$schema": "http://json-schema.org/draft-07/schema#",
            "contains": false,
            "minContains": 0
        }),
    ] {
        let policy = serde_json::json!({"Search": schema});
        crate::model::validate_args_policy_value(&policy)
            .expect("an asserting JSON Schema must remain executable");
    }
}

#[test]
fn ref_shaped_instance_data_is_not_a_schema_reference() {
    let policy = serde_json::json!({
        "version": "2.0",
        "schemas": {
            "record": {"const": {"$ref": "https://example.invalid/instance-data"}}
        }
    });
    crate::model::validate_args_policy_value(&policy)
        .expect("a $ref key inside const is ordinary instance data");
}

#[test]
fn json_schema_expected_does_not_retrieve_file_refs() {
    let dir = tempfile::tempdir().expect("tempdir");
    let schema_path = dir.path().join("external.json");
    std::fs::write(&schema_path, r#"{"type":"string"}"#).expect("write external schema");
    let external_ref = url::Url::from_file_path(&schema_path)
        .expect("absolute path becomes file URL")
        .to_string();
    let schema = serde_json::json!({"$ref": external_ref}).to_string();
    let yaml = format!(
        "id: local_only_schema\ninput: test\nexpected:\n  type: json_schema\n  json_schema: '{}'\n",
        schema.replace('\'', "''")
    );
    let test = serde_yaml::from_str::<TestCase>(&yaml).expect("test case parses");

    let err = crate::model::validate_test_case_for_execution(&test)
        .expect_err("preflight must not retrieve an external schema");
    assert!(err.to_string().contains("schema compile failed"), "{err:#}");
}

#[test]
fn shared_defs_cannot_overwrite_tool_local_definitions() {
    let collision = serde_json::json!({
        "version": "2.0",
        "schemas": {
            "$defs": {"identifier": {"type": "string"}},
            "lookup": {
                "$defs": {"identifier": {"type": "integer"}},
                "$ref": "#/$defs/identifier"
            }
        }
    });
    let err = crate::model::validate_args_policy_value(&collision)
        .expect_err("shared and local definitions need explicit collision semantics");
    assert!(
        err.to_string().contains("$defs entries must not overlap"),
        "{err:#}"
    );
}

#[test]
fn external_schema_refs_are_rejected_before_retrieval() {
    let dir = tempfile::tempdir().expect("tempdir");
    let schema_path = dir.path().join("external.json");
    std::fs::write(&schema_path, r#"{"type":"string"}"#).expect("write external schema");
    let external_ref = url::Url::from_file_path(&schema_path)
        .expect("absolute path becomes file URL")
        .to_string();
    let policy = serde_json::json!({
        "version": "2.0",
        "schemas": {"lookup": {"$ref": external_ref}}
    });

    let err = crate::model::validate_args_policy_value(&policy)
        .expect_err("policy validation must never retrieve an external schema");
    assert!(
        err.to_string()
            .contains("external JSON Schema retrieval is disabled"),
        "{err:#}"
    );
}

#[test]
fn test_tagged_tool_output_valid_without_schemas_is_hard_error() {
    let yaml = r#"
            id: vacuous_output
            input: "test"
            expected:
              type: tool_output_valid
        "#;
    let err = serde_yaml::from_str::<TestCase>(yaml)
        .expect_err("tool_output_valid without schemas must not parse");
    assert!(err.to_string().contains("asserts nothing"), "{}", err);
}

/// A block that opts into the tagged form and matches NO legacy key gets the
/// underlying serde error, not the generic "unrecognized keys" message.
#[test]
fn test_tagged_expected_reports_underlying_error() {
    let yaml = r#"
            id: bad_tagged
            input: "test"
            expected:
              type: regex_match
              pattten: "^hi"
        "#;
    let err = serde_yaml::from_str::<TestCase>(yaml)
        .expect_err("tagged block with a missing field must not parse");
    let msg = err.to_string();
    assert!(
        msg.contains("bad_tagged") && msg.contains("pattern"),
        "message must name the test and the missing field: {}",
        msg
    );
}

/// A failed tagged parse must not change metric type through an unrelated legacy
/// key. This block asks for `regex_match`; accepting it as `must_contain` would
/// silently enforce a different assertion than the author selected.
#[test]
fn test_tagged_parse_failure_cannot_fallback_to_different_legacy_metric() {
    let yaml = r#"
            id: mismatched_tag
            input: "test"
            expected:
              type: regex_match
              must_contain: "not-the-dummy-output"
        "#;
    let err = serde_yaml::from_str::<TestCase>(yaml)
        .expect_err("failed tagged parse must not change metric type");
    let msg = err.to_string();
    assert!(
        msg.contains("pattern"),
        "message must preserve the tagged parse failure: {}",
        msg
    );
}

#[test]
fn test_unrelated_tagged_failure_is_not_replaced_by_legacy_value_error() {
    let yaml = r#"
            id: mismatched_malformed_legacy
            input: "test"
            expected:
              type: regex_match
              must_contain: {not: a-list}
        "#;
    let err = serde_yaml::from_str::<TestCase>(yaml)
        .expect_err("an unrelated legacy decoder must not replace the tagged error");
    let msg = err.to_string();
    assert!(
        msg.contains("invalid `expected:` block") && !msg.contains("must be a string or a list"),
        "message must preserve the tagged parse failure: {}",
        msg
    );
}

#[test]
fn test_valid_tagged_metric_rejects_additional_legacy_assertion() {
    let yaml = r#"
            id: tagged_extra
            input: "test"
            expected:
              type: regex_match
              pattern: "^hello$"
              must_contain: "ignored"
        "#;
    let err = serde_yaml::from_str::<TestCase>(yaml)
        .expect_err("a tagged assertion must not ignore another assertion");
    assert!(err.to_string().contains("must_contain"), "{}", err);
}

#[test]
fn test_valid_tagged_metric_rejects_unknown_field() {
    let yaml = r#"
            id: tagged_typo
            input: "test"
            expected:
              type: regex_match
              pattern: "^hello$"
              pattten: "ignored"
        "#;
    let err = serde_yaml::from_str::<TestCase>(yaml)
        .expect_err("a tagged assertion must not ignore a misspelled field");
    assert!(err.to_string().contains("pattten"), "{}", err);
}

#[test]
fn test_legacy_metric_rejects_unknown_field() {
    let yaml = r#"
            id: legacy_typo
            input: "test"
            expected:
              must_contain: "hello"
              extra_check: "ignored"
        "#;
    let err = serde_yaml::from_str::<TestCase>(yaml)
        .expect_err("a legacy assertion must not ignore a misspelled field");
    assert!(err.to_string().contains("extra_check"), "{}", err);
}

#[test]
fn test_tagged_legacy_compatibility_rejects_second_assertion() {
    let yaml = r#"
            id: tagged_ambiguous
            input: "test"
            expected:
              type: must_contain
              must_contain: "hello"
              sequence: ["Search"]
        "#;
    let err = serde_yaml::from_str::<TestCase>(yaml)
        .expect_err("tagged compatibility must not hide a second assertion");
    assert!(err.to_string().contains("ambiguous"), "{}", err);
}

#[test]
fn test_scalar_expected_value_is_rejected() {
    let yaml = r#"
            id: scalar_expected
            input: "test"
            expected: "hello"
        "#;
    let err = serde_yaml::from_str::<TestCase>(yaml).expect_err("scalar expected must not parse");
    assert!(err.to_string().contains("must be a mapping"), "{}", err);
}

#[test]
fn test_legacy_ref_still_parses_and_requires_a_string() {
    let valid = r#"
            id: ref_test
            input: "test"
            expected:
              $ref: "shared/checks.yaml"
        "#;
    let tc: TestCase = serde_yaml::from_str(valid).expect("legacy $ref must parse");
    match tc.expected {
        Expected::Reference { path } => assert_eq!(path, "shared/checks.yaml"),
        other => panic!("Expected Reference, got {:?}", other),
    }

    let invalid = r#"
            id: bad_ref
            input: "test"
            expected:
              $ref: 42
        "#;
    let err =
        serde_yaml::from_str::<TestCase>(invalid).expect_err("non-string $ref must not parse");
    assert!(
        err.to_string().contains("`$ref` must be a string"),
        "{}",
        err
    );
}

#[test]
fn test_legacy_schema_parses_but_cannot_be_combined() {
    let valid = r#"
            id: schema_test
            input: "test"
            expected:
              schema:
                Search: {type: object}
        "#;
    let tc: TestCase = serde_yaml::from_str(valid).expect("legacy schema must parse");
    assert!(matches!(tc.expected, Expected::ArgsValid { .. }));

    let ambiguous = r#"
            id: schema_ambiguous
            input: "test"
            expected:
              schema: {Search: {type: object}}
              must_contain: "hello"
        "#;
    let err = serde_yaml::from_str::<TestCase>(ambiguous)
        .expect_err("schema plus another legacy assertion must not parse");
    assert!(err.to_string().contains("ambiguous"), "{}", err);
}

/// Untagged single mappings are read with the same legacy heuristics as list
/// entries. Before the fix, this shape silently became an empty `must_contain`.
#[test]
fn test_untagged_single_object_uses_legacy_heuristics() {
    let yaml = r#"
            id: test1
            input: "test"
            expected:
              must_contain: ["Paris"]
        "#;
    let tc: TestCase = serde_yaml::from_str(yaml).expect("failed to parse");
    match tc.expected {
        Expected::MustContain { must_contain } => assert_eq!(must_contain, vec!["Paris"]),
        other => panic!("Expected MustContain, got {:?}", other),
    }
}

/// Multiple legacy keys are multiple assertions. Choosing one would silently
/// discard the rest, so the single-assertion model must reject the block.
#[test]
fn test_ambiguous_legacy_expected_is_rejected() {
    let yaml = r#"
            id: ambiguous_legacy
            input: "test"
            expected:
              must_contain: "passed"
              sequence: ["Search"]
        "#;
    let err = serde_yaml::from_str::<TestCase>(yaml)
        .expect_err("ambiguous legacy assertions must not be truncated");
    let msg = err.to_string();
    assert!(msg.contains("ambiguous"), "{}", msg);
    assert!(
        msg.contains("must_contain") && msg.contains("sequence"),
        "{}",
        msg
    );
}

/// Writers must not emit a config the parser rejects.
///
/// A test that omits `expected:` holds the vacuous default. Serializing it verbatim
/// would write `must_contain: []`, which is now a hard parse error — so `assay migrate`
/// would produce files that no longer load. `skip_serializing_if` prevents that; this
/// test pins the round-trip.
#[test]
fn test_omitted_expected_round_trips_through_serialization() {
    let yaml = r#"
            id: assertions_only
            input: "test"
            assertions:
              - type: trace_must_call_tool
                tool: Search
        "#;
    let tc: TestCase = serde_yaml::from_str(yaml).expect("failed to parse");

    let written = serde_yaml::to_string(&tc).expect("serialize");
    assert!(
        !written.contains("must_contain"),
        "vacuous default must not be materialised into config: {}",
        written
    );

    let reparsed: TestCase = serde_yaml::from_str(&written).expect("writer output must load again");
    assert_eq!(reparsed.id, "assertions_only");
}

#[test]
fn test_explicit_expected_variant_is_not_erased_during_serialization() {
    let tc = TestCase {
        id: "explicit-regex".into(),
        input: TestInput {
            prompt: "test".into(),
            context: None,
        },
        expected: Expected::RegexMatch {
            pattern: String::new(),
            flags: Vec::new(),
        },
        assertions: None,
        on_error: None,
        tags: Vec::new(),
        metadata: None,
    };

    let written = serde_yaml::to_string(&tc).expect("serialize");
    assert!(written.contains("regex_match"), "{written}");
    assert!(written.contains("pattern"), "{written}");
}

#[test]
fn test_impossible_negative_assertions_are_rejected() {
    for yaml in [
        r#"
            id: impossible_substring
            input: "test"
            expected:
              type: must_not_contain
              must_not_contain: [""]
        "#,
        r#"
            id: impossible_regex
            input: "test"
            expected:
              type: regex_not_match
              pattern: ""
        "#,
    ] {
        let err = serde_yaml::from_str::<TestCase>(yaml)
            .expect_err("an assertion that no response can satisfy must be rejected");
        assert!(err.to_string().contains("pass"), "{err}");
    }
}

/// (d) A missing `expected:` key stays permissive: `assertions:` may carry the
/// checks. `assay validate` reports the case where neither is present.
#[test]
fn test_missing_expected_key_still_parses() {
    let yaml = r#"
            id: test1
            input: "test"
        "#;
    let tc: TestCase = serde_yaml::from_str(yaml).expect("missing expected must stay permissive");
    match tc.expected {
        Expected::MustContain { must_contain } => assert!(must_contain.is_empty()),
        other => panic!("Expected default MustContain, got {:?}", other),
    }
}

#[test]
fn test_scalar_must_contain_promotion() {
    let yaml = r#"
            id: test1
            input: "test"
            expected:
              - must_contain: "single value"
        "#;
    let tc: TestCase = serde_yaml::from_str(yaml).unwrap();
    if let Expected::MustContain { must_contain } = tc.expected {
        assert_eq!(must_contain, vec!["single value"]);
    } else {
        panic!("Expected MustContain");
    }
}

#[test]
fn test_validate_ref_in_v1() {
    let config = EvalConfig {
        version: 1,
        suite: "test".into(),
        model: "test".into(),
        settings: Settings::default(),
        thresholds: Default::default(),
        tests: vec![TestCase {
            id: "t1".into(),
            input: TestInput {
                prompt: "hi".into(),
                context: None,
            },
            expected: Expected::Reference {
                path: "foo.yaml".into(),
            },
            assertions: None,
            tags: vec![],
            metadata: None,
            on_error: None,
        }],
        otel: Default::default(),
    };
    assert!(config.validate().is_err());
}

#[test]
fn test_thresholding_for_metric() {
    // No thresholding
    let exp = Expected::SemanticSimilarityTo {
        semantic_similarity_to: "ref".into(),
        min_score: 0.8,
        thresholding: None,
    };
    assert!(exp
        .thresholding_for_metric("semantic_similarity_to")
        .is_none());
    // With thresholding
    let exp = Expected::SemanticSimilarityTo {
        semantic_similarity_to: "ref".into(),
        min_score: 0.8,
        thresholding: Some(ThresholdingConfig {
            max_drop: Some(0.05),
        }),
    };
    let t = exp
        .thresholding_for_metric("semantic_similarity_to")
        .unwrap();
    assert_eq!(t.max_drop, Some(0.05));
    // Wrong metric name
    assert!(exp.thresholding_for_metric("faithfulness").is_none());
    // Faithfulness variant
    let exp = Expected::Faithfulness {
        min_score: 0.7,
        rubric_version: None,
        thresholding: Some(ThresholdingConfig {
            max_drop: Some(0.1),
        }),
    };
    let t = exp.thresholding_for_metric("faithfulness").unwrap();
    assert_eq!(t.max_drop, Some(0.1));
}

#[test]
fn f1_structured_policy_with_only_object_roots_is_not_vacuous() {
    // Regression: a structured args_valid policy whose root values are all
    // objects (no scalar discriminator) must not be rejected as vacuous.
    for schema in [
        serde_json::json!({
            "tools": {"allow": ["read_*"]},
            "schemas": {"read_file": {"type": "object", "required": ["path"]}}
        }),
        serde_json::json!({
            "enforcement": {"unconstrained_tools": "deny"},
            "schemas": {"read_file": {"type": "object", "required": ["path"]}}
        }),
        // Maximally strict and carrying no schemas at all.
        serde_json::json!({"tools": {"deny": ["*"]}}),
        // Deny-by-default with no per-tool schemas: pins the `enforcement`
        // clause, which every other case short-circuits before reaching.
        serde_json::json!({"enforcement": {"unconstrained_tools": "deny"}}),
    ] {
        let expected = Expected::ArgsValid {
            schema: Some(schema.clone()),
            policy: None,
        };
        assert!(
            crate::model::validation::vacuous_expected_field(&expected).is_none(),
            "structured policy wrongly rejected as vacuous: {schema}"
        );
    }
}

#[test]
fn dependencies_asserts_under_every_dialect() {
    // jsonschema compiles `dependencies` whenever the applicator vocabulary is
    // declared, which 2020-12 does — so a policy using it is enforced at
    // runtime and must not be rejected at parse as vacuous.
    for schema in [
        serde_json::json!({
            "$schema": "https://json-schema.org/draft/2020-12/schema",
            "dependencies": {"credit_card": ["billing_address"]}
        }),
        serde_json::json!({"dependencies": {"credit_card": ["billing_address"]}}),
        serde_json::json!({
            "$schema": "http://json-schema.org/draft-07/schema#",
            "dependencies": {"credit_card": ["billing_address"]}
        }),
    ] {
        let policy = serde_json::json!({"pay": schema.clone()});
        crate::model::validate_args_policy_value(&policy)
            .unwrap_or_else(|e| panic!("`dependencies` must assert: {schema} -> {e:#}"));
    }
}

#[test]
fn dependencies_no_op_forms_are_still_vacuous() {
    // Removing the old no_ops row left the arm with no negative coverage, so a
    // blanket `"dependencies" => true` would have passed CI.
    for schema in [
        serde_json::json!({
            "$schema": "https://json-schema.org/draft/2020-12/schema",
            "dependencies": {"credit_card": []}
        }),
        serde_json::json!({"dependencies": {}}),
    ] {
        let policy = serde_json::json!({"pay": schema.clone()});
        assert!(
            crate::model::validate_args_policy_value(&policy).is_err(),
            "`dependencies` no-op must stay vacuous: {schema}"
        );
    }
}

#[test]
fn boolean_dependency_subschema_asserts() {
    // jsonschema compiles a bool dependency (canonical/parse.rs matches
    // `Value::Object(_) | Value::Bool(_)`), and `false` rejects every object
    // carrying the key — maximally strict, so never vacuous.
    let policy = serde_json::json!({"pay": {"dependencies": {"credit_card": false}}});
    crate::model::validate_args_policy_value(&policy)
        .expect("a `false` dependency subschema is maximally strict, not vacuous");
}

#[test]
fn universal_or_empty_tool_allowlists_do_not_rescue_a_vacuous_policy() {
    // The load-time oracle must agree with `validate_args_policy_value`: an
    // allow-list of `*` or an empty list narrows nothing.
    for schema in [
        serde_json::json!({"tools": {"allow": ["*"]}}),
        serde_json::json!({"tools": {"allow": []}}),
    ] {
        let expected = Expected::ArgsValid {
            schema: Some(schema.clone()),
            policy: None,
        };
        assert!(
            crate::model::validation::vacuous_expected_field(&expected).is_some(),
            "a universal or empty allow-list asserts nothing: {schema}"
        );
    }
}

#[test]
fn args_policy_oracles_agree() {
    // Two implementations of one rule; every round of review found them drifted
    // apart somewhere new, so pin them to each other rather than separately.
    //
    // The safety direction is unconditional: load must never reject a policy
    // that execution would accept, or a working config stops loading. The
    // converse holds only for policies the load-time rule claims to fully
    // understand; for the rest it deliberately defers so execution's specific
    // diagnosis ("not enforced", "must be a list") is not replaced by a generic
    // "asserts nothing".
    let fully_understood = [
        serde_json::json!({"tools": {"allow": ["read_*"]}}),
        serde_json::json!({"tools": {"allow": ["*"]}}),
        serde_json::json!({"tools": {"allow": []}}),
        serde_json::json!({"tools": {"allow": ["*", "read_x"]}}),
        serde_json::json!({"tools": {"deny": []}}),
        serde_json::json!({"tools": {"deny": ["*"]}}),
        serde_json::json!({"allow": ["*"]}),
        serde_json::json!({"allow": []}),
        serde_json::json!({"allow": ["read_x"], "tools": {"allow": ["*"]}}),
        serde_json::json!({"enforcement": {"unconstrained_tools": "deny"}}),
        serde_json::json!({"enforcement": {"unconstrained_tools": "warn"}}),
        serde_json::json!({"enforcement": {"unconstrained_tools": "allow"}}),
        serde_json::json!({"version": "2.0"}),
        serde_json::json!({
            "enforcement": {"unconstrained_tools": "warn"},
            "schemas": {"read_file": {"type": "object", "required": ["path"]}}
        }),
        serde_json::json!({"version": "2.0", "schemas": {"$defs": {"p": {"type": "string"}}}}),
    ];
    // Load defers to a more specific execution-time diagnosis. Each of these was
    // a live disagreement found in review.
    let load_defers = [
        serde_json::json!({"tools": {"redact_args": ["password"]}}),
        serde_json::json!({"tools": {"deny": ["fs_write"], "redact_args": ["password"]}}),
        serde_json::json!({"allow": ["read_x", 1]}),
        serde_json::json!({"tools": {"deny": ["x", 1]}}),
        serde_json::json!({"version": "2.0", "allow": "read_x", "deny": ["y"]}),
        serde_json::json!({"version": "2.0", "schemas": "nope"}),
        serde_json::json!({"version": "2.0", "schemas": []}),
        serde_json::json!({"limits": {}}),
    ];

    let loads = |schema: &serde_json::Value| {
        let expected = Expected::ArgsValid {
            schema: Some(schema.clone()),
            policy: None,
        };
        crate::model::validation::vacuous_expected_field(&expected).is_none()
    };

    for schema in fully_understood.iter().chain(load_defers.iter()) {
        let runs = crate::model::validate_args_policy_value(schema).is_ok();
        assert!(
            !runs || loads(schema),
            "load rejects a policy execution accepts: {schema}"
        );
    }
    for schema in &fully_understood {
        let runs = crate::model::validate_args_policy_value(schema).is_ok();
        assert_eq!(
            loads(schema),
            runs,
            "oracles disagree for {schema}: load={}, execution={runs}",
            loads(schema)
        );
    }
    for schema in &load_defers {
        assert!(
            loads(schema),
            "load must defer so execution's specific diagnosis survives: {schema}"
        );
        assert!(
            crate::model::validate_args_policy_value(schema).is_err(),
            "case is mislabelled as deferred: {schema}"
        );
    }
}

/// #1951: the validation layer, the vacuity rule, and the runtime metric reason about the SAME
/// prepared map. A shared-$defs `$ref` shape validates (it used to fail as unresolvable because
/// validation compiled tool schemas verbatim), and a collision is a loud validation error rather
/// than a config that reads as effective and then validates nothing at runtime.
#[test]
fn tool_output_valid_shared_defs_validate_and_collisions_are_loud() {
    let supported = crate::model::Expected::ToolOutputValid {
        schemas: Some(serde_json::json!({
            "$defs": {"NonEmpty": {"type": "string", "minLength": 1}},
            "exec": {"$ref": "#/$defs/NonEmpty"}
        })),
    };
    crate::model::validation::validate_expected_for_execution(&supported)
        .expect("shared $defs with a resolvable ref is a supported, effective config");

    let colliding = crate::model::Expected::ToolOutputValid {
        schemas: Some(serde_json::json!({
            "$defs": {"X": {"type": "string"}},
            "exec": {"$defs": {"X": {}}}
        })),
    };
    let err = crate::model::validation::validate_expected_for_execution(&colliding)
        .expect_err("a $defs collision is a preparation failure, not an effective schema");
    assert!(err.to_string().contains("overlap"), "{err:#}");

    let non_mapping = crate::model::Expected::ToolOutputValid {
        schemas: Some(serde_json::json!({
            "$defs": ["not", "a", "mapping"],
            "exec": {"type": "object"}
        })),
    };
    let err = crate::model::validation::validate_expected_for_execution(&non_mapping)
        .expect_err("a non-mapping $defs is a preparation failure");
    assert!(err.to_string().contains("mapping"), "{err:#}");
}

/// A map whose only entry is `$defs` still asserts nothing after preparation: the merge consumes
/// the entry and leaves no tool schema behind, so the vacuity diagnosis fires, not a compile error.
#[test]
fn tool_output_valid_defs_only_map_is_vacuous() {
    let defs_only = crate::model::Expected::ToolOutputValid {
        schemas: Some(serde_json::json!({
            "$defs": {"NonEmpty": {"type": "string", "minLength": 1}}
        })),
    };
    let err = crate::model::validation::validate_expected_for_execution(&defs_only)
        .expect_err("definitions without any tool schema assert nothing");
    assert!(err.to_string().contains("nothing"), "{err:#}");
}