ironflow_engine/executor/
stored.rs1use serde_json::Value;
4
5use ironflow_store::entities::Step;
6
7use super::{StepArtifacts, StepOutput};
8
9impl From<&Step> for StepOutput {
31 fn from(step: &Step) -> Self {
32 Self {
33 output: step.output.clone().unwrap_or(Value::Null),
34 duration_ms: step.duration_ms,
35 cost_usd: step.cost_usd,
36 input_tokens: step.input_tokens,
37 cache_read_input_tokens: step.cache_read_input_tokens,
38 cache_creation_input_tokens: step.cache_creation_input_tokens,
39 output_tokens: step.output_tokens,
40 model: None,
41 debug_messages: None,
42 artifacts: StepArtifacts::default(),
43 account_id: step.account_id,
44 environment_id: step.environment_id.clone(),
45 }
46 }
47}
48
49#[cfg(test)]
50mod tests {
51 use std::collections::HashMap;
52
53 use ironflow_store::entities::{
54 NewRun, NewStep, StepKind, StepStatus, StepUpdate, TriggerKind,
55 };
56 use ironflow_store::memory::InMemoryStore;
57 use ironflow_store::store::RunStore;
58 use rust_decimal::Decimal;
59 use serde_json::json;
60 use uuid::Uuid;
61
62 use super::*;
63
64 async fn stored_step(output: Option<Value>) -> Step {
66 let store = InMemoryStore::new();
67 let run = store
68 .create_run(NewRun {
69 created_by: None,
70 workflow_name: "collect".to_string(),
71 trigger: TriggerKind::Manual,
72 payload: json!({}),
73 max_retries: 0,
74 handler_version: None,
75 labels: HashMap::new(),
76 scheduled_at: None,
77 idempotency_key: None,
78 concurrency_key: None,
79 concurrency_limits: Vec::new(),
80 max_cost_usd: None,
81 worker_tags: Vec::new(),
82 })
83 .await
84 .expect("create run")
85 .into_run();
86 let step = store
87 .create_step(NewStep {
88 run_id: run.id,
89 trace_id: Uuid::now_v7(),
90 name: "disk".to_string(),
91 kind: StepKind::Shell,
92 position: 0,
93 input: None,
94 is_error_handler: false,
95 })
96 .await
97 .expect("create step");
98 store
99 .update_step(
100 step.id,
101 StepUpdate {
102 status: Some(StepStatus::Running),
103 ..StepUpdate::default()
104 },
105 )
106 .await
107 .expect("to running");
108 if output.is_some() {
109 store
110 .update_step(
111 step.id,
112 StepUpdate {
113 status: Some(StepStatus::Completed),
114 output,
115 duration_ms: Some(12),
116 cost_usd: Some(Decimal::new(3, 2)),
117 ..StepUpdate::default()
118 },
119 )
120 .await
121 .expect("to completed");
122 }
123 store.get_step(step.id).await.expect("get").expect("exists")
124 }
125
126 #[tokio::test]
127 async fn a_stored_shell_step_reads_through_the_accessors() {
128 let step = stored_step(Some(
129 json!({"stdout": "42%\n", "stderr": "", "exit_code": 0}),
130 ))
131 .await;
132
133 let output = StepOutput::from(&step);
134
135 assert_eq!(output.stdout(), "42%\n");
136 assert_eq!(output.exit_code(), Some(0));
137 assert!(output.is_success());
138 assert_eq!(output.duration_ms, 12);
139 assert_eq!(output.cost_usd, Decimal::new(3, 2));
140 }
141
142 #[tokio::test]
143 async fn step_output_error_reads_from_stored_step() {
144 let step = stored_step(Some(json!({"error": "x"}))).await;
145 assert_eq!(StepOutput::from(&step).error(), Some("x"));
146
147 let step = stored_step(None).await;
148 assert_eq!(StepOutput::from(&step).error(), None);
149 }
150
151 #[tokio::test]
152 async fn a_step_without_output_reads_as_empty() {
153 let step = stored_step(None).await;
154
155 let output = StepOutput::from(&step);
156
157 assert_eq!(output.output, Value::Null);
158 assert_eq!(output.stdout(), "");
159 assert_eq!(output.exit_code(), None);
160 assert!(!output.is_success());
161 }
162
163 #[tokio::test]
164 async fn a_stored_step_carries_its_environment_id() {
165 let mut step = stored_step(None).await;
166 assert_eq!(StepOutput::from(&step).environment_id, None);
167
168 step.environment_id = Some("ironflow-env-0a1b2c".to_string());
169 assert_eq!(
170 StepOutput::from(&step).environment_id.as_deref(),
171 Some("ironflow-env-0a1b2c")
172 );
173 }
174}