yang-base 0.3.0

个人使用构建后端服务器
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
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
use super::*;
use crate::action::{ActionContext, TypedHandler};
use crate::error::BaseError;
use crate::tools::{Tools, ToolsBuilder};
use async_trait::async_trait;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

#[derive(Debug, Deserialize, JsonSchema)]
struct NoopInput {}

#[derive(Debug, Serialize, JsonSchema)]
struct NoopOutput {}

#[derive(Debug, Deserialize, JsonSchema)]
struct EchoInput {
    value: i64,
}

#[derive(Debug, Serialize, JsonSchema, PartialEq, Eq)]
struct EchoOutput {
    value: i64,
}

crate::params! {
    #[deny_unknown_fields]
pub CreateUserInput {
        username: Str::new().require(true).max_length(64),
        status: Radio::<i8>::new().require(true).options([(1, "启用"), (0, "禁用")]),
        note: Text::new(),
    }
}

crate::params! {
    #[deny_unknown_fields]
    MultiSourceInput {
        title: Str::new().require(true),
        #[param(source = query)]
        page: Int::new().require(true),
        #[param(source = path)]
        user_id: Key::new().require(true),
        #[param(source = header)]
        x_trace_id: Str::new().require(true),
    }
}

crate::params! {
    #[deny_unknown_fields]
    RadioQueryInput {
        #[param(source = query)]
        status: Radio::<String>::new().require(true).options([("active", "启用")]),
        #[param(source = query)]
        level: Radio::<i8>::new().require(true).options([(1, "启用"), (0, "禁用")]),
    }
}

/// 含二进制文件字段的上传输入;`format: binary` 是构建期媒体类型校验的判据。
#[derive(Debug, Deserialize, JsonSchema)]
#[serde(deny_unknown_fields)]
struct MultipartUploadInput {
    status: i8,
    file: crate::action::UploadedFile,
}

impl ParamInput for MultipartUploadInput {
    fn params() -> Params {
        Params::new()
    }
}

#[derive(crate::Action)]
#[action(
    name = "create_user",
    display_name = "创建用户",
    method = "POST",
    path = "/native/users",
    success_status = 201,
    public
)]
struct CreateUserAction;

#[async_trait]
impl crate::action::Action for CreateUserAction {
    type Input = CreateUserInput;
    type Output = EchoOutput;

    async fn index(
        &self,
        _ctx: ActionContext,
        input: Self::Input,
    ) -> Result<Self::Output, BaseError> {
        Ok(EchoOutput {
            value: i64::from(input.status),
        })
    }
}

#[derive(crate::Action)]
#[action(name = "linked_create", path = "/native/users/linked")]
struct LinkedCreateAction {
    link: ActionLink<CreateUserInput, EchoOutput>,
}

#[async_trait]
impl crate::action::Action for LinkedCreateAction {
    type Input = CreateUserInput;
    type Output = EchoOutput;

    fn calls(&self) -> Vec<ActionRef> {
        vec![self.link.reference().clone()]
    }

    fn bind_registry(&self, registry: &Registry) -> Result<(), BaseError> {
        self.link.bind(registry)
    }

    async fn index(
        &self,
        ctx: ActionContext,
        input: Self::Input,
    ) -> Result<Self::Output, BaseError> {
        ctx.plugins()?.api_run(self.link.handle()?, input).await
    }
}

#[derive(crate::Action)]
#[action(name = "noop", public)]
struct NoopAction;

#[derive(crate::Action)]
#[action(
    name = "upload",
    request_media = "multipart",
    content_types("application/pdf"),
    max_files = 1,
    max_file_bytes = 1024,
    max_total_bytes = 131072
)]
struct MultipartNoopAction;

#[derive(crate::Action)]
#[action(name = "echo", public)]
struct EchoAction;

#[async_trait]
impl TypedHandler for NoopAction {
    type Input = NoopInput;
    type Output = NoopOutput;

    async fn handle(
        &self,
        _ctx: ActionContext,
        _input: Self::Input,
    ) -> Result<Self::Output, BaseError> {
        Ok(NoopOutput {})
    }
}

#[async_trait]
impl crate::action::Action for MultipartNoopAction {
    type Input = MultipartUploadInput;
    type Output = EchoOutput;

    async fn index(
        &self,
        _ctx: ActionContext,
        input: Self::Input,
    ) -> Result<Self::Output, BaseError> {
        Ok(EchoOutput {
            value: i64::from(input.status) + i64::try_from(input.file.size()).unwrap_or_default(),
        })
    }
}

#[derive(crate::Action)]
#[action(name = "json_upload", path = "/native/upload/json")]
struct JsonUploadAction;

#[async_trait]
impl crate::action::Action for JsonUploadAction {
    type Input = MultipartUploadInput;
    type Output = EchoOutput;

    async fn index(
        &self,
        _ctx: ActionContext,
        input: Self::Input,
    ) -> Result<Self::Output, BaseError> {
        Ok(EchoOutput {
            value: i64::from(input.status) + i64::try_from(input.file.size()).unwrap_or_default(),
        })
    }
}

#[async_trait]
impl TypedHandler for EchoAction {
    type Input = EchoInput;
    type Output = EchoOutput;

    async fn handle(
        &self,
        _ctx: ActionContext,
        input: Self::Input,
    ) -> Result<Self::Output, BaseError> {
        Ok(EchoOutput { value: input.value })
    }
}

/// 符合关系选项契约的 select Action 夹具:输入/输出即稳定 DTO 对,
/// 经 `Action::Input` 解码时自动带上 `RelationOptionsRequest::validate` 边界校验。
#[derive(crate::Action)]
#[action(name = "select", public)]
struct RelationOptionsAction;

#[async_trait]
impl crate::action::Action for RelationOptionsAction {
    type Input = crate::table::RelationOptionsRequest;
    type Output = crate::table::RelationOptionsResponse;

    async fn index(
        &self,
        _ctx: ActionContext,
        input: Self::Input,
    ) -> Result<Self::Output, BaseError> {
        Ok(crate::table::RelationOptionsResponse {
            items: Vec::new(),
            page: input.page,
            limit: input.limit,
            total: Some(0),
        })
    }
}

fn test_tools() -> Tools {
    ToolsBuilder::new().build().expect("测试 Tools 应构建成功")
}

fn addon(value: &str) -> AddonName {
    AddonName::new(value).expect("测试 Addon 名称应有效")
}

fn module(value: &str) -> ModuleName {
    ModuleName::new(value).expect("测试 Module 名称应有效")
}

fn action(value: &str) -> ActionName {
    ActionName::new(value).expect("测试 Action 名称应有效")
}

#[test]
fn radio_builder_declares_filter_and_sort_access() {
    let fields = crate::fields! {
        status => Radio::<String>::new()
            .options([("active", "启用")])
            .filterable(true)
            .sortable(true),
    };
    let status = &fields.as_slice()[0];
    assert!(status.access.filterable);
    assert!(status.access.sortable);
}

#[cfg(feature = "mysql")]
#[tokio::test]
async fn radio_can_keep_varchar_storage_without_losing_value_validation() {
    use crate::table::FieldType;
    use std::collections::HashMap;
    use std::sync::Arc;

    let spec = TableSpec::new(table("users")).fields(crate::fields! {
        id => Key::new(),
        status => Radio::<String>::new()
            .varchar(16)
            .require(true)
            .options([("active", "启用"), ("disabled", "停用")]),
    });
    let definition = spec
        .table_definition()
        .unwrap_or_else(|error| panic!("VARCHAR Radio 表定义应有效: {error}"));
    assert!(matches!(
        definition
            .field("status")
            .unwrap_or_else(|| panic!("应存在 status 字段"))
            .field_type(),
        FieldType::String { max_length: 16 }
    ));

    let pool = sqlx::mysql::MySqlPoolOptions::new()
        .connect_lazy("mysql://root:test@127.0.0.1:3306/test")
        .unwrap_or_else(|error| panic!("测试连接配置应有效: {error}"));
    let query = definition
        .bind(Arc::new(pool))
        .query(std::iter::empty::<&str>());
    let valid = HashMap::from([("status".to_string(), serde_json::json!("active"))]);
    assert!(query.validate_update_data(&valid).is_ok());
    let invalid = HashMap::from([("status".to_string(), serde_json::json!("pending"))]);
    assert!(matches!(
        query.validate_update_data(&invalid),
        Err(BaseError::InvalidEnumValue(field, value)) if field == "status" && value == "pending"
    ));
}

