sz-orm-core 7.6.0

Core ORM engine: Model trait, ActiveRecord, QueryBuilder, Pool, Transaction, migration, and SQL dialect abstraction
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
//! API 覆盖率审计工具。
//!
//! 提供 sz-orm-core 公共 API 与各绑定层暴露方法的覆盖率审计功能。
//!
//! ## 核心概念
//!
//! - [`CoreApiRegistry`]:核心 API 方法注册表,包含 pool/query/transaction 等模块的全部公共方法
//! - [`BindingApiSnapshot`]:绑定层 API 快照,记录某绑定层当前暴露的方法(已映射到 core 命名空间)
//! - [`ApiCoverageReport`]:覆盖率审计报告,包含覆盖率百分比、已覆盖/未覆盖方法清单
//!
//! ## 使用示例
//!
//! ```rust,ignore
//! use sz_orm_core::api_coverage::*;
//!
//! let registry = CoreApiRegistry::new();
//! let snapshot = BindingApiSnapshot::python();
//! let report = audit_coverage(&registry, &snapshot);
//! println!("Python 覆盖率: {:.1}%", report.coverage_percent);
//! ```

use std::collections::BTreeSet;

// ────────────────────────────────────────────────────────────────────────────
//  枚举定义
// ────────────────────────────────────────────────────────────────────────────

/// Core API 方法所属模块。
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub enum ApiModule {
    /// 连接池
    Pool,
    /// 查询构造器
    Query,
    /// 事务管理
    Transaction,
    /// 模型 trait
    Model,
    /// 活跃模型
    ActiveModel,
    /// 仓储
    Repository,
    /// 钩子
    Hooks,
    /// 观察者
    Observer,
    /// 分页器
    Paginator,
    /// 迁移
    Migration,
}

impl ApiModule {
    /// 返回模块的字符串名称。
    pub fn as_str(&self) -> &'static str {
        match self {
            Self::Pool => "pool",
            Self::Query => "query",
            Self::Transaction => "transaction",
            Self::Model => "model",
            Self::ActiveModel => "active_model",
            Self::Repository => "repository",
            Self::Hooks => "hooks",
            Self::Observer => "observer",
            Self::Paginator => "paginator",
            Self::Migration => "migration",
        }
    }
}

/// 绑定层名称。
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum BindingLayer {
    /// Python 绑定 (PyO3)
    Python,
    /// Java 绑定 (JNI)
    Java,
    /// Go 绑定 (CGO)
    Go,
    /// C++ 绑定 (FFI)
    Cpp,
    /// Node.js 绑定 (napi-rs)
    NodeJs,
    /// WASM 绑定
    Wasm,
    /// C ABI 统一 FFI 层
    CAbi,
}

impl BindingLayer {
    /// 返回绑定层的字符串名称。
    pub fn as_str(&self) -> &'static str {
        match self {
            Self::Python => "Python",
            Self::Java => "Java",
            Self::Go => "Go",
            Self::Cpp => "C++",
            Self::NodeJs => "Node.js",
            Self::Wasm => "WASM",
            Self::CAbi => "C ABI",
        }
    }
}

// ────────────────────────────────────────────────────────────────────────────
//  核心数据结构
// ────────────────────────────────────────────────────────────────────────────

/// Core API 方法描述。
#[derive(Debug, Clone)]
pub struct CoreApiMethod {
    /// 方法所属模块
    pub module: ApiModule,
    /// 方法名
    pub name: &'static str,
    /// 是否异步
    pub is_async: bool,
}

/// API 覆盖率报告。
#[derive(Debug, Clone)]
pub struct ApiCoverageReport {
    /// 绑定层名称
    pub binding_layer_name: String,
    /// 覆盖率百分比 (0.0 ~ 100.0)
    pub coverage_percent: f64,
    /// 已覆盖的方法列表(格式:`module::method`)
    pub covered_methods: Vec<String>,
    /// 未覆盖的方法列表(格式:`module::method`)
    pub uncovered_methods: Vec<String>,
    /// 核心方法总数
    pub total_methods: usize,
    /// 已覆盖方法数
    pub covered_count: usize,
}

impl ApiCoverageReport {
    /// 返回未覆盖方法数。
    pub fn uncovered_count(&self) -> usize {
        self.total_methods - self.covered_count
    }

    /// 是否达到目标覆盖率。
    pub fn meets_target(&self, target_percent: f64) -> bool {
        self.coverage_percent >= target_percent
    }
}

// ────────────────────────────────────────────────────────────────────────────
//  Core API 注册表
// ────────────────────────────────────────────────────────────────────────────

/// Core API 注册表,持有全部核心公共方法。
pub struct CoreApiRegistry {
    methods: Vec<CoreApiMethod>,
}

impl CoreApiRegistry {
    /// 创建包含所有核心 API 的注册表。
    pub fn new() -> Self {
        Self {
            methods: core_api_methods(),
        }
    }

