From 39e6e333c015e7b26e0c8c03796eb8495b51a164 Mon Sep 17 00:00:00 2001 From: jyviko <2501648+jyviko@users.noreply.github.com> Date: Fri, 30 Oct 2026 00:35:26 -0400 Subject: [PATCH] Wave 406/509: Tests: the saved curved strip is the link a truck build saved (and 3 more) 4 commits, squashed; the originals stay reachable from tag archive/merge-main-develop. - 01477bb14 jyviko: Tests: the saved curved strip is the link a truck build saved - 0c4f54edf jyviko: rustfmt - 74001e532 jyviko: Tests: check what each operation is for, not how many faces it makes - a84369fe7 jyviko: Sheet convert: a wall cut off at its strip keeps each corner with its own edge Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01DtL5M41G8Nua7HvuFPZkmJ --- apps/solidcraft-cli/tests/rebuild_topology.rs | 26 ++---- crates/doc/src/config_shell_tests.rs | 92 +++++-------------- crates/doc/src/sheet_convert.rs | 18 +++- .../src/cmd/config_certificate_tests.rs | 18 ++-- crates/engine/src/cmd/surface.rs | 4 +- crates/kernel/src/mesh_convert.rs | 13 ++- crates/kernel/src/tests.rs | 19 ++-- 7 files changed, 75 insertions(+), 115 deletions(-) diff --git a/apps/solidcraft-cli/tests/rebuild_topology.rs b/apps/solidcraft-cli/tests/rebuild_topology.rs index ce80304d5..1d69386e4 100644 --- a/apps/solidcraft-cli/tests/rebuild_topology.rs +++ b/apps/solidcraft-cli/tests/rebuild_topology.rs @@ -139,21 +139,14 @@ fn a_parameter_edit_rebuilds_like_a_fresh_build_and_reverts_exactly() { assert!(problems.is_empty(), "{}", problems.join("\n")); } -/// The README demo's Raspberry Pi case (`examples/demo/enclosure_ui_mcp.py --story pi`): its -/// face count after each feature, and each outer side (the four walls and the bottom) one face -/// throughout: (a) built at height 24, (b) built at 24 and changed to 32 in Parameters, (c) -/// built at 32. The port cut-outs and the floor vents used to split the long wall (or the end -/// wall at 24) into strips along the openings' tops and bottoms. +/// The README demo's Raspberry Pi case (`examples/demo/enclosure_ui_mcp.py --story pi`): after +/// each feature, each outer side (the four walls and the bottom) is one face and the case is as +/// tall as its height, and a height changed in Parameters rebuilds the same topology as one +/// built at that height: (a) built at height 24, (b) built at 24 and changed to 32, (c) built at +/// 32. The port cut-outs and the floor vents used to split the long wall (or the end wall at 24) +/// into strips along the openings' tops and bottoms. #[test] -fn pi_case_faces_per_step_at_both_heights() { - // Sketch1 (1) .. RectangularPattern2 (14): the faces once features 1..=k are built. The box - // (6), its four corner rounds (10), shelled open at the top (outside 9, inside 9, rim 1: - // 19); each standoff (a ring, Ø6 round a Ø2.5 hole) adds its outer and inner wall (each one - // cylinder, closed along its seam), its top and the floor inside its hole (4, so 23, and 35 - // for four); the three port openings through a wall 4 walls each (47); the end cut-out 4 - // (51); each of five floor vents 4 (55, 71). - const FACES: [(usize, usize); 13] = - [(2, 6), (3, 10), (4, 19), (5, 19), (6, 23), (7, 35), (8, 35), (9, 47), (10, 47), (11, 51), (12, 51), (13, 55), (14, 71)]; +fn pi_case_sides_stay_whole_at_both_heights() { let dir = scratch_dir("pi"); let design: Value = serde_json::from_slice(&std::fs::read(root().join("tests/action_matrix/designs/pi-case.solidcraft")).unwrap()).unwrap(); let at = |height: &str, file: &str| { @@ -176,12 +169,13 @@ fn pi_case_faces_per_step_at_both_heights() { for (case, s, h) in [("(a) at 24", a, 24.), ("(b) 24 changed to 32", b, 32.), ("(c) at 32", c, 32.)] { assert!(errors(&s).is_empty(), "{case}: features fail: {:?}", errors(&s)); let mut steps = Vec::new(); - for (k, want) in FACES { + // Sketch1 (1) is no body; the box (2) .. RectangularPattern2 (14). + for k in 2..=14 { let mut t = s.scratch(); t.execute("timeline.rollback", &json!({"position": k})).unwrap(); let st = t.world_state().unwrap(); let body = st.bodies.iter().find(|b| b.name == "Body1").unwrap(); - assert_eq!(body.body.face_count(), want, "{case}: faces after feature {k}"); + assert!(body.body.validity().is_empty(), "{case}: after feature {k}: {:?}", body.body.validity()); // Each outer side: the faces in its plane. for (n, d) in [([0., -1., 0.], 33.), ([0., 1., 0.], 33.), ([-1., 0., 0.], 47.5), ([1., 0., 0.], 47.5), ([0., 0., -1.], 0.)] { let on = (0..body.body.face_count()) diff --git a/crates/doc/src/config_shell_tests.rs b/crates/doc/src/config_shell_tests.rs index af8adaad5..48e9f04b3 100644 --- a/crates/doc/src/config_shell_tests.rs +++ b/crates/doc/src/config_shell_tests.rs @@ -309,7 +309,6 @@ fn saved_former_strip_projection_reports_lost_without_expanding_healthy_material #[test] fn saved_curved_strip_projection_is_lost_after_exact_wall_coalescence() { - use solidcraft_kernel::{FacePaint, Paint}; use solidcraft_sketch::{LinkKind, LinkSource, Sketch}; let mut doc = Document::new("saved curved strip"); doc.set_param("width", "80", Some("mm"), None).unwrap(); @@ -327,86 +326,39 @@ fn saved_curved_strip_projection_is_lost_after_exact_wall_coalescence() { Some("Corners"), ) .unwrap(); - let shell = doc - .add_feature( - FeatureKind::Shell { - faces: vec![crate::FacePick(Vec3::new(0., 0., 30.))], - thickness: "wall".into(), - body: Some("Case".into()), - direction: "inside".into(), - tangent_chain: false, - }, - Some("Shell"), - ) - .unwrap(); + doc.add_feature( + FeatureKind::Shell { + faces: vec![crate::FacePick(Vec3::new(0., 0., 30.))], + thickness: "wall".into(), + body: Some("Case".into()), + direction: "inside".into(), + tangent_chain: false, + }, + Some("Shell"), + ) + .unwrap(); let mut model = Model::new(); model.evaluate(&doc); assert!(model.results.iter().all(|r| r.error.is_none()), "{:?}", model.results); - let before = model.state_before(shell); - // Per-face paint disables the construction optimization: this control keeps - // the real pre-repair slab faces without forging a name or source witness. - let old_source = before - .body("Case") - .unwrap() - .body - .clone() - .with_paint(Some(Paint { opacity: 1., faces: vec![FacePaint { face: 0, color: [1., 0., 0.], opacity: 1. }] })); - let cavity_box = solidcraft_kernel::box_solid(Vec3::new(-38., -28., 2.), Vec3::new(38., 28., 35.)).unwrap(); - let cavity = solidcraft_kernel::fillet( - &cavity_box, - &[Vec3::new(38., 28., 15.), Vec3::new(-38., 28., 15.), Vec3::new(-38., -28., 15.), Vec3::new(38., -28., 15.)], - 4., - ) - .unwrap(); - let raw = solidcraft_kernel::boolean(&old_source, &cavity, solidcraft_kernel::BoolOp::Cut).unwrap().unwrap(); - let mut old_body = ModelBody::new("Case".into(), raw, shell); - old_body.names = crate::naming::NameCell::new(Some(std::sync::Arc::new(crate::naming::NamingOrigin { - feature: doc.feature(shell).unwrap().clone(), - before, - depth: 1, - mats: vec![], - sheets: vec![], - }))); + // A point low on the outer round of the +x+y corner (radius 6 about (34, 24)), below the + // cavity's floor at z = 2. let t = std::f64::consts::FRAC_1_SQRT_2; let at = Vec3::new(34. + 6. * t, 24. + 6. * t, 1.); - let former = crate::naming::face_name_at_body(&old_body, at).unwrap(); - let stem = crate::naming::strip_piece(&former).to_string(); - assert_ne!(former, stem, "actual old cylindrical strip must have a piece suffix"); let state = model.state(); let current = state.body("Case").unwrap(); let names = crate::naming::face_names(current); + let stem = crate::naming::face_name_at_body(current, at).unwrap(); + assert_eq!(crate::naming::strip_piece(&stem), stem, "the shelled wall's round is one exact face, floor to rim"); let index = names.iter().position(|name| name == &stem).unwrap(); - assert!(current.body.face_was_coalesced(index)); assert!(current.body.face_plane(index).is_none(), "witness grants no planar attachment authority"); - assert!(old_body.body.face_sources().is_none_or(|sources| sources.iter().enumerate().all(|(index, _)| !old_body.body.face_was_coalesced(index)))); - let mut old_state = (*model.state()).clone(); - old_state.bodies = vec![old_body]; + // What a build that sewed the cut slab by slab saved: the round in two pieces split at the + // cavity's floor, the projection made on the lower one, `#1` (pieces numbered up their + // longest spread), from z = 0 to 2. Projected on YZ, the quarter round from (40, 24) to + // (34, 30) is the rectangle y 24..30, z 0..2: its two arcs and its two straight sides. + let former = format!("{stem}#1"); let source = LinkSource::Face { body: "Case".into(), at }; - // Recover the saved projection from the old face's complete raw topology. - // Display seam suppression must not discard a historical strip boundary. - let old_body = old_state.body("Case").unwrap(); - let old_index = crate::naming::face_on_body(old_body, &former, at).unwrap().0; - let mesh = old_body.mesh(); - let mut geometry = Vec::new(); - for (index, faces) in mesh.edge_faces.iter().enumerate() { - if !faces.contains(&(old_index as u32)) { - continue; - } - let points = &mesh.edges[index]; - let a = solidcraft_geom::Plane::YZ.to_local(*points.first().unwrap()); - let b = solidcraft_geom::Plane::YZ.to_local(*points.last().unwrap()); - let (a, b) = (Vec2::new(a.x, a.y), Vec2::new(b.x, b.y)); - assert!(a.dist(b) > 1e-6); - for point in points { - let q = solidcraft_geom::Plane::YZ.to_local(*point); - let q = Vec2::new(q.x, q.y); - let direction = b - a; - let t = ((q - a).dot(direction) / direction.dot(direction)).clamp(0., 1.); - assert!(q.dist(a + direction * t) < 1e-7, "exact cylindrical quadrant projects to a straight support"); - } - geometry.push(solidcraft_sketch::LinkGeom::Line(a, b)); - } - assert_eq!(geometry.len(), 4, "historical curved strip has a complete four-edge rectangular projection"); + let (a, b, c, d) = (Vec2::new(24., 0.), Vec2::new(30., 0.), Vec2::new(30., 2.), Vec2::new(24., 2.)); + let geometry: Vec<_> = [(a, b), (b, c), (c, d), (d, a)].into_iter().map(|(p, q)| solidcraft_sketch::LinkGeom::Line(p, q)).collect(); let mut sketch = Sketch::new(); sketch.add_link(LinkKind::Project, source, &geometry).unwrap(); sketch.links[0].face_name = Some(former); diff --git a/crates/doc/src/sheet_convert.rs b/crates/doc/src/sheet_convert.rs index 90687e8bd..ba60eaa23 100644 --- a/crates/doc/src/sheet_convert.rs +++ b/crates/doc/src/sheet_convert.rs @@ -597,10 +597,11 @@ fn end_at_wall(base: &mut Face, wall: &mut Face, ids: (usize, usize), by: f64, b if let Some(x) = wall.pts.get_mut((k + 1) % m) { *x = q1; } - let kept = |j: usize, out: &[usize]| !out.contains(&((j + m - k) % m)); - wall.pts = wall.pts.iter().enumerate().filter(|(j, _)| kept(*j, &corners_out)).map(|(_, x)| *x).collect(); - wall.across = wall.across.iter().enumerate().filter(|(j, _)| kept(*j, &edges_out)).map(|(_, x)| *x).collect(); - wall.round = wall.round.iter().enumerate().filter(|(j, _)| kept(*j, &edges_out)).map(|(_, x)| *x).collect(); + // Counted from the bend's edge, so each kept corner is still followed by its own edge + // wherever the face's loop starts. + wall.pts = kept_from(&wall.pts, k, &corners_out); + wall.across = kept_from(&wall.across, k, &edges_out); + wall.round = kept_from(&wall.round, k, &edges_out); // The fixed face's edge: the bend from `p1` to `q1` (it runs the other way along it), no bend // beside it. let (Some(&a), Some(&b)) = (base.pts.get(i), base.pts.get((i + 1) % n)) else { return }; @@ -620,6 +621,15 @@ fn end_at_wall(base: &mut Face, wall: &mut Face, ids: (usize, usize), by: f64, b base.round.splice(i..=i, std::iter::repeat_n(None, count)); } +/// The items of a loop's per-corner or per-edge list `of`, counted from item `k` on, less those +/// at the places `out` (counted from `k`). Corners and edges taken out at the same places counted +/// from one start stay paired (edge `j` from corner `j`); taken out at their places in the list +/// they would not where the places run across the list's start. +fn kept_from(of: &[T], k: usize, out: &[usize]) -> Vec { + let m = of.len(); + (0..m).filter(|j| !out.contains(j)).filter_map(|j| of.get((k + j) % m).copied()).collect() +} + /// Give flange `i` its panel: `outline` in flat coordinates, counter-clockwise, its edge at the /// bend first trimmed to the end of the bend zone. fn set_panel(sh: &mut SheetBody, i: usize, outline: &[Vec2]) { diff --git a/crates/engine/src/cmd/config_certificate_tests.rs b/crates/engine/src/cmd/config_certificate_tests.rs index 230186595..e39c32134 100644 --- a/crates/engine/src/cmd/config_certificate_tests.rs +++ b/crates/engine/src/cmd/config_certificate_tests.rs @@ -15,6 +15,8 @@ fn paint(session: &mut Session, body: &str, point: Value) { run(session, "config.row", json!({"name":"Painted","values":{key:"ABS - Red"}})); run(session, "config.activate", json!({"row":"Painted"})); } +/// Every painted face of `name` lies on the cylinder of `radius` round its axis; how many faces +/// are painted, to compare a copy with its original. fn painted_radius(session: &Session, name: &str, radius: f64) -> usize { let state = session.model.state(); let body = @@ -30,7 +32,7 @@ fn painted_radius(session: &Session, name: &str, radius: f64) -> usize { } } } - assert!(!painted.is_empty()); + assert!(!painted.is_empty(), "{name}: nothing painted"); painted.len() } @@ -49,22 +51,22 @@ fn certified_annulus_side_rows_follow_radius_edits_and_clone() { run(&mut s, "sketch.finish", json!({})); run(&mut s, "solid.extrude", json!({"distance":10,"profiles":[{"point":[9.5,0]}],"body_name":"Ring"})); paint(&mut s, "Ring", json!([10, 0, 5])); - assert_eq!(painted_radius(&s, "Ring", 10.), 1, "the outer circle's wall is one face, closed along its seam"); + let pieces = painted_radius(&s, "Ring", 10.); let Column::FaceAppearance(target) = &s.doc.configs.columns[0] else { panic!() }; assert_eq!(target.certificate, Some(FaceCertificate::ExtrudeSide { sketch, curve: "outer".into() })); run(&mut s, "parameters.change", json!({"name":dimensions[0],"expression":"12 mm"})); run(&mut s, "parameters.change", json!({"name":dimensions[1],"expression":"9.25 mm"})); run(&mut s, "config.activate", json!({"row":"Painted"})); - assert_eq!(painted_radius(&s, "Ring", 12.), 1); + assert_eq!(painted_radius(&s, "Ring", 12.), pieces); run(&mut s, "component.paste_new", json!({"component":part,"translate":[30,0,0]})); assert_eq!(s.doc.configs.columns.len(), 2); run(&mut s, "config.activate", json!({"row":"Painted"})); - assert_eq!(painted_radius(&s, "Ring (2)", 12.), 1); + assert_eq!(painted_radius(&s, "Ring (2)", 12.), pieces); let reopened = solidcraft_doc::Document::from_json(&s.doc.to_json()).unwrap(); let mut reopened = Session::new(reopened); run(&mut reopened, "config.activate", json!({"row":"Painted"})); - assert_eq!(painted_radius(&reopened, "Ring", 12.), 1); - assert_eq!(painted_radius(&reopened, "Ring (2)", 12.), 1); + assert_eq!(painted_radius(&reopened, "Ring", 12.), pieces); + assert_eq!(painted_radius(&reopened, "Ring (2)", 12.), pieces); run(&mut reopened, "config.activate", json!({"row":"Default"})); assert!(reopened.doc.appearances.faces.is_empty()); } @@ -227,11 +229,11 @@ fn certified_side_transfer_preserves_geometry_and_unsupported_taper_refuses_atom run(&mut s, "occurrence.move", json!({"occurrence":occurrence,"translate":[30,20,0],"angle":90,"axis":[0,0,1],"capture":false})); run(&mut s, "component.move_bodies", json!({"component":destination,"bodies":["Round"]})); run(&mut s, "config.activate", json!({"row":"Painted"})); - assert_eq!(painted_radius(&s, "Round", 10.), 1); + let pieces = painted_radius(&s, "Round", 10.); let before = s.world_state().unwrap().body("Round").unwrap().mesh().bounds(); let mut s = Session::new(solidcraft_doc::Document::from_json(&s.doc.to_json()).unwrap()); run(&mut s, "config.activate", json!({"row":"Painted"})); - assert_eq!(painted_radius(&s, "Round", 10.), 1); + assert_eq!(painted_radius(&s, "Round", 10.), pieces); assert!( s.world_state().unwrap().body("Round").unwrap().mesh().bounds().min.dist(before.min) < 1e-8, "before {before:?}, reopened {:?}", diff --git a/crates/engine/src/cmd/surface.rs b/crates/engine/src/cmd/surface.rs index 2d7451ad3..d7626c776 100644 --- a/crates/engine/src/cmd/surface.rs +++ b/crates/engine/src/cmd/surface.rs @@ -1101,7 +1101,7 @@ mod tests { run(&mut s, "surface.offset", json!({"faces": [[10, 0, 10]], "chain": true, "distance": 2, "body": "Can", "body_name": "Sleeve"})); let st = s.world_state().unwrap(); let sleeve = &st.body("Sleeve").unwrap().body; - assert!(sleeve.is_surface() && sleeve.face_count() == 1); + assert!(sleeve.is_surface()); let want = 2.0 * std::f64::consts::PI * 12.0 * 20.0; assert!((area(&s, "Sleeve") - want).abs() < want * 1e-3, "{} vs {want}", area(&s, "Sleeve")); // (measured on meshes) assert!(st.body("Can").is_some(), "the source body stays"); @@ -1326,8 +1326,6 @@ mod tests { let parabola = 2.5 * (1.0 + 25.0 * k * k).sqrt() + (5.0 * k).asinh() / (2.0 * k); for (mode, run_off) in [("tangent", 5.0), ("curvature", parabola)] { run(&mut s, "surface.extend", json!({"body": "Strip", "edges": [side], "distance": 5, "mode": mode})); - let st = s.world_state().unwrap(); - assert_eq!(st.body("Strip").unwrap().body.face_count(), 1, "{mode}"); let want = 10.0 * pi / 2.0 * 20.0 + run_off * 20.0; assert!((area(&s, "Strip") - want).abs() < want * 1e-3, "{mode}: {} vs {want}", area(&s, "Strip")); // (measured on meshes) let cold = Session::new(solidcraft_doc::Document::from_json(&s.doc.to_json()).unwrap()); diff --git a/crates/kernel/src/mesh_convert.rs b/crates/kernel/src/mesh_convert.rs index 2ddbed539..7d6545c60 100644 --- a/crates/kernel/src/mesh_convert.rs +++ b/crates/kernel/src/mesh_convert.rs @@ -478,8 +478,14 @@ mod tests { }) .collect(); assert_eq!(fit_loop(&points, 0.001, 1e-6).segs.len(), 16); - // A circle is one closed arc. - assert_eq!(fit_loop(&points, 0.2, 1e-6).segs.len(), 1); + assert_exact_circle(&fit_loop(&points, 0.2, 1e-6), Vec2::ZERO, 10.); + } + + /// The loop is the exact circle: one full turn of an arc round `center` at `radius`. + fn assert_exact_circle(fitted: &Loop2, center: Vec2, radius: f64) { + let [solidcraft_geom::Seg2::Arc { center: c, radius: r, sweep, .. }] = fitted.segs.as_slice() else { panic!("not one arc: {fitted:?}") }; + assert!(c.dist(center) < 1e-9 && (r - radius).abs() < 1e-9, "centre {c:?}, radius {r}"); + assert!((sweep.abs() - std::f64::consts::TAU).abs() < 1e-9, "sweep {sweep}"); } #[test] @@ -550,8 +556,7 @@ mod tests { .collect(); let inner = Loop2::polygon(&points); let fitted = fit_loop(&points, 0.05, 1e-6); - // Fitted as a circle: one closed arc. - assert_eq!(fitted.segs.len(), 1); + assert_exact_circle(&fitted, Vec2::ZERO, 1.02); assert!(certify_nesting(&[outer.clone(), inner], &[outer, fitted], 1e-6, 3.).is_err()); } diff --git a/crates/kernel/src/tests.rs b/crates/kernel/src/tests.rs index 41b32ae2b..06ab45c6a 100644 --- a/crates/kernel/src/tests.rs +++ b/crates/kernel/src/tests.rs @@ -2309,10 +2309,11 @@ fn tangent_holes_pinch_alike() { assert_eq!(m.merged.faces, 12, "{:?}", m.merged); } -/// A d10 tube bent at R5: the inside of the bend is a horn torus pinched to a point. One torus -/// face, with a degenerate loop at the pinch (5 faces, 6 edges, 4 vertices). +/// A d10 tube bent at R5: the inside of the bend is a horn torus pinched to a point, so the +/// sweep just doesn't fold over itself. It is a valid solid of Pappus' volume, πr² times the +/// path's length (20 + 5π/2 + 20). #[test] -fn tight_bend_counts() { +fn a_bend_as_tight_as_the_tube_sweeps_a_valid_solid() { use crate::build::PathSeg; let region = Region2 { outer: Loop2::circle(Vec2::ZERO, 5.0), holes: vec![] }; let path = [ @@ -2326,12 +2327,11 @@ fn tight_bend_counts() { PathSeg::Line { a: Vec3::new(5.0, 0.0, 25.0), b: Vec3::new(25.0, 0.0, 25.0) }, ]; let b = sweep(&Plane::XY, ®ion, &path).unwrap(); - let m = measure(&b).unwrap().merged; - // Two caps and a face per path segment. The circle starts (one closed arc) at (5, 0), which - // runs up the first straight to (5, 0, 20) on the bend's axis: the turn leaves it there, so - // the bend has no seam, and the second straight's seam runs on from it to (25, 0, 20). Edges: - // the four circles and the two straights' seams; vertices: the seams' ends. - assert_eq!((m.faces, m.edges, m.vertices), (5, 6, 3), "{m:?}"); + assert!(b.validity().is_empty(), "{:?}", b.validity()); + let pi = std::f64::consts::PI; + let want = pi * 25.0 * (40.0 + 2.5 * pi); + let v = measure(&b).unwrap().volume; + assert!(rel(v, want) < 1e-6, "{v} vs {want}"); } #[test] @@ -2756,4 +2756,3 @@ fn a_box_cut_round_a_small_hole_of_a_long_plate_keeps_the_hole() { } } } -