#[test]
fn varchar_radio_rejects_empty_duplicate_and_oversized_candidates() {
    let empty = TableSpec::new(table("empty_radio")).fields(crate::fields! {
        id => Key::new(),
        status => Radio::<String>::new().varchar(8).options([] as [(&str, &str); 0]),
    });
    assert!(empty.table_definition().is_err());

    let duplicate = TableSpec::new(table("duplicate_radio")).fields(crate::fields! {
        id => Key::new(),
        status => Radio::<String>::new()
            .varchar(8)
            .options([("active", "启用"), ("active", "重复")]),
    });
    assert!(duplicate.table_definition().is_err());

    let oversized = TableSpec::new(table("oversized_radio")).fields(crate::fields! {
        id => Key::new(),
        status => Radio::<String>::new().varchar(4).options([("active", "启用")]),
    });
    assert!(oversized.table_definition().is_err());
}

fn table(value: &str) -> TableName {
    TableName::new(value).expect("测试 Table 名称应有效")
}

fn field(value: &str) -> FieldName {
    FieldName::new(value).expect("测试 Field 名称应有效")
}

fn view(value: &str) -> ViewName {
    ViewName::new(value).expect("测试 View 名称应有效")
}

fn action_ref(module_name: &str, action_name: &str) -> ActionRef {
    ActionRef::new(module(module_name), action(action_name))
}

fn field_ref(table_name: &str, field_name: &str) -> FieldRef {
    FieldRef::new(table(table_name), field(field_name))
}

#[test]
fn fields_macro_builds_the_native_field_collection() {
    let fields = crate::fields! {
        id => Key::new().title("ID"),
        username => Str::new().require(true).max_length(64),
        amount => Decimal::new().precision(18, 2),
    };
    assert_eq!(fields.as_slice().len(), 3);
    assert_eq!(fields.as_slice()[1].name.as_str(), "username");
    assert_eq!(fields.as_slice()[2].kind, FieldKind::Decimal);
}

#[test]
fn string_email_semantics_project_to_fields_and_action_params() {
    let fields = crate::fields! {
        email => Str::new().require(true).email(),
    };
    let params = Params::new().param(
        field("email"),
        ParamSource::Body,
        Str::new().require(true).email(),
    );

    let expected = Some(r"^[A-Za-z0-9._%+\-]+@[A-Za-z0-9.\-]+\.[A-Za-z]{2,}$".to_string());
    assert_eq!(fields.as_slice()[0].validation.pattern, expected);
    assert_eq!(params.as_slice()[0].validation.pattern, expected);
    assert_eq!(
        fields.as_slice()[0].presentation.widget,
        Some(WidgetHint::Email)
    );
    assert_eq!(
        params.as_slice()[0].presentation.widget,
        Some(WidgetHint::Email)
    );
}

#[test]
fn params_macro_generates_typed_input_and_native_params() {
    let input: CreateUserInput = serde_json::from_value(serde_json::json!({
        "username": "alice",
        "status": 1
    }))
    .expect("强类型参数应只反序列化一次");
    assert_eq!(input.username, "alice");
    assert_eq!(input.status, 1);
    assert!(input.note.is_none());
    let params = <CreateUserInput as ParamInput>::params();
    assert_eq!(params.as_slice().len(), 3);
    assert!(params.as_slice()[0].required);
    assert_eq!(params.as_slice()[2].source, ParamSource::Body);
    assert_eq!(
        <CreateUserAction as crate::action::Action>::params()
            .as_slice()
            .len(),
        3
    );
}

#[test]
fn name_and_reference_macros_build_controlled_native_types() {
    assert_eq!(crate::addon!("account").as_str(), "account");
    assert_eq!(crate::module!("account.user").as_str(), "account.user");
    assert_eq!(crate::table!("users").as_str(), "users");
    assert_eq!(crate::field!("users.id").to_string(), "users.id");
    assert_eq!(
        crate::action!("account.user.login").to_string(),
        "account.user.login"
    );
    assert_eq!(
        crate::view!("account.user.default").to_string(),
        "account.user.default"
    );
    const KEY: crate::action::ContextKey<i64> = crate::context_key!("tenant_id");
    assert_eq!(KEY.name(), "tenant_id");
}

#[test]
fn params_macro_decodes_body_query_path_and_header_from_one_definition() {
    let mut request = crate::action::Request::new(serde_json::json!({"title": "hello"}))
        .query("page", "3")
        .path_param("user_id", "42")
        .header("X-Trace-Id", "trace-1");

    let input = <MultiSourceInput as ParamInput>::decode(&mut request)
        .expect("四种来源应按 ParamSpec 合并为强类型 Input");
    assert_eq!(input.title, "hello");
    assert_eq!(input.page, 3);
    assert_eq!(input.user_id, 42);
    assert_eq!(input.x_trace_id, "trace-1");

    let params = <MultiSourceInput as ParamInput>::params();
    assert_eq!(params.as_slice()[0].source, ParamSource::Body);
    assert_eq!(params.as_slice()[1].source, ParamSource::Query);
    assert_eq!(params.as_slice()[2].source, ParamSource::Path);
    assert_eq!(params.as_slice()[3].source, ParamSource::Header);
}

#[test]
fn params_macro_decodes_radio_string_and_numeric_from_query() {
    // Radio::<String> 的 query 值未经 JSON 引号,必须走字符串兜底;Radio::<i8> 走 JSON 解析。
    let mut request = crate::action::Request::new(serde_json::json!({}))
        .query("status", "active")
        .query("level", "5");

    let input =
        <RadioQueryInput as ParamInput>::decode(&mut request).expect("Radio query 值应成功解码");
    assert_eq!(input.status, "active".to_string());
    assert_eq!(input.level, 5);

    // 非法数字 query 值必须报错(i8 不能解析)
    let mut bad = crate::action::Request::new(serde_json::json!({})).query("level", "abc");
    assert!(matches!(
        <RadioQueryInput as ParamInput>::decode(&mut bad),
        Err(BaseError::ParamInvalid(_, _))
    ));
}

fn get_action(name: &str, path: &str, operation_id: &str) -> ActionSpec {
    ActionSpec::new(
        action(name),
        RouteSpec::new(HttpMethod::Get, path, operation_id),
    )
    .public(true)
}

struct NativeUserModule;

impl Module for NativeUserModule {
    fn name(&self) -> ModuleName {
        module("native.user")
    }

    fn table(&self) -> Option<TableName> {
        Some(table("native_user"))
    }

    fn fields(&self) -> Fields {
        crate::fields! {
            id => Key::new(),
            username => Str::new().require(true).max_length(64),
        }
    }

    fn actions(&self) -> Actions {
        crate::actions![CreateUserAction]
    }
}

struct NativeAddon;

impl Addon for NativeAddon {
    fn name(&self) -> AddonName {
        addon("native")
    }

    fn modules(&self) -> Modules {
        crate::modules![NativeUserModule]
    }
}