    /// 返回所有方法名(格式:`module::method`)。
    pub fn method_names(&self) -> Vec<String> {
        self.methods
            .iter()
            .map(|m| format!("{}::{}", m.module.as_str(), m.name))
            .collect()
    }

    /// 返回方法总数。
    pub fn len(&self) -> usize {
        self.methods.len()
    }

    /// 是否为空。
    pub fn is_empty(&self) -> bool {
        self.methods.is_empty()
    }

    /// 按模块获取方法引用列表。
    pub fn methods_by_module(&self, module: ApiModule) -> Vec<&CoreApiMethod> {
        self.methods.iter().filter(|m| m.module == module).collect()
    }

    /// 按模块统计方法数。
    pub fn count_by_module(&self) -> Vec<(ApiModule, usize)> {
        let mut counts = Vec::new();
        for module in [
            ApiModule::Pool,
            ApiModule::Query,
            ApiModule::Transaction,
            ApiModule::Model,
            ApiModule::ActiveModel,
            ApiModule::Repository,
            ApiModule::Hooks,
            ApiModule::Observer,
            ApiModule::Paginator,
            ApiModule::Migration,
        ] {
            let count = self.methods.iter().filter(|m| m.module == module).count();
            counts.push((module, count));
        }
        counts
    }
}

impl Default for CoreApiRegistry {
    fn default() -> Self {
        Self::new()
    }
}

// ────────────────────────────────────────────────────────────────────────────
//  绑定层 API 快照
// ────────────────────────────────────────────────────────────────────────────

/// 绑定层 API 快照,记录某绑定层当前暴露的方法。
#[derive(Debug, Clone)]
pub struct BindingApiSnapshot {
    /// 绑定层
    pub layer: BindingLayer,
    /// 暴露的方法名集合(格式:`module::method`,已映射到 core 命名空间)
    pub exposed_methods: BTreeSet<String>,
}

impl BindingApiSnapshot {
    /// 创建空的绑定层快照。
    pub fn new(layer: BindingLayer) -> Self {
        Self {
            layer,
            exposed_methods: BTreeSet::new(),
        }
    }

    /// 从方法名列表创建快照。
    pub fn from_methods(layer: BindingLayer, methods: &[&str]) -> Self {
        Self {
            layer,
            exposed_methods: methods.iter().map(|s| s.to_string()).collect(),
        }
    }

    /// 添加暴露的方法。
    pub fn with_method(mut self, method: &str) -> Self {
        self.exposed_methods.insert(method.to_string());
        self
    }

    /// Python 绑定层当前 API 快照。
    pub fn python() -> Self {
        Self::from_methods(
            BindingLayer::Python,
            &[
                "pool::new",
                "pool::acquire",
                "pool::release",
                "pool::status",
                "pool::close_all",
                "query::new",
                "query::table",
                "query::select",
                "query::where_eq",
                "query::where_ne",
                "query::where_gt",
                "query::where_lt",
                "query::where_like",
                "query::where_in",
                "query::where_between",
                "query::where_null",
                "query::where_not_null",
                "query::order_by",
                "query::order_desc",
                "query::limit",
                "query::offset",
                "query::page",
                "query::join_inner",
                "query::join_left",
                "query::build_select",
                "query::build_insert",
                "query::build_update",
                "query::build_delete",
                "query::build_count",
                "transaction::new",
                "transaction::commit",
                "transaction::rollback",
                "transaction::execute",
                "transaction::query",
                "model::table_name",
                "model::pk",
                "model::set_pk",
                "active_model::from_model",
                "active_model::set",
                "active_model::get",
                "active_model::changed_fields",
                "repository::new",
                "hooks::new",
                "hooks::register",
                "hooks::dispatch",
                "hooks::clear",
                "hooks::count",
                "observer::new",
                "observer::subscribe",
                "observer::dispatch",
            ],
        )
    }

    /// Java 绑定层当前 API 快照。
    pub fn java() -> Self {
        Self::from_methods(
            BindingLayer::Java,
            &[
                "pool::new",
                "pool::acquire",
                "pool::release",
                "pool::status",
                "pool::close_all",
                "query::new",
                "query::table",
                "query::where_eq",
                "query::order_by",
                "query::limit",
                "query::build_select",
                "query::build_insert",
                "query::build_update",
                "query::build_delete",
                "transaction::new",
                "transaction::commit",
                "transaction::rollback",
                "transaction::execute",
                "model::table_name",
                "model::pk",
                "model::set_pk",
                "active_model::set",
                "active_model::get",
                "active_model::save",
            ],
        )
    }

    /// Go 绑定层当前 API 快照。
    pub fn go() -> Self {
        Self::from_methods(
            BindingLayer::Go,
            &[
                "pool::new",
                "pool::acquire",
                "pool::release",
                "pool::status",
                "pool::close_all",
                "query::new",
                "query::table",
                "query::where_eq",
                "query::order_by",
                "query::limit",
                "query::build_select",
                "query::build_insert",
                "query::build_update",
                "query::build_delete",
                "transaction::new",
                "transaction::commit",
                "transaction::rollback",
                "transaction::execute",
                "model::table_name",
                "model::pk",
                "model::set_pk",
                "active_model::set",
                "active_model::get",
                "active_model::save",
            ],
        )
    }

