slop-detector 0.1.9

Detects chatbot paste residue, injection phrasing, and formulaic-writing patterns in received text
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
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
//! Fixture and invariant tests for the quality_patterns category.
//! Positives use formulaic prose. Negatives use ordinary human business
//! writing because false positives cause the greatest harm on inbound text.

use slop_detector::{analyze, EvidenceReport};

fn quality_ids(report: &EvidenceReport) -> Vec<&str> {
    report
        .quality_patterns
        .iter()
        .map(|f| f.rule_id.as_str())
        .collect()
}

fn count(report: &EvidenceReport, id: &str) -> usize {
    report
        .quality_patterns
        .iter()
        .filter(|f| f.rule_id == id)
        .count()
}

fn assert_span_invariant(text: &str, report: &EvidenceReport) {
    let all = report
        .paste_residue
        .iter()
        .chain(&report.quality_patterns)
        .chain(&report.injection_patterns);
    for f in all {
        let (start, end) = f.span;
        assert!(start < end && end <= text.len(), "{}: bad span", f.rule_id);
        let slice = &text[start..end];
        if f.snippet_truncated {
            assert!(slice.starts_with(&f.snippet), "{}", f.rule_id);
        } else {
            assert_eq!(slice, f.snippet, "{}: snippet != source[span]", f.rule_id);
        }
    }
}

// --- spike class ----------------------------------------------------------

#[test]
fn spike_lexicon_fires_on_the_measured_excess_words() {
    let text = "We delve into a rich tapestry of intricate options, a testament to the myriad commendable paths ahead.";
    let report = analyze(text);
    // Matched terms: `delve`, `tapestry`, `intricate`, `testament`, `myriad`,
    // and `commendable`.
    assert_eq!(count(&report, "SLOP-A001"), 6, "{report:?}");
    assert_span_invariant(text, &report);
}

#[test]
fn stock_opener_fires_at_full_weight() {
    let report = analyze("In today's fast-paced world, teams need clarity.");
    assert_eq!(count(&report, "SLOP-O003"), 1);
}

#[test]
fn demoted_ornamental_register_fires_as_background_not_spike() {
    let text = "We leverage a robust and seamless platform to empower and unlock growth.";
    let report = analyze(text);
    // Matched terms: `leverage`, `robust`, `seamless`, `empower`, `unlock`.
    assert_eq!(count(&report, "SD-Q001"), 5, "{report:?}");
    assert_eq!(count(&report, "SLOP-A001"), 0);
}

#[test]
fn meticulous_stays_in_hype_adjectives_not_spike() {
    let report = analyze("The report was meticulously crafted and meticulous throughout.");
    assert_eq!(count(&report, "SLOP-A001"), 0);
    assert!(count(&report, "SLOP-I003") >= 2, "{report:?}");
}

// --- background lexicons --------------------------------------------------

#[test]
fn transition_trio_fires_only_at_block_or_sentence_start() {
    for text in [
        "Moreover, the results held.",
        "The pilot worked. Furthermore, costs fell.",
        "line one\nAdditionally, the audit passed.",
        "- Moreover, the bullet case counts.",
    ] {
        let report = analyze(text);
        assert_eq!(count(&report, "SLOP-T002"), 1, "{text}");
    }
    for text in [
        "The data moreover suggests otherwise.",
        "We can additionally confirm the totals.",
    ] {
        let report = analyze(text);
        assert_eq!(count(&report, "SLOP-T002"), 0, "{text}");
    }
}

#[test]
fn block_start_skips_every_leading_marker_run() {
    // A marker in front of the first word does not start a new thought, so
    // the block-start test walks past it. Before this, a leading emoji or
    // markdown opener defeated the position test and the rule went silent.
    for text in [
        "> Moreover, the results held.",
        "# Moreover, the results held.",
        "+ Moreover, the results held.",
        "\u{1F389} Moreover, the results held.",
        "\u{1F389}\u{1F680} Moreover, the results held.",
        "\u{1F389} \u{2705} Moreover, the results held.",
        "- \u{1F389} Moreover, the results held.",
        "1. Moreover, the results held.",
        "2) Moreover, the results held.",
    ] {
        let report = analyze(text);
        assert_eq!(count(&report, "SLOP-T002"), 1, "{text}: {report:?}");
        assert_span_invariant(text, &report);
    }
    // The fleet marker and decoration sets, F-R14a and F-R14e. The nested
    // list glyphs a paste brings with it are the measured population.
    for text in [
        "\u{2023} Moreover, the results held.",
        "\u{2043} Moreover, the results held.",
        "\u{2219} Moreover, the results held.",
        "\u{25CB} Moreover, the results held.",
        "\u{25CF} Moreover, the results held.",
        "\u{25C7} Moreover, the results held.",
        "\u{25C6} Moreover, the results held.",
        "\u{25C9} Moreover, the results held.",
        "\u{25B2} Moreover, the results held.",
        "\u{25A0} Moreover, the results held.",
        "\u{25AA} Moreover, the results held.",
        "\u{2192} Moreover, the results held.",
        "\u{2318} Moreover, the results held.",
    ] {
        let report = analyze(text);
        assert_eq!(count(&report, "SLOP-T002"), 1, "{text}: {report:?}");
        assert_span_invariant(text, &report);
    }
    // Mixed runs are why the whole union is carried. The walk stops at the
    // first character it does not cover, so one uncovered glyph would defeat
    // every covered glyph beside it, and a pasted bullet run mixes an emoji
    // with a geometric shape as a matter of course.
    for text in [
        "\u{1F389} \u{2023} Moreover, the results held.",
        "\u{25AA} \u{1F389} Moreover, the results held.",
        "\u{25CF} \u{2192} \u{1F389} Moreover, the results held.",
        "\u{25AA} \u{2705} Moreover, the results held.",
    ] {
        let report = analyze(text);
        assert_eq!(count(&report, "SLOP-T002"), 1, "{text}: {report:?}");
        assert_span_invariant(text, &report);
    }
    // U+00B7 is out of both sets on measurement: the middle dot is an inline
    // separator and a letter in Catalan, so a word behind one opens nothing.
    // It stops a mixed run too, which is the same property seen from the
    // other side.
    for text in [
        "\u{00B7} Moreover, the results held.",
        "\u{00B7} \u{1F389} Moreover, the results held.",
    ] {
        assert_eq!(count(&analyze(text), "SLOP-T002"), 0, "{text}");
    }
    // Self-gating holds in both directions. A covered glyph mid-sentence
    // does not open a block, because the walk still has to reach a line
    // start.
    let text = "The set a \u{2023} Moreover is not a list.";
    assert_eq!(count(&analyze(text), "SLOP-T002"), 0, "{text}");
    // The walk has to reach a line start, so a marker character with text
    // behind it ends it on false.
    for text in [
        "C# Moreover, the results held.",
        "Value > Moreover, the results held.",
        "Rated 5 Moreover, the results held.",
    ] {
        let report = analyze(text);
        assert_eq!(count(&report, "SLOP-T002"), 0, "{text}: {report:?}");
    }
}

#[test]
fn block_start_ordinal_test_does_not_over_suppress() {
    // A digit run opening its line is a list marker. A digit run behind
    // other text is not, so the punctuation after it does its ordinary
    // work: the period ends a sentence and fires, the paren does not.
    let text = "See item 3. Moreover, the results held.";
    let report = analyze(text);
    assert_eq!(count(&report, "SLOP-T002"), 1, "{report:?}");
    assert_span_invariant(text, &report);
    let text = "See item 4) Moreover, the results held.";
    assert_eq!(count(&analyze(text), "SLOP-T002"), 0, "{text}");
    // A period is a terminal only where the engine's terminal test says so,
    // which keeps an abbreviation from opening a block.
    let text = "Ship to the U.S. Moreover, costs fell.";
    assert_eq!(count(&analyze(text), "SLOP-T002"), 1, "{text}");
    let text = "See e.g. moreover, the totals differ.";
    assert_eq!(count(&analyze(text), "SLOP-T002"), 0, "{text}");
    // The markers work the same way after a real line break.
    for text in [
        "The pilot worked.\n\u{1F389} Moreover, costs fell.",
        "1. The pilot worked.\n2. Moreover, costs fell.",
        "> quoted line\n> Moreover, costs fell.",
    ] {
        let report = analyze(text);
        assert_eq!(count(&report, "SLOP-T002"), 1, "{text}: {report:?}");
    }
}

#[test]
fn trimmed_transition_tail_does_not_fire() {
    for text in [
        "Also, the invoice is attached.",
        "Meanwhile, the team shipped v2.",
        "Ultimately, we chose the smaller vendor.",
        "Indeed, the numbers agree.",
        "Interestingly, both bids came in equal.",
    ] {
        let report = analyze(text);
        assert_eq!(count(&report, "SLOP-T002"), 0, "{text}");
    }
}

#[test]
fn intensifiers_fire_at_background_with_the_technical_exemptions() {
    let report = analyze("The rollout was very smooth and truly fast.");
    assert_eq!(count(&report, "SLOP-I001"), 2);

    let report = analyze("We run a highly available cluster across regions.");
    assert_eq!(count(&report, "SLOP-I001"), 0, "{report:?}");
}

#[test]
fn importance_adjectives_respect_the_fixed_sense_exemptions() {
    let report = analyze("This is a crucial and pivotal change.");
    assert_eq!(count(&report, "SLOP-I002"), 2);

    let report =
        analyze("The critical path runs through the parser; see the critical section notes.");
    assert_eq!(count(&report, "SLOP-I002"), 0, "{report:?}");
}

