From 4b30496a4cc78aae258721856563b07fc3e76cc7 Mon Sep 17 00:00:00 2001 From: mavrick Date: Thu, 12 Nov 2026 18:50:48 -0600 Subject: [PATCH 1/4] test(analysis): several threads give what one thread gives, as a property Each case makes a scene of polygons, points and a raster whose sizes straddle the tools' thresholds (256 features, 200,000 units of raster work) and runs 34 tool outputs with the thread setting at one and at 2, 3, 7 or 8: overlays, buffer and dissolve, near, spatial join, summarize within, zonal statistics, slope, aspect, hillshade, band math, k-means, viewshed, IDW, kriging and kernel density. Layers, messages and raster cells must match bit for bit. The nightly runs it on a new seed with 200 cases. Co-Authored-By: Claude Opus 5.5 --- .github/workflows/nightly.yml | 1 + Cargo.lock | 1 + crates/analysis/Cargo.toml | 3 + crates/analysis/tests/parallel_serial.rs | 289 +++++++++++++++++++++++ 4 files changed, 294 insertions(+) create mode 100644 crates/analysis/tests/parallel_serial.rs diff --git a/.github/workflows/nightly.yml b/.github/workflows/nightly.yml index f8e9a91459..569ecabfd5 100644 --- a/.github/workflows/nightly.yml +++ b/.github/workflows/nightly.yml @@ -120,6 +120,7 @@ jobs: PROPERTY_CASES: "2000" run: | cargo test --locked -p rustgis-engine --test never_silent_roundtrip + PROPERTY_CASES=200 cargo test --locked -p rustgis-analysis --test parallel_serial report: name: Report diff --git a/Cargo.lock b/Cargo.lock index 8a61abbe4a..edfb1e25a6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4730,6 +4730,7 @@ dependencies = [ "a5", "geo", "log", + "proptest", "rustgis-data", "rustgis-geom", "serde", diff --git a/crates/analysis/Cargo.toml b/crates/analysis/Cargo.toml index 07338fa303..023adaf767 100644 --- a/crates/analysis/Cargo.toml +++ b/crates/analysis/Cargo.toml @@ -16,3 +16,6 @@ a5 = { workspace = true } serde = { workspace = true } serde_json = { workspace = true } log = { workspace = true } + +[dev-dependencies] +proptest = { workspace = true } diff --git a/crates/analysis/tests/parallel_serial.rs b/crates/analysis/tests/parallel_serial.rs new file mode 100644 index 0000000000..f9d6992132 --- /dev/null +++ b/crates/analysis/tests/parallel_serial.rs @@ -0,0 +1,289 @@ +//! Never hold the window (AGENTS.md), as a property: work split over several threads gives what +//! one thread gives, bit for bit. Each case makes a scene of polygons, points and a raster whose +//! sizes straddle the tools' thresholds (256 features, 200,000 units of raster work), runs every +//! tool on it with the thread setting at one and at several, and compares the outputs exactly: +//! features, their order, values (floats by their bits), messages and raster cells. Nothing may +//! differ: a refusal included, since the work caps that grow with the threads (#1291) are far +//! above these scenes. +//! +//! The thread setting holds for the whole process, so the property is one test, alone in its +//! file. Pull requests run a fixed seed; the nightly a new one with more cases. A failure gives +//! its seed and the smallest scene found. To run a seed again, or deeper: +//! `PROPERTY_SEED=7 PROPERTY_CASES=200 cargo test -p rustgis-analysis --test parallel_serial`. +use proptest::prelude::*; +use proptest::test_runner::{Config, RngAlgorithm, TestCaseError, TestRng, TestRunner}; +use rustgis_analysis::{Inputs, RasterInput, density, kriging, rasterops}; +use rustgis_data::raster::{ColorHint, Crs, GeoTransform, Raster}; +use rustgis_data::{Coord, Dataset, Extent, Field, FieldType, Geometry, GeometryType, Value, threads}; +use serde_json::{Value as Json, json}; + +/// A number from the environment, else `default`. +fn env(name: &str, default: u64) -> u64 { + std::env::var(name).ok().and_then(|v| v.parse().ok()).unwrap_or(default) +} + +/// The layers and the raster a case runs the tools on. +#[derive(Debug, Clone)] +struct Scene { + polygons: Dataset, + points: Dataset, + overlay: Dataset, + /// Width of a 450-row raster over the scene: 380 to 520 columns, 171,000 to 234,000 cells. + width: u32, + /// The raster's cells left without data, by index. + holes: Vec, + /// Cells across the scene for IDW, kriging and kernel density. + across: u32, + threads: usize, +} + +impl Inputs for Scene { + fn dataset(&self, name: &str) -> Option<&Dataset> { + [&self.polygons, &self.points, &self.overlay].into_iter().find(|d| d.name == name) + } + fn selection(&self, _: &str) -> &[usize] { + &[] + } +} + +/// A number on a quarter-degree grid half the time, so shapes share edges and points lie on them. +fn snapped(lo: f64, hi: f64) -> impl Strategy { + prop_oneof![(lo..hi).prop_map(|v| (v * 4.0).round() / 4.0), lo..hi] +} + +/// A square, a sliver or a point-sized square around a centre in the 4° scene. +fn square() -> impl Strategy { + let size = prop_oneof![3 => 0.05..0.6f64, 1 => prop::sample::select(vec![0.25, 0.5, 1e-9])]; + (snapped(0.0, 4.0), snapped(0.0, 4.0), size, prop::bool::weighted(0.1)).prop_map(|(x, y, s, thin)| { + let (w, h) = if thin { (s, 1e-7) } else { (s, s) }; + let c = |x, y| Coord::new(x, y); + Geometry::Polygon(vec![vec![vec![c(x, y), c(x + w, y), c(x + w, y + h), c(x, y + h), c(x, y)]]]) + }) +} + +/// A value for a decimal field, leaning on edges. +fn number() -> BoxedStrategy { + prop_oneof![ + 6 => (-1e3..1e3f64).prop_map(Value::Float), + 1 => prop::sample::select(vec![0.1, -0.0, 1e300, -1e300, 5e-324, f64::NAN, f64::INFINITY, 0.0]).prop_map(Value::Float), + 1 => Just(Value::Null), + ] + .boxed() +} + +/// Fields every layer has: a class, a decimal and a whole number. +fn fields() -> Vec { + vec![Field::new("class", FieldType::Text), Field::new("v", FieldType::Double), Field::new("n", FieldType::Integer)] +} + +/// A layer of `count` features of `geometry`, some repeated, a few without geometry. +fn layer( + name: &'static str, + kind: GeometryType, + geometry: BoxedStrategy, + count: impl Strategy, +) -> impl Strategy { + let class = prop::sample::select(vec!["a", "b", "c", ""]).prop_map(|s| Value::Text(s.into())); + let whole = prop_oneof![4 => (-50i64..50).prop_map(Value::Int), 1 => prop::sample::select(vec![i64::MAX, i64::MIN, 1 << 53]).prop_map(Value::Int)]; + let feature = (prop::option::weighted(0.97, geometry), class, number(), whole, prop::bool::weighted(0.05)); + count.prop_flat_map(move |n| prop::collection::vec(feature.clone(), n)).prop_map(move |features| { + let mut ds = Dataset::new(name, kind, fields()); + let mut last = None; + for (g, class, v, n, again) in features { + // A feature repeating the one before it, as duplicates in real data do. + let (g, attrs) = match (again, last.clone()) { + (true, Some(prev)) => prev, + _ => (g, vec![class, v, n]), + }; + last = Some((g.clone(), attrs.clone())); + ds.push(g, attrs); + } + ds + }) +} + +/// A count around 256, where feature tools go from one thread to several, or a few. +fn around_threshold() -> impl Strategy { + prop_oneof![4 => 240usize..290, 1 => 1usize..20] +} + +fn scene() -> impl Strategy { + let point = (snapped(-0.2, 4.2), snapped(-0.2, 4.2)).prop_map(|(x, y)| Geometry::Point(Coord::new(x, y))).boxed(); + ( + layer("polygons", GeometryType::Polygon, square().boxed(), around_threshold()), + layer("points", GeometryType::Point, point, around_threshold()), + layer("overlay", GeometryType::Polygon, square().boxed(), 1usize..8), + 380u32..520, + prop::collection::vec(0usize..234_000, 0..40), + 16u32..44, + prop::sample::select(vec![2usize, 3, 7, 8]), + ) + .prop_map(|(polygons, points, overlay, width, holes, across, threads)| Scene { + polygons, + points, + overlay, + width, + holes, + across, + threads, + }) +} + +/// The scene's raster: hills over the 4° scene, with holes. +fn raster(s: &Scene) -> Raster { + let (w, h) = (s.width as usize, 450usize); + let mut cells: Vec = + (0..w * h).map(|i| ((i % w) as f32 * 0.05).sin() * 300.0 + ((i / w) as f32 * 0.07).cos() * 200.0 + (i % 7) as f32).collect(); + for &i in &s.holes { + if let Some(c) = cells.get_mut(i) { + *c = -9999.0; + } + } + let gt = GeoTransform { origin_x: 0.0, origin_y: 4.0, pixel_width: 4.0 / w as f64, pixel_height: -4.0 / h as f64 }; + Raster::new("dem", s.width, 450, vec![cells], gt, Crs::LonLat, Some(-9999.0), ColorHint::Gray, "Float32").unwrap() +} + +/// The points with a finite value, scaled by `scale` (metres for a projected system). +fn weighted(s: &Scene, scale: f64) -> Vec<(Coord, f64)> { + s.points + .features + .iter() + .filter_map(|f| match (&f.geometry, f.attrs.get(1)) { + (Some(Geometry::Point(c)), Some(Value::Float(v))) if v.is_finite() && v.abs() < 1e6 => { + Some((Coord::new(c.x * scale, c.y * scale), *v)) + } + _ => None, + }) + .collect() +} + +/// What a tool made: its layer and messages as their debug text (which writes a float exactly), +/// and a raster's cells by their bits. +type Made = Result<(String, Vec), String>; + +/// Every tool's output on the scene, by name, or its error. +fn outputs(s: &Scene) -> Vec<(String, Made)> { + let tool = |id: &str, p: Json| rustgis_analysis::run(id, s, &p).map(|r| (format!("{:?} {:?}", r.output, r.messages), Vec::new())); + let stats = json!([{"field": "v", "statistic": "SUM"}, {"field": "v", "statistic": "MEAN"}, {"field": "n", "statistic": "SUM"}]); + let overlays = ["clip", "erase", "intersect", "identity", "union", "symmetricalDifference", "update", "split"]; + let mut out: Vec<(String, Made)> = overlays + .iter() + .map(|id| { + let key = match *id { + "clip" | "erase" | "split" | "update" => *id, + _ => "overlay", + }; + (id.to_string(), tool(id, json!({"input": "polygons", key: "overlay"}))) + }) + .collect(); + let mut add = |name: &str, r: Made| out.push((name.to_string(), r)); + add("clip points", tool("clip", json!({"input": "points", "clip": "overlay"}))); + add("buffer", tool("buffer", json!({"input": "points", "distance": 5, "units": "Kilometers"}))); + add("buffer dissolved", tool("buffer", json!({"input": "polygons", "distance": 2, "dissolve": "All"}))); + add("dissolve", tool("dissolve", json!({"input": "polygons", "field": "class", "statistics": stats}))); + add("near", tool("near", json!({"input": "points", "near": "polygons"}))); + add("convex hull", tool("convexHull", json!({"input": "polygons", "group": "None"}))); + add("feature to point", tool("featureToPoint", json!({"input": "polygons"}))); + add("simplify", tool("simplify", json!({"input": "polygons", "tolerance": 5}))); + add("select by location", tool("selectByLocation", json!({"input": "points", "select": "polygons", "distance": 1}))); + add("summarize within", tool("countPoints", json!({"polygons": "polygons", "points": "points", "statistics": stats}))); + add("calculate geometry", tool("calculateGeometry", json!({"input": "polygons"}))); + let merge = json!([{"field": "v", "rule": "sum"}, {"field": "n", "rule": "max"}]); + add("spatial join", tool("spatialJoin", json!({"target": "polygons", "join": "points", "merge": merge}))); + add("spatial join closest", tool("spatialJoin", json!({"target": "points", "join": "polygons", "match": "Closest"}))); + add("spatial join many", tool("spatialJoin", json!({"target": "polygons", "join": "points", "operation": "Join one to many"}))); + + let dem = raster(s); + let bits = |r: Result, String>| r.map(|v| (String::new(), v.iter().map(|c| c.to_bits()).collect())); + let rasters = |r: Result| bits(r.map(|r| r.bands.into_iter().flat_map(|b| b.data).collect())); + add("slope", bits(rasterops::slope(&dem, 0, 1.0))); + add("aspect", bits(rasterops::aspect(&dem, 0))); + add("hillshade", bits(rasterops::hillshade(&dem, 0, 315.0, 45.0, 1.0))); + add("band math", bits(rasterops::band_math(&dem, "(b1 - 100) / (b1 + 0.5) * 3"))); + add("k-means", bits(rasterops::kmeans(&dem, &[0], 6, 4))); + let viewshed = rasterops::viewshed(&dem, 0, Coord::new(2.0, 2.0), 30.0, 150_000.0, rasterops::Heights::default()); + add("viewshed", viewshed.map(|v| (format!("{} {}", v.through_gaps, v.read_nearest), v.cells.iter().map(|c| c.to_bits()).collect()))); + let with_raster = WithRaster(s, &dem); + let zonal = rustgis_analysis::run("zonalStats", &with_raster, &json!({"raster": "dem", "polygons": "polygons"})); + add("zonal statistics", zonal.map(|r| (format!("{:?} {:?}", r.output, r.messages), Vec::new()))); + + let extent = Extent::new(-0.2, -0.2, 4.2, 4.2); + let cell = 4.4 / f64::from(s.across); + let points = weighted(s, 1.0); + add("idw", rasters(rasterops::idw(&points, &extent, cell, 2.0, 0, "idw", Crs::LonLat))); + add("idw power 3", rasters(rasterops::idw(&points, &extent, cell, 3.0, 12, "idw", Crs::LonLat))); + let params = kriging::auto_variogram(&points, &extent, kriging::VariogramModel::Spherical); + add("kriging", rasters(kriging::kriging(&points, &extent, cell, ¶ms, 12, "kriging", Crs::LonLat))); + // Kernel density takes a projected system: the scene in metres, 50 km around each point. + let metres = weighted(s, 100_000.0); + let radius = 50_000.0; + for kernel in [density::KernelType::Quartic, density::KernelType::Gaussian] { + let made = density::kernel_density(&metres, None, radius / f64::from(2 * s.across), radius, kernel, "kd", Crs::WebMercator); + add(&format!("kernel density {kernel:?}"), rasters(made)); + } + out +} + +/// The scene with its raster, for Zonal Statistics. +struct WithRaster<'a>(&'a Scene, &'a Raster); + +impl Inputs for WithRaster<'_> { + fn dataset(&self, name: &str) -> Option<&Dataset> { + self.0.dataset(name) + } + fn selection(&self, _: &str) -> &[usize] { + &[] + } + fn raster(&self, name: &str) -> Option> { + (name == "dem").then_some(RasterInput { raster: self.1, path: None, embedded: None }) + } +} + +/// Where two outputs first differ: the text with some of each around it, or the cell. +fn first_difference((a, cells): &(String, Vec), (b, others): &(String, Vec)) -> String { + if let Some(i) = cells.iter().zip(others).position(|(x, y)| x != y).or((cells.len() != others.len()).then_some(0)) { + let at = |c: &[u32]| c.get(i).map(|v| f32::from_bits(*v)); + return format!("cell {i}: one thread {:?}, several {:?}", at(cells), at(others)); + } + let at = a.bytes().zip(b.bytes()).position(|(x, y)| x != y).unwrap_or(a.len().min(b.len())); + let around = |t: &str| t.get(at.saturating_sub(60)..(at + 60).min(t.len())).unwrap_or("").to_string(); + format!("at byte {at}: one thread «{}», several «{}»", around(a), around(b)) +} + +#[test] +fn several_threads_give_what_one_thread_gives() { + let seed = env("PROPERTY_SEED", 1); + let mut bytes = [0u8; 32]; + bytes[..8].copy_from_slice(&seed.to_le_bytes()); + // About two seconds a case. + let cases = u32::try_from(env("PROPERTY_CASES", 6)).unwrap_or(6); + let config = Config { cases, failure_persistence: None, ..Config::default() }; + let mut runner = TestRunner::new_with_rng(config, TestRng::from_seed(RngAlgorithm::ChaCha, &bytes)); + let result = runner.run(&scene(), |s| { + threads::set_max(Some(1)).unwrap(); + let one = outputs(&s); + threads::set_max(Some(s.threads)).unwrap(); + let several = outputs(&s); + threads::set_max(None).unwrap(); + let short = |r: &Made| match r { + Ok((t, _)) => format!("made {}…", t.chars().take(120).collect::()), + Err(e) => format!("refused: {e}"), + }; + for ((name, a), (_, b)) in one.iter().zip(&several) { + match (a, b) { + (Ok(x), Ok(y)) if x != y => { + return Err(TestCaseError::fail(format!("{name} on {} threads: {}", s.threads, first_difference(x, y)))); + } + _ if a != b => { + return Err(TestCaseError::fail(format!("{name} on {} threads: one {}; several {}", s.threads, short(a), short(b)))); + } + _ => {} + } + } + Ok(()) + }); + threads::set_max(None).unwrap(); + if let Err(e) = result { + panic!("PROPERTY_SEED={seed}: {e}"); + } +} From 0e39c80ea7601b90b0cb72f76c3581781e313d46 Mon Sep 17 00:00:00 2001 From: mavrick Date: Thu, 12 Nov 2026 17:23:59 -0600 Subject: [PATCH 2/4] test(engine): saving and opening a project gives the same project, as a property A project built with commands from small fixture layers (unicode, long and blank names, every renderer kind, nested groups, an empty layer, values that aren't finite numbers) is saved with Save As and opened again; what it saves must come back unchanged, or the save or open says what changed. A second property adds unknown fields to a saved file and raises its format: a newer format is refused with the update advice and changes nothing, and unknown fields open as the file would without them, or are refused. Co-Authored-By: Claude Opus 5.5 --- .github/workflows/nightly.yml | 1 + crates/engine/tests/project_save_roundtrip.rs | 381 ++++++++++++++++++ 2 files changed, 382 insertions(+) create mode 100644 crates/engine/tests/project_save_roundtrip.rs diff --git a/.github/workflows/nightly.yml b/.github/workflows/nightly.yml index 569ecabfd5..10604da458 100644 --- a/.github/workflows/nightly.yml +++ b/.github/workflows/nightly.yml @@ -121,6 +121,7 @@ jobs: run: | cargo test --locked -p rustgis-engine --test never_silent_roundtrip PROPERTY_CASES=200 cargo test --locked -p rustgis-analysis --test parallel_serial + cargo test --locked -p rustgis-engine --test project_save_roundtrip report: name: Report diff --git a/crates/engine/tests/project_save_roundtrip.rs b/crates/engine/tests/project_save_roundtrip.rs new file mode 100644 index 0000000000..5130f94cdb --- /dev/null +++ b/crates/engine/tests/project_save_roundtrip.rs @@ -0,0 +1,381 @@ +//! Saving a project and opening it again gives back the same project, as a property: a project +//! built with commands from a few small layers (Project › Save As, then Project › Open) opens with +//! the same layers, symbology, labels, groups, layouts, bookmarks, time and map settings, or the +//! save or open says what changed. What a project file doesn't keep is listed in [`LIMITS`], with +//! the reason. Projects lean on edges: unicode, long and blank names, every renderer kind, nested +//! groups, an empty layer, and values that aren't finite numbers, as a reader may leave them. +//! +//! A second property: a saved file with fields this version doesn't know opens as it would +//! without them, or is refused; one that declares a newer format is refused, says to update, and +//! changes nothing. +//! +//! Pull requests run a fixed seed; the nightly a new one with more cases. To run a seed again, or +//! deeper: `PROPERTY_SEED=7 PROPERTY_CASES=2000 cargo test -p rustgis-engine --test project_save_roundtrip`. +use proptest::prelude::*; +use proptest::test_runner::{Config, RngAlgorithm, TestCaseError, TestRng, TestRunner}; +use rustgis_engine::{ + Project, Session, + data::{Dataset, Field, FieldType, GeometryType}, +}; +use serde_json::{Value, json}; +use std::path::Path; + +/// Parts of a project that saving and opening change by design, as (a path in the saved project, +/// `*` for any one step of it, and why). +const LIMITS: &[(&str, &str)] = &[ + ("/name", "an opened project takes its file's name (`name_from_file`)"), + ("/maps/*/layers/*/geometry_rev", "a counter that tells caches the geometry changed, bumped as a layer is read"), +]; + +/// Whether the change `c` ("path: before → after") is at a path listed in [`LIMITS`]. +fn declared(c: &str) -> bool { + let path: Vec<&str> = c.split(": ").next().unwrap_or_default().split('/').collect(); + LIMITS.iter().any(|(limit, _)| { + let limit: Vec<&str> = limit.split('/').collect(); + limit.len() <= path.len() && limit.iter().zip(&path).all(|(l, p)| *l == "*" || l == p) + }) +} + +/// A number from the environment, else `default`. +fn env(name: &str, default: u64) -> u64 { + std::env::var(name).ok().and_then(|v| v.parse().ok()).unwrap_or(default) +} + +/// Names leaning on edges: unicode, long, blank, spaces around, quotes, a duplicate. +fn name() -> impl Strategy { + prop_oneof![ + 3 => "[A-Za-z][A-Za-z0-9 ]{0,10}", + 1 => prop::sample::select(vec![ + "Ünïcødé 名前 😀", "", " spaced ", "quote \"x\" 'y'", "line\nbreak", "Points", "a/b\\c", "&", " ", + ]) + .prop_map(String::from), + 1 => (100usize..400).prop_map(|n| "é".repeat(n)), + ] +} + +/// A colour, sometimes transparent. +fn color() -> impl Strategy { + prop_oneof![ + 3 => any::<[u8; 3]>().prop_map(|[r, g, b]| format!("#{r:02X}{g:02X}{b:02X}")), + 1 => any::<[u8; 4]>().prop_map(|[r, g, b, a]| format!("#{r:02X}{g:02X}{b:02X}{a:02X}")), + ] +} + +/// A number, sometimes on an edge. +fn number() -> impl Strategy { + prop_oneof![4 => 0.0..100.0f64, 1 => prop::sample::select(vec![0.0, -1.0, 1e-9, 1e12, 99.999, 100.0])] +} + +/// One edit, as a command and its parameters; "@L" stands for the id of the i-th layer. +fn step() -> impl Strategy { + let layer = (0usize..4).prop_map(|i| format!("@L{i}")); + prop_oneof![ + (layer.clone(), color()).prop_map(|(l, c)| ("layer.symbology", json!({"layer": l, "type": "single", "color": c}))), + (layer.clone(), prop::sample::select(vec!["name", "flag", "pop"])) + .prop_map(|(l, f)| ("layer.symbology", json!({"layer": l, "type": "unique", "field": f}))), + layer.clone().prop_map(|l| ("layer.symbology", json!({"layer": l, "type": "unique", "expression": "name || '!'"}))), + (layer.clone(), 1u32..13, prop::sample::select(vec!["equal", "quantile", "natural", "manual"])) + .prop_map(|(l, n, m)| ("layer.symbology", json!({"layer": l, "type": "graduated", "field": "pop", "classes": n, "method": m}))), + layer.clone().prop_map(|l| ("layer.symbology", json!({"layer": l, "type": "proportional", "field": "pop"}))), + (layer.clone(), 0u32..4, color(), name()) + .prop_map(|(l, i, c, n)| ("@classStyle", json!({"layer": l, "index": i, "color": c, "label": n}))), + (layer.clone(), prop::sample::select(vec!["Ember", "Viridis"])).prop_map(|(l, r)| ("layer.ramp", json!({"layer": l, "ramp": r}))), + (layer.clone(), number()).prop_map(|(l, v)| ("layer.transparency", json!({"layer": l, "value": v}))), + (layer.clone(), any::()).prop_map(|(l, v)| ("layer.visibility", json!({"layer": l, "visible": v}))), + (layer.clone(), number(), number()).prop_map(|(l, a, b)| ("layer.scaleRange", json!({"layer": l, "min": a * 1e5, "max": b * 1e3}))), + (layer.clone(), name()).prop_map(|(l, n)| ("layer.rename", json!({"layer": l, "name": n}))), + (layer.clone(), color(), number()).prop_map(|(l, c, w)| ("layer.outline", json!({"layer": l, "color": c, "width": w / 10.0}))), + (layer.clone(), prop::sample::select(vec!["A", "★", "😀"]), number()) + .prop_map(|(l, c, n)| ("layer.marker", json!({"layer": l, "char": c, "size": n}))), + (layer.clone(), prop::sample::select(vec!["pop > 10", "name = 'é'", ""])) + .prop_map(|(l, w)| ("layer.definitionQuery", json!({"layer": l, "where": w}))), + (layer.clone(), prop::sample::select(vec!["multiply", "screen", "normal"])) + .prop_map(|(l, m)| ("layer.blend", json!({"layer": l, "mode": m}))), + (layer.clone(), name()).prop_map(|(l, n)| ( + "layer.displayFilters", + json!({"layer": l, "filters": [{"name": n, "minScale": 1e8, "maxScale": 1e3, "where": "pop > 1"}]}) + )), + (layer.clone(), color()) + .prop_map(|(l, c)| ("layer.fillLayers", json!({"layer": l, "fills": [{"color": c}, {"pattern": "hatch", "angle": 135}]}))), + layer.clone().prop_map(|l| ("layer.markerRotation", json!({"layer": l, "field": "pop", "counterclockwise": true}))), + (layer.clone(), -5i32..6).prop_map(|(l, v)| ("layer.priority", json!({"layer": l, "value": v}))), + (layer.clone(), number()).prop_map(|(l, v)| ("layer.referenceScale", json!({"layer": l, "scale": v * 1e4}))), + layer.clone().prop_map(|l| ("layer.symbolBy", json!({"layer": l, "size": "pop / 10", "fill": "if(pop > 5, '#ff0000', NULL)"}))), + layer.clone().prop_map(|l| ("layer.noFill", json!({"layer": l}))), + (layer.clone(), any::()).prop_map(|(l, v)| ("labels.enable", json!({"layer": l, "on": v}))), + (layer.clone(), prop::sample::select(vec!["name || ' ' || pop", "'Ünï 😀'", "pop"])) + .prop_map(|(l, t)| ("labels.by", json!({"layer": l, "text": t}))), + (layer.clone(), number(), any::(), any::()) + .prop_map(|(l, n, b, i)| ("labels.style", json!({"layer": l, "size": n / 2.0 + 1.0, "bold": b, "italic": i}))), + (layer.clone(), 0u32..100).prop_map(|(l, c)| ("labels.placement", json!({"layer": l, "callout": c}))), + (layer.clone(), number()).prop_map(|(l, m)| ("labels.scaleRange", json!({"layer": l, "min": m * 1e5}))), + (name(), layer.clone(), layer.clone()).prop_map(|(n, a, b)| ("group.add", json!({"name": n, "layers": [a, b]}))), + (layer.clone(), layer.clone()).prop_map(|(a, b)| ("@nest", json!([a, b]))), + name().prop_map(|n| ("bookmark.add", json!({"name": n}))), + layer.clone().prop_map(|l| ("time.bind", json!({"layer": l, "field": "when"}))), + (prop::sample::select(vec!["2015-01", "2020-02-29", "1900-01-01"]), 1u32..5, prop::sample::select(vec!["cumulative", "wide"])) + .prop_map(|(d, i, w)| ("time.set", json!({"start": "1900", "end": "2030", "date": d, "interval": i, "show": w}))), + name().prop_map(|n| ("layout.new", json!({"name": n}))), + (prop::sample::select(vec!["legend", "scaleBar", "northArrow", "text", "rectangle", "grid", "colorbar"]), name()) + .prop_map(|(k, t)| ("layout.addElement", json!({"kind": k, "text": t}))), + prop::sample::select(vec!["paper", "midnight", "ocean", "atlas", "blank"]).prop_map(|n| ("map.basemap", json!({"name": n}))), + prop::sample::select(vec!["Albers Equal Area Conic +lat_1=29.5 +lat_2=45.5", "EPSG:3857", "EPSG:4326"]) + .prop_map(|t| ("map.projection", json!({"type": t}))), + (-180.0..180.0f64).prop_map(|b| ("map.setBearing", json!({"bearing": b}))), + prop::sample::select(vec!["degrees", "measured", "utm"]).prop_map(|t| ("map.graticule", json!({"on": true, "type": t}))), + (-180.0..180.0f64, -85.0..85.0f64, number()) + .prop_map(|(x, y, s)| ("map.camera", json!({"lon": x, "lat": y, "scale": s * 1e5 + 1.0}))), + name().prop_map(|n| ("map.new", json!({"name": n}))), + (layer.clone(), name()).prop_map(|(l, n)| ("style.new", json!({"layer": l, "name": n}))), + (layer, 0u8..4, prop::sample::select(vec![f64::NAN, f64::INFINITY, f64::NEG_INFINITY, -0.0])) + .prop_map(|(l, row, v)| ("@poison", json!([l, row, v.to_string()]))), + ] +} + +/// The fixture: points, lines and polygons with text, numbers, dates and yes/no values, and an +/// empty layer, each under a generated name. +fn layers(s: &mut Session, names: &[String]) -> Vec { + const TEXT: [&str; 4] = ["Ünï 😀", "", "a, b", "x"]; + let props = |i: usize| json!({"name": TEXT[i % 4], "pop": i as f64 * 3.5, "when": format!("201{i}-0{}-15", i + 1), "flag": i.is_multiple_of(2)}); + let points = (0..4).map(|i| json!({"type": "Feature", "properties": props(i), "geometry": {"type": "Point", "coordinates": [i as f64 * 40.0 - 179.9, 89.9 - i as f64 * 50.0]}})); + let lines = (0..3).map(|i| json!({"type": "Feature", "properties": props(i), "geometry": {"type": "LineString", "coordinates": [[-10.0, i as f64], [10.0, i as f64 + 1.0]]}})); + let polygons = (0..3).map(|i| { + let x = i as f64 * 5.0; + json!({"type": "Feature", "properties": props(i), "geometry": {"type": "Polygon", "coordinates": [[[x, 0.0], [x + 4.0, 0.0], [x + 4.0, 4.0], [x, 0.0]]]}}) + }); + let sets: [Vec; 3] = [points.collect(), lines.collect(), polygons.collect()]; + let mut ids = Vec::new(); + for (features, name) in sets.iter().zip(names) { + let text = json!({"type": "FeatureCollection", "features": features}).to_string(); + let before: Vec = s.map().layers.iter().map(|l| l.id).collect(); + s.run("layer.addDataText", &json!({"fileName": "layer.geojson", "text": text, "name": name})).unwrap(); + ids.extend(s.map().layers.iter().map(|l| l.id).filter(|id| !before.contains(id))); + } + // A file without features isn't read as a layer, so the empty one is added as a reader leaves it. + let fields = ["name", "when"].map(|f| Field::new(f, FieldType::Text)).into_iter().chain([Field::new("pop", FieldType::Double)]); + let empty = Dataset::new(names.get(3).map_or("", String::as_str), GeometryType::Point, fields.collect()); + ids.push(s.map_mut().add_dataset(empty)); + ids +} + +/// `params` with each "@L" replaced by that layer's id. +fn resolve(params: &Value, ids: &[u32]) -> Value { + match params { + Value::String(t) => { + t.strip_prefix("@L").and_then(|i| i.parse::().ok()).and_then(|i| ids.get(i)).map_or(params.clone(), |id| json!(id)) + } + Value::Array(a) => Value::Array(a.iter().map(|v| resolve(v, ids)).collect()), + Value::Object(o) => Value::Object(o.iter().map(|(k, v)| (k.clone(), resolve(v, ids))).collect()), + _ => params.clone(), + } +} + +/// Runs one edit, true when it changed something. `@nest` puts the second layer's group inside +/// the first's; `@classStyle` styles a class of Unique Values; `@poison` leaves a number that isn't +/// finite in a row's values, as a reader may (a NaN REAL in a GeoPackage). The commands and the +/// readers refuse one in a style, so a style isn't given one. +fn apply(s: &mut Session, cmd: &str, params: &Value, ids: &[u32]) -> bool { + let p = resolve(params, ids); + // A layer's command works on the layer selected in the Layers pane. + if let Some(id) = p.get("layer").and_then(Value::as_u64) { + s.active_layer = u32::try_from(id).ok(); + } + match cmd { + "@nest" => { + let groups = s.run("group.add", &json!({"name": "Outer", "layers": [p[0]]})).ok().and_then(|v| v["group"].as_u64()); + let inner = s.run("group.add", &json!({"name": "Inner", "layers": [p[1]]})).ok().and_then(|v| v["group"].as_u64()); + groups.zip(inner).is_some_and(|(g, i)| s.run("group.setParent", &json!({"group": i, "parent": g, "before": null})).is_ok()) + } + "@classStyle" => { + s.run("layer.symbology", &json!({"layer": p["layer"], "type": "unique", "field": "name"})).is_ok() + && s.run("layer.classStyle", &p).is_ok() + } + "@poison" => { + let (Some(id), Some(row), Some(v)) = (p[0].as_u64(), p[1].as_u64(), p[2].as_str().and_then(|v| v.parse::().ok())) else { + return false; + }; + let Some(l) = s.map_mut().layer_mut(id as u32) else { return false }; + let pop = l.dataset.fields.iter().position(|f| f.name == "pop"); + let cell = l.dataset.features.get_mut(row as usize).zip(pop).and_then(|(f, i)| f.attrs.get_mut(i)); + cell.map(|c| *c = rustgis_engine::data::Value::Float(v)).is_some() + } + _ => s.run(cmd, &p).is_ok(), + } +} + +/// The paths at which two saved projects differ, with both values. +fn diff(path: &str, a: &Value, b: &Value, out: &mut Vec) { + match (a, b) { + (Value::Object(x), Value::Object(y)) => { + for k in x.keys().chain(y.keys().filter(|k| !x.contains_key(*k))) { + diff(&format!("{path}/{k}"), x.get(k).unwrap_or(&Value::Null), y.get(k).unwrap_or(&Value::Null), out); + } + } + (Value::Array(x), Value::Array(y)) if x.len() == y.len() => { + for (i, (u, v)) in x.iter().zip(y).enumerate() { + diff(&format!("{path}/{i}"), u, v, out); + } + } + _ if a != b => { + let short = |v: &Value| v.to_string().chars().take(80).collect::(); + out.push(format!("{path}: {} → {}", short(a), short(b))); + } + _ => {} + } +} + +/// What the user sees of a project: everything it saves. +fn seen(s: &Session) -> Value { + serde_json::to_value(&s.project).unwrap() +} + +/// What notices since `mark` said, besides that a file was saved or opened. +fn said(s: &Session, mark: u64) -> Vec { + s.notices_since(mark).iter().map(|n| n.text.clone()).filter(|t| !t.starts_with("Saved ") && !t.starts_with("Opened ")).collect() +} + +/// A project: names for the four fixture layers and up to twelve edits. +fn project() -> impl Strategy, Vec<(&'static str, Value)>)> { + (prop::collection::vec(name(), 4), prop::collection::vec(step(), 0..12)) +} + +/// Builds the project in a new session, with how many edits changed something. +fn build(names: &[String], steps: &[(&'static str, Value)]) -> (Session, usize) { + let mut s = Session::new(Project::new("Project")); + let ids = layers(&mut s, names); + s.run("layout.new", &json!({"name": "Page"})).unwrap(); + let applied = steps.iter().filter(|(cmd, p)| apply(&mut s, cmd, p, &ids)).count(); + (s, applied) +} + +/// A seeded runner, as the convention says. +fn runner(default_cases: u32) -> (u64, TestRunner) { + let seed = env("PROPERTY_SEED", 1); + let mut bytes = [0u8; 32]; + bytes[..8].copy_from_slice(&seed.to_le_bytes()); + let cases = u32::try_from(env("PROPERTY_CASES", u64::from(default_cases))).unwrap_or(default_cases); + let config = Config { cases, failure_persistence: None, ..Config::default() }; + (seed, TestRunner::new_with_rng(config, TestRng::from_seed(RngAlgorithm::ChaCha, &bytes))) +} + +/// A private scratch folder for one test. +fn scratch(test: &str) -> std::path::PathBuf { + let dir = std::env::temp_dir().join(format!("rustgis-{test}-{}", std::process::id())); + let _ = std::fs::remove_dir_all(&dir); + std::fs::create_dir_all(&dir).unwrap(); + dir +} + +/// Saves `s` as `path`, and opens it in a new session: what each said, and the opened session. +fn save_and_open(s: &mut Session, path: &Path) -> Result<(Vec, Session), String> { + let mark = s.notice_mark(); + s.run("project.saveAs", &json!({"path": path}))?; + let mut said_all = said(s, mark); + let mut opened = Session::new(Project::new("Other")); + let mark = opened.notice_mark(); + opened.run("project.open", &json!({"path": path}))?; + said_all.extend(said(&opened, mark)); + Ok((said_all, opened)) +} + +#[test] +fn a_saved_project_opens_as_it_was_saved() { + let dir = scratch("project-save"); + let path = dir.join("project.rgis"); + let (seed, mut runner) = runner(256); + let (cases, edits, compared) = (std::cell::Cell::new(0), std::cell::Cell::new(0), std::cell::Cell::new(0)); + let result = runner.run(&project(), |(names, steps)| { + let (mut s, applied) = build(&names, &steps); + cases.set(cases.get() + usize::from(applied > 0)); + edits.set(edits.get() + applied); + let before = seen(&s); + let (notices, opened) = save_and_open(&mut s, &path).map_err(|e| TestCaseError::fail(format!("didn't save and open: {e}")))?; + let mut changed = Vec::new(); + diff("", &before, &seen(&opened), &mut changed); + compared.set(compared.get() + usize::from(notices.is_empty())); + // The one loss saving says (values that aren't finite numbers, written as null) is in + // the layers' values; a notice excuses nothing else. + changed.retain(|c| !declared(c) && (notices.is_empty() || !c.contains("/dataset/features/"))); + if !changed.is_empty() { + return Err(TestCaseError::fail(format!("changed, saying {notices:?}: {changed:#?}"))); + } + Ok(()) + }); + let _ = std::fs::remove_dir_all(&dir); + eprintln!("{} cases had edits, {} edits in all, {} compared without a notice", cases.get(), edits.get(), compared.get()); + if let Err(e) = result { + panic!("PROPERTY_SEED={seed}: {e}"); + } +} + +/// Puts `key` in the `n`-th object of `v` (counting depth first), if it has that many. +fn insert_at(v: &mut Value, n: &mut usize, key: &str) -> bool { + match v { + Value::Object(o) => { + if *n == 0 { + o.insert(key.to_string(), json!({"from": "a newer RustGIS", "values": [1, 2]})); + return true; + } + *n -= 1; + o.values_mut().any(|v| insert_at(v, n, key)) + } + Value::Array(a) => a.iter_mut().any(|v| insert_at(v, n, key)), + _ => false, + } +} + +#[test] +fn a_project_from_a_newer_rustgis_opens_only_when_this_version_can_keep_it() { + let dir = scratch("project-newer"); + let (clean, newer) = (dir.join("clean.rgis"), dir.join("newer.rgis")); + let (seed, mut runner) = runner(128); + let places = prop::collection::vec(0usize..400, 1..4); + let raised = prop::option::of(prop::sample::select(vec![1u64, 2, 1000, u64::from(u32::MAX)])); + let (opened, refused) = (std::cell::Cell::new(0), std::cell::Cell::new(0)); + let result = runner.run(&(project(), places, raised), |((names, steps), places, raised)| { + let (mut s, _) = build(&names, &steps); + s.run("project.saveAs", &json!({"path": clean})).map_err(TestCaseError::fail)?; + let mut file: Value = serde_json::from_str(&std::fs::read_to_string(&clean).unwrap()).unwrap(); + for (i, mut place) in places.into_iter().enumerate() { + insert_at(&mut file, &mut place, &format!("futureField{i}")); + } + if let Some(k) = raised { + file["format"] = json!(u64::from(rustgis_engine::project::FORMAT) + k); + } + std::fs::write(&newer, file.to_string()).unwrap(); + let mut a = Session::new(Project::new("Other")); + a.run("project.open", &json!({"path": clean})).map_err(TestCaseError::fail)?; + let mut b = Session::new(Project::new("Other")); + let before = seen(&b); + let open = b.run("project.open", &json!({"path": newer})); + let count = if open.is_ok() { &opened } else { &refused }; + count.set(count.get() + 1); + match (open, raised) { + (Ok(_), Some(k)) => Err(TestCaseError::fail(format!("a project of format +{k} opened"))), + (Err(e), Some(_)) if !e.contains("Update RustGIS") || seen(&b) != before => { + Err(TestCaseError::fail(format!("refused without saying to update, or changed the open project: {e}"))) + } + // A field put where a newer RustGIS couldn't write one (beside an enum's variant, in a + // list of values) makes a file this version can't read: refused, it changes nothing. + (Err(e), None) if seen(&b) != before => Err(TestCaseError::fail(format!("a refused file changed the open project: {e}"))), + (Ok(_), None) => { + // Fields this version doesn't know are left out, as a newer RustGIS that writes + // something an older one would lose raises the format (docs/maintaining.md). + let mut changed = Vec::new(); + diff("", &seen(&a), &seen(&b), &mut changed); + changed.retain(|c| !declared(c)); + if changed.is_empty() { + Ok(()) + } else { + Err(TestCaseError::fail(format!("unknown fields changed the project: {changed:#?}"))) + } + } + _ => Ok(()), + } + }); + let _ = std::fs::remove_dir_all(&dir); + eprintln!("{} opened, {} refused", opened.get(), refused.get()); + if let Err(e) = result { + panic!("PROPERTY_SEED={seed}: {e}"); + } +} From 33b99f804426128816b1083ca28dcdaf9c0493f0 Mon Sep 17 00:00:00 2001 From: mavrick Date: Fri, 13 Nov 2026 02:50:19 -0600 Subject: [PATCH 3/4] test(engine): the project property excuses only values saved as null, and refusals keep a full project A notice of non-finite values written as null excuses only a feature value that became null; a refused file is opened over a generated project, which must stay as it was. Co-Authored-By: Claude Opus 5.5 --- crates/engine/tests/project_save_roundtrip.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/crates/engine/tests/project_save_roundtrip.rs b/crates/engine/tests/project_save_roundtrip.rs index 5130f94cdb..139895217a 100644 --- a/crates/engine/tests/project_save_roundtrip.rs +++ b/crates/engine/tests/project_save_roundtrip.rs @@ -295,7 +295,8 @@ fn a_saved_project_opens_as_it_was_saved() { compared.set(compared.get() + usize::from(notices.is_empty())); // The one loss saving says (values that aren't finite numbers, written as null) is in // the layers' values; a notice excuses nothing else. - changed.retain(|c| !declared(c) && (notices.is_empty() || !c.contains("/dataset/features/"))); + let non_finite = notices.iter().any(|n| n.contains("aren't finite numbers")); + changed.retain(|c| !declared(c) && !(non_finite && c.contains("/dataset/features/") && c.ends_with(" → null"))); if !changed.is_empty() { return Err(TestCaseError::fail(format!("changed, saying {notices:?}: {changed:#?}"))); } @@ -345,7 +346,8 @@ fn a_project_from_a_newer_rustgis_opens_only_when_this_version_can_keep_it() { std::fs::write(&newer, file.to_string()).unwrap(); let mut a = Session::new(Project::new("Other")); a.run("project.open", &json!({"path": clean})).map_err(TestCaseError::fail)?; - let mut b = Session::new(Project::new("Other")); + // Refused, it leaves the project open as it was, layers, groups and layouts too. + let (mut b, _) = build(&names, &steps); let before = seen(&b); let open = b.run("project.open", &json!({"path": newer})); let count = if open.is_ok() { &opened } else { &refused }; From 9602fcac49e64acf07c39c10067956f1b60d9e00 Mon Sep 17 00:00:00 2001 From: mavrick Date: Fri, 13 Nov 2026 03:32:52 -0600 Subject: [PATCH 4/4] test(engine): the project property excuses nothing; non-finite values are null before saving too Co-Authored-By: Claude Opus 5.5 --- crates/engine/tests/project_save_roundtrip.rs | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/crates/engine/tests/project_save_roundtrip.rs b/crates/engine/tests/project_save_roundtrip.rs index 139895217a..8bb3d9afd5 100644 --- a/crates/engine/tests/project_save_roundtrip.rs +++ b/crates/engine/tests/project_save_roundtrip.rs @@ -293,10 +293,9 @@ fn a_saved_project_opens_as_it_was_saved() { let mut changed = Vec::new(); diff("", &before, &seen(&opened), &mut changed); compared.set(compared.get() + usize::from(notices.is_empty())); - // The one loss saving says (values that aren't finite numbers, written as null) is in - // the layers' values; a notice excuses nothing else. - let non_finite = notices.iter().any(|n| n.contains("aren't finite numbers")); - changed.retain(|c| !declared(c) && !(non_finite && c.contains("/dataset/features/") && c.ends_with(" → null"))); + // Values that aren't finite numbers, which saving writes as null and says so, are null in + // what the user sees before saving too (`Value`'s `Serialize`): nothing is excused. + changed.retain(|c| !declared(c)); if !changed.is_empty() { return Err(TestCaseError::fail(format!("changed, saying {notices:?}: {changed:#?}"))); }