    /// C++ 绑定层当前 API 快照。
    pub fn cpp() -> Self {
        Self::from_methods(
            BindingLayer::Cpp,
            &[
                "pool::new",
                "pool::acquire",
                "pool::release",
                "pool::status",
                "pool::close_all",
                "query::new",
                "query::table",
                "query::where_eq",
                "query::order_by",
                "query::limit",
                "query::build_select",
                "query::build_insert",
                "query::build_update",
                "query::build_delete",
                "transaction::new",
                "transaction::commit",
                "transaction::rollback",
                "transaction::execute",
                "model::table_name",
                "model::pk",
                "model::set_pk",
                "active_model::set",
                "active_model::get",
                "active_model::save",
            ],
        )
    }

    /// Node.js 绑定层当前 API 快照。
    pub fn nodejs() -> Self {
        Self::from_methods(
            BindingLayer::NodeJs,
            &[
                "pool::new",
                "pool::acquire",
                "pool::release",
                "pool::status",
                "pool::close_all",
                "query::new",
                "query::table",
                "query::select",
                "query::where_eq",
                "query::where_ne",
                "query::where_gt",
                "query::where_lt",
                "query::where_like",
                "query::where_in",
                "query::where_between",
                "query::where_null",
                "query::where_not_null",
                "query::order_by",
                "query::order_desc",
                "query::limit",
                "query::offset",
                "query::page",
                "query::join_inner",
                "query::join_left",
                "query::build_select",
                "query::build_insert",
                "query::build_update",
                "query::build_delete",
                "query::build_count",
                "transaction::new",
                "transaction::commit",
                "transaction::rollback",
                "transaction::execute",
                "transaction::query",
                "model::table_name",
                "model::pk",
                "model::set_pk",
                "active_model::from_model",
                "active_model::set",
                "active_model::get",
                "active_model::changed_fields",
                "active_model::into_model",
                "migration::new",
                "migration::migrate",
                "migration::up",
                "migration::down",
                "migration::rollback",
                "migration::progress",
            ],
        )
    }

    /// WASM 绑定层当前 API 快照。
    pub fn wasm() -> Self {
        Self::from_methods(
            BindingLayer::Wasm,
            &[
                "query::new",
                "query::table",
                "query::select",
                "query::where_eq",
                "query::where_like",
                "query::where_in",
                "query::order_by",
                "query::limit",
                "query::offset",
                "query::build_select",
                "query::build_insert",
                "query::build_update",
                "query::build_delete",
                "dual_env::detect_environment",
                "dual_env::verify_sandbox_isolation",
                "dual_env::build_command",
                "proxy_server::handle_http_request",
                "proxy_server::error_to_status_code",
            ],
        )
    }

    /// C ABI 统一 FFI 层当前 API 快照。
    pub fn cabi() -> Self {
        Self::from_methods(
            BindingLayer::CAbi,
            &[
                "pool::new",
                "pool::acquire",
                "pool::release",
                "pool::status",
                "pool::close_all",
                "query::build_select",
                "query::build_insert",
                "query::build_update",
                "query::build_delete",
                "query::build_count",
                "transaction::new",
                "transaction::commit",
                "transaction::rollback",
                "transaction::execute",
                "model::table_name",
                "model::pk",
                "model::set_pk",
            ],
        )
    }

    /// 返回所有绑定层的快照。
    pub fn all_layers() -> Vec<Self> {
        vec![
            Self::python(),
            Self::java(),
            Self::go(),
            Self::cpp(),
            Self::nodejs(),
            Self::wasm(),
            Self::cabi(),
        ]
    }
}

// ────────────────────────────────────────────────────────────────────────────
//  审计函数
// ────────────────────────────────────────────────────────────────────────────

/// 审计单个绑定层的 API 覆盖率。
///
/// 对比核心 API 注册表与绑定层快照,生成覆盖率报告。
pub fn audit_coverage(
    registry: &CoreApiRegistry,
    snapshot: &BindingApiSnapshot,
) -> ApiCoverageReport {
    let all_methods: Vec<String> = registry.method_names();
    let total = all_methods.len();

    let mut covered = Vec::new();
    let mut uncovered = Vec::new();

    for method in &all_methods {
        if snapshot.exposed_methods.contains(method) {
            covered.push(method.clone());
        } else {
            uncovered.push(method.clone());
        }
    }

    let covered_count = covered.len();
    let coverage_percent = if total == 0 {
        100.0
    } else {
        (covered_count as f64 / total as f64) * 100.0
    };

    ApiCoverageReport {
        binding_layer_name: snapshot.layer.as_str().to_string(),
        coverage_percent,
        covered_methods: covered,
        uncovered_methods: uncovered,
        total_methods: total,
        covered_count,
    }
}