#[test]
fn addon_and_module_traits_feed_the_only_app_builder_path() {
    let app = AppBuilder::new()
        .addon(NativeAddon)
        .build(test_tools())
        .expect("原生 Addon/Module 应直接构建");
    assert_eq!(app.catalog().addons()[0].name.as_str(), "native");
    assert_eq!(app.table_definitions()[0].name(), "native_user");
    assert_eq!(app.compiled_views()[0].name().as_str(), "default");
    let action = &app.catalog().addons()[0].modules[0].actions()[0];
    assert_eq!(action.route.path, "/native/users");
    assert_eq!(action.success_status, 201);
    assert_eq!(action.params.len(), 3);
}

#[test]
fn native_multipart_contract_reaches_catalog_and_ui_projection() {
    let app = AppBuilder::new()
        .addon(
            AddonSpec::new(addon("upload"))
                .module(ModuleSpec::new(module("upload.file")).native_action(MultipartNoopAction)),
        )
        .build(test_tools())
        .expect("合法 multipart Action 应构建成功");
    let action = &app.catalog().addons()[0].modules[0].actions()[0];
    assert_eq!(action.request_media_type, ActionMediaType::Multipart);
    let multipart = action.multipart.as_ref().expect("Catalog 应保留上传限制");
    assert_eq!(multipart.max_files, 1);
    assert_eq!(multipart.max_file_bytes, 1024);
    assert_eq!(multipart.max_total_bytes, 131072);
    assert_eq!(
        multipart.max_text_field_bytes,
        DEFAULT_MULTIPART_MAX_TEXT_FIELD_BYTES
    );
    assert_eq!(multipart.allowed_content_types, ["application/pdf"]);

    let catalog = app
        .ui_catalog(
            &app.context(crate::action::Request::new(serde_json::Value::Null))
                .with_user(crate::action::User::new(1, "uploader")),
        )
        .expect("multipart UI Catalog revision 应可计算");
    assert_eq!(
        catalog.actions[0].request_media_type,
        ActionMediaType::Multipart
    );
    assert_eq!(
        catalog.actions[0]
            .multipart
            .as_ref()
            .map(|spec| spec.lifecycle),
        Some(UploadLifecycle::RequestScoped)
    );
}

#[test]
fn app_builder_rejects_unsafe_or_inconsistent_multipart_contracts() {
    let build = |action: ActionSpec| {
        AppBuilder::new()
            .addon(
                AddonSpec::new(addon("upload"))
                    .module(ModuleSpec::new(module("upload.file")).action(action, NoopAction)),
            )
            .build(test_tools())
    };
    let post = || {
        ActionSpec::new(
            action("upload"),
            RouteSpec::new(HttpMethod::Post, "/upload", "upload.file.upload"),
        )
    };
    let valid = || MultipartSpec::new(["application/pdf"]);

    let mut json_with_limits = post();
    json_with_limits.multipart = Some(valid());
    let mut missing_limits = post();
    missing_limits.request_media_type = ActionMediaType::Multipart;
    let get_upload = ActionSpec::new(
        action("upload"),
        RouteSpec::new(HttpMethod::Get, "/upload", "upload.file.upload"),
    )
    .multipart(valid());
    let zero_files = post().multipart(valid().max_files(0));
    let oversized_file = post().multipart(valid().max_file_bytes(4096).max_total_bytes(2048));
    let wildcard = post().multipart(MultipartSpec::new(["image/*"]));
    let duplicate = post().multipart(MultipartSpec::new(["image/png", "image/png"]));

    for (name, spec) in [
        ("json_with_limits", json_with_limits),
        ("missing_limits", missing_limits),
        ("get_upload", get_upload),
        ("zero_files", zero_files),
        ("oversized_file", oversized_file),
        ("wildcard", wildcard),
        ("duplicate", duplicate),
    ] {
        let error = match build(spec) {
            Ok(_) => panic!("{name} 必须在启动期失败"),
            Err(error) => error,
        };
        assert!(
            matches!(
                error,
                BuildError::InvalidReference {
                    kind: "Action request media",
                    ..
                }
            ),
            "{name} 返回了错误类型: {error}"
        );
    }
}

#[test]
fn app_builder_rejects_incoherent_text_field_limits() {
    // 文本字段上限的构建期校验必须以其自身原因拒绝(而非被后续校验顺带拦截):
    // 手工 ActionSpec 的 input_schema 无文件字段,若上限校验缺失,构建会落到
    // “必须至少声明一个文件字段” 的原因上,断言 reference 可区分两者。
    let build = |multipart: MultipartSpec| {
        let spec = ActionSpec::new(
            action("upload"),
            RouteSpec::new(HttpMethod::Post, "/upload", "upload.file.upload"),
        )
        .multipart(multipart);
        AppBuilder::new()
            .addon(
                AddonSpec::new(addon("upload"))
                    .module(ModuleSpec::new(module("upload.file")).action(spec, NoopAction)),
            )
            .build(test_tools())
    };
    let cases: [(&str, MultipartSpec); 2] = [
        (
            "文本字段字节上限必须大于 0",
            MultipartSpec::new(["application/pdf"]).max_text_field_bytes(0),
        ),
        (
            "max_text_field_bytes 不能大于 max_total_bytes",
            MultipartSpec::new(["application/pdf"])
                .max_file_bytes(1024)
                .max_text_field_bytes(4096)
                .max_total_bytes(2048),
        ),
    ];
    for (reason, multipart) in cases {
        let error = match build(multipart) {
            Ok(_) => panic!("{reason} 必须在启动期失败"),
            Err(error) => error,
        };
        assert!(
            matches!(
                &error,
                BuildError::InvalidReference {
                    kind: "Action request media",
                    reference,
                } if reference.contains(reason)
            ),
            "拒绝原因必须是 {reason}: {error}"
        );
    }
}

#[test]
fn app_builder_rejects_json_action_with_binary_input_field() {
    // C-1:input_schema 含二进制文件字段(format: binary)的 Action 必须声明 multipart,
    // 否则客户端可经 JSON 通道伪造 UploadedFile 实例。
    let result = AppBuilder::new()
        .addon(
            AddonSpec::new(addon("upload"))
                .module(ModuleSpec::new(module("upload.file")).native_action(JsonUploadAction)),
        )
        .build(test_tools());
    let error = match result {
        Ok(_) => panic!("JSON Action 声明二进制文件字段必须在构建期失败"),
        Err(error) => error,
    };
    assert!(
        matches!(
            error,
            BuildError::InvalidReference {
                kind: "Action request media",
                ..
            }
        ),
        "返回了错误类型: {error}"
    );
}

#[test]
fn app_builder_rejects_multipart_action_without_binary_input_field() {
    // C-1 反向:multipart Action 的输入必须至少声明一个文件字段,
    // 防止"声明了上传通道却没有文件落点"的错误契约进入 Catalog。
    let spec = ActionSpec::new(
        action("upload"),
        RouteSpec::new(HttpMethod::Post, "/upload", "upload.file.upload"),
    )
    .multipart(MultipartSpec::new(["application/pdf"]));
    let result = AppBuilder::new()
        .addon(
            AddonSpec::new(addon("upload"))
                .module(ModuleSpec::new(module("upload.file")).action(spec, NoopAction)),
        )
        .build(test_tools());
    let error = match result {
        Ok(_) => panic!("multipart Action 缺少文件字段必须在构建期失败"),
        Err(error) => error,
    };
    assert!(
        matches!(
            error,
            BuildError::InvalidReference {
                kind: "Action request media",
                ..
            }
        ),
        "返回了错误类型: {error}"
    );
}

