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
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
use crate::{
config,
data::{Commit, MeasurementData},
defaults,
measurement_retrieval::{self, summarize_measurements},
stats::{self, DispersionMethod, ReductionFunc, StatsWithUnit, VecAggregation},
};
use anyhow::{anyhow, bail, Result};
use itertools::Itertools;
use log::info;
use sparklines::spark;
use std::cmp::Ordering;
use std::collections::HashSet;
use std::iter;
/// Formats a z-score for display in audit output.
/// Only finite z-scores are displayed with numeric values.
/// Infinite and NaN values return an empty string.
fn format_z_score_display(z_score: f64) -> String {
if z_score.is_finite() {
format!(" {:.2}", z_score)
} else {
String::new()
}
}
/// Determines the direction arrow based on comparison of head and tail means.
/// Returns ↑ for greater, ↓ for less, → for equal.
/// Returns → for NaN values to avoid panicking.
fn get_direction_arrow(head_mean: f64, tail_mean: f64) -> &'static str {
match head_mean.partial_cmp(&tail_mean) {
Some(Ordering::Greater) => "↑",
Some(Ordering::Less) => "↓",
Some(Ordering::Equal) | None => "→",
}
}
#[derive(Debug, PartialEq)]
struct AuditResult {
message: String,
passed: bool,
}
/// Resolved audit parameters for a specific measurement.
#[derive(Debug, PartialEq)]
pub(crate) struct ResolvedAuditParams {
pub min_count: u16,
pub summarize_by: ReductionFunc,
pub sigma: f64,
pub dispersion_method: DispersionMethod,
}
/// Resolves audit parameters for a specific measurement with proper precedence:
/// CLI option -> measurement-specific config -> global config -> built-in default
///
/// Note: When CLI provides min_count, the caller (audit_multiple) uses the same
/// value for all measurements. When CLI is None, this function reads per-measurement config.
pub(crate) fn resolve_audit_params(
measurement: &str,
cli_min_count: Option<u16>,
cli_summarize_by: Option<ReductionFunc>,
cli_sigma: Option<f64>,
cli_dispersion_method: Option<DispersionMethod>,
) -> ResolvedAuditParams {
let min_count = cli_min_count
.or_else(|| config::audit_min_measurements(measurement))
.unwrap_or(defaults::DEFAULT_MIN_MEASUREMENTS);
let summarize_by = cli_summarize_by
.or_else(|| config::audit_aggregate_by(measurement).map(ReductionFunc::from))
.unwrap_or(ReductionFunc::Min);
let sigma = cli_sigma
.or_else(|| config::audit_sigma(measurement))
.unwrap_or(defaults::DEFAULT_SIGMA);
let dispersion_method = cli_dispersion_method
.or_else(|| {
Some(DispersionMethod::from(config::audit_dispersion_method(
measurement,
)))
})
.unwrap_or(DispersionMethod::StandardDeviation);
ResolvedAuditParams {
min_count,
summarize_by,
sigma,
dispersion_method,
}
}
/// Discovers all unique measurement names from commits that match the filters and selectors.
/// This is used to efficiently find which measurements to audit when filters are provided.
fn discover_matching_measurements(
commits: &[Result<Commit>],
filters: &[regex::Regex],
selectors: &[(String, String)],
) -> Vec<String> {
let mut unique_measurements = HashSet::new();
for commit in commits.iter().flatten() {
for measurement in &commit.measurements {
// Check if measurement name matches any filter
if !crate::filter::matches_any_filter(&measurement.name, filters) {
continue;
}
// Check if measurement matches selectors
if !measurement.key_values_is_superset_of(selectors) {
continue;
}
// This measurement matches - add to set
unique_measurements.insert(measurement.name.clone());
}
}
// Convert to sorted vector for deterministic ordering
let mut result: Vec<String> = unique_measurements.into_iter().collect();
result.sort();
result
}
/// Compute group value combinations for splitting measurements by metadata keys.
///
/// Returns a vector of group values where each inner vector contains the values
/// for the split keys. If no splits are specified, returns a single empty group.
///
/// # Errors
/// Returns error if separate_by is non-empty but no measurements have all required keys
fn compute_group_values(
commits: &[Result<Commit>],
measurement_name: &str,
selectors: &[(String, String)],
separate_by: &[String],
) -> Result<Vec<Vec<String>>> {
if separate_by.is_empty() {
return Ok(vec![vec![]]);
}
let mut unique_groups = HashSet::new();
for commit in commits.iter().flatten() {
for measurement in &commit.measurements {
// Only consider measurements that match the name
if measurement.name != measurement_name {
continue;
}
// Check if measurement matches selectors
if !measurement.key_values_is_superset_of(selectors) {
continue;
}
// Extract values for separate_by keys
let values: Vec<String> = separate_by
.iter()
.filter_map(|key| measurement.key_values.get(key).cloned())
.collect();
// Only include if all keys are present
if values.len() == separate_by.len() {
unique_groups.insert(values);
}
}
}
if unique_groups.is_empty() {
bail!(
"Measurement '{}': Invalid separator supplied, no measurements have all required keys: {:?}",
measurement_name,
separate_by
);
}
// Convert to sorted vector for deterministic ordering
let mut result: Vec<Vec<String>> = unique_groups.into_iter().collect();
result.sort();
Ok(result)
}
/// Formats a group label from separate_by keys and values.
/// Example: ["os", "arch"] with ["ubuntu", "x64"] -> "os=ubuntu/arch=x64"
fn format_group_label(separate_by: &[String], group_values: &[String]) -> String {
separate_by
.iter()
.zip(group_values.iter())
.map(|(key, value)| format!("{}={}", key, value))
.collect::<Vec<_>>()
.join("/")
}
#[allow(clippy::too_many_arguments)]
pub fn audit_multiple(
start_commit: &str,
max_count: usize,
min_count: Option<u16>,
selectors: &[(String, String)],
summarize_by: Option<ReductionFunc>,
sigma: Option<f64>,
dispersion_method: Option<DispersionMethod>,
combined_patterns: &[String],
separate_by: &[String],
_no_change_point_warning: bool, // TODO: Implement change point warning in Phase 2
) -> Result<()> {
// Early return if patterns are empty - nothing to audit
if combined_patterns.is_empty() {
return Ok(());
}
// Validate that separate_by keys don't overlap with selectors (would produce contradictory filters)
let selector_keys: std::collections::HashSet<&str> =
selectors.iter().map(|(k, _)| k.as_str()).collect();
for key in separate_by {
if selector_keys.contains(key.as_str()) {
bail!(
"separate-by key '{}' already present in selectors; remove it from --selectors or --separate-by",
key
);
}
}
// Compile combined regex patterns (measurements as exact matches + filter patterns)
// early to fail fast on invalid patterns
let filters = crate::filter::compile_filters(combined_patterns)?;
// Phase 1: Walk commits ONCE (optimization: scan commits only once)
// Collect into Vec so we can reuse the data for multiple measurements
let all_commits: Vec<Result<Commit>> =
measurement_retrieval::walk_commits_from(start_commit, max_count)?.collect();
// Phase 2: Discover all measurements that match the combined patterns from the commit data
// The combined_patterns already include both measurements (as exact regex) and filters (OR behavior)
let measurements_to_audit = discover_matching_measurements(&all_commits, &filters, selectors);
// If no measurements were discovered, provide appropriate error message
if measurements_to_audit.is_empty() {
// Check if we have any commits at all
if all_commits.is_empty() {
bail!("No commit at HEAD");
}
// Check if any commits have any measurements at all
let has_any_measurements = all_commits.iter().any(|commit_result| {
if let Ok(commit) = commit_result {
!commit.measurements.is_empty()
} else {
false
}
});
if !has_any_measurements {
// No measurements exist in any commits - specific error for this case
bail!("No measurement for HEAD");
}
// Measurements exist but don't match the patterns
bail!("No measurements found matching the provided patterns");
}
let mut failed = false;
let mut total_groups = 0;
let mut passed_groups = 0;
// Phase 3: For each measurement, audit using the pre-loaded commit data
for measurement in measurements_to_audit {
let params = resolve_audit_params(
&measurement,
min_count,
summarize_by,
sigma,
dispersion_method,
);
// Warn if max_count limits historical data below min_measurements requirement
if (max_count as u16) < params.min_count {
eprintln!(
"⚠️ Warning: --max_count ({}) is less than min_measurements ({}) for measurement '{}'.",
max_count, params.min_count, measurement
);
eprintln!(
" This limits available historical data and may prevent achieving statistical significance."
);
}
// Compute groups for this measurement
let groups = compute_group_values(&all_commits, &measurement, selectors, separate_by)?;
// Audit each group independently
for group_values in &groups {
// Build combined selectors (original selectors + group selectors)
let mut group_selectors = selectors.to_vec();
for (key, value) in separate_by.iter().zip(group_values.iter()) {
group_selectors.push((key.clone(), value.clone()));
}
// Format group label for display
let group_label = if separate_by.is_empty() {
String::new()
} else {
format!(" ({})", format_group_label(separate_by, group_values))
};
let result = audit_with_commits(
&measurement,
&all_commits,
params.min_count,
&group_selectors,
params.summarize_by,
params.sigma,
params.dispersion_method,
)?;
// TODO(Phase 2): Add change point detection warning here
// If !_no_change_point_warning, detect change points in current epoch
// and warn if any exist, as they make z-score comparisons unreliable:
// ⚠️ WARNING: Change point detected in current epoch at commit a1b2c3d (+23.5%)
// Historical z-score comparison may be unreliable due to regime shift.
// Consider bumping epoch or investigating the change.
// See docs/plans/change-point-detection.md for implementation details.
// Print the result with group label
if !separate_by.is_empty() {
// Print header for the group
println!("Auditing measurement \"{}\"{}:", measurement, group_label);
// Indent the result message
for line in result.message.lines() {
println!(" {}", line);
}
println!(); // Add blank line between groups
} else {
println!("{}", result.message);
}
if !separate_by.is_empty() {
total_groups += 1;
if result.passed {
passed_groups += 1;
}
}
if !result.passed {
failed = true;
}
}
}
// Print summary if grouping is active
if !separate_by.is_empty() {
if failed {
println!(
"Overall: FAILED ({}/{} groups passed)",
passed_groups, total_groups
);
} else {
println!(
"Overall: PASSED ({}/{} groups passed)",
passed_groups, total_groups
);
}
}
if failed {
bail!("One or more measurements failed audit.");
}
Ok(())
}
/// Audits a measurement using pre-loaded commit data.
/// This is more efficient than the old `audit` function when auditing multiple measurements,
/// as it reuses the same commit data instead of walking commits multiple times.
fn audit_with_commits(
measurement: &str,
commits: &[Result<Commit>],
min_count: u16,
selectors: &[(String, String)],
summarize_by: ReductionFunc,
sigma: f64,
dispersion_method: DispersionMethod,
) -> Result<AuditResult> {
// Convert Vec<Result<Commit>> into an iterator of Result<Commit> by cloning references
// This is necessary because summarize_measurements expects an iterator of Result<Commit>
let commits_iter = commits.iter().map(|r| match r {
Ok(commit) => Ok(Commit {
commit: commit.commit.clone(),
title: commit.title.clone(),
author: commit.author.clone(),
measurements: commit.measurements.clone(),
}),
Err(e) => Err(anyhow::anyhow!("{}", e)),
});
// Filter to only this specific measurement with matching selectors
let filter_by =
|m: &MeasurementData| m.name == measurement && m.key_values_is_superset_of(selectors);
let mut aggregates = measurement_retrieval::take_while_same_epoch(summarize_measurements(
commits_iter,
&summarize_by,
&filter_by,
));
let head = aggregates
.next()
.ok_or(anyhow!("No commit at HEAD"))
.and_then(|s| {
s.and_then(|cs| {
cs.measurement
.map(|m| m.val)
.ok_or(anyhow!("No measurement for HEAD."))
})
})?;
let tail: Vec<_> = aggregates
.filter_map_ok(|cs| cs.measurement.map(|m| m.val))
.try_collect()?;
audit_with_data(
measurement,
head,
tail,
min_count,
sigma,
dispersion_method,
summarize_by,
)
}
/// Core audit logic that can be tested with mock data
/// This function contains all the mutation-tested logic paths
fn audit_with_data(
measurement: &str,
head: f64,
tail: Vec<f64>,
min_count: u16,
sigma: f64,
dispersion_method: DispersionMethod,
summarize_by: ReductionFunc,
) -> Result<AuditResult> {
// Note: CLI enforces min_count >= 2 via clap::value_parser!(u16).range(2..)
// Tests may use lower values for edge case testing, but production code
// should never call this with min_count < 2
assert!(min_count >= 2, "min_count must be at least 2");
// Get unit for this measurement from config
let unit = config::measurement_unit(measurement);
let unit_str = unit.as_deref();
let head_summary = stats::aggregate_measurements(iter::once(&head));
let tail_summary = stats::aggregate_measurements(tail.iter());
// Generate sparkline and calculate range for all measurements - used in both skip and normal paths
let all_measurements = tail.into_iter().chain(iter::once(head)).collect::<Vec<_>>();
let mut tail_measurements = all_measurements.clone();
tail_measurements.pop(); // Remove head to get just tail for median calculation
let tail_median = tail_measurements.median().unwrap_or_default();
// Calculate min and max once for use in both branches
let min_val = all_measurements
.iter()
.min_by(|a, b| a.partial_cmp(b).unwrap())
.unwrap();
let max_val = all_measurements
.iter()
.max_by(|a, b| a.partial_cmp(b).unwrap())
.unwrap();
// Tiered approach for sparkline display:
// 1. If tail median is non-zero: use median as baseline, show percentages (default behavior)
// 2. If tail median is zero: show absolute differences instead
let tail_median_is_zero = tail_median.abs() < f64::EPSILON;
let sparkline = if tail_median_is_zero {
// Median is zero - show absolute range
format!(
" [{} – {}] {}",
min_val,
max_val,
spark(all_measurements.as_slice())
)
} else {
// MUTATION POINT: / vs % (Line 140)
// Median is non-zero - use it as baseline for percentage ranges
let relative_min = min_val / tail_median - 1.0;
let relative_max = max_val / tail_median - 1.0;
format!(
" [{:+.2}% – {:+.2}%] {}",
(relative_min * 100.0),
(relative_max * 100.0),
spark(all_measurements.as_slice())
)
};
// Helper function to build the measurement summary text
// This is used for both skipped and normal audit results to avoid duplication
let build_summary = || -> String {
let mut summary = String::new();
// Use the length of all_measurements vector for total count
let total_measurements = all_measurements.len();
// If only 1 total measurement (head only, no tail), show only head summary
if total_measurements == 1 {
let head_display = StatsWithUnit {
stats: &head_summary,
unit: unit_str,
};
summary.push_str(&format!("Head: {}\n", head_display));
} else if total_measurements >= 2 {
// 2+ measurements: show aggregation method, z-score, head, tail, and sparkline
let direction = get_direction_arrow(head_summary.mean, tail_summary.mean);
let z_score = head_summary.z_score_with_method(&tail_summary, dispersion_method);
let z_score_display = format_z_score_display(z_score);
let method_name = match dispersion_method {
DispersionMethod::StandardDeviation => "stddev",
DispersionMethod::MedianAbsoluteDeviation => "mad",
};
let head_display = StatsWithUnit {
stats: &head_summary,
unit: unit_str,
};
let tail_display = StatsWithUnit {
stats: &tail_summary,
unit: unit_str,
};
summary.push_str(&format!("Aggregation: {summarize_by}\n"));
summary.push_str(&format!(
"z-score ({method_name}): {direction}{}\n",
z_score_display
));
summary.push_str(&format!("Head: {}\n", head_display));
summary.push_str(&format!("Tail: {}\n", tail_display));
summary.push_str(&sparkline);
}
// If 0 total measurements, return empty summary
summary
};
// MUTATION POINT: < vs == (Line 120)
if tail_summary.len < min_count.into() {
let number_measurements = tail_summary.len;
// MUTATION POINT: > vs < (Line 122)
let plural_s = if number_measurements == 1 { "" } else { "s" };
info!("Only {number_measurements} historical measurement{plural_s} found. Less than requested min_measurements of {min_count}. Skipping test.");
let mut skip_message = format!(
"⏭️ '{measurement}'\nOnly {number_measurements} historical measurement{plural_s} found. Less than requested min_measurements of {min_count}. Skipping test."
);
// Add summary using the same logic as passing/failing cases
let summary = build_summary();
if !summary.is_empty() {
skip_message.push('\n');
skip_message.push_str(&summary);
}
return Ok(AuditResult {
message: skip_message,
passed: true,
});
}
// MUTATION POINT: / vs % (Line 150)
// Calculate relative deviation - naturally handles infinity when tail_median is zero
let head_relative_deviation = (head / tail_median - 1.0).abs() * 100.0;
// Calculate absolute deviation
let head_absolute_deviation = (head - tail_median).abs();
// Check if we have a minimum relative deviation threshold configured
let min_relative_deviation = config::audit_min_relative_deviation(measurement);
let min_absolute_deviation = config::audit_min_absolute_deviation(measurement);
// MUTATION POINT: < vs == (Line 156)
let passed_due_to_relative_threshold = min_relative_deviation
.map(|threshold| head_relative_deviation < threshold)
.unwrap_or(false);
let passed_due_to_absolute_threshold = min_absolute_deviation
.map(|threshold| head_absolute_deviation < threshold)
.unwrap_or(false);
let passed_due_to_threshold =
passed_due_to_relative_threshold || passed_due_to_absolute_threshold;
let text_summary = build_summary();
// MUTATION POINT: > vs >= (Line 178)
let z_score_exceeds_sigma =
head_summary.is_significant(&tail_summary, sigma, dispersion_method);
// MUTATION POINT: ! removal (Line 181)
let passed = !z_score_exceeds_sigma || passed_due_to_threshold;
// Add threshold information to output if applicable
// Only show note when the audit would have failed without the threshold
let threshold_note = if z_score_exceeds_sigma {
let mut notes = Vec::new();
if passed_due_to_relative_threshold {
notes.push(format!(
"Note: Passed due to relative deviation ({:.1}%) being below threshold ({:.1}%)",
head_relative_deviation,
min_relative_deviation.unwrap()
));
}
if passed_due_to_absolute_threshold {
notes.push(format!(
"Note: Passed due to absolute deviation ({:.1}) being below threshold ({:.1})",
head_absolute_deviation,
min_absolute_deviation.unwrap()
));
}
if notes.is_empty() {
String::new()
} else {
format!("\n{}", notes.join("\n"))
}
} else {
String::new()
};
// MUTATION POINT: ! removal (Line 194)
if !passed {
return Ok(AuditResult {
message: format!(
"❌ '{measurement}'\nHEAD differs significantly from tail measurements.\n{text_summary}{threshold_note}"
),
passed: false,
});
}
Ok(AuditResult {
message: format!("✅ '{measurement}'\n{text_summary}{threshold_note}"),
passed: true,
})
}
#[cfg(test)]
mod test {
use crate::test_helpers::with_isolated_test_setup;
use super::*;
#[test]
fn test_format_z_score_display() {
// Test cases for z-score display formatting
let test_cases = vec![
(2.5_f64, " 2.50"),
(0.0_f64, " 0.00"),
(-1.5_f64, " -1.50"),
(999.999_f64, " 1000.00"),
(0.001_f64, " 0.00"),
(f64::INFINITY, ""),
(f64::NEG_INFINITY, ""),
(f64::NAN, ""),
];
for (z_score, expected) in test_cases {
let result = format_z_score_display(z_score);
assert_eq!(result, expected, "Failed for z_score: {}", z_score);
}
}
#[test]
fn test_direction_arrows() {
// Test cases for direction arrow logic
let test_cases = vec![
(5.0_f64, 3.0_f64, "↑"), // head > tail
(1.0_f64, 3.0_f64, "↓"), // head < tail
(3.0_f64, 3.0_f64, "→"), // head == tail
];
for (head_mean, tail_mean, expected) in test_cases {
let result = get_direction_arrow(head_mean, tail_mean);
assert_eq!(
result, expected,
"Failed for head_mean: {}, tail_mean: {}",
head_mean, tail_mean
);
}
}
#[test]
fn test_audit_with_different_dispersion_methods() {
// Test that audit produces different results with different dispersion methods
// Create mock data that would produce different z-scores with stddev vs MAD
let head_value = 35.0;
let tail_values = [30.0, 30.0, 30.0, 30.0, 30.0, 30.0, 30.0, 30.0, 30.0, 100.0];
let head_summary = stats::aggregate_measurements(std::iter::once(&head_value));
let tail_summary = stats::aggregate_measurements(tail_values.iter());
// Calculate z-scores with both methods
let z_score_stddev =
head_summary.z_score_with_method(&tail_summary, DispersionMethod::StandardDeviation);
let z_score_mad = head_summary
.z_score_with_method(&tail_summary, DispersionMethod::MedianAbsoluteDeviation);
// With the outlier (100.0), stddev should be much larger than MAD
// So z-score with stddev should be smaller than z-score with MAD
assert!(
z_score_stddev < z_score_mad,
"stddev z-score ({}) should be smaller than MAD z-score ({}) with outlier data",
z_score_stddev,
z_score_mad
);
// Both should be positive since head > tail mean
assert!(z_score_stddev > 0.0);
assert!(z_score_mad > 0.0);
}
#[test]
fn test_dispersion_method_conversion() {
// Test that the conversion from CLI types to stats types works correctly
// Test stddev conversion
let cli_stddev = git_perf_cli_types::DispersionMethod::StandardDeviation;
let stats_stddev: DispersionMethod = cli_stddev.into();
assert_eq!(stats_stddev, DispersionMethod::StandardDeviation);
// Test MAD conversion
let cli_mad = git_perf_cli_types::DispersionMethod::MedianAbsoluteDeviation;
let stats_mad: DispersionMethod = cli_mad.into();
assert_eq!(stats_mad, DispersionMethod::MedianAbsoluteDeviation);
}
#[test]
fn test_audit_multiple_with_no_measurements() {
// This test exercises the actual production audit_multiple function
// Tests the case where no patterns are provided (empty list)
// With no patterns, it should succeed (nothing to audit)
with_isolated_test_setup(|_git_dir, _home_path| {
let result = audit_multiple(
"HEAD",
100,
Some(1),
&[],
Some(ReductionFunc::Mean),
Some(2.0),
Some(DispersionMethod::StandardDeviation),
&[], // Empty combined_patterns
&[], // Empty separate_by
false,
);
// Should succeed when no measurements need to be audited
assert!(
result.is_ok(),
"audit_multiple should succeed with empty pattern list"
);
});
}
// MUTATION TESTING COVERAGE TESTS - Exercise actual production code paths
#[test]
fn test_min_count_boundary_condition() {
// COVERS MUTATION: tail_summary.len < min_count.into() vs ==
// Test with exactly min_count measurements (should NOT skip)
let result = audit_with_data(
"test_measurement",
15.0,
vec![10.0, 11.0, 12.0], // Exactly 3 measurements
3, // min_count = 3
2.0,
DispersionMethod::StandardDeviation,
ReductionFunc::Min,
);
assert!(result.is_ok());
let audit_result = result.unwrap();
// Should NOT be skipped (would be skipped if < was changed to ==)
assert!(!audit_result.message.contains("Skipping test"));
// Test with fewer than min_count (should skip)
let result = audit_with_data(
"test_measurement",
15.0,
vec![10.0, 11.0], // Only 2 measurements
3, // min_count = 3
2.0,
DispersionMethod::StandardDeviation,
ReductionFunc::Min,
);
assert!(result.is_ok());
let audit_result = result.unwrap();
assert!(audit_result.message.contains("Skipping test"));
assert!(audit_result.passed); // Skipped tests are marked as passed
}
#[test]
fn test_pluralization_logic() {
// COVERS MUTATION: number_measurements > 1 vs ==
// Test with 0 measurements (should have 's' - grammatically correct)
let result = audit_with_data(
"test_measurement",
15.0,
vec![], // 0 measurements
5, // min_count > 0 to trigger skip
2.0,
DispersionMethod::StandardDeviation,
ReductionFunc::Min,
);
assert!(result.is_ok());
let message = result.unwrap().message;
assert!(message.contains("0 historical measurements found")); // Has 's'
assert!(!message.contains("0 historical measurement found")); // Should not be singular
// Test with 1 measurement (no 's')
let result = audit_with_data(
"test_measurement",
15.0,
vec![10.0], // 1 measurement
5, // min_count > 1 to trigger skip
2.0,
DispersionMethod::StandardDeviation,
ReductionFunc::Min,
);
assert!(result.is_ok());
let message = result.unwrap().message;
assert!(message.contains("1 historical measurement found")); // No 's'
// Test with 2+ measurements (should have 's')
let result = audit_with_data(
"test_measurement",
15.0,
vec![10.0, 11.0], // 2 measurements
5, // min_count > 2 to trigger skip
2.0,
DispersionMethod::StandardDeviation,
ReductionFunc::Min,
);
assert!(result.is_ok());
let message = result.unwrap().message;
assert!(message.contains("2 historical measurements found")); // Has 's'
}
#[test]
fn test_skip_with_summaries() {
// Test that when audit is skipped, summaries are shown based on TOTAL measurement count
// Total measurements = 1 head + N tail
// and the format matches passing/failing cases
// Test with 0 tail measurements (1 total): should show Head only
let result = audit_with_data(
"test_measurement",
15.0,
vec![], // 0 tail measurements = 1 total measurement
5, // min_count > 0 to trigger skip
2.0,
DispersionMethod::StandardDeviation,
ReductionFunc::Min,
);
assert!(result.is_ok());
let message = result.unwrap().message;
assert!(message.contains("Skipping test"));
assert!(message.contains("Head:")); // Head summary shown
assert!(!message.contains("z-score")); // No z-score (only 1 total measurement)
assert!(!message.contains("Tail:")); // No tail
assert!(!message.contains("[")); // No sparkline
// Test with 1 tail measurement (2 total): should show everything
let result = audit_with_data(
"test_measurement",
15.0,
vec![10.0], // 1 tail measurement = 2 total measurements
5, // min_count > 1 to trigger skip
2.0,
DispersionMethod::StandardDeviation,
ReductionFunc::Min,
);
assert!(result.is_ok());
let message = result.unwrap().message;
assert!(message.contains("Skipping test"));
assert!(message.contains("z-score (stddev):")); // Z-score with method shown
assert!(message.contains("Head:")); // Head summary shown
assert!(message.contains("Tail:")); // Tail summary shown
assert!(message.contains("[")); // Sparkline shown
// Verify order: z-score, Head, Tail, sparkline
let z_pos = message.find("z-score").unwrap();
let head_pos = message.find("Head:").unwrap();
let tail_pos = message.find("Tail:").unwrap();
let spark_pos = message.find("[").unwrap();
assert!(z_pos < head_pos, "z-score should come before Head");
assert!(head_pos < tail_pos, "Head should come before Tail");
assert!(tail_pos < spark_pos, "Tail should come before sparkline");
// Test with 2 tail measurements (3 total): should show everything
let result = audit_with_data(
"test_measurement",
15.0,
vec![10.0, 11.0], // 2 tail measurements = 3 total measurements
5, // min_count > 2 to trigger skip
2.0,
DispersionMethod::StandardDeviation,
ReductionFunc::Min,
);
assert!(result.is_ok());
let message = result.unwrap().message;
assert!(message.contains("Skipping test"));
assert!(message.contains("z-score (stddev):")); // Z-score with method shown
assert!(message.contains("Head:")); // Head summary shown
assert!(message.contains("Tail:")); // Tail summary shown
assert!(message.contains("[")); // Sparkline shown
// Verify order: z-score, Head, Tail, sparkline
let z_pos = message.find("z-score").unwrap();
let head_pos = message.find("Head:").unwrap();
let tail_pos = message.find("Tail:").unwrap();
let spark_pos = message.find("[").unwrap();
assert!(z_pos < head_pos, "z-score should come before Head");
assert!(head_pos < tail_pos, "Head should come before Tail");
assert!(tail_pos < spark_pos, "Tail should come before sparkline");
// Test with MAD dispersion method to ensure method name is correct
let result = audit_with_data(
"test_measurement",
15.0,
vec![10.0, 11.0], // 2 tail measurements = 3 total measurements
5, // min_count > 2 to trigger skip
2.0,
DispersionMethod::MedianAbsoluteDeviation,
ReductionFunc::Min,
);
assert!(result.is_ok());
let message = result.unwrap().message;
assert!(message.contains("z-score (mad):")); // MAD method shown
}
#[test]
fn test_relative_calculations_division_vs_modulo() {
// COVERS MUTATIONS: / vs % in relative_min, relative_max, head_relative_deviation
// Use values where division and modulo produce very different results
let result = audit_with_data(
"test_measurement",
25.0, // head
vec![10.0, 10.0, 10.0], // tail, median = 10.0
2,
10.0, // High sigma to avoid z-score failures
DispersionMethod::StandardDeviation,
ReductionFunc::Min,
);
assert!(result.is_ok());
let audit_result = result.unwrap();
// With division:
// - relative_min = (10.0 / 10.0 - 1.0) * 100 = 0.0%
// - relative_max = (25.0 / 10.0 - 1.0) * 100 = 150.0%
// With modulo:
// - relative_min = (10.0 % 10.0 - 1.0) * 100 = -100.0% (since 10.0 % 10.0 = 0.0)
// - relative_max = (25.0 % 10.0 - 1.0) * 100 = -50.0% (since 25.0 % 10.0 = 5.0)
// Check that the calculation uses division, not modulo
// The range should show [+0.00% – +150.00%], not [-100.00% – -50.00%]
assert!(audit_result.message.contains("[+0.00% – +150.00%]"));
// Ensure the modulo results are NOT present
assert!(!audit_result.message.contains("[-100.00% – -50.00%]"));
assert!(!audit_result.message.contains("-100.00%"));
assert!(!audit_result.message.contains("-50.00%"));
}
#[test]
fn test_core_pass_fail_logic() {
// COVERS MUTATION: !z_score_exceeds_sigma || passed_due_to_threshold
// vs z_score_exceeds_sigma || passed_due_to_threshold
// Case 1: z_score exceeds sigma, no threshold bypass (should fail)
let result = audit_with_data(
"test_measurement", // No config threshold for this name
100.0, // Very high head value
vec![10.0, 10.0, 10.0, 10.0, 10.0], // Low tail values
2,
0.5, // Low sigma threshold
DispersionMethod::StandardDeviation,
ReductionFunc::Min,
);
assert!(result.is_ok());
let audit_result = result.unwrap();
assert!(!audit_result.passed); // Should fail
assert!(audit_result.message.contains("❌"));
// Case 2: z_score within sigma (should pass)
let result = audit_with_data(
"test_measurement",
10.2, // Close to tail values
vec![10.0, 10.1, 10.0, 10.1, 10.0], // Some variance to avoid zero stddev
2,
100.0, // Very high sigma threshold
DispersionMethod::StandardDeviation,
ReductionFunc::Min,
);
assert!(result.is_ok());
let audit_result = result.unwrap();
assert!(audit_result.passed); // Should pass
assert!(audit_result.message.contains("✅"));
}
#[test]
fn test_final_result_logic() {
// COVERS MUTATION: if !passed vs if passed
// This tests the final branch that determines success vs failure message
// Test failing case (should get failure message)
let result = audit_with_data(
"test_measurement",
1000.0, // Extreme outlier
vec![10.0, 10.0, 10.0, 10.0, 10.0],
2,
0.1, // Very strict sigma
DispersionMethod::StandardDeviation,
ReductionFunc::Min,
);
assert!(result.is_ok());
let audit_result = result.unwrap();
assert!(!audit_result.passed);
assert!(audit_result.message.contains("❌"));
assert!(audit_result.message.contains("differs significantly"));
// Test passing case (should get success message)
let result = audit_with_data(
"test_measurement",
10.01, // Very close to tail
vec![10.0, 10.1, 10.0, 10.1, 10.0], // Varied values to avoid zero variance
2,
100.0, // Very lenient sigma
DispersionMethod::StandardDeviation,
ReductionFunc::Min,
);
assert!(result.is_ok());
let audit_result = result.unwrap();
assert!(audit_result.passed);
assert!(audit_result.message.contains("✅"));
assert!(!audit_result.message.contains("differs significantly"));
}
#[test]
fn test_dispersion_methods_produce_different_results() {
// Test that different dispersion methods work in the production code
let head = 35.0;
let tail = vec![30.0, 30.0, 30.0, 30.0, 30.0, 30.0, 30.0, 30.0, 30.0, 100.0];
let result_stddev = audit_with_data(
"test_measurement",
head,
tail.clone(),
2,
2.0,
DispersionMethod::StandardDeviation,
ReductionFunc::Min,
);
let result_mad = audit_with_data(
"test_measurement",
head,
tail,
2,
2.0,
DispersionMethod::MedianAbsoluteDeviation,
ReductionFunc::Min,
);
assert!(result_stddev.is_ok());
assert!(result_mad.is_ok());
let stddev_result = result_stddev.unwrap();
let mad_result = result_mad.unwrap();
// Both should contain method indicators
assert!(stddev_result.message.contains("stddev"));
assert!(mad_result.message.contains("mad"));
}
#[test]
fn test_head_and_tail_have_units_and_auto_scaling() {
// Test that both head and tail measurements display units with auto-scaling
// First, set up a test environment with a configured unit
use crate::test_helpers::setup_test_env_with_config;
let config_content = r#"
[measurement."build_time"]
unit = "ms"
"#;
let (_temp_dir, _dir_guard) = setup_test_env_with_config(config_content);
// Test with large millisecond values that should auto-scale to seconds
let head = 12_345.67; // Will auto-scale to ~12.35s
let tail = vec![10_000.0, 10_500.0, 11_000.0, 11_500.0, 12_000.0]; // Will auto-scale to 10s, 10.5s, 11s, etc.
let result = audit_with_data(
"build_time",
head,
tail,
2,
10.0, // High sigma to ensure it passes
DispersionMethod::StandardDeviation,
ReductionFunc::Min,
);
assert!(result.is_ok());
let audit_result = result.unwrap();
let message = &audit_result.message;
// Verify Head section exists
assert!(
message.contains("Head:"),
"Message should contain Head section"
);
// With auto-scaling, 12345.67ms should become ~12.35s or 12.3s
// Check that the value is auto-scaled (contains 's' for seconds)
assert!(
message.contains("12.3s") || message.contains("12.35s"),
"Head mean should be auto-scaled to seconds, got: {}",
message
);
let head_section: Vec<&str> = message
.lines()
.filter(|line| line.contains("Head:"))
.collect();
assert!(
!head_section.is_empty(),
"Should find Head section in message"
);
let head_line = head_section[0];
// With auto-scaling, all values (mean, stddev, MAD) get their units auto-scaled
// They should all have units now (not just mean)
assert!(
head_line.contains("μ:") && head_line.contains("σ:") && head_line.contains("MAD:"),
"Head line should contain μ, σ, and MAD labels, got: {}",
head_line
);
// Verify Tail section has units
assert!(
message.contains("Tail:"),
"Message should contain Tail section"
);
let tail_section: Vec<&str> = message
.lines()
.filter(|line| line.contains("Tail:"))
.collect();
assert!(
!tail_section.is_empty(),
"Should find Tail section in message"
);
let tail_line = tail_section[0];
// Tail mean should be auto-scaled to seconds (10000-12000ms → 10-12s)
assert!(
tail_line.contains("11s")
|| tail_line.contains("11.")
|| tail_line.contains("10.")
|| tail_line.contains("12."),
"Tail should contain auto-scaled second values, got: {}",
tail_line
);
// Verify the basic format structure is present
assert!(
tail_line.contains("μ:")
&& tail_line.contains("σ:")
&& tail_line.contains("MAD:")
&& tail_line.contains("n:"),
"Tail line should contain all stat labels, got: {}",
tail_line
);
}
#[test]
fn test_threshold_note_only_shown_when_audit_would_fail() {
// Test that the threshold note is only shown when the audit would have
// failed without the threshold (i.e., when z_score_exceeds_sigma is true)
use crate::test_helpers::setup_test_env_with_config;
let config_content = r#"
[measurement."build_time"]
min_relative_deviation = 10.0
"#;
let (_temp_dir, _dir_guard) = setup_test_env_with_config(config_content);
// Case 1: Low z-score AND low relative deviation (threshold is configured but not needed)
// Should pass without showing the note
let result = audit_with_data(
"build_time",
10.1, // Very close to tail values
vec![10.0, 10.1, 10.0, 10.1, 10.0], // Low variance
2,
100.0, // Very high sigma threshold - won't be exceeded
DispersionMethod::StandardDeviation,
ReductionFunc::Min,
);
assert!(result.is_ok());
let audit_result = result.unwrap();
assert!(audit_result.passed);
assert!(audit_result.message.contains("✅"));
// The note should NOT be shown because the audit would have passed anyway
assert!(
!audit_result
.message
.contains("Note: Passed due to relative deviation"),
"Note should not appear when audit passes without needing threshold bypass"
);
// Case 2: High z-score but low relative deviation (threshold saves the audit)
// Should pass and show the note
let result = audit_with_data(
"build_time",
1002.0, // High z-score outlier but low relative deviation
vec![1000.0, 1000.1, 1000.0, 1000.1, 1000.0], // Very low variance
2,
0.5, // Low sigma threshold - will be exceeded
DispersionMethod::StandardDeviation,
ReductionFunc::Min,
);
assert!(result.is_ok());
let audit_result = result.unwrap();
assert!(audit_result.passed);
assert!(audit_result.message.contains("✅"));
// The note SHOULD be shown because the audit would have failed without the threshold
assert!(
audit_result
.message
.contains("Note: Passed due to relative deviation"),
"Note should appear when audit passes due to threshold bypass. Got: {}",
audit_result.message
);
// Case 3: High z-score AND high relative deviation (threshold doesn't help)
// Should fail
let result = audit_with_data(
"build_time",
1200.0, // High z-score AND high relative deviation
vec![1000.0, 1000.1, 1000.0, 1000.1, 1000.0], // Very low variance
2,
0.5, // Low sigma threshold - will be exceeded
DispersionMethod::StandardDeviation,
ReductionFunc::Min,
);
assert!(result.is_ok());
let audit_result = result.unwrap();
assert!(!audit_result.passed);
assert!(audit_result.message.contains("❌"));
// No note shown because the audit still failed
assert!(
!audit_result
.message
.contains("Note: Passed due to relative deviation"),
"Note should not appear when audit fails"
);
}
#[test]
fn test_absolute_threshold_note_and_deviation_value() {
// Tests that:
// 1. The note shows the correct absolute deviation value (catches - vs / mutation)
// 2. The boundary: deviation exactly AT threshold fails (catches < vs <= mutation)
use crate::test_helpers::setup_test_env_with_config;
let config_content = r#"
[measurement."build_time"]
min_absolute_deviation = 50.0
"#;
let (_temp_dir, _dir_guard) = setup_test_env_with_config(config_content);
// Case 1: High z-score but low absolute deviation (threshold saves the audit)
// head=1010, tail values very tightly clustered around 1000
// absolute deviation = |1010 - 1000| = 10 < 50 => should pass
// if - were replaced with /, deviation would be |1010/1000| = 1.01, still < 50 (passes anyway)
// So we need values where subtraction and division give meaningfully different results
// head=1005, tail=1000: subtract=5, divide=1.005; but threshold=50, both < 50
// Let's use head=100, tail_median=10: subtract=90, divide=10; threshold=50
// With threshold=50: subtract(90) >= 50 fails, divide(10) < 50 passes
// This catches the - vs / mutation
let result = audit_with_data(
"build_time",
100.0, // head value
vec![10.0, 10.0, 10.0, 10.0, 10.0], // tail values, median=10
2,
0.5, // Low sigma - will be exceeded
DispersionMethod::StandardDeviation,
ReductionFunc::Min,
);
assert!(result.is_ok());
let audit_result = result.unwrap();
// absolute deviation = |100 - 10| = 90, which is > 50 threshold => should FAIL
assert!(
!audit_result.passed,
"Should fail: absolute deviation 90 > threshold 50. Got: {}",
audit_result.message
);
// Case 2: absolute deviation exactly equals threshold => should FAIL (< not <=)
// head=1050, tail_median=1000, absolute_deviation=50, threshold=50
// With < : 50 < 50 is false => fails (correct)
// With <= : 50 <= 50 is true => passes (wrong)
let result = audit_with_data(
"build_time",
1050.0, // head value
vec![1000.0, 1000.0, 1000.0, 1000.0, 1000.0], // tail values, median=1000
2,
0.5, // Low sigma - will be exceeded
DispersionMethod::StandardDeviation,
ReductionFunc::Min,
);
assert!(result.is_ok());
let audit_result = result.unwrap();
// absolute deviation = |1050 - 1000| = 50, which equals threshold 50 => should FAIL
assert!(
!audit_result.passed,
"Should fail: absolute deviation 50 == threshold 50 (not strictly less than). Got: {}",
audit_result.message
);
// Case 3: absolute deviation strictly below threshold => should PASS with note
// head=1049, tail_median=1000, absolute_deviation=49, threshold=50
let result = audit_with_data(
"build_time",
1049.0, // head value
vec![1000.0, 1000.0, 1000.0, 1000.0, 1000.0], // tail values, median=1000
2,
0.5, // Low sigma - will be exceeded
DispersionMethod::StandardDeviation,
ReductionFunc::Min,
);
assert!(result.is_ok());
let audit_result = result.unwrap();
assert!(
audit_result.passed,
"Should pass: absolute deviation 49 < threshold 50. Got: {}",
audit_result.message
);
assert!(
audit_result
.message
.contains("Note: Passed due to absolute deviation"),
"Note should appear when audit passes due to absolute threshold. Got: {}",
audit_result.message
);
// Verify the note contains the correct deviation value (catches - vs / mutation)
// If / were used: |1049/1000| = 1.049, note would say "1.0" not "49.0"
assert!(
audit_result.message.contains("49.0"),
"Note should show absolute deviation 49.0, not 1.0 (which would indicate / instead of -). Got: {}",
audit_result.message
);
}
// Integration tests that verify per-measurement config determination
#[cfg(test)]
mod integration {
use super::*;
use crate::config::{
audit_aggregate_by, audit_dispersion_method, audit_min_measurements, audit_sigma,
};
use crate::test_helpers::setup_test_env_with_config;
#[test]
fn test_different_dispersion_methods_per_measurement() {
let (_temp_dir, _dir_guard) = setup_test_env_with_config(
r#"
[measurement]
dispersion_method = "stddev"
[measurement."build_time"]
dispersion_method = "mad"
[measurement."memory_usage"]
dispersion_method = "stddev"
"#,
);
// Verify each measurement gets its own config
let build_time_method = audit_dispersion_method("build_time");
let memory_usage_method = audit_dispersion_method("memory_usage");
let other_method = audit_dispersion_method("other_metric");
assert_eq!(
DispersionMethod::from(build_time_method),
DispersionMethod::MedianAbsoluteDeviation,
"build_time should use MAD"
);
assert_eq!(
DispersionMethod::from(memory_usage_method),
DispersionMethod::StandardDeviation,
"memory_usage should use stddev"
);
assert_eq!(
DispersionMethod::from(other_method),
DispersionMethod::StandardDeviation,
"other_metric should use default stddev"
);
}
#[test]
fn test_different_min_measurements_per_measurement() {
let (_temp_dir, _dir_guard) = setup_test_env_with_config(
r#"
[measurement]
min_measurements = 5
[measurement."build_time"]
min_measurements = 10
[measurement."memory_usage"]
min_measurements = 3
"#,
);
assert_eq!(
audit_min_measurements("build_time"),
Some(10),
"build_time should require 10 measurements"
);
assert_eq!(
audit_min_measurements("memory_usage"),
Some(3),
"memory_usage should require 3 measurements"
);
assert_eq!(
audit_min_measurements("other_metric"),
Some(5),
"other_metric should use default 5 measurements"
);
}
#[test]
fn test_different_aggregate_by_per_measurement() {
let (_temp_dir, _dir_guard) = setup_test_env_with_config(
r#"
[measurement]
aggregate_by = "median"
[measurement."build_time"]
aggregate_by = "max"
[measurement."memory_usage"]
aggregate_by = "mean"
"#,
);
assert_eq!(
audit_aggregate_by("build_time"),
Some(git_perf_cli_types::ReductionFunc::Max),
"build_time should use max"
);
assert_eq!(
audit_aggregate_by("memory_usage"),
Some(git_perf_cli_types::ReductionFunc::Mean),
"memory_usage should use mean"
);
assert_eq!(
audit_aggregate_by("other_metric"),
Some(git_perf_cli_types::ReductionFunc::Median),
"other_metric should use default median"
);
}
#[test]
fn test_different_sigma_per_measurement() {
let (_temp_dir, _dir_guard) = setup_test_env_with_config(
r#"
[measurement]
sigma = 3.0
[measurement."build_time"]
sigma = 5.5
[measurement."memory_usage"]
sigma = 2.0
"#,
);
assert_eq!(
audit_sigma("build_time"),
Some(5.5),
"build_time should use sigma 5.5"
);
assert_eq!(
audit_sigma("memory_usage"),
Some(2.0),
"memory_usage should use sigma 2.0"
);
assert_eq!(
audit_sigma("other_metric"),
Some(3.0),
"other_metric should use default sigma 3.0"
);
}
#[test]
fn test_cli_overrides_config() {
let (_temp_dir, _dir_guard) = setup_test_env_with_config(
r#"
[measurement."build_time"]
min_measurements = 10
aggregate_by = "max"
sigma = 5.5
dispersion_method = "mad"
"#,
);
// Test that CLI values override config
let params = super::resolve_audit_params(
"build_time",
Some(2), // CLI min
Some(ReductionFunc::Min), // CLI aggregate
Some(3.0), // CLI sigma
Some(DispersionMethod::StandardDeviation), // CLI dispersion
);
assert_eq!(
params.min_count, 2,
"CLI min_measurements should override config"
);
assert_eq!(
params.summarize_by,
ReductionFunc::Min,
"CLI aggregate_by should override config"
);
assert_eq!(params.sigma, 3.0, "CLI sigma should override config");
assert_eq!(
params.dispersion_method,
DispersionMethod::StandardDeviation,
"CLI dispersion should override config"
);
}
#[test]
fn test_config_overrides_defaults() {
let (_temp_dir, _dir_guard) = setup_test_env_with_config(
r#"
[measurement."build_time"]
min_measurements = 10
aggregate_by = "max"
sigma = 5.5
dispersion_method = "mad"
"#,
);
// Test that config values are used when no CLI values provided
let params = super::resolve_audit_params(
"build_time",
None, // No CLI values
None,
None,
None,
);
assert_eq!(
params.min_count, 10,
"Config min_measurements should override default"
);
assert_eq!(
params.summarize_by,
ReductionFunc::Max,
"Config aggregate_by should override default"
);
assert_eq!(params.sigma, 5.5, "Config sigma should override default");
assert_eq!(
params.dispersion_method,
DispersionMethod::MedianAbsoluteDeviation,
"Config dispersion should override default"
);
}
#[test]
fn test_uses_defaults_when_no_config_or_cli() {
let (_temp_dir, _dir_guard) = setup_test_env_with_config("");
// Test that defaults are used when no CLI or config
let params = super::resolve_audit_params(
"non_existent_measurement",
None, // No CLI values
None,
None,
None,
);
assert_eq!(
params.min_count, 2,
"Should use default min_measurements of 2"
);
assert_eq!(
params.summarize_by,
ReductionFunc::Min,
"Should use default aggregate_by of Min"
);
assert_eq!(params.sigma, 4.0, "Should use default sigma of 4.0");
assert_eq!(
params.dispersion_method,
DispersionMethod::StandardDeviation,
"Should use default dispersion of stddev"
);
}
}
#[test]
fn test_discover_matching_measurements() {
use crate::data::{Commit, MeasurementData};
use std::collections::HashMap;
// Create mock commits with various measurements
let commits = vec![
Ok(Commit {
commit: "abc123".to_string(),
title: "test: commit 1".to_string(),
author: "Test Author".to_string(),
measurements: vec![
MeasurementData {
epoch: 0,
name: "bench_cpu".to_string(),
timestamp: 1000.0,
val: 100.0,
key_values: {
let mut map = HashMap::new();
map.insert("os".to_string(), "linux".to_string());
map
},
},
MeasurementData {
epoch: 0,
name: "bench_memory".to_string(),
timestamp: 1000.0,
val: 200.0,
key_values: {
let mut map = HashMap::new();
map.insert("os".to_string(), "linux".to_string());
map
},
},
MeasurementData {
epoch: 0,
name: "test_unit".to_string(),
timestamp: 1000.0,
val: 50.0,
key_values: {
let mut map = HashMap::new();
map.insert("os".to_string(), "linux".to_string());
map
},
},
],
}),
Ok(Commit {
commit: "def456".to_string(),
title: "test: commit 2".to_string(),
author: "Test Author".to_string(),
measurements: vec![
MeasurementData {
epoch: 0,
name: "bench_cpu".to_string(),
timestamp: 1000.0,
val: 105.0,
key_values: {
let mut map = HashMap::new();
map.insert("os".to_string(), "mac".to_string());
map
},
},
MeasurementData {
epoch: 0,
name: "other_metric".to_string(),
timestamp: 1000.0,
val: 75.0,
key_values: {
let mut map = HashMap::new();
map.insert("os".to_string(), "linux".to_string());
map
},
},
],
}),
];
// Test 1: Single filter pattern matching "bench_*"
let patterns = vec!["bench_.*".to_string()];
let filters = crate::filter::compile_filters(&patterns).unwrap();
let selectors = vec![];
let discovered = discover_matching_measurements(&commits, &filters, &selectors);
assert_eq!(discovered.len(), 2);
assert!(discovered.contains(&"bench_cpu".to_string()));
assert!(discovered.contains(&"bench_memory".to_string()));
assert!(!discovered.contains(&"test_unit".to_string()));
assert!(!discovered.contains(&"other_metric".to_string()));
// Test 2: Multiple filter patterns (OR behavior)
let patterns = vec!["bench_cpu".to_string(), "test_.*".to_string()];
let filters = crate::filter::compile_filters(&patterns).unwrap();
let discovered = discover_matching_measurements(&commits, &filters, &selectors);
assert_eq!(discovered.len(), 2);
assert!(discovered.contains(&"bench_cpu".to_string()));
assert!(discovered.contains(&"test_unit".to_string()));
assert!(!discovered.contains(&"bench_memory".to_string()));
// Test 3: Filter with selectors
let patterns = vec!["bench_.*".to_string()];
let filters = crate::filter::compile_filters(&patterns).unwrap();
let selectors = vec![("os".to_string(), "linux".to_string())];
let discovered = discover_matching_measurements(&commits, &filters, &selectors);
// bench_cpu and bench_memory both have os=linux (in first commit)
// bench_cpu also has os=mac (in second commit) but selector filters it to only linux
assert_eq!(discovered.len(), 2);
assert!(discovered.contains(&"bench_cpu".to_string()));
assert!(discovered.contains(&"bench_memory".to_string()));
// Test 4: No matches
let patterns = vec!["nonexistent.*".to_string()];
let filters = crate::filter::compile_filters(&patterns).unwrap();
let selectors = vec![];
let discovered = discover_matching_measurements(&commits, &filters, &selectors);
assert_eq!(discovered.len(), 0);
// Test 5: Empty filters (should match all)
let filters = vec![];
let selectors = vec![];
let discovered = discover_matching_measurements(&commits, &filters, &selectors);
// Empty filters should match nothing based on the logic
// Actually, looking at matches_any_filter, empty filters return true
// So this should discover all measurements
assert_eq!(discovered.len(), 4);
assert!(discovered.contains(&"bench_cpu".to_string()));
assert!(discovered.contains(&"bench_memory".to_string()));
assert!(discovered.contains(&"test_unit".to_string()));
assert!(discovered.contains(&"other_metric".to_string()));
// Test 6: Selector filters out everything
let patterns = vec!["bench_.*".to_string()];
let filters = crate::filter::compile_filters(&patterns).unwrap();
let selectors = vec![("os".to_string(), "windows".to_string())];
let discovered = discover_matching_measurements(&commits, &filters, &selectors);
assert_eq!(discovered.len(), 0);
// Test 7: Exact match with anchored regex (simulating -m argument)
let patterns = vec!["^bench_cpu$".to_string()];
let filters = crate::filter::compile_filters(&patterns).unwrap();
let selectors = vec![];
let discovered = discover_matching_measurements(&commits, &filters, &selectors);
assert_eq!(discovered.len(), 1);
assert!(discovered.contains(&"bench_cpu".to_string()));
// Test 8: Sorted output (verify deterministic ordering)
let patterns = vec![".*".to_string()]; // Match all
let filters = crate::filter::compile_filters(&patterns).unwrap();
let selectors = vec![];
let discovered = discover_matching_measurements(&commits, &filters, &selectors);
// Should be sorted alphabetically
assert_eq!(discovered[0], "bench_cpu");
assert_eq!(discovered[1], "bench_memory");
assert_eq!(discovered[2], "other_metric");
assert_eq!(discovered[3], "test_unit");
}
#[test]
fn test_audit_multiple_with_combined_patterns() {
// This test verifies that combining explicit measurements (-m) and filter patterns (--filter)
// works correctly with OR behavior. Both should be audited.
// Note: This is an integration test that uses actual audit_multiple function,
// but we can't easily test it without a real git repo, so we test the pattern combination
// and discovery logic instead.
use crate::data::{Commit, MeasurementData};
use std::collections::HashMap;
// Create mock commits
let commits = vec![Ok(Commit {
commit: "abc123".to_string(),
title: "test: commit".to_string(),
author: "Test Author".to_string(),
measurements: vec![
MeasurementData {
epoch: 0,
name: "timer".to_string(),
timestamp: 1000.0,
val: 10.0,
key_values: HashMap::new(),
},
MeasurementData {
epoch: 0,
name: "bench_cpu".to_string(),
timestamp: 1000.0,
val: 100.0,
key_values: HashMap::new(),
},
MeasurementData {
epoch: 0,
name: "memory".to_string(),
timestamp: 1000.0,
val: 500.0,
key_values: HashMap::new(),
},
],
})];
// Simulate combining -m timer with --filter "bench_.*"
// This is what combine_measurements_and_filters does in cli.rs
let measurements = vec!["timer".to_string()];
let filter_patterns = vec!["bench_.*".to_string()];
let combined =
crate::filter::combine_measurements_and_filters(&measurements, &filter_patterns);
// combined should have: ["^timer$", "bench_.*"]
assert_eq!(combined.len(), 2);
assert_eq!(combined[0], "^timer$");
assert_eq!(combined[1], "bench_.*");
// Now compile and discover
let filters = crate::filter::compile_filters(&combined).unwrap();
let selectors = vec![];
let discovered = discover_matching_measurements(&commits, &filters, &selectors);
// Should discover both timer (exact match) and bench_cpu (pattern match)
assert_eq!(discovered.len(), 2);
assert!(discovered.contains(&"timer".to_string()));
assert!(discovered.contains(&"bench_cpu".to_string()));
assert!(!discovered.contains(&"memory".to_string())); // Not in -m or filter
// Test with multiple explicit measurements and multiple filters
let measurements = vec!["timer".to_string(), "memory".to_string()];
let filter_patterns = vec!["bench_.*".to_string(), "test_.*".to_string()];
let combined =
crate::filter::combine_measurements_and_filters(&measurements, &filter_patterns);
assert_eq!(combined.len(), 4);
let filters = crate::filter::compile_filters(&combined).unwrap();
let discovered = discover_matching_measurements(&commits, &filters, &selectors);
// Should discover timer, memory, and bench_cpu (no test_* in commits)
assert_eq!(discovered.len(), 3);
assert!(discovered.contains(&"timer".to_string()));
assert!(discovered.contains(&"memory".to_string()));
assert!(discovered.contains(&"bench_cpu".to_string()));
}
#[test]
fn test_audit_with_empty_tail() {
// Test for division by zero bug when tail is empty
// This test reproduces the bug where tail_median is 0.0 when tail is empty,
// causing division by zero in sparkline calculation
let result = audit_with_data(
"test_measurement",
10.0, // head
vec![], // empty tail - triggers the bug
2, // min_count
2.0, // sigma
DispersionMethod::StandardDeviation,
ReductionFunc::Min,
);
// Should succeed and skip (not crash with division by zero)
assert!(result.is_ok(), "Should not crash on empty tail");
let audit_result = result.unwrap();
// Should be skipped due to insufficient measurements
assert!(audit_result.passed);
assert!(audit_result.message.contains("Skipping test"));
// The message should not contain inf or NaN
assert!(!audit_result.message.to_lowercase().contains("inf"));
assert!(!audit_result.message.to_lowercase().contains("nan"));
}
#[test]
fn test_audit_with_all_zero_tail() {
// Test for division by zero when all tail measurements are 0.0
// This tests the edge case where median is 0.0 even with measurements
let result = audit_with_data(
"test_measurement",
5.0, // non-zero head
vec![0.0, 0.0, 0.0], // all zeros in tail
2, // min_count
2.0, // sigma
DispersionMethod::StandardDeviation,
ReductionFunc::Min,
);
// Should succeed (not crash with division by zero)
assert!(result.is_ok(), "Should not crash when tail median is 0.0");
let audit_result = result.unwrap();
// The message should not contain inf or NaN
assert!(!audit_result.message.to_lowercase().contains("inf"));
assert!(!audit_result.message.to_lowercase().contains("nan"));
}
#[test]
fn test_tiered_baseline_approach() {
// Test the tiered approach:
// 1. Non-zero median → use median, show percentages
// 2. Zero median → show absolute values
// Case 1: Median is non-zero - use percentages (default behavior)
let result = audit_with_data(
"test_measurement",
15.0, // head
vec![10.0, 11.0, 12.0], // median=11.0 (non-zero)
2,
2.0,
DispersionMethod::StandardDeviation,
ReductionFunc::Min,
);
assert!(result.is_ok());
let audit_result = result.unwrap();
// Should use median as baseline and show percentage
assert!(audit_result.message.contains('%'));
assert!(!audit_result.message.to_lowercase().contains("inf"));
// Case 2: Median is zero with non-zero head - use absolute values
let result = audit_with_data(
"test_measurement",
5.0, // head (non-zero)
vec![0.0, 0.0, 0.0], // median=0
2,
2.0,
DispersionMethod::StandardDeviation,
ReductionFunc::Min,
);
assert!(result.is_ok());
let audit_result = result.unwrap();
// Should show absolute values instead of percentages
// The message should contain the sparkline but not percentage symbols
assert!(!audit_result.message.to_lowercase().contains("inf"));
assert!(!audit_result.message.to_lowercase().contains("nan"));
// Check that sparkline exists (contains the dash character)
assert!(audit_result.message.contains('–') || audit_result.message.contains('-'));
// Case 3: Everything is zero - show absolute values [0 - 0]
let result = audit_with_data(
"test_measurement",
0.0, // head
vec![0.0, 0.0, 0.0], // median=0
2,
2.0,
DispersionMethod::StandardDeviation,
ReductionFunc::Min,
);
assert!(result.is_ok());
let audit_result = result.unwrap();
// Should show absolute range [0 - 0]
assert!(!audit_result.message.to_lowercase().contains("inf"));
assert!(!audit_result.message.to_lowercase().contains("nan"));
}
#[test]
fn test_min_measurements_two_with_no_tail() {
// Test the minimum allowed min_measurements value (2) with no tail measurements.
// This should skip the audit since we have 0 < 2 tail measurements.
let result = audit_with_data(
"test_measurement",
15.0, // head
vec![], // no tail measurements
2, // min_count = 2 (minimum allowed by CLI)
2.0,
DispersionMethod::StandardDeviation,
ReductionFunc::Min,
);
assert!(result.is_ok());
let audit_result = result.unwrap();
// Should pass (skipped) since we have 0 < 2 tail measurements
assert!(audit_result.passed);
assert!(audit_result.message.contains("Skipping test"));
assert!(audit_result
.message
.contains("0 historical measurements found"));
assert!(audit_result
.message
.contains("Less than requested min_measurements of 2"));
// Should show Head summary only (total_measurements = 1)
assert!(audit_result.message.contains("Head:"));
assert!(!audit_result.message.contains("z-score"));
assert!(!audit_result.message.contains("Tail:"));
}
#[test]
fn test_min_measurements_two_with_single_tail() {
// Test the minimum allowed min_measurements value (2) with a single tail measurement.
// This should skip since we have 1 < 2 tail measurements.
let result = audit_with_data(
"test_measurement",
15.0, // head
vec![10.0], // single tail measurement
2, // min_count = 2 (minimum allowed by CLI)
2.0,
DispersionMethod::StandardDeviation,
ReductionFunc::Min,
);
assert!(result.is_ok());
let audit_result = result.unwrap();
// Should pass (skipped) since we have 1 < 2 tail measurements
assert!(audit_result.passed);
assert!(audit_result.message.contains("Skipping test"));
assert!(audit_result
.message
.contains("1 historical measurement found"));
assert!(audit_result
.message
.contains("Less than requested min_measurements of 2"));
// Should show both Head and Tail summaries with z-score (total_measurements = 2)
assert!(audit_result.message.contains("Head:"));
assert!(audit_result.message.contains("Tail:"));
assert!(audit_result.message.contains("z-score"));
assert!(audit_result.message.contains("["));
}
#[test]
fn test_aggregation_method_display_min() {
// Test that the aggregation method is displayed correctly with ReductionFunc::Min
let result = audit_with_data(
"test_measurement",
15.0,
vec![10.0, 11.0, 12.0],
2,
2.0,
DispersionMethod::StandardDeviation,
ReductionFunc::Min,
);
assert!(result.is_ok());
let audit_result = result.unwrap();
assert!(audit_result.message.contains("Aggregation: min"));
}
#[test]
fn test_aggregation_method_display_max() {
// Test that the aggregation method is displayed correctly with ReductionFunc::Max
let result = audit_with_data(
"test_measurement",
15.0,
vec![10.0, 11.0, 12.0],
2,
2.0,
DispersionMethod::StandardDeviation,
ReductionFunc::Max,
);
assert!(result.is_ok());
let audit_result = result.unwrap();
assert!(audit_result.message.contains("Aggregation: max"));
}
#[test]
fn test_aggregation_method_display_median() {
// Test that the aggregation method is displayed correctly with ReductionFunc::Median
let result = audit_with_data(
"test_measurement",
15.0,
vec![10.0, 11.0, 12.0],
2,
2.0,
DispersionMethod::StandardDeviation,
ReductionFunc::Median,
);
assert!(result.is_ok());
let audit_result = result.unwrap();
assert!(audit_result.message.contains("Aggregation: median"));
}
#[test]
fn test_aggregation_method_display_mean() {
// Test that the aggregation method is displayed correctly with ReductionFunc::Mean
let result = audit_with_data(
"test_measurement",
15.0,
vec![10.0, 11.0, 12.0],
2,
2.0,
DispersionMethod::StandardDeviation,
ReductionFunc::Mean,
);
assert!(result.is_ok());
let audit_result = result.unwrap();
assert!(audit_result.message.contains("Aggregation: mean"));
}
#[test]
fn test_aggregation_method_not_shown_with_single_measurement() {
// Test that aggregation method is NOT shown when there's only 1 measurement
let result = audit_with_data(
"test_measurement",
15.0,
vec![], // No tail measurements, total = 1
2,
2.0,
DispersionMethod::StandardDeviation,
ReductionFunc::Median,
);
assert!(result.is_ok());
let audit_result = result.unwrap();
// Should NOT show aggregation method (only 1 measurement total)
assert!(!audit_result.message.contains("Aggregation:"));
// But should show Head summary
assert!(audit_result.message.contains("Head:"));
}
}