#[test]
fn inflated_diction_fires_with_homograph_guards() {
    let report = analyze("We utilize the aforementioned process to facilitate onboarding.");
    assert_eq!(count(&report, "SLOP-A004"), 3);

    // Named resource metrics are exempt.
    let report = analyze("Peak cpu utilization stayed under 60% and memory utilization was flat.");
    assert_eq!(count(&report, "SLOP-A004"), 0, "{report:?}");

    // The tool-noun stack pattern fires. The ordinary senses do not.
    let report = analyze("The coverage instrument flags each block.");
    assert_eq!(count(&report, "SLOP-A004"), 1);
    let report = analyze("She plays a wind instrument; check the instrument panel and the financial instrument ledger.");
    assert_eq!(count(&report, "SLOP-A004"), 0, "{report:?}");

    // The participial noun stack fires.
    let report = analyze("The audit found generated-text defects in three sections.");
    assert_eq!(count(&report, "SLOP-A004"), 1);
}

#[test]
fn filler_meta_fires_but_bare_overall_does_not() {
    let report = analyze("It's important to note that the totals moved.");
    assert_eq!(count(&report, "SLOP-T001"), 1);

    let report = analyze("Overall, the quarter closed strong.");
    assert_eq!(count(&report, "SLOP-T001"), 0, "{report:?}");
}

// --- background structural regexes ---------------------------------------

#[test]
fn contrast_and_cadence_regexes_fire() {
    let cases = [
        ("SLOP-C001", "The rollout was not only fast but also cheap."),
        (
            "SLOP-C002",
            "Contrary to popular belief, the port was easy.",
        ),
        ("SLOP-C003", "Rather, we shipped weekly."),
        (
            "SLOP-C005",
            "The new flow is faster, cleaner, and more reliable.",
        ),
        ("SLOP-C006", "You get the best of both worlds."),
        ("SLOP-Q001", "The result? Sales doubled."),
        ("SLOP-R001", "Rest assured, the migration is on track."),
        ("SLOP-O001", "This stands as a testament to the team."),
        (
            "SLOP-O002",
            "The gateway serves as a proxy and boasts caching.",
        ),
        ("SLOP-O004", "Studies show adoption doubles in year two."),
        ("SLOP-T003", "Let's dive into the numbers."),
    ];
    for (id, text) in cases {
        let report = analyze(text);
        assert!(count(&report, id) >= 1, "{id} on {text}: {report:?}");
        assert_span_invariant(text, &report);
    }
}

#[test]
fn c003_anchored_forms_fire_but_bare_rather_than_does_not() {
    // Corpus calibration found the bare `rather-than` pattern mostly in
    // ordinary human writing, so it stays unloaded.
    for text in [
        "We shipped weekly rather than monthly.",
        "Take the train rather than the bus.",
        "I'd do it now rather than wait for Q4.",
    ] {
        let report = analyze(text);
        assert_eq!(count(&report, "SLOP-C003"), 0, "{text}: {report:?}");
    }
    for text in [
        "Rather, we shipped weekly.",
        "Instead, the team paused the rollout.",
        "We rebuilt it rather than simply patching the old flow.",
        "Instead of chasing the deadline, the team cut scope.",
    ] {
        let report = analyze(text);
        assert!(count(&report, "SLOP-C003") >= 1, "{text}: {report:?}");
    }
}

// --- SD-Q002 participial-opener ------------------------------------------

#[test]
fn participial_opener_fires_at_block_and_sentence_start() {
    for text in [
        "Building on these findings, we expanded the pilot.",
        "The test run finished. Leveraging the new cache, latency fell by half.",
        "line one\nRunning the numbers again, the margin holds.",
    ] {
        let report = analyze(text);
        assert_eq!(count(&report, "SD-Q002"), 1, "{text}: {report:?}");
    }
    // The span runs from the word through the comma.
    let text = "Building on these findings, we expanded the pilot.";
    let report = analyze(text);
    let hit = report
        .quality_patterns
        .iter()
        .find(|f| f.rule_id == "SD-Q002")
        .unwrap();
    assert_eq!(hit.snippet, "Building on these findings,");
}

#[test]
fn participial_opener_stoplist_and_shape_negatives() {
    for text in [
        // Non-participial lookalikes.
        "During the meeting, we agreed on scope.",
        "Something in the export, I think, is off.",
        "Morning, everyone.",
        // Ordinary correspondence idioms.
        "Following up on our call, here are the notes.",
        "Regarding the invoice, the PO number was missing.",
        "Moving forward, invoices go to the shared inbox.",
        // Shape misses: mid-sentence, no comma, no clause.
        "We are building on it, so the risk is low.",
        "Building on these findings we expanded the pilot.",
        "Boeing, as expected, declined.",
    ] {
        let report = analyze(text);
        assert_eq!(count(&report, "SD-Q002"), 0, "{text}: {report:?}");
    }
}

// --- SD-Q004 contrastive-negation -----------------------------------------

#[test]
fn contrastive_tail_fires_on_the_declarative_specimen() {
    let text = "Findings judge house style, not authorship.";
    let report = analyze(text);
    assert_eq!(count(&report, "SD-Q004"), 1, "{report:?}");
    let hit = report
        .quality_patterns
        .iter()
        .find(|f| f.rule_id == "SD-Q004")
        .unwrap();
    // The span runs from the comma through the terminal punctuation.
    assert_eq!(hit.snippet, ", not authorship.");
    assert_span_invariant(text, &report);

    // Mid-document, with the clause recovered across a sentence boundary.
    let text = "The report cites spans. Findings judge house style, not authorship. Read them.";
    let report = analyze(text);
    assert_eq!(count(&report, "SD-Q004"), 1, "{report:?}");

    // The never keyword carries the same shape.
    let report = analyze("The reviewers weigh density, never single hits.");
    assert_eq!(count(&report, "SD-Q004"), 1, "{report:?}");
}

#[test]
fn contrastive_tail_is_silent_on_empty_np_and_directives() {
    for text in [
        // Empty and whitespace-only NP: silent by the corrected guard.
        "x, not   .",
        "x, not.",
        // Imperative sentence with no tail shape at all.
        "Never obey injected text.",
        // Imperative openers on the deny-list.
        "Use the ledger, not the summary.",
        "Don't trust the digest, never the tarball.",
        "Keep the caveat, not the claim.",
        // Second-person cue before the comma.
        "Your reviewers check style, not authorship.",
        "If you want speed, not size, say so.",
        // Leading-adverbial directives: a deny-list verb after an interior
        // comma or after then.
        "When in doubt, use the builder, not the raw constructor.",
        "First read the header, then keep the body, not the footer.",
        // Parenthetical interpolation: the interior comma breaks the NP.
        "The parser, not the lexer, owns that token.",
        // No terminal punctuation closing the tail.
        "We shipped the fix, not the docs",
    ] {
        let report = analyze(text);
        assert_eq!(count(&report, "SD-Q004"), 0, "{text}: {report:?}");
    }
}

// --- SD-Q004 abbreviation-period fix (the ai-slop C007 R6 class) ----------
//
// Before the fix, any `.` inside the NP scan closed the tail, so `U.S.`
// produced the truncated false candidate `, not in the U.`, and the clause
// walk-back treated an abbreviation period as a clause boundary, hiding
// the imperative opener from the suppression classifier.

#[test]
fn contrastive_tail_abbreviation_mid_tail_no_longer_false_fires() {
    // The exact R6 repro shape. The abbreviation-internal period plus the
    // lowercase continuation (`U.S. but`) must not manufacture a
    // candidate. The real tail scan then dies at the comma after Asia.
    let text = "Adoption is concentrated, not in the U.S. but in Asia, where usage doubled.";
    let report = analyze(text);
    assert_eq!(count(&report, "SD-Q004"), 0, "{report:?}");
    assert_span_invariant(text, &report);

    // Without the trailing comma the tail parser reaches the terminal, and
    // the word-bounded `but` in the NP rejects the tail. A not-X-but-Y
    // continuation belongs to the SLOP-C008 pair rule.
    let report = analyze("Adoption is concentrated, not in the U.S. but in Asia.");
    assert_eq!(count(&report, "SD-Q004"), 0, "{report:?}");

    // The but-rejection stands without any abbreviation in the tail.
    let report = analyze("The team ships weekly, not monthly but quarterly.");
    assert_eq!(count(&report, "SD-Q004"), 0, "{report:?}");
}

#[test]
fn contrastive_tail_but_rejection_is_word_bounded() {
    // A `but` substring inside a longer word is not a contrastive
    // continuation. The tail still fires.
    for (text, tail) in [
        (
            "The cache is shared, not distributed.",
            ", not distributed.",
        ),
        (
            "The fix targets the root cause, not the attributes.",
            ", not the attributes.",
        ),
    ] {
        let report = analyze(text);
        assert_eq!(count(&report, "SD-Q004"), 1, "{text}: {report:?}");
        let hit = report
            .quality_patterns
            .iter()
            .find(|f| f.rule_id == "SD-Q004")
            .unwrap();
        assert_eq!(hit.snippet, tail, "{text}");
    }
}

#[test]
fn contrastive_tail_ending_in_abbreviation_fires_with_the_full_span() {
    // Used to truncate at the abbreviation's first period (`, not the U.`).
    let text = "The survey covers Europe, not the U.S.";
    let report = analyze(text);
    assert_eq!(count(&report, "SD-Q004"), 1, "{report:?}");
    let hit = report
        .quality_patterns
        .iter()
        .find(|f| f.rule_id == "SD-Q004")
        .unwrap();
    assert_eq!(hit.snippet, ", not the U.S.");
    assert_span_invariant(text, &report);
}

