@@ -3,7 +3,9 @@ use serde_json::json;
33use std:: ops:: Deref ;
44use std:: sync:: Arc ;
55use tokio:: runtime:: Builder ;
6- use zen_engine:: { Decision , DecisionGraphValidationError , EvaluationError , EvaluationOptions } ;
6+ use zen_engine:: {
7+ Decision , DecisionGraphValidationError , EvaluationError , EvaluationOptions , Variable ,
8+ } ;
79
810mod support;
911
@@ -244,3 +246,163 @@ async fn decision_table_missing_cell_key_is_treated_as_empty() {
244246 . unwrap ( ) ;
245247 assert_eq ! ( traced. result, result. result) ;
246248}
249+
250+ #[ tokio:: test]
251+ #[ cfg_attr( miri, ignore) ]
252+ async fn node_handlers_do_not_mutate_aliased_inputs ( ) {
253+ let content = serde_json:: from_value ( json ! ( {
254+ "nodes" : [
255+ { "id" : "in" , "name" : "in" , "type" : "inputNode" , "content" : { } } ,
256+ {
257+ "id" : "e1" , "name" : "e1" , "type" : "expressionNode" ,
258+ "content" : {
259+ "passThrough" : true ,
260+ "expressions" : [ { "id" : "x1" , "key" : "first" , "value" : "a + 1" } ]
261+ }
262+ } ,
263+ {
264+ "id" : "sw" , "name" : "sw" , "type" : "switchNode" ,
265+ "content" : {
266+ "hitPolicy" : "first" ,
267+ "statements" : [ { "id" : "s1" , "condition" : "" } ]
268+ }
269+ } ,
270+ {
271+ "id" : "e2" , "name" : "e2" , "type" : "expressionNode" ,
272+ "content" : {
273+ "passThrough" : true ,
274+ "expressions" : [ { "id" : "x2" , "key" : "second" , "value" : "first + 1" } ]
275+ }
276+ } ,
277+ {
278+ "id" : "dt" , "name" : "dt" , "type" : "decisionTableNode" ,
279+ "content" : {
280+ "passThrough" : true ,
281+ "hitPolicy" : "first" ,
282+ "inputs" : [ { "id" : "i1" , "name" : "A" , "field" : "a" } ] ,
283+ "outputs" : [ { "id" : "o1" , "name" : "Result" , "field" : "dtResult" } ] ,
284+ "rules" : [ { "_id" : "r1" , "i1" : "" , "o1" : "'hit'" } ]
285+ }
286+ } ,
287+ { "id" : "out" , "name" : "out" , "type" : "outputNode" , "content" : { } }
288+ ] ,
289+ "edges" : [
290+ { "id" : "ed1" , "sourceId" : "in" , "targetId" : "e1" } ,
291+ { "id" : "ed2" , "sourceId" : "e1" , "targetId" : "sw" } ,
292+ { "id" : "ed3" , "sourceId" : "sw" , "targetId" : "e2" , "sourceHandle" : "s1" } ,
293+ { "id" : "ed4" , "sourceId" : "e2" , "targetId" : "dt" } ,
294+ { "id" : "ed5" , "sourceId" : "dt" , "targetId" : "out" }
295+ ]
296+ } ) )
297+ . unwrap ( ) ;
298+ let decision = Decision :: from ( Arc :: new ( content) ) ;
299+
300+ let input: Variable = json ! ( { "a" : 1 , "nested" : { "k" : "v" } } ) . into ( ) ;
301+ let snapshot = input. to_value ( ) ;
302+
303+ let response = decision
304+ . evaluate_with_opts (
305+ input. clone ( ) ,
306+ EvaluationOptions {
307+ trace : true ,
308+ ..Default :: default ( )
309+ } ,
310+ )
311+ . await
312+ . unwrap ( ) ;
313+
314+ assert_eq ! ( input. to_value( ) , snapshot) ;
315+ assert_eq ! (
316+ response. result,
317+ json!( { "a" : 1 , "nested" : { "k" : "v" } , "first" : 2 , "second" : 3 , "dtResult" : "hit" } )
318+ . into( )
319+ ) ;
320+
321+ let trace = response. trace . unwrap ( ) . into_graph ( ) . unwrap ( ) ;
322+ let output_of = |id : & str | trace. get ( id) . unwrap ( ) . output . clone ( ) ;
323+
324+ assert_eq ! (
325+ output_of( "in" ) ,
326+ json!( { "a" : 1 , "nested" : { "k" : "v" } } ) . into( )
327+ ) ;
328+ assert_eq ! (
329+ output_of( "e1" ) ,
330+ json!( { "a" : 1 , "nested" : { "k" : "v" } , "first" : 2 } ) . into( )
331+ ) ;
332+ assert_eq ! (
333+ output_of( "sw" ) ,
334+ json!( { "a" : 1 , "nested" : { "k" : "v" } , "first" : 2 } ) . into( )
335+ ) ;
336+ assert_eq ! (
337+ output_of( "e2" ) ,
338+ json!( { "a" : 1 , "nested" : { "k" : "v" } , "first" : 2 , "second" : 3 } ) . into( )
339+ ) ;
340+ }
341+
342+ #[ tokio:: test]
343+ #[ cfg_attr( miri, ignore) ]
344+ async fn merged_inputs_do_not_mutate_parent_node_outputs ( ) {
345+ let content = serde_json:: from_value ( json ! ( {
346+ "nodes" : [
347+ { "id" : "in" , "name" : "in" , "type" : "inputNode" , "content" : { } } ,
348+ {
349+ "id" : "pa" , "name" : "pa" , "type" : "expressionNode" ,
350+ "content" : {
351+ "passThrough" : true ,
352+ "expressions" : [ { "id" : "x1" , "key" : "pa" , "value" : "10" } ]
353+ }
354+ } ,
355+ {
356+ "id" : "pb" , "name" : "pb" , "type" : "expressionNode" ,
357+ "content" : {
358+ "passThrough" : true ,
359+ "expressions" : [ { "id" : "x2" , "key" : "pb" , "value" : "20" } ]
360+ }
361+ } ,
362+ {
363+ "id" : "join" , "name" : "join" , "type" : "expressionNode" ,
364+ "content" : {
365+ "passThrough" : true ,
366+ "expressions" : [ { "id" : "x3" , "key" : "sum" , "value" : "pa + pb" } ]
367+ }
368+ } ,
369+ { "id" : "out" , "name" : "out" , "type" : "outputNode" , "content" : { } }
370+ ] ,
371+ "edges" : [
372+ { "id" : "ed1" , "sourceId" : "in" , "targetId" : "pa" } ,
373+ { "id" : "ed2" , "sourceId" : "in" , "targetId" : "pb" } ,
374+ { "id" : "ed3" , "sourceId" : "pa" , "targetId" : "join" } ,
375+ { "id" : "ed4" , "sourceId" : "pb" , "targetId" : "join" } ,
376+ { "id" : "ed5" , "sourceId" : "join" , "targetId" : "out" }
377+ ]
378+ } ) )
379+ . unwrap ( ) ;
380+ let decision = Decision :: from ( Arc :: new ( content) ) ;
381+
382+ let input: Variable = json ! ( { "a" : 1 } ) . into ( ) ;
383+ let snapshot = input. to_value ( ) ;
384+
385+ let response = decision
386+ . evaluate_with_opts (
387+ input. clone ( ) ,
388+ EvaluationOptions {
389+ trace : true ,
390+ ..Default :: default ( )
391+ } ,
392+ )
393+ . await
394+ . unwrap ( ) ;
395+
396+ assert_eq ! ( input. to_value( ) , snapshot) ;
397+ assert_eq ! (
398+ response. result,
399+ json!( { "a" : 1 , "pa" : 10 , "pb" : 20 , "sum" : 30 } ) . into( )
400+ ) ;
401+
402+ let trace = response. trace . unwrap ( ) . into_graph ( ) . unwrap ( ) ;
403+ let output_of = |id : & str | trace. get ( id) . unwrap ( ) . output . clone ( ) ;
404+
405+ assert_eq ! ( output_of( "in" ) , json!( { "a" : 1 } ) . into( ) ) ;
406+ assert_eq ! ( output_of( "pa" ) , json!( { "a" : 1 , "pa" : 10 } ) . into( ) ) ;
407+ assert_eq ! ( output_of( "pb" ) , json!( { "a" : 1 , "pb" : 20 } ) . into( ) ) ;
408+ }
0 commit comments