mockforge-cli 0.3.0

CLI interface for MockForge
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
//! MockAI (Behavioral Mock Intelligence) CLI commands
//!
//! This module provides CLI commands for managing MockAI features including
//! learning from examples, generating rules from OpenAPI, and enabling
//! intelligent behavior for endpoints.

use chrono::{DateTime, Utc};
use clap::Subcommand;
use mockforge_core::intelligent_behavior::{
    openapi_generator::{OpenApiGenerationConfig, OpenApiSpecGenerator},
    rule_generator::{ExamplePair, RuleGenerator},
    IntelligentBehaviorConfig, MockAI,
};
use mockforge_core::OpenApiSpec;
use mockforge_recorder::{
    database::RecorderDatabase,
    openapi_export::{QueryFilters, RecordingsToOpenApi},
};
use serde_json::Value;
use std::path::PathBuf;

/// MockAI CLI commands
#[derive(Subcommand, Debug)]
pub enum MockAICommands {
    /// Learn behavioral rules from example payloads
    ///
    /// Analyzes example request/response pairs to automatically generate
    /// behavioral rules, validation rules, and state machines.
    ///
    /// Examples:
    ///   mockforge mockai learn --from-examples examples.json
    ///   mockforge mockai learn --from-examples examples.json --output rules.yaml
    Learn {
        /// Path to examples file (JSON or YAML)
        #[arg(long)]
        from_examples: Option<PathBuf>,
        /// Path to OpenAPI specification
        #[arg(long)]
        from_openapi: Option<PathBuf>,
        /// Output file for generated rules
        #[arg(short, long)]
        output: Option<PathBuf>,
        /// Enable verbose output
        #[arg(short, long)]
        verbose: bool,
    },

    /// Generate rules from OpenAPI specification
    ///
    /// Automatically generates behavioral rules from an OpenAPI spec by
    /// analyzing endpoints, schemas, and examples.
    ///
    /// Examples:
    ///   mockforge mockai generate --from-openapi api.yaml
    ///   mockforge mockai generate --from-openapi api.json --output rules.yaml
    Generate {
        /// Path to OpenAPI specification
        #[arg(long, required = true)]
        from_openapi: PathBuf,
        /// Output file for generated rules
        #[arg(short, long)]
        output: Option<PathBuf>,
        /// Enable verbose output
        #[arg(short, long)]
        verbose: bool,
    },

    /// Enable MockAI for specific endpoints
    ///
    /// Enables intelligent behavior for one or more endpoints. If no
    /// endpoints are specified, enables MockAI for all endpoints.
    ///
    /// Examples:
    ///   mockforge mockai enable --endpoint "/api/users"
    ///   mockforge mockai enable --endpoint "/api/users" --endpoint "/api/products"
    ///   mockforge mockai enable  # Enable for all endpoints
    Enable {
        /// Endpoint paths to enable MockAI for
        #[arg(long)]
        endpoint: Vec<String>,
        /// Configuration file to update
        #[arg(short, long)]
        config: Option<PathBuf>,
    },

    /// Disable MockAI for specific endpoints
    ///
    /// Disables intelligent behavior for specified endpoints.
    ///
    /// Examples:
    ///   mockforge mockai disable --endpoint "/api/users"
    ///   mockforge mockai disable  # Disable for all endpoints
    Disable {
        /// Endpoint paths to disable MockAI for
        #[arg(long)]
        endpoint: Vec<String>,
        /// Configuration file to update
        #[arg(short, long)]
        config: Option<PathBuf>,
    },

    /// Show MockAI status and configuration
    ///
    /// Displays current MockAI configuration and enabled endpoints.
    ///
    /// Examples:
    ///   mockforge mockai status
    ///   mockforge mockai status --config config.yaml
    Status {
        /// Configuration file to read
        #[arg(short, long)]
        config: Option<PathBuf>,
    },