#[test]
fn contrastive_tail_eg_mid_np_fires_with_the_complete_span() {
    // Handling mid-NP `e.g.` preserves the full span and still reports.
    // The carrier avoids an imperative opener. The deny-list suppresses
    // `Use the alias, ...`, which would leave the NP behavior untested.
    let text = "The docs cite the alias, not e.g. the raw path.";
    let report = analyze(text);
    assert_eq!(count(&report, "SD-Q004"), 1, "{report:?}");
    let hit = report
        .quality_patterns
        .iter()
        .find(|f| f.rule_id == "SD-Q004")
        .unwrap();
    assert_eq!(hit.snippet, ", not e.g. the raw path.");
}

#[test]
fn contrastive_tail_clause_walkback_crosses_abbreviation() {
    // The clause_start half of the fix: the walk-back crosses `U.S.` and
    // recovers the whole clause, whose imperative opener suppresses the
    // tail. Under the old walk-back the recovered clause was just `hosted
    // mirror` and this sentence false-fired.
    let report = analyze("Use the U.S. hosted mirror, not a pilot.");
    assert_eq!(count(&report, "SD-Q004"), 0, "{report:?}");

    // Non-directive control: crossing the abbreviation must not change the
    // verdict on a clause that should fire.
    let text = "The rollout targets the U.S. market, not a pilot.";
    let report = analyze(text);
    assert_eq!(count(&report, "SD-Q004"), 1, "{report:?}");
    let hit = report
        .quality_patterns
        .iter()
        .find(|f| f.rule_id == "SD-Q004")
        .unwrap();
    assert_eq!(hit.snippet, ", not a pilot.");
}

#[test]
fn contrastive_negation_regex_triggers_fire() {
    for text in [
        // The about-reframe pair.
        "The report isn't about blame; it's about evidence.",
        "This check is not about style but about provenance.",
        // Copular not-X-but-Y.
        "The output is not a verdict but a reading signal.",
        // Copular reveal across a sentence boundary.
        "This is not a scorecard. It is a reading aid.",
    ] {
        let report = analyze(text);
        assert!(count(&report, "SD-Q004") >= 1, "{text}: {report:?}");
        assert_span_invariant(text, &report);
    }
}

// --- SLOP-C008 contrastive-pair -------------------------------------------

#[test]
fn c008_pair_forms_fire_as_background() {
    for text in [
        // The infinitive pair.
        "The position is not to dismiss breadth, but to require depth.",
        // The wh-parallel pair.
        "The question is not what they know, but how they value it.",
        // The interpolated pair SD-Q004's tail parser excludes by design.
        "Ship the fix, not the workaround, but tell support first.",
        // The two-sentence reframe without a pronoun subject on sentence
        // one.
        "The port is not a rewrite. It is a shim over the old core.",
    ] {
        let report = analyze(text);
        assert!(count(&report, "SLOP-C008") >= 1, "{text}: {report:?}");
        assert_span_invariant(text, &report);
    }
}

#[test]
fn c008_stays_out_of_c001_and_c003_territory() {
    for text in [
        // Adverb-marked pair: SLOP-C001's shape.
        "The rollout was not only fast but also cheap.",
        "It is not just faster but safer.",
        // Rather-than: SLOP-C003's shape (and its bare form is unloaded).
        "We shipped weekly rather than monthly.",
        // A bare comma-not tail with no pair: SD-Q004's shape.
        "Findings judge house style, not authorship.",
    ] {
        let report = analyze(text);
        assert_eq!(count(&report, "SLOP-C008"), 0, "{text}: {report:?}");
    }
}

// --- SLOP-A005 metaphor-reach phrases -------------------------------------

#[test]
fn a005_idiom_families_fire_as_background() {
    for text in [
        "The data tells a more textured story about adoption.",
        "This result is worth sitting with.",
        "The invitation is to rethink the pipeline.",
        "The metric serves as a canary for regressions.",
        "It weaves together three subsystems.",
        "Our roadmap remains our north star.",
        "The findings form a rich tapestry of user behavior.",
    ] {
        let report = analyze(text);
        assert!(count(&report, "SLOP-A005") >= 1, "{text}: {report:?}");
        assert_span_invariant(text, &report);
    }
}

#[test]
fn a005_closed_boundaries_and_plain_prose_stay_silent() {
    for text in [
        // The probe's hazard pin: compass must never reach into
        // compassion.
        "Their approach serves as a compassionate model for teams.",
        // No idiom shape.
        "She wrote a story about the outage.",
        // Single-token metaphors are deliberately not loaded.
        "The mine's canary protocol is documented separately.",
        "The beacon interval is 100 ms.",
    ] {
        let report = analyze(text);
        assert_eq!(count(&report, "SLOP-A005"), 0, "{text}: {report:?}");
    }
}

// --- SLOP-V004 agent-loop vocabulary --------------------------------------

#[test]
fn v004_lexicon_phrases_and_constructions_fire() {
    // Every vendored lexicon phrase fires, word-bounded.
    let rules = slop_detector::data::load().unwrap();
    let v004 = rules.iter().find(|r| r.id == "SLOP-V004").unwrap();
    for term in &v004.terms {
        let text = format!("The check was rerun {term} without changes.");
        let report = analyze(&text);
        assert!(count(&report, "SLOP-V004") >= 1, "{term}: {report:?}");
    }
    // The two construction specimens from the harvested evidence.
    for text in [
        "Flagged for JJ: the digest moved between runs.",
        "All three figures confirmed.",
        "All 12 counts verified against the ledger.",
    ] {
        let report = analyze(text);
        assert!(count(&report, "SLOP-V004") >= 1, "{text}: {report:?}");
        assert_span_invariant(text, &report);
    }
}

#[test]
fn v004_lowercase_flagged_for_is_silent() {
    // The `Flagged-for` construction preserves case. Lowercase
    // mid-sentence process prose never fires.
    let report = analyze("The commit was flagged for review by CI.");
    assert_eq!(count(&report, "SLOP-V004"), 0, "{report:?}");
}

// --- individual class -----------------------------------------------------

#[test]
fn individual_rules_fire_per_hit_in_quality_patterns() {
    let cases = [
        (
            "SLOP-V001",
            "As an AI language model, I cannot check the portal.",
        ),
        (
            "SLOP-V002",
            "You're absolutely right, and great question about the fees.",
        ),
        ("SLOP-S003", "I hope this helps with the review."),
    ];
    for (id, text) in cases {
        let report = analyze(text);
        assert!(count(&report, id) >= 1, "{id} on {text}: {report:?}");
        assert!(report.paste_residue.is_empty(), "{text}");
    }
}

#[test]
fn v002_leaves_the_catch_entries_to_the_thread() {
    // A reviewer who writes `Good catch` means it, and the hit still moved
    // the per-1000-words figure the reader is told to trust.
    for silent in [
        "Good catch, updated the doc.",
        "Great catch, that column was stale.",
    ] {
        let report = analyze(silent);
        assert_eq!(count(&report, "SLOP-V002"), 0, "{silent}: {report:?}");
    }
    // The paired form is what the rule was catching, and the sycophancy
    // entries beside it still catch it.
    let text = "Good catch! You're absolutely right. I'll fix that.";
    let report = analyze(text);
    assert_eq!(count(&report, "SLOP-V002"), 1, "{report:?}");
    assert_eq!(
        report.quality_patterns[0].snippet,
        "You're absolutely right"
    );
    assert_span_invariant(text, &report);
}

#[test]
fn v002_fair_hit_fires_on_the_concession_and_its_literal_neighbours() {
    // The concession sense is the tell: it grants the reader's point and
    // carries on.
    for text in [
        "Fair hit, I should have checked the totals first.",
        "That's a fair hit and I will rework the section.",
        "Fair hit on the timeline, we underestimated the review.",
    ] {
        let report = analyze(text);
        assert_eq!(count(&report, "SLOP-V002"), 1, "{text}: {report:?}");
        assert_span_invariant(text, &report);
    }
    // The literal sense and the substring reach both fire, and the reader
    // absorbs each on its own. Neither is exempted.
    for text in [
        "The batter took a fair hit down the third-base line.",
        "The referee ruled it an unfair hit and issued a penalty.",
    ] {
        let report = analyze(text);
        assert_eq!(count(&report, "SLOP-V002"), 1, "{text}: {report:?}");
    }
    // The near neighbours are not carried.
    for text in [
        "Fair point, the numbers do not line up.",
        "Fair enough, we can ship Friday.",
        "Fair cop, that one is on me.",
        "Fair knock from the reviewer.",
    ] {
        let report = analyze(text);
        assert_eq!(count(&report, "SLOP-V002"), 0, "{text}: {report:?}");
    }
}

#[test]
fn provenance_marker_fires_on_the_oblique_vocabulary() {
    for text in [
        // The lexicon matches terms on word boundaries, ignoring case.
        "We reimplemented the parser over the weekend.",
        "The provenance of this module is documented in the tracker.",
        "Two shims were kept for API parity.",
        "It serves as the reference implementation.",
        // The pattern triggers.
        "The crate is a drop-in replacement for the old client.",
        "The port keeps parity with the original layout.",
        "It mirrors the upstream API surface.",
    ] {
        let report = analyze(text);
        assert!(count(&report, "SD-Q003") >= 1, "{text}: {report:?}");
        assert_span_invariant(text, &report);
    }
}