#[test]
fn binary_schema_scanner_resolves_local_refs_and_plain_fields() {
    // C-1 复审:传输层文本 part 拦截依赖同一扫描器;$ref/anyOf/items 必须递归解析。
    #[allow(dead_code)] // 字段仅为 schema 形状存在,不参与运行期读取
    #[derive(Debug, Deserialize, JsonSchema)]
    #[serde(deny_unknown_fields)]
    struct Bundle {
        note: String,
        file: crate::action::UploadedFile,
    }

    #[allow(dead_code)] // 字段仅为 schema 形状存在,不参与运行期读取
    #[derive(Debug, Deserialize, JsonSchema)]
    #[serde(deny_unknown_fields)]
    struct ScannerInput {
        title: String,
        files: Vec<crate::action::UploadedFile>,
        bundles: Vec<Bundle>,
        maybe_file: Option<crate::action::UploadedFile>,
    }

    let document = serde_json::to_value(schemars::schema_for!(ScannerInput))
        .expect("扫描器测试 schema 应可序列化");
    let properties = document.get("properties").expect("schema 应有 properties");
    let contains = |name: &str| {
        builder::schema_subtree_contains_binary(
            &document,
            properties.get(name).expect("字段应存在于 properties"),
        )
    };
    assert!(
        contains("files"),
        "Vec<UploadedFile> 的 items $ref 应解析出 binary"
    );
    assert!(
        contains("bundles"),
        "嵌套包装结构体的 $ref 链应解析出 binary"
    );
    assert!(
        contains("maybe_file"),
        "Option<UploadedFile> 的 anyOf $ref 应解析出 binary"
    );
    assert!(!contains("title"), "普通字符串字段不得误判为 binary");
    assert!(
        super::builder::schema_contains_binary_field(&document),
        "构建期整文档扫描应检测到 binary"
    );

    // 自引用类型不得死循环(循环保护)。
    #[allow(dead_code)] // 字段仅为 schema 形状存在,不参与运行期读取
    #[derive(Debug, Deserialize, JsonSchema)]
    #[serde(deny_unknown_fields)]
    struct Recursive {
        next: Option<Box<Recursive>>,
    }
    let recursive =
        serde_json::to_value(schemars::schema_for!(Recursive)).expect("自引用 schema 应可序列化");
    assert!(!super::builder::schema_contains_binary_field(&recursive));
}

#[tokio::test]
async fn action_link_is_bound_once_and_internal_call_has_no_json_roundtrip() {
    let target = action_ref("native.user", "create_user");
    let link = ActionLink::<CreateUserInput, EchoOutput>::new(target);
    let module = ModuleSpec::new(module("native.user"))
        .native_action(CreateUserAction)
        .native_action(LinkedCreateAction { link: link.clone() });
    let app = AppBuilder::new()
        .addon(AddonSpec::new(addon("native")).module(module))
        .build(test_tools())
        .expect("ActionLink 应在 AppBuilder 构建期绑定");

    let input = CreateUserInput {
        username: "alice".to_string(),
        status: 1,
        note: None,
    };
    let output = app
        .context(crate::action::Request::new(serde_json::Value::Null))
        .plugins()
        .expect("BuiltApp context 应绑定 Registry")
        .api_run(link.handle().expect("Link 应已解析为 slot"), input)
        .await
        .expect("内部调用应直接传递 Rust 值");
    assert_eq!(output, EchoOutput { value: 1 });
}

fn org_app() -> AppBuilder {
    let org_select = get_action("select", "/orgs/select", "org.org.select");
    let user_add = ActionSpec::new(
        action("add"),
        RouteSpec::new(HttpMethod::Post, "/org/users", "org.user.add"),
    )
    .public(true)
    .param(
        ParamSpec::new(field("username"), ParamSource::Body)
            .from_field(field_ref("org_user", "username"))
            .required(true),
    )
    .calls(action_ref("org.org", "select"));

    let org_module = ModuleSpec::new(module("org.org"))
        .table(
            TableSpec::new(table("org_org"))
                .field(FieldSpec::new(field("id"), FieldKind::Key))
                .field(FieldSpec::new(field("name"), FieldKind::Str).required(true)),
        )
        .action(org_select, RelationOptionsAction);
    let user_module = ModuleSpec::new(module("org.user"))
        .table(
            TableSpec::new(table("org_user"))
                .field(FieldSpec::new(field("id"), FieldKind::Key))
                .field(FieldSpec::new(field("username"), FieldKind::Str).required(true))
                .field(
                    FieldSpec::new(field("org_org"), FieldKind::Table)
                        .relation(field_ref("org_org", "id"))
                        .select(action_ref("org.org", "select"))
                        .tenant_key(true),
                ),
        )
        .action(user_add, NoopAction)
        .view(
            ViewSpec::new(view("list"))
                .field(field_ref("org_user", "username"))
                .action(action_ref("org.user", "add")),
        );

    AppBuilder::new().addon(
        AddonSpec::new(addon("org"))
            .module(user_module)
            .module(org_module),
    )
}

#[test]
fn build_resolves_action_slots_and_sorts_catalog() {
    let app = org_app().build(test_tools()).expect("完整定义应构建成功");
    let addons = app.catalog().addons();
    assert_eq!(addons.len(), 1);
    assert_eq!(addons[0].modules[0].name.as_str(), "org.org");
    assert_eq!(addons[0].modules[1].name.as_str(), "org.user");
    assert_eq!(app.registry().len(), 2);
    assert_eq!(app.compiled_views().len(), 2);
    assert_eq!(app.compiled_views()[0].module().as_str(), "org.org");
    assert_eq!(app.compiled_views()[0].name().as_str(), "default");
    assert_eq!(app.compiled_views()[0].fields()[0].as_str(), "org_org.id");
    assert_eq!(app.compiled_views()[1].module().as_str(), "org.user");
    assert_eq!(app.compiled_views()[1].name().as_str(), "list");
    assert_eq!(app.compiled_views()[1].actions()[0].slot(), 1);
    assert_eq!(
        app.registry()
            .resolve(&action_ref("org.org", "select"))
            .expect("引用应在构建期解析")
            .slot(),
        0
    );
    assert_eq!(
        app.registry()
            .resolve(&action_ref("org.user", "add"))
            .expect("引用应在构建期解析")
            .slot(),
        1
    );
}

#[tokio::test]
async fn registry_executes_the_handler_bound_by_the_same_definition() {
    let app = org_app().build(test_tools()).expect("完整定义应构建成功");
    let handle = app
        .registry()
        .resolve(&action_ref("org.user", "add"))
        .expect("ActionRef 应在构建期解析");

    let response = app
        .dispatch(handle, crate::action::Request::new(serde_json::json!({})))
        .await
        .expect("预绑定 Handler 应执行成功");

    assert_eq!(response.code, 0);
    assert_eq!(app.catalog().addons()[0].modules[1].actions().len(), 1);
    assert_eq!(app.registry().len(), 2);
}

#[tokio::test]
async fn plugins_calls_typed_action_without_json_round_trip() {
    let reference = action_ref("org.echo", "echo");
    let app = AppBuilder::new()
        .addon(
            AddonSpec::new(addon("org")).module(
                ModuleSpec::new(module("org.echo"))
                    .action(get_action("echo", "/echo", "org.echo.echo"), EchoAction),
            ),
        )
        .build(test_tools())
        .expect("应用定义应构建成功");
    let handle = app
        .registry()
        .resolve_typed::<EchoInput, EchoOutput>(&reference)
        .expect("强类型签名应在启动期匹配");
    let output = app
        .context(crate::action::Request::new(serde_json::Value::Null))
        .plugins()
        .expect("BuiltApp 上下文应绑定 Registry")
        .api_run(handle, EchoInput { value: 42 })
        .await
        .expect("内部调用应成功");

    assert_eq!(output, EchoOutput { value: 42 });
}

