From 9ec2f5c4bd964a9e8924d95fd5ec9e59bc717751 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Wed, 2 Dec 2026 21:26:18 -0800 Subject: [PATCH] Tests: cover three undo group paths no test reached - A document added outside a command while edit.undoGroup.begin is open joins the group: its two edits are one "Job" step, and one undo takes both back (Session::add_document). - Canceling a group rewrites a batch's journal entry: a dropped step that left another document in front becomes document.activate with that index, so the edit made after the group replays in the same document (keep_batch_steps). - Renaming a graph design to its own name succeeds and records no undo step, and the design list stays the same. Each test fails with the covered lines removed. --- crates/engine/src/cmd/graph/tests.rs | 5 +++ crates/engine/src/tests/journal.rs | 58 ++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+) diff --git a/crates/engine/src/cmd/graph/tests.rs b/crates/engine/src/cmd/graph/tests.rs index b41415ef02..95aa571b48 100644 --- a/crates/engine/src/cmd/graph/tests.rs +++ b/crates/engine/src/cmd/graph/tests.rs @@ -6891,6 +6891,11 @@ fn new_design_rename_delete_and_select_unused() { assert_eq!(spec(&s, id).series_markers, [Some("Dot".to_string()), Some("Dot".to_string())]); assert!(s.execute("graph.design", &json!({"rename": "Dot", "to": "New Design 2"})).is_err(), "taken"); assert!(s.execute("graph.design", &json!({"rename": "Nope", "to": "X"})).is_err()); + // Renaming a design to its own name succeeds and records no undo step. + let (steps, designs) = (s.doc().unwrap().history.undo.len(), s.execute("graph.design", &json!({})).unwrap()["designs"].clone()); + assert_eq!(s.execute("graph.design", &json!({"rename": "Dot", "to": "Dot"})).unwrap()["name"], "Dot"); + assert_eq!(s.doc().unwrap().history.undo.len(), steps); + assert_eq!(s.execute("graph.design", &json!({})).unwrap()["designs"], designs); // Delete several at once. s.execute("graph.design", &json!({"delete": ["Dot", "New Design 2"]})).unwrap(); assert_eq!(s.execute("graph.design", &json!({})).unwrap()["designs"], json!([])); diff --git a/crates/engine/src/tests/journal.rs b/crates/engine/src/tests/journal.rs index 0b35b2f772..35f86fa582 100644 --- a/crates/engine/src/tests/journal.rs +++ b/crates/engine/src/tests/journal.rs @@ -966,6 +966,45 @@ fn canceling_an_undo_group_keeps_a_batchs_steps_in_other_documents() { assert_eq!(r.active_index(), s.active_index()); } +/// A batch step in the canceled group that leaves another document in front (switching to the +/// grouped document measures its type again in its own composite font) is dropped from the batch's +/// journal entry and replaced by that switch, so the edit made after the group replays in the +/// document it was made in. +#[test] +fn canceling_an_undo_group_keeps_the_switch_of_a_dropped_batch_step() { + let mut s = Session::new(); + let fonts = |size: i32| { + let parts = json!({"kanji": {"font": "Source Sans 3"}, "roman": {"font": "JetBrains Mono", "size": size}}); + json!({"fonts": [{"name": "CF Undo Group", "parts": parts}]}) + }; + s.execute("file.new", &json!({"created": null})).unwrap(); + s.execute("type.compositeFonts.set", &fonts(50)).unwrap(); + s.execute("text.create", &json!({"x": 10, "y": 50, "text": "Ab", "size": 20, "font": "CF Undo Group"})).unwrap(); + s.execute("file.new", &json!({"created": null})).unwrap(); + s.execute("type.compositeFonts.set", &fonts(75)).unwrap(); + let objects = |s: &Session| s.documents().iter().map(|d| d.doc.node_count()).collect::>(); + let before = objects(&s); + let activate = |index: usize| json!({"command": "document.activate", "params": {"index": index}}); + let steps = json!([rect_step(10), activate(0)]); + for (id, p) in [ + ("document.activate", json!({"index": 0})), + ("edit.undoGroup.begin", json!({})), + ("shape.rectangle", rect_step(50)["params"].clone()), + ("document.activate", json!({"index": 1})), + ("command.batch", json!({ "commands": steps })), + ("edit.undoGroup.end", json!({"cancel": true})), + ("shape.rectangle", rect_step(30)["params"].clone()), + ] { + s.execute(id, &p).unwrap(); + } + assert_eq!(objects(&s), vec![before[0] + 1, before[1] + 1]); + let (_, batch) = s.journal.iter().find(|(id, _)| id == "command.batch").unwrap(); + assert_eq!(batch["commands"], json!([steps[0], activate(0)])); + let r = replayed(&s); + assert_eq!(documents(&r), documents(&s)); + assert_eq!(r.active_index(), s.active_index()); +} + /// The edits made in an undo group (a scrubbed numeric field, #400) are one undo step, journaled /// one by one; a cancelled group undoes them and leaves the journal as it was. #[test] @@ -1074,6 +1113,25 @@ fn an_undo_group_begun_by_command_opens_in_each_document_created_before_its_end( assert!(s.doc().unwrap().undo_group.is_none()); } +/// Between `edit.undoGroup.begin` and `edit.undoGroup.end`, a document added outside a command (a +/// template the app opens itself) opens an undo group with the begun group's name too: its edits are +/// one undo step there. +#[test] +fn an_undo_group_begun_by_command_opens_in_a_document_added_outside_a_command() { + let mut s = Session::new(); + let rect = json!({"x": 10, "y": 10, "width": 100, "height": 50}); + let labels = |s: &Session| s.doc().unwrap().history.undo.iter().map(|e| e.label.clone()).collect::>(); + let objects = |s: &Session| s.doc().unwrap().doc.layers[0].children().map_or(0, |c| c.len()); + s.execute("edit.undoGroup.begin", &json!({"label": "Job"})).unwrap(); + s.add_document(vectorcraft_doc::Document::new(400.0, 300.0), None); + s.execute("shape.rectangle", &rect).unwrap(); + s.execute("shape.ellipse", &rect).unwrap(); + s.execute("edit.undoGroup.end", &json!({})).unwrap(); + assert_eq!(labels(&s), ["Job"]); + s.execute("edit.undo", &json!({})).unwrap(); + assert_eq!(objects(&s), 0, "one undo takes back both edits"); +} + /// Between `edit.undoGroup.begin` and `edit.undoGroup.end`, a command that creates a document and /// edits it (a `command.batch`) keeps its edits as an undo step of its own in that document, and the /// edits after it are the group's step. A cancel undoes only those, keeps the document and the