mockforge-cli 0.3.118

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
//! HTTP Fixture Validation Tool
//!
//! Standalone tool for validating HTTP fixtures in a directory.
//! Can validate individual files or entire directories.

use anyhow::{bail, Context, Result};
use mockforge_openapi::custom_fixture::CustomFixture;
use serde::Deserialize;
use serde_json::Value;
use std::collections::HashMap;
use std::path::{Path, PathBuf};
use tokio::fs;

/// Validation result for a single fixture
#[derive(Debug)]
pub struct ValidationResult {
    pub file: PathBuf,
    pub valid: bool,
    pub error: Option<String>,
    pub format: FixtureFormat,
}

/// Format of the fixture
#[derive(Debug, Clone, Copy)]
pub enum FixtureFormat {
    Flat,
    Nested,
    Invalid,
}

/// Validate a single fixture file
pub async fn validate_file(file_path: &Path) -> Result<ValidationResult> {
    let content = fs::read_to_string(file_path)
        .await
        .with_context(|| format!("Failed to read fixture file: {}", file_path.display()))?;

    // Check if it's a template file
    if should_skip_file(&content) {
        return Ok(ValidationResult {
            file: file_path.to_path_buf(),
            valid: false,
            error: Some("Template file (contains _comment, _usage, or scenario field)".to_string()),
            format: FixtureFormat::Invalid,
        });
    }

    // Try to parse as flat format
    match serde_json::from_str::<CustomFixture>(&content) {
        Ok(mut fixture) => {
            // Normalize path
            fixture.path = normalize_path(&fixture.path);

            // Validate
            match validate_fixture(&fixture, file_path) {
                Ok(_) => Ok(ValidationResult {
                    file: file_path.to_path_buf(),
                    valid: true,
                    error: None,
                    format: FixtureFormat::Flat,
                }),
                Err(e) => Ok(ValidationResult {
                    file: file_path.to_path_buf(),
                    valid: false,
                    error: Some(e.to_string()),
                    format: FixtureFormat::Flat,
                }),
            }
        }
        Err(_) => {
            // Try nested format
            match serde_json::from_str::<NestedFixture>(&content) {
                Ok(nested) => match convert_nested_to_flat(nested) {
                    Ok(fixture) => match validate_fixture(&fixture, file_path) {
                        Ok(_) => Ok(ValidationResult {
                            file: file_path.to_path_buf(),
                            valid: true,
                            error: None,
                            format: FixtureFormat::Nested,
                        }),
                        Err(e) => Ok(ValidationResult {
                            file: file_path.to_path_buf(),
                            valid: false,
                            error: Some(e.to_string()),
                            format: FixtureFormat::Nested,
                        }),
                    },
                    Err(e) => Ok(ValidationResult {
                        file: file_path.to_path_buf(),
                        valid: false,
                        error: Some(e.to_string()),
                        format: FixtureFormat::Nested,
                    }),
                },
                Err(e) => Ok(ValidationResult {
                    file: file_path.to_path_buf(),
                    valid: false,
                    error: Some(format!("Invalid JSON or fixture format: {}", e)),
                    format: FixtureFormat::Invalid,
                }),
            }
        }
    }
}

/// Validate all fixtures in a directory
pub async fn validate_directory(dir_path: &Path) -> Result<Vec<ValidationResult>> {
    let mut results = Vec::new();

    if !dir_path.exists() {
        anyhow::bail!("Directory does not exist: {}", dir_path.display());
    }

    if !dir_path.is_dir() {
        anyhow::bail!("Path is not a directory: {}", dir_path.display());
    }

    let mut entries = fs::read_dir(dir_path)
        .await
        .with_context(|| format!("Failed to read directory: {}", dir_path.display()))?;

    while let Some(entry) = entries.next_entry().await? {
        let path = entry.path();
        if path.is_file() && path.extension().and_then(|s| s.to_str()) == Some("json") {
            let result = validate_file(&path).await?;
            results.push(result);
        }
    }

    Ok(results)
}

// Helper functions (duplicated from CustomFixtureLoader for standalone use)
fn should_skip_file(content: &str) -> bool {
    // Check for template indicators
    if content.contains("\"_comment\"") || content.contains("\"_usage\"") {
        return true;
    }

    // Check if it's a scenario/config file (not a fixture)
    if content.contains("\"scenario\"") || content.contains("\"presentation_mode\"") {
        return true;
    }

    false
}