#[test]
fn typed_action_handle_rejects_mismatched_signature_at_startup() {
    let reference = action_ref("org.echo", "echo");
    let app = AppBuilder::new()
        .addon(
            AddonSpec::new(addon("org")).module(
                ModuleSpec::new(module("org.echo"))
                    .action(get_action("echo", "/echo", "org.echo.echo"), EchoAction),
            ),
        )
        .build(test_tools())
        .expect("应用定义应构建成功");

    assert!(app
        .registry()
        .resolve_typed::<NoopInput, NoopOutput>(&reference)
        .is_err());
}

#[test]
fn catalog_is_independent_of_addon_registration_order() {
    let alpha = AddonSpec::new(addon("alpha")).module(
        ModuleSpec::new(module("alpha.main"))
            .action(get_action("list", "/alpha", "alpha.main.list"), NoopAction),
    );
    let beta = AddonSpec::new(addon("beta")).module(
        ModuleSpec::new(module("beta.main"))
            .action(get_action("list", "/beta", "beta.main.list"), NoopAction),
    );
    let left = AppBuilder::new()
        .addon(beta.clone())
        .addon(alpha.clone())
        .build(test_tools())
        .expect("定义应构建成功");
    let right = AppBuilder::new()
        .addon(alpha)
        .addon(beta)
        .build(test_tools())
        .expect("定义应构建成功");
    assert_eq!(left.catalog(), right.catalog());
}

#[test]
fn duplicate_names_fail_during_build() {
    let duplicate_addon = AppBuilder::new()
        .addon(AddonSpec::new(addon("org")))
        .addon(AddonSpec::new(addon("org")))
        .build(test_tools());
    assert!(matches!(
        duplicate_addon,
        Err(BuildError::DuplicateName { kind: "Addon", .. })
    ));

    let duplicate_action = AppBuilder::new()
        .addon(
            AddonSpec::new(addon("org")).module(
                ModuleSpec::new(module("org.user"))
                    .action(get_action("list", "/users", "org.user.list"), NoopAction)
                    .action(
                        get_action("list", "/users/again", "org.user.list_again"),
                        NoopAction,
                    ),
            ),
        )
        .build(test_tools());
    assert!(matches!(
        duplicate_action,
        Err(BuildError::DuplicateName { kind: "Action", .. })
    ));
}

#[test]
fn missing_dependency_and_wrong_module_owner_fail() {
    let missing = AppBuilder::new()
        .addon(AddonSpec::new(addon("org")).depends_on(addon("user")))
        .build(test_tools());
    assert!(matches!(missing, Err(BuildError::DependencyMissing { .. })));

    let wrong_owner = AppBuilder::new()
        .addon(AddonSpec::new(addon("org")).module(ModuleSpec::new(module("user.account"))))
        .build(test_tools());
    assert!(matches!(
        wrong_owner,
        Err(BuildError::InvalidReference {
            kind: "Module owner",
            ..
        })
    ));
}

#[test]
fn invalid_field_and_action_references_fail() {
    let invalid_field = AppBuilder::new()
        .addon(
            AddonSpec::new(addon("org")).module(
                ModuleSpec::new(module("org.user")).table(
                    TableSpec::new(table("org_user")).field(
                        FieldSpec::new(field("org_org"), FieldKind::Table)
                            .relation(field_ref("missing_table", "id")),
                    ),
                ),
            ),
        )
        .build(test_tools());
    assert!(matches!(
        invalid_field,
        Err(BuildError::InvalidReference { kind: "Field", .. })
    ));

    let invalid_action = AppBuilder::new()
        .addon(
            AddonSpec::new(addon("org")).module(
                ModuleSpec::new(module("org.user")).action(
                    get_action("add", "/users", "org.user.add")
                        .calls(action_ref("org.user", "missing")),
                    NoopAction,
                ),
            ),
        )
        .build(test_tools());
    assert!(matches!(
        invalid_action,
        Err(BuildError::InvalidReference { kind: "Action", .. })
    ));
}

#[test]
fn relation_options_action_must_implement_the_contract_dto_pair() {
    // I-4:UI 目录承诺 select Action 讲 RelationOptionsRequest/RelationOptionsResponse,
    // 输入/输出签名不符的 Action 接入关系选择器必须在构建期拒绝。
    let module = ModuleSpec::new(module("org.org"))
        .table(
            TableSpec::new(table("org_org"))
                .field(FieldSpec::new(field("id"), FieldKind::Key))
                .field(
                    FieldSpec::new(field("parent_id"), FieldKind::Table)
                        .relation(field_ref("org_org", "id"))
                        .select(action_ref("org.org", "select")),
                ),
        )
        .action(
            get_action("select", "/orgs/select", "org.org.select"),
            NoopAction,
        );
    let error = AppBuilder::new()
        .addon(AddonSpec::new(addon("org")).module(module))
        .build(test_tools())
        .expect_err("签名不符的 select Action 必须在构建期拒绝");
    assert!(
        matches!(
            &error,
            BuildError::InvalidReference {
                kind: "Relation Options Action",
                reference,
            } if reference.contains("RelationOptionsRequest")
        ),
        "拒绝原因必须指向契约类型: {error}"
    );
}

#[tokio::test]
async fn dispatch_select_action_enforces_permission_beyond_catalog_hiding() {
    // I-4:UI 目录按身份隐藏无权限的 select Action 只是投影层过滤;直接 dispatch
    // 必须由授权层拦截。已认证但缺少权限的用户必须得到 PermissionDenied,
    // 而不是未认证场景的 Unauthorized。
    let module = ModuleSpec::new(module("org.org"))
        .table(
            TableSpec::new(table("org_org"))
                .field(FieldSpec::new(field("id"), FieldKind::Key))
                .field(
                    FieldSpec::new(field("parent_id"), FieldKind::Table)
                        .relation(field_ref("org_org", "id"))
                        .select(action_ref("org.org", "select")),
                ),
        )
        .action(
            get_action("select", "/orgs/select", "org.org.select")
                .public(false)
                .permissions(["org.select"], crate::action::PermissionMode::All),
            RelationOptionsAction,
        );
    let app = AppBuilder::new()
        .addon(AddonSpec::new(addon("org")).module(module))
        .build(test_tools())
        .expect("契约合规的 select Action 应构建成功");
    let handle = app
        .registry()
        .resolve(&action_ref("org.org", "select"))
        .expect("select Action 应在构建期解析");

    let context = app
        .context(crate::action::Request::new(serde_json::json!({})))
        .with_user(crate::action::User::new(7, "outsider"));
    let error = app
        .dispatch_context(handle, context)
        .await
        .expect_err("无权限用户直接调用 select Action 必须被拒绝");
    assert!(
        matches!(error, BaseError::PermissionDenied(_)),
        "缺少权限必须返回 PermissionDenied: {error}"
    );

    // 对照:持有权限的用户通过授权并完成派发(同时走通 decode+validate 默认路径)。
    let context = app
        .context(crate::action::Request::new(serde_json::json!({})))
        .with_user(crate::action::User::new(8, "insider").with_permissions(["org.select"]));
    app.dispatch_context(handle, context)
        .await
        .expect("持有权限的用户应可调用 select Action");
}

#[test]
fn route_conflicts_fail_but_different_methods_share_exact_path() {
    let conflict = AppBuilder::new()
        .addon(
            AddonSpec::new(addon("org")).module(
                ModuleSpec::new(module("org.user"))
                    .action(get_action("get", "/users/{id}", "org.user.get"), NoopAction)
                    .action(
                        get_action("find", "/users/{user_id}", "org.user.find"),
                        NoopAction,
                    ),
            ),
        )
        .build(test_tools());
    assert!(matches!(conflict, Err(BuildError::RouteConflict { .. })));

    let shared_path = AppBuilder::new()
        .addon(
            AddonSpec::new(addon("org")).module(
                ModuleSpec::new(module("org.user"))
                    .action(get_action("list", "/users", "org.user.list"), NoopAction)
                    .action(
                        ActionSpec::new(
                            action("add"),
                            RouteSpec::new(HttpMethod::Post, "/users", "org.user.add"),
                        ),
                        NoopAction,
                    ),
            ),
        )
        .build(test_tools());
    assert!(shared_path.is_ok());
}

