pbfhogg 0.5.0

Fast OpenStreetMap PBF reader and writer for Rust. Read, write, and merge .osm.pbf files with pipelined parallel decoding.
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
//! CLI-driven integration tests for `pbfhogg degrade`.
//!
//! Fixtures are built with the stable-allowlist writer helpers; the
//! degrade command runs via the compiled `pbfhogg` binary through
//! `CliInvoker`; output is verified by reading the resulting PBF with
//! the stable-allowlist reader helpers and (where useful) by piping the
//! output through `pbfhogg sort` to confirm it round-trips back to the
//! original element set. No imports from `pbfhogg::commands::degrade` -
//! a rewrite of `src/commands/degrade/` cannot break these tests by
//! type changes alone.

#![allow(clippy::unwrap_used)]

mod common;

use std::path::Path;

use common::cli::CliInvoker;
use common::{
    TestNode, TestRelation, TestWay, assert_has_tagdata, assert_indexed, assert_no_tagdata,
    assert_no_tagdata_all_blobs, assert_non_indexed, assert_sorted_file, count_tagdata_blobs,
    generate_nodes, generate_relations, generate_ways, read_header, read_normalized,
    write_multi_block_test_pbf,
};
use pbfhogg::block_builder::{BlockBuilder, HeaderBuilder, MemberData, Metadata};
use pbfhogg::writer::{Compression, PbfWriter};
use pbfhogg::{BlobDecode, BlobReader, Element};

/// Build a sorted, multi-blob fixture: 60 nodes, 12 ways, 6 relations,
/// packed at 20 elements/blob. Yields 3 node blobs + 1 way blob + 1
/// relation blob = 5 OsmData blobs in the input.
fn write_degrade_fixture(path: &Path) -> (Vec<TestNode>, Vec<TestWay>, Vec<TestRelation>) {
    let mut nodes = generate_nodes(60, 1);
    // Tag elements in several distinct blobs (and across all three kinds) so
    // the fixture carries tagdata in more than one blob. With 20 elements per
    // blob the tagged nodes land in the first node blob (ids 1 and 8) and the
    // third node blob (id 43); tagging a way and a relation adds tagdata to
    // the way blob and the relation blob too. That makes the whole-file
    // `assert_no_tagdata_all_blobs` check on `--strip-tagdata` output
    // meaningful rather than a single-blob assertion in disguise.
    nodes[0].tags = vec![("place", "city"), ("name", "Origo")];
    nodes[7].tags = vec![("amenity", "cafe")];
    nodes[42].tags = vec![("highway", "bus_stop")];
    let mut ways = generate_ways(12, 1, 3, 1);
    ways[0].tags = vec![("highway", "residential")];
    let mut rels = generate_relations(6, 1, 2, 1);
    rels[0].tags = vec![("type", "route")];
    write_multi_block_test_pbf(path, &nodes, &ways, &rels, 20);
    (nodes, ways, rels)
}

/// The `HeaderBlock.bbox` the bbox fixture declares, in
/// `HeaderBuilder::bbox` order (left/bottom/right/top degrees). Chosen to
/// enclose every `generate_nodes` coordinate (node `n` sits at
/// `n * 1e-4` degrees, so `1..60` land in `[1e-4, 6e-3]`).
const FIXTURE_BBOX: (f64, f64, f64, f64) = (0.0, 0.0, 0.01, 0.01);

// Rich HeaderBlock metadata the bbox fixture carries beyond the bbox. Every
// one of these is a field that a `HeaderBuilder::from_header` rebuild would
// silently drop or replace (source and custom optional features have no
// `HeaderBuilder` encoder; the writingprogram would be reset to "pbfhogg"),
// so asserting they SURVIVE `--strip-bbox` pins that the passthrough path
// preserves the input HeaderBlock payload verbatim instead of rebuilding it.
const FIXTURE_WRITING_PROGRAM: &str = "degrade-test-writer/3.1";
const FIXTURE_SOURCE: &str = "survey-import-2019";
const FIXTURE_CUSTOM_FEATURE: &str = "Custom.Extension-v9";
const FIXTURE_REPL_TS: i64 = 1_700_000_000;
const FIXTURE_REPL_SEQ: i64 = 4242;
const FIXTURE_REPL_URL: &str = "https://example.org/replication";

/// Append a base-128 varint to `buf` (protobuf wire encoding).
fn push_varint(buf: &mut Vec<u8>, mut v: u64) {
    loop {
        let byte = (v & 0x7f) as u8;
        v >>= 7;
        if v != 0 {
            buf.push(byte | 0x80);
        } else {
            buf.push(byte);
            break;
        }
    }
}

/// Append a length-delimited (wire type 2) field to a protobuf message.
fn push_len_field(buf: &mut Vec<u8>, field: u32, data: &[u8]) {
    push_varint(buf, (u64::from(field) << 3) | 2);
    push_varint(buf, data.len() as u64);
    buf.extend_from_slice(data);
}

/// Build the rich HeaderBlock protobuf bytes the bbox fixtures share: a bbox,
/// sorted flag, a non-default writingprogram, a custom optional feature, the
/// three osmosis replication fields, and a `source` (field 17). `HeaderBuilder`
/// cannot emit `source`, so it is appended at the wire level.
fn rich_bbox_header_bytes() -> Vec<u8> {
    let (left, bottom, right, top) = FIXTURE_BBOX;
    let mut header = HeaderBuilder::new()
        .sorted()
        .bbox(left, bottom, right, top)
        .writing_program(FIXTURE_WRITING_PROGRAM)
        .optional_feature(FIXTURE_CUSTOM_FEATURE)
        .replication_timestamp(FIXTURE_REPL_TS)
        .replication_sequence_number(FIXTURE_REPL_SEQ)
        .replication_base_url(FIXTURE_REPL_URL)
        .build()
        .expect("build header");
    // Field 17: source. Protobuf fields may appear in any order, so appending
    // is valid; the reader picks it up as HeaderBlock.source.
    push_len_field(&mut header, 17, FIXTURE_SOURCE.as_bytes());
    header
}

/// Write the three element kinds into `writer`, one block per kind.
fn write_fixture_elements(
    writer: &mut PbfWriter<impl std::io::Write>,
    nodes: &[TestNode],
    ways: &[TestWay],
    rels: &[TestRelation],
) {
    let no_meta: Option<&Metadata> = None;
    let mut bb = BlockBuilder::new();
    for n in nodes {
        bb.add_node(n.id, n.lat, n.lon, n.tags.iter().copied(), no_meta);
    }
    if let Some(bytes) = bb.take().expect("take") {
        writer.write_primitive_block(bytes).expect("write nodes");
    }
    for w in ways {
        bb.add_way(w.id, w.tags.iter().copied(), &w.refs, no_meta);
    }
    if let Some(bytes) = bb.take().expect("take") {
        writer.write_primitive_block(bytes).expect("write ways");
    }
    for r in rels {
        let members: Vec<MemberData<'_>> = r
            .members
            .iter()
            .map(|m| MemberData {
                id: m.id,
                role: m.role,
            })
            .collect();
        bb.add_relation(r.id, r.tags.iter().copied(), &members, no_meta);
    }
    if let Some(bytes) = bb.take().expect("take") {
        writer
            .write_primitive_block(bytes)
            .expect("write relations");
    }
}

/// Build a sorted, indexed, multi-blob fixture that carries a rich header:
/// a `HeaderBlock.bbox`, a non-default writingprogram, a custom optional
/// feature, replication metadata, and a `source`. Written via
/// `PbfWriter::to_path`, which embeds `indexdata`, so the output is both
/// indexed (extractable) and bbox-bearing.
fn write_bbox_fixture(path: &Path) -> (Vec<TestNode>, Vec<TestWay>, Vec<TestRelation>) {
    let nodes = generate_nodes(60, 1);
    let ways = generate_ways(12, 1, 3, 1);
    let rels = generate_relations(6, 1, 2, 1);

    let header = rich_bbox_header_bytes();
    let mut writer =
        PbfWriter::to_path(path, Compression::default(), &header).expect("create writer");
    write_fixture_elements(&mut writer, &nodes, &ways, &rels);
    writer.flush().expect("flush");
    (nodes, ways, rels)
}

/// Assert the rich non-bbox header fields the bbox fixture declares are all
/// present. Used as an input precondition so the survival assertions after a
/// `--strip-bbox` are not vacuous.
fn assert_rich_header_fields_present(path: &Path) {
    let h = read_header(path);
    assert_eq!(h.writing_program(), Some(FIXTURE_WRITING_PROGRAM));
    assert_eq!(h.source(), Some(FIXTURE_SOURCE));
    assert_eq!(h.osmosis_replication_timestamp(), Some(FIXTURE_REPL_TS));
    assert_eq!(
        h.osmosis_replication_sequence_number(),
        Some(FIXTURE_REPL_SEQ)
    );
    assert_eq!(h.osmosis_replication_base_url(), Some(FIXTURE_REPL_URL));
    assert!(
        h.optional_features()
            .iter()
            .any(|f| f == FIXTURE_CUSTOM_FEATURE),
        "fixture must declare the custom optional feature"
    );
    assert!(h.is_sorted());
    assert!(h.bbox().is_some());
}

