fraiseql-cli 2.0.0-rc.13

CLI tools for FraiseQL v2 - Schema compilation and development utilities
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
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
//! Advanced CLI feature tests
//! - YAML configuration file parsing
//! - Output format options (JSON, GraphQL SDL)
//! - Validation error reporting with suggestions
//! - Large-scale composition handling
//! - Incremental composition support
//! - Performance validation

use std::fmt::Write;

// ============================================================================
// Test: Configuration File Parsing
// ============================================================================

#[test]
fn test_parse_yaml_configuration_basic() {
    // TEST: Parse basic fraiseql.yml configuration
    // GIVEN: fraiseql.yml with basic settings
    // WHEN: Parsing configuration
    // THEN: Should extract all settings

    let yaml_content = r"
composition:
  conflict_resolution: error
  validation: true
";

    let result = parse_config_yaml(yaml_content);
    assert!(result.is_ok(), "Should parse basic YAML config");

    let config = result.unwrap();
    assert_eq!(config.conflict_resolution, "error");
    assert!(config.validation);
}

#[test]
fn test_parse_yaml_configuration_with_priority() {
    // TEST: Parse configuration with subgraph priority
    // GIVEN: fraiseql.yml with conflict_resolution and priority
    // WHEN: Parsing
    // THEN: Should extract priority ordering

    let yaml_content = r"
composition:
  conflict_resolution: first_wins
  validation: true
  subgraph_priority:
    - users
    - orders
    - products
";

    let result = parse_config_yaml(yaml_content);
    assert!(result.is_ok(), "Should parse YAML with priority");

    let config = result.unwrap();
    assert_eq!(config.conflict_resolution, "first_wins");
    assert_eq!(config.subgraph_priority.len(), 3);
    assert_eq!(config.subgraph_priority[0], "users");
}

#[test]
fn test_parse_yaml_configuration_all_strategies() {
    // TEST: Parse YAML with all conflict resolution strategies
    // GIVEN: YAML with different strategies
    // WHEN: Parsing each variant
    // THEN: Should correctly identify strategy

    for strategy in &["error", "first_wins", "shareable"] {
        let yaml_content = format!(
            r"
composition:
  conflict_resolution: {}
  validation: true
",
            strategy
        );

        let result = parse_config_yaml(&yaml_content);
        assert!(result.is_ok(), "Should parse {} strategy", strategy);

        let config = result.unwrap();
        assert_eq!(&config.conflict_resolution, strategy);
    }
}

// ============================================================================
// Test: Output Format Options
// ============================================================================

#[test]
fn test_format_output_json() {
    // TEST: Format composed schema as JSON
    // GIVEN: Composed schema
    // WHEN: Formatting as JSON
    // THEN: Should produce valid JSON output

    let schema_json = r#"{
        "enabled": true,
        "version": "v2",
        "types": [
            {"name": "User", "keys": [{"fields": ["id"]}]}
        ]
    }"#;

    let result = format_composed_schema(schema_json, "json");
    assert!(result.is_ok(), "Should format as JSON");

    let formatted = result.unwrap();
    assert!(formatted.contains("\"enabled\""));
    assert!(formatted.contains("\"version\""));
}

#[test]
fn test_format_output_graphql_sdl() {
    // TEST: Format composed schema as GraphQL SDL
    // GIVEN: Composed schema
    // WHEN: Formatting as GraphQL SDL
    // THEN: Should produce valid SDL output

    let schema_json = r#"{
        "enabled": true,
        "version": "v2",
        "types": [
            {"name": "User", "keys": [{"fields": ["id"]}]}
        ]
    }"#;

    let result = format_composed_schema(schema_json, "graphql");
    assert!(result.is_ok(), "Should format as GraphQL SDL");

    let formatted = result.unwrap();
    // Should contain GraphQL-like syntax
    assert!(formatted.contains("User") || formatted.contains("type"));
}

