From 094f60b2fb6a29bc420fff27f03ac146a7babbff Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Thu, 3 Dec 2026 20:36:08 -0800 Subject: [PATCH] Tests: DWG opens through the DXF Import Options dialog and the CLI worker - The DXF Import Options dialog test opens a DWG, confirms it at 1:1 in model space and places it. - `vectorcraft-cli info` reads a DWG as the DXF it was written from, and five damaged copies (cut short, overwritten, bytes flipped) end in an error that names DWG or open with warnings, with the process exiting normally. - The engine's DWG and DXF test modules are listed in order. --- Cargo.lock | 2 ++ apps/vectorcraft-cli/Cargo.toml | 2 ++ apps/vectorcraft-cli/tests/it/fileio.rs | 37 ++++++++++++++++++++++++ crates/engine/src/cmd/fileio.rs | 4 +-- crates/ui-egui/Cargo.toml | 2 ++ crates/ui-egui/src/dialogs/dxf_import.rs | 19 ++++++++++++ 6 files changed, 64 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 227bcc9a37..b6bdcc0afe 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -8699,6 +8699,7 @@ dependencies = [ name = "vectorcraft-cli" version = "0.25.1" dependencies = [ + "craft-dwg", "log", "objc2-core-foundation", "objc2-core-text", @@ -9235,6 +9236,7 @@ dependencies = [ "bytemuck", "chrono", "craft", + "craft-dwg", "craft-image-codecs", "egui", "egui_extras", diff --git a/apps/vectorcraft-cli/Cargo.toml b/apps/vectorcraft-cli/Cargo.toml index 25cffe599e..f3115d0779 100644 --- a/apps/vectorcraft-cli/Cargo.toml +++ b/apps/vectorcraft-cli/Cargo.toml @@ -47,6 +47,8 @@ objc2-core-text = { version = "0.3", default-features = false, features = ["std" [dev-dependencies] tempfile = "3" vectorcraft-testkit = { workspace = true } +# DWG files for the `info` test, written from DXF by craft-dwg. +craft-dwg = { workspace = true } serde_json = { workspace = true } # The files of other crates that this crate compiles or its tests read, for CI's test selection. diff --git a/apps/vectorcraft-cli/tests/it/fileio.rs b/apps/vectorcraft-cli/tests/it/fileio.rs index 9593d2630e..0c2ee7aaef 100644 --- a/apps/vectorcraft-cli/tests/it/fileio.rs +++ b/apps/vectorcraft-cli/tests/it/fileio.rs @@ -378,3 +378,40 @@ fn convert_and_run_write_ait_templates() { assert!(!one.status.success()); assert!(String::from_utf8_lossy(&one.stderr).contains("every artboard"), "{}", String::from_utf8_lossy(&one.stderr)); } + +/// A DWG drawing opens through a worker copy of the program, so a damaged one ends in an error from +/// `info`, never in a crash. craft-dwg reads and writes DWG everywhere but wasm32 and FreeBSD on +/// aarch64. +#[cfg(not(any(target_arch = "wasm32", all(target_os = "freebsd", target_arch = "aarch64"))))] +#[test] +fn info_reads_a_dwg_and_a_damaged_one_is_an_error_not_a_crash() { + let dxf = tmp("plan.dxf"); + make(&dxf, 120, 80); + craft_dwg::allow_in_process(); + let dwg = craft_dwg::dxf_to_dwg_as(&std::fs::read(&dxf).unwrap(), "AC1018").unwrap(); + let good = tmp("plan.dwg"); + std::fs::write(&good, &dwg).unwrap(); + // The DWG comes in as the DXF it was written from. + let (v, from) = (info(&good), info(&dxf)); + assert!(v["kinds"]["Path"].as_u64().is_some_and(|n| n > 0), "{v}"); + assert_eq!((&v["kinds"], &v["artboards"][0]["rect"]), (&from["kinds"], &from["artboards"][0]["rect"]), "{v}\n{from}"); + + let n = dwg.len(); + let mut damaged = vec![dwg[..n / 3].to_vec(), dwg[..2 * n / 3].to_vec(), dwg[..16].to_vec()]; + let mut overwritten = dwg.clone(); + overwritten[n / 2..(n / 2 + 64).min(n)].fill(0xFF); + damaged.push(overwritten); + let mut flipped = dwg.clone(); + flipped.iter_mut().skip(32).step_by(97).for_each(|b| *b ^= 0xA5); + damaged.push(flipped); + for (i, bytes) in damaged.iter().enumerate() { + let path = tmp(&format!("damaged-{i}.dwg")); + std::fs::write(&path, bytes).unwrap(); + let out = Command::new(crate::bin()).args(["info", &path]).output().unwrap(); + // A process ended by a signal has no exit code. + assert!(out.status.code().is_some(), "damaged file {i}: {:?}", out.status); + if !out.status.success() { + assert!(String::from_utf8_lossy(&out.stderr).contains("DWG"), "damaged file {i}: {}", String::from_utf8_lossy(&out.stderr)); + } + } +} diff --git a/crates/engine/src/cmd/fileio.rs b/crates/engine/src/cmd/fileio.rs index 9d89de2c3c..1a24bf623b 100644 --- a/crates/engine/src/cmd/fileio.rs +++ b/crates/engine/src/cmd/fileio.rs @@ -246,11 +246,11 @@ mod tests_svgimport; #[cfg(test)] mod tests_eps; -#[cfg(test)] -mod tests_dxfimport; #[cfg(test)] mod tests_dwgimport; #[cfg(test)] +mod tests_dxfimport; +#[cfg(test)] mod tests_hpgl; #[cfg(test)] mod tests_metafile; diff --git a/crates/ui-egui/Cargo.toml b/crates/ui-egui/Cargo.toml index 49fad8d56f..ac7b1fe1a6 100644 --- a/crates/ui-egui/Cargo.toml +++ b/crates/ui-egui/Cargo.toml @@ -95,6 +95,8 @@ web-time = "1" rustix = { workspace = true, features = ["std", "process"] } [dev-dependencies] +# DWG files for the DXF Import Options tests, written from DXF by craft-dwg. +craft-dwg = { workspace = true } # `Fixture` and the stories sit behind craft-ui's `testing` feature. craft = { workspace = true, features = ["ui-testing"] } egui_kittest = { workspace = true, features = ["wgpu", "snapshot"] } diff --git a/crates/ui-egui/src/dialogs/dxf_import.rs b/crates/ui-egui/src/dialogs/dxf_import.rs index a493f3474b..0a35c97b32 100644 --- a/crates/ui-egui/src/dialogs/dxf_import.rs +++ b/crates/ui-egui/src/dialogs/dxf_import.rs @@ -616,6 +616,25 @@ mod tests { assert_eq!(w, 100.0, "1 pt = 1 unit"); } + /// craft-dwg reads and writes DWG everywhere but wasm32 and FreeBSD on aarch64. + #[cfg(not(any(target_arch = "wasm32", all(target_os = "freebsd", target_arch = "aarch64"))))] + #[test] + fn a_dwg_drawing_opens_and_places_through_the_dialog() { + vectorcraft_engine::dwg::allow_in_process(); + let dwg = craft_dwg::dxf_to_dwg_as(&drawing(), "AC1018").unwrap(); + let mut app = host::app(); + io::open_bytes(&mut app, "plan.dwg", &dwg, None).unwrap(); + let f = host::fields(&app, KIND); + assert_eq!((&f["mode"], &f["layout"], &f["unit"]), (&json!("open"), &json!("Model"), &json!("Millimeters"))); + host::confirm(&mut app, KIND).unwrap(); + assert!(!host::is_open(&app, KIND)); + let w = app.session.doc().unwrap().doc.artboards[0].rect.width(); + assert!((w - 100.0 * Unit::Millimeters.points()).abs() < 1e-6, "the model space line at 1:1: {w}"); + app.run("file.new", json!({"width": 600, "height": 400})).unwrap(); + crate::place::run(&mut app, &json!({"name": "plan.dwg", "dataBase64": vectorcraft_format::base64_encode(&dwg)})).unwrap(); + assert_eq!(host::fields(&app, KIND)["mode"], "place"); + } + #[test] fn placing_a_drawing_asks_then_places_one_group() { let mut app = host::app();