/// 审计所有绑定层的 API 覆盖率。
///
/// 返回每个绑定层的覆盖率报告。
pub fn audit_all_layers(registry: &CoreApiRegistry) -> Vec<ApiCoverageReport> {
    BindingApiSnapshot::all_layers()
        .iter()
        .map(|snapshot| audit_coverage(registry, snapshot))
        .collect()
}

/// 生成覆盖率摘要文本报告。
///
/// 输出各绑定层的覆盖率百分比和未覆盖方法数。
pub fn format_summary(reports: &[ApiCoverageReport]) -> String {
    let mut out = String::new();
    out.push_str("=== API 覆盖率审计报告 ===\n\n");
    out.push_str(&format!(
        "{:<12} {:>8} {:>10} {:>10} {:>10}\n",
        "绑定层", "覆盖率", "已覆盖", "未覆盖", "总数"
    ));
    out.push_str(&"-".repeat(55));
    out.push('\n');

    for report in reports {
        out.push_str(&format!(
            "{:<12} {:>7.1}% {:>10} {:>10} {:>10}\n",
            report.binding_layer_name,
            report.coverage_percent,
            report.covered_count,
            report.uncovered_count(),
            report.total_methods,
        ));
    }

    out
}

/// 生成详细覆盖率报告(含未覆盖方法清单)。
///
/// 按优先级排序未覆盖方法:Pool/Query/Transaction > Model/ActiveModel > Hooks/Observer > Repository/Paginator
pub fn format_detail(report: &ApiCoverageReport) -> String {
    let mut out = String::new();
    out.push_str(&format!(
        "=== {} 绑定层 API 覆盖率详细报告 ===\n\n",
        report.binding_layer_name
    ));
    out.push_str(&format!("覆盖率: {:.1}%\n", report.coverage_percent));
    out.push_str(&format!(
        "已覆盖: {}/{}\n\n",
        report.covered_count, report.total_methods
    ));

    if !report.uncovered_methods.is_empty() {
        out.push_str("未覆盖方法(按优先级排序):\n");
        let mut sorted = report.uncovered_methods.clone();
        sorted.sort_by_key(|a| method_priority(a));
        for method in &sorted {
            out.push_str(&format!("  - {}\n", method));
        }
    }

    out
}

/// 计算方法的优先级排序权重。
///
/// Pool/Query/Transaction (0) > Model/ActiveModel (1) > Hooks/Observer (2) > Repository/Paginator (3) > Migration (4)
fn method_priority(method: &str) -> u8 {
    if method.starts_with("pool::")
        || method.starts_with("query::")
        || method.starts_with("transaction::")
    {
        0
    } else if method.starts_with("model::") || method.starts_with("active_model::") {
        1
    } else if method.starts_with("hooks::") || method.starts_with("observer::") {
        2
    } else if method.starts_with("repository::") || method.starts_with("paginator::") {
        3
    } else {
        4
    }
}

// ────────────────────────────────────────────────────────────────────────────
//  Core API 方法清单(从 core_api_baseline.txt 生成)
// ────────────────────────────────────────────────────────────────────────────

