ktstr 0.17.0

Test harness for Linux process schedulers
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
use super::*;

// -- Dimension / derive_slicing_dims / pairing dims --

/// `Dimension::ALL` lists all eight dims in canonical order.
/// Order matters for [`PairingKey::from_row`] and for header
/// rendering — a regression that reordered the slice would
/// silently shift every dynamic key, splitting previously-
/// paired rows. Pin the literal order.
#[test]
fn dimension_all_canonical_order() {
    assert_eq!(
        Dimension::ALL,
        &[
            Dimension::Kernel,
            Dimension::Scheduler,
            Dimension::Topology,
            Dimension::WorkType,
            Dimension::ProjectCommit,
            Dimension::KernelCommit,
            Dimension::RunSource,
            Dimension::CpuBudget,
        ],
    );
}

/// `Dimension::pairing_dims` returns every dim NOT in the
/// slicing set, preserving canonical order. Two slicing
/// orderings produce the same pairing-dim list (the function
/// iterates `ALL`, not `slicing`).
#[test]
fn dimension_pairing_dims_complements_slicing() {
    let pair = Dimension::pairing_dims(&[Dimension::Kernel, Dimension::ProjectCommit]);
    assert_eq!(
        pair,
        vec![
            Dimension::Scheduler,
            Dimension::Topology,
            Dimension::WorkType,
            Dimension::KernelCommit,
            Dimension::RunSource,
            Dimension::CpuBudget,
        ],
    );
    // Order of slicing input doesn't change the output —
    // the function iterates ALL and filters.
    let pair_reversed = Dimension::pairing_dims(&[Dimension::ProjectCommit, Dimension::Kernel]);
    assert_eq!(pair, pair_reversed);
}

/// Empty slicing set → every dim is a pairing dim.
#[test]
fn dimension_pairing_dims_empty_slicing_yields_all() {
    let pair = Dimension::pairing_dims(&[]);
    assert_eq!(pair, Dimension::ALL.to_vec());
}

/// `derive_slicing_dims` returns every dimension on which
/// filter_a and filter_b differ. Equal filters → empty
/// slicing.
#[test]
fn derive_slicing_dims_identical_filters_yields_empty() {
    let f = RowFilter {
        schedulers: vec!["scx_alpha".to_string()],
        ..RowFilter::default()
    };
    assert!(derive_slicing_dims(&f, &f).is_empty());
}

/// One-dim diff: only the differing dimension is reported.
#[test]
fn derive_slicing_dims_single_dim_diff() {
    let f_a = RowFilter {
        schedulers: vec!["scx_alpha".to_string()],
        ..RowFilter::default()
    };
    let f_b = RowFilter {
        schedulers: vec!["scx_beta".to_string()],
        ..RowFilter::default()
    };
    assert_eq!(derive_slicing_dims(&f_a, &f_b), vec![Dimension::Scheduler]);
}

/// Vec dims (kernels/commits) compare as sorted-deduped sets —
/// order and duplicates inside the filter don't shift the
/// slicing-dim derivation.
#[test]
fn derive_slicing_dims_vec_compares_as_set() {
    let f_a = RowFilter {
        kernels: vec!["6.14".to_string(), "6.15".to_string()],
        ..RowFilter::default()
    };
    let f_b = RowFilter {
        kernels: vec!["6.15".to_string(), "6.14".to_string(), "6.14".to_string()],
        ..RowFilter::default()
    };
    assert!(
        derive_slicing_dims(&f_a, &f_b).is_empty(),
        "same set in different order/multiplicity must NOT slice",
    );
}

/// Multi-dim diff: every differing dimension is reported, in
/// canonical [`Dimension::ALL`] order.
#[test]
fn derive_slicing_dims_multi_dim_diff_in_canonical_order() {
    let f_a = RowFilter {
        kernels: vec!["6.14".to_string()],
        schedulers: vec!["scx_alpha".to_string()],
        ..RowFilter::default()
    };
    let f_b = RowFilter {
        kernels: vec!["6.15".to_string()],
        schedulers: vec!["scx_beta".to_string()],
        ..RowFilter::default()
    };
    assert_eq!(
        derive_slicing_dims(&f_a, &f_b),
        vec![Dimension::Kernel, Dimension::Scheduler],
    );
}

/// Source-only diff: filters that disagree on `run_sources`
/// and agree on every other dimension produce a slicing-dim
/// set containing exactly `Dimension::RunSource`. Pins the
/// Source arm of the per-dimension comparison switch in
/// [`derive_slicing_dims`] — a regression that omitted the
/// arm or compared the wrong field would surface here as an
/// empty slicing-dim set (and downstream as a `compare`
/// command that mistakenly bails with "A and B select
/// identical rows" on a legitimate source contrast).
#[test]
fn derive_slicing_dims_source_only_diff() {
    let f_a = RowFilter {
        run_sources: vec!["local".to_string()],
        ..RowFilter::default()
    };
    let f_b = RowFilter {
        run_sources: vec!["ci".to_string()],
        ..RowFilter::default()
    };
    assert_eq!(
        derive_slicing_dims(&f_a, &f_b),
        vec![Dimension::RunSource],
        "differing `run_sources` must surface Source as a slicing dim",
    );

    // Sorted-deduped Vec semantics also apply on the Source
    // dim — same set in different order/multiplicity must NOT
    // slice. Mirrors the `derive_slicing_dims_vec_compares_as_set`
    // contract for the Source arm.
    let f_c = RowFilter {
        run_sources: vec!["local".to_string(), "ci".to_string()],
        ..RowFilter::default()
    };
    let f_d = RowFilter {
        run_sources: vec!["ci".to_string(), "local".to_string(), "local".to_string()],
        ..RowFilter::default()
    };
    assert!(
        derive_slicing_dims(&f_c, &f_d).is_empty(),
        "same run_source set in different order/multiplicity must NOT slice",
    );
}