    /// Generate OpenAPI specification from recorded traffic
    ///
    /// Analyzes recorded HTTP traffic and generates an OpenAPI 3.0 specification
    /// using pattern detection and optional LLM inference.
    ///
    /// Examples:
    ///   mockforge mockai generate-from-traffic --database ./recordings.db
    ///   mockforge mockai generate-from-traffic --database ./recordings.db --output openapi.yaml
    ///   mockforge mockai generate-from-traffic --database ./recordings.db --since 2025-01-01 --min-confidence 0.8
    GenerateFromTraffic {
        /// Path to recorder database (default: ./recordings.db)
        #[arg(long)]
        database: Option<PathBuf>,
        /// Output file for generated OpenAPI spec
        #[arg(short, long)]
        output: Option<PathBuf>,
        /// Start time for filtering (ISO 8601 format, e.g., 2025-01-01T00:00:00Z)
        #[arg(long)]
        since: Option<String>,
        /// End time for filtering (ISO 8601 format)
        #[arg(long)]
        until: Option<String>,
        /// Path pattern filter (supports wildcards, e.g., /api/*)
        #[arg(long)]
        path_pattern: Option<String>,
        /// Minimum confidence score for including paths (0.0 to 1.0)
        #[arg(long, default_value = "0.7")]
        min_confidence: f64,
        /// Enable verbose output
        #[arg(short, long)]
        verbose: bool,
    },
}

/// Handle MockAI CLI commands
pub async fn handle_mockai_command(
    command: MockAICommands,
) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
    match command {
        MockAICommands::Learn {
            from_examples,
            from_openapi,
            output,
            verbose,
        } => {
            handle_learn(from_examples, from_openapi, output, verbose).await?;
        }
        MockAICommands::Generate {
            from_openapi,
            output,
            verbose,
        } => {
            handle_generate(from_openapi, output, verbose).await?;
        }
        MockAICommands::Enable { endpoint, config } => {
            handle_enable(endpoint, config).await?;
        }
        MockAICommands::Disable { endpoint, config } => {
            handle_disable(endpoint, config).await?;
        }
        MockAICommands::Status { config } => {
            handle_status(config).await?;
        }
        MockAICommands::GenerateFromTraffic {
            database,
            output,
            since,
            until,
            path_pattern,
            min_confidence,
            verbose,
        } => {
            handle_generate_from_traffic(
                database,
                output,
                since,
                until,
                path_pattern,
                min_confidence,
                verbose,
            )
            .await?;
        }
    }

    Ok(())
}

/// Handle learn command
async fn handle_learn(
    from_examples: Option<PathBuf>,
    from_openapi: Option<PathBuf>,
    output: Option<PathBuf>,
    verbose: bool,
) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
    let config = IntelligentBehaviorConfig::default();

    let examples = if let Some(examples_path) = from_examples {
        // Load examples from file
        let content = tokio::fs::read_to_string(&examples_path).await?;
        let examples: Vec<ExamplePair> = if examples_path.extension().and_then(|s| s.to_str())
            == Some("yaml")
            || examples_path.extension().and_then(|s| s.to_str()) == Some("yml")
        {
            serde_yaml::from_str(&content)?
        } else {
            serde_json::from_str(&content)?
        };

        if verbose {
            println!("📚 Loaded {} examples from {:?}", examples.len(), examples_path);
        }

        examples
    } else if let Some(openapi_path) = from_openapi {
        // Load OpenAPI spec and extract examples
        let content = tokio::fs::read_to_string(&openapi_path).await?;
        let spec_json: Value = if openapi_path.extension().and_then(|s| s.to_str()) == Some("yaml")
            || openapi_path.extension().and_then(|s| s.to_str()) == Some("yml")
        {
            serde_yaml::from_str(&content)?
        } else {
            serde_json::from_str(&content)?
        };

        let spec = OpenApiSpec::from_json(spec_json)?;
        let examples = MockAI::extract_examples_from_openapi(&spec)?;

        if verbose {
            println!("📚 Extracted {} examples from OpenAPI spec", examples.len());
        }

        examples
    } else {
        return Err("Either --from-examples or --from-openapi must be specified".into());
    };

    // Generate rules
    let rule_generator = RuleGenerator::new(config.behavior_model.clone());
    let rules = rule_generator.generate_rules_from_examples(examples).await?;

    if verbose {
        println!("✅ Generated {} consistency rules", rules.consistency_rules.len());
        println!("✅ Generated {} schemas", rules.schemas.len());
        println!("✅ Generated {} state machines", rules.state_transitions.len());
    }

    // Output rules
    if let Some(output_path) = output {
        let output_content = if output_path.extension().and_then(|s| s.to_str()) == Some("yaml")
            || output_path.extension().and_then(|s| s.to_str()) == Some("yml")
        {
            serde_yaml::to_string(&rules)?
        } else {
            serde_json::to_string_pretty(&rules)?
        };

        tokio::fs::write(&output_path, output_content).await?;
        println!("💾 Saved rules to {:?}", output_path);
    } else {
        // Print to stdout
        let output_content = serde_json::to_string_pretty(&rules)?;
        println!("{}", output_content);
    }

    Ok(())
}

