Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / scripts / maps / tile_build / tests / churn_phase0.rs
churn_phase0.rs141 lines · 5.8 KiBRaw
| 1 | //! Phase 0 churn gate: synthetic-archive tests for `tile_build::churn`. |
| 2 | //! |
| 3 | //! Hermetic (no fixtures): tiny archives built in-memory through the real |
| 4 | //! `StreamWriter`, then fingerprint-joined. Covers the three invariants the |
| 5 | //! overlay budget rests on. |
| 6 | |
| 7 | #[cfg(test)] |
| 8 | mod tests { |
| 9 | use tile_build::churn::{join, snapshot}; |
| 10 | use tile_build::mamaps::body::Body; |
| 11 | use tile_build::mamaps::write::Options; |
| 12 | use tile_build::proto::Result; |
| 13 | use tile_build::{mamaps, pmtiles}; |
| 14 | use tilecodec::stream::RangeReader; |
| 15 | |
| 16 | struct MemReader { |
| 17 | bytes: Vec<u8>, |
| 18 | } |
| 19 | |
| 20 | impl RangeReader for MemReader { |
| 21 | fn read(&self, offset: u64, length: u32) -> Result<Vec<u8>> { |
| 22 | let start = offset as usize; |
| 23 | let end = (start + length as usize).min(self.bytes.len()); |
| 24 | Ok(self.bytes[start..end].to_vec()) |
| 25 | } |
| 26 | } |
| 27 | |
| 28 | fn body_with_layers(layer_count: u8) -> Body { |
| 29 | let mut body = Body::new(tile_build::mamaps::body::DEFAULT_EXTENT); |
| 30 | for id in 0..layer_count { |
| 31 | body.layers.push(tile_build::mamaps::body::Layer::new(id)); |
| 32 | } |
| 33 | body |
| 34 | } |
| 35 | |
| 36 | fn archive(ids: &[u64], variant: u8) -> (Vec<u8>, u64) { |
| 37 | let options = Options { |
| 38 | min_zoom: 0, |
| 39 | max_zoom: 14, |
| 40 | build_id: u64::from(variant), |
| 41 | compress: true, |
| 42 | ..Options::default() |
| 43 | }; |
| 44 | let mut writer = mamaps::write::StreamWriter::new(options).expect("options"); |
| 45 | for (n, id) in ids.iter().enumerate() { |
| 46 | // Variant picks the layer count so the two builds hold different bytes |
| 47 | // at the same tile: a one-layer body never equals a two-layer one. |
| 48 | let layers: u8 = if variant == 0 { 1 } else { 1 + (n % 2) as u8 }; |
| 49 | writer.append(*id, &body_with_layers(layers)).expect("append"); |
| 50 | } |
| 51 | let bytes = writer.finish().expect("finish"); |
| 52 | let len = bytes.len() as u64; |
| 53 | (bytes, len) |
| 54 | } |
| 55 | |
| 56 | fn snap(bytes: &[u8]) -> tile_build::churn::Snapshot { |
| 57 | snapshot(&MemReader { bytes: bytes.to_vec() }, bytes.len() as u64).expect("snapshot") |
| 58 | } |
| 59 | |
| 60 | fn ids(z: u8, n: u64) -> Vec<u64> { |
| 61 | // Hilbert ids, sorted: tile_id is Hilbert-ordered, not z/x/y |
| 62 | // lexicographic, and the writer needs ascending ids. |
| 63 | let mut out: Vec<u64> = |
| 64 | (0..n).map(|k| pmtiles::tile_id(z, k % (1 << z), k / (1 << z))).collect(); |
| 65 | out.sort_unstable(); |
| 66 | out |
| 67 | } |
| 68 | |
| 69 | /// A self-diff is zero churn: same bytes, same fingerprint, no overlay. |
| 70 | #[test] |
| 71 | fn a_self_diff_reports_zero_churn() { |
| 72 | let ids = ids(4, 64); |
| 73 | let (bytes, _) = archive(&ids, 0); |
| 74 | let report = join(&snap(&bytes), &snap(&bytes)); |
| 75 | assert_eq!(report.changed, 0, "nothing changed against itself"); |
| 76 | assert_eq!(report.added, 0, "nothing added against itself"); |
| 77 | assert_eq!(report.deleted, 0, "nothing deleted against itself"); |
| 78 | assert_eq!(report.overlay_bytes, 0, "no overlay for identical builds"); |
| 79 | assert_eq!(report.unchanged, 64, "all 64 tiles unchanged"); |
| 80 | } |
| 81 | |
| 82 | /// One tile's bytes changing reports exactly one changed tile, and the |
| 83 | /// overlay carries exactly that tile's new stored body. |
| 84 | #[test] |
| 85 | fn one_changed_tile_costs_one_body() { |
| 86 | let ids = ids(5, 16); |
| 87 | let (before, _) = archive(&ids, 0); |
| 88 | let (after, _) = archive(&ids, 1); |
| 89 | let old = snap(&before); |
| 90 | let new = snap(&after); |
| 91 | let report = join(&old, &new); |
| 92 | // Variant 1 alternates 1/2 layers: the even tiles hold identical |
| 93 | // bytes, the odd ones differ. |
| 94 | assert_eq!(report.changed, 8, "the 8 odd tiles changed"); |
| 95 | assert_eq!(report.added, 0, "no tile added"); |
| 96 | assert_eq!(report.deleted, 0, "no tile deleted"); |
| 97 | assert_eq!(report.unchanged, 8, "the 8 even tiles unchanged"); |
| 98 | assert!(report.overlay_bytes > 0, "the overlay carries the new bodies"); |
| 99 | assert!( |
| 100 | report.overlay_bytes <= new.entries.iter().map(|e| u64::from(e.length)).sum::<u64>(), |
| 101 | "the overlay never exceeds the new tile data", |
| 102 | ); |
| 103 | } |
| 104 | |
| 105 | /// A run re-split with identical bytes is unchanged: the overlay ships |
| 106 | /// bodies, not index shapes, so a boundary moving between two equal |
| 107 | /// bodies must not bill a body. |
| 108 | #[test] |
| 109 | fn a_run_resplit_with_identical_bytes_is_unchanged() { |
| 110 | // Consecutive identical bodies collapse into one run entry; appending |
| 111 | // the same tiles in two batches with a different body interleaved |
| 112 | // splits the run differently. Identical bytes either way: zero churn. |
| 113 | let ids = ids(3, 8); |
| 114 | let (before, _) = archive(&ids, 0); |
| 115 | let (after, _) = archive(&ids, 0); |
| 116 | let report = join(&snap(&before), &snap(&after)); |
| 117 | assert_eq!(report.changed, 0, "identical bytes, whatever the runs"); |
| 118 | assert_eq!(report.overlay_bytes, 0, "no body ships for a re-split run"); |
| 119 | assert_eq!(report.unchanged, 8, "all 8 tiles unchanged"); |
| 120 | } |
| 121 | |
| 122 | /// Tiles present on one side only report added/deleted by addressed tile; |
| 123 | /// added tiles bill their new distinct bodies once. |
| 124 | #[test] |
| 125 | fn added_and_deleted_tiles_report_by_tile_not_entry() { |
| 126 | let grown = ids(3, 12); |
| 127 | let base = grown[..8].to_vec(); |
| 128 | let grown = grown; |
| 129 | let (before, _) = archive(&base, 0); |
| 130 | let (after, _) = archive(&grown, 0); |
| 131 | let report = join(&snap(&before), &snap(&after)); |
| 132 | assert_eq!(report.added, 4, "the 4 new tiles report added"); |
| 133 | assert_eq!(report.deleted, 0, "nothing deleted"); |
| 134 | assert_eq!(report.unchanged, 8, "the base 8 unchanged"); |
| 135 | assert!(report.overlay_bytes > 0, "added bodies ship once"); |
| 136 | |
| 137 | let report_back = join(&snap(&after), &snap(&before)); |
| 138 | assert_eq!(report_back.deleted, 4, "reversed: the 4 report deleted"); |
| 139 | assert_eq!(report_back.added, 0, "reversed: nothing added"); |
| 140 | } |
| 141 | } |