ironflow-cli 0.1.1

CLI tool to drive the Ironflow workflow engine
Documentation
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
//! Integration tests: CLI commands against a real ironflow-api server (in-memory store).
//!
//! Reuses the same `spawn_server` pattern as `ironflow-sdk/tests/integration.rs`.

use std::sync::Arc;
use std::time::Duration;

use ironflow_api::routes::{RouterConfig, create_router};
use ironflow_api::state::AppState;
use ironflow_auth::jwt::{AccessToken, JwtConfig};
use ironflow_auth::password;
use ironflow_core::providers::claude::ClaudeCodeProvider;
use ironflow_engine::context::WorkflowContext;
use ironflow_engine::engine::Engine;
use ironflow_engine::handler::{HandlerFuture, WorkflowHandler};
use ironflow_engine::notify::Event;
use ironflow_sdk::IronflowClient;
use ironflow_sdk::client::ClientConfig;
use ironflow_store::entities::NewUser;
use ironflow_store::memory::InMemoryStore;
use ironflow_store::store::Store;
use tokio::net::TcpListener;
use tokio::sync::broadcast;
use uuid::Uuid;

use ironflow_cli::commands;
use ironflow_cli::commands::run::{RunArgs, RunCommands};
use ironflow_cli::commands::workflow::{WorkflowArgs, WorkflowCommands};

struct DeployWorkflow;

impl WorkflowHandler for DeployWorkflow {
    fn name(&self) -> &str {
        "deploy"
    }
    fn execute<'a>(&'a self, _ctx: &'a mut WorkflowContext) -> HandlerFuture<'a> {
        Box::pin(async move { Ok(()) })
    }
}

struct BuildWorkflow;

impl WorkflowHandler for BuildWorkflow {
    fn name(&self) -> &str {
        "build"
    }
    fn execute<'a>(&'a self, _ctx: &'a mut WorkflowContext) -> HandlerFuture<'a> {
        Box::pin(async move { Ok(()) })
    }
}

fn jwt_config() -> Arc<JwtConfig> {
    Arc::new(JwtConfig {
        secret: "cli-integration-test-secret".to_string(),
        access_token_ttl_secs: 900,
        refresh_token_ttl_secs: 604800,
        cookie_domain: None,
        cookie_secure: false,
    })
}

async fn spawn_server() -> (String, String) {
    let store: Arc<dyn Store> = Arc::new(InMemoryStore::new());
    let provider = Arc::new(ClaudeCodeProvider::new());
    let mut engine = Engine::new(store.clone(), provider);
    engine.register(DeployWorkflow).unwrap();
    engine.register(BuildWorkflow).unwrap();

    let jwt_cfg = jwt_config();
    let (event_sender, _) = broadcast::channel::<Event>(16);

    let hash = password::hash("test-password").unwrap();
    let user = store
        .create_user(NewUser {
            email: "cli-test@test.local".to_string(),
            username: "cli-test".to_string(),
            password_hash: hash,
            is_admin: Some(true),
        })
        .await
        .unwrap();

    let state = AppState::new(
        store,
        Arc::new(engine),
        jwt_cfg.clone(),
        "test-worker-token".to_string(),
        event_sender,
    );

    let config = RouterConfig {
        rate_limit_auth: None,
        rate_limit_general: None,
        ..RouterConfig::default()
    };
    let router = create_router(state, config);

    let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
    let addr = listener.local_addr().unwrap();
    tokio::spawn(async move {
        axum::serve(listener, router).await.unwrap();
    });

    let base_url = format!("http://{addr}");
    let token = AccessToken::for_user(user.id, "cli-test", true, &jwt_cfg).unwrap();

    (base_url, token.0)
}

fn make_client(base_url: &str, token: &str) -> IronflowClient {
    let config = ClientConfig {
        base_url: base_url.to_string(),
        api_key: token.to_string(),
        timeout: Duration::from_secs(10),
    };
    IronflowClient::from_config(config)
}

// ── Stats ──────────────────────────────────────────────────────

#[tokio::test]
async fn stats_table_output() {
    let (base_url, token) = spawn_server().await;
    let client = make_client(&base_url, &token);

    commands::stats::execute(&client, false).await.unwrap();
}