/// 返回核心 API 方法清单。
fn core_api_methods() -> Vec<CoreApiMethod> {
    vec![
        // ── Pool ──
        CoreApiMethod {
            module: ApiModule::Pool,
            name: "new",
            is_async: false,
        },
        CoreApiMethod {
            module: ApiModule::Pool,
            name: "new_async",
            is_async: true,
        },
        CoreApiMethod {
            module: ApiModule::Pool,
            name: "acquire",
            is_async: true,
        },
        CoreApiMethod {
            module: ApiModule::Pool,
            name: "release",
            is_async: true,
        },
        CoreApiMethod {
            module: ApiModule::Pool,
            name: "status",
            is_async: true,
        },
        CoreApiMethod {
            module: ApiModule::Pool,
            name: "close_all",
            is_async: true,
        },
        CoreApiMethod {
            module: ApiModule::Pool,
            name: "reap_idle",
            is_async: true,
        },
        CoreApiMethod {
            module: ApiModule::Pool,
            name: "health_check",
            is_async: true,
        },
        CoreApiMethod {
            module: ApiModule::Pool,
            name: "shutdown",
            is_async: true,
        },
        CoreApiMethod {
            module: ApiModule::Pool,
            name: "warmup",
            is_async: true,
        },
        CoreApiMethod {
            module: ApiModule::Pool,
            name: "config",
            is_async: false,
        },
        CoreApiMethod {
            module: ApiModule::Pool,
            name: "max_size",
            is_async: false,
        },
        CoreApiMethod {
            module: ApiModule::Pool,
            name: "resize",
            is_async: false,
        },
        CoreApiMethod {
            module: ApiModule::Pool,
            name: "set_max_size",
            is_async: false,
        },
        CoreApiMethod {
            module: ApiModule::Pool,
            name: "prewarm",
            is_async: true,
        },
        // ── Query ──
        CoreApiMethod {
            module: ApiModule::Query,
            name: "new",
            is_async: false,
        },
        CoreApiMethod {
            module: ApiModule::Query,
            name: "table",
            is_async: false,
        },
        CoreApiMethod {
            module: ApiModule::Query,
            name: "select",
            is_async: false,
        },
        CoreApiMethod {
            module: ApiModule::Query,
            name: "where_eq",
            is_async: false,
        },
        CoreApiMethod {
            module: ApiModule::Query,
            name: "where_ne",
            is_async: false,
        },
        CoreApiMethod {
            module: ApiModule::Query,
            name: "where_gt",
            is_async: false,
        },
        CoreApiMethod {
            module: ApiModule::Query,
            name: "where_ge",
            is_async: false,
        },
        CoreApiMethod {
            module: ApiModule::Query,
            name: "where_lt",
            is_async: false,
        },
        CoreApiMethod {
            module: ApiModule::Query,
            name: "where_le",
            is_async: false,
        },
        CoreApiMethod {
            module: ApiModule::Query,
            name: "where_like",
            is_async: false,
        },
        CoreApiMethod {
            module: ApiModule::Query,
            name: "where_in",
            is_async: false,
        },
        CoreApiMethod {
            module: ApiModule::Query,
            name: "where_not_in",
            is_async: false,
        },
        CoreApiMethod {
            module: ApiModule::Query,
            name: "where_between",
            is_async: false,
        },
        CoreApiMethod {
            module: ApiModule::Query,
            name: "where_not_between",
            is_async: false,
        },
        CoreApiMethod {
            module: ApiModule::Query,
            name: "where_null",
            is_async: false,
        },
        CoreApiMethod {
            module: ApiModule::Query,
            name: "where_not_null",
            is_async: false,
        },
        CoreApiMethod {
            module: ApiModule::Query,
            name: "or_where_eq",
            is_async: false,
        },
        CoreApiMethod {
            module: ApiModule::Query,
            name: "or_where_ne",
            is_async: false,
        },
        CoreApiMethod {
            module: ApiModule::Query,
            name: "or_where_gt",
            is_async: false,
        },
        CoreApiMethod {
            module: ApiModule::Query,
            name: "or_where_like",
            is_async: false,
        },
        CoreApiMethod {
            module: ApiModule::Query,
            name: "order_by",
            is_async: false,
        },
        CoreApiMethod {
            module: ApiModule::Query,
            name: "order_desc",
            is_async: false,
        },
        CoreApiMethod {
            module: ApiModule::Query,
            name: "group_by",
            is_async: false,
        },
        CoreApiMethod {
            module: ApiModule::Query,
            name: "having",
            is_async: false,
        },
        CoreApiMethod {
            module: ApiModule::Query,
            name: "limit",
            is_async: false,
        },
        CoreApiMethod {
            module: ApiModule::Query,
            name: "offset",
            is_async: false,
        },
        CoreApiMethod {
            module: ApiModule::Query,
            name: "page",
            is_async: false,
        },
        CoreApiMethod {
            module: ApiModule::Query,
            name: "join_inner",
            is_async: false,
        },
        CoreApiMethod {
            module: ApiModule::Query,
            name: "join_left",
            is_async: false,
        },
        CoreApiMethod {
            module: ApiModule::Query,
            name: "join_right",
            is_async: false,
        },
        CoreApiMethod {
            module: ApiModule::Query,
            name: "build_select",
            is_async: false,
        },
        CoreApiMethod {
            module: ApiModule::Query,
            name: "build_insert",
            is_async: false,
        },
        CoreApiMethod {
            module: ApiModule::Query,
            name: "build_update",
            is_async: false,
        },
        CoreApiMethod {
            module: ApiModule::Query,
            name: "build_delete",
            is_async: false,
        },
        CoreApiMethod {
            module: ApiModule::Query,
            name: "build_count",
            is_async: false,
        },
        CoreApiMethod {
            module: ApiModule::Query,
            name: "build_exists",
            is_async: false,
        },
        CoreApiMethod {
            module: ApiModule::Query,
            name: "build_max",
            is_async: false,
        },
        CoreApiMethod {
            module: ApiModule::Query,
            name: "build_min",
            is_async: false,
        },
        CoreApiMethod {
            module: ApiModule::Query,
            name: "build_sum",
            is_async: false,
        },
        CoreApiMethod {
            module: ApiModule::Query,
            name: "build_avg",
            is_async: false,
        },
        CoreApiMethod {
            module: ApiModule::Query,
            name: "validate",
            is_async: false,
        },
        CoreApiMethod {
            module: ApiModule::Query,
            name: "sql",
            is_async: false,
        },
        // ── Transaction ──
        CoreApiMethod {
            module: ApiModule::Transaction,
            name: "new",
            is_async: false,
        },
        CoreApiMethod {
            module: ApiModule::Transaction,
            name: "commit",
            is_async: true,
        },
        CoreApiMethod {
            module: ApiModule::Transaction,
            name: "rollback",
            is_async: true,
        },
        CoreApiMethod {
            module: ApiModule::Transaction,
            name: "execute",
            is_async: true,
        },
        CoreApiMethod {
            module: ApiModule::Transaction,
            name: "query",
            is_async: true,
        },
        CoreApiMethod {
            module: ApiModule::Transaction,
            name: "savepoint",
            is_async: true,
        },
        CoreApiMethod {
            module: ApiModule::Transaction,
            name: "rollback_to_savepoint",
            is_async: true,
        },
        CoreApiMethod {
            module: ApiModule::Transaction,
            name: "release_savepoint",
            is_async: true,
        },
        CoreApiMethod {
            module: ApiModule::Transaction,
            name: "state",
            is_async: false,
        },
        CoreApiMethod {
            module: ApiModule::Transaction,
            name: "is_active",
            is_async: false,
        },
        CoreApiMethod {
            module: ApiModule::Transaction,
            name: "with_isolation",
            is_async: false,
        },
        CoreApiMethod {
            module: ApiModule::Transaction,
            name: "read_only",
            is_async: false,
        },
        CoreApiMethod {
            module: ApiModule::Transaction,
            name: "with_timeout",
            is_async: false,
        },
        // ── Model ──
        CoreApiMethod {
            module: ApiModule::Model,
            name: "table_name",
            is_async: false,
        },
        CoreApiMethod {
            module: ApiModule::Model,
            name: "pk_name",
            is_async: false,
        },
        CoreApiMethod {
            module: ApiModule::Model,
            name: "pk",
            is_async: false,
        },
        CoreApiMethod {
            module: ApiModule::Model,
            name: "set_pk",
            is_async: false,
        },
        CoreApiMethod {
            module: ApiModule::Model,
            name: "foreign_key",
            is_async: false,
        },
        CoreApiMethod {
            module: ApiModule::Model,
            name: "timestamp_fields",
            is_async: false,
        },
        CoreApiMethod {
            module: ApiModule::Model,
            name: "soft_delete_field",
            is_async: false,
        },
        // ── ActiveModel ──
        CoreApiMethod {
            module: ApiModule::ActiveModel,
            name: "from_model",
            is_async: false,
        },
        CoreApiMethod {
            module: ApiModule::ActiveModel,
            name: "set",
            is_async: false,
        },
        CoreApiMethod {
            module: ApiModule::ActiveModel,
            name: "unset",
            is_async: false,
        },
        CoreApiMethod {
            module: ApiModule::ActiveModel,
            name: "get",
            is_async: false,
        },
        CoreApiMethod {
            module: ApiModule::ActiveModel,
            name: "changed_fields",
            is_async: false,
        },
        CoreApiMethod {
            module: ApiModule::ActiveModel,
            name: "into_model",
            is_async: false,
        },
        CoreApiMethod {
            module: ApiModule::ActiveModel,
            name: "as_model",
            is_async: false,
        },
        CoreApiMethod {
            module: ApiModule::ActiveModel,
            name: "save",
            is_async: true,
        },
        CoreApiMethod {
            module: ApiModule::ActiveModel,
            name: "update",
            is_async: true,
        },
        // ── Repository ──
        CoreApiMethod {
            module: ApiModule::Repository,
            name: "new",
            is_async: false,
        },
        CoreApiMethod {
            module: ApiModule::Repository,
            name: "from_vec",
            is_async: false,
        },
        CoreApiMethod {
            module: ApiModule::Repository,
            name: "len",
            is_async: false,
        },
        CoreApiMethod {
            module: ApiModule::Repository,
            name: "is_empty",
            is_async: false,
        },
        CoreApiMethod {
            module: ApiModule::Repository,
            name: "clear",
            is_async: false,
        },
        // ── Hooks ──
        CoreApiMethod {
            module: ApiModule::Hooks,
            name: "new",
            is_async: false,
        },
        CoreApiMethod {
            module: ApiModule::Hooks,
            name: "register",
            is_async: false,
        },
        CoreApiMethod {
            module: ApiModule::Hooks,
            name: "dispatch",
            is_async: false,
        },
        CoreApiMethod {
            module: ApiModule::Hooks,
            name: "clear",
            is_async: false,
        },
        CoreApiMethod {
            module: ApiModule::Hooks,
            name: "clear_all",
            is_async: false,
        },
        CoreApiMethod {
            module: ApiModule::Hooks,
            name: "count",
            is_async: false,
        },
        // ── Observer ──
        CoreApiMethod {
            module: ApiModule::Observer,
            name: "new",
            is_async: false,
        },
        CoreApiMethod {
            module: ApiModule::Observer,
            name: "add_observer",
            is_async: false,
        },
        CoreApiMethod {
            module: ApiModule::Observer,
            name: "subscribe",
            is_async: false,
        },
        CoreApiMethod {
            module: ApiModule::Observer,
            name: "dispatch",
            is_async: false,
        },
        CoreApiMethod {
            module: ApiModule::Observer,
            name: "clear",
            is_async: false,
        },
        // ── Paginator ──
        CoreApiMethod {
            module: ApiModule::Paginator,
            name: "fetch_page",
            is_async: true,
        },
        CoreApiMethod {
            module: ApiModule::Paginator,
            name: "set_total",
            is_async: false,
        },
        // ── Migration ──
        CoreApiMethod {
            module: ApiModule::Migration,
            name: "new",
            is_async: false,
        },
        CoreApiMethod {
            module: ApiModule::Migration,
            name: "add_migrations",
            is_async: false,
        },
        CoreApiMethod {
            module: ApiModule::Migration,
            name: "migrate",
            is_async: true,
        },
        CoreApiMethod {
            module: ApiModule::Migration,
            name: "up",
            is_async: true,
        },
        CoreApiMethod {
            module: ApiModule::Migration,
            name: "down",
            is_async: true,
        },
        CoreApiMethod {
            module: ApiModule::Migration,
            name: "rollback",
            is_async: true,
        },
        CoreApiMethod {
            module: ApiModule::Migration,
            name: "reset",
            is_async: true,
        },
        CoreApiMethod {
            module: ApiModule::Migration,
            name: "refresh",
            is_async: true,
        },
        CoreApiMethod {
            module: ApiModule::Migration,
            name: "progress",
            is_async: false,
        },
        CoreApiMethod {
            module: ApiModule::Migration,
            name: "build",
            is_async: false,
        },
    ]
}