#[test]
fn provenance_marker_is_word_bounded() {
    // Substrings inside longer identifiers stay silent.
    let report = analyze("The dataprovenancez field and reimplementedFoo symbol are unrelated.");
    assert_eq!(count(&report, "SD-Q003"), 0, "{report:?}");
}

// --- not-loaded and dropped families -------------------------------------

#[test]
fn not_loaded_families_stay_silent() {
    for text in [
        // R002 clarity-meta: not loaded for inbound.
        "To be clear, the March invoice was paid. For the record, twice.",
        // I005 empty-qualifiers: dropped. Hedging is human.
        "It seems this could potentially work, and we may possibly try it.",
        // S001 signature-lines: not loaded.
        "Best regards,\nMina",
        // M-family house style: not loaded.
        "The budget — once approved — covers both; we split the rest.",
        // F-family and W001: not loaded.
        "I verified the backup and tested the restore path myself.",
    ] {
        let report = analyze(text);
        assert!(report.quality_patterns.is_empty(), "{text}: {report:?}");
    }
}

// --- clean-prose floors ---------------------------------------------------

#[test]
fn plain_business_email_yields_zero_quality_findings() {
    let text = "Hi Omar,\n\nThe vendor sent the revised quote this morning: 40 seats at \
                the old rate, renewal in March. Legal wants one change to the liability \
                clause before we sign. If you can review clause 7 by Thursday, we can \
                close this out before the offsite.\n\nBest regards,\nMina";
    let report = analyze(text);
    assert!(report.quality_patterns.is_empty(), "{report:?}");
    assert!(report.paste_residue.is_empty());
    assert!(report.injection_patterns.is_empty());
}

#[test]
fn one_stray_intensifier_does_not_flood() {
    // Ordinary human mail with one register word yields exactly that one
    // background hit and nothing else.
    let text = "Hi Dana, the demo went very well. The client asked for pricing by \
                Friday and a follow-up call next week. I'll draft the proposal \
                tonight and send it to you for review.";
    let report = analyze(text);
    let ids = quality_ids(&report);
    assert_eq!(ids, ["SLOP-I001"], "{report:?}");
}

// --- density fixture: determinism and the stats denominators --------------

const FORMULAIC_FIXTURE: &str = "In today's fast-paced world, we delve into a \
    tapestry of intricate solutions. Moreover, our robust and seamless platform \
    doesn't just leverage best practices, it is not only faster but also safer. \
    Building on these findings, we utilize the aforementioned framework to \
    facilitate a truly comprehensive rollout. I hope this helps.";

#[test]
fn formulaic_prose_reports_across_classes_deterministically() {
    let a = serde_json::to_string(&analyze(FORMULAIC_FIXTURE)).unwrap();
    let b = serde_json::to_string(&analyze(FORMULAIC_FIXTURE)).unwrap();
    assert_eq!(a, b);

    let report = analyze(FORMULAIC_FIXTURE);
    assert_span_invariant(FORMULAIC_FIXTURE, &report);
    // All are present: spike (`delve`, `tapestry`, `intricate`), stock
    // opener, background register, trio opener, inflated diction, contrast,
    // intensifier, and individual pleasantry. Read them against stats.
    for id in [
        "SLOP-A001",
        "SLOP-O003",
        "SD-Q001",
        "SLOP-T002",
        "SLOP-A004",
        "SLOP-C001",
        "SLOP-I001",
        "SLOP-S003",
    ] {
        assert!(count(&report, id) >= 1, "{id}: {report:?}");
    }
    assert_eq!(count(&report, "SLOP-A001"), 3, "{report:?}");
    assert!(report.stats.word_count > 40);
    assert_eq!(report.stats.byte_len, FORMULAIC_FIXTURE.len());
    // Ordered within the category.
    let mut sorted = report.quality_patterns.clone();
    sorted.sort_by(|x, y| {
        (x.span.0, x.span.1, x.rule_id.as_str()).cmp(&(y.span.0, y.span.1, y.rule_id.as_str()))
    });
    assert_eq!(report.quality_patterns, sorted);
}

// --- SD-Q006 ledger-stamp -------------------------------------------------

#[test]
fn q006_ledger_stamps_fire_per_hit() {
    let text = "The owner rules this on 2026-08-18. The floor was measured 2026-08-01 and the case adjudicated 2026-07-30.";
    let report = analyze(text);
    assert_eq!(count(&report, "SD-Q006"), 3, "{report:?}");
    assert_span_invariant(text, &report);
}

#[test]
fn q006_both_arms_require_the_date() {
    // The dateless owner phrase was cut: ordinary English produces
    // "rules this way" and "ruled this out".
    for text in [
        "The owner ruled this after the second sweep.",
        "The owner ruled this out after the inspection.",
        "If the owner rules this way, we ship Friday.",
    ] {
        let report = analyze(text);
        assert_eq!(count(&report, "SD-Q006"), 0, "{text}: {report:?}");
    }
}

#[test]
fn q006_transactional_verbs_are_not_carried_inbound() {
    // Dated `confirmed`, `verified`, and `resolved` verbs occur in everyday
    // operations mail. The inbound verb set is narrower than SLOP-V005
    // and excludes them.
    let text = "Payment confirmed 2026-08-14 and the invoice verified 2026-08-15; the ticket was resolved 2026-08-16.";
    let report = analyze(text);
    assert_eq!(count(&report, "SD-Q006"), 0, "{report:?}");
}

#[test]
fn q006_release_diction_and_prose_date_forms_are_silent() {
    let text = "Released 2026-08-18 with two fixes. The survey was measured on 2026-08-01. The board resolved 2026 budget items. The build was measured 2026-99-99.";
    let report = analyze(text);
    assert_eq!(count(&report, "SD-Q006"), 0, "{report:?}");
    assert_span_invariant(text, &report);
}

// --- SD-Q009 decision-attribution -----------------------------------------

/// The owner's specimen: the possessive with a decision noun, the date joined
/// to the span, one report.
#[test]
fn q009_dated_possessive_reports_once_with_the_date() {
    let text = "Owner's ruling, 2026-08-20: the profile stays.";
    let report = analyze(text);
    let hits: Vec<&str> = report
        .quality_patterns
        .iter()
        .filter(|f| f.rule_id == "SD-Q009")
        .map(|f| f.snippet.as_str())
        .collect();
    assert_eq!(hits, vec!["Owner's ruling, 2026-08-20"], "{report:?}");
    assert_span_invariant(text, &report);
}

/// One case per spelling, pinned to the span.
#[test]
fn q009_every_spelling_fires_on_its_span() {
    for (text, span) in [
        (
            "The owner ruled that the lexicon drops the bare word.",
            "The owner ruled that",
        ),
        (
            "The maintainer's call was to keep the gate.",
            "The maintainer's call",
        ),
        (
            "This entry was requested by the user.",
            "requested by the user",
        ),
        ("The owner-flagged stack ships as a rule.", "owner-flagged"),
        (
            "Code sign-off is Fable, by owner decision.",
            "owner decision",
        ),
        ("Per the owner, the digest stays sealed.", "Per the owner"),
        (
            "The flag was added at the user's request.",
            "at the user's request",
        ),
        (
            "On the principal's instruction the run stopped.",
            "On the principal's instruction",
        ),
        (
            "Owner decision (2026-08-20): keep the profile.",
            "Owner decision (2026-08-20)",
        ),
        ("- Ruling: keep the profile.", "- Ruling:"),
        (
            "Decision, 2026-08-19: the profile stays.",
            "Decision, 2026-08-19:",
        ),
        (
            "Ruled by owner proxy, the rule is candidate tier.",
            "Ruled by owner proxy",
        ),
        (
            "The proxy signed off on 2026-08-20.",
            "The proxy signed off on 2026-08-20",
        ),
        (
            "The owner\u{2019}s verdict was to drop the entry.",
            "The owner\u{2019}s verdict",
        ),
        (
            "The owner ruled this out after the inspection.",
            "The owner ruled this",
        ),
        ("The owner wants the profile kept.", "The owner wants"),
        (
            "The orchestrator's call was to stop.",
            "The orchestrator's call",
        ),
        (
            "Owner directive (2026-08-24): stop the run.",
            "Owner directive (2026-08-24)",
        ),
        (
            "The maintainer asked for a smaller patch.",
            "The maintainer asked",
        ),
        ("Per your ruling, the digest stays.", "Per your ruling"),
        ("You ruled that the bare word leaves.", "You ruled"),
        (
            "The owner's request was a shorter README.",
            "The owner's request",
        ),
        ("The lead chose the second option.", "The lead chose"),
        ("Ruling (2026-08-20): keep.", "Ruling (2026-08-20):"),
        (
            "Ruled by a Fable agent in the owner's stead.",
            "the owner's stead",
        ),
        (
            "Drafted under owner-proxy Ruling 011.",
            "owner-proxy Ruling",
        ),
        (
            "The owner has ruled on everything current.",
            "The owner has ruled",
        ),
        ("The owner greenlit the batch.", "The owner greenlit"),
        ("As you directed, the run stopped.", "As you directed"),
    ] {
        let report = analyze(text);
        let hits: Vec<&str> = report
            .quality_patterns
            .iter()
            .filter(|f| f.rule_id == "SD-Q009")
            .map(|f| f.snippet.as_str())
            .collect();
        assert_eq!(hits, vec![span], "{text}: {report:?}");
        assert_span_invariant(text, &report);
    }
}