/// Topology-only diff: filters that disagree on `topologies`
/// and agree on every other dimension produce a slicing-dim
/// set containing exactly `Dimension::Topology`. Pins the
/// Topology arm of the per-dimension comparison switch in
/// [`derive_slicing_dims`] for the post-Vec-promotion
/// `topologies` field; before promotion `--topology` was a
/// single-value `Option<String>` and the per-arm comparison
/// shape was `Option<String> != Option<String>`. Mirror of
/// `derive_slicing_dims_source_only_diff` for the Topology
/// arm.
#[test]
fn derive_slicing_dims_topology_only_diff() {
    let f_a = RowFilter {
        topologies: vec!["1n2l4c1t".to_string()],
        ..RowFilter::default()
    };
    let f_b = RowFilter {
        topologies: vec!["1n2l4c2t".to_string()],
        ..RowFilter::default()
    };
    assert_eq!(
        derive_slicing_dims(&f_a, &f_b),
        vec![Dimension::Topology],
        "differing `topologies` must surface Topology as a slicing dim",
    );

    // Sorted-deduped Vec semantics: same set in different
    // order/multiplicity must NOT slice.
    let f_c = RowFilter {
        topologies: vec!["1n2l4c1t".to_string(), "1n2l4c2t".to_string()],
        ..RowFilter::default()
    };
    let f_d = RowFilter {
        topologies: vec![
            "1n2l4c2t".to_string(),
            "1n2l4c1t".to_string(),
            "1n2l4c1t".to_string(),
        ],
        ..RowFilter::default()
    };
    assert!(
        derive_slicing_dims(&f_c, &f_d).is_empty(),
        "same topology set in different order/multiplicity must NOT slice",
    );
}

/// WorkType-only diff: filters that disagree on `work_types`
/// and agree on every other dimension produce a slicing-dim
/// set containing exactly `Dimension::WorkType`. Mirror of
/// `derive_slicing_dims_topology_only_diff` for the WorkType
/// arm.
#[test]
fn derive_slicing_dims_work_type_only_diff() {
    let f_a = RowFilter {
        work_types: vec!["SpinWait".to_string()],
        ..RowFilter::default()
    };
    let f_b = RowFilter {
        work_types: vec!["PageFaultChurn".to_string()],
        ..RowFilter::default()
    };
    assert_eq!(
        derive_slicing_dims(&f_a, &f_b),
        vec![Dimension::WorkType],
        "differing `work_types` must surface WorkType as a slicing dim",
    );

    // Sorted-deduped Vec semantics: same set in different
    // order/multiplicity must NOT slice.
    let f_c = RowFilter {
        work_types: vec!["SpinWait".to_string(), "PageFaultChurn".to_string()],
        ..RowFilter::default()
    };
    let f_d = RowFilter {
        work_types: vec![
            "PageFaultChurn".to_string(),
            "SpinWait".to_string(),
            "SpinWait".to_string(),
        ],
        ..RowFilter::default()
    };
    assert!(
        derive_slicing_dims(&f_c, &f_d).is_empty(),
        "same work_type set in different order/multiplicity must NOT slice",
    );
}

/// `kernel_filter_matches`: major.minor (`6.12`) prefix
/// matches every patch in the series via the
/// `starts_with("6.12.")` arm, and ALSO matches `6.12`
/// exactly. Three-segment-or-longer filters are strict.
#[test]
fn kernel_filter_matches_major_minor_prefix() {
    // Two-segment filter: prefix matches.
    assert!(kernel_filter_matches("6.12", "6.12"));
    assert!(kernel_filter_matches("6.12", "6.12.0"));
    assert!(kernel_filter_matches("6.12", "6.12.5"));
    assert!(!kernel_filter_matches("6.12", "6.13.0"));
    // Critically: `6.1` must not match `6.10.0` — the
    // trailing-dot in the prefix path prevents the
    // accidental wildcard.
    assert!(!kernel_filter_matches("6.1", "6.10.0"));
}

/// `kernel_filter_matches`: major.minor prefix admits the
/// `MAJOR.MINOR-rcN` pre-release shape via the
/// `starts_with("MAJOR.MINOR-")` arm. The rcN kernel shares
/// the `(major, minor, patch=0)` tuple with the eventual
/// release per `kernel_path::decompose_version_for_compare`,
/// and the operator filtering on `6.14` wants the whole
/// series — release AND pre-releases. This complements the
/// `6.14.0-rc3` (kernel-banner) shape which already matched
/// via the trailing-dot prefix.
#[test]
fn kernel_filter_matches_major_minor_admits_rc_pre_release() {
    // No-patch pre-release shape (kernel_path KernelId::Version
    // doc cites `6.15-rc3` as a valid version string).
    assert!(kernel_filter_matches("6.14", "6.14-rc3"));
    assert!(kernel_filter_matches("6.14", "6.14-rc1"));
    // Patch+rc shape (kernel banner from a kernel.org
    // `v6.14-rc3` tag is `Linux version 6.14.0-rc3+`).
    assert!(kernel_filter_matches("6.14", "6.14.0-rc3"));
    assert!(kernel_filter_matches("6.14", "6.14.0-rc3+"));
    // The dash-prefix arm must NOT wildcard across series:
    // `6.1` filtering must reject `6.14-rc3` for the same
    // reason `6.1` rejects `6.10.0`.
    assert!(!kernel_filter_matches("6.1", "6.14-rc3"));
    // Cross-minor rc rejection.
    assert!(!kernel_filter_matches("6.14", "6.15-rc3"));
}

/// `kernel_filter_matches`: three-segment+ filters are strict
/// equality.
#[test]
fn kernel_filter_matches_strict_for_three_plus_segments() {
    assert!(kernel_filter_matches("6.14.2", "6.14.2"));
    // Critically: `6.14.2` must NOT match `6.14.20` — the
    // strict-equality arm prevents the patch-level prefix
    // wildcarding.
    assert!(!kernel_filter_matches("6.14.2", "6.14.20"));
    assert!(!kernel_filter_matches("6.14.2", "6.14.21"));
    // RC suffixes are also strict.
    assert!(kernel_filter_matches("6.15-rc3", "6.15-rc3"));
    assert!(!kernel_filter_matches("6.15-rc3", "6.15-rc30"));
}

/// `RowFilter::matches` with a major.minor `--kernel` filter
/// admits the row whose `kernel_version` is a patch in that
/// series.
#[test]
fn row_filter_kernel_major_minor_prefix_admits_patch_version() {
    let row = make_filter_row("t", "scx_a", "1n2l4c1t", "SpinWait", Some("6.12.5"));
    let filter = RowFilter {
        kernels: vec!["6.12".to_string()],
        ..RowFilter::default()
    };
    assert!(
        filter.matches(&row),
        "major.minor filter `6.12` must admit row with kernel_version `6.12.5`",
    );
}

// -- PairingKey --