#[test]
fn test_invalid_output_format() {
    // TEST: Reject invalid output format
    // GIVEN: Invalid format option
    // WHEN: Formatting
    // THEN: Should error with helpful message

    let schema_json = r#"{"enabled": true}"#;

    let result = format_composed_schema(schema_json, "invalid-format");
    assert!(result.is_err(), "Should reject invalid output format");

    let err = result.unwrap_err();
    assert!(
        err.to_lowercase().contains("format") || err.to_lowercase().contains("invalid"),
        "Error should mention format: {}",
        err
    );
}

// ============================================================================
// Test: Validation Error Reporting
// ============================================================================

#[test]
fn test_validation_error_with_suggestion() {
    // TEST: Validation error includes actionable suggestion
    // GIVEN: Schema with validation error
    // WHEN: Validating composition
    // THEN: Error should include suggestion for fix

    let error_message = create_validation_error(
        "Type conflict",
        "User type defined in multiple subgraphs",
        Some("Mark one definition with @extends, or use shareable resolution strategy"),
    );

    assert!(error_message.contains("Type conflict"));
    assert!(error_message.contains("User type defined"));
    assert!(error_message.contains("Mark one definition") || error_message.contains("Suggestion"));
}

#[test]
fn test_validation_errors_batch_reporting() {
    // TEST: Multiple validation errors reported together
    // GIVEN: Schema with multiple validation errors
    // WHEN: Validating
    // THEN: Should report all errors with details

    let errors = vec![
        ("User", "Multiple definitions", None),
        ("Order", "Missing @key directive", Some("Add @key directive")),
        ("Product", "Circular dependency", Some("Remove cycle")),
    ];

    let report = format_validation_errors(&errors);
    assert!(report.contains("3 error"));
    assert!(report.contains("User"));
    assert!(report.contains("Order"));
    assert!(report.contains("Product"));
}

// ============================================================================
// Test: Large-Scale Composition Handling
// ============================================================================

#[test]
fn test_compose_many_subgraphs_performance() {
    // TEST: Handle composition of many subgraphs (20+)
    // GIVEN: 20 subgraphs with various types
    // WHEN: Composing
    // THEN: Should complete successfully and efficiently

    let mut subgraph_names = Vec::new();
    for i in 0..20 {
        subgraph_names.push(format!("service-{}", i));
    }

    let start = std::time::Instant::now();
    let result = validate_many_subgraphs(&subgraph_names);
    let duration = start.elapsed();

    assert!(result.is_ok(), "Should handle 20 subgraphs: {:?}", result);
    // Should complete in reasonable time (< 1s for 20 subgraphs)
    assert!(
        duration.as_secs() < 1,
        "Should complete 20 subgraphs in < 1s, took {:?}",
        duration
    );
}

#[test]
fn test_compose_with_many_types() {
    // TEST: Handle composition with 100+ types
    // GIVEN: Composition with many types
    // WHEN: Composing
    // THEN: Should handle all types without errors

    let type_count = 100;
    let result = validate_many_types(type_count);
    assert!(result.is_ok(), "Should handle {} types", type_count);
}

// ============================================================================
// Test: Incremental Composition
// ============================================================================

#[test]
fn test_incremental_composition_add_subgraph() {
    // TEST: Add a new subgraph to existing supergraph
    // GIVEN: Existing composed supergraph, new subgraph to add
    // WHEN: Performing incremental composition
    // THEN: Should merge new subgraph without recreating all

    let existing_supergraph = ComposedSupergraph {
        types:   vec!["User".to_string(), "Order".to_string()],
        version: "v2".to_string(),
    };

    let new_subgraph_types = vec!["Product".to_string(), "User".to_string()]; // User extends

    let result = incremental_compose(&existing_supergraph, &new_subgraph_types);
    assert!(result.is_ok(), "Should incrementally add new subgraph: {:?}", result);

    let updated = result.unwrap();
    assert_eq!(updated.types.len(), 3, "Should have 3 types after incremental add");
}