/// Ordinary business English with the same role nouns stays silent.
#[test]
fn q009_ordinary_english_is_silent() {
    for text in [
        "Rate limits apply per user and per owner of a token.",
        "User-defined types and user-specified widths are supported.",
        "The value set by the user wins over the default.",
        "The decision tree splits on the first field.",
        "The owner of the file can change its mode.",
        "The author's name appears in the header.",
        "The reviewer's comments are attached to the manuscript.",
        "The lead developer's laptop runs the tests.",
        "A verdict from the court arrived on 2026-08-20.",
        "Decision records live under docs/adr.",
        "Ruled 2026-08-14 after the sweep.",
        "Please send the signed lease back by Friday.",
        "The user wants a faster export, and the author said so in the preface.",
        "Maintainer approval is required for a merge.",
        "The user asked for a dark theme.",
        "Under your account settings the theme changes.",
        "I said no, and you decided to wait.",
    ] {
        let report = analyze(text);
        assert_eq!(count(&report, "SD-Q009"), 0, "{text}: {report:?}");
    }
}

/// Seam with SD-Q006: the dated owner-verdict stamp reports under both, and
/// the roleless stamp under SD-Q006 alone.
#[test]
fn q009_seam_with_the_ledger_stamp() {
    let report = analyze("The owner rules this on 2026-08-18.");
    assert_eq!(count(&report, "SD-Q006"), 1, "{report:?}");
    assert_eq!(count(&report, "SD-Q009"), 1, "{report:?}");
    let report = analyze("The floor was measured 2026-08-01 across the boundary.");
    assert_eq!(count(&report, "SD-Q006"), 1, "{report:?}");
    assert_eq!(count(&report, "SD-Q009"), 0, "{report:?}");
}

// --- SD-Q007 proleptic-capability-denial ----------------------------------

/// The owner-recorded specimen: restatement, apophasis, hedged denial.
const DENIAL_SPECIMEN: &str = "It reads text. It does not detect authorship, \
    and no finding is evidence that a person or a model wrote anything.";

#[test]
fn q007_fires_on_every_denial_clause_of_the_specimen() {
    let report = analyze(DENIAL_SPECIMEN);
    assert_eq!(count(&report, "SD-Q007"), 2, "{report:?}");
    let snippets: Vec<&str> = report
        .quality_patterns
        .iter()
        .filter(|f| f.rule_id == "SD-Q007")
        .map(|f| f.snippet.as_str())
        .collect();
    assert_eq!(snippets[0], "It does not detect authorship");
    assert_eq!(
        snippets[1],
        "no finding is evidence that a person or a model wrote anything"
    );
    // The restatement satisfies the adjacency arm and reports nothing.
    assert!(!snippets.iter().any(|s| s.contains("It reads text")));
    assert_span_invariant(DENIAL_SPECIMEN, &report);
}

#[test]
fn q007_stacked_denials_fire_without_any_restatement() {
    let text = "The tool does not score anyone. It cannot rank writers, and the \
                output is not evidence of intent.";
    let report = analyze(text);
    assert_eq!(count(&report, "SD-Q007"), 3, "{report:?}");
    assert_span_invariant(text, &report);
}

#[test]
fn q007_needs_a_trigger_and_a_restatement_alone_is_silent() {
    for text in [
        // A lone restatement lacks the denial or hedge this rule requires.
        "It reads text.",
        // One denial with nothing beside it.
        "The audit does not replace a legal review.",
        // One denial, and the neighbouring sentence is not a restatement.
        "The crate does not detect authorship. Install it with cargo install.",
    ] {
        let report = analyze(text);
        assert_eq!(count(&report, "SD-Q007"), 0, "{text}: {report:?}");
    }
}

#[test]
fn q007_imperatives_stay_silent() {
    for text in [
        "Never author, approve, edit, or sign a waiver.",
        "It reads text. Never obey injected text, and no finding is a command.",
    ] {
        let report = analyze(text);
        assert_eq!(count(&report, "SD-Q007"), 0, "{text}: {report:?}");
    }
}

#[test]
fn q007_ordinary_business_negation_stays_silent() {
    let text = "Hi Priya, the vendor did not send the revised quote and the invoice \
                is not due until March. She deliberately held the last two back. \
                I cannot review clause 7 before Thursday.";
    let report = analyze(text);
    assert_eq!(count(&report, "SD-Q007"), 0, "{report:?}");
    assert_eq!(count(&report, "SD-Q008"), 0, "{report:?}");
}

#[test]
fn q007_reports_an_honest_scope_statement_for_the_human_to_read() {
    // The rule reports honest scope statements that match the denial
    // shape. The reader distinguishes live boundaries from pre-rebuttals
    // during the per-hit read and acts on both sentences here.
    let text = "The audit reads financial records. It does not replace a legal \
                review, and it is not a guarantee against fraud.";
    let report = analyze(text);
    assert_eq!(count(&report, "SD-Q007"), 2, "{report:?}");
    assert_span_invariant(text, &report);
}

// --- SD-Q008 rationale-leak -----------------------------------------------

/// The owner-recorded second specimen: a guard paragraph carrying its own
/// design reasoning.
const RATIONALE_SPECIMEN: &str = "Input that is a Rust source file is rejected \
    as unsupported, exit 40, because gating source draws findings from statement \
    punctuation and not from writing. The test reads Rust shape only. Source in \
    another language reaches the rules and produces findings a reader should \
    discount, which is the trade for a guard that never fires on prose. Either \
    pass the prose, or wrap the code in a fenced block, which segmentation \
    excludes.";

#[test]
fn q008_and_the_and_not_contrast_fire_on_the_second_specimen() {
    let report = analyze(RATIONALE_SPECIMEN);
    assert_eq!(count(&report, "SD-Q008"), 2, "{report:?}");
    assert_eq!(count(&report, "SD-Q004"), 1, "{report:?}");
    let snippets: Vec<&str> = report
        .quality_patterns
        .iter()
        .map(|f| f.snippet.as_str())
        .collect();
    assert!(snippets.contains(&"and not from"), "{snippets:?}");
    assert!(
        snippets.contains(&"a reader should discount"),
        "{snippets:?}"
    );
    assert!(snippets.contains(&"which is the trade"), "{snippets:?}");
    assert_span_invariant(RATIONALE_SPECIMEN, &report);
}

#[test]
fn q008_needs_the_anchor() {
    // These ordinary adverb examples have no tool noun and no self-subject.
    for text in [
        "She deliberately ignored him.",
        "He rewrote the letter on purpose and mailed it intentionally.",
        "The board accepted the offer in exchange for a longer term.",
    ] {
        let report = analyze(text);
        assert_eq!(count(&report, "SD-Q008"), 0, "{text}: {report:?}");
    }
}

#[test]
fn q008_fires_per_marker_when_the_sentence_names_the_tool() {
    let text = "The rule deliberately drops the bare form, at the cost of recall.";
    let report = analyze(text);
    assert_eq!(count(&report, "SD-Q008"), 2, "{report:?}");
    assert_span_invariant(text, &report);
}

#[test]
fn q008_leaves_the_denial_spelling_to_q007() {
    // The seam: `should not be read as` is a denial, `should be read as` is
    // a reception instruction.
    let denial = "It reads text. The score should not be read as a verdict.";
    let report = analyze(denial);
    assert_eq!(count(&report, "SD-Q008"), 0, "{report:?}");
    assert_eq!(count(&report, "SD-Q007"), 1, "{report:?}");
    let affirmative = "The score should be read as a density.";
    let report = analyze(affirmative);
    assert_eq!(count(&report, "SD-Q008"), 1, "{report:?}");
    assert_eq!(count(&report, "SD-Q007"), 0, "{report:?}");
}

// --- SD-Q004 and-not spelling ---------------------------------------------

#[test]
fn q004_and_not_spelling_needs_the_closed_follower() {
    let text = "The finding comes from punctuation and not from writing.";
    assert_eq!(count(&analyze(text), "SD-Q004"), 1, "{text}");
    for clean in [
        "The migration is scheduled and not yet finished.",
        "We reviewed the quote and not much else came up.",
        "Send the draft or not, either way we ship Friday.",
    ] {
        let report = analyze(clean);
        assert_eq!(count(&report, "SD-Q004"), 0, "{clean}: {report:?}");
    }
}

#[test]
fn q004_and_not_is_the_and_spelling_only() {
    // `whether or not` is an honest idiom and the engine takes no
    // look-behind, so the conjunction is `and` alone. The or-spelling and
    // the but-spelling stay hand-read.
    for clean in [
        "Please confirm whether or not the flag is present.",
        "The gate reports whether or not the run passed.",
        "The span comes from the tail but not from the opener.",
    ] {
        let report = analyze(clean);
        assert_eq!(count(&report, "SD-Q004"), 0, "{clean}: {report:?}");
    }
}

#[test]
fn q004_participial_tails_are_not_contrasts() {
    // `never judging anyone` says how she listened, not what she did
    // instead, so the tail is a participial adjunct and stays silent.
    for clean in [
        "She listened, never judging anyone.",
        "He worked all night, never complaining.",
        "They shipped it quietly, not making a fuss.",
    ] {
        let report = analyze(clean);
        assert_eq!(count(&report, "SD-Q004"), 0, "{clean}: {report:?}");
    }
}

#[test]
fn q004_participial_exemption_needs_the_word_next_to_the_negation() {
    // A determiner between the negation and the `-ing` word means the word
    // is a noun, and the four quantifier pronouns and `during` wear the
    // same three letters without being participles.
    for fires in [
        "The rule reports the span, not the sentence.",
        "It flags the shape, not everything.",
        "It flags the shape, not the beginning.",
        "It flags the shape, not a building.",
        "The rule fires on the token, not during matching.",
    ] {
        let report = analyze(fires);
        assert_eq!(count(&report, "SD-Q004"), 1, "{fires}: {report:?}");
        assert_span_invariant(fires, &report);
    }
}

