fukurow-cli 0.1.0

Command-line interface for Fukurow reasoning and knowledge graph operations
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
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
//! Tests for the cli crate

use reasoner_cli::commands::{Cli, Commands, CommandResult, CommandExecutor, OutputFormat};
use clap::Parser;
use reasoner_graph::model::CyberEvent;
use std::path::PathBuf;

#[test]
fn test_cli_parsing_info() {
    let args = vec!["reasoner-cli", "info"];
    let cli = Cli::try_parse_from(args).unwrap();

    match cli.command {
        Commands::Info => {} // Expected
        _ => panic!("Expected Info command"),
    }
}

#[test]
fn test_cli_parsing_serve() {
    let args = vec!["reasoner-cli", "serve", "--host", "127.0.0.1", "--port", "8080"];
    let cli = Cli::try_parse_from(args).unwrap();

    match cli.command {
        Commands::Serve { host, port } => {
            assert_eq!(host, "127.0.0.1");
            assert_eq!(port, 8080);
        }
        _ => panic!("Expected Serve command"),
    }
}

#[test]
fn test_cli_parsing_analyze_with_file() {
    let args = vec!["reasoner-cli", "analyze", "--file", "test.json", "--format", "json"];
    let cli = Cli::try_parse_from(args).unwrap();

    match cli.command {
        Commands::Analyze { file, json, format } => {
            assert_eq!(file, Some(PathBuf::from("test.json")));
            assert_eq!(json, None);
            assert_eq!(format, OutputFormat::Json);
        }
        _ => panic!("Expected Analyze command"),
    }
}

#[test]
fn test_cli_parsing_analyze_with_json() {
    let json_data = r#"{"type": "NetworkConnection", "source_ip": "192.168.1.10"}"#;
    let args = vec!["reasoner-cli", "analyze", "--json", json_data, "--format", "text"];
    let cli = Cli::try_parse_from(args).unwrap();

    match cli.command {
        Commands::Analyze { file, json, format } => {
            assert_eq!(file, None);
            assert_eq!(json, Some(json_data.to_string()));
            assert_eq!(format, OutputFormat::Text);
        }
        _ => panic!("Expected Analyze command"),
    }
}

#[test]
fn test_cli_parsing_process() {
    let args = vec!["reasoner-cli", "process", "--input", "events.json", "--output", "results.json"];
    let cli = Cli::try_parse_from(args).unwrap();

    match cli.command {
        Commands::Process { input, output, format } => {
            assert_eq!(input, PathBuf::from("events.json"));
            assert_eq!(output, Some(PathBuf::from("results.json")));
            assert_eq!(format, OutputFormat::Json); // Default
        }
        _ => panic!("Expected Process command"),
    }
}

#[test]
fn test_cli_parsing_query() {
    let args = vec![
        "reasoner-cli", "query",
        "--subject", "https://example.com/user/1",
        "--predicate", "https://example.com/ns/name",
        "--format", "text"
    ];
    let cli = Cli::try_parse_from(args).unwrap();

    match cli.command {
        Commands::Query { subject, predicate, object, format } => {
            assert_eq!(subject, Some("https://example.com/user/1".to_string()));
            assert_eq!(predicate, Some("https://example.com/ns/name".to_string()));
            assert_eq!(object, None);
            assert_eq!(format, OutputFormat::Text);
        }
        _ => panic!("Expected Query command"),
    }
}

#[test]
fn test_cli_parsing_threat_stats() {
    let args = vec!["reasoner-cli", "threat", "stats"];
    let cli = Cli::try_parse_from(args).unwrap();

    match cli.command {
        Commands::Threat { command } => {
            match command {
                reasoner_cli::commands::ThreatCommands::Stats => {} // Expected
                _ => panic!("Expected Stats subcommand"),
            }
        }
        _ => panic!("Expected Threat command"),
    }
}