// ────────────────────────────────────────────────────────────────────────────
//  单元测试
// ────────────────────────────────────────────────────────────────────────────

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_core_api_registry_non_empty() {
        let registry = CoreApiRegistry::new();
        assert!(!registry.is_empty());
        assert!(
            registry.len() >= 50,
            "核心 API 应有 50+ 方法,实际: {}",
            registry.len()
        );
    }

    #[test]
    fn test_core_api_methods_by_module() {
        let registry = CoreApiRegistry::new();
        let pool_methods = registry.methods_by_module(ApiModule::Pool);
        assert!(!pool_methods.is_empty());
        assert!(pool_methods.iter().all(|m| m.module == ApiModule::Pool));

        let query_methods = registry.methods_by_module(ApiModule::Query);
        assert!(!query_methods.is_empty());
        assert!(query_methods.iter().all(|m| m.module == ApiModule::Query));
    }

    #[test]
    fn test_core_api_count_by_module() {
        let registry = CoreApiRegistry::new();
        let counts = registry.count_by_module();
        assert_eq!(counts.len(), 10);

        let total: usize = counts.iter().map(|(_, c)| c).sum();
        assert_eq!(total, registry.len());
    }

    #[test]
    fn test_audit_coverage_python() {
        let registry = CoreApiRegistry::new();
        let snapshot = BindingApiSnapshot::python();
        let report = audit_coverage(&registry, &snapshot);

        assert_eq!(report.binding_layer_name, "Python");
        assert!(report.coverage_percent > 0.0);
        assert!(report.coverage_percent < 100.0);
        assert_eq!(report.covered_count, report.covered_methods.len());
        assert_eq!(report.total_methods, registry.len());
    }

    #[test]
    fn test_audit_coverage_all_layers() {
        let registry = CoreApiRegistry::new();
        let reports = audit_all_layers(&registry);

        assert_eq!(reports.len(), 7);
        for report in &reports {
            assert!(report.coverage_percent >= 0.0);
            assert!(report.coverage_percent <= 100.0);
            assert_eq!(report.total_methods, registry.len());
        }
    }

    #[test]
    fn test_audit_coverage_nodejs_higher_than_java() {
        let registry = CoreApiRegistry::new();
        let nodejs_report = audit_coverage(&registry, &BindingApiSnapshot::nodejs());
        let java_report = audit_coverage(&registry, &BindingApiSnapshot::java());

        assert!(
            nodejs_report.coverage_percent > java_report.coverage_percent,
            "Node.js 覆盖率 ({:.1}%) 应高于 Java ({:.1}%)",
            nodejs_report.coverage_percent,
            java_report.coverage_percent
        );
    }

    #[test]
    fn test_audit_coverage_full_coverage() {
        let registry = CoreApiRegistry::new();
        let owned = registry.method_names();
        let all_methods: Vec<&str> = owned.iter().map(|s| s.as_str()).collect();
        let snapshot = BindingApiSnapshot::from_methods(BindingLayer::CAbi, &all_methods);
        let report = audit_coverage(&registry, &snapshot);

        assert_eq!(report.coverage_percent, 100.0);
        assert!(report.uncovered_methods.is_empty());
    }

    #[test]
    fn test_audit_coverage_zero_coverage() {
        let registry = CoreApiRegistry::new();
        let snapshot = BindingApiSnapshot::new(BindingLayer::Wasm);
        let report = audit_coverage(&registry, &snapshot);

        assert_eq!(report.coverage_percent, 0.0);
        assert!(report.covered_methods.is_empty());
        assert_eq!(report.uncovered_count(), registry.len());
    }

    #[test]
    fn test_format_summary() {
        let registry = CoreApiRegistry::new();
        let reports = audit_all_layers(&registry);
        let summary = format_summary(&reports);

        assert!(summary.contains("API 覆盖率审计报告"));
        assert!(summary.contains("Python"));
        assert!(summary.contains("Node.js"));
    }

    #[test]
    fn test_format_detail() {
        let registry = CoreApiRegistry::new();
        let report = audit_coverage(&registry, &BindingApiSnapshot::python());
        let detail = format_detail(&report);

        assert!(detail.contains("Python"));
        assert!(detail.contains("覆盖率"));
    }

    #[test]
    fn test_method_priority_ordering() {
        assert_eq!(method_priority("pool::new"), 0);
        assert_eq!(method_priority("query::select"), 0);
        assert_eq!(method_priority("transaction::commit"), 0);
        assert_eq!(method_priority("model::table_name"), 1);
        assert_eq!(method_priority("active_model::set"), 1);
        assert_eq!(method_priority("hooks::register"), 2);
        assert_eq!(method_priority("observer::dispatch"), 2);
        assert_eq!(method_priority("repository::new"), 3);
        assert_eq!(method_priority("paginator::fetch_page"), 3);
        assert_eq!(method_priority("migration::up"), 4);
    }

    #[test]
    fn test_api_module_as_str() {
        assert_eq!(ApiModule::Pool.as_str(), "pool");
        assert_eq!(ApiModule::Query.as_str(), "query");
        assert_eq!(ApiModule::Transaction.as_str(), "transaction");
        assert_eq!(ApiModule::Migration.as_str(), "migration");
    }

    #[test]
    fn test_binding_layer_as_str() {
        assert_eq!(BindingLayer::Python.as_str(), "Python");
        assert_eq!(BindingLayer::Java.as_str(), "Java");
        assert_eq!(BindingLayer::NodeJs.as_str(), "Node.js");
        assert_eq!(BindingLayer::CAbi.as_str(), "C ABI");
    }

    #[test]
    fn test_report_meets_target() {
        let registry = CoreApiRegistry::new();
        let report = audit_coverage(&registry, &BindingApiSnapshot::python());

        assert!(!report.meets_target(90.0), "Python 当前覆盖率不应达到 90%");
        assert!(report.meets_target(0.0));
    }

    /// v6.9.0:验证 Python 绑定层覆盖率从基线 32.5% 提升。
    #[test]
    fn test_python_coverage_v690_enhanced() {
        let registry = CoreApiRegistry::new();
        let report = audit_coverage(&registry, &BindingApiSnapshot::python());

        println!(
            "\nPython 绑定层 v6.9.0 覆盖率: {:.1}% ({}/{} 方法)",
            report.coverage_percent, report.covered_count, report.total_methods
        );
        println!("已覆盖模块: active_model, repository, hooks, observer (v6.9.0 新增)");
        println!("未覆盖方法数: {}", report.uncovered_count());

        assert!(
            report.coverage_percent > 40.0,
            "v6.9.0 Python 覆盖率应 > 40%,实际: {:.1}%",
            report.coverage_percent
        );

        let has_active_model = report
            .covered_methods
            .iter()
            .any(|m| m.starts_with("active_model::"));
        let has_hooks = report
            .covered_methods
            .iter()
            .any(|m| m.starts_with("hooks::"));
        let has_observer = report
            .covered_methods
            .iter()
            .any(|m| m.starts_with("observer::"));
        let has_repository = report
            .covered_methods
            .iter()
            .any(|m| m.starts_with("repository::"));

        assert!(has_active_model, "应覆盖 active_model 模块");
        assert!(has_hooks, "应覆盖 hooks 模块");
        assert!(has_observer, "应覆盖 observer 模块");
        assert!(has_repository, "应覆盖 repository 模块");
    }

    /// 输出所有绑定层覆盖率基线报告(--nocapture 时可见)。
    #[test]
    fn test_coverage_baseline_report() {
        let registry = CoreApiRegistry::new();
        let reports = audit_all_layers(&registry);

        println!("\n{}", format_summary(&reports));

        let target_layers = ["Python", "Java", "Go", "C++", "Node.js"];
        for report in &reports {
            if target_layers.contains(&report.binding_layer_name.as_str()) {
                println!("{}", format_detail(report));
            }
        }

        for report in &reports {
            assert!(report.coverage_percent >= 0.0);
            assert!(report.coverage_percent <= 100.0);
        }
    }
}