Merge pull request #2386 from gmrmad1/fix/2181-batch-step-refusals

Batch: a refused step names its step and keeps its kind (#2181)
This commit is contained in:
BFlat authored and GitHub committed 2026-11-17 18:16:16 +07:00
commit f16f620f95
2 files changed
+59

No files matched your search

+57
View File
@@ -652,3 +652,60 @@ fn a_continue_batch_reports_runtime_errors_and_runs_later_steps() {
assert_eq!(results[6]["ok"], true);
assert_eq!(s.doc().unwrap().node(s.page().unwrap()).unwrap().name, "Done");
}
#[test]
fn a_refused_step_names_its_step_and_keeps_its_kind() {
let mut s = Session::new();
let comp = id(&s.execute("node.create", &json!({"type": "component", "name": "Button", "width": 100, "height": 40})).unwrap());
let inst = id(&s.execute("instance.create", &json!({"component": comp})).unwrap());
let before = s.doc().unwrap().clone();
let undo = s.undo_depth();
let result = s.execute(
"command.batch",
&json!({"commands": [
{"command": "node.create", "params": {"type": "rectangle", "parent": inst, "width": 10, "height": 10}}
]}),
);
match result {
Err(EngineError::Disabled(cmd, msg)) => {
assert_eq!(cmd, "node.create");
assert!(msg.starts_with("step 0: "), "{msg}");
assert!(msg.contains("can't be added"), "{msg}");
}
other => panic!("expected Disabled, got {other:?}"),
}
assert_eq!(s.doc().unwrap(), &before, "the batch rolled back");
assert_eq!(s.undo_depth(), undo, "no undo step for the refused batch");
// Outside a batch the same refusal keeps its kind and says no step.
match s.execute("node.create", &json!({"type": "rectangle", "parent": inst, "width": 10, "height": 10})) {
Err(EngineError::Disabled(_, msg)) => assert!(!msg.contains("step "), "{msg}"),
other => panic!("expected Disabled, got {other:?}"),
}
}
#[test]
fn an_internal_step_says_which_step_without_changing_kind() {
use crate::derived;
let mut s = Session::new();
let r = id(&s.execute("node.create", &json!({"type": "rectangle", "width": 10, "height": 10})).unwrap());
let before = s.doc().unwrap().clone();
derived::PANIC.with(|p| p.set(true));
let result = s.execute(
"command.batch",
&json!({"commands": [
{"command": "node.set", "params": {"ids": [r], "props": {"name": "Renamed"}}}
]}),
);
derived::PANIC.with(|p| p.set(false));
match result {
Err(EngineError::Internal { cmd, msg }) => {
assert_eq!(cmd, "node.set");
assert!(msg.starts_with("step 0: "), "{msg}");
}
other => panic!("expected Internal, got {other:?}"),
}
assert_eq!(s.doc().unwrap(), &before, "the panicking step is rolled back");
// Kinds without a message are left as they are.
assert!(matches!(EngineError::NoNode(figcraft_doc::NodeId(7)).at_step(2), EngineError::NoNode(_)));
assert!(matches!(EngineError::Waiting.at_step(2), EngineError::Waiting));
}
+2
View File
@@ -43,6 +43,8 @@ impl EngineError {
match self {
EngineError::BadParams { cmd, msg } => EngineError::BadParams { cmd, msg: format!("step {step}: {msg}") },
EngineError::Other(msg) => EngineError::Other(format!("step {step}: {msg}")),
EngineError::Disabled(cmd, msg) => EngineError::Disabled(cmd, format!("step {step}: {msg}")),
EngineError::Internal { cmd, msg } => EngineError::Internal { cmd, msg: format!("step {step}: {msg}") },
e => e,
}
}