#[test]
fn test_cli_parsing_threat_check() {
    let args = vec!["reasoner-cli", "threat", "check", "--value", "192.168.1.100", "--type", "ip"];
    let cli = Cli::try_parse_from(args).unwrap();

    match cli.command {
        Commands::Threat { command } => {
            match command {
                reasoner_cli::commands::ThreatCommands::Check { value, r#type } => {
                    assert_eq!(value, "192.168.1.100");
                    assert_eq!(r#type, "ip");
                }
                _ => panic!("Expected Check subcommand"),
            }
        }
        _ => panic!("Expected Threat command"),
    }
}

#[test]
fn test_cli_invalid_args() {
    let args = vec!["reasoner-cli", "analyze"]; // Missing required --file or --json
    let result = Cli::try_parse_from(args);
    assert!(result.is_err()); // Should fail due to missing required args
}

#[test]
fn test_output_format_enum() {
    let text_format = OutputFormat::Text;
    let json_format = OutputFormat::Json;
    let json_pretty_format = OutputFormat::JsonPretty;

    assert_eq!(format!("{:?}", text_format), "Text");
    assert_eq!(format!("{:?}", json_format), "Json");
    assert_eq!(format!("{:?}", json_pretty_format), "JsonPretty");
}

#[test]
fn test_command_result_creation() {
    let success_result = CommandResult {
        success: true,
        message: "Operation completed".to_string(),
        data: Some(serde_json::json!({"count": 5})),
    };

    assert!(success_result.success);
    assert_eq!(success_result.message, "Operation completed");
    assert!(success_result.data.is_some());

    let error_result = CommandResult {
        success: false,
        message: "Operation failed".to_string(),
        data: None,
    };

    assert!(!error_result.success);
    assert_eq!(error_result.message, "Operation failed");
    assert!(error_result.data.is_none());
}

#[tokio::test]
async fn test_command_executor_creation() {
    let executor = CommandExecutor::new();
    // Should create without panicking
    assert!(true); // If we reach here, creation succeeded
}

#[tokio::test]
async fn test_command_executor_info() {
    let mut executor = CommandExecutor::new();
    let result = executor.execute(Commands::Info).await.unwrap();

    assert!(result.success);
    assert!(result.message.contains("System information"));
    assert!(result.data.is_some());

    if let Some(data) = &result.data {
        let info = data.as_object().unwrap();
        assert!(info.contains_key("name"));
        assert!(info.contains_key("version"));
        assert!(info.contains_key("rust_version"));
        assert!(info.contains_key("features"));
    }
}

#[tokio::test]
async fn test_command_executor_analyze_invalid_input() {
    let mut executor = CommandExecutor::new();

    let command = Commands::Analyze {
        file: None,
        json: Some("invalid json".to_string()),
        format: OutputFormat::Text,
    };

    let result = executor.execute(command).await;
    assert!(result.is_err()); // Should fail due to invalid JSON
}

#[tokio::test]
async fn test_command_executor_analyze_valid_json() {
    let mut executor = CommandExecutor::new();

    let event_json = r#"{
        "type": "NetworkConnection",
        "source_ip": "192.168.1.10",
        "dest_ip": "10.0.0.50",
        "port": 443,
        "protocol": "tcp",
        "timestamp": 1640995200
    }"#;

    let command = Commands::Analyze {
        file: None,
        json: Some(event_json.to_string()),
        format: OutputFormat::Json,
    };

    let result = executor.execute(command).await.unwrap();

    assert!(result.success);
    assert!(result.message.contains("Event analyzed"));
    assert!(result.data.is_some());
}

#[tokio::test]
async fn test_command_executor_query_empty_graph() {
    let mut executor = CommandExecutor::new();

    let command = Commands::Query {
        subject: None,
        predicate: None,
        object: None,
        format: OutputFormat::Text,
    };

    let result = executor.execute(command).await.unwrap();

    assert!(result.success);
    assert!(result.message.contains("Found 0 triples"));
    assert!(result.data.is_some());
}

#[tokio::test]
async fn test_command_executor_threat_stats() {
    let mut executor = CommandExecutor::new();

    let command = Commands::Threat {
        command: reasoner_cli::commands::ThreatCommands::Stats,
    };

    let result = executor.execute(command).await.unwrap();

    assert!(result.success);
    assert!(result.message.contains("Threat intelligence statistics"));
    assert!(result.data.is_some());

    if let Some(data) = &result.data {
        let stats = data.as_object().unwrap();
        assert!(stats.contains_key("total_indicators"));
        assert!(stats.contains_key("ip_indicators"));
        assert!(stats.contains_key("domain_indicators"));
        assert!(stats.contains_key("sources"));
    }
}

#[tokio::test]
async fn test_command_executor_threat_check_malicious() {
    let mut executor = CommandExecutor::new();

    let command = Commands::Threat {
        command: reasoner_cli::commands::ThreatCommands::Check {
            value: "192.168.1.100".to_string(),
            r#type: "ip".to_string(),
        },
    };

    let result = executor.execute(command).await.unwrap();

    assert!(result.success);
    assert!(result.message.contains("Threat check completed"));
    assert!(result.data.is_some());

    if let Some(data) = &result.data {
        let check_result = data.as_object().unwrap();
        assert!(check_result.contains_key("value"));
        assert!(check_result.contains_key("type"));
        assert!(check_result.contains_key("is_threat"));
        assert!(check_result.contains_key("threat_info"));
    }
}

#[tokio::test]
async fn test_command_executor_threat_check_safe() {
    let mut executor = CommandExecutor::new();

    let command = Commands::Threat {
        command: reasoner_cli::commands::ThreatCommands::Check {
            value: "8.8.8.8".to_string(),
            r#type: "ip".to_string(),
        },
    };

    let result = executor.execute(command).await.unwrap();

    assert!(result.success);
    assert!(result.message.contains("Threat check completed"));
    assert!(result.data.is_some());

    if let Some(data) = &result.data {
        let check_result = data.as_object().unwrap();
        assert_eq!(check_result.get("is_threat"), Some(&serde_json::Value::Bool(false)));
        assert_eq!(check_result.get("threat_info"), Some(&serde_json::Value::Null));
    }
}