/// `PairingKey::from_row` always puts `scenario` first, then
/// the requested dims in canonical order. Two rows with the
/// same scenario+dims agree; one with a different topology
/// (when topology IS a pairing dim) does not.
#[test]
fn pairing_key_from_row_basic() {
    let row_a = make_filter_row("scenA", "scx_a", "1n1l", "SpinWait", Some("6.14"));
    let row_b = make_filter_row("scenA", "scx_a", "1n1l", "SpinWait", Some("6.14"));
    let row_c = make_filter_row("scenA", "scx_a", "2n2l", "SpinWait", Some("6.14"));
    let dims = &[Dimension::Topology, Dimension::WorkType];
    assert_eq!(
        PairingKey::from_row(&row_a, dims),
        PairingKey::from_row(&row_b, dims),
    );
    assert_ne!(
        PairingKey::from_row(&row_a, dims),
        PairingKey::from_row(&row_c, dims),
        "different topology must distinguish the keys when topology is a pairing dim",
    );
}

/// Slicing on topology means topology is NOT in the pairing
/// dim set — so two rows that differ ONLY on topology pair
/// to the same key, allowing the comparison to contrast
/// them across A/B sides.
#[test]
fn pairing_key_excludes_slicing_dim() {
    let row_a = make_filter_row("scenA", "scx_a", "1n1l", "SpinWait", Some("6.14"));
    let row_b = make_filter_row("scenA", "scx_a", "2n2l", "SpinWait", Some("6.14"));
    // Pairing dims = ALL minus Topology. So these two rows
    // pair iff they agree on everything BUT topology.
    let pair_dims = Dimension::pairing_dims(&[Dimension::Topology]);
    assert_eq!(
        PairingKey::from_row(&row_a, &pair_dims),
        PairingKey::from_row(&row_b, &pair_dims),
        "rows differing only on a slicing dim must produce equal pairing keys",
    );
}

/// `PairingKey::from_row` first slot is always scenario;
/// rendering via `parts.join("/")` reproduces the
/// `scenario/topology/work_type` shape when those dims are
/// pairing dims.
#[test]
fn pairing_key_join_renders_legacy_shape() {
    let row = make_filter_row("test_a", "scx_a", "1n2l", "SpinWait", Some("6.14"));
    let key = PairingKey::from_row(&row, LEGACY_PAIRING_DIMS);
    assert_eq!(
        key.0.join("/"),
        "test_a/1n2l/SpinWait",
        "legacy-shape join must render the three-segment label",
    );
}

/// `PairingKey::from_row` includes the row's `kernel_commit`
/// when `KernelCommit` is in the pairing-dim list, and
/// excludes it when `KernelCommit` is the slicing dim. Pins
/// the [`Dimension::KernelCommit`] arm of the from_row match
/// — a regression that omitted the arm or substituted the
/// wrong row field would surface here as either a missing
/// key slot or a slot carrying the wrong value.
///
/// `None` kernel_commit renders as the empty string slot per
/// the `unwrap_or_default()` policy on Option dims; that
/// shape is shared across every Option-typed dim arm.
#[test]
fn pairing_key_from_row_includes_kernel_commit_when_pairing() {
    let mut row_some = make_filter_row("scn", "scx_a", "1n1l", "SpinWait", Some("6.14"));
    row_some.kernel_commit = Some("kabcde7".to_string());
    let mut row_none = make_filter_row("scn", "scx_a", "1n1l", "SpinWait", Some("6.14"));
    row_none.kernel_commit = None;

    // KernelCommit in pairing dims → key carries the commit
    // value (or the empty slot for None). The two rows
    // therefore produce DIFFERENT keys because their
    // kernel_commit values disagree.
    let pair_dims = &[Dimension::KernelCommit];
    let key_some = PairingKey::from_row(&row_some, pair_dims);
    let key_none = PairingKey::from_row(&row_none, pair_dims);
    assert_eq!(
        key_some.0,
        vec!["scn".to_string(), "kabcde7".to_string()],
        "Some(kernel_commit) must occupy the second slot verbatim",
    );
    assert_eq!(
        key_none.0,
        vec!["scn".to_string(), String::new()],
        "None kernel_commit must collapse to an empty slot per \
             unwrap_or_default policy",
    );
    assert_ne!(
        key_some, key_none,
        "two rows differing on kernel_commit must produce \
             distinct pairing keys when KernelCommit is a pairing dim",
    );

    // KernelCommit excluded (slicing) → the two rows pair to
    // the same key because the dim is dropped. Pins the
    // dimensional-slicing semantic for the new arm.
    let slice_dims = Dimension::pairing_dims(&[Dimension::KernelCommit]);
    assert_eq!(
        PairingKey::from_row(&row_some, &slice_dims),
        PairingKey::from_row(&row_none, &slice_dims),
        "rows differing only on the slicing dim (KernelCommit) \
             must produce equal pairing keys",
    );
}

/// `PairingKey::from_row` includes the row's `run_source`
/// when `RunSource` is in the pairing-dim list, and excludes it
/// when `RunSource` is the slicing dim. Pins the
/// [`Dimension::RunSource`] arm of the from_row match — same
/// shape and motivation as
/// `pairing_key_from_row_includes_kernel_commit_when_pairing`
/// but for the run_source arm. A regression that omitted the
/// arm or substituted `row.kernel_commit` for
/// `row.run_source` would surface here.
#[test]
fn pairing_key_from_row_includes_run_source_when_pairing() {
    let mut row_local = make_filter_row("scn", "scx_a", "1n1l", "SpinWait", Some("6.14"));
    row_local.run_source = Some("local".to_string());
    let mut row_ci = make_filter_row("scn", "scx_a", "1n1l", "SpinWait", Some("6.14"));
    row_ci.run_source = Some("ci".to_string());
    let mut row_none = make_filter_row("scn", "scx_a", "1n1l", "SpinWait", Some("6.14"));
    row_none.run_source = None;

    let pair_dims = &[Dimension::RunSource];
    let key_local = PairingKey::from_row(&row_local, pair_dims);
    let key_ci = PairingKey::from_row(&row_ci, pair_dims);
    let key_none = PairingKey::from_row(&row_none, pair_dims);
    assert_eq!(
        key_local.0,
        vec!["scn".to_string(), "local".to_string()],
        "Some(run_source) must occupy the second slot verbatim",
    );
    assert_eq!(key_ci.0, vec!["scn".to_string(), "ci".to_string()]);
    assert_eq!(
        key_none.0,
        vec!["scn".to_string(), String::new()],
        "None run_source must collapse to an empty slot per \
             unwrap_or_default policy",
    );
    assert_ne!(
        key_local, key_ci,
        "two rows differing on run_source must produce \
             distinct pairing keys when Source is a pairing dim",
    );

    // Source excluded (slicing) → the differing-run_source
    // rows pair to the same key.
    let slice_dims = Dimension::pairing_dims(&[Dimension::RunSource]);
    assert_eq!(
        PairingKey::from_row(&row_local, &slice_dims),
        PairingKey::from_row(&row_ci, &slice_dims),
        "rows differing only on the slicing dim (Source) must \
             produce equal pairing keys",
    );
}