#[tokio::test]
async fn stats_json_output() {
    let (base_url, token) = spawn_server().await;
    let client = make_client(&base_url, &token);

    commands::stats::execute(&client, true).await.unwrap();
}

// ── Workflow ───────────────────────────────────────────────────

#[tokio::test]
async fn workflow_list_table() {
    let (base_url, token) = spawn_server().await;
    let client = make_client(&base_url, &token);

    let args = WorkflowArgs {
        command: WorkflowCommands::List,
    };
    commands::workflow::execute(&client, &args, false)
        .await
        .unwrap();
}

#[tokio::test]
async fn workflow_list_json() {
    let (base_url, token) = spawn_server().await;
    let client = make_client(&base_url, &token);

    let args = WorkflowArgs {
        command: WorkflowCommands::List,
    };
    commands::workflow::execute(&client, &args, true)
        .await
        .unwrap();
}

#[tokio::test]
async fn workflow_get_existing() {
    let (base_url, token) = spawn_server().await;
    let client = make_client(&base_url, &token);

    let args = WorkflowArgs {
        command: WorkflowCommands::Get {
            name: "deploy".to_string(),
        },
    };
    commands::workflow::execute(&client, &args, false)
        .await
        .unwrap();
}

#[tokio::test]
async fn workflow_get_not_found() {
    let (base_url, token) = spawn_server().await;
    let client = make_client(&base_url, &token);

    let args = WorkflowArgs {
        command: WorkflowCommands::Get {
            name: "nonexistent".to_string(),
        },
    };
    let result = commands::workflow::execute(&client, &args, false).await;
    assert!(result.is_err());
}

// ── Run list ──────────────────────────────────────────────────

#[tokio::test]
async fn run_list_empty_table() {
    let (base_url, token) = spawn_server().await;
    let client = make_client(&base_url, &token);

    let args = RunArgs {
        command: RunCommands::List {
            status: None,
            workflow: None,
            page: None,
            per_page: None,
        },
    };
    commands::run::execute(&client, &args, false, false)
        .await
        .unwrap();
}

#[tokio::test]
async fn run_list_empty_json() {
    let (base_url, token) = spawn_server().await;
    let client = make_client(&base_url, &token);

    let args = RunArgs {
        command: RunCommands::List {
            status: None,
            workflow: None,
            page: None,
            per_page: None,
        },
    };
    commands::run::execute(&client, &args, true, false)
        .await
        .unwrap();
}

#[tokio::test]
async fn run_list_with_filters() {
    let (base_url, token) = spawn_server().await;
    let client = make_client(&base_url, &token);

    let args = RunArgs {
        command: RunCommands::List {
            status: Some("completed".to_string()),
            workflow: Some("deploy".to_string()),
            page: Some(1),
            per_page: Some(10),
        },
    };
    commands::run::execute(&client, &args, false, false)
        .await
        .unwrap();
}

// ── Run create ────────────────────────────────────────────────