/// Handle generate command
async fn handle_generate(
    from_openapi: PathBuf,
    output: Option<PathBuf>,
    verbose: bool,
) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
    // Load OpenAPI spec
    let content = tokio::fs::read_to_string(&from_openapi).await?;
    let spec_json: Value = if from_openapi.extension().and_then(|s| s.to_str()) == Some("yaml")
        || from_openapi.extension().and_then(|s| s.to_str()) == Some("yml")
    {
        serde_yaml::from_str(&content)?
    } else {
        serde_json::from_str(&content)?
    };

    let spec = OpenApiSpec::from_json(spec_json)?;

    if verbose {
        println!("📋 Loaded OpenAPI specification: {}", spec.title());
    }

    // Generate MockAI from OpenAPI
    let config = IntelligentBehaviorConfig::default();
    let mockai = MockAI::from_openapi(&spec, config).await?;

    if verbose {
        println!("✅ Generated behavioral rules");
        println!("   - {} consistency rules", mockai.rules().consistency_rules.len());
        println!("   - {} schemas", mockai.rules().schemas.len());
        println!("   - {} state machines", mockai.rules().state_transitions.len());
    }

    // Output rules
    if let Some(output_path) = output {
        let output_content = if output_path.extension().and_then(|s| s.to_str()) == Some("yaml")
            || output_path.extension().and_then(|s| s.to_str()) == Some("yml")
        {
            serde_yaml::to_string(mockai.rules())?
        } else {
            serde_json::to_string_pretty(mockai.rules())?
        };

        tokio::fs::write(&output_path, output_content).await?;
        println!("💾 Saved rules to {:?}", output_path);
    } else {
        // Print to stdout
        let output_content = serde_json::to_string_pretty(mockai.rules())?;
        println!("{}", output_content);
    }

    Ok(())
}

/// Handle enable command
async fn handle_enable(
    endpoints: Vec<String>,
    config_path: Option<PathBuf>,
) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
    let config_path = if let Some(path) = config_path {
        path
    } else {
        // Try to discover config file (synchronous fallback)
        match std::env::current_dir() {
            Ok(current_dir) => {
                let possible_paths = vec![
                    current_dir.join("mockforge.yaml"),
                    current_dir.join("mockforge.yml"),
                    current_dir.join(".mockforge.yaml"),
                ];
                possible_paths.into_iter().find(|p| p.exists()).ok_or_else(|| {
                    "No configuration file found. Specify --config or create mockforge.yaml"
                })?
            }
            Err(_) => {
                return Err(
                    "No configuration file found. Specify --config or create mockforge.yaml".into(),
                );
            }
        }
    };

    // Load config
    let mut config = mockforge_core::config::load_config_auto(&config_path).await?;

    // Enable MockAI
    config.mockai.enabled = true;

    // Add endpoints to enabled list if specified
    let endpoint_count = endpoints.len();
    if !endpoints.is_empty() {
        config.mockai.enabled_endpoints.extend(endpoints);
    }

    // Save config
    mockforge_core::config::save_config(&config_path, &config).await?;

    if endpoint_count == 0 {
        println!("✅ Enabled MockAI for all endpoints");
    } else {
        println!("✅ Enabled MockAI for {} endpoint(s)", endpoint_count);
    }

    Ok(())
}