/// `PairingKey::from_row` includes the row's cpu_budget when CpuBudget
/// is a pairing dim — so cross-budget rows NEVER pair — and excludes
/// it when CpuBudget is the slicing dim. This is the whole point of
/// A 4-CPU-budget run and a 32-CPU-budget run measure different
/// things and must not be silently compared.
#[test]
fn pairing_key_from_row_includes_cpu_budget_when_pairing() {
    let mut row_4 = make_filter_row("scn", "scx_a", "1n1l", "SpinWait", Some("6.14"));
    row_4.cpu_budget = Some(4);
    let mut row_32 = make_filter_row("scn", "scx_a", "1n1l", "SpinWait", Some("6.14"));
    row_32.cpu_budget = Some(32);
    let mut row_none = make_filter_row("scn", "scx_a", "1n1l", "SpinWait", Some("6.14"));
    row_none.cpu_budget = None;

    let pair_dims = &[Dimension::CpuBudget];
    let key_4 = PairingKey::from_row(&row_4, pair_dims);
    let key_32 = PairingKey::from_row(&row_32, pair_dims);
    let key_none = PairingKey::from_row(&row_none, pair_dims);
    assert_eq!(key_4.0, vec!["scn".to_string(), "4".to_string()]);
    assert_eq!(key_32.0, vec!["scn".to_string(), "32".to_string()]);
    assert_eq!(
        key_none.0,
        vec!["scn".to_string(), String::new()],
        "None cpu_budget (a skip) collapses to an empty slot",
    );
    assert_ne!(
        key_4, key_32,
        "rows of different cpu_budget must NOT pair when CpuBudget pairs",
    );
    assert_ne!(
        key_4, key_none,
        "a budgeted row must not pair with a skip (None budget)",
    );

    // CpuBudget sliced → the dim is dropped → the two budgets pair.
    let slice_dims = Dimension::pairing_dims(&[Dimension::CpuBudget]);
    assert_eq!(
        PairingKey::from_row(&row_4, &slice_dims),
        PairingKey::from_row(&row_32, &slice_dims),
        "rows differing only on the sliced dim (CpuBudget) must pair",
    );
}

/// Clean and dirty contributors at the same canonical hex
/// must land in the same pairing bucket. Without the
/// `-dirty` strip in `commit_pairing_key_part`, `abc1234`
/// and `abc1234-dirty` shatter into separate groups,
/// defeating `group_and_average_by`'s `+mixed` cohort
/// detection (which can only fire when the two contributors
/// land in ONE group).
#[test]
fn pairing_key_from_row_strips_dirty_suffix_on_commit() {
    let mut row_clean = make_filter_row("scn", "scx_a", "1n1l", "SpinWait", Some("6.14"));
    row_clean.commit = Some("abc1234".to_string());
    let mut row_dirty = make_filter_row("scn", "scx_a", "1n1l", "SpinWait", Some("6.14"));
    row_dirty.commit = Some("abc1234-dirty".to_string());

    let pair_dims = &[Dimension::ProjectCommit];
    let key_clean = PairingKey::from_row(&row_clean, pair_dims);
    let key_dirty = PairingKey::from_row(&row_dirty, pair_dims);

    assert_eq!(
        key_clean, key_dirty,
        "clean `abc1234` and dirty `abc1234-dirty` must produce \
             EQUAL pairing keys so the +mixed cohort machinery in \
             group_and_average_by can surface their disagreement",
    );
    assert_eq!(
        key_clean.0,
        vec!["scn".to_string(), "abc1234".to_string()],
        "key part must be the canonical un-suffixed hex",
    );
}

/// Same shape on the kernel_commit dimension. Pins the
/// second commit dim's strip independently because
/// `from_row` uses two parallel arms; a regression could
/// strip one but not the other.
#[test]
fn pairing_key_from_row_strips_dirty_suffix_on_kernel_commit() {
    let mut row_clean = make_filter_row("scn", "scx_a", "1n1l", "SpinWait", Some("6.14"));
    row_clean.kernel_commit = Some("def5678".to_string());
    let mut row_dirty = make_filter_row("scn", "scx_a", "1n1l", "SpinWait", Some("6.14"));
    row_dirty.kernel_commit = Some("def5678-dirty".to_string());

    let pair_dims = &[Dimension::KernelCommit];
    let key_clean = PairingKey::from_row(&row_clean, pair_dims);
    let key_dirty = PairingKey::from_row(&row_dirty, pair_dims);

    assert_eq!(
        key_clean, key_dirty,
        "clean and dirty kernel_commit at the same canonical \
             hex must pair together",
    );
    assert_eq!(key_clean.0, vec!["scn".to_string(), "def5678".to_string()],);
}

/// Distinct hexes still differentiate even when one carries
/// `-dirty`. Pins that the strip operates ONLY on the
/// suffix, not on the entire value — `aaa1111-dirty` and
/// `bbb2222` remain distinct.
#[test]
fn pairing_key_from_row_distinct_hexes_remain_distinct_under_strip() {
    let mut row_a = make_filter_row("scn", "scx_a", "1n1l", "SpinWait", Some("6.14"));
    row_a.commit = Some("aaa1111-dirty".to_string());
    let mut row_b = make_filter_row("scn", "scx_a", "1n1l", "SpinWait", Some("6.14"));
    row_b.commit = Some("bbb2222".to_string());

    let pair_dims = &[Dimension::ProjectCommit];
    let key_a = PairingKey::from_row(&row_a, pair_dims);
    let key_b = PairingKey::from_row(&row_b, pair_dims);

    assert_ne!(
        key_a, key_b,
        "distinct canonical hexes must remain distinct after the \
             -dirty strip — only the suffix is stripped",
    );
    assert_eq!(key_a.0[1], "aaa1111");
    assert_eq!(key_b.0[1], "bbb2222");
}

/// `None` commit values still collapse to the empty slot
/// (the strip is a no-op on `None`). Pins the absence path
/// against a regression that special-cased the strip and
/// inadvertently changed the unwrap_or_default behavior.
#[test]
fn pairing_key_from_row_none_commit_unchanged_under_strip() {
    let mut row = make_filter_row("scn", "scx_a", "1n1l", "SpinWait", Some("6.14"));
    row.commit = None;
    row.kernel_commit = None;
    let pair_dims = &[Dimension::ProjectCommit, Dimension::KernelCommit];
    let key = PairingKey::from_row(&row, pair_dims);
    assert_eq!(
        key.0,
        vec!["scn".to_string(), String::new(), String::new()],
        "None commit and None kernel_commit must collapse to empty slots",
    );
}