fn normalize_path(path: &str) -> String {
    let mut normalized = path.trim().to_string();

    // Strip query string if present (query strings are handled separately)
    if let Some(query_start) = normalized.find('?') {
        normalized = normalized[..query_start].to_string();
    }

    // Collapse multiple slashes into one
    while normalized.contains("//") {
        normalized = normalized.replace("//", "/");
    }

    // Remove trailing slash (except for root path)
    if normalized.len() > 1 && normalized.ends_with('/') {
        normalized.pop();
    }

    // Ensure path starts with /
    if !normalized.starts_with('/') {
        normalized = format!("/{}", normalized);
    }

    normalized
}

fn validate_fixture(fixture: &CustomFixture, _file_path: &Path) -> Result<()> {
    // Check required fields
    if fixture.method.is_empty() {
        bail!("method is required and cannot be empty");
    }

    if fixture.path.is_empty() {
        bail!("path is required and cannot be empty");
    }

    // Validate HTTP method
    let method_upper = fixture.method.to_uppercase();
    let valid_methods = [
        "GET", "POST", "PUT", "PATCH", "DELETE", "HEAD", "OPTIONS", "TRACE",
    ];
    if !valid_methods.contains(&method_upper.as_str()) {
        // Warn but don't fail
    }

    // Validate status code
    if fixture.status < 100 || fixture.status >= 600 {
        bail!("status code {} is not a valid HTTP status code (100-599)", fixture.status);
    }

    Ok(())
}

fn convert_nested_to_flat(nested: NestedFixture) -> Result<CustomFixture> {
    let request = nested
        .request
        .ok_or_else(|| anyhow::anyhow!("Nested fixture missing 'request' object"))?;

    let response = nested
        .response
        .ok_or_else(|| anyhow::anyhow!("Nested fixture missing 'response' object"))?;

    Ok(CustomFixture {
        method: request.method,
        path: normalize_path(&request.path),
        status: response.status,
        response: response.body,
        headers: response.headers,
        delay_ms: 0,
    })
}

// Nested fixture types for parsing
#[derive(Debug, Deserialize)]
struct NestedFixture {
    request: Option<NestedRequest>,
    response: Option<NestedResponse>,
}

#[derive(Debug, Deserialize)]
struct NestedRequest {
    method: String,
    path: String,
}

#[derive(Debug, Deserialize)]
struct NestedResponse {
    status: u16,
    #[serde(default)]
    headers: HashMap<String, String>,
    body: Value,
}