#[test]
fn action_route_cannot_shadow_framework_health_endpoints() {
    let result = AppBuilder::new()
        .addon(
            AddonSpec::new(addon("ops")).module(ModuleSpec::new(module("ops.health")).action(
                get_action("live", "/health/live", "ops.health.live"),
                NoopAction,
            )),
        )
        .build(test_tools());
    assert!(
        matches!(result, Err(BuildError::RouteConflict { .. })),
        "与框架 /health/live 冲突必须在构建期报 RouteConflict: {result:?}"
    );

    let ready = AppBuilder::new()
        .addon(
            AddonSpec::new(addon("ops")).module(ModuleSpec::new(module("ops.ready")).action(
                get_action("ready", "/health/ready", "ops.health.ready"),
                NoopAction,
            )),
        )
        .build(test_tools());
    assert!(
        matches!(ready, Err(BuildError::RouteConflict { .. })),
        "与框架 /health/ready 冲突必须在构建期报 RouteConflict: {ready:?}"
    );

    // 相同 method+path 之外不得误伤:其他 method 或子路径仍可正常声明。
    let allowed = AppBuilder::new()
        .addon(
            AddonSpec::new(addon("ops")).module(
                ModuleSpec::new(module("ops.health"))
                    .action(
                        get_action("detail", "/health/live/detail", "ops.health.detail"),
                        NoopAction,
                    )
                    .action(
                        ActionSpec::new(
                            action("probe"),
                            RouteSpec::new(HttpMethod::Post, "/health/live", "ops.health.probe"),
                        ),
                        NoopAction,
                    ),
            ),
        )
        .build(test_tools());
    assert!(
        allowed.is_ok(),
        "非同一 method 或非保留路径不应被拒绝: {:?}",
        allowed.err()
    );
}

#[test]
fn field_shape_and_path_param_are_validated() {
    let missing_relation = AppBuilder::new()
        .addon(
            AddonSpec::new(addon("org")).module(
                ModuleSpec::new(module("org.user")).table(
                    TableSpec::new(table("org_user"))
                        .field(FieldSpec::new(field("org_org"), FieldKind::Table)),
                ),
            ),
        )
        .build(test_tools());
    assert!(matches!(
        missing_relation,
        Err(BuildError::InvalidFieldDefinition { .. })
    ));

    let missing_path = AppBuilder::new()
        .addon(
            AddonSpec::new(addon("org")).module(
                ModuleSpec::new(module("org.user")).action(
                    get_action("get", "/users", "org.user.get")
                        .param(ParamSpec::new(field("id"), ParamSource::Path)),
                    NoopAction,
                ),
            ),
        )
        .build(test_tools());
    assert!(matches!(
        missing_path,
        Err(BuildError::InvalidReference {
            kind: "Path param",
            ..
        })
    ));
}

#[cfg(feature = "mysql")]
#[test]
fn module_crud_registers_six_actions_and_one_compiled_table() {
    let module = ModuleSpec::new(module("org.post"))
        .table(
            TableSpec::new(table("org_post"))
                .field(FieldSpec::new(field("id"), FieldKind::Key))
                .field(FieldSpec::new(field("title"), FieldKind::Str).required(true)),
        )
        .crud()
        .expect("合法主表应注册标准 CRUD");
    let app = AppBuilder::new()
        .addon(AddonSpec::new(addon("org")).module(module))
        .build(test_tools())
        .expect("CRUD 应用应构建成功");

    assert_eq!(app.registry().len(), 6);
    assert_eq!(app.table_definitions().len(), 1);
    assert_eq!(app.table_definitions()[0].name(), "org_post");
    for name in ["add", "put", "del", "get", "select", "table"] {
        assert!(app
            .registry()
            .resolve(&action_ref("org.post", name))
            .is_some());
    }
}

#[cfg(feature = "openapi")]
#[test]
fn definition_catalog_projects_typed_openapi_contract() {
    let action = ActionSpec::new(
        action("echo"),
        RouteSpec::new(HttpMethod::Post, "/echo/{id}", "org.echo.echo"),
    )
    .public(true)
    .param(ParamSpec::new(field("id"), ParamSource::Path).required(true));
    let app = AppBuilder::new()
        .addon(
            AddonSpec::new(addon("org"))
                .module(ModuleSpec::new(module("org.echo")).action(action, EchoAction)),
        )
        .build(test_tools())
        .expect("OpenAPI 样板应构建成功");
    let document = app
        .catalog()
        .to_openapi(OpenApiInfo::new("YANG", "0.3"))
        .expect("Catalog 应生成 OpenAPI");

    let operation = &document["paths"]["/echo/{id}"]["post"];
    assert_eq!(operation["operationId"], "org.echo.echo");
    assert_eq!(operation["parameters"][0]["in"], "path");
    assert_eq!(operation["x-public"], true);
    assert!(
        operation["responses"]["200"]["content"]["application/json"]["schema"]["properties"]
            ["data"]
            .is_object()
    );
}

#[cfg(feature = "openapi")]
#[test]
fn openapi_projects_multipart_content_type_and_resource_limits() {
    let app = AppBuilder::new()
        .addon(
            AddonSpec::new(addon("upload"))
                .module(ModuleSpec::new(module("upload.file")).native_action(MultipartNoopAction)),
        )
        .build(test_tools())
        .expect("multipart OpenAPI 测试应用应构建成功");
    let document = app
        .catalog()
        .to_openapi(OpenApiInfo::new("YANG", "0.3"))
        .expect("Catalog 应生成 multipart OpenAPI");
    let media =
        &document["paths"]["/upload"]["post"]["requestBody"]["content"]["multipart/form-data"];

    assert!(media["schema"].is_object());
    assert_eq!(media["x-yang-multipart"]["max_files"], 1);
    assert_eq!(media["x-yang-multipart"]["max_file_bytes"], 1024);
    assert_eq!(
        media["x-yang-multipart"]["max_text_field_bytes"],
        DEFAULT_MULTIPART_MAX_TEXT_FIELD_BYTES
    );
    assert_eq!(
        media["x-yang-multipart"]["allowed_content_types"][0],
        "application/pdf"
    );
    assert_eq!(
        media["x-yang-multipart"]["lifecycle"],
        serde_json::to_value(UploadLifecycle::RequestScoped).expect("上传生命周期应可序列化"),
        "OpenAPI 扩展必须序列化 spec.lifecycle 真实值而不是硬编码"
    );
}