// -- render_side_label --

/// Empty slicing dims → the bare label is returned.
#[test]
fn render_side_label_empty_dims_yields_bare() {
    let f = RowFilter::default();
    assert_eq!(render_side_label(&f, &[], "A"), "A");
}

/// Single-dim single-value scheduler renders the value
/// verbatim. After the Vec promotion of `--scheduler` the
/// scheduler arm goes through `render_vec_dim` like every
/// other Vec dim; a single entry still surfaces the bare
/// string.
#[test]
fn render_side_label_single_value_dim() {
    let f = RowFilter {
        schedulers: vec!["scx_rusty".to_string()],
        ..RowFilter::default()
    };
    assert_eq!(
        render_side_label(&f, &[Dimension::Scheduler], "A"),
        "scx_rusty",
    );
}

/// Vec dim with ≤3 entries joins with `|` (sorted).
#[test]
fn render_side_label_vec_dim_short_joins_with_pipe() {
    let f = RowFilter {
        kernels: vec!["6.15".to_string(), "6.14".to_string()],
        ..RowFilter::default()
    };
    assert_eq!(
        render_side_label(&f, &[Dimension::Kernel], "A"),
        "6.14|6.15",
        "≤3 values must join sorted with `|`",
    );
}

/// Vec dim with >3 entries collapses to the bare label.
#[test]
fn render_side_label_vec_dim_long_collapses_to_bare() {
    let f = RowFilter {
        kernels: vec![
            "6.10".to_string(),
            "6.11".to_string(),
            "6.12".to_string(),
            "6.13".to_string(),
            "6.14".to_string(),
        ],
        ..RowFilter::default()
    };
    assert_eq!(
        render_side_label(&f, &[Dimension::Kernel], "A"),
        "A",
        ">3 values must collapse to the bare letter so the \
             column header stays readable",
    );
}

/// Multi-dim slicing joins per-dim parts with `:`.
#[test]
fn render_side_label_multi_dim_joins_with_colon() {
    let f = RowFilter {
        kernels: vec!["6.14".to_string()],
        schedulers: vec!["scx_rusty".to_string()],
        ..RowFilter::default()
    };
    assert_eq!(
        render_side_label(&f, &[Dimension::Kernel, Dimension::Scheduler], "A"),
        "6.14:scx_rusty",
    );
}

/// Empty per-side filter on a slicing dim falls back to the
/// bare label (the slice exists because the OTHER side
/// populated the dim).
#[test]
fn render_side_label_empty_dim_value_uses_bare() {
    let f = RowFilter::default();
    assert_eq!(
        render_side_label(&f, &[Dimension::Kernel], "B"),
        "B",
        "empty Vec dim must fall back to the bare letter",
    );
    assert_eq!(
        render_side_label(&f, &[Dimension::Scheduler], "B"),
        "B",
        "None Option dim must fall back to the bare letter",
    );
}

/// `Dimension::KernelCommit` arm of [`render_side_label`] reads
/// `filter.kernel_commits` (a Vec) and routes through the same
/// `render_vec_dim` path as `Kernel` / `ProjectCommit`. Pins
/// the arm so a regression that omitted it (or substituted the
/// wrong field, e.g. `filter.project_commits`) surfaces here
/// instead of silently rendering the bare label even when the
/// filter is populated.
///
/// Single-value: emits the value verbatim. Two-value: joins
/// sorted with `|` per `render_vec_dim`'s ≤3 rule. >3 values:
/// collapse to bare. Empty Vec: bare. Same shape as the
/// `Kernel` arm pinned above; a regression in the
/// `KernelCommit` arm specifically would NOT be caught by the
/// existing `render_side_label_vec_dim_*` tests because those
/// only exercise the `Kernel` field.
#[test]
fn render_side_label_kernel_commit_arm_renders_filter_value() {
    let f_one = RowFilter {
        kernel_commits: vec!["kabcde7".to_string()],
        ..RowFilter::default()
    };
    assert_eq!(
        render_side_label(&f_one, &[Dimension::KernelCommit], "A"),
        "kabcde7",
        "single kernel_commit value must render verbatim — \
             a regression that read `filter.project_commits` instead of \
             `filter.kernel_commits` would render `A` here because \
             the project-commit field is empty",
    );

    let f_two = RowFilter {
        kernel_commits: vec!["kbbb222".to_string(), "kaaa111".to_string()],
        ..RowFilter::default()
    };
    assert_eq!(
        render_side_label(&f_two, &[Dimension::KernelCommit], "A"),
        "kaaa111|kbbb222",
        "≤3 kernel_commit values must join sorted with `|`",
    );

    let f_long = RowFilter {
        kernel_commits: vec![
            "k111".to_string(),
            "k222".to_string(),
            "k333".to_string(),
            "k444".to_string(),
        ],
        ..RowFilter::default()
    };
    assert_eq!(
        render_side_label(&f_long, &[Dimension::KernelCommit], "A"),
        "A",
        ">3 kernel_commit values must collapse to the bare letter",
    );

    let f_empty = RowFilter::default();
    assert_eq!(
        render_side_label(&f_empty, &[Dimension::KernelCommit], "B"),
        "B",
        "empty kernel_commits Vec must fall back to the bare letter",
    );
}

/// `Dimension::RunSource` arm of [`render_side_label`] reads
/// `filter.run_sources` (a Vec) and routes through the same
/// `render_vec_dim` path as the other Vec dims. Mirror of
/// `render_side_label_kernel_commit_arm_renders_filter_value`
/// for the Source arm. A regression that omitted the Source
/// arm or substituted the wrong field would surface here
/// instead of silently rendering the bare label even when
/// the filter is populated.
#[test]
fn render_side_label_source_arm_renders_filter_value() {
    let f_one = RowFilter {
        run_sources: vec!["local".to_string()],
        ..RowFilter::default()
    };
    assert_eq!(
        render_side_label(&f_one, &[Dimension::RunSource], "A"),
        "local",
        "single run_source value must render verbatim — a \
             regression that read another field would render `A` here",
    );

    let f_two = RowFilter {
        run_sources: vec!["local".to_string(), "ci".to_string()],
        ..RowFilter::default()
    };
    assert_eq!(
        render_side_label(&f_two, &[Dimension::RunSource], "A"),
        "ci|local",
        "≤3 run_source values must join sorted with `|`",
    );

    let f_long = RowFilter {
        run_sources: vec![
            "a".to_string(),
            "b".to_string(),
            "c".to_string(),
            "d".to_string(),
        ],
        ..RowFilter::default()
    };
    assert_eq!(
        render_side_label(&f_long, &[Dimension::RunSource], "A"),
        "A",
        ">3 run_source values must collapse to the bare letter",
    );

    let f_empty = RowFilter::default();
    assert_eq!(
        render_side_label(&f_empty, &[Dimension::RunSource], "B"),
        "B",
        "empty run_sources Vec must fall back to the bare letter",
    );
}