/// Print validation results in a formatted way
pub fn print_results(results: &[ValidationResult], verbose: bool) {
    let valid_count = results.iter().filter(|r| r.valid).count();
    let invalid_count = results.len() - valid_count;
    let flat_count = results.iter().filter(|r| matches!(r.format, FixtureFormat::Flat)).count();
    let nested_count = results.iter().filter(|r| matches!(r.format, FixtureFormat::Nested)).count();

    println!("\n📊 Validation Summary");
    println!("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━");
    println!("Total files: {}", results.len());
    println!("✅ Valid: {}", valid_count);
    println!("❌ Invalid: {}", invalid_count);
    println!("📄 Flat format: {}", flat_count);
    println!("📦 Nested format: {}", nested_count);

    if invalid_count > 0 {
        println!("\n❌ Invalid Fixtures:");
        println!("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━");
        for result in results.iter().filter(|r| !r.valid) {
            println!("\n  File: {}", result.file.display());
            if let Some(ref error) = result.error {
                println!("  Error: {}", error);
            }
        }
    }

    if verbose && valid_count > 0 {
        println!("\n✅ Valid Fixtures:");
        println!("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━");
        for result in results.iter().filter(|r| r.valid) {
            let format_str = match result.format {
                FixtureFormat::Flat => "flat",
                FixtureFormat::Nested => "nested",
                FixtureFormat::Invalid => "invalid",
            };
            println!("  {} ({})", result.file.display(), format_str);
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use tempfile::TempDir;

    // FixtureFormat tests
    #[test]
    fn test_fixture_format_debug() {
        let format = FixtureFormat::Flat;
        let debug = format!("{:?}", format);
        assert!(debug.contains("Flat"));
    }

    #[test]
    fn test_fixture_format_clone() {
        let format = FixtureFormat::Nested;
        let cloned = format;
        assert!(matches!(cloned, FixtureFormat::Nested));
    }

    // normalize_path tests
    #[test]
    fn test_normalize_path_simple() {
        assert_eq!(normalize_path("/api/users"), "/api/users");
    }

    #[test]
    fn test_normalize_path_adds_leading_slash() {
        assert_eq!(normalize_path("api/users"), "/api/users");
    }

    #[test]
    fn test_normalize_path_removes_trailing_slash() {
        assert_eq!(normalize_path("/api/users/"), "/api/users");
    }

    #[test]
    fn test_normalize_path_root() {
        assert_eq!(normalize_path("/"), "/");
    }

    #[test]
    fn test_normalize_path_collapses_double_slashes() {
        assert_eq!(normalize_path("/api//users"), "/api/users");
        assert_eq!(normalize_path("/api///users"), "/api/users");
    }

    #[test]
    fn test_normalize_path_strips_query_string() {
        assert_eq!(normalize_path("/api/users?page=1"), "/api/users");
        assert_eq!(normalize_path("/api/users?page=1&limit=10"), "/api/users");
    }

    #[test]
    fn test_normalize_path_trims_whitespace() {
        assert_eq!(normalize_path("  /api/users  "), "/api/users");
    }

    #[test]
    fn test_normalize_path_empty() {
        assert_eq!(normalize_path(""), "/");
    }

    // should_skip_file tests
    #[test]
    fn test_should_skip_file_with_comment() {
        let content = r#"{"_comment": "This is a template", "method": "GET"}"#;
        assert!(should_skip_file(content));
    }

    #[test]
    fn test_should_skip_file_with_usage() {
        let content = r#"{"_usage": "Example usage", "method": "GET"}"#;
        assert!(should_skip_file(content));
    }

    #[test]
    fn test_should_skip_file_with_scenario() {
        let content = r#"{"scenario": "test-scenario", "steps": []}"#;
        assert!(should_skip_file(content));
    }

    #[test]
    fn test_should_skip_file_with_presentation_mode() {
        let content = r#"{"presentation_mode": true, "slides": []}"#;
        assert!(should_skip_file(content));
    }

    #[test]
    fn test_should_skip_file_normal_fixture() {
        let content = r#"{"method": "GET", "path": "/api/users", "status": 200}"#;
        assert!(!should_skip_file(content));
    }

    // validate_file tests
    #[tokio::test]
    async fn test_validate_file_valid_flat_fixture() {
        let temp_dir = TempDir::new().unwrap();
        let file_path = temp_dir.path().join("fixture.json");
        let fixture = r#"{
            "method": "GET",
            "path": "/api/users",
            "status": 200,
            "response": {"users": []}
        }"#;
        std::fs::write(&file_path, fixture).unwrap();

        let result = validate_file(&file_path).await.unwrap();
        assert!(result.valid);
        assert!(matches!(result.format, FixtureFormat::Flat));
    }

    #[tokio::test]
    async fn test_validate_file_valid_nested_fixture() {
        let temp_dir = TempDir::new().unwrap();
        let file_path = temp_dir.path().join("fixture.json");
        let fixture = r#"{
            "request": {
                "method": "POST",
                "path": "/api/users"
            },
            "response": {
                "status": 201,
                "body": {"id": 1}
            }
        }"#;
        std::fs::write(&file_path, fixture).unwrap();

        let result = validate_file(&file_path).await.unwrap();
        assert!(result.valid);
        assert!(matches!(result.format, FixtureFormat::Nested));
    }

    #[tokio::test]
    async fn test_validate_file_invalid_json() {
        let temp_dir = TempDir::new().unwrap();
        let file_path = temp_dir.path().join("fixture.json");
        std::fs::write(&file_path, "{ invalid json }").unwrap();

        let result = validate_file(&file_path).await.unwrap();
        assert!(!result.valid);
        assert!(result.error.is_some());
        assert!(matches!(result.format, FixtureFormat::Invalid));
    }

    #[tokio::test]
    async fn test_validate_file_template_file() {
        let temp_dir = TempDir::new().unwrap();
        let file_path = temp_dir.path().join("template.json");
        let template = r#"{"_comment": "Template file", "method": "GET"}"#;
        std::fs::write(&file_path, template).unwrap();

        let result = validate_file(&file_path).await.unwrap();
        assert!(!result.valid);
        assert!(result.error.unwrap().contains("Template file"));
    }

    #[tokio::test]
    async fn test_validate_file_missing_method() {
        let temp_dir = TempDir::new().unwrap();
        let file_path = temp_dir.path().join("fixture.json");
        let fixture = r#"{
            "path": "/api/users",
            "status": 200,
            "response": {}
        }"#;
        std::fs::write(&file_path, fixture).unwrap();

        let result = validate_file(&file_path).await.unwrap();
        // May fail during parsing or validation
        // Just ensure it handles gracefully
        assert!(result.valid || result.error.is_some());
    }

    #[tokio::test]
    async fn test_validate_file_invalid_status_code() {
        let temp_dir = TempDir::new().unwrap();
        let file_path = temp_dir.path().join("fixture.json");
        let fixture = r#"{
            "method": "GET",
            "path": "/api/users",
            "status": 999,
            "response": {}
        }"#;
        std::fs::write(&file_path, fixture).unwrap();

        let result = validate_file(&file_path).await.unwrap();
        assert!(!result.valid);
        assert!(result.error.unwrap().contains("status code"));
    }

    #[tokio::test]
    async fn test_validate_file_status_code_too_low() {
        let temp_dir = TempDir::new().unwrap();
        let file_path = temp_dir.path().join("fixture.json");
        let fixture = r#"{
            "method": "GET",
            "path": "/api/users",
            "status": 50,
            "response": {}
        }"#;
        std::fs::write(&file_path, fixture).unwrap();

        let result = validate_file(&file_path).await.unwrap();
        assert!(!result.valid);
    }

    // validate_directory tests
    #[tokio::test]
    async fn test_validate_directory_empty() {
        let temp_dir = TempDir::new().unwrap();
        let results = validate_directory(temp_dir.path()).await.unwrap();
        assert!(results.is_empty());
    }

    #[tokio::test]
    async fn test_validate_directory_with_fixtures() {
        let temp_dir = TempDir::new().unwrap();

        // Create valid fixture
        let valid_fixture = r#"{
            "method": "GET",
            "path": "/api/users",
            "status": 200,
            "response": []
        }"#;
        std::fs::write(temp_dir.path().join("valid.json"), valid_fixture).unwrap();

        // Create invalid fixture
        std::fs::write(temp_dir.path().join("invalid.json"), "{ bad json }").unwrap();

        let results = validate_directory(temp_dir.path()).await.unwrap();
        assert_eq!(results.len(), 2);

        let valid_count = results.iter().filter(|r| r.valid).count();
        let invalid_count = results.iter().filter(|r| !r.valid).count();
        assert_eq!(valid_count, 1);
        assert_eq!(invalid_count, 1);
    }

    #[tokio::test]
    async fn test_validate_directory_ignores_non_json() {
        let temp_dir = TempDir::new().unwrap();

        // Create JSON fixture
        let fixture = r#"{"method": "GET", "path": "/api", "status": 200, "response": {}}"#;
        std::fs::write(temp_dir.path().join("fixture.json"), fixture).unwrap();

        // Create non-JSON file
        std::fs::write(temp_dir.path().join("readme.txt"), "This is not JSON").unwrap();

        let results = validate_directory(temp_dir.path()).await.unwrap();
        assert_eq!(results.len(), 1);
    }

    #[tokio::test]
    async fn test_validate_directory_not_exists() {
        let result = validate_directory(Path::new("/nonexistent/path")).await;
        assert!(result.is_err());
    }

    #[tokio::test]
    async fn test_validate_directory_is_file() {
        let temp_dir = TempDir::new().unwrap();
        let file_path = temp_dir.path().join("file.txt");
        std::fs::write(&file_path, "content").unwrap();

        let result = validate_directory(&file_path).await;
        assert!(result.is_err());
    }

    // ValidationResult tests
    #[test]
    fn test_validation_result_debug() {
        let result = ValidationResult {
            file: PathBuf::from("test.json"),
            valid: true,
            error: None,
            format: FixtureFormat::Flat,
        };
        let debug = format!("{:?}", result);
        assert!(debug.contains("ValidationResult"));
        assert!(debug.contains("test.json"));
    }

    // print_results tests (just ensure no panic)
    #[test]
    fn test_print_results_empty() {
        let results: Vec<ValidationResult> = vec![];
        print_results(&results, false);
    }

    #[test]
    fn test_print_results_with_valid() {
        let results = vec![ValidationResult {
            file: PathBuf::from("test.json"),
            valid: true,
            error: None,
            format: FixtureFormat::Flat,
        }];
        print_results(&results, true);
    }

    #[test]
    fn test_print_results_with_invalid() {
        let results = vec![ValidationResult {
            file: PathBuf::from("test.json"),
            valid: false,
            error: Some("Test error".to_string()),
            format: FixtureFormat::Invalid,
        }];
        print_results(&results, false);
    }

    #[test]
    fn test_print_results_mixed() {
        let results = vec![
            ValidationResult {
                file: PathBuf::from("valid.json"),
                valid: true,
                error: None,
                format: FixtureFormat::Flat,
            },
            ValidationResult {
                file: PathBuf::from("nested.json"),
                valid: true,
                error: None,
                format: FixtureFormat::Nested,
            },
            ValidationResult {
                file: PathBuf::from("invalid.json"),
                valid: false,
                error: Some("Parse error".to_string()),
                format: FixtureFormat::Invalid,
            },
        ];
        print_results(&results, true);
    }
}