/// Assert every rich non-bbox header field survived a transform verbatim.
fn assert_rich_header_fields_survived(path: &Path) {
    let h = read_header(path);
    assert_eq!(
        h.writing_program(),
        Some(FIXTURE_WRITING_PROGRAM),
        "writingprogram must survive (a HeaderBuilder rebuild would reset it to pbfhogg)"
    );
    assert_eq!(
        h.source(),
        Some(FIXTURE_SOURCE),
        "source (field 17) must survive (HeaderBuilder cannot even emit it)"
    );
    assert_eq!(h.osmosis_replication_timestamp(), Some(FIXTURE_REPL_TS));
    assert_eq!(
        h.osmosis_replication_sequence_number(),
        Some(FIXTURE_REPL_SEQ)
    );
    assert_eq!(h.osmosis_replication_base_url(), Some(FIXTURE_REPL_URL));
    assert!(
        h.optional_features()
            .iter()
            .any(|f| f == FIXTURE_CUSTOM_FEATURE),
        "custom optional feature must survive (a HeaderBuilder rebuild drops it)"
    );
    assert!(h.is_sorted(), "Sort.Type_then_ID must survive");
}

/// Extract the raw byte frames of every `OSMData` blob in `path`, in file
/// order. Used to assert the passthrough path copies OsmData frames verbatim
/// (byte-for-byte) rather than re-encoding them. Walks the PBF wire envelope
/// `[4-byte BE header_len][BlobHeader][Blob]` directly and parses each
/// BlobHeader's type (field 1) and datasize (field 3).
fn osm_data_frames(path: &Path) -> Vec<Vec<u8>> {
    fn read_varint_at(buf: &[u8], pos: &mut usize) -> u64 {
        let mut result = 0u64;
        let mut shift = 0u32;
        loop {
            let byte = buf[*pos];
            *pos += 1;
            result |= u64::from(byte & 0x7f) << shift;
            if byte & 0x80 == 0 {
                break;
            }
            shift += 7;
        }
        result
    }

    let data = std::fs::read(path).expect("read pbf");
    let mut pos = 0usize;
    let mut frames = Vec::new();
    while pos < data.len() {
        let frame_start = pos;
        let header_len =
            u32::from_be_bytes([data[pos], data[pos + 1], data[pos + 2], data[pos + 3]]) as usize;
        pos += 4;
        let header_end = pos + header_len;
        let mut hpos = pos;
        let mut blob_type = String::new();
        let mut datasize = 0usize;
        while hpos < header_end {
            let tag = read_varint_at(&data, &mut hpos);
            let field = tag >> 3;
            let wire = tag & 7;
            match (field, wire) {
                (1, 2) => {
                    let len = usize::try_from(read_varint_at(&data, &mut hpos)).expect("len fits");
                    blob_type = String::from_utf8(data[hpos..hpos + len].to_vec()).expect("utf8");
                    hpos += len;
                }
                (3, 0) => {
                    datasize =
                        usize::try_from(read_varint_at(&data, &mut hpos)).expect("datasize fits");
                }
                (_, 0) => {
                    read_varint_at(&data, &mut hpos);
                }
                (_, 2) => {
                    let len = usize::try_from(read_varint_at(&data, &mut hpos)).expect("len fits");
                    hpos += len;
                }
                _ => panic!("unexpected wire type {wire} in BlobHeader"),
            }
        }
        let frame_end = header_end + datasize;
        if blob_type == "OSMData" {
            frames.push(data[frame_start..frame_end].to_vec());
        }
        pos = frame_end;
    }
    frames
}

/// Build a non-indexed, bbox-bearing fixture via the sync `PbfWriter::new`
/// path (which does not embed `indexdata`). Carries the same rich header as
/// `write_bbox_fixture`. Used to prove `--strip-bbox` needs no indexdata
/// precondition: the passthrough path never calls `require_indexdata`, so the
/// run must succeed on a non-indexed input without `--force` (the decode path
/// would error out).
fn write_non_indexed_bbox_fixture(path: &Path) -> (Vec<TestNode>, Vec<TestWay>, Vec<TestRelation>) {
    let nodes = generate_nodes(60, 1);
    let ways = generate_ways(12, 1, 3, 1);
    let rels = generate_relations(6, 1, 2, 1);

    let file = std::fs::File::create(path).expect("create file");
    let buf = std::io::BufWriter::new(file);
    let mut writer = PbfWriter::new(buf, Compression::default());
    let header = rich_bbox_header_bytes();
    writer.write_header(&header).expect("write header");

    // write_primitive_block_no_indexdata keeps the OsmData BlobHeaders free of
    // the indexdata field, so the fixture is genuinely non-indexed.
    let no_meta: Option<&Metadata> = None;
    let mut bb = BlockBuilder::new();
    for n in &nodes {
        bb.add_node(n.id, n.lat, n.lon, n.tags.iter().copied(), no_meta);
    }
    if let Some(bytes) = bb.take().expect("take") {
        writer
            .write_primitive_block_no_indexdata(bytes)
            .expect("write nodes");
    }
    for w in &ways {
        bb.add_way(w.id, w.tags.iter().copied(), &w.refs, no_meta);
    }
    if let Some(bytes) = bb.take().expect("take") {
        writer
            .write_primitive_block_no_indexdata(bytes)
            .expect("write ways");
    }
    for r in &rels {
        let members: Vec<MemberData<'_>> = r
            .members
            .iter()
            .map(|m| MemberData {
                id: m.id,
                role: m.role,
            })
            .collect();
        bb.add_relation(r.id, r.tags.iter().copied(), &members, no_meta);
    }
    if let Some(bytes) = bb.take().expect("take") {
        writer
            .write_primitive_block_no_indexdata(bytes)
            .expect("write relations");
    }
    writer.flush().expect("flush");
    (nodes, ways, rels)
}

/// Build a sorted, indexed fixture whose ways carry inline `LocationsOnWays`
/// coordinates and whose header declares the `LocationsOnWays` optional
/// feature. Used to make the standalone `--strip-locations` assertion
/// non-vacuous: the input genuinely declares LOW, so clearing it is a real
/// change rather than a no-op on a header that never had it.
fn write_low_fixture(path: &Path) -> (Vec<TestNode>, Vec<TestWay>, Vec<TestRelation>) {
    let nodes = generate_nodes(60, 1);
    let ways = generate_ways(12, 1, 3, 1);
    let rels = generate_relations(6, 1, 2, 1);

    let header = HeaderBuilder::new()
        .sorted()
        .optional_feature("LocationsOnWays")
        .build()
        .expect("build header");
    let mut writer =
        PbfWriter::to_path(path, Compression::default(), &header).expect("create writer");

    let no_meta: Option<&Metadata> = None;
    let mut bb = BlockBuilder::new();
    for n in &nodes {
        bb.add_node(n.id, n.lat, n.lon, n.tags.iter().copied(), no_meta);
    }
    if let Some(bytes) = bb.take().expect("take") {
        writer.write_primitive_block(bytes).expect("write nodes");
    }
    for w in &ways {
        // Give every ref an inline coordinate so the way genuinely carries
        // LocationsOnWays data (not just the header feature flag). Exact
        // values are irrelevant to the strip; a fixed decimicro pair suffices.
        let locations: Vec<(i32, i32)> = w.refs.iter().map(|_| (1_000_000, 2_000_000)).collect();
        bb.add_way_with_locations(w.id, w.tags.iter().copied(), &w.refs, &locations, no_meta);
    }
    if let Some(bytes) = bb.take().expect("take") {
        writer.write_primitive_block(bytes).expect("write ways");
    }
    for r in &rels {
        let members: Vec<MemberData<'_>> = r
            .members
            .iter()
            .map(|m| MemberData {
                id: m.id,
                role: m.role,
            })
            .collect();
        bb.add_relation(r.id, r.tags.iter().copied(), &members, no_meta);
    }
    if let Some(bytes) = bb.take().expect("take") {
        writer
            .write_primitive_block(bytes)
            .expect("write relations");
    }
    writer.flush().expect("flush");
    (nodes, ways, rels)
}

/// Build a sorted fixture whose input blobs are deliberately *smaller*
/// than the `--block-cap` the unsort tests use (4 elements/blob vs cap
/// 10). This is the regime that distinguishes the two unsort modes and
/// reproduces the real-world bug: when input blobs are smaller than the
/// cap, the buggy per-input-blob boundary flush confines the swap to one
/// output blob (intra-blob inversion) instead of producing the documented
/// cross-blob overlap. Each kind has well over `cap + 1` elements so the
/// swap fires for all three kinds.
fn write_unsort_fixture(path: &Path) -> (Vec<TestNode>, Vec<TestWay>, Vec<TestRelation>) {
    let nodes = generate_nodes(60, 1);
    let ways = generate_ways(24, 1, 3, 1);
    let rels = generate_relations(24, 1, 2, 1);
    write_multi_block_test_pbf(path, &nodes, &ways, &rels, 4);
    (nodes, ways, rels)
}