/// `zero_match_diagnostic` flags a `--run-source` value that is
/// not present in the pool, naming the unknown value AND the
/// distinct values actually seen. Guards against the
/// typo-class miss (e.g. `--run-source loca` for `local`,
/// `--run-source CI` for `ci`) that produces a silent
/// zero-match in `compare_partitions`.
#[test]
fn zero_match_diagnostic_unknown_run_source_lists_present_values() {
    let mut row_local = make_row("scn", "1n1l1c1t", true, 1.0);
    row_local.run_source = Some("local".to_string());
    let mut row_ci = make_row("scn", "1n1l1c1t", true, 1.0);
    row_ci.run_source = Some("ci".to_string());
    let rows = vec![row_local, row_ci];
    let filter = RowFilter {
        run_sources: vec!["loca".to_string()],
        ..Default::default()
    };

    let msg = zero_match_diagnostic("A", &filter, &rows, rows.len());

    assert!(
        msg.contains("--run-source `loca` not found"),
        "must name the unknown value verbatim; got:\n{msg}",
    );
    assert!(
        msg.contains("`ci`") && msg.contains("`local`"),
        "must list distinct values present in the pool so the \
             operator can correct the typo; got:\n{msg}",
    );
    assert!(
        msg.contains("case-sensitive"),
        "must mention case sensitivity (`ci` ≠ `CI`); got:\n{msg}",
    );
}

/// `zero_match_diagnostic` flags a `--cpu-budget` value not present
/// in the pool, naming the unknown value AND the distinct budgets
/// actually seen — the numeric mirror of the run_source hint. Skip
/// rows (`cpu_budget: None`) contribute no budget to the list.
#[test]
fn zero_match_diagnostic_unknown_cpu_budget_lists_present_values() {
    let mut row4 = make_row("scn", "1n1l1c1t", true, 1.0);
    row4.cpu_budget = Some(4);
    let mut row32 = make_row("scn", "1n1l1c1t", true, 1.0);
    row32.cpu_budget = Some(32);
    let skip = make_row("scn", "1n1l1c1t", true, 1.0); // cpu_budget None
    let rows = vec![row4, row32, skip];
    let filter = RowFilter {
        cpu_budgets: vec!["64".to_string()],
        ..Default::default()
    };

    let msg = zero_match_diagnostic("A", &filter, &rows, rows.len());

    assert!(
        msg.contains("--cpu-budget `64` not found"),
        "must name the unknown budget verbatim; got:\n{msg}",
    );
    assert!(
        msg.contains("`4`") && msg.contains("`32`"),
        "must list distinct budgets present in the pool; got:\n{msg}",
    );
}

/// A `--cpu-budget` value that DOES match a row must NOT trigger
/// the unknown-budget hint (guards against the hint firing for
/// every populated `--cpu-budget` regardless of pool membership).
#[test]
fn zero_match_diagnostic_known_cpu_budget_does_not_fire_unknown_hint() {
    let mut row = make_row("scn", "1n1l1c1t", true, 1.0);
    row.cpu_budget = Some(4);
    let rows = vec![row];
    let filter = RowFilter {
        cpu_budgets: vec!["4".to_string()],
        ..Default::default()
    };

    let msg = zero_match_diagnostic("A", &filter, &rows, rows.len());

    assert!(
        !msg.contains("--cpu-budget `4` not found"),
        "a present budget must not fire the unknown-budget hint; got:\n{msg}",
    );
}

/// When every row has `run_source: None`, the hint surfaces the
/// "(none — every row has `run_source: null`)" form rather than
/// an empty list. This is the post-`apply_archive_source_override`
/// path with a pool that pre-dates the run_source field, so
/// distinguishing "unknown value, no values present" from
/// "unknown value, here's what's there" is operator-actionable.
#[test]
fn zero_match_diagnostic_unknown_run_source_with_empty_pool_explains_absence() {
    let row = make_row("scn", "1n1l1c1t", true, 1.0);
    let rows = vec![row];
    let filter = RowFilter {
        run_sources: vec!["ci".to_string()],
        ..Default::default()
    };

    let msg = zero_match_diagnostic("A", &filter, &rows, rows.len());

    assert!(
        msg.contains("--run-source `ci` not found"),
        "must name the unknown value; got:\n{msg}",
    );
    assert!(
        msg.contains("none — every row has `run_source: null`"),
        "must explain the empty-distinct-values case rather than \
             listing nothing; got:\n{msg}",
    );
}

/// A `--run-source` value that DOES match a row in the pool
/// must NOT trigger the unknown-value hint, even when the
/// filter still matches zero rows due to other dimension
/// mismatches (e.g. scenario filter zeroes the set first).
/// Pinning this guards against a regression where the hint
/// fires for every populated `--run-source` regardless of
/// pool membership.
#[test]
fn zero_match_diagnostic_known_run_source_does_not_fire_unknown_hint() {
    let mut row = make_row("scn", "1n1l1c1t", true, 1.0);
    row.run_source = Some("local".to_string());
    let rows = vec![row];
    let filter = RowFilter {
        run_sources: vec!["local".to_string()],
        ..Default::default()
    };

    let msg = zero_match_diagnostic("A", &filter, &rows, rows.len());

    assert!(
        !msg.contains("--run-source") || !msg.contains("not found"),
        "must NOT fire the unknown-source hint when the value is \
             present in the pool; got:\n{msg}",
    );
}