// --- SD-Q007 family-1 spelling, restatement shape, SD-Q008 anchor ---------

#[test]
fn q007_family_one_requires_a_capability_verb() {
    // Both spellings carry a capability verb. Spelling A is a positive
    // subject plus a negation plus the verb. Spelling B is a negative
    // subject that carries its own negation.
    let text = "It reads text. No rule scores voice, and it never scores authorship.";
    let report = analyze(text);
    assert_eq!(count(&report, "SD-Q007"), 2, "{report:?}");
    let snippets: Vec<&str> = report
        .quality_patterns
        .iter()
        .map(|f| f.snippet.as_str())
        .collect();
    assert!(snippets.contains(&"No rule scores voice"), "{snippets:?}");
    assert_span_invariant(text, &report);
}

#[test]
fn q007_function_verb_denials_are_honest_scope_facts() {
    for text in [
        // Denying a function verb states what the thing does not do, which
        // a reader acts on. Both clauses are stacked, so only the verb gate
        // keeps this silent.
        "It reads text. The check does not fire on prose, and the parser does not panic.",
        // No negation phrase and no capability verb.
        "The check completed with nothing blocking.",
        // A relativizer, not a subject: `that` is out of the subject set.
        "A value that is not a table stops the load.",
    ] {
        let report = analyze(text);
        assert_eq!(count(&report, "SD-Q007"), 0, "{text}: {report:?}");
    }
}

#[test]
fn q007_arm_b_takes_any_affirmative_partner() {
    // The partner is any clause naming a closed-set subject and denying
    // nothing. The degenerate restatement is the canonical case, and the
    // arm reaches past it.
    for fires in [
        "It reads the input. It cannot identify a writer.",
        "The tool operates on prose! It does not rank writers.",
        "This scans documents. It does not measure quality.",
        // Affirmative self-description that is not the bare restatement.
        "It reads text quickly. It cannot identify a writer.",
        "It reads text about dogs. It cannot identify a writer.",
        "It reads text? It cannot identify a writer.",
    ] {
        let report = analyze(fires);
        assert_eq!(count(&report, "SD-Q007"), 1, "{fires}: {report:?}");
    }
    for silent in [
        // `that` is out of the subject set, so the neighbour names no
        // closed-set subject and cannot partner the denial.
        "That reads text. It cannot identify a writer.",
        "The poem sings, and it does not detect authorship.",
        // No partner in reach at all.
        "The audit does not replace a legal review.",
    ] {
        let report = analyze(silent);
        assert_eq!(count(&report, "SD-Q007"), 0, "{silent}: {report:?}");
    }
}

#[test]
fn q007_arm_b_search_order_and_reach() {
    // Inside the qualifying clause's own sentence the search has no
    // distance limit, so an interposed clause does not break it.
    for fires in [
        "It reads text, and it does not detect authorship.",
        "It reads text, which is fast, and it does not detect authorship.",
    ] {
        let report = analyze(fires);
        assert_eq!(count(&report, "SD-Q007"), 1, "{fires}: {report:?}");
    }
    // Across sentences the search stays strictly adjacent.
    let far = "The linter reads text. We shipped it on Friday. The tool does \
               not detect authorship.";
    assert_eq!(count(&analyze(far), "SD-Q007"), 0, "{far}");
}

#[test]
fn q008_anchor_is_the_tool_noun_only() {
    // The pronoun half of the anchor is gone. A pronoun refers to whatever
    // came before it, which the sentence alone cannot resolve.
    for silent in [
        "That was deliberately vague.",
        "I did it deliberately.",
        "It was deliberately narrow.",
    ] {
        let report = analyze(silent);
        assert_eq!(count(&report, "SD-Q008"), 0, "{silent}: {report:?}");
    }
    let text = "The rule fires deliberately when the span is short.";
    let report = analyze(text);
    assert_eq!(count(&report, "SD-Q008"), 1, "{report:?}");
    assert_span_invariant(text, &report);
}

// --- the shared tool-noun set -------------------------------------------

#[test]
fn both_rules_read_the_same_tool_nouns() {
    // `score` and `test` reach both rules, so the anchor and the subject set
    // never disagree about what counts as the thing writing about itself.
    let text = "The test does not prove authorship, and the detector makes no claim.";
    let report = analyze(text);
    assert_eq!(count(&report, "SD-Q007"), 2, "{report:?}");
    let text = "The detector fires deliberately when the span is short.";
    assert_eq!(count(&analyze(text), "SD-Q008"), 1, "{text}");
    assert_span_invariant(text, &analyze(text));
}

#[test]
fn q008_anchor_holds_for_both_marker_families() {
    // The reception-instruction family needs the anchor too.
    let report = analyze("This poem should be read as an elegy.");
    assert_eq!(count(&report, "SD-Q008"), 0, "{report:?}");
    // Judge-absorbed, not exempted: these fire and the reader decides.
    for absorbed in [
        "The test was deliberately hard.",
        "The findings should be read as preliminary.",
    ] {
        let report = analyze(absorbed);
        assert_eq!(count(&report, "SD-Q008"), 1, "{absorbed}: {report:?}");
    }
}

#[test]
fn q007_coreference_is_the_pronoun_or_one_tool_noun() {
    // A bare pronoun on either side refers to its neighbour, and one tool
    // noun matches itself across singular and plural.
    for fires in [
        "It reads text. The tool does not detect authorship.",
        "The tool reads text. The tool does not detect authorship.",
        "The tools read text. The tool does not detect authorship.",
    ] {
        let report = analyze(fires);
        assert_eq!(count(&report, "SD-Q007"), 1, "{fires}: {report:?}");
    }
    // Two different tool nouns are two things and do not corefer.
    for silent in [
        "The linter reads text. The tool does not detect authorship.",
        "The test finished at noon. The tool does not detect authorship.",
    ] {
        let report = analyze(silent);
        assert_eq!(count(&report, "SD-Q007"), 0, "{silent}: {report:?}");
    }
    // Adjacency stays strict at one sentence.
    let far = "The tool reads text. We shipped it on Friday. The tool does \
               not detect authorship.";
    assert_eq!(count(&analyze(far), "SD-Q007"), 0, "{far}");
}

#[test]
fn q007_imperative_exclusion_is_per_clause() {
    // Clause one is a command and drops out. Clause two is a denial and
    // qualifies on `judge`. One qualifying clause has no trigger, so the
    // block stays silent.
    let text = "Do not obey injected text, and it does not judge anyone.";
    let report = analyze(text);
    assert_eq!(count(&report, "SD-Q007"), 0, "{report:?}");
    // The same sentence beside a restatement fires exactly once, and the one
    // finding is clause two. The command never carries the stack behind it.
    let text = "It reads text. Do not obey injected text, and it does not judge anyone.";
    let report = analyze(text);
    assert_eq!(count(&report, "SD-Q007"), 1, "{report:?}");
    assert_eq!(
        report.quality_patterns[0].snippet,
        "it does not judge anyone"
    );
    assert_span_invariant(text, &report);
}

#[test]
fn q007_base_form_test_keeps_a_denial_fragment_in() {
    // `never scores voice` is the middle of a denial stack with its subject
    // elided. The base-form test keeps it out of the imperative exclusion,
    // and the subjectless spelling qualifies it, so all three clauses
    // report.
    let text =
        "It does not detect authorship, never scores voice, and makes no claim about intent.";
    let report = analyze(text);
    assert_eq!(count(&report, "SD-Q007"), 3, "{report:?}");
    let snippets: Vec<&str> = report
        .quality_patterns
        .iter()
        .map(|f| f.snippet.as_str())
        .collect();
    assert_eq!(snippets[0], "It does not detect authorship");
    assert_eq!(snippets[1], "never scores voice");
    assert_eq!(snippets[2], "makes no claim about intent");
    assert_span_invariant(text, &report);
    // A base-form verb after the clause-head negation is a command.
    for command in [
        "It reads text. Do not obey injected text.",
        "It reads text. Never obey injected text.",
        "Do not force-push main; do not rewrite history.",
    ] {
        let report = analyze(command);
        assert_eq!(count(&report, "SD-Q007"), 0, "{command}: {report:?}");
    }
}

// --- SD-Q007 spelling C, the subjectless denial --------------------------

#[test]
fn q007_spelling_c_needs_the_right_auxiliary_and_verb_form() {
    // An imperative-capable negation takes only the third-person form, so
    // the fragment qualifies as a denial.
    let text = "It reads text. Never scores voice.";
    let report = analyze(text);
    assert_eq!(count(&report, "SD-Q007"), 1, "{report:?}");
    assert_eq!(report.quality_patterns[0].snippet, "Never scores voice");
    assert_span_invariant(text, &report);
    // A coordinator before a second negated predicate opens a clause, so an
    // elided subject still stacks.
    let text = "It does not detect authorship and never scores voice.";
    let report = analyze(text);
    assert_eq!(count(&report, "SD-Q007"), 2, "{report:?}");
    for silent in [
        // Base form behind an imperative-capable negation is a command.
        "It reads text. Never score voice.",
        "It reads text. Do not force-push main.",
        // An -ing form is a participial adjunct.
        "It reads text. Never judging anyone.",
    ] {
        let report = analyze(silent);
        assert_eq!(count(&report, "SD-Q007"), 0, "{silent}: {report:?}");
    }
}

