-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathengine.rs
More file actions
1720 lines (1566 loc) · 55.3 KB
/
Copy pathengine.rs
File metadata and controls
1720 lines (1566 loc) · 55.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//! Public runtime execution API.
//!
//! This module is the new entrypoint for flow execution from external crates.
//! It executes compiled flow plans via the runtime engine executor loop.
mod compiler;
mod emitter;
mod executor;
mod model;
use futures_lite::future::block_on;
use tucana::shared::{ExecutionFlow, NodeExecutionResult, NodeFunction, Value};
use crate::handler::registry::FunctionStore;
use crate::runtime::execution::value_store::ValueStore;
use crate::runtime::remote::RemoteRuntime;
use crate::types::exit_reason::ExitReason;
use crate::types::signal::Signal;
use compiler::compile_flow;
pub use emitter::{EmitType, ExecutionId, RespondEmitter};
fn null_value() -> Value {
Value {
kind: Some(tucana::shared::value::Kind::NullValue(0)),
}
}
/// Runtime engine entrypoint used by runtime binaries and CLI tools.
pub struct ExecutionEngine {
handlers: FunctionStore,
}
/// Full result of one engine execution, including per-node results for reporting.
#[derive(Debug, Clone)]
pub struct EngineExecutionReport {
pub signal: Signal,
pub exit_reason: ExitReason,
pub node_execution_results: Vec<NodeExecutionResult>,
}
impl Default for ExecutionEngine {
fn default() -> Self {
Self::new()
}
}
impl ExecutionEngine {
/// Build a new execution engine with default handler registry.
pub fn new() -> Self {
Self {
handlers: FunctionStore::default(),
}
}
/// Execute an `ExecutionFlow`.
pub fn execute_flow(
&self,
flow: ExecutionFlow,
remote: Option<&dyn RemoteRuntime>,
respond_emitter: Option<&dyn RespondEmitter>,
with_trace: bool,
) -> (Signal, ExitReason) {
let report = block_on(self.execute_flow_with_execution_id_report_async(
ExecutionId::new_v4(),
flow,
remote,
respond_emitter,
with_trace,
));
(report.signal, report.exit_reason)
}
/// Execute an `ExecutionFlow` asynchronously.
pub async fn execute_flow_async(
&self,
flow: ExecutionFlow,
remote: Option<&dyn RemoteRuntime>,
respond_emitter: Option<&dyn RespondEmitter>,
with_trace: bool,
) -> (Signal, ExitReason) {
let report = self
.execute_flow_with_execution_id_report_async(
ExecutionId::new_v4(),
flow,
remote,
respond_emitter,
with_trace,
)
.await;
(report.signal, report.exit_reason)
}
/// Execute an `ExecutionFlow` with a caller-provided execution id.
pub fn execute_flow_with_execution_id(
&self,
execution_id: ExecutionId,
flow: ExecutionFlow,
remote: Option<&dyn RemoteRuntime>,
respond_emitter: Option<&dyn RespondEmitter>,
with_trace: bool,
) -> (Signal, ExitReason) {
let report = block_on(self.execute_flow_with_execution_id_report_async(
execution_id,
flow,
remote,
respond_emitter,
with_trace,
));
(report.signal, report.exit_reason)
}
/// Execute an `ExecutionFlow` asynchronously with a caller-provided execution id.
pub async fn execute_flow_with_execution_id_async(
&self,
execution_id: ExecutionId,
flow: ExecutionFlow,
remote: Option<&dyn RemoteRuntime>,
respond_emitter: Option<&dyn RespondEmitter>,
with_trace: bool,
) -> (Signal, ExitReason) {
let report = self
.execute_flow_with_execution_id_report_async(
execution_id,
flow,
remote,
respond_emitter,
with_trace,
)
.await;
(report.signal, report.exit_reason)
}
/// Execute an `ExecutionFlow` and return the final signal plus per-node execution results.
pub fn execute_flow_report(
&self,
flow: ExecutionFlow,
remote: Option<&dyn RemoteRuntime>,
respond_emitter: Option<&dyn RespondEmitter>,
with_trace: bool,
) -> EngineExecutionReport {
block_on(self.execute_flow_with_execution_id_report_async(
ExecutionId::new_v4(),
flow,
remote,
respond_emitter,
with_trace,
))
}
/// Execute an `ExecutionFlow` asynchronously and return per-node execution results.
pub async fn execute_flow_report_async(
&self,
flow: ExecutionFlow,
remote: Option<&dyn RemoteRuntime>,
respond_emitter: Option<&dyn RespondEmitter>,
with_trace: bool,
) -> EngineExecutionReport {
self.execute_flow_with_execution_id_report_async(
ExecutionId::new_v4(),
flow,
remote,
respond_emitter,
with_trace,
)
.await
}
/// Execute an `ExecutionFlow` with a caller-provided execution id and return per-node results.
pub fn execute_flow_with_execution_id_report(
&self,
execution_id: ExecutionId,
flow: ExecutionFlow,
remote: Option<&dyn RemoteRuntime>,
respond_emitter: Option<&dyn RespondEmitter>,
with_trace: bool,
) -> EngineExecutionReport {
block_on(self.execute_flow_with_execution_id_report_async(
execution_id,
flow,
remote,
respond_emitter,
with_trace,
))
}
/// Execute an `ExecutionFlow` asynchronously with a caller-provided execution id and return per-node results.
pub async fn execute_flow_with_execution_id_report_async(
&self,
execution_id: ExecutionId,
flow: ExecutionFlow,
remote: Option<&dyn RemoteRuntime>,
respond_emitter: Option<&dyn RespondEmitter>,
with_trace: bool,
) -> EngineExecutionReport {
self.execute_graph_with_project_id_report_async(
execution_id,
flow.project_id,
flow.starting_node_id,
flow.node_functions,
flow.input_value,
remote,
respond_emitter,
with_trace,
)
.await
}
/// Execute a graph described by node list and start node.
pub fn execute_graph(
&self,
start_node_id: i64,
node_functions: Vec<NodeFunction>,
flow_input: Option<Value>,
remote: Option<&dyn RemoteRuntime>,
respond_emitter: Option<&dyn RespondEmitter>,
with_trace: bool,
) -> (Signal, ExitReason) {
let report = block_on(self.execute_graph_with_execution_id_report_async(
ExecutionId::new_v4(),
start_node_id,
node_functions,
flow_input,
remote,
respond_emitter,
with_trace,
));
(report.signal, report.exit_reason)
}
/// Execute a graph asynchronously.
pub async fn execute_graph_async(
&self,
start_node_id: i64,
node_functions: Vec<NodeFunction>,
flow_input: Option<Value>,
remote: Option<&dyn RemoteRuntime>,
respond_emitter: Option<&dyn RespondEmitter>,
with_trace: bool,
) -> (Signal, ExitReason) {
let report = self
.execute_graph_with_execution_id_report_async(
ExecutionId::new_v4(),
start_node_id,
node_functions,
flow_input,
remote,
respond_emitter,
with_trace,
)
.await;
(report.signal, report.exit_reason)
}
/// Execute a graph described by node list and start node with a caller-provided execution id.
pub fn execute_graph_with_execution_id(
&self,
execution_id: ExecutionId,
start_node_id: i64,
node_functions: Vec<NodeFunction>,
flow_input: Option<Value>,
remote: Option<&dyn RemoteRuntime>,
respond_emitter: Option<&dyn RespondEmitter>,
with_trace: bool,
) -> (Signal, ExitReason) {
let report = block_on(self.execute_graph_with_execution_id_report_async(
execution_id,
start_node_id,
node_functions,
flow_input,
remote,
respond_emitter,
with_trace,
));
(report.signal, report.exit_reason)
}
/// Execute a graph asynchronously with a caller-provided execution id.
pub async fn execute_graph_with_execution_id_async(
&self,
execution_id: ExecutionId,
start_node_id: i64,
node_functions: Vec<NodeFunction>,
flow_input: Option<Value>,
remote: Option<&dyn RemoteRuntime>,
respond_emitter: Option<&dyn RespondEmitter>,
with_trace: bool,
) -> (Signal, ExitReason) {
let report = self
.execute_graph_with_execution_id_report_async(
execution_id,
start_node_id,
node_functions,
flow_input,
remote,
respond_emitter,
with_trace,
)
.await;
(report.signal, report.exit_reason)
}
/// Execute a graph and return the final signal plus per-node execution results.
pub fn execute_graph_report(
&self,
start_node_id: i64,
node_functions: Vec<NodeFunction>,
flow_input: Option<Value>,
remote: Option<&dyn RemoteRuntime>,
respond_emitter: Option<&dyn RespondEmitter>,
with_trace: bool,
) -> EngineExecutionReport {
block_on(self.execute_graph_with_execution_id_report_async(
ExecutionId::new_v4(),
start_node_id,
node_functions,
flow_input,
remote,
respond_emitter,
with_trace,
))
}
/// Execute a graph asynchronously and return per-node execution results.
pub async fn execute_graph_report_async(
&self,
start_node_id: i64,
node_functions: Vec<NodeFunction>,
flow_input: Option<Value>,
remote: Option<&dyn RemoteRuntime>,
respond_emitter: Option<&dyn RespondEmitter>,
with_trace: bool,
) -> EngineExecutionReport {
self.execute_graph_with_execution_id_report_async(
ExecutionId::new_v4(),
start_node_id,
node_functions,
flow_input,
remote,
respond_emitter,
with_trace,
)
.await
}
/// Execute a graph with a caller-provided execution id and return per-node results.
pub fn execute_graph_with_execution_id_report(
&self,
execution_id: ExecutionId,
start_node_id: i64,
node_functions: Vec<NodeFunction>,
flow_input: Option<Value>,
remote: Option<&dyn RemoteRuntime>,
respond_emitter: Option<&dyn RespondEmitter>,
with_trace: bool,
) -> EngineExecutionReport {
block_on(self.execute_graph_with_execution_id_report_async(
execution_id,
start_node_id,
node_functions,
flow_input,
remote,
respond_emitter,
with_trace,
))
}
/// Execute a graph asynchronously with a caller-provided execution id and return per-node results.
pub async fn execute_graph_with_execution_id_report_async(
&self,
execution_id: ExecutionId,
start_node_id: i64,
node_functions: Vec<NodeFunction>,
flow_input: Option<Value>,
remote: Option<&dyn RemoteRuntime>,
respond_emitter: Option<&dyn RespondEmitter>,
with_trace: bool,
) -> EngineExecutionReport {
self.execute_graph_with_project_id_report_async(
execution_id,
0,
start_node_id,
node_functions,
flow_input,
remote,
respond_emitter,
with_trace,
)
.await
}
async fn execute_graph_with_project_id_report_async(
&self,
execution_id: ExecutionId,
project_id: i64,
start_node_id: i64,
node_functions: Vec<NodeFunction>,
flow_input: Option<Value>,
remote: Option<&dyn RemoteRuntime>,
respond_emitter: Option<&dyn RespondEmitter>,
with_trace: bool,
) -> EngineExecutionReport {
if let Some(emitter) = respond_emitter {
emitter.emit(execution_id, EmitType::StartingExec, null_value());
}
let mut value_store = match flow_input {
Some(v) => ValueStore::new(v),
None => ValueStore::default(),
};
let compiled = match compile_flow(project_id, start_node_id, node_functions) {
Ok(plan) => plan,
Err(err) => {
let runtime_error = err.as_runtime_error();
if let Some(emitter) = respond_emitter {
emitter.emit(execution_id, EmitType::FailedExec, runtime_error.as_value());
}
let signal = Signal::Failure(runtime_error);
return EngineExecutionReport {
signal,
exit_reason: ExitReason::Failure,
node_execution_results: Vec::new(),
};
}
};
let (signal, trace_run) = executor::execute_compiled(
&compiled,
&self.handlers,
&mut value_store,
remote,
execution_id,
respond_emitter,
with_trace,
)
.await;
if with_trace && let Some(trace_run) = trace_run {
println!(
"{}",
crate::runtime::execution::render::render_trace(&trace_run)
);
}
if let Some(emitter) = respond_emitter {
match &signal {
Signal::Failure(err) => {
emitter.emit(execution_id, EmitType::FailedExec, err.as_value())
}
Signal::Success(value) | Signal::Return(value) | Signal::Respond(value) => {
emitter.emit(execution_id, EmitType::FinishedExec, value.clone())
}
Signal::Stop => emitter.emit(execution_id, EmitType::FinishedExec, null_value()),
}
}
let exit_reason = signal.exit_reason();
EngineExecutionReport {
signal,
exit_reason,
node_execution_results: value_store.node_execution_results(),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::handler::argument::Argument;
use crate::handler::registry::{FunctionRegistration, FunctionStore, ThunkRunner};
use crate::runtime::execution::value_store::ValueStore;
use crate::runtime::remote::{RemoteExecution, RemoteRuntime};
use crate::types::exit_reason::ExitReason;
use async_trait::async_trait;
use std::sync::{Arc, Mutex};
use std::time::Duration;
use tucana::shared::{
InputType, ListValue, NodeExecutionResult, NodeParameter, NodeValue, ReferenceValue,
Struct, SubFlow, SubFlowSetting, Value, node_execution_result, node_value, reference_value,
sub_flow::ExecutionReference, value::Kind,
};
fn literal_param(database_id: i64, runtime_parameter_id: &str, value: Value) -> NodeParameter {
NodeParameter {
database_id,
runtime_parameter_id: runtime_parameter_id.to_string(),
value: Some(NodeValue {
value: Some(node_value::Value::LiteralValue(value)),
}),
cast: None,
}
}
fn thunk_param(database_id: i64, runtime_parameter_id: &str, node_id: i64) -> NodeParameter {
NodeParameter {
database_id,
runtime_parameter_id: runtime_parameter_id.to_string(),
value: Some(NodeValue {
value: Some(node_value::Value::SubFlow(SubFlow {
signature: String::new(),
settings: Vec::new(),
execution_reference: Some(ExecutionReference::StartingNodeId(node_id)),
})),
}),
cast: None,
}
}
fn function_thunk_param(
database_id: i64,
runtime_parameter_id: &str,
function_identifier: &str,
settings: Vec<SubFlowSetting>,
) -> NodeParameter {
NodeParameter {
database_id,
runtime_parameter_id: runtime_parameter_id.to_string(),
value: Some(NodeValue {
value: Some(node_value::Value::SubFlow(SubFlow {
signature: String::new(),
settings,
execution_reference: Some(ExecutionReference::FunctionIdentifier(
function_identifier.to_string(),
)),
})),
}),
cast: None,
}
}
fn subflow_setting(
identifier: &str,
default_value: Option<Value>,
optional: bool,
hidden: bool,
) -> SubFlowSetting {
SubFlowSetting {
identifier: identifier.to_string(),
default_value,
optional: Some(optional),
hidden: Some(hidden),
}
}
fn node_result_ref_param(
database_id: i64,
runtime_parameter_id: &str,
node_id: i64,
) -> NodeParameter {
NodeParameter {
database_id,
runtime_parameter_id: runtime_parameter_id.to_string(),
value: Some(NodeValue {
value: Some(node_value::Value::ReferenceValue(ReferenceValue {
target: Some(reference_value::Target::NodeId(node_id)),
paths: Vec::new(),
})),
}),
cast: None,
}
}
fn node(
database_id: i64,
runtime_function_id: &str,
parameters: Vec<NodeParameter>,
next_node_id: Option<i64>,
) -> NodeFunction {
NodeFunction {
database_id: Some(database_id),
runtime_function_id: runtime_function_id.to_string(),
parameters,
next_node_id,
definition_source: Some("taurus".to_string()),
}
}
fn int_value(value: i64) -> Value {
crate::value::value_from_i64(value)
}
fn string_value(value: &str) -> Value {
Value {
kind: Some(Kind::StringValue(value.to_string())),
}
}
fn null_value() -> Value {
Value {
kind: Some(Kind::NullValue(0)),
}
}
fn empty_struct_value() -> Value {
Value {
kind: Some(Kind::StructValue(Struct {
fields: std::collections::HashMap::new(),
})),
}
}
fn list_value(values: Vec<Value>) -> Value {
Value {
kind: Some(Kind::ListValue(ListValue { values })),
}
}
fn expect_success(signal: Signal) -> Value {
match signal {
Signal::Success(value) => value,
other => panic!("expected success, got {:?}", other),
}
}
fn assert_node_result_id(result: &NodeExecutionResult, expected_id: i64) {
assert_eq!(
result.id,
Some(node_execution_result::Id::NodeId(expected_id))
);
}
fn assert_function_result_id(result: &NodeExecutionResult, expected_id: &str) {
assert_eq!(
result.id,
Some(node_execution_result::Id::FunctionIdentifier(
expected_id.to_string()
))
);
}
fn sleep_handler(
_args: &[Argument],
_ctx: &mut ValueStore,
_run: &mut ThunkRunner<'_>,
) -> Signal {
std::thread::sleep(Duration::from_micros(2_000));
Signal::Success(null_value())
}
fn echo_first_arg_handler(
args: &[Argument],
_ctx: &mut ValueStore,
_run: &mut ThunkRunner<'_>,
) -> Signal {
match args.first() {
Some(Argument::Eval(value)) => Signal::Success(value.clone()),
_ => Signal::Failure(crate::types::errors::runtime_error::RuntimeError::new(
"T-TEST-000001",
"MissingEchoArgument",
"expected first eager argument",
)),
}
}
#[derive(Clone)]
struct StubRemoteRuntime {
result: NodeExecutionResult,
target_services: Option<Arc<Mutex<Vec<String>>>>,
project_ids: Option<Arc<Mutex<Vec<i64>>>>,
}
#[async_trait]
impl RemoteRuntime for StubRemoteRuntime {
async fn execute_remote(
&self,
execution: RemoteExecution,
) -> Result<NodeExecutionResult, crate::types::errors::runtime_error::RuntimeError>
{
if let Some(target_services) = &self.target_services {
target_services
.lock()
.expect("target service recorder should not be poisoned")
.push(execution.target_service);
}
if let Some(project_ids) = &self.project_ids {
project_ids
.lock()
.expect("project id recorder should not be poisoned")
.push(execution.request.project_id);
}
Ok(self.result.clone())
}
}
fn input_type_ref_param(
database_id: i64,
runtime_parameter_id: &str,
node_id: i64,
parameter_index: i64,
input_index: i64,
) -> NodeParameter {
NodeParameter {
database_id,
runtime_parameter_id: runtime_parameter_id.to_string(),
value: Some(NodeValue {
value: Some(node_value::Value::ReferenceValue(ReferenceValue {
target: Some(reference_value::Target::InputType(InputType {
node_id,
parameter_index,
input_index,
})),
paths: Vec::new(),
})),
}),
cast: None,
}
}
#[test]
fn eager_thunk_return_unwinds_one_level_and_continues_with_parent_next() {
let engine = ExecutionEngine::new();
// Node 10 is used as eager parameter thunk by node 2.
// It returns 42 and must not continue to its own next node.
let return_node = node(
10,
"std::control::return",
vec![literal_param(100, "value", int_value(42))],
Some(12),
);
// If this node ever executes, the test expectation below will fail.
let unreachable_after_return = node(12, "std::number::add", vec![], None);
// Parent node A (id=2): eager arg is node 10.
let parent = node(
2,
"std::number::add",
vec![
thunk_param(200, "lhs", 10),
literal_param(201, "rhs", int_value(1)),
],
Some(3),
);
// Next node B (id=3): depends on A result and adds 1.
let next = node(
3,
"std::number::add",
vec![
node_result_ref_param(300, "lhs", 2),
literal_param(301, "rhs", int_value(1)),
],
None,
);
let (signal, reason) = engine.execute_graph(
2,
vec![parent, next, return_node, unreachable_after_return],
None,
None,
None,
false,
);
assert_eq!(reason, ExitReason::Success);
match signal {
Signal::Success(Value {
kind: Some(Kind::NumberValue(number)),
}) => match number.number {
Some(tucana::shared::number_value::Number::Integer(v)) => assert_eq!(v, 43),
other => panic!("expected integer result 43, got {:?}", other),
},
other => panic!("expected success with value 43, got {:?}", other),
}
}
#[test]
fn return_inside_map_callback_returns_callback_value_only() {
let engine = ExecutionEngine::new();
let map_node = node(
1,
"std::list::map",
vec![
literal_param(
100,
"list",
list_value(vec![
string_value("age"),
string_value("email"),
string_value("username"),
]),
),
thunk_param(101, "transform", 2),
],
None,
);
let is_equal_node = node(
2,
"std::text::is_equal",
vec![
input_type_ref_param(200, "first", 1, 1, 0),
literal_param(201, "second", string_value("username")),
],
Some(3),
);
let if_node = node(
3,
"std::control::if",
vec![
node_result_ref_param(300, "condition", 2),
thunk_param(301, "runnable", 4),
],
Some(5),
);
let return_item_node = node(
4,
"std::control::return",
vec![input_type_ref_param(400, "value", 1, 1, 0)],
None,
);
let return_null_node = node(
5,
"std::control::return",
vec![literal_param(500, "value", null_value())],
None,
);
let (signal, reason) = engine.execute_graph(
1,
vec![
map_node,
is_equal_node,
if_node,
return_item_node,
return_null_node,
],
None,
None,
None,
false,
);
assert_eq!(reason, ExitReason::Success);
match signal {
Signal::Success(Value {
kind: Some(Kind::ListValue(ListValue { values })),
}) => {
assert_eq!(
values,
vec![null_value(), null_value(), string_value("username")]
);
}
other => panic!(
"expected Success([null, null, \"username\"]), got {:?}",
other
),
}
}
#[test]
fn function_subflow_map_executes_function_identifier_with_iteration_input() {
let engine = ExecutionEngine::new();
let map_node = node(
1,
"std::list::map",
vec![
literal_param(100, "list", list_value(vec![int_value(1), int_value(2)])),
function_thunk_param(
101,
"transform",
"std::number::add",
vec![
subflow_setting("lhs", None, false, false),
subflow_setting("rhs", Some(int_value(2)), false, true),
],
),
],
None,
);
let (signal, reason) = engine.execute_graph(1, vec![map_node], None, None, None, false);
assert_eq!(reason, ExitReason::Success);
assert_eq!(
expect_success(signal),
list_value(vec![int_value(3), int_value(4)])
);
}
#[test]
fn function_subflow_filter_executes_predicate_identifier() {
let engine = ExecutionEngine::new();
let filter_node = node(
1,
"std::list::filter",
vec![
literal_param(
100,
"list",
list_value(vec![int_value(1), int_value(4), int_value(7)]),
),
function_thunk_param(
101,
"predicate",
"std::number::is_greater",
vec![
subflow_setting("lhs", None, false, false),
subflow_setting("rhs", Some(int_value(3)), false, true),
],
),
],
None,
);
let (signal, reason) = engine.execute_graph(1, vec![filter_node], None, None, None, false);
assert_eq!(reason, ExitReason::Success);
assert_eq!(
expect_success(signal),
list_value(vec![int_value(4), int_value(7)])
);
}
#[test]
fn function_subflow_default_replaces_null_callback_input() {
let engine = ExecutionEngine::new();
let map_node = node(
1,
"std::list::map",
vec![
literal_param(100, "list", list_value(vec![null_value(), int_value(5)])),
function_thunk_param(
101,
"transform",
"std::control::value",
vec![subflow_setting("value", Some(int_value(9)), false, false)],
),
],
None,
);
let (signal, reason) = engine.execute_graph(1, vec![map_node], None, None, None, false);
assert_eq!(reason, ExitReason::Success);
assert_eq!(
expect_success(signal),
list_value(vec![int_value(9), int_value(5)])
);
}
#[test]
fn function_subflow_hidden_setting_always_uses_default() {
let engine = ExecutionEngine::new();
let map_node = node(
1,
"std::list::map",
vec![
literal_param(100, "list", list_value(vec![int_value(1), int_value(2)])),
function_thunk_param(
101,
"transform",
"std::control::value",
vec![subflow_setting("value", Some(int_value(9)), false, true)],
),
],
None,
);
let (signal, reason) = engine.execute_graph(1, vec![map_node], None, None, None, false);
assert_eq!(reason, ExitReason::Success);
assert_eq!(
expect_success(signal),
list_value(vec![int_value(9), int_value(9)])
);
}
#[test]
fn function_subflow_optional_missing_setting_uses_null() {
let engine = ExecutionEngine::new();
let if_node = node(
1,
"std::control::if",
vec![
literal_param(
100,
"condition",
Value {
kind: Some(Kind::BoolValue(true)),
},
),
function_thunk_param(
101,
"runnable",
"std::control::value",
vec![subflow_setting("value", None, true, false)],
),
],
None,
);