/// `zero_match_diagnostic` fires the dirty-form hint for a
/// `--project-commit X` filter when the pool contains a
/// matching `X-dirty` row — pointing the operator at the
/// dirty form so they don't have to manually scan
/// `stats list-values`. The hint must name the original
/// value, the dirty form, and the suggested replacement
/// flag form.
#[test]
fn zero_match_diagnostic_project_commit_dirty_hint_fires() {
    let mut row = make_row("scn", "1n1l1c1t", true, 1.0);
    row.commit = Some("abcdef1-dirty".to_string());
    let rows = vec![row];
    let filter = RowFilter {
        project_commits: vec!["abcdef1".to_string()],
        ..Default::default()
    };

    let msg = zero_match_diagnostic("A", &filter, &rows, rows.len());

    assert!(
        msg.contains("no rows match `--project-commit abcdef1`"),
        "hint must name the unmatched filter value verbatim; \
             got:\n{msg}",
    );
    assert!(
        msg.contains("`abcdef1-dirty` exists in the pool"),
        "hint must surface the dirty form found in the pool; \
             got:\n{msg}",
    );
    assert!(
        msg.contains("did you mean `--project-commit abcdef1-dirty`"),
        "hint must propose the dirty form as the corrected flag; \
             got:\n{msg}",
    );
}

/// Companion to
/// `zero_match_diagnostic_project_commit_dirty_hint_fires`
/// for the `kernel_commits` arm. Same shape: hint names the
/// unmatched value, the matching `-dirty` form found in the
/// pool, and the suggested `--kernel-commit` replacement.
/// A regression that wired the kernel_commits arm to scan
/// `row.commit` (or never wired it at all) would surface
/// here as a missing hint.
#[test]
fn zero_match_diagnostic_kernel_commit_dirty_hint_fires() {
    let mut row = make_row("scn", "1n1l1c1t", true, 1.0);
    row.kernel_commit = Some("kabcde7-dirty".to_string());
    let rows = vec![row];
    let filter = RowFilter {
        kernel_commits: vec!["kabcde7".to_string()],
        ..Default::default()
    };

    let msg = zero_match_diagnostic("A", &filter, &rows, rows.len());

    assert!(
        msg.contains("no rows match `--kernel-commit kabcde7`"),
        "hint must name the unmatched kernel_commit value verbatim; \
             got:\n{msg}",
    );
    assert!(
        msg.contains("`kabcde7-dirty` exists in the pool"),
        "hint must surface the dirty form found in the pool; \
             got:\n{msg}",
    );
    assert!(
        msg.contains("did you mean `--kernel-commit kabcde7-dirty`"),
        "hint must propose the dirty form as the corrected flag; \
             got:\n{msg}",
    );
}

/// `zero_match_diagnostic` appends the `stats list-values`
/// redirect when the operator narrowed on a commit
/// dimension (project_commits OR kernel_commits populated)
/// — that redirect points at the per-dimension dump where
/// the commit values can be cross-referenced. Without a
/// commit-dim filter the redirect is suppressed because
/// `list-values` would dump every dimension, which is no
/// more actionable than the existing `stats list` redirect
/// at the top of the message for a kernel / scheduler /
/// topology miss.
#[test]
fn zero_match_diagnostic_list_values_redirect_when_commit_dim_populated() {
    let row = make_row("scn", "1n1l1c1t", true, 1.0);
    let rows = vec![row];
    let filter = RowFilter {
        project_commits: vec!["abcdef1".to_string()],
        ..Default::default()
    };

    let msg = zero_match_diagnostic("A", &filter, &rows, rows.len());

    assert!(
        msg.contains("cargo ktstr stats list-values"),
        "must include the list-values redirect when commit \
             dim filter is populated; got:\n{msg}",
    );

    // Same redirect when only kernel_commits is populated.
    let filter_kc = RowFilter {
        kernel_commits: vec!["kabcde7".to_string()],
        ..Default::default()
    };
    let msg_kc = zero_match_diagnostic("A", &filter_kc, &rows, rows.len());
    assert!(
        msg_kc.contains("cargo ktstr stats list-values"),
        "list-values redirect must also fire on the \
             kernel_commits arm; got:\n{msg_kc}",
    );
}

/// Without a commit-dim filter populated, the list-values
/// redirect must NOT fire — generic kernel / scheduler /
/// topology / work-type misses already get the `stats list`
/// redirect, and a list-values dump would be noise rather
/// than signal. Pins the suppression so a regression that
/// always emitted the redirect (or omitted the touched-
/// commit-dim guard) surfaces here.
#[test]
fn zero_match_diagnostic_no_list_values_redirect_when_no_commit_dim() {
    let row = make_row("scn", "1n1l1c1t", true, 1.0);
    let rows = vec![row];
    // Filter narrowed on a non-commit dim only — the
    // redirect must stay quiet.
    let filter = RowFilter {
        schedulers: vec!["scx_alpha".to_string()],
        ..Default::default()
    };

    let msg = zero_match_diagnostic("A", &filter, &rows, rows.len());

    assert!(
        !msg.contains("cargo ktstr stats list-values"),
        "list-values redirect must NOT fire when no commit-dim \
             filter is populated; got:\n{msg}",
    );
}

// -- sorted_run_entries (testable extraction of list_runs sort logic) --

/// `sorted_run_entries` orders subdirectories under `root` by
/// directory mtime DESCENDING — newest first. Pins the contract
/// so a regression that flips the sort direction (e.g. drops
/// `Reverse`) or removes the mtime probe (reverting to
/// `file_name`-only sort) surfaces here as the order shift.
///
/// Three subdirs are created with `std::thread::sleep` between
/// `create_dir` calls so each directory's mtime is captured at
/// a strictly later instant than the previous one. 100 ms is
/// generous: ext4/btrfs/xfs nsec resolution + monotonic
/// CLOCK_REALTIME advancement guarantee distinct mtimes per
/// dir at this granularity.
///
/// The OLDEST directory is named `aaa_oldest` and the NEWEST
/// is named `zzz_newest` — paired so the lexical-ascending
/// order (aaa, mmm, zzz) is the OPPOSITE of the mtime-descending
/// order (zzz_newest, mmm_middle, aaa_oldest). Without this
/// pairing, lexical-ascending and mtime-descending would
/// produce the same output and a regression to filename-only
/// sort would not be detectable. With this pairing, any
/// regression that drops `Reverse` (mtime-ASCENDING) OR
/// reverts to filename-only sort (lexical-ASCENDING) yields
/// `aaa, mmm, zzz` — the WRONG order — and the test fails
/// loud.
#[test]
fn sorted_run_entries_orders_by_mtime_descending() {
    use std::thread::sleep;
    use std::time::Duration;

    let root = tempfile::TempDir::new().expect("tempdir");
    let oldest = root.path().join("aaa_oldest");
    let middle = root.path().join("mmm_middle");
    let newest = root.path().join("zzz_newest");
    std::fs::create_dir(&oldest).expect("mkdir oldest");
    sleep(Duration::from_millis(100));
    std::fs::create_dir(&middle).expect("mkdir middle");
    sleep(Duration::from_millis(100));
    std::fs::create_dir(&newest).expect("mkdir newest");

    let rows = super::sorted_run_entries(root.path()).expect("sorted_run_entries must succeed");
    let names: Vec<String> = rows
        .iter()
        .map(|(p, _, _, _)| {
            p.file_name()
                .expect("path must have a file_name")
                .to_string_lossy()
                .into_owned()
        })
        .collect();
    assert_eq!(
        names,
        vec![
            "zzz_newest".to_string(),
            "mmm_middle".to_string(),
            "aaa_oldest".to_string(),
        ],
        "rows must be sorted by mtime descending: newest dir \
             (`zzz_newest`) first, oldest dir (`aaa_oldest`) last. \
             A regression that drops Reverse (mtime-ascending) or \
             reverts to filename-only sort (lexical-ascending) \
             would yield aaa, mmm, zzz — the OPPOSITE of the \
             expected mtime-descending order — and would fail this \
             assertion.",
    );
}

