Merge remote-tracking branch 'origin/pr/98' into review/pr-98

This commit is contained in:
Rafael Mendes committed 2026-11-05 15:53:09 -03:00
commit 24601325ca
2 files changed
+94 -1

No files matched your search

+7 -1
View File
@@ -177,8 +177,14 @@ fn reset(s: &mut Session, p: &J) -> Result<J> {
match &param {
Some(k) => {
n.params.remove(k);
n.bindings.remove(k);
n.functions.remove(k);
}
None => {
n.params.retain(|k, _| k == "graph" || k == "identifier");
n.bindings.clear();
n.functions.clear();
}
None => n.params.retain(|k, _| k == "graph" || k == "identifier"),
}
Ok(J::Null)
})
+87
View File
@@ -0,0 +1,87 @@
use materialcraft_engine::{
Session,
core::{NodeId, Value, function::FnGraph},
execute, formats, new_session,
};
use serde_json::{Value as Json, json};
fn run(s: &mut Session, id: &str, params: Json) -> Json {
execute(s, id, &params).unwrap()
}
fn setup() -> (Session, NodeId) {
let mut s = new_session();
s.preview_size = Some(32);
let source = run(&mut s, "graph.node.add", json!({"def": "uniform"}))["id"].as_u64().unwrap();
let levels = NodeId(run(&mut s, "graph.node.add", json!({"def": "levels", "connectFrom": {"node": source}}))["id"].as_u64().unwrap());
(s, levels)
}
fn drive(s: &mut Session, id: NodeId, param: &str, value: f32, function: bool) {
if function {
run(s, "graph.node.function", json!({"id": id.0, "param": param, "function": FnGraph::constant(Value::Float(value))}));
} else {
run(s, "graph.exposed.add", json!({"id": param, "default": value, "bind": {"node": id.0, "param": param}}));
}
}
fn pixel(s: &mut Session, id: NodeId) -> f32 {
let cooked = s.cook();
assert!(cooked.nodes[&id].error.is_none());
cooked.image(id, 0).unwrap().get_gray(0, 0, false)
}
fn check_single_reset(function: bool) {
let (mut s, id) = setup();
drive(&mut s, id, "in_low", 0.4, function);
drive(&mut s, id, "out_high", 0.8, !function);
let before = s.pkg.graphs.clone();
let driven = pixel(&mut s, id);
assert!(driven < 0.2);
run(&mut s, "graph.node.reset", json!({"id": id.0, "param": "in_low"}));
assert!((pixel(&mut s, id) - 0.4).abs() < 1e-5, "reset left the in_low driver active (function={function})");
let n = s.graph().unwrap().node(id).unwrap();
assert!(!n.bindings.contains_key("in_low") && !n.functions.contains_key("in_low"));
assert!(n.bindings.contains_key("out_high") || n.functions.contains_key("out_high"));
run(&mut s, "edit.undo", json!({}));
assert_eq!(s.pkg.graphs, before);
assert!((pixel(&mut s, id) - driven).abs() < 1e-5);
run(&mut s, "edit.redo", json!({}));
assert!((pixel(&mut s, id) - 0.4).abs() < 1e-5);
}
#[test]
fn resetting_one_bound_parameter_preserves_other_drivers() {
check_single_reset(false);
}
#[test]
fn resetting_one_function_parameter_preserves_other_drivers() {
check_single_reset(true);
}
#[test]
fn resetting_all_parameters_removes_drivers_and_restores_default_output() {
let (mut s, id) = setup();
drive(&mut s, id, "in_low", 0.4, false);
drive(&mut s, id, "out_high", 0.8, true);
run(&mut s, "graph.node.param", json!({"id": id.0, "param": "out_low", "value": 0.1}));
let before = s.pkg.graphs.clone();
let driven = pixel(&mut s, id);
assert!((driven - 0.5).abs() > 0.1);
let history = s.history().len();
run(&mut s, "graph.node.reset", json!({"id": id.0}));
assert!((pixel(&mut s, id) - 0.5).abs() < 1e-5, "reset left dynamic parameters active");
let n = s.graph().unwrap().node(id).unwrap();
assert!(n.params.is_empty() && n.bindings.is_empty() && n.functions.is_empty());
assert_eq!(s.history().len(), history + 1);
assert_eq!(s.graph().unwrap().exposed, before[0].exposed);
run(&mut s, "edit.undo", json!({}));
assert_eq!(s.pkg.graphs, before);
assert!((pixel(&mut s, id) - driven).abs() < 1e-5);
run(&mut s, "edit.redo", json!({}));
assert!((pixel(&mut s, id) - 0.5).abs() < 1e-5);
let mut reopened = Session::new(formats::load_package(&formats::save_package(&s.pkg).unwrap()).unwrap());
reopened.preview_size = Some(32);
assert!((pixel(&mut reopened, id) - 0.5).abs() < 1e-5);
}