/// The `--block-cap` the unsort tests pass. Larger than the unsort
/// fixture's 4-element input blobs so the two modes diverge.
const UNSORT_CAP: &str = "10";

/// Build a sorted fixture whose input blobs are deliberately *larger* than
/// the `--block-cap` the large-blob test uses (20 elements/blob vs cap 5).
/// This is the regime that exposed finding 1: one input blob carrying more
/// than `cap` same-kind elements. Keying the swap to the `cap` boundary
/// (the old shared logic) made `--unsort-intra` fill and flush an output
/// block here, producing the cross-blob overlap shape instead of an
/// intra-blob inversion. The fix keys `--unsort-intra`'s swap to the first
/// two elements, so it stays inside the first output block regardless of
/// input blob size. Each kind has well over `cap` elements.
fn write_large_blob_unsort_fixture(
    path: &Path,
) -> (Vec<TestNode>, Vec<TestWay>, Vec<TestRelation>) {
    let nodes = generate_nodes(60, 1);
    let ways = generate_ways(60, 1, 3, 1);
    let rels = generate_relations(60, 1, 2, 1);
    write_multi_block_test_pbf(path, &nodes, &ways, &rels, 20);
    (nodes, ways, rels)
}

/// The `--block-cap` the large-blob test passes. Smaller than the
/// large-blob fixture's 20-element input blobs so a single input blob
/// spans more than one output block.
const LARGE_BLOB_CAP: &str = "5";

/// Per-blob `(kind, ordered element ids)` from the output file. Element
/// ids are returned in stream order so callers can detect intra-blob
/// inversions (a descending step within one blob).
fn blob_elements(path: &Path) -> Vec<(BlobKindLabel, Vec<i64>)> {
    let reader = BlobReader::from_path(path).expect("open pbf");
    let mut out = Vec::new();
    for blob in reader {
        let blob = blob.expect("read blob");
        if let BlobDecode::OsmData(block) = blob.decode().expect("decode blob") {
            let mut ids = Vec::new();
            let mut nodes = 0;
            let mut ways = 0;
            for element in block.elements() {
                match element {
                    Element::Node(n) => {
                        nodes += 1;
                        ids.push(n.id());
                    }
                    Element::DenseNode(dn) => {
                        nodes += 1;
                        ids.push(dn.id());
                    }
                    Element::Way(w) => {
                        ways += 1;
                        ids.push(w.id());
                    }
                    Element::Relation(r) => {
                        ids.push(r.id());
                    }
                    _ => {}
                }
            }
            let kind = if nodes > 0 {
                BlobKindLabel::Node
            } else if ways > 0 {
                BlobKindLabel::Way
            } else {
                BlobKindLabel::Relation
            };
            out.push((kind, ids));
        }
    }
    out
}

/// Count adjacent same-kind blob pairs with overlapping ID ranges
/// (`max_id` of one blob >= `min_id` of the next same-kind blob). The CLI
/// promises exactly one such overlap per eligible kind under `--unsort`,
/// so tests assert on the count, not just presence.
fn count_adjacent_overlaps(
    blobs: &[(BlobKindLabel, i64, i64, usize)],
    kind: BlobKindLabel,
) -> usize {
    let same: Vec<_> = blobs.iter().filter(|(k, ..)| *k == kind).collect();
    same.windows(2)
        .filter(|w| {
            let (_, _, a_max, _) = w[0];
            let (_, b_min, _, _) = w[1];
            a_max >= b_min
        })
        .count()
}

/// Count strictly-descending steps (internal ID inversions) across all
/// blobs of `kind`. Each unsort swap contributes exactly one, so
/// `--unsort-intra` tests assert this equals one per eligible kind and
/// `--unsort` tests assert it equals zero.
fn count_intra_blob_inversions(blobs: &[(BlobKindLabel, Vec<i64>)], kind: BlobKindLabel) -> usize {
    blobs
        .iter()
        .filter(|(k, _)| *k == kind)
        .map(|(_, ids)| ids.windows(2).filter(|w| w[0] > w[1]).count())
        .sum()
}