/// Handle disable command
async fn handle_disable(
    endpoints: Vec<String>,
    config_path: Option<PathBuf>,
) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
    let config_path = if let Some(path) = config_path {
        path
    } else {
        // Try to discover config file (synchronous fallback)
        match std::env::current_dir() {
            Ok(current_dir) => {
                let possible_paths = vec![
                    current_dir.join("mockforge.yaml"),
                    current_dir.join("mockforge.yml"),
                    current_dir.join(".mockforge.yaml"),
                ];
                possible_paths.into_iter().find(|p| p.exists()).ok_or_else(|| {
                    "No configuration file found. Specify --config or create mockforge.yaml"
                })?
            }
            Err(_) => {
                return Err(
                    "No configuration file found. Specify --config or create mockforge.yaml".into(),
                );
            }
        }
    };

    // Load config
    let mut config = mockforge_core::config::load_config_auto(&config_path).await?;

    let endpoint_count = endpoints.len();
    if endpoints.is_empty() {
        // Disable for all endpoints
        config.mockai.enabled = false;
        config.mockai.enabled_endpoints.clear();
        println!("✅ Disabled MockAI for all endpoints");
    } else {
        // Remove specified endpoints
        for endpoint in &endpoints {
            config.mockai.enabled_endpoints.retain(|e| e != endpoint);
        }
        println!("✅ Disabled MockAI for {} endpoint(s)", endpoint_count);
    }

    // Save config
    mockforge_core::config::save_config(&config_path, &config).await?;

    Ok(())
}

/// Handle status command
async fn handle_status(
    config_path: Option<PathBuf>,
) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
    let config_path = if let Some(path) = config_path {
        path
    } else {
        // Try to discover config file (synchronous fallback)
        match std::env::current_dir() {
            Ok(current_dir) => {
                let possible_paths = vec![
                    current_dir.join("mockforge.yaml"),
                    current_dir.join("mockforge.yml"),
                    current_dir.join(".mockforge.yaml"),
                ];
                possible_paths.into_iter().find(|p| p.exists()).ok_or_else(|| {
                    "No configuration file found. Specify --config or create mockforge.yaml"
                })?
            }
            Err(_) => {
                return Err(
                    "No configuration file found. Specify --config or create mockforge.yaml".into(),
                );
            }
        }
    };

    // Load config
    let config = mockforge_core::config::load_config_auto(&config_path).await?;

    println!("📊 MockAI Status");
    println!("   Enabled: {}", config.mockai.enabled);
    println!("   Auto-learn: {}", config.mockai.auto_learn);
    println!("   Mutation detection: {}", config.mockai.mutation_detection);
    println!("   AI validation errors: {}", config.mockai.ai_validation_errors);
    println!("   Intelligent pagination: {}", config.mockai.intelligent_pagination);

    if config.mockai.enabled_endpoints.is_empty() {
        println!("   Endpoints: All endpoints");
    } else {
        println!("   Endpoints: {} specific endpoint(s)", config.mockai.enabled_endpoints.len());
        for endpoint in &config.mockai.enabled_endpoints {
            println!("     - {}", endpoint);
        }
    }

    Ok(())
}