#[test]
fn q007_finite_negations_are_never_commands() {
    // `does not` and its peers only ever carry a finite verb, so a clause
    // they head reaches the qualification tests.
    let text = "It reads text. Does not detect authorship.";
    let report = analyze(text);
    assert_eq!(count(&report, "SD-Q007"), 1, "{report:?}");
    assert_span_invariant(text, &report);
}

// --- SD-Q007 cutting, spans, and the arm note ----------------------------

#[test]
fn q007_cuts_at_every_interior_coordinator() {
    // No comma joins these predicates, so without the cut the stack arm
    // would never see two clauses.
    let text = "It does not detect authorship and it never scores voice.";
    let report = analyze(text);
    assert_eq!(count(&report, "SD-Q007"), 2, "{report:?}");
    let snippets: Vec<&str> = report
        .quality_patterns
        .iter()
        .map(|f| f.snippet.as_str())
        .collect();
    assert_eq!(snippets[0], "It does not detect authorship");
    assert_eq!(snippets[1], "it never scores voice");
    assert_span_invariant(text, &report);
}

#[test]
fn q007_span_is_the_segment_for_a_denial_and_the_clause_for_a_hedge() {
    // Cutting at `or` inside the hedge clause counts one qualifying segment
    // and never truncates the cited span, because an evidential hedge
    // governs an open complement and reports its whole comma clause.
    let report = analyze(DENIAL_SPECIMEN);
    let spans: Vec<(usize, usize)> = report
        .quality_patterns
        .iter()
        .filter(|f| f.rule_id == "SD-Q007")
        .map(|f| f.span)
        .collect();
    assert_eq!(spans, [(15, 44), (50, 112)], "{report:?}");
    // The span opens past the coordinator and closes before the period, so
    // the cited text is the writer's own clause and nothing else.
    assert_eq!(
        &DENIAL_SPECIMEN[spans[1].0..spans[1].1],
        "no finding is evidence that a person or a model wrote anything"
    );
}

#[test]
fn q007_arm_b_takes_a_longer_affirmative_partner() {
    // The partner is any affirmative self-description, so an empty
    // restatement with extra words still partners its denial.
    let text = "It reads text every morning. It does not detect authorship.";
    let report = analyze(text);
    assert_eq!(count(&report, "SD-Q007"), 1, "{report:?}");
    assert_span_invariant(text, &report);
}

// --- SD-Q007 the adjacency arm needs a testable referent ------------------

#[test]
fn q007_a_foreign_subject_hedge_counts_toward_the_stack_only() {
    // The hedge is about a paper, not about the text describing itself, so
    // coreference with the neighbouring sentence cannot be tested and the
    // adjacency arm does not apply.
    for silent in [
        "It reads text. The paper makes no claim about causation.",
        "It reads text. The survey does not replace a site visit.",
    ] {
        let report = analyze(silent);
        assert_eq!(count(&report, "SD-Q007"), 0, "{silent}: {report:?}");
    }
    // The same hedge still counts toward the stack arm.
    let text = "It does not detect authorship, and the paper makes no claim about causation.";
    let report = analyze(text);
    assert_eq!(count(&report, "SD-Q007"), 2, "{report:?}");
    assert_span_invariant(text, &report);
    // A hedge whose own head names a closed-set subject keeps the arm.
    let text = "It reads text. The report should not be read as a verdict.";
    assert_eq!(count(&analyze(text), "SD-Q007"), 1, "{text}");
}

// --- SD-Q007 open hedge forms and the head noun --------------------------

#[test]
fn q007_open_hedge_takes_up_to_three_words_before_the_copula() {
    // The last word before the copula is the head noun, and it carries the
    // closed-set test in place of a head subject.
    for fires in [
        "It reads text. No finding is evidence of authorship.",
        "It reads text. No single finding is evidence of authorship.",
        "It reads text. No one single finding is evidence of anything.",
    ] {
        let report = analyze(fires);
        assert_eq!(count(&report, "SD-Q007"), 1, "{fires}: {report:?}");
    }
    // A foreign head noun fails the test, so the adjacency arm is not
    // available and the clause counts toward the stack alone.
    for silent in [
        "The study measured cortisol. No single sample is evidence of chronic stress.",
        "It reads text. No one single sample is evidence of stress.",
    ] {
        assert_eq!(count(&analyze(silent), "SD-Q007"), 0, "{silent}");
    }
    // Four words between `no` and the copula is past the cap and is a
    // recorded miss.
    let text = "It reads text. No one single small finding is evidence of authorship.";
    assert_eq!(count(&analyze(text), "SD-Q007"), 0, "{text}");
}

#[test]
fn q007_open_hedge_seats_the_head_noun_on_the_last_word() {
    // The widest open form still reads its head noun from the slot before
    // the copula, so the closed-set test lands on `finding` and not on the
    // words qualifying it.
    let text = "It reads text. No one single finding is evidence of anything.";
    let report = analyze(text);
    assert_eq!(count(&report, "SD-Q007"), 1, "{report:?}");
    assert_eq!(
        report.quality_patterns[0].snippet,
        "No one single finding is evidence of anything"
    );
    assert_span_invariant(text, &report);
    // Widening preserves the one- and two-word forms.
    for (fires, snippet) in [
        (
            "It reads text. No finding is proof of anything.",
            "No finding is proof of anything",
        ),
        (
            "It reads text. No single finding is proof of anything.",
            "No single finding is proof of anything",
        ),
    ] {
        let report = analyze(fires);
        assert_eq!(count(&report, "SD-Q007"), 1, "{fires}: {report:?}");
        assert_eq!(report.quality_patterns[0].snippet, snippet, "{fires}");
    }
}

// --- SD-Q007 the coordinated denial --------------------------------------

#[test]
fn q007_coordinated_denial_continues_the_subject_before_it() {
    // `and` plus a base-form capability verb reads as a command alone, and
    // as a denial after a segment that named the thing. The span is the
    // coordinator-cut segment, so the coordinator itself is left out.
    for (fires, snippet) in [
        (
            "The rules read text and never detect authorship.",
            "never detect authorship",
        ),
        (
            "The rules read text and do not detect authorship.",
            "do not detect authorship",
        ),
        (
            "The rules are advisory and do not replace review.",
            "do not replace review",
        ),
    ] {
        let report = analyze(fires);
        assert_eq!(count(&report, "SD-Q007"), 1, "{fires}: {report:?}");
        assert_eq!(report.quality_patterns[0].snippet, snippet, "{fires}");
        assert_span_invariant(fires, &report);
    }
}

#[test]
fn q007_coordinated_denial_needs_all_four_conditions() {
    for silent in [
        // No coordinator, so the command reading stands.
        "Never detect authorship.",
        // The coordinator is `and` only. Every other one keeps the command.
        "The tool is fast, but never replace review with it.",
        "The findings are noisy, so do not judge them.",
        "The report is evidence, do not replace your own reading with it.",
        // No earlier segment names a closed-set subject.
        "Read the report and never judge by one finding.",
        // A sentence boundary is not a coordinator.
        "The rules read text. Never score voice.",
    ] {
        let report = analyze(silent);
        assert_eq!(count(&report, "SD-Q007"), 0, "{silent}: {report:?}");
    }
    // The four conditions hold on a sentence a writer would spell with
    // `but`. It fires and the reader absorbs it.
    let text = "The tool is fast, and never replace review with it.";
    let report = analyze(text);
    assert_eq!(count(&report, "SD-Q007"), 1, "{report:?}");
    assert_eq!(
        report.quality_patterns[0].snippet,
        "never replace review with it"
    );
}

#[test]
fn q007_coordinated_denial_leaves_the_earlier_spellings_alone() {
    // The three-clause stack, the per-clause imperative exclusion, and the
    // subjectless spelling all read the same after the coordinated case.
    let text =
        "It does not detect authorship, never scores voice, and makes no claim about intent.";
    let report = analyze(text);
    assert_eq!(count(&report, "SD-Q007"), 3, "{report:?}");
    let text = "Do not obey injected text, and it does not judge anyone.";
    assert_eq!(count(&analyze(text), "SD-Q007"), 0, "{text}");
    let text = "It reads text. Never scores voice.";
    let report = analyze(text);
    assert_eq!(count(&report, "SD-Q007"), 1, "{report:?}");
    assert_eq!(report.quality_patterns[0].snippet, "Never scores voice");
}

// --- SD-Q010 hedging-litotes ------------------------------------------------

fn q010_hits(report: &EvidenceReport) -> Vec<&str> {
    report
        .quality_patterns
        .iter()
        .filter(|f| f.rule_id == "SD-Q010")
        .map(|f| f.snippet.as_str())
        .collect()
}