#[cfg(feature = "openapi")]
#[test]
fn openapi_projects_response_kind_specific_success_contract() {
    let spec = |name: &str, path: &str, operation_id: &str, kind: ActionResponseKind| {
        ActionSpec::new(
            action(name),
            RouteSpec::new(HttpMethod::Get, path, operation_id),
        )
        .public(true)
        .response_kind(kind)
    };
    let app = AppBuilder::new()
        .addon(
            AddonSpec::new(addon("res")).module(
                ModuleSpec::new(module("res.kind"))
                    .action(
                        spec(
                            "json",
                            "/res/json",
                            "res.kind.json",
                            ActionResponseKind::Json,
                        ),
                        NoopAction,
                    )
                    .action(
                        spec(
                            "download",
                            "/res/download",
                            "res.kind.download",
                            ActionResponseKind::Download,
                        ),
                        NoopAction,
                    )
                    .action(
                        spec(
                            "preview",
                            "/res/preview",
                            "res.kind.preview",
                            ActionResponseKind::Preview,
                        ),
                        NoopAction,
                    )
                    .action(
                        spec(
                            "redirect",
                            "/res/redirect",
                            "res.kind.redirect",
                            ActionResponseKind::Redirect,
                        ),
                        NoopAction,
                    ),
            ),
        )
        .build(test_tools())
        .expect("response_kind OpenAPI 测试应用应构建成功");
    let document = app
        .catalog()
        .to_openapi(OpenApiInfo::new("YANG", "0.3"))
        .expect("Catalog 应生成 OpenAPI");

    let json_op = &document["paths"]["/res/json"]["get"];
    assert_eq!(json_op["x-yang-response-kind"], "json");
    assert!(
        json_op["responses"]["200"]["content"]["application/json"]["schema"]["properties"]["data"]
            .is_object(),
        "JSON Action 保持信封契约: {json_op}"
    );

    let download_op = &document["paths"]["/res/download"]["get"];
    assert_eq!(download_op["x-yang-response-kind"], "download");
    assert_eq!(
        download_op["responses"]["200"]["content"]["application/octet-stream"]["schema"],
        serde_json::json!({"type": "string", "format": "binary"}),
        "下载成功响应必须是二进制流而不是 JSON 信封: {download_op}"
    );
    assert!(
        download_op["responses"]["200"]["content"]
            .get("application/json")
            .is_none(),
        "下载 Action 不得声明 JSON 信封: {download_op}"
    );

    let preview_op = &document["paths"]["/res/preview"]["get"];
    assert_eq!(preview_op["x-yang-response-kind"], "preview");
    assert!(
        preview_op["responses"]["200"]["content"]["application/octet-stream"].is_object(),
        "预览成功响应必须是二进制流: {preview_op}"
    );

    let redirect_op = &document["paths"]["/res/redirect"]["get"];
    assert_eq!(redirect_op["x-yang-response-kind"], "redirect");
    assert!(
        redirect_op["responses"]["302"].is_object(),
        "重定向必须声明 3xx 成功响应: {redirect_op}"
    );
    assert!(
        redirect_op["responses"]["302"].get("content").is_none(),
        "重定向成功响应不得携带 content: {redirect_op}"
    );
    assert!(
        redirect_op["responses"]["302"]["headers"]["Location"].is_object(),
        "重定向必须声明 Location 响应头: {redirect_op}"
    );
    assert!(
        redirect_op["responses"].get("200").is_none(),
        "重定向不得声明 200 JSON 信封: {redirect_op}"
    );
}

/// searchable / filterable / sortable 在声明层是三个独立开关,`into_schema_field` 必须按位
/// 精确写入 table 层定义,不得折叠成单一 filterable 位,也不得继承 table 层的
/// 宽松默认值(fail-closed)。
#[test]
fn query_capability_bits_map_independently_to_table_definition() {
    let with_access = |name: &str, searchable: bool, filterable: bool, sortable: bool| {
        let mut field = FieldSpec::new(field(name), FieldKind::Str);
        field.access.searchable = searchable;
        field.access.filterable = filterable;
        field.access.sortable = sortable;
        field
    };
    let spec = TableSpec::new(table("org_doc"))
        .field(FieldSpec::new(field("id"), FieldKind::Key))
        .field(with_access("all", true, true, true))
        .field(with_access("search_only", true, false, false))
        .field(with_access("filter_only", false, true, false))
        .field(with_access("sort_only", false, false, true))
        .field(with_access("neither", false, false, false));
    let definition = spec.table_definition().expect("字段位映射表定义应有效");

    let all = definition.field("all").expect("all 字段应存在");
    assert!(all.is_searchable());
    assert!(all.is_filterable());
    assert!(all.is_sortable());

    let search_only = definition
        .field("search_only")
        .expect("search_only 字段应存在");
    assert!(search_only.is_searchable());
    assert!(
        !search_only.is_filterable(),
        "searchable 不得连带开放结构化筛选"
    );
    assert!(!search_only.is_sortable());

    let filter_only = definition
        .field("filter_only")
        .expect("filter_only 字段应存在");
    assert!(
        !filter_only.is_searchable(),
        "filterable 不得连带开放关键词搜索"
    );
    assert!(filter_only.is_filterable());
    assert!(!filter_only.is_sortable());

    let sort_only = definition.field("sort_only").expect("sort_only 字段应存在");
    assert!(!sort_only.is_searchable());
    assert!(!sort_only.is_filterable());
    assert!(sort_only.is_sortable());

    let neither = definition.field("neither").expect("neither 字段应存在");
    assert!(!neither.is_searchable());
    assert!(
        !neither.is_filterable(),
        "未声明 filterable 的字段必须 fail-closed"
    );
    assert!(
        !neither.is_sortable(),
        "未声明 sortable 的字段必须 fail-closed"
    );
}

#[cfg(feature = "mysql")]
#[test]
fn module_crud_at_uses_explicit_versioned_path_and_stable_operation_ids() {
    let spec = ModuleSpec::new(module("org.user"))
        .table(TableSpec::new(table("org_user")).field(FieldSpec::new(field("id"), FieldKind::Key)))
        .crud_at("/api/v1/org/users")
        .expect("版本化 CRUD 路径应有效");

    let action = |name: &str| {
        spec.actions()
            .iter()
            .find(|action| action.name.as_str() == name)
            .unwrap_or_else(|| panic!("应存在 org.user.{name}"))
    };
    assert_eq!(action("add").route.path, "/api/v1/org/users");
    assert_eq!(action("select").route.path, "/api/v1/org/users/query");
    assert_eq!(action("table").route.path, "/api/v1/org/users/schema");
    for name in ["add", "put", "del", "get", "select", "table"] {
        assert_eq!(action(name).route.operation_id, format!("org.user.{name}"));
    }

    let invalid = ModuleSpec::new(module("org.user"))
        .table(TableSpec::new(table("org_user")).field(FieldSpec::new(field("id"), FieldKind::Key)))
        .crud_at("api/v1/org/users");
    assert!(matches!(invalid, Err(BaseError::ConfigError(_))));
}

#[cfg(feature = "mysql")]
#[test]
fn module_crud_at_with_mutations_preserves_framework_contracts() {
    use crate::action::builtin::{AddAction, DelAction, PutAction};

    let spec = ModuleSpec::new(module("org.user"))
        .table(TableSpec::new(table("org_user")).field(FieldSpec::new(field("id"), FieldKind::Key)))
        .crud_at_with_mutations(
            "/api/v1/org/users",
            AddAction::new(),
            PutAction::new(),
            DelAction::new(),
        )
        .expect("自定义 writer 应复用框架 CRUD 契约");

    for name in ["add", "put", "del", "get", "select", "table"] {
        let action = spec
            .actions()
            .iter()
            .find(|action| action.name.as_str() == name)
            .unwrap_or_else(|| panic!("应存在 org.user.{name}"));
        assert!(
            !action.input_schema.is_null(),
            "{name} 应保留表驱动输入契约"
        );
        assert!(
            !action.output_schema.is_null(),
            "{name} 应保留表驱动输出契约"
        );
        assert_eq!(action.route.operation_id, format!("org.user.{name}"));
    }
}