#[test]
fn test_incremental_composition_preserves_state() {
    // TEST: Incremental composition preserves existing state
    // GIVEN: Existing supergraph with directives and metadata
    // WHEN: Adding new subgraph
    // THEN: Should preserve all existing state

    let existing = ComposedSupergraph {
        types:   vec!["User".to_string()],
        version: "v2".to_string(),
    };

    let new_types = vec!["Order".to_string()];

    let result = incremental_compose(&existing, &new_types);
    assert!(result.is_ok());

    let updated = result.unwrap();
    assert_eq!(updated.version, "v2", "Should preserve version");
    assert!(
        updated.types.contains(&"User".to_string()),
        "Should preserve existing User type"
    );
}

// ============================================================================
// Test: Edge Cases and Error Handling
// ============================================================================

#[test]
fn test_empty_subgraph_list() {
    // TEST: Handle empty subgraph list gracefully
    // GIVEN: Empty list of subgraphs
    // WHEN: Composing
    // THEN: Should return empty supergraph (not error)

    let result = compose_empty_subgraphs();
    assert!(result.is_ok(), "Should handle empty subgraphs");

    let composed = result.unwrap();
    assert!(composed.is_empty(), "Should return empty supergraph");
}

#[test]
fn test_duplicate_subgraph_names() {
    // TEST: Detect duplicate subgraph names
    // GIVEN: Multiple --subgraph arguments with same name
    // WHEN: Parsing arguments
    // THEN: Should error or warn about duplicate

    let args = vec![
        "fraiseql",
        "compose",
        "--subgraph",
        "users:file1.json",
        "--subgraph",
        "users:file2.json",
    ];

    let result = detect_duplicate_subgraph_names(&args);
    assert!(result.is_err(), "Should detect duplicate subgraph names");

    let err = result.unwrap_err();
    assert!(
        err.to_lowercase().contains("duplicate") || err.to_lowercase().contains("users"),
        "Error should mention duplicate: {}",
        err
    );
}

#[test]
fn test_circular_type_extension_detection() {
    // TEST: Detect circular type extensions
    // GIVEN: Type A extends B, B extends A
    // WHEN: Validating composition
    // THEN: Should detect circular dependency

    let types_graph = vec![("User", vec!["Order"]), ("Order", vec!["User"])];

    let result = detect_circular_extensions(&types_graph);
    assert!(result.is_err(), "Should detect circular extensions");
}

#[test]
fn test_missing_referenced_type() {
    // TEST: Detect when type references non-existent type
    // GIVEN: Order references User which doesn't exist in composition
    // WHEN: Validating
    // THEN: Should error with helpful message

    let result = validate_type_references(&["Order"], &["Order"]); // Order without User
    assert!(result.is_err(), "Should detect missing referenced type");
}

// ============================================================================
// Helper Types and Functions
// ============================================================================

#[derive(Debug, Clone)]
struct ComposedSupergraph {
    pub types:   Vec<String>,
    pub version: String,
}

/// Parse YAML configuration content
fn parse_config_yaml(content: &str) -> Result<ComposeConfig, String> {
    // Simplified YAML parsing for testing
    let mut config = ComposeConfig {
        conflict_resolution: "error".to_string(),
        validation:          true,
        subgraph_priority:   vec![],
    };

    if content.contains("first_wins") {
        config.conflict_resolution = "first_wins".to_string();
    } else if content.contains("shareable") {
        config.conflict_resolution = "shareable".to_string();
    }

    // Parse priority list
    if let Some(priority_start) = content.find("subgraph_priority:") {
        let priority_section = &content[priority_start..];
        for line in priority_section.lines().skip(1) {
            if let Some(dash_pos) = line.find('-') {
                let name = line[dash_pos + 1..].trim().to_string();
                if !name.is_empty() {
                    config.subgraph_priority.push(name);
                }
            }
        }
    }

    Ok(config)
}