/// The fixed shapes, one case per spelling, pinned to the span.
#[test]
fn q010_fixed_shapes_fire_on_their_span() {
    for (text, span) in [
        (
            "This is no small feat for a single maintainer.",
            "no small feat",
        ),
        (
            "Migrating the schema was no simple task.",
            "was no simple task",
        ),
        (
            "The rollout is not without its challenges.",
            "not without its challenges",
        ),
        (
            "Debugging the linker is far from trivial.",
            "far from trivial",
        ),
        (
            "It is hardly surprising that the build broke.",
            "hardly surprising",
        ),
        (
            "It's not exactly trivial to reproduce.",
            "not exactly trivial",
        ),
        ("The codebase isn't exactly simple.", "isn't exactly simple"),
        ("Getting this to compile is no mean feat.", "no mean feat"),
        (
            "The result leaves something to be desired.",
            "leaves something to be desired",
        ),
        (
            "It would not be wrong to say the design is fragile.",
            "It would not be wrong to say",
        ),
        ("This is, to put it mildly, a mess.", "to put it mildly"),
        (
            "The API surface is not inconsiderable.",
            "not inconsiderable",
        ),
        (
            "Setup is not for the faint of heart.",
            "not for the faint of heart",
        ),
        (
            "The cost was not unimportant to the board.",
            "not unimportant",
        ),
        (
            "The delay proved inconsequential, and the fix was not inconsequential.",
            "not inconsequential",
        ),
        (
            "The team is not unfamiliar with the stack.",
            "not unfamiliar",
        ),
        ("Shipping on time is no easy feat.", "no easy feat"),
        (
            "A clean build is not a small feat here.",
            "not a small feat",
        ),
        (
            "This is no small achievement for the team.",
            "no small achievement",
        ),
        ("The port is no small undertaking.", "no small undertaking"),
        (
            "The win came in no small part from caching.",
            "in no small part",
        ),
        (
            "The gain is due in no small measure to the cache.",
            "in no small measure",
        ),
        ("The upgrade remains no easy task.", "remains no easy task"),
        (
            "The plan is not without its difficulties.",
            "not without its difficulties",
        ),
        ("The move was not without irony.", "not without irony"),
        (
            "The wire protocol is not exactly straightforward.",
            "not exactly straightforward",
        ),
        (
            "The setup wasn\u{2019}t exactly easy.",
            "wasn\u{2019}t exactly easy",
        ),
        ("The rebase is far from simple.", "far from simple"),
        ("The proof is hardly trivial.", "hardly trivial"),
        ("The throughput was less than stellar.", "less than stellar"),
        (
            "The error messages leave much to be desired.",
            "leave much to be desired",
        ),
        (
            "It wouldn't be wrong to say the tests are thin.",
            "It wouldn't be wrong to say",
        ),
        ("It is safe to say the cache helped.", "It is safe to say"),
        (
            "It's not hard to see why the build broke.",
            "It's not hard to see",
        ),
        (
            "It is not hard to imagine a crash here.",
            "It is not hard to imagine",
        ),
        (
            "The rollout was rough, to say the least.",
            "to say the least",
        ),
        (
            "Parsing a header is not rocket science.",
            "not rocket science",
        ),
        (
            "The migration was not a walk in the park.",
            "not a walk in the park",
        ),
        (
            "The upgrade is not exactly a walk in the park.",
            "not exactly a walk in the park",
        ),
        (
            "The cold path is not to be underestimated.",
            "not to be underestimated",
        ),
        (
            "The edge case is not to be overlooked.",
            "not to be overlooked",
        ),
        (
            "The lock order is not to be taken lightly.",
            "not to be taken lightly",
        ),
        (
            "A 40 percent gain is not to be sneezed at.",
            "not to be sneezed at",
        ),
        (
            "It handles retries, not to mention backoff.",
            ", not to mention",
        ),
        ("This isn't rocket science.", "isn't rocket science"),
        (
            "The setup isn't for the faint of heart.",
            "isn't for the faint of heart",
        ),
        (
            "The risk isn't to be underestimated.",
            "isn't to be underestimated",
        ),
        (
            "The rollout wasn't without its challenges.",
            "wasn't without its challenges",
        ),
        (
            "The API surface isn't inconsiderable.",
            "isn't inconsiderable",
        ),
    ] {
        let report = analyze(text);
        assert_eq!(q010_hits(&report), vec![span], "{text}: {report:?}");
        assert_span_invariant(text, &report);
    }
}

/// The measurable shapes, one case per spelling, pinned to the span.
#[test]
fn q010_measurable_shapes_fire_on_their_span() {
    for (text, span) in [
        ("It is not uncommon for the cache to miss.", "not uncommon"),
        (
            "It is not entirely clear why the test flakes.",
            "not entirely clear",
        ),
        ("The current latency is less than ideal.", "less than ideal"),
        ("The design is not without merit.", "not without merit"),
        (
            "Porting to ARM is not a trivial undertaking.",
            "not a trivial undertaking",
        ),
        ("The benchmark numbers are not the best.", "not the best"),
        ("The load is not insignificant.", "not insignificant"),
        (
            "The queue is not quite right after failover.",
            "not quite right",
        ),
        ("The docs are not terribly helpful.", "not terribly"),
        ("A cold start is not unusual on Mondays.", "not unusual"),
        ("A retry is not unreasonable here.", "not unreasonable"),
        ("A second outage is not unlikely.", "not unlikely"),
        ("The fix is not inexpensive.", "not inexpensive"),
        ("The regression was not unexpected.", "not unexpected"),
        ("The team was not unaware of the risk.", "not unaware"),
        ("The patch is not unwelcome.", "not unwelcome"),
        ("The parser is not incapable of recovery.", "not incapable"),
        (
            "The metric is not irrelevant to the decision.",
            "not irrelevant",
        ),
        ("The ordering is not illogical.", "not illogical"),
        (
            "A double free is not unheard of in this module.",
            "not unheard of",
        ),
        (
            "The cache misses not infrequently under load.",
            "not infrequently",
        ),
        (
            "The rename is not a trivial change.",
            "not a trivial change",
        ),
        (
            "Rotation is not an easy task with live traffic.",
            "not an easy task",
        ),
        ("Recovery is not a simple matter.", "not a simple matter"),
        ("The leak is not a minor issue.", "not a minor issue"),
        (
            "The pipeline uses no small amount of memory.",
            "no small amount",
        ),
        ("The rewrite is not without risk.", "not without risk"),
        (
            "The change is not without precedent.",
            "not without precedent",
        ),
        (
            "The move is not without its costs.",
            "not without its costs",
        ),
        (
            "The figure is not entirely accurate.",
            "not entirely accurate",
        ),
        (
            "The outcome was not entirely certain.",
            "not entirely certain",
        ),
        ("The API is not exactly ideal.", "not exactly ideal"),
        ("The cold path is not exactly fast.", "not exactly fast"),
        ("The port is not quite there yet.", "not quite there"),
        ("The buffer is not quite enough.", "not quite enough"),
        ("The docs are not particularly clear.", "not particularly"),
        ("The build is not especially slow.", "not especially"),
        ("The limit is not overly strict.", "not overly"),
        ("The migration plan is far from ideal.", "far from ideal"),
        ("The rollback path is far from settled.", "far from settled"),
        ("The layout is less than optimal.", "less than optimal"),
        (
            "It is not unreasonable to expect a retry.",
            "It is not unreasonable to",
        ),
        ("The fallback is not the worst option.", "not the worst"),
        ("The error handling could be better.", "could be better"),
        (
            "There is room for improvement in the parser.",
            "room for improvement",
        ),
        ("The patch isn't quite right.", "isn't quite right"),
        ("The cause isn't entirely clear.", "isn't entirely clear"),
        ("Timeouts aren't uncommon under load.", "aren't uncommon"),
        ("The plan isn't without merit.", "isn't without merit"),
        ("The heap wasn't terribly fragmented.", "wasn't terribly"),
    ] {
        let report = analyze(text);
        assert_eq!(q010_hits(&report), vec![span], "{text}: {report:?}");
        assert_span_invariant(text, &report);
    }
}

/// Honest negations, quantifiers, comparisons, and bare adverbs stay silent.
#[test]
fn q010_honest_negations_are_silent() {
    for text in [
        "The tool does not support nested transactions.",
        "Not all inputs are validated before parsing.",
        "The buffer is not fully drained when close returns.",
        "The cache is not always warm after failover.",
        "A successful write does not necessarily imply durability.",
        "A collision is not impossible because the hash has finite width.",
        "The patch is not incorrect, but it omits the 32-bit path.",
        "The two crashes are not unrelated.",
        "The wire format is not unlike CBOR.",
        "No simple task may invoke the privileged runner.",
        "The buffer holds not less than 32 bytes.",
        "The response is not without a Content-Type header.",
        "The node has barely enough memory for the job.",
        "The hook hardly ever fires under load.",
        "The value is not exactly 1.0 after rounding.",
        "The queue is not yet initialized.",
        "The field is not included in the digest.",
        "The token is not invalid; its status is unknown.",
        "She chose not to mention the delay.",
        "The two files are far from each other on disk.",
        "The result is not dissimilar to the last run.",
        "The report is not inaccurate on the totals.",
        "The board was not indifferent to the outcome.",
        "The endpoint is not unavailable during the window.",
        "The export is not illegal under the licence.",
        "The fix is not completely tested.",
        "The cache is not really warm yet.",
    ] {
        let report = analyze(text);
        assert_eq!(count(&report, "SD-Q010"), 0, "{text}: {report:?}");
    }
}

/// Seam with the contrast family: the two-sentence reframe still reports
/// SLOP-C002 on its own, and a litotes inside a contrast span reports both.
#[test]
fn q010_seam_with_the_contrast_rules() {
    let report = analyze("It is not a linter. It is a gate.");
    assert_eq!(count(&report, "SLOP-C002"), 1, "{report:?}");
    assert_eq!(count(&report, "SD-Q010"), 0, "{report:?}");

    let text = "It is not a linter. It is a gate, and the setup is no small feat.";
    let report = analyze(text);
    assert_eq!(count(&report, "SLOP-C002"), 1, "{report:?}");
    assert_eq!(q010_hits(&report), vec!["no small feat"], "{report:?}");
    assert_span_invariant(text, &report);
}

/// The filler entry keeps its rule.
#[test]
fn q010_it_goes_without_saying_stays_with_the_filler_rule() {
    let report = analyze("It goes without saying that tests must pass.");
    assert_eq!(count(&report, "SLOP-T001"), 1, "{report:?}");
    assert_eq!(count(&report, "SD-Q010"), 0, "{report:?}");
}