/// Per-blob `(kind, min_id, max_id, count)` from the output file. Used
/// to assert overlap structure after `--unsort`.
fn blob_index_summary(path: &Path) -> Vec<(BlobKindLabel, i64, i64, usize)> {
    let reader = BlobReader::from_path(path).expect("open pbf");
    let mut out = Vec::new();
    for blob in reader {
        let blob = blob.expect("read blob");
        if let BlobDecode::OsmData(block) = blob.decode().expect("decode blob") {
            let mut min_id = i64::MAX;
            let mut max_id = i64::MIN;
            let mut nodes = 0;
            let mut ways = 0;
            let mut rels = 0;
            for element in block.elements() {
                match element {
                    Element::Node(n) => {
                        nodes += 1;
                        min_id = min_id.min(n.id());
                        max_id = max_id.max(n.id());
                    }
                    Element::DenseNode(dn) => {
                        nodes += 1;
                        min_id = min_id.min(dn.id());
                        max_id = max_id.max(dn.id());
                    }
                    Element::Way(w) => {
                        ways += 1;
                        min_id = min_id.min(w.id());
                        max_id = max_id.max(w.id());
                    }
                    Element::Relation(r) => {
                        rels += 1;
                        min_id = min_id.min(r.id());
                        max_id = max_id.max(r.id());
                    }
                    _ => {}
                }
            }
            let kind = if nodes > 0 {
                BlobKindLabel::Node
            } else if ways > 0 {
                BlobKindLabel::Way
            } else {
                BlobKindLabel::Relation
            };
            let count = nodes + ways + rels;
            out.push((kind, min_id, max_id, count));
        }
    }
    out
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum BlobKindLabel {
    Node,
    Way,
    Relation,
}

// ---------------------------------------------------------------------------
// --strip-indexdata
// ---------------------------------------------------------------------------

/// `--strip-indexdata` clears the BlobHeader.indexdata field on every
/// OsmData blob. Element semantics, sortedness, and `LocationsOnWays`
/// (when set) all pass through unchanged because the blob payload is
/// not touched.
#[test]
fn degrade_strip_indexdata_drops_indexdata() {
    let dir = tempfile::tempdir().expect("tempdir");
    let input = dir.path().join("in.osm.pbf");
    let output = dir.path().join("out.osm.pbf");

    write_degrade_fixture(&input);
    assert_indexed(&input);

    CliInvoker::new()
        .arg("degrade")
        .arg(&input)
        .arg("-o")
        .arg(&output)
        .arg("--strip-indexdata")
        .assert_success();

    assert_non_indexed(&output);

    // Sortedness preserved (the blob payload is unchanged; only the
    // BlobHeader.indexdata is cleared).
    assert!(
        read_header(&output).is_sorted(),
        "--strip-indexdata should not clear Sort.Type_then_ID"
    );

    // Element semantics preserved.
    let original = read_normalized(&input);
    let degraded = read_normalized(&output);
    assert_eq!(original.nodes, degraded.nodes);
    assert_eq!(original.ways, degraded.ways);
    assert_eq!(original.relations, degraded.relations);
}

// ---------------------------------------------------------------------------
// --strip-locations
// ---------------------------------------------------------------------------

/// `--strip-locations` clears the `LocationsOnWays` header feature. The
/// fixture genuinely declares LOW (its ways carry inline coordinates and the
/// header sets the optional feature), so the assertion below is non-vacuous:
/// the flag really removes a feature the input had, rather than confirming a
/// no-op on a header that never declared it. Element data (ids, tags, refs,
/// members) round-trips through the BlockBuilder re-encode.
#[test]
fn degrade_strip_locations_clears_low_and_preserves_elements() {
    let dir = tempfile::tempdir().expect("tempdir");
    let input = dir.path().join("in.osm.pbf");
    let output = dir.path().join("out.osm.pbf");

    write_low_fixture(&input);
    // Precondition: the input actually declares LocationsOnWays, so clearing
    // it is a real change.
    assert!(
        read_header(&input).has_locations_on_ways(),
        "fixture must declare LocationsOnWays for the strip to be meaningful"
    );

    CliInvoker::new()
        .arg("degrade")
        .arg(&input)
        .arg("-o")
        .arg(&output)
        .arg("--strip-locations")
        .assert_success();

    assert!(
        !read_header(&output).has_locations_on_ways(),
        "--strip-locations output must not declare LocationsOnWays"
    );

    let original = read_normalized(&input);
    let degraded = read_normalized(&output);
    assert_eq!(original.nodes, degraded.nodes);
    assert_eq!(original.ways, degraded.ways);
    assert_eq!(original.relations, degraded.relations);
}

// ---------------------------------------------------------------------------
// --strip-tagdata
// ---------------------------------------------------------------------------

/// `--strip-tagdata` clears the BlobHeader.tagdata field (the per-blob tag
/// key index) on every OsmData blob, forcing `tags-filter`'s no-hint
/// fallback path. Like `--strip-indexdata` it is a header-only passthrough:
/// indexdata, sortedness, and every element property pass through unchanged
/// because the blob payload is not touched. Crucially it leaves indexdata
/// intact - a tagdata-stripped file is still indexed.
#[test]
fn degrade_strip_tagdata_drops_tagdata() {
    let dir = tempfile::tempdir().expect("tempdir");
    let input = dir.path().join("in.osm.pbf");
    let output = dir.path().join("out.osm.pbf");

    write_degrade_fixture(&input);
    // Precondition: the fixture carries tagdata in more than one blob (tagged
    // nodes in two node blobs plus a tagged way and relation), so a stripper
    // that only cleared the first blob would be caught by the whole-file walk
    // below rather than passing a single-blob assertion.
    assert_has_tagdata(&input);
    assert!(
        count_tagdata_blobs(&input) > 1,
        "fixture must carry tagdata in more than one blob to make the \
         whole-file strip assertion meaningful, got {}",
        count_tagdata_blobs(&input)
    );
    assert_indexed(&input);

    CliInvoker::new()
        .arg("degrade")
        .arg(&input)
        .arg("-o")
        .arg(&output)
        .arg("--strip-tagdata")
        .assert_success();

    // Every output blob must be free of tagdata, not just the first.
    assert_no_tagdata_all_blobs(&output);

    // indexdata is preserved (only tagdata is targeted). The passthrough keeps
    // the original indexdata bytes verbatim, so the file is still indexed.
    assert_indexed(&output);

    // Sortedness preserved (the blob payload is unchanged; only the
    // BlobHeader.tagdata is cleared).
    assert!(
        read_header(&output).is_sorted(),
        "--strip-tagdata should not clear Sort.Type_then_ID"
    );

    // Element semantics preserved.
    let original = read_normalized(&input);
    let degraded = read_normalized(&output);
    assert_eq!(original.nodes, degraded.nodes);
    assert_eq!(original.ways, degraded.ways);
    assert_eq!(original.relations, degraded.relations);
}

// ---------------------------------------------------------------------------
// --strip-bbox
// ---------------------------------------------------------------------------

/// `--strip-bbox` clears the `HeaderBlock.bbox` while leaving every OsmData
/// blob untouched: it is a header-only passthrough, so indexdata, tagdata,
/// sortedness, and the element multiset all survive.
#[test]
fn degrade_strip_bbox_clears_header_bbox() {
    let dir = tempfile::tempdir().expect("tempdir");
    let input = dir.path().join("in.osm.pbf");
    let output = dir.path().join("out.osm.pbf");

    write_bbox_fixture(&input);
    // Precondition: the fixture actually declares a bbox AND a full set of
    // other header fields, so the strip is meaningful and the survival
    // assertions below are not vacuous.
    assert!(
        read_header(&input).bbox().is_some(),
        "fixture must declare a HeaderBlock.bbox for the strip to be meaningful"
    );
    assert_rich_header_fields_present(&input);
    assert_indexed(&input);

    CliInvoker::new()
        .arg("degrade")
        .arg(&input)
        .arg("-o")
        .arg(&output)
        .arg("--strip-bbox")
        .assert_success();

    assert!(
        read_header(&output).bbox().is_none(),
        "--strip-bbox output must not declare a HeaderBlock.bbox"
    );

    // The bbox is the ONLY header field that changes: source, writingprogram,
    // the custom optional feature, replication metadata, and sortedness all
    // survive verbatim. A HeaderBuilder rebuild would have dropped or reset
    // several of these, so this pins the verbatim-passthrough contract.
    assert_rich_header_fields_survived(&output);

    // Header-only passthrough: indexdata is untouched.
    assert_indexed(&output);

    // The OsmData blob frames are copied byte-for-byte - the strip touches
    // only the OSMHeader.
    assert_eq!(
        osm_data_frames(&output),
        osm_data_frames(&input),
        "--strip-bbox must leave every OsmData frame byte-identical"
    );

    let original = read_normalized(&input);
    let degraded = read_normalized(&output);
    assert_eq!(original.nodes, degraded.nodes);
    assert_eq!(original.ways, degraded.ways);
    assert_eq!(original.relations, degraded.relations);
}

/// `--strip-bbox` on a *non-indexed* bbox-bearing input succeeds without
/// `--force`, proving it carries no indexdata precondition and does not
/// switch to the decode path. The decode path calls `require_indexdata` and
/// would error on a non-indexed input without `--force`; a clean success
/// therefore proves the run stayed on the header-only passthrough. The output
/// stays non-indexed, the bbox is gone, the other header fields survive, and
/// the OsmData frames are copied verbatim.
#[test]
fn degrade_strip_bbox_no_indexdata_uses_passthrough() {
    let dir = tempfile::tempdir().expect("tempdir");
    let input = dir.path().join("in.osm.pbf");
    let output = dir.path().join("out.osm.pbf");

    write_non_indexed_bbox_fixture(&input);
    assert_non_indexed(&input);
    assert!(read_header(&input).bbox().is_some());
    assert_rich_header_fields_present(&input);

    // No --force: a decode-path dispatch would fail the indexdata precondition.
    CliInvoker::new()
        .arg("degrade")
        .arg(&input)
        .arg("-o")
        .arg(&output)
        .arg("--strip-bbox")
        .assert_success();

    assert!(read_header(&output).bbox().is_none());
    // Passthrough on a non-indexed input leaves it non-indexed.
    assert_non_indexed(&output);
    assert_rich_header_fields_survived(&output);
    assert_eq!(
        osm_data_frames(&output),
        osm_data_frames(&input),
        "non-indexed --strip-bbox must copy OsmData frames byte-for-byte"
    );

    let original = read_normalized(&input);
    let degraded = read_normalized(&output);
    assert_eq!(original.nodes, degraded.nodes);
    assert_eq!(original.ways, degraded.ways);
    assert_eq!(original.relations, degraded.relations);
}

/// `--strip-bbox --strip-indexdata` composes on the passthrough path: the
/// bbox is dropped from the OSMHeader *and* indexdata is cleared from every
/// OsmData blob, while sortedness and the element multiset survive.
#[test]
fn degrade_strip_bbox_and_strip_indexdata_compose() {
    let dir = tempfile::tempdir().expect("tempdir");
    let input = dir.path().join("in.osm.pbf");
    let output = dir.path().join("out.osm.pbf");

    write_bbox_fixture(&input);
    assert!(read_header(&input).bbox().is_some());
    assert_indexed(&input);

    CliInvoker::new()
        .arg("degrade")
        .arg(&input)
        .arg("-o")
        .arg(&output)
        .arg("--strip-bbox")
        .arg("--strip-indexdata")
        .assert_success();

    assert!(read_header(&output).bbox().is_none());
    assert_non_indexed(&output);
    assert!(read_header(&output).is_sorted());

    let original = read_normalized(&input);
    let degraded = read_normalized(&output);
    assert_eq!(original.nodes, degraded.nodes);
    assert_eq!(original.ways, degraded.ways);
    assert_eq!(original.relations, degraded.relations);
}

/// `--strip-bbox --strip-locations` composes across the path boundary:
/// `--strip-locations` forces the decode path, and `--strip-bbox` must still
/// drop the bbox from the rebuilt output header. Confirms the bbox strip is
/// wired into the decode path's header construction, not just the
/// passthrough path's.
#[test]
fn degrade_strip_bbox_and_strip_locations_compose() {
    let dir = tempfile::tempdir().expect("tempdir");
    let input = dir.path().join("in.osm.pbf");
    let output = dir.path().join("out.osm.pbf");

    write_bbox_fixture(&input);
    assert!(read_header(&input).bbox().is_some());

    CliInvoker::new()
        .arg("degrade")
        .arg(&input)
        .arg("-o")
        .arg(&output)
        .arg("--strip-bbox")
        .arg("--strip-locations")
        .assert_success();

    assert!(
        read_header(&output).bbox().is_none(),
        "--strip-bbox output must not declare a HeaderBlock.bbox (decode path)"
    );
    assert!(
        !read_header(&output).has_locations_on_ways(),
        "--strip-locations output must not declare LocationsOnWays"
    );

    let original = read_normalized(&input);
    let degraded = read_normalized(&output);
    assert_eq!(original.nodes, degraded.nodes);
    assert_eq!(original.ways, degraded.ways);
    assert_eq!(original.relations, degraded.relations);
}

/// `extract --bbox` on the stripped output produces the same elements as
/// `extract --bbox` on the original bbox-bearing input. `extract` derives
/// its region purely from the CLI `--bbox` argument and prunes blobs with
/// the per-blob `indexdata` bboxes; it never consults the `HeaderBlock`
/// bbox, so removing it is inert for extract correctness. This pins that
/// invariant end-to-end: the degraded file stays a valid extract input.
#[test]
fn degrade_strip_bbox_extract_bbox_matches_original() {
    let dir = tempfile::tempdir().expect("tempdir");
    let input = dir.path().join("in.osm.pbf");
    let stripped = dir.path().join("stripped.osm.pbf");
    let extract_orig = dir.path().join("extract_orig.osm.pbf");
    let extract_stripped = dir.path().join("extract_stripped.osm.pbf");

    write_bbox_fixture(&input);

    CliInvoker::new()
        .arg("degrade")
        .arg(&input)
        .arg("-o")
        .arg(&stripped)
        .arg("--strip-bbox")
        .assert_success();
    assert!(read_header(&stripped).bbox().is_none());

    // A sub-region of the fixture's coordinate span so the extract is
    // non-empty but not the whole file. osmium order: minlon,minlat,maxlon,maxlat.
    let region = "0.0,0.0,0.00055,0.00055";
    for (src, out) in [(&input, &extract_orig), (&stripped, &extract_stripped)] {
        CliInvoker::new()
            .arg("extract")
            .arg(src)
            .arg("-o")
            .arg(out)
            .arg("--bbox")
            .arg(region)
            .assert_success();
    }

    let orig = read_normalized(&extract_orig);
    let strip = read_normalized(&extract_stripped);
    let full = read_normalized(&input);
    // Guard against a vacuous (empty) extract passing trivially.
    assert!(
        !orig.nodes.is_empty(),
        "extract region must select at least one node"
    );
    // Prove the spatial filter really ran: the sub-region must select
    // strictly fewer nodes than the whole file. If extract returned every
    // element the "matches" comparison below would be trivially satisfiable
    // by a no-op passthrough.
    assert!(
        orig.nodes.len() < full.nodes.len(),
        "extract must select strictly fewer than all {} nodes, got {}",
        full.nodes.len(),
        orig.nodes.len()
    );
    assert_eq!(orig.nodes, strip.nodes);
    assert_eq!(orig.ways, strip.ways);
    assert_eq!(orig.relations, strip.relations);
}

/// `--strip-bbox --generator <name>` on a passthrough-eligible bbox-bearing
/// input takes the *rebuild* branch of `passthrough_header_bytes`, not the
/// verbatim-forward branch: `--generator` is a `HeaderOverrides` override, so
/// `passthrough_header_bytes` rebuilds the header via `HeaderBuilder` (bbox
/// omitted under `--strip-bbox`) instead of forwarding the decompressed
/// `HeaderBlock` protobuf field-for-field. Two independent signals pin this:
/// the bbox is gone (proving the strip still applied) AND `writingprogram`
/// equals the override rather than the fixture's original value (proving the
/// override actually took effect, which only happens on the rebuild path -
/// the verbatim path has no override plumbing at all). Without this test the
/// rebuild branch in `passthrough_header_bytes` had no direct coverage; every
/// other `--strip-bbox` test above exercises only the verbatim branch.
#[test]
fn degrade_strip_bbox_with_generator_override_rebuilds_header() {
    let dir = tempfile::tempdir().expect("tempdir");
    let input = dir.path().join("in.osm.pbf");
    let output = dir.path().join("out.osm.pbf");

    write_bbox_fixture(&input);
    assert!(
        read_header(&input).bbox().is_some(),
        "fixture must declare a HeaderBlock.bbox for the strip to be meaningful"
    );
    assert_eq!(
        read_header(&input).writing_program(),
        Some(FIXTURE_WRITING_PROGRAM),
        "fixture's original writingprogram must differ from the override below"
    );

    const OVERRIDE_GENERATOR: &str = "degrade-rebuild-override/9.0";

    CliInvoker::new()
        .arg("degrade")
        .arg(&input)
        .arg("-o")
        .arg(&output)
        .arg("--strip-bbox")
        .arg("--generator")
        .arg(OVERRIDE_GENERATOR)
        .assert_success();

    let out_header = read_header(&output);
    assert!(
        out_header.bbox().is_none(),
        "--strip-bbox must still clear HeaderBlock.bbox on the override-rebuild path"
    );
    assert_eq!(
        out_header.writing_program(),
        Some(OVERRIDE_GENERATOR),
        "--generator must win on the rebuild path, proving passthrough_header_bytes \
         took the HeaderBuilder rebuild branch rather than the verbatim-forward branch"
    );

    // Element semantics preserved: only the header changed.
    let original = read_normalized(&input);
    let degraded = read_normalized(&output);
    assert_eq!(original.nodes, degraded.nodes);
    assert_eq!(original.ways, degraded.ways);
    assert_eq!(original.relations, degraded.relations);
}

// ---------------------------------------------------------------------------
// --unsort
// ---------------------------------------------------------------------------

/// `--unsort` clears `Sort.Type_then_ID` and produces genuine cross-blob
/// overlap: at least one adjacent same-kind blob pair whose indexdata ID
/// ranges overlap, per kind that has more than `block_cap + 1` elements.
///
/// The fixture packs input at 4 elements/blob and the run uses
/// `--block-cap 10`, so input blobs are smaller than the cap. This is the
/// regime that regressed before the fix (the per-input-blob boundary
/// flush confined the swap to one output blob and `detect_overlaps`
/// returned zero). The central builder must now pack continuously across
/// input blobs so the swap straddles a real output-blob boundary. The two
/// straddling blobs stay internally ID-monotone - the disorder lives at
/// the inter-blob seam, not inside a blob.
#[test]
fn degrade_unsort_creates_adjacent_overlap_per_kind() {
    let dir = tempfile::tempdir().expect("tempdir");
    let input = dir.path().join("in.osm.pbf");
    let output = dir.path().join("out.osm.pbf");

    write_unsort_fixture(&input);

    CliInvoker::new()
        .arg("degrade")
        .arg(&input)
        .arg("-o")
        .arg(&output)
        .arg("--unsort")
        .arg("--block-cap")
        .arg(UNSORT_CAP)
        .assert_success();

    assert_unsort_cross_blob_shape(&output, &input);
}

/// Shared assertions for the `--unsort` cross-blob shape: header sortedness
/// cleared, exactly one adjacent cross-blob overlap per kind, zero
/// intra-blob inversions (each blob internally ID-monotone), element
/// multiset preserved.
fn assert_unsort_cross_blob_shape(output: &Path, input: &Path) {
    assert!(
        !read_header(output).is_sorted(),
        "--unsort output must not declare Sort.Type_then_ID"
    );

    let summary = blob_index_summary(output);
    let elements = blob_elements(output);
    for kind in [
        BlobKindLabel::Node,
        BlobKindLabel::Way,
        BlobKindLabel::Relation,
    ] {
        let same_count = summary.iter().filter(|(k, ..)| *k == kind).count();
        assert!(
            same_count >= 2,
            "kind {kind:?}: need at least 2 blobs to verify overlap, got {same_count}"
        );
        // The CLI promises exactly one adjacent cross-blob overlap per
        // eligible kind - the minimum perturbation that fires sort's
        // detect_overlaps. Count it, don't just check presence.
        assert_eq!(
            count_adjacent_overlaps(&summary, kind),
            1,
            "kind {kind:?}: expected exactly one adjacent cross-blob overlap, \
             blobs were {:?}",
            summary
                .iter()
                .filter(|(k, ..)| *k == kind)
                .collect::<Vec<_>>()
        );
        // The overlap is expressed cross-blob; each blob stays internally
        // ID-monotone (this is what separates --unsort from --unsort-intra).
        assert_eq!(
            count_intra_blob_inversions(&elements, kind),
            0,
            "kind {kind:?}: --unsort blobs must be internally ID-monotone, \
             blobs were {:?}",
            elements
                .iter()
                .filter(|(k, _)| *k == kind)
                .collect::<Vec<_>>()
        );
    }

    // Element multiset preserved (just reordered).
    let original = read_normalized(input);
    let degraded = read_normalized(output);
    assert_eq!(original.nodes, degraded.nodes);
    assert_eq!(original.ways, degraded.ways);
    assert_eq!(original.relations, degraded.relations);
}

/// `--unsort-intra` clears `Sort.Type_then_ID` and produces the intra-blob
/// adversarial shape: exactly one same-kind blob per kind has an internal
/// ID-order inversion, but no adjacent same-kind blob pair overlaps.
///
/// This is the shape that slips past a blob-range overlap check: `sort`
/// decides whether to rewrite by comparing adjacent same-kind blobs'
/// `(min_id, max_id)` ranges, and here every blob's range stays disjoint
/// from its neighbours even though one blob is internally unsorted. So the
/// stream is genuinely out of order while a range-only check sees nothing
/// to fix and the header no longer claims sortedness - a monotonicity
/// blind spot for any consumer that trusts declared sortedness plus
/// non-overlapping ranges.
#[test]
fn degrade_unsort_intra_creates_intra_blob_inversion() {
    let dir = tempfile::tempdir().expect("tempdir");
    let input = dir.path().join("in.osm.pbf");
    let output = dir.path().join("out.osm.pbf");

    write_unsort_fixture(&input);

    CliInvoker::new()
        .arg("degrade")
        .arg(&input)
        .arg("-o")
        .arg(&output)
        .arg("--unsort-intra")
        .arg("--block-cap")
        .arg(UNSORT_CAP)
        .assert_success();

    assert_unsort_intra_shape(&output, &input);
}

/// `--unsort-intra` stays intra-blob even when a single input blob carries
/// more than `--block-cap` same-kind elements (finding 1's regime). The
/// fixture packs 20 elements/blob and the run caps output blocks at 5, so
/// each input blob spans four output blocks. The old shared swap keyed to
/// the cap boundary would have filled and flushed a block here, producing
/// the cross-blob overlap shape; the fix keys the swap to the first two
/// elements so it lands at the start of the first output block.
#[test]
fn degrade_unsort_intra_large_input_blobs_stay_intra_blob() {
    let dir = tempfile::tempdir().expect("tempdir");
    let input = dir.path().join("in.osm.pbf");
    let output = dir.path().join("out.osm.pbf");

    write_large_blob_unsort_fixture(&input);

    CliInvoker::new()
        .arg("degrade")
        .arg(&input)
        .arg("-o")
        .arg(&output)
        .arg("--unsort-intra")
        .arg("--block-cap")
        .arg(LARGE_BLOB_CAP)
        .assert_success();

    assert_unsort_intra_shape(&output, &input);
}

/// Shared assertions for the `--unsort-intra` shape: header sortedness
/// cleared, exactly one intra-blob inversion per kind, zero cross-blob
/// overlaps, element multiset preserved.
fn assert_unsort_intra_shape(output: &Path, input: &Path) {
    assert!(
        !read_header(output).is_sorted(),
        "--unsort-intra output must not declare Sort.Type_then_ID"
    );

    let summary = blob_index_summary(output);
    let elements = blob_elements(output);
    for kind in [
        BlobKindLabel::Node,
        BlobKindLabel::Way,
        BlobKindLabel::Relation,
    ] {
        assert_eq!(
            count_intra_blob_inversions(&elements, kind),
            1,
            "kind {kind:?}: expected exactly one intra-blob inversion, \
             blobs were {:?}",
            elements
                .iter()
                .filter(|(k, _)| *k == kind)
                .collect::<Vec<_>>()
        );
        // No cross-blob overlap: this is exactly the shape a blob-range
        // overlap check cannot see.
        assert_eq!(
            count_adjacent_overlaps(&summary, kind),
            0,
            "kind {kind:?}: --unsort-intra must not produce cross-blob overlap, \
             blobs were {:?}",
            summary
                .iter()
                .filter(|(k, ..)| *k == kind)
                .collect::<Vec<_>>()
        );
    }

    // Element multiset preserved (just reordered).
    let original = read_normalized(input);
    let degraded = read_normalized(output);
    assert_eq!(original.nodes, degraded.nodes);
    assert_eq!(original.ways, degraded.ways);
    assert_eq!(original.relations, degraded.relations);
}

/// `--unsort` and `--unsort-intra` are mutually exclusive.
#[test]
fn degrade_unsort_and_unsort_intra_are_mutually_exclusive() {
    let dir = tempfile::tempdir().expect("tempdir");
    let input = dir.path().join("in.osm.pbf");
    let output = dir.path().join("out.osm.pbf");

    write_unsort_fixture(&input);

    CliInvoker::new()
        .arg("degrade")
        .arg(&input)
        .arg("-o")
        .arg(&output)
        .arg("--unsort")
        .arg("--unsort-intra")
        .assert_failure()
        .assert_stderr_contains("unsort-intra");
}

/// `--unsort` output piped through `pbfhogg sort` recovers the original
/// element set with `Sort.Type_then_ID` re-declared. Closes the loop on
/// the design's primary use case: the cross-blob overlap must actually
/// reach `sort`'s overlap-rewrite path.
#[test]
fn degrade_unsort_then_sort_round_trips() {
    let dir = tempfile::tempdir().expect("tempdir");
    let input = dir.path().join("in.osm.pbf");
    let unsorted = dir.path().join("unsorted.osm.pbf");
    let resorted = dir.path().join("resorted.osm.pbf");

    write_unsort_fixture(&input);

    CliInvoker::new()
        .arg("degrade")
        .arg(&input)
        .arg("-o")
        .arg(&unsorted)
        .arg("--unsort")
        .arg("--block-cap")
        .arg(UNSORT_CAP)
        .assert_success();

    let sort_out = CliInvoker::new()
        .arg("sort")
        .arg(&unsorted)
        .arg("-o")
        .arg(&resorted)
        .arg("--force")
        .assert_success();

    // Prove sort actually hit the overlap-rewrite path rather than passing
    // the file through untouched. Sort prints this line only when
    // detect_overlaps flags at least one blob run for decode + re-encode;
    // the cross-blob overlap --unsort injects is what makes it fire. (A
    // full passthrough would print nothing here.)
    sort_out.assert_stderr_contains("blobs in overlap runs");

    // Prove the output is genuinely sorted in file order, not merely
    // element-equivalent. read_normalized re-sorts every section before
    // comparison, so it would accept a stream that sort left disordered;
    // assert_sorted_file walks the file in blob order and checks the
    // header flag plus per-type monotonicity, catching a passthrough that
    // never repaired the overlap.
    assert_sorted_file(&resorted);

    let original = read_normalized(&input);
    let recovered = read_normalized(&resorted);
    assert_eq!(original.nodes, recovered.nodes);
    assert_eq!(original.ways, recovered.ways);
    assert_eq!(original.relations, recovered.relations);
}

// ---------------------------------------------------------------------------
// Composition
// ---------------------------------------------------------------------------

/// `--unsort --strip-indexdata` composes: output is unsorted *and*
/// unindexed.
#[test]
fn degrade_unsort_and_strip_indexdata_compose() {
    let dir = tempfile::tempdir().expect("tempdir");
    let input = dir.path().join("in.osm.pbf");
    let output = dir.path().join("out.osm.pbf");

    write_degrade_fixture(&input);

    CliInvoker::new()
        .arg("degrade")
        .arg(&input)
        .arg("-o")
        .arg(&output)
        .arg("--unsort")
        .arg("--strip-indexdata")
        .arg("--block-cap")
        .arg("5")
        .assert_success();

    assert_non_indexed(&output);
    assert!(!read_header(&output).is_sorted());

    let original = read_normalized(&input);
    let degraded = read_normalized(&output);
    assert_eq!(original.nodes, degraded.nodes);
    assert_eq!(original.ways, degraded.ways);
    assert_eq!(original.relations, degraded.relations);
}

/// `--unsort --strip-locations` composes: the cross-blob overlap shape is
/// preserved *and* `LocationsOnWays` is cleared. Confirms the swap logic
/// still fires when the ways go through the coordinate-dropping re-encode.
#[test]
fn degrade_unsort_and_strip_locations_compose() {
    let dir = tempfile::tempdir().expect("tempdir");
    let input = dir.path().join("in.osm.pbf");
    let output = dir.path().join("out.osm.pbf");

    write_unsort_fixture(&input);

    CliInvoker::new()
        .arg("degrade")
        .arg(&input)
        .arg("-o")
        .arg(&output)
        .arg("--unsort")
        .arg("--strip-locations")
        .arg("--block-cap")
        .arg(UNSORT_CAP)
        .assert_success();

    assert!(
        !read_header(&output).has_locations_on_ways(),
        "--strip-locations output must not declare LocationsOnWays"
    );
    assert_unsort_cross_blob_shape(&output, &input);
}

/// `--unsort-intra --strip-locations` composes: the intra-blob inversion
/// shape is preserved *and* `LocationsOnWays` is cleared.
#[test]
fn degrade_unsort_intra_and_strip_locations_compose() {
    let dir = tempfile::tempdir().expect("tempdir");
    let input = dir.path().join("in.osm.pbf");
    let output = dir.path().join("out.osm.pbf");

    write_unsort_fixture(&input);

    CliInvoker::new()
        .arg("degrade")
        .arg(&input)
        .arg("-o")
        .arg(&output)
        .arg("--unsort-intra")
        .arg("--strip-locations")
        .arg("--block-cap")
        .arg(UNSORT_CAP)
        .assert_success();

    assert!(
        !read_header(&output).has_locations_on_ways(),
        "--strip-locations output must not declare LocationsOnWays"
    );
    assert_unsort_intra_shape(&output, &input);
}

/// `--unsort-intra --strip-indexdata` composes: output is intra-blob
/// unsorted *and* unindexed.
#[test]
fn degrade_unsort_intra_and_strip_indexdata_compose() {
    let dir = tempfile::tempdir().expect("tempdir");
    let input = dir.path().join("in.osm.pbf");
    let output = dir.path().join("out.osm.pbf");

    write_unsort_fixture(&input);

    CliInvoker::new()
        .arg("degrade")
        .arg(&input)
        .arg("-o")
        .arg(&output)
        .arg("--unsort-intra")
        .arg("--strip-indexdata")
        .arg("--block-cap")
        .arg(UNSORT_CAP)
        .assert_success();

    assert_non_indexed(&output);
    assert_unsort_intra_shape(&output, &input);
}

/// `--strip-tagdata --strip-indexdata` composes on the passthrough path:
/// both header fields are cleared while the blob payload (and sortedness)
/// pass through untouched.
#[test]
fn degrade_strip_tagdata_and_strip_indexdata_compose() {
    let dir = tempfile::tempdir().expect("tempdir");
    let input = dir.path().join("in.osm.pbf");
    let output = dir.path().join("out.osm.pbf");

    write_degrade_fixture(&input);
    assert_has_tagdata(&input);
    assert_indexed(&input);

    CliInvoker::new()
        .arg("degrade")
        .arg(&input)
        .arg("-o")
        .arg(&output)
        .arg("--strip-tagdata")
        .arg("--strip-indexdata")
        .assert_success();

    assert_no_tagdata_all_blobs(&output);
    assert_non_indexed(&output);
    // Passthrough leaves the payload alone, so sortedness survives.
    assert!(read_header(&output).is_sorted());

    let original = read_normalized(&input);
    let degraded = read_normalized(&output);
    assert_eq!(original.nodes, degraded.nodes);
    assert_eq!(original.ways, degraded.ways);
    assert_eq!(original.relations, degraded.relations);
}

/// `--unsort --strip-tagdata` composes on the decode path: the elements are
/// re-encoded (unsorted) *and* every output blob is emitted without tagdata.
/// This exercises the `frame_and_write_batch` tagdata=None path that the
/// merge thread uses under either unsort mode.
#[test]
fn degrade_unsort_and_strip_tagdata_compose() {
    let dir = tempfile::tempdir().expect("tempdir");
    let input = dir.path().join("in.osm.pbf");
    let output = dir.path().join("out.osm.pbf");

    write_degrade_fixture(&input);
    assert_has_tagdata(&input);

    CliInvoker::new()
        .arg("degrade")
        .arg(&input)
        .arg("-o")
        .arg(&output)
        .arg("--unsort")
        .arg("--strip-tagdata")
        .arg("--block-cap")
        .arg("5")
        .assert_success();

    assert_no_tagdata(&output);
    assert!(!read_header(&output).is_sorted());

    let original = read_normalized(&input);
    let degraded = read_normalized(&output);
    assert_eq!(original.nodes, degraded.nodes);
    assert_eq!(original.ways, degraded.ways);
    assert_eq!(original.relations, degraded.relations);
}

/// `--strip-locations --strip-tagdata` composes on the decode path's
/// non-unsort shape: with `--block-cap 10` against the fixture's 20-element
/// input blobs, workers pre-frame full cap-blocks via `frame_owned`, so this
/// exercises the `frame_owned` tagdata=None path (distinct from the merge
/// thread's batch path above). `LocationsOnWays` is cleared and no output
/// blob carries tagdata.
#[test]
fn degrade_strip_locations_and_strip_tagdata_compose() {
    let dir = tempfile::tempdir().expect("tempdir");
    let input = dir.path().join("in.osm.pbf");
    let output = dir.path().join("out.osm.pbf");

    write_degrade_fixture(&input);
    assert_has_tagdata(&input);

    CliInvoker::new()
        .arg("degrade")
        .arg(&input)
        .arg("-o")
        .arg(&output)
        .arg("--strip-locations")
        .arg("--strip-tagdata")
        .arg("--block-cap")
        .arg("10")
        .assert_success();

    assert_no_tagdata(&output);
    assert!(
        !read_header(&output).has_locations_on_ways(),
        "--strip-locations output must not declare LocationsOnWays"
    );

    let original = read_normalized(&input);
    let degraded = read_normalized(&output);
    assert_eq!(original.nodes, degraded.nodes);
    assert_eq!(original.ways, degraded.ways);
    assert_eq!(original.relations, degraded.relations);
}

// ---------------------------------------------------------------------------
// Validation
// ---------------------------------------------------------------------------

/// Running `degrade` with no transformation flags is rejected.
#[test]
fn degrade_requires_at_least_one_flag() {
    let dir = tempfile::tempdir().expect("tempdir");
    let input = dir.path().join("in.osm.pbf");
    let output = dir.path().join("out.osm.pbf");

    write_degrade_fixture(&input);

    CliInvoker::new()
        .arg("degrade")
        .arg(&input)
        .arg("-o")
        .arg(&output)
        .assert_failure()
        .assert_stderr_contains("at least one transformation flag");
}

// ---------------------------------------------------------------------------
// --drop-ids
// ---------------------------------------------------------------------------

fn normalized_count(path: &Path) -> usize {
    let pbf = read_normalized(path);
    pbf.nodes.len() + pbf.ways.len() + pbf.relations.len()
}

/// Extract an unsigned-integer field from the `refs` object of a
/// `check --refs --json` document. The output is pretty-printed one field
/// per line as `"name": value,`; the closing quote in the needle makes the
/// match exact (so `missing_relation_members` never matches
/// `missing_relation_member_occurrences`). Kept dependency-free because the
/// integration-test crate has no `serde_json` dev-dependency.
fn refs_field(stdout: &str, field: &str) -> u64 {
    let needle = format!("\"{field}\"");
    let line = stdout
        .lines()
        .find(|l| l.trim_start().starts_with(&needle))
        .unwrap_or_else(|| panic!("field {field:?} not found in check --refs json:\n{stdout}"));
    let value = line
        .split_once(':')
        .expect("json field line has a colon")
        .1
        .trim()
        .trim_end_matches(',');
    value
        .parse()
        .unwrap_or_else(|_| panic!("field {field:?} value {value:?} is not an integer"))
}

/// Compute the four unique dangling-reference counts hash-independently from
/// the degrade *output* alone, per the spec's Section 7.3 definition: a
/// reference dangles iff its target `(kind, id)` is absent from the output.
/// Returns `(missing_node_refs, missing_way_refs, missing_node_members,
/// missing_relation_members)`.
fn expected_dangles(out: &common::NormalizedPbf) -> (u64, u64, u64, u64) {
    use std::collections::BTreeSet;
    let node_ids: BTreeSet<i64> = out.nodes.iter().map(|n| n.id).collect();
    let way_ids: BTreeSet<i64> = out.ways.iter().map(|w| w.id).collect();
    let rel_ids: BTreeSet<i64> = out.relations.iter().map(|r| r.id).collect();

    let mut missing_node_refs = BTreeSet::new();
    for w in &out.ways {
        for r in &w.refs {
            if !node_ids.contains(r) {
                missing_node_refs.insert(*r);
            }
        }
    }

    let mut missing_node_members = BTreeSet::new();
    let mut missing_way_refs = BTreeSet::new();
    let mut missing_relation_members = BTreeSet::new();
    for rel in &out.relations {
        for m in &rel.members {
            match m.member_type.as_str() {
                "node" if !node_ids.contains(&m.ref_id) => {
                    missing_node_members.insert(m.ref_id);
                }
                "way" if !way_ids.contains(&m.ref_id) => {
                    missing_way_refs.insert(m.ref_id);
                }
                "relation" if !rel_ids.contains(&m.ref_id) => {
                    missing_relation_members.insert(m.ref_id);
                }
                _ => {}
            }
        }
    }
    (
        missing_node_refs.len() as u64,
        missing_way_refs.len() as u64,
        missing_node_members.len() as u64,
        missing_relation_members.len() as u64,
    )
}

/// Run `check --refs --check-relations --json` on `path` and assert its four
/// `missing_*` fields equal the hash-independent expectation derived from the
/// output. `check` exits 1 when integrity fails while still printing the JSON,
/// so this uses `run()` (not `assert_success`) and reads stdout. Returns the
/// four-field sum so callers can guard against a vacuous (zero-dangle) run.
fn assert_check_refs_matches_output(path: &Path) -> u64 {
    let out = read_normalized(path);
    let (mnr, mwr, mnm, mrm) = expected_dangles(&out);
    let check = CliInvoker::new()
        .arg("check")
        .arg(path)
        .arg("--refs")
        .arg("--check-relations")
        .arg("--json")
        .run();
    let stdout = check.stdout_str();
    assert_eq!(
        refs_field(&stdout, "missing_node_refs"),
        mnr,
        "missing_node_refs"
    );
    assert_eq!(
        refs_field(&stdout, "missing_way_refs"),
        mwr,
        "missing_way_refs"
    );
    assert_eq!(
        refs_field(&stdout, "missing_node_members"),
        mnm,
        "missing_node_members"
    );
    assert_eq!(
        refs_field(&stdout, "missing_relation_members"),
        mrm,
        "missing_relation_members"
    );
    mnr + mwr + mnm + mrm
}

/// The consumer contract: dropping referenced elements makes surviving
/// ways/relations dangle, and `check --refs` reports exactly the dangles the
/// output structure implies. Expectations are computed hash-independently from
/// the output (spec Section 7.3), so the test survives a fixture tweak; the
/// pinned `10:16` is verified to drop nodes 2 and 3 (referenced by every way)
/// and way 2 (a member of every relation), so the four-field sum is > 0.
#[test]
fn degrade_drop_ids_dangling_refs_match_check_refs() {
    let dir = tempfile::tempdir().expect("tempdir");
    let input = dir.path().join("in.osm.pbf");
    let output = dir.path().join("out.osm.pbf");
    write_degrade_fixture(&input);

    CliInvoker::new()
        .arg("degrade")
        .arg(&input)
        .arg("-o")
        .arg(&output)
        .arg("--drop-ids")
        .arg("10:16")
        .assert_success();

    assert_eq!(normalized_count(&output), normalized_count(&input) - 10);
    let sum = assert_check_refs_matches_output(&output);
    assert!(
        sum > 0,
        "10:16 must drop referenced elements so dangles are produced, got sum 0"
    );
}

#[test]
fn degrade_drop_ids_removes_exactly_n() {
    let dir = tempfile::tempdir().expect("tempdir");
    let input = dir.path().join("in.osm.pbf");
    let output = dir.path().join("out.osm.pbf");
    write_degrade_fixture(&input);

    CliInvoker::new()
        .arg("degrade")
        .arg(&input)
        .arg("-o")
        .arg(&output)
        .arg("--drop-ids")
        .arg("10:1")
        .assert_success();

    assert_eq!(normalized_count(&output), normalized_count(&input) - 10);
    assert!(read_header(&output).is_sorted());
    assert_sorted_file(&output);
}

#[test]
fn degrade_drop_ids_is_reproducible_and_seed_changes_selection() {
    let dir = tempfile::tempdir().expect("tempdir");
    let input = dir.path().join("in.osm.pbf");
    let first = dir.path().join("first.osm.pbf");
    let second = dir.path().join("second.osm.pbf");
    let third = dir.path().join("third.osm.pbf");
    write_degrade_fixture(&input);
    for (output, spec) in [(&first, "10:7"), (&second, "10:7"), (&third, "10:8")] {
        CliInvoker::new()
            .arg("degrade")
            .arg(&input)
            .arg("-o")
            .arg(output)
            .arg("--drop-ids")
            .arg(spec)
            .assert_success();
    }
    assert_eq!(
        std::fs::read(&first).expect("first"),
        std::fs::read(&second).expect("second")
    );
    assert_ne!(
        std::fs::read(&first).expect("first"),
        std::fs::read(&third).expect("third")
    );
}

#[test]
fn degrade_drop_ids_validates_arguments_and_total() {
    let dir = tempfile::tempdir().expect("tempdir");
    let input = dir.path().join("in.osm.pbf");
    let output = dir.path().join("out.osm.pbf");
    write_degrade_fixture(&input);
    for (spec, message) in [
        ("0:1", "N must be >= 1"),
        ("10", "N:SEED"),
        ("1000000:1", "input has only"),
    ] {
        CliInvoker::new()
            .arg("degrade")
            .arg(&input)
            .arg("-o")
            .arg(&output)
            .arg("--drop-ids")
            .arg(spec)
            .assert_failure()
            .assert_stderr_contains(message);
    }
}

/// `--block-cap 0` is rejected up front.
#[test]
fn degrade_rejects_zero_block_cap() {
    let dir = tempfile::tempdir().expect("tempdir");
    let input = dir.path().join("in.osm.pbf");
    let output = dir.path().join("out.osm.pbf");

    write_degrade_fixture(&input);

    CliInvoker::new()
        .arg("degrade")
        .arg(&input)
        .arg("-o")
        .arg(&output)
        .arg("--unsort")
        .arg("--block-cap")
        .arg("0")
        .assert_failure()
        .assert_stderr_contains("must be > 0");
}

/// `--unsort-intra --block-cap 1` is rejected: an intra-blob inversion
/// needs two same-kind elements in one output block, which a cap of 1
/// cannot hold. Rejecting up front avoids a silent no-op that would still
/// clear Sort.Type_then_ID.
#[test]
fn degrade_unsort_intra_rejects_block_cap_one() {
    let dir = tempfile::tempdir().expect("tempdir");
    let input = dir.path().join("in.osm.pbf");
    let output = dir.path().join("out.osm.pbf");

    write_unsort_fixture(&input);

    CliInvoker::new()
        .arg("degrade")
        .arg(&input)
        .arg("-o")
        .arg(&output)
        .arg("--unsort-intra")
        .arg("--block-cap")
        .arg("1")
        .assert_failure()
        .assert_stderr_contains("block-cap >= 2");
}

/// `--unsort --block-cap 1` is supported (not a silent no-op): each output
/// blob holds one element, and swapping the first two adjacent
/// single-element blobs produces exactly one descending cross-blob step -
/// the same overlap shape sort's detect_overlaps fires on.
#[test]
fn degrade_unsort_accepts_block_cap_one() {
    let dir = tempfile::tempdir().expect("tempdir");
    let input = dir.path().join("in.osm.pbf");
    let output = dir.path().join("out.osm.pbf");

    write_unsort_fixture(&input);

    CliInvoker::new()
        .arg("degrade")
        .arg(&input)
        .arg("-o")
        .arg(&output)
        .arg("--unsort")
        .arg("--block-cap")
        .arg("1")
        .assert_success();

    assert_unsort_cross_blob_shape(&output, &input);
}

mod tier2 {
    use super::*;

    #[test]
    fn degrade_drop_ids_and_strip_locations_compose() {
        let dir = tempfile::tempdir().expect("tempdir");
        let input = dir.path().join("in.osm.pbf");
        let output = dir.path().join("out.osm.pbf");
        write_degrade_fixture(&input);
        let input_count = normalized_count(&input);
        CliInvoker::new()
            .arg("degrade")
            .arg(&input)
            .arg("-o")
            .arg(&output)
            .args(["--drop-ids", "10:16", "--strip-locations"])
            .assert_success();
        assert_eq!(normalized_count(&output), input_count - 10);
        assert!(!read_header(&output).has_locations_on_ways());
        assert!(read_header(&output).is_sorted());
    }

    #[test]
    fn degrade_drop_ids_and_strip_indexdata_compose() {
        let dir = tempfile::tempdir().expect("tempdir");
        let input = dir.path().join("in.osm.pbf");
        let output = dir.path().join("out.osm.pbf");
        write_degrade_fixture(&input);
        let input_count = normalized_count(&input);
        CliInvoker::new()
            .arg("degrade")
            .arg(&input)
            .arg("-o")
            .arg(&output)
            .args(["--drop-ids", "10:16", "--strip-indexdata"])
            .assert_success();
        assert_eq!(normalized_count(&output), input_count - 10);
        assert_non_indexed(&output);
    }

    #[test]
    fn degrade_drop_ids_and_unsort_compose() {
        let dir = tempfile::tempdir().expect("tempdir");
        let input = dir.path().join("in.osm.pbf");
        let output = dir.path().join("out.osm.pbf");
        write_unsort_fixture(&input);
        let input_count = normalized_count(&input);
        CliInvoker::new()
            .arg("degrade")
            .arg(&input)
            .arg("-o")
            .arg(&output)
            .args(["--drop-ids", "10:16", "--unsort", "--block-cap", UNSORT_CAP])
            .assert_success();
        assert_eq!(normalized_count(&output), input_count - 10);
        assert!(!read_header(&output).is_sorted());
        for kind in [
            BlobKindLabel::Node,
            BlobKindLabel::Way,
            BlobKindLabel::Relation,
        ] {
            assert_eq!(
                count_adjacent_overlaps(&blob_index_summary(&output), kind),
                1
            );
            assert_eq!(
                count_intra_blob_inversions(&blob_elements(&output), kind),
                0
            );
        }
        // The consumer contract must still hold on an unsorted-but-kind-
        // separated file: check --refs reports exactly the dangles the output
        // structure implies (spec Section 8.2 #9). Not merely a cleared flag.
        let sum = assert_check_refs_matches_output(&output);
        assert!(sum > 0, "10:16 on the unsort fixture must produce dangles");
    }
}