/// Handle generate-from-traffic command
async fn handle_generate_from_traffic(
    database: Option<PathBuf>,
    output: Option<PathBuf>,
    since: Option<String>,
    until: Option<String>,
    path_pattern: Option<String>,
    min_confidence: f64,
    verbose: bool,
) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
    // Determine database path
    let db_path = database.unwrap_or_else(|| {
        std::env::current_dir()
            .unwrap_or_else(|_| PathBuf::from("."))
            .join("recordings.db")
    });

    if verbose {
        println!("📂 Using recorder database: {:?}", db_path);
    }

    // Open database
    let db = RecorderDatabase::new(&db_path).await?;

    // Parse time filters
    let since_dt = if let Some(ref since_str) = since {
        Some(
            DateTime::parse_from_rfc3339(since_str)
                .map_err(|e| format!("Invalid --since format: {}. Use ISO 8601 format (e.g., 2025-01-01T00:00:00Z)", e))?
                .with_timezone(&Utc),
        )
    } else {
        None
    };

    let until_dt = if let Some(ref until_str) = until {
        Some(
            DateTime::parse_from_rfc3339(until_str)
                .map_err(|e| format!("Invalid --until format: {}. Use ISO 8601 format (e.g., 2025-01-01T00:00:00Z)", e))?
                .with_timezone(&Utc),
        )
    } else {
        None
    };

    // Build query filters
    let query_filters = QueryFilters {
        since: since_dt,
        until: until_dt,
        path_pattern,
        min_status_code: None,
        max_requests: Some(1000),
    };

    // Query HTTP exchanges
    if verbose {
        println!("🔍 Querying recorded HTTP traffic...");
    }

    let exchanges = RecordingsToOpenApi::query_http_exchanges(&db, Some(query_filters)).await?;

    if exchanges.is_empty() {
        return Err("No HTTP exchanges found matching the specified filters".into());
    }

    if verbose {
        println!("📊 Found {} HTTP exchanges", exchanges.len());
    }

    // Create OpenAPI generator config
    let behavior_config = IntelligentBehaviorConfig::default();
    let gen_config = OpenApiGenerationConfig {
        min_confidence,
        behavior_model: Some(behavior_config.behavior_model),
    };

    // Generate OpenAPI spec
    if verbose {
        println!("🤖 Generating OpenAPI specification...");
    }

    let generator = OpenApiSpecGenerator::new(gen_config);
    let result = generator.generate_from_exchanges(exchanges).await?;

    if verbose {
        println!("✅ Generated OpenAPI specification");
        println!("   - Requests analyzed: {}", result.metadata.requests_analyzed);
        println!("   - Paths inferred: {}", result.metadata.paths_inferred);
        println!("   - Generation time: {}ms", result.metadata.duration_ms);
        println!("\n📈 Confidence Scores:");
        for (path, score) in &result.metadata.path_confidence {
            if score.value >= min_confidence {
                println!("   - {}: {:.2} - {}", path, score.value, score.reason);
            }
        }
    }

    // Output spec
    // Use raw_document if available, otherwise serialize the spec
    let spec_json = if let Some(ref raw) = result.spec.raw_document {
        raw.clone()
    } else {
        serde_json::to_value(&result.spec.spec)?
    };

    let output_content = if let Some(ref output_path) = output {
        let is_yaml = output_path
            .extension()
            .and_then(|s| s.to_str())
            .map(|s| s == "yaml" || s == "yml")
            .unwrap_or(false);

        if is_yaml {
            serde_yaml::to_string(&spec_json)?
        } else {
            serde_json::to_string_pretty(&spec_json)?
        }
    } else {
        // Default to JSON for stdout
        serde_json::to_string_pretty(&spec_json)?
    };

    if let Some(output_path) = output {
        tokio::fs::write(&output_path, output_content).await?;
        println!("💾 Saved OpenAPI specification to {:?}", output_path);
    } else {
        // Print to stdout
        println!("{}", output_content);
    }

    Ok(())
}