#[test]
fn table_spec_projects_validated_composite_indexes() {
    let spec = TableSpec::new(table("org_user"))
        .field(FieldSpec::new(field("id"), FieldKind::Key))
        .field(FieldSpec::new(field("org_org"), FieldKind::Int))
        .field(FieldSpec::new(field("user_user"), FieldKind::Int))
        .unique_named(
            "uk_org_user_membership",
            [
                field_ref("org_user", "org_org"),
                field_ref("org_user", "user_user"),
            ],
        );
    let definition = spec.table_definition().expect("复合索引应编译成功");
    let index = &definition.shared_config().unique_indexes[0];

    assert_eq!(index.fields, ["org_org", "user_user"]);
    assert_eq!(index.name.as_deref(), Some("uk_org_user_membership"));

    let cross_table = TableSpec::new(table("org_user"))
        .field(FieldSpec::new(field("org_org"), FieldKind::Int))
        .unique([field_ref("org_org", "id")]);
    assert!(matches!(
        cross_table.table_definition(),
        Err(BaseError::ConfigError(message)) if message.contains("其他表字段")
    ));
}

/// 非文本字段(Str/Text 以外的 FieldKind)声明 searchable 必须在构建期报错:
/// 服务端搜索本就会跳过非文本字段,允许声明等于让 UI 契约说谎(fail-closed)。
#[test]
fn non_text_searchable_field_is_rejected_at_build_time() {
    let build_with = |kind: FieldKind| {
        let mut spec = FieldSpec::new(field("target"), kind);
        spec.access.searchable = true;
        AppBuilder::new()
            .addon(
                AddonSpec::new(addon("org")).module(
                    ModuleSpec::new(module("org.user")).table(
                        TableSpec::new(table("org_user"))
                            .field(FieldSpec::new(field("id"), FieldKind::Key))
                            .field(spec),
                    ),
                ),
            )
            .build(test_tools())
    };

    for kind in [FieldKind::Int, FieldKind::Timestamp, FieldKind::Switch] {
        assert!(
            matches!(
                build_with(kind),
                Err(BuildError::InvalidFieldDefinition { .. })
            ),
            "非文本字段 {kind:?} 声明 searchable 必须构建失败"
        );
    }
    for kind in [FieldKind::Str, FieldKind::Text] {
        assert!(
            build_with(kind).is_ok(),
            "文本字段 {kind:?} 声明 searchable 应构建成功"
        );
    }
}

/// TreeViewSpec 的 max_nodes 在启动期解析进 CompiledTreeView:缺省回退到服务端
/// 默认常量,显式配置覆盖;配置 0 在构建期拒绝(fail-closed)。
#[test]
fn tree_view_max_nodes_defaults_overrides_and_validates() {
    let field_ref = |name: &str| FieldRef::new(table("org_node"), field(name));
    let build = |tree: TreeViewSpec| {
        let module = ModuleSpec::new(module("org.node"))
            .table(
                TableSpec::new(table("org_node"))
                    .field(FieldSpec::new(field("id"), FieldKind::Key))
                    .field(FieldSpec::new(field("parent_id"), FieldKind::Int))
                    .field(FieldSpec::new(field("name"), FieldKind::Str)),
            )
            .view(
                ViewSpec::new(ViewName::new("main").expect("测试 View 名称应有效"))
                    .field(field_ref("id"))
                    .field(field_ref("parent_id"))
                    .field(field_ref("name"))
                    .tree(tree),
            );
        AppBuilder::new()
            .addon(AddonSpec::new(addon("org")).module(module))
            .build(test_tools())
    };
    let tree_spec =
        || TreeViewSpec::new(field_ref("id"), field_ref("parent_id"), field_ref("name"));

    let app = build(tree_spec()).expect("默认树 View 应构建成功");
    let tree = app.compiled_views()[0].tree().expect("树拓扑应已编译");
    assert_eq!(
        tree.max_nodes(),
        crate::table::DEFAULT_TREE_MAX_NODES,
        "缺省 max_nodes 必须回退到服务端默认常量"
    );

    let app = build(tree_spec().max_nodes(42)).expect("覆盖树节点上限应构建成功");
    let tree = app.compiled_views()[0].tree().expect("树拓扑应已编译");
    assert_eq!(tree.max_nodes(), 42);

    let rejected = build(tree_spec().max_nodes(0));
    assert!(
        matches!(
            rejected,
            Err(BuildError::InvalidReference {
                kind: "Tree View",
                ..
            })
        ),
        "max_nodes 为 0 必须构建期拒绝: {:?}",
        rejected.err()
    );

    // H7:硬天花板本身可接受,超过即拒绝,避免 View 把树查询 LIMIT 抬成无界。
    build(tree_spec().max_nodes(crate::table::DEFAULT_TREE_MAX_NODES))
        .expect("max_nodes 等于硬天花板应构建成功");

    let rejected = build(tree_spec().max_nodes(crate::table::DEFAULT_TREE_MAX_NODES + 1));
    assert!(
        matches!(
            rejected,
            Err(BuildError::InvalidReference {
                kind: "Tree View",
                ..
            })
        ),
        "max_nodes 超过硬天花板必须构建期拒绝: {:?}",
        rejected.err()
    );
}

/// ValidationSpec.minimum/maximum 必须映射为 table 层服务端强制边界(Min/Max validator),
/// 而不是仅停留在 UI 预校验提示;Decimal 字段携带的十进制字符串由 Min/Max 数值提取兜底。
#[test]
fn validation_min_max_map_to_server_enforced_bounds_on_decimal_write() {
    let mut score = FieldSpec::new(field("score"), FieldKind::Decimal);
    score.validation.minimum = Some("0".into());
    score.validation.maximum = Some("100".into());

    let spec = TableSpec::new(table("scores"))
        .field(FieldSpec::new(field("id"), FieldKind::Key))
        .field(score);
    let definition = spec
        .table_definition()
        .expect("Decimal 数值边界声明应编译成功");
    let config = definition.shared_config();
    let score = config.get_field("score").expect("score 字段应存在");
    let cache = crate::table::RegexCache::default();

    // 字符串形态的越界值必须被 Min 验证器拒绝(Decimal 合法携带十进制字符串)。
    assert!(matches!(
        score.validate(&serde_json::json!("-1"), &cache),
        Err(BaseError::ValidationFailed(_, _))
    ));
    assert!(matches!(
        score.validate(&serde_json::json!("-0.5"), &cache),
        Err(BaseError::ValidationFailed(_, _))
    ));
    // 字符串形态的合规值必须通过,防止 Decimal 被 Min/Max 误伤。
    assert!(score.validate(&serde_json::json!("10.5"), &cache).is_ok());
    assert!(score.validate(&serde_json::json!("100"), &cache).is_ok());
    // 数值形态仍走同一 validator。
    assert!(matches!(
        score.validate(&serde_json::json!(101), &cache),
        Err(BaseError::ValidationFailed(_, _))
    ));
}

/// 数值边界声明不合法时必须在构建期 fail-closed:非数值字段声明 min/max 报 ConfigError,
/// min > max 报"数值下限不能大于上限"。
#[test]
fn validation_min_max_reject_incompatible_field_and_reversed_bounds() {
    let mut name = FieldSpec::new(field("name"), FieldKind::Str);
    name.validation.minimum = Some("0".into());
    let incompatible = TableSpec::new(table("rejected"))
        .field(FieldSpec::new(field("id"), FieldKind::Key))
        .field(name);
    assert!(
        matches!(
            incompatible.table_definition(),
            Err(BaseError::ConfigError(_))
        ),
        "Str 字段声明 minimum 必须在构建期报 ConfigError"
    );

    let mut reversed = FieldSpec::new(field("score"), FieldKind::Decimal);
    reversed.validation.minimum = Some("10".into());
    reversed.validation.maximum = Some("1".into());
    let reversed_range = TableSpec::new(table("reversed"))
        .field(FieldSpec::new(field("id"), FieldKind::Key))
        .field(reversed);
    assert!(matches!(
        reversed_range.table_definition(),
        Err(BaseError::ConfigError(message)) if message.contains("数值下限不能大于上限")
    ));
}