#[tokio::test]
async fn run_create_and_get() {
    let (base_url, token) = spawn_server().await;
    let client = make_client(&base_url, &token);

    let args = RunArgs {
        command: RunCommands::Create {
            workflow: "deploy".to_string(),
            payload: Some(r#"{"env": "staging"}"#.to_string()),
            payload_file: None,
        },
    };
    commands::run::execute(&client, &args, false, false)
        .await
        .unwrap();

    let runs = client.list_runs().await.unwrap();
    assert_eq!(runs.data.len(), 1);
    let run_id = runs.data[0].id;

    let get_args = RunArgs {
        command: RunCommands::Get { id: run_id },
    };
    commands::run::execute(&client, &get_args, false, false)
        .await
        .unwrap();
    commands::run::execute(&client, &get_args, true, false)
        .await
        .unwrap();
}

#[tokio::test]
async fn run_create_unknown_workflow() {
    let (base_url, token) = spawn_server().await;
    let client = make_client(&base_url, &token);

    let args = RunArgs {
        command: RunCommands::Create {
            workflow: "nonexistent".to_string(),
            payload: None,
            payload_file: None,
        },
    };
    let result = commands::run::execute(&client, &args, false, false).await;
    assert!(result.is_err());
}

#[tokio::test]
async fn run_create_invalid_payload() {
    let (base_url, token) = spawn_server().await;
    let client = make_client(&base_url, &token);

    let args = RunArgs {
        command: RunCommands::Create {
            workflow: "deploy".to_string(),
            payload: Some("not valid json".to_string()),
            payload_file: None,
        },
    };
    let result = commands::run::execute(&client, &args, false, false).await;
    assert!(result.is_err());
    assert!(result.unwrap_err().to_string().contains("invalid JSON"));
}

#[tokio::test]
async fn run_create_non_object_payload() {
    let (base_url, token) = spawn_server().await;
    let client = make_client(&base_url, &token);

    let args = RunArgs {
        command: RunCommands::Create {
            workflow: "deploy".to_string(),
            payload: Some(r#""just a string""#.to_string()),
            payload_file: None,
        },
    };
    let result = commands::run::execute(&client, &args, false, false).await;
    assert!(result.is_err());
    assert!(result.unwrap_err().to_string().contains("JSON object"));
}

// ── Run get not found ─────────────────────────────────────────

#[tokio::test]
async fn run_get_not_found() {
    let (base_url, token) = spawn_server().await;
    let client = make_client(&base_url, &token);

    let args = RunArgs {
        command: RunCommands::Get { id: Uuid::now_v7() },
    };
    let result = commands::run::execute(&client, &args, false, false).await;
    assert!(result.is_err());
}

// ── Run cancel ────────────────────────────────────────────────

#[tokio::test]
async fn run_cancel_not_found() {
    let (base_url, token) = spawn_server().await;
    let client = make_client(&base_url, &token);

    let args = RunArgs {
        command: RunCommands::Cancel { id: Uuid::now_v7() },
    };
    let result = commands::run::execute(&client, &args, false, false).await;
    assert!(result.is_err());
}

// ── Run approve ───────────────────────────────────────────────

#[tokio::test]
async fn run_approve_not_found() {
    let (base_url, token) = spawn_server().await;
    let client = make_client(&base_url, &token);

    let args = RunArgs {
        command: RunCommands::Approve { id: Uuid::now_v7() },
    };
    let result = commands::run::execute(&client, &args, false, false).await;
    assert!(result.is_err());
}

// ── Run retry ─────────────────────────────────────────────────

#[tokio::test]
async fn run_retry_not_found() {
    let (base_url, token) = spawn_server().await;
    let client = make_client(&base_url, &token);

    let args = RunArgs {
        command: RunCommands::Retry { id: Uuid::now_v7() },
    };
    let result = commands::run::execute(&client, &args, false, false).await;
    assert!(result.is_err());
}

// ── Unauthorized ──────────────────────────────────────────────

#[tokio::test]
async fn unauthorized_returns_error() {
    let (base_url, _) = spawn_server().await;
    let client = make_client(&base_url, "invalid-token");

    let result = commands::stats::execute(&client, false).await;
    assert!(result.is_err());
}

// ── Payload from file ─────────────────────────────────────────

#[tokio::test]
async fn run_create_from_payload_file() {
    let (base_url, token) = spawn_server().await;
    let client = make_client(&base_url, &token);

    let mut tmp = tempfile::NamedTempFile::new().unwrap();
    std::io::Write::write_all(&mut tmp, br#"{"env": "prod"}"#).unwrap();

    let args = RunArgs {
        command: RunCommands::Create {
            workflow: "deploy".to_string(),
            payload: None,
            payload_file: Some(tmp.path().to_path_buf()),
        },
    };
    commands::run::execute(&client, &args, false, false)
        .await
        .unwrap();
}

#[tokio::test]
async fn run_create_from_missing_file() {
    let (base_url, token) = spawn_server().await;
    let client = make_client(&base_url, &token);

    let args = RunArgs {
        command: RunCommands::Create {
            workflow: "deploy".to_string(),
            payload: None,
            payload_file: Some("/nonexistent/payload.json".into()),
        },
    };
    let result = commands::run::execute(&client, &args, false, false).await;
    assert!(result.is_err());
    assert!(result.unwrap_err().to_string().contains("cannot read"));
}