/// Empty root: `sorted_run_entries` returns an empty vec
/// rather than erroring. Pins the no-runs path that the
/// `list_runs` caller short-circuits with the
/// "no runs found" eprintln.
#[test]
fn sorted_run_entries_empty_root_yields_empty_vec() {
    let root = tempfile::TempDir::new().expect("tempdir");
    let rows = super::sorted_run_entries(root.path()).expect("sorted_run_entries must succeed");
    assert!(
        rows.is_empty(),
        "empty root must yield empty vec; got {rows:?}",
    );
}

/// `sorted_run_entries` skips files (only subdirectories
/// become rows). Pins the `is_dir()` filter — a regression
/// that included file entries would surface here as a row
/// for the file.
#[test]
fn sorted_run_entries_skips_non_directory_entries() {
    let root = tempfile::TempDir::new().expect("tempdir");
    std::fs::create_dir(root.path().join("a_dir")).expect("mkdir");
    std::fs::write(root.path().join("a_file"), b"not a run dir").expect("write file");

    let rows = super::sorted_run_entries(root.path()).expect("sorted_run_entries must succeed");
    let names: Vec<String> = rows
        .iter()
        .map(|(p, _, _, _)| {
            p.file_name()
                .expect("path must have a file_name")
                .to_string_lossy()
                .into_owned()
        })
        .collect();
    assert_eq!(
        names,
        vec!["a_dir".to_string()],
        "only the subdirectory must be returned; file entries are skipped",
    );
}

/// `sorted_run_entries` skips dotfile-prefixed subdirectories.
/// Pins the filter that excludes the flock sentinel
/// subdirectory `.locks/` from `cargo ktstr stats list` —
/// a regression that dropped the dotfile filter would
/// surface here as a `.locks` row in the listing, polluting
/// the operator-facing run table with internal coordination
/// state. Other dotfile directories (`.git`, `.cache`, etc.)
/// are filtered uniformly by the same predicate so the test
/// uses two different dotfile names to pin the rule rather
/// than the specific `.locks` instance.
#[test]
fn sorted_run_entries_skips_dotfile_subdirectories() {
    let root = tempfile::TempDir::new().expect("tempdir");
    std::fs::create_dir(root.path().join("real-run")).expect("mkdir");
    std::fs::create_dir(root.path().join(".locks")).expect("mkdir .locks");
    std::fs::create_dir(root.path().join(".cache")).expect("mkdir .cache");

    let rows = super::sorted_run_entries(root.path()).expect("sorted_run_entries must succeed");
    let names: Vec<String> = rows
        .iter()
        .map(|(p, _, _, _)| {
            p.file_name()
                .expect("path must have a file_name")
                .to_string_lossy()
                .into_owned()
        })
        .collect();
    assert_eq!(
        names,
        vec!["real-run".to_string()],
        "dotfile-prefixed subdirs (.locks, .cache) must be filtered \
             out of the run listing; only `real-run` may surface",
    );
}

/// `sorted_run_entries` extracts the arch from the first
/// sidecar that carries `host.arch`. Pins the arch-extraction
/// contract so a regression that drops the field, scans the
/// wrong option leg, or stops short on a host-None sidecar
/// surfaces here.
#[test]
fn sorted_run_entries_extracts_arch_from_first_sidecar() {
    let root = tempfile::TempDir::new().expect("tempdir");
    let run_dir = root.path().join("run-with-arch");
    std::fs::create_dir(&run_dir).expect("mkdir run dir");
    // First sidecar: host populated → arch surfaces.
    let mut sc = crate::test_support::SidecarResult::test_fixture();
    sc.host = Some(crate::host_context::HostContext::test_fixture());
    std::fs::write(
        run_dir.join("t-0000000000000000.ktstr.json"),
        serde_json::to_string(&sc).expect("serialize fixture"),
    )
    .expect("write sidecar");

    let rows = super::sorted_run_entries(root.path()).expect("sorted_run_entries must succeed");
    assert_eq!(rows.len(), 1, "one run dir must yield one row");
    let (_, _, _, arch) = &rows[0];
    assert_eq!(
        arch.as_deref(),
        Some("x86_64"),
        "arch must come from host.arch on the first sidecar — \
             test_fixture populates `Some(\"x86_64\")`",
    );
}

/// A run with no host-populated sidecars yields `None` for
/// arch. Pins the absent-host fallback so the caller's
/// display-sentinel substitution (the `"-"` cell in
/// `list_runs`) is reached.
#[test]
fn sorted_run_entries_arch_none_when_no_host() {
    let root = tempfile::TempDir::new().expect("tempdir");
    let run_dir = root.path().join("run-no-host");
    std::fs::create_dir(&run_dir).expect("mkdir run dir");
    // SidecarResult::test_fixture defaults host: None.
    let sc = crate::test_support::SidecarResult::test_fixture();
    std::fs::write(
        run_dir.join("t-0000000000000000.ktstr.json"),
        serde_json::to_string(&sc).expect("serialize fixture"),
    )
    .expect("write sidecar");

    let rows = super::sorted_run_entries(root.path()).expect("sorted_run_entries must succeed");
    assert_eq!(rows.len(), 1, "one run dir must yield one row");
    let (_, _, _, arch) = &rows[0];
    assert!(
        arch.is_none(),
        "no host-populated sidecar must yield None arch; got {arch:?}",
    );
}