From e7d838784fdabf0bb03a27c82c89443876ade61e Mon Sep 17 00:00:00 2001 From: Joshua Davis Date: Mon, 2 Nov 2026 15:05:56 -0500 Subject: [PATCH] Close gaps in the GPX and repeat-route tests Mutations run against #28 survived these tests: export.gpx coordinates, thinning on import, and the new-road flag changing during a track. The tests now compare export-all and re-imported points, require thinning, drive a detour off known road and back, and end a track by stopping. Story: CHOMP-3 Story: CHOMP-5 Co-Authored-By: Claude Opus 5.5 --- crates/carchompd/src/api.rs | 82 ++++++++++++++++++++++++-------- crates/carchompd/src/recorder.rs | 60 +++++++++++++++++++++++ 2 files changed, 121 insertions(+), 21 deletions(-) diff --git a/crates/carchompd/src/api.rs b/crates/carchompd/src/api.rs index eac2cfe..7b0ea48 100644 --- a/crates/carchompd/src/api.rs +++ b/crates/carchompd/src/api.rs @@ -1094,6 +1094,21 @@ mod tests { (a.lon - b.lon).abs() < 1e-6 && (a.lat - b.lat).abs() < 1e-6 } + /// `kept` keeps the ends of `sent`, and each point in it is one `sent` has: + /// the same time, place and elevation. + fn assert_kept_from(sent: &[interchange::Point], kept: &[interchange::Point]) { + assert_eq!(kept[0].time, sent[0].time, "first point"); + assert_eq!(kept.last().unwrap().time, sent.last().unwrap().time, "last"); + for b in kept { + let a = sent + .iter() + .find(|a| a.time == b.time) + .unwrap_or_else(|| panic!("{b:?} was never sent")); + assert!(close(a, b), "{a:?} came back as {b:?}"); + assert_eq!(a.ele, b.ele, "elevation"); + } + } + /// Needs PostGIS at `DATABASE_URL`, so `cargo test` skips it and CI runs /// it with `--ignored`. Uses a database of its own, as it commits. #[tokio::test] @@ -1133,44 +1148,69 @@ mod tests { assert!(b.time.is_some(), "stored points have times"); } - // A recorded drive: thinned on the way in, but its ends and times stay. - let timed = r#" - - - - - "#; - let sent_timed = interchange::parse(timed).unwrap().remove(0); - let (code, ids) = http(addr, "POST", "/api/tracks/import", timed).await; + // A recorded drive, a minute at 20 m/s: thinned on the way in, but what + // stays is exactly what was sent, and its ends stay. + let trkpts: String = (0..60) + .map(|i| { + format!( + r#""#, + -122.7 + 0.00025 * f64::from(i) + ) + }) + .collect(); + let timed = format!( + r#"{trkpts}"# + ); + let sent_timed = interchange::parse(&timed).unwrap().remove(0); + let (code, ids) = http(addr, "POST", "/api/tracks/import", &timed).await; assert_eq!(code, 200, "{ids}"); let ids: Vec = serde_json::from_str(&ids).unwrap(); let (_, exported) = http(addr, "GET", &format!("/api/tracks/{}/gpx", ids[0]), "").await; let drive = interchange::parse(&exported).unwrap().remove(0); - for (a, b) in [ - (&sent_timed.points[0], &drive.points[0]), - ( - sent_timed.points.last().unwrap(), - drive.points.last().unwrap(), - ), - ] { - assert!(close(a, b), "{a:?} came back as {b:?}"); - assert_eq!(a.time, b.time); - } + assert!( + drive.points.len() < sent_timed.points.len(), + "not thinned: kept all {}", + drive.points.len() + ); + assert_kept_from(&sent_timed.points, &drive.points); - // Export all holds both, and imports again as what it was. + // Export all holds both, the same as exporting each alone. let (code, all) = http(addr, "GET", "/api/tracks/export.gpx", "").await; assert_eq!(code, 200); let tracks = interchange::parse(&all).unwrap(); assert_eq!(tracks.len(), 2); + let named = |tracks: &[interchange::Track], name: Option<&str>| -> Vec<_> { + tracks + .iter() + .filter(|t| t.name.as_deref() == name) + .cloned() + .collect() + }; + assert_eq!(named(&tracks, Some("Round trip"))[0].points, back.points); + assert_eq!(named(&tracks, None)[0].points, drive.points); + + // Importing that again gives each track a second copy. Everything is + // timed by now, so the copies may be thinned, but only to what it was. let (code, _) = http(addr, "POST", "/api/tracks/import", &all).await; assert_eq!(code, 200); + let (code, all) = http(addr, "GET", "/api/tracks/export.gpx", "").await; + assert_eq!(code, 200); + let again = interchange::parse(&all).unwrap(); + for (name, original) in [(Some("Round trip"), &back), (None, &drive)] { + let copies = named(&again, name); + assert_eq!(copies.len(), 2, "{name:?}"); + for copy in &copies { + assert_kept_from(&original.points, ©.points); + } + } let (code, health) = http(addr, "GET", "/api/health", "").await; assert_eq!(code, 200); let health: serde_json::Value = serde_json::from_str(&health).unwrap(); assert_eq!(health["tracks"], 4, "{health}"); assert_eq!(health["version"], VERSION); - assert!(health["fixes"].as_i64().unwrap() >= 8, "{health}"); + let stored: usize = again.iter().map(|t| t.points.len()).sum(); + assert_eq!(health["fixes"], stored, "{health}"); assert!(health["system"].is_object(), "{health}"); let (code, _) = http(addr, "POST", "/api/tracks/import", "not a track").await; diff --git a/crates/carchompd/src/recorder.rs b/crates/carchompd/src/recorder.rs index b6c3927..e390d7e 100644 --- a/crates/carchompd/src/recorder.rs +++ b/crates/carchompd/src/recorder.rs @@ -510,6 +510,40 @@ mod tests { flags } + /// Drives legs of (course, fixes), one fix per second at 20 m/s, and returns + /// the new-road flag after each fix. + async fn drive_legs( + rec: &mut Recorder, + source: i16, + start: OffsetDateTime, + from: (f64, f64), + legs: &[(f64, u32)], + ) -> Vec> { + let (mut lon, mut lat) = from; + let mut time = start; + let mut flags = Vec::new(); + for &(course, n) in legs { + for _ in 0..n { + let fix = Fix { + time, + lat, + lon, + alt: None, + speed: Some(20.0), + course: Some(course), + h_err: None, + }; + rec.fix(source, &fix).await.unwrap(); + flags.push(rec.status.borrow().road_new); + let metres = 20.0 / 111_320.0; + lat += metres * course.to_radians().cos(); + lon += metres * course.to_radians().sin() / lat.to_radians().cos(); + time += Duration::seconds(1); + } + } + flags + } + /// Needs PostGIS: see `known_road_query`. Its own database, as it commits. #[tokio::test] #[ignore = "needs PostGIS at DATABASE_URL"] @@ -559,5 +593,31 @@ mod tests { "new road: {elsewhere:?}" ); rec.end_track().await.unwrap(); + + // One track that leaves the known road for a new one and rejoins: the + // flag has to change while driving, not just between tracks. + let start = t + lap * 4; + let legs = [(90.0, 20), (0.0, 15), (90.0, 10), (180.0, 15), (90.0, 15)]; + let mut detour = drive_legs(&mut rec, source, start, west_end, &legs).await; + detour.dedup(); + assert_eq!( + detour, + [Some(false), Some(true), Some(false)], + "known, new, known" + ); + + // Stopping for longer than track_idle ends it, without being told to. + assert!(rec.status.borrow().track.is_some(), "still driving"); + let stopped = Fix { + time: start + Duration::seconds(75) + Duration::seconds_f64(rec.track_idle + 1.0), + lat: west_end.1, + lon: west_end.0, + alt: None, + speed: Some(0.0), + course: None, + h_err: None, + }; + rec.fix(source, &stopped).await.unwrap(); + assert_eq!(*rec.status.borrow(), Status::default(), "idle ended it"); } }