#[tokio::test]
async fn test_command_executor_threat_check_invalid_type() {
    let mut executor = CommandExecutor::new();

    let command = Commands::Threat {
        command: reasoner_cli::commands::ThreatCommands::Check {
            value: "192.168.1.100".to_string(),
            r#type: "invalid_type".to_string(),
        },
    };

    let result = executor.execute(command).await;
    assert!(result.is_err()); // Should fail due to invalid indicator type
}

#[tokio::test]
async fn test_command_executor_serve_command_structure() {
    let mut executor = CommandExecutor::new();

    let command = Commands::Serve {
        host: "127.0.0.1".to_string(),
        port: 8080,
    };

    // Note: This will attempt to start a server, but we expect it to fail gracefully
    // in a test environment (since we're not actually running an async runtime for server)
    let result = executor.execute(command).await;

    // The result depends on the server implementation, but it should not panic
    assert!(result.is_ok() || result.is_err()); // Just ensure it doesn't crash
}

#[tokio::test]
async fn test_command_executor_process_invalid_file() {
    let mut executor = CommandExecutor::new();

    let command = Commands::Process {
        input: PathBuf::from("nonexistent_file.json"),
        output: None,
        format: OutputFormat::Json,
    };

    let result = executor.execute(command).await;
    assert!(result.is_err()); // Should fail due to non-existent file
}

#[tokio::test]
async fn test_command_executor_process_valid_data() {
    use std::fs;
    use tempfile::NamedTempFile;

    let mut executor = CommandExecutor::new();

    // Create temporary input file with valid JSON
    let events_data = r#"[
        {
            "type": "NetworkConnection",
            "source_ip": "192.168.1.10",
            "dest_ip": "10.0.0.50",
            "port": 443,
            "protocol": "tcp",
            "timestamp": 1640995200
        }
    ]"#;

    let input_file = NamedTempFile::new().unwrap();
    fs::write(&input_file, events_data).unwrap();

    let command = Commands::Process {
        input: input_file.path().to_path_buf(),
        output: None,
        format: OutputFormat::Json,
    };

    let result = executor.execute(command).await.unwrap();

    assert!(result.success);
    assert!(result.message.contains("Processed 1 events"));
    assert!(result.data.is_some());

    // Cleanup
    drop(input_file);
}

#[test]
fn test_interactive_mode_parsing() {
    // Test that shell-words parsing works for interactive commands
    let input = "analyze --json '{\"type\": \"NetworkConnection\"}' --format json";
    let args_result = shell_words::split(input);

    assert!(args_result.is_ok());
    let args = args_result.unwrap();

    // Should parse into: ["analyze", "--json", "{\"type\": \"NetworkConnection\"}", "--format", "json"]
    assert_eq!(args.len(), 5);
    assert_eq!(args[0], "analyze");
    assert_eq!(args[1], "--json");
    assert_eq!(args[3], "--format");
    assert_eq!(args[4], "json");
}

#[test]
fn test_interactive_mode_complex_commands() {
    let test_cases = vec![
        "query --subject https://example.com/user/1",
        "threat check --value 192.168.1.100 --type ip",
        "process --input events.json --output results.json --format json-pretty",
        "analyze --file test.json --format text",
    ];

    for input in test_cases {
        let args_result = shell_words::split(input);
        assert!(args_result.is_ok(), "Failed to parse: {}", input);

        let args = args_result.unwrap();
        assert!(!args.is_empty(), "Empty args for: {}", input);
    }
}

#[tokio::test]
async fn test_command_executor_integration() {
    let mut executor = CommandExecutor::new();

    // Add an event
    let analyze_result = executor.execute(Commands::Analyze {
        file: None,
        json: Some(r#"{
            "type": "NetworkConnection",
            "source_ip": "192.168.1.10",
            "dest_ip": "10.0.0.50",
            "port": 443,
            "protocol": "tcp",
            "timestamp": 1640995200
        }"#.to_string()),
        format: OutputFormat::Json,
    }).await;

    assert!(analyze_result.is_ok());
    assert!(analyze_result.unwrap().success);

    // Query the graph - should now have triples
    let query_result = executor.execute(Commands::Query {
        subject: None,
        predicate: None,
        object: None,
        format: OutputFormat::Json,
    }).await;

    assert!(query_result.is_ok());
    let query_command_result = query_result.unwrap();
    assert!(query_command_result.success);
    assert!(query_command_result.message.contains("Found"));
    assert!(query_command_result.data.is_some());
}