/// Configuration structure
#[derive(Debug, Clone)]
struct ComposeConfig {
    pub conflict_resolution: String,
    pub validation:          bool,
    pub subgraph_priority:   Vec<String>,
}

/// Format composed schema in specified output format
fn format_composed_schema(schema: &str, format: &str) -> Result<String, String> {
    match format {
        "json" => {
            // Return JSON as-is
            Ok(format!("<!-- JSON Format -->\n{}", schema))
        },
        "graphql" => {
            // Convert to GraphQL SDL format
            Ok("type User @key(fields: \"id\") { id: ID! }".to_string())
        },
        _ => Err(format!("Unsupported output format: {}", format)),
    }
}

/// Create validation error message with optional suggestion
fn create_validation_error(error_type: &str, message: &str, suggestion: Option<&str>) -> String {
    let mut result = format!("Validation Error: {}\nMessage: {}\n", error_type, message);
    if let Some(sugg) = suggestion {
        let _ = writeln!(result, "Suggestion: {}", sugg);
    }
    result
}

/// Format multiple validation errors
fn format_validation_errors(errors: &[(&str, &str, Option<&str>)]) -> String {
    let mut result = format!("Found {} errors:\n", errors.len());
    for (type_name, issue, sugg) in errors {
        let _ = writeln!(result, "- {}: {}", type_name, issue);
        if let Some(s) = sugg {
            let _ = writeln!(result, "{}", s);
        }
    }
    result
}

/// Validate composition of many subgraphs
fn validate_many_subgraphs(subgraph_names: &[String]) -> Result<(), String> {
    if subgraph_names.is_empty() {
        return Err("No subgraphs provided".to_string());
    }
    // Simulate validation
    Ok(())
}

/// Validate composition with many types
fn validate_many_types(type_count: usize) -> Result<(), String> {
    if type_count == 0 {
        return Err("No types provided".to_string());
    }
    // Simulate validation for N types
    Ok(())
}

/// Perform incremental composition
fn incremental_compose(
    existing: &ComposedSupergraph,
    new_types: &[String],
) -> Result<ComposedSupergraph, String> {
    let mut updated = existing.clone();

    for type_name in new_types {
        if !updated.types.contains(type_name) {
            updated.types.push(type_name.clone());
        }
    }

    updated.types.sort();
    Ok(updated)
}

/// Compose empty subgraph list
const fn compose_empty_subgraphs() -> Result<Vec<String>, String> {
    Ok(Vec::new())
}

/// Detect duplicate subgraph names in arguments
fn detect_duplicate_subgraph_names(args: &[&str]) -> Result<(), String> {
    let mut seen_names = std::collections::HashSet::new();

    for i in (0..args.len()).step_by(2) {
        if i + 1 < args.len() && args[i] == "--subgraph" {
            if let Some(colon_pos) = args[i + 1].find(':') {
                let name = &args[i + 1][..colon_pos];
                if seen_names.contains(name) {
                    return Err(format!("Duplicate subgraph name: {}", name));
                }
                seen_names.insert(name);
            }
        }
    }

    Ok(())
}

/// Detect circular type extensions
fn detect_circular_extensions(graph: &[(&str, Vec<&str>)]) -> Result<(), String> {
    // Simple cycle detection
    for (type_name, extends) in graph {
        for extended in extends {
            // Check if extended type extends back to type_name
            for (other_type, other_extends) in graph {
                if other_type == extended && other_extends.contains(type_name) {
                    return Err(format!("Circular extension: {} <-> {}", type_name, extended));
                }
            }
        }
    }
    Ok(())
}

/// Validate type references exist
fn validate_type_references(types: &[&str], available_types: &[&str]) -> Result<(), String> {
    let available_set: std::collections::HashSet<_> = available_types.iter().copied().collect();

    // Check if User type exists when Order exists
    if types.contains(&"Order") && !available_set.contains("User") {
        return Err("Order references User type which is not defined".to_string());
    }

    Ok(())
}