mockforge-core 0.3.114

Shared logic for MockForge - routing, validation, latency, proxy
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
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
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
//! AI-guided debugging analyzer
//!
//! This module provides functionality to analyze test failures and suggest fixes.
//! It integrates with the existing failure analysis infrastructure to provide
//! AI-powered debugging assistance.

use crate::ai_studio::debug_context::DebugContext as UnifiedDebugContext;
use crate::ai_studio::debug_context_integrator::DebugContextIntegrator;
use crate::failure_analysis::{
    context_collector::FailureContextCollector, narrative_generator::FailureNarrativeGenerator,
    types::FailureContext,
};
use crate::intelligent_behavior::llm_client::LlmClient;
use crate::intelligent_behavior::types::LlmGenerationRequest;
use crate::intelligent_behavior::IntelligentBehaviorConfig;
use crate::Result;
use serde::{Deserialize, Serialize};

/// Debug analyzer for test failure analysis
pub struct DebugAnalyzer {
    /// Context collector for gathering failure details
    context_collector: FailureContextCollector,
    /// Narrative generator for root cause analysis
    narrative_generator: FailureNarrativeGenerator,
    /// LLM client for generating suggestions
    llm_client: LlmClient,
    /// Optional debug context integrator for collecting subsystem context
    context_integrator: Option<DebugContextIntegrator>,
}

impl DebugAnalyzer {
    /// Create a new debug analyzer with default configuration
    pub fn new() -> Self {
        let config = IntelligentBehaviorConfig::default();
        Self {
            context_collector: FailureContextCollector::new(),
            narrative_generator: FailureNarrativeGenerator::new(config.clone()),
            llm_client: LlmClient::new(config.behavior_model),
            context_integrator: None,
        }
    }

    /// Create a new debug analyzer with custom configuration
    pub fn with_config(config: IntelligentBehaviorConfig) -> Self {
        Self {
            context_collector: FailureContextCollector::new(),
            narrative_generator: FailureNarrativeGenerator::new(config.clone()),
            llm_client: LlmClient::new(config.behavior_model),
            context_integrator: None,
        }
    }

    /// Create a new debug analyzer with context integrator
    pub fn with_integrator(integrator: DebugContextIntegrator) -> Self {
        let config = IntelligentBehaviorConfig::default();
        Self {
            context_collector: FailureContextCollector::new(),
            narrative_generator: FailureNarrativeGenerator::new(config.clone()),
            llm_client: LlmClient::new(config.behavior_model),
            context_integrator: Some(integrator),
        }
    }

    /// Create a new debug analyzer with config and integrator
    pub fn with_config_and_integrator(
        config: IntelligentBehaviorConfig,
        integrator: DebugContextIntegrator,
    ) -> Self {
        Self {
            context_collector: FailureContextCollector::new(),
            narrative_generator: FailureNarrativeGenerator::new(config.clone()),
            llm_client: LlmClient::new(config.behavior_model),
            context_integrator: Some(integrator),
        }
    }

    /// Analyze a test failure and suggest fixes
    ///
    /// This method analyzes test failure logs and provides:
    /// - Root cause identification
    /// - Specific suggestions for fixing the issue
    /// - Links to related mock configurations (personas, reality settings, contracts)
    pub async fn analyze(&self, request: &DebugRequest) -> Result<DebugResponse> {
        // Parse test logs to extract failure information
        let failure_info = self.parse_test_logs(&request.test_logs)?;

        // Collect failure context
        let context = self.context_collector.collect_context(
            &failure_info.method.unwrap_or_else(|| "UNKNOWN".to_string()),
            &failure_info.path.unwrap_or_else(|| "/".to_string()),
            failure_info.status_code,
            failure_info.error_message.clone(),
        )?;

        // Collect unified debug context from subsystems (if integrator is available)
        let unified_context = if let Some(ref integrator) = self.context_integrator {
            Some(integrator.collect_unified_context(request.workspace_id.as_deref()).await?)
        } else {
            None
        };

        // Generate narrative for root cause
        let narrative = self.narrative_generator.generate_narrative(&context).await?;
        let root_cause = if narrative.summary.is_empty() {
            "Unable to determine root cause from provided logs".to_string()
        } else {
            narrative.summary.clone()
        };

        // Generate AI-powered suggestions with unified context
        let mut suggestions = self
            .generate_suggestions(&context, &narrative, unified_context.as_ref())
            .await?;

        // Generate patch operations for suggestions
        self.generate_patches(&mut suggestions, &context, &narrative, unified_context.as_ref())?;

        // Identify related configurations with unified context
        let related_configs = self.identify_related_configs(&context, unified_context.as_ref());

        Ok(DebugResponse {
            root_cause,
            suggestions,
            related_configs,
            context: Some(context),
            unified_context,
        })
    }

    /// Parse test logs to extract failure information
    fn parse_test_logs(&self, logs: &str) -> Result<ParsedFailureInfo> {
        // Simple parsing - in a real implementation, this would use more sophisticated
        // log parsing to extract HTTP methods, paths, status codes, etc.
        let mut info = ParsedFailureInfo::default();

        // Try to extract HTTP method
        for method in &["GET", "POST", "PUT", "DELETE", "PATCH"] {
            if logs.contains(method) {
                info.method = Some(method.to_string());
                break;
            }
        }

        // Try to extract status code (simple pattern matching)
        for line in logs.lines() {
            // Look for 3-digit status codes (400-599 for errors)
            for word in line.split_whitespace() {
                if let Ok(status) = word.parse::<u16>() {
                    if (400..600).contains(&status) {
                        info.status_code = Some(status);
                        break;
                    }
                }
            }
            if info.status_code.is_some() {
                break;
            }
        }

        // Try to extract path (simple pattern matching)
        for line in logs.lines() {
            for method in &["GET", "POST", "PUT", "DELETE", "PATCH"] {
                if let Some(pos) = line.find(method) {
                    let after_method = &line[pos + method.len()..];
                    if let Some(path_start) = after_method.find('/') {
                        let path_part = &after_method[path_start..];
                        if let Some(path_end) =
                            path_part.find(|c: char| c.is_whitespace() || c == '?' || c == '\n')
                        {
                            info.path = Some(path_part[..path_end].to_string());
                        } else {
                            info.path = Some(path_part.trim().to_string());
                        }
                        break;
                    }
                }
            }
            if info.path.is_some() {
                break;
            }
        }

        // Extract error message (look for common error patterns)
        if logs.contains("error") || logs.contains("Error") || logs.contains("ERROR") {
            info.error_message = Some(
                logs.lines()
                    .find(|line| {
                        line.to_lowercase().contains("error")
                            || line.to_lowercase().contains("fail")
                    })
                    .unwrap_or("Test failure detected")
                    .to_string(),
            );
        }

        Ok(info)
    }

    /// Generate AI-powered suggestions for fixing the failure
    async fn generate_suggestions(
        &self,
        context: &FailureContext,
        narrative: &crate::failure_analysis::types::FailureNarrative,
        unified_context: Option<&UnifiedDebugContext>,
    ) -> Result<Vec<DebugSuggestion>> {
        // Build prompt for suggestion generation
        let system_prompt = r#"You are an expert at debugging API test failures in mock environments.
Analyze the failure context and provide specific, actionable suggestions for fixing the issue.

For each suggestion, provide:
1. A clear title
2. A detailed description of what to do
3. A specific action to take
4. The configuration path to update (if applicable)
5. Linked artifacts (persona IDs, scenario names, contract paths) that are relevant

Focus on:
- Contract validation issues (suggest tightening validation or updating contracts)
- Persona mismatches (suggest adjusting persona traits or reality settings)
- Mock scenario issues (suggest adding explicit error examples)
- Reality continuum settings (suggest adjusting reality ratios)
- Chaos configuration issues (suggest disabling or adjusting chaos rules)

Return your response as a JSON array of suggestions."#;

        // Build unified context summary
        let unified_summary = if let Some(uc) = unified_context {
            format!(
                r#"
Unified Subsystem Context:
- Reality Level: {} (chaos: {}, latency: {}ms, MockAI: {})
- Contract Validation: {} (enforcement: {})
- Active Scenario: {}
- Active Persona: {}
- Chaos Rules: {} active
"#,
                uc.reality.level_name.as_deref().unwrap_or("unknown"),
                uc.reality.chaos_enabled,
                uc.reality.latency_base_ms,
                uc.reality.mockai_enabled,
                uc.contract.validation_enabled,
                uc.contract.enforcement_mode,
                uc.scenario.active_scenario.as_deref().unwrap_or("none"),
                uc.persona.active_persona_id.as_deref().unwrap_or("none"),
                uc.chaos.active_rules.len()
            )
        } else {
            String::new()
        };

        let user_prompt = format!(
            r#"Failure Context:
- Request: {} {}
- Status Code: {:?}
- Error: {:?}
- Active Chaos Configs: {}
- Active Consistency Rules: {}
- Contract Validation: {:?}
- Behavioral Rules: {}

Narrative Summary: {}
{}

Provide 3-5 specific suggestions for fixing this test failure. Include linked artifacts (persona IDs, scenario names, contract paths) in your suggestions."#,
            context.request.method,
            context.request.path,
            context.response.as_ref().map(|r| r.status_code),
            context.error_message,
            context.chaos_configs.len(),
            context.consistency_rules.len(),
            context.contract_validation.is_some(),
            context.behavioral_rules.len(),
            if narrative.summary.is_empty() {
                "No narrative available"
            } else {
                &narrative.summary
            },
            unified_summary
        );

        let llm_request = LlmGenerationRequest {
            system_prompt: system_prompt.to_string(),
            user_prompt,
            temperature: 0.3,
            max_tokens: 1500,
            schema: None,
        };

        // Generate suggestions from LLM
        let response = self.llm_client.generate(&llm_request).await?;

        // Parse suggestions from response
        let mut suggestions: Vec<DebugSuggestion> = if let Some(suggestions_array) =
            response.get("suggestions")
        {
            serde_json::from_value(suggestions_array.clone()).unwrap_or_else(|_| {
                // Fallback: create a generic suggestion
                vec![DebugSuggestion {
                    title: "Review Mock Configuration".to_string(),
                    description: "Check your mock configuration for issues related to this failure"
                        .to_string(),
                    action: "Review config.yaml and related mock settings".to_string(),
                    config_path: Some("config.yaml".to_string()),
                    patch: None,
                    linked_artifacts: Vec::new(),
                }]
            })
        } else {
            // Fallback suggestions
            vec![
                DebugSuggestion {
                    title: "Check Contract Validation".to_string(),
                    description: "The failure may be due to contract validation issues. Review your OpenAPI spec and request/response schemas.".to_string(),
                    action: "Review contract validation settings".to_string(),
                    config_path: Some("contract_validation".to_string()),
                    patch: None,
                    linked_artifacts: Vec::new(),
                },
                DebugSuggestion {
                    title: "Review Persona Settings".to_string(),
                    description: "The failure might be related to persona configuration. Check if the active persona matches your test expectations.".to_string(),
                    action: "Review persona configuration".to_string(),
                    config_path: Some("consistency.personas".to_string()),
                    patch: None,
                    linked_artifacts: Vec::new(),
                },
            ]
        };

        // Enhance suggestions with linked artifacts from unified context
        if let Some(uc) = unified_context {
            for suggestion in &mut suggestions {
                // Add persona link if relevant
                if suggestion.title.to_lowercase().contains("persona")
                    || suggestion.description.to_lowercase().contains("persona")
                {
                    if let Some(ref persona_id) = uc.persona.active_persona_id {
                        suggestion.linked_artifacts.push(LinkedArtifact {
                            artifact_type: "persona".to_string(),
                            artifact_id: persona_id.to_string(),
                            artifact_name: uc.persona.active_persona_name.clone(),
                        });
                    }
                }

                // Add scenario link if relevant
                if suggestion.title.to_lowercase().contains("scenario")
                    || suggestion.description.to_lowercase().contains("scenario")
                {
                    if let Some(ref scenario_id) = uc.scenario.active_scenario {
                        suggestion.linked_artifacts.push(LinkedArtifact {
                            artifact_type: "scenario".to_string(),
                            artifact_id: scenario_id.to_string(),
                            artifact_name: None,
                        });
                    }
                }

                // Add contract links if relevant
                if suggestion.title.to_lowercase().contains("contract")
                    || suggestion.description.to_lowercase().contains("contract")
                {
                    for contract_path in &uc.contract.active_contracts {
                        suggestion.linked_artifacts.push(LinkedArtifact {
                            artifact_type: "contract".to_string(),
                            artifact_id: contract_path.to_string(),
                            artifact_name: None,
                        });
                    }
                }

                // Add reality level link if relevant
                if suggestion.title.to_lowercase().contains("reality")
                    || suggestion.description.to_lowercase().contains("reality")
                {
                    if let Some(ref level_name) = uc.reality.level_name {
                        suggestion.linked_artifacts.push(LinkedArtifact {
                            artifact_type: "reality".to_string(),
                            artifact_id: uc
                                .reality
                                .level
                                .map(|l| l.value().to_string())
                                .unwrap_or_default(),
                            artifact_name: Some(level_name.clone()),
                        });
                    }
                }
            }
        }

        Ok(suggestions)
    }

    /// Generate JSON Patch operations for suggestions
    fn generate_patches(
        &self,
        suggestions: &mut [DebugSuggestion],
        context: &FailureContext,
        _narrative: &crate::failure_analysis::types::FailureNarrative,
        _unified_context: Option<&UnifiedDebugContext>,
    ) -> Result<()> {
        for suggestion in suggestions.iter_mut() {
            // Generate patch based on suggestion type and context
            if let Some(config_path) = &suggestion.config_path {
                // Generate appropriate patch based on the suggestion
                let patch = self.create_patch_for_suggestion(suggestion, config_path, context)?;
                suggestion.patch = patch;
            }
        }
        Ok(())
    }

    /// Create a JSON Patch operation for a specific suggestion
    fn create_patch_for_suggestion(
        &self,
        suggestion: &DebugSuggestion,
        config_path: &str,
        context: &FailureContext,
    ) -> Result<Option<DebugPatch>> {
        // Determine patch operation based on suggestion content
        let patch = if suggestion.action.contains("add") || suggestion.action.contains("Add") {
            // Add operation - typically for adding new examples or configurations
            Some(DebugPatch {
                op: "add".to_string(),
                path: self.build_patch_path(config_path, &suggestion.title),
                value: self.infer_patch_value(suggestion, context),
                from: None,
            })
        } else if suggestion.action.contains("remove") || suggestion.action.contains("Remove") {
            // Remove operation
            Some(DebugPatch {
                op: "remove".to_string(),
                path: self.build_patch_path(config_path, &suggestion.title),
                value: None,
                from: None,
            })
        } else {
            // Replace operation (default)
            Some(DebugPatch {
                op: "replace".to_string(),
                path: self.build_patch_path(config_path, &suggestion.title),
                value: self.infer_patch_value(suggestion, context),
                from: None,
            })
        };

        Ok(patch)
    }

    /// Build JSON Pointer path from config path and suggestion context
    fn build_patch_path(&self, config_path: &str, suggestion_title: &str) -> String {
        // Convert config path to JSON Pointer format
        // Example: "consistency.personas" -> "/consistency/personas"
        // Example: "contract_validation" -> "/contract_validation"
        let mut path = config_path.replace('.', "/");
        if !path.starts_with('/') {
            path = format!("/{}", path);
        }

        // If suggestion mentions a specific field, append it
        if suggestion_title.to_lowercase().contains("error rate") {
            path = format!("{}/error_rate", path);
        } else if suggestion_title.to_lowercase().contains("schema") {
            path = format!("{}/schema", path);
        } else if suggestion_title.to_lowercase().contains("example") {
            path = format!("{}/examples", path);
        }

        path
    }

    /// Infer patch value from suggestion and context
    fn infer_patch_value(
        &self,
        suggestion: &DebugSuggestion,
        context: &FailureContext,
    ) -> Option<serde_json::Value> {
        // Generate appropriate value based on suggestion type
        if suggestion.title.contains("422") || suggestion.description.contains("422") {
            // Add 422 validation error example
            Some(serde_json::json!({
                "status": 422,
                "body": {
                    "error": "Validation failed",
                    "message": context.error_message.clone().unwrap_or_else(|| "Invalid request".to_string())
                }
            }))
        } else if suggestion.title.contains("schema") || suggestion.description.contains("schema") {
            // Schema tightening - suggest number type for amount fields
            if suggestion.description.contains("amount") {
                Some(serde_json::json!({
                    "type": "number",
                    "format": "float"
                }))
            } else {
                Some(serde_json::json!({
                    "type": "string"
                }))
            }
        } else if suggestion.title.contains("persona") || suggestion.description.contains("persona")
        {
            // Persona configuration
            Some(serde_json::json!({
                "traits": {},
                "domain": "general"
            }))
        } else {
            // Generic configuration value
            Some(serde_json::json!({
                "enabled": true
            }))
        }
    }

    /// Identify related mock configurations
    fn identify_related_configs(
        &self,
        context: &FailureContext,
        unified_context: Option<&UnifiedDebugContext>,
    ) -> Vec<String> {
        let mut configs = Vec::new();

        // Add contract validation config if present
        if context.contract_validation.is_some() {
            configs.push("Contract Validation".to_string());
        }

        // Add persona configs if behavioral rules are present
        if !context.behavioral_rules.is_empty() {
            configs.push("Persona Configuration".to_string());
        }

        // Add chaos configs if present
        if !context.chaos_configs.is_empty() {
            configs.push("Chaos Configuration".to_string());
        }

        // Add consistency rules if present
        if !context.consistency_rules.is_empty() {
            configs.push("Consistency Rules".to_string());
        }

        // Enhance with unified context information
        if let Some(uc) = unified_context {
            if uc.reality.level.is_some() {
                configs.push(format!(
                    "Reality Level: {}",
                    uc.reality.level_name.as_ref().unwrap_or(&"Unknown".to_string())
                ));
            }
            if uc.scenario.active_scenario.is_some() {
                configs.push(format!(
                    "Active Scenario: {}",
                    uc.scenario.active_scenario.as_ref().unwrap()
                ));
            }
            if uc.persona.active_persona_id.is_some() {
                configs.push(format!(
                    "Active Persona: {}",
                    uc.persona.active_persona_id.as_ref().unwrap()
                ));
            }
            if !uc.contract.active_contracts.is_empty() {
                configs
                    .push(format!("Active Contracts: {}", uc.contract.active_contracts.join(", ")));
            }
        }

        // Add reality continuum if no specific configs found
        if configs.is_empty() {
            configs.push("Reality Continuum Settings".to_string());
        }

        configs
    }
}

impl Default for DebugAnalyzer {
    fn default() -> Self {
        Self::new()
    }
}

/// Parsed failure information from test logs
#[derive(Debug, Default)]
struct ParsedFailureInfo {
    method: Option<String>,
    path: Option<String>,
    status_code: Option<u16>,
    error_message: Option<String>,
}

/// Request for debug analysis
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DebugRequest {
    /// Test failure logs
    pub test_logs: String,

    /// Test name/identifier
    pub test_name: Option<String>,

    /// Workspace ID for context
    pub workspace_id: Option<String>,
}

/// Response from debug analysis
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DebugResponse {
    /// Identified root cause
    pub root_cause: String,

    /// Suggested fixes
    pub suggestions: Vec<DebugSuggestion>,

    /// Related mock configurations
    pub related_configs: Vec<String>,

    /// Full failure context (optional, for detailed analysis)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub context: Option<FailureContext>,

    /// Unified debug context from subsystems (optional)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub unified_context: Option<UnifiedDebugContext>,
}

/// Debug suggestion for fixing a test failure
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DebugSuggestion {
    /// Suggestion title
    pub title: String,

    /// Detailed description
    pub description: String,

    /// Suggested action
    pub action: String,

    /// Configuration path to update
    pub config_path: Option<String>,

    /// JSON Patch operation for applying the fix (optional)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub patch: Option<DebugPatch>,

    /// Linked artifacts (persona IDs, scenario names, contract paths)
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub linked_artifacts: Vec<LinkedArtifact>,
}

/// Linked artifact reference
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LinkedArtifact {
    /// Artifact type (persona, scenario, contract, reality)
    pub artifact_type: String,
    /// Artifact ID or path
    pub artifact_id: String,
    /// Artifact name (optional)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub artifact_name: Option<String>,
}

/// JSON Patch operation for applying a debug suggestion
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DebugPatch {
    /// Patch operation type: "add", "remove", or "replace"
    pub op: String,

    /// JSON Pointer path to the field to modify
    pub path: String,

    /// Value to add or replace (for "add" and "replace" operations)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub value: Option<serde_json::Value>,

    /// Source path for "move" or "copy" operations
    #[serde(skip_serializing_if = "Option::is_none")]
    pub from: Option<String>,
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::ai_studio::debug_context_integrator::DebugContextIntegrator;
    use crate::intelligent_behavior::config::BehaviorModelConfig;
    use serde_json::json;

    fn create_test_config() -> IntelligentBehaviorConfig {
        IntelligentBehaviorConfig {
            behavior_model: BehaviorModelConfig {
                llm_provider: "ollama".to_string(),
                model: "llama2".to_string(),
                api_endpoint: Some("http://localhost:11434/api/chat".to_string()),
                api_key: None,
                temperature: 0.7,
                max_tokens: 2000,
                rules: crate::intelligent_behavior::types::BehaviorRules::default(),
            },
            ..Default::default()
        }
    }

    #[test]
    fn test_debug_analyzer_new() {
        let analyzer = DebugAnalyzer::new();
        // Just verify it can be created
        let _ = analyzer;
    }

    #[test]
    fn test_debug_analyzer_default() {
        let analyzer = DebugAnalyzer::default();
        // Just verify it can be created
        let _ = analyzer;
    }

    #[test]
    fn test_debug_analyzer_with_config() {
        let config = create_test_config();
        let analyzer = DebugAnalyzer::with_config(config);
        // Just verify it can be created
        let _ = analyzer;
    }

    #[test]
    fn test_debug_analyzer_with_integrator() {
        // Create a minimal integrator for testing
        // Note: This might fail if DebugContextIntegrator::new() requires parameters
        // In that case, we'll need to adjust the test
        let integrator = DebugContextIntegrator::new();
        let analyzer = DebugAnalyzer::with_integrator(integrator);
        // Just verify it can be created
        let _ = analyzer;
    }

    #[test]
    fn test_debug_analyzer_with_config_and_integrator() {
        let config = create_test_config();
        let integrator = DebugContextIntegrator::new();
        let analyzer = DebugAnalyzer::with_config_and_integrator(config, integrator);
        // Just verify it can be created
        let _ = analyzer;
    }

    #[test]
    fn test_debug_request_creation() {
        let request = DebugRequest {
            test_logs: "GET /api/users 404".to_string(),
            test_name: Some("test_get_user".to_string()),
            workspace_id: Some("ws-123".to_string()),
        };

        assert_eq!(request.test_logs, "GET /api/users 404");
        assert_eq!(request.test_name, Some("test_get_user".to_string()));
        assert_eq!(request.workspace_id, Some("ws-123".to_string()));
    }

    #[test]
    fn test_debug_request_serialization() {
        let request = DebugRequest {
            test_logs: "Error: 500 Internal Server Error".to_string(),
            test_name: None,
            workspace_id: None,
        };

        let json = serde_json::to_string(&request).unwrap();
        assert!(json.contains("Error: 500"));
    }

    #[test]
    fn test_debug_response_creation() {
        let response = DebugResponse {
            root_cause: "Authentication failed".to_string(),
            suggestions: vec![],
            related_configs: vec!["Persona: admin".to_string()],
            context: None,
            unified_context: None,
        };

        assert_eq!(response.root_cause, "Authentication failed");
        assert_eq!(response.related_configs.len(), 1);
    }

    #[test]
    fn test_debug_response_serialization() {
        let response = DebugResponse {
            root_cause: "Root cause".to_string(),
            suggestions: vec![],
            related_configs: vec![],
            context: None,
            unified_context: None,
        };

        let json = serde_json::to_string(&response).unwrap();
        assert!(json.contains("Root cause"));
    }

    #[test]
    fn test_debug_suggestion_creation() {
        let suggestion = DebugSuggestion {
            title: "Fix authentication".to_string(),
            description: "Update the auth token".to_string(),
            action: "Update config".to_string(),
            config_path: Some("/auth/token".to_string()),
            patch: None,
            linked_artifacts: vec![],
        };

        assert_eq!(suggestion.title, "Fix authentication");
        assert_eq!(suggestion.config_path, Some("/auth/token".to_string()));
    }

    #[test]
    fn test_debug_suggestion_serialization() {
        let suggestion = DebugSuggestion {
            title: "Test suggestion".to_string(),
            description: "Test description".to_string(),
            action: "Test action".to_string(),
            config_path: None,
            patch: None,
            linked_artifacts: vec![],
        };

        let json = serde_json::to_string(&suggestion).unwrap();
        assert!(json.contains("Test suggestion"));
    }

    #[test]
    fn test_linked_artifact_creation() {
        let artifact = LinkedArtifact {
            artifact_type: "persona".to_string(),
            artifact_id: "persona-123".to_string(),
            artifact_name: Some("Admin Persona".to_string()),
        };

        assert_eq!(artifact.artifact_type, "persona");
        assert_eq!(artifact.artifact_id, "persona-123");
        assert_eq!(artifact.artifact_name, Some("Admin Persona".to_string()));
    }

    #[test]
    fn test_linked_artifact_serialization() {
        let artifact = LinkedArtifact {
            artifact_type: "scenario".to_string(),
            artifact_id: "scenario-456".to_string(),
            artifact_name: None,
        };

        let json = serde_json::to_string(&artifact).unwrap();
        assert!(json.contains("scenario"));
        assert!(json.contains("scenario-456"));
    }

    #[test]
    fn test_debug_patch_creation() {
        let patch = DebugPatch {
            op: "replace".to_string(),
            path: "/status".to_string(),
            value: Some(json!("active")),
            from: None,
        };

        assert_eq!(patch.op, "replace");
        assert_eq!(patch.path, "/status");
        assert!(patch.value.is_some());
    }

    #[test]
    fn test_debug_patch_serialization() {
        let patch = DebugPatch {
            op: "add".to_string(),
            path: "/new_field".to_string(),
            value: Some(json!({"key": "value"})),
            from: None,
        };

        let json = serde_json::to_string(&patch).unwrap();
        assert!(json.contains("add"));
        assert!(json.contains("new_field"));
    }

    #[test]
    fn test_debug_patch_with_from() {
        let patch = DebugPatch {
            op: "move".to_string(),
            path: "/target".to_string(),
            value: None,
            from: Some("/source".to_string()),
        };

        assert_eq!(patch.op, "move");
        assert_eq!(patch.from, Some("/source".to_string()));
    }

    #[test]
    fn test_parsed_failure_info_default() {
        let info = ParsedFailureInfo::default();
        assert!(info.method.is_none());
        assert!(info.path.is_none());
        assert!(info.status_code.is_none());
        assert!(info.error_message.is_none());
    }

    #[test]
    fn test_debug_request_clone() {
        let request1 = DebugRequest {
            test_logs: "GET /api/test 404".to_string(),
            test_name: Some("test".to_string()),
            workspace_id: Some("ws-1".to_string()),
        };
        let request2 = request1.clone();
        assert_eq!(request1.test_logs, request2.test_logs);
    }

    #[test]
    fn test_debug_request_debug() {
        let request = DebugRequest {
            test_logs: "Error occurred".to_string(),
            test_name: None,
            workspace_id: None,
        };
        let debug_str = format!("{:?}", request);
        assert!(debug_str.contains("DebugRequest"));
    }

    #[test]
    fn test_debug_response_clone() {
        let response1 = DebugResponse {
            root_cause: "Root cause".to_string(),
            suggestions: vec![],
            related_configs: vec![],
            context: None,
            unified_context: None,
        };
        let response2 = response1.clone();
        assert_eq!(response1.root_cause, response2.root_cause);
    }

    #[test]
    fn test_debug_response_debug() {
        let response = DebugResponse {
            root_cause: "Test root cause".to_string(),
            suggestions: vec![],
            related_configs: vec!["config1".to_string()],
            context: None,
            unified_context: None,
        };
        let debug_str = format!("{:?}", response);
        assert!(debug_str.contains("DebugResponse"));
    }

    #[test]
    fn test_debug_suggestion_clone() {
        let suggestion1 = DebugSuggestion {
            title: "Fix issue".to_string(),
            description: "Description".to_string(),
            action: "Action".to_string(),
            config_path: None,
            patch: None,
            linked_artifacts: vec![],
        };
        let suggestion2 = suggestion1.clone();
        assert_eq!(suggestion1.title, suggestion2.title);
    }

    #[test]
    fn test_debug_suggestion_debug() {
        let suggestion = DebugSuggestion {
            title: "Test suggestion".to_string(),
            description: "Test description".to_string(),
            action: "Test action".to_string(),
            config_path: Some("/config/path".to_string()),
            patch: None,
            linked_artifacts: vec![],
        };
        let debug_str = format!("{:?}", suggestion);
        assert!(debug_str.contains("DebugSuggestion"));
    }

    #[test]
    fn test_linked_artifact_clone() {
        let artifact1 = LinkedArtifact {
            artifact_type: "persona".to_string(),
            artifact_id: "id-1".to_string(),
            artifact_name: Some("Name".to_string()),
        };
        let artifact2 = artifact1.clone();
        assert_eq!(artifact1.artifact_type, artifact2.artifact_type);
    }

    #[test]
    fn test_linked_artifact_debug() {
        let artifact = LinkedArtifact {
            artifact_type: "scenario".to_string(),
            artifact_id: "id-2".to_string(),
            artifact_name: None,
        };
        let debug_str = format!("{:?}", artifact);
        assert!(debug_str.contains("LinkedArtifact"));
    }

    #[test]
    fn test_debug_patch_clone() {
        let patch1 = DebugPatch {
            op: "replace".to_string(),
            path: "/path".to_string(),
            value: Some(json!("value")),
            from: None,
        };
        let patch2 = patch1.clone();
        assert_eq!(patch1.op, patch2.op);
    }

    #[test]
    fn test_debug_patch_debug() {
        let patch = DebugPatch {
            op: "add".to_string(),
            path: "/new".to_string(),
            value: None,
            from: Some("/old".to_string()),
        };
        let debug_str = format!("{:?}", patch);
        assert!(debug_str.contains("DebugPatch"));
    }

    #[test]
    fn test_parsed_failure_info_creation() {
        let info = ParsedFailureInfo {
            method: Some("POST".to_string()),
            path: Some("/api/users".to_string()),
            status_code: Some(500),
            error_message: Some("Internal error".to_string()),
        };
        assert_eq!(info.method, Some("POST".to_string()));
        assert_eq!(info.status_code, Some(500));
    }

    #[test]
    fn test_debug_request_with_all_fields() {
        let request = DebugRequest {
            test_logs: "POST /api/users 201\nResponse: {\"id\": 1}".to_string(),
            test_name: Some("test_create_user".to_string()),
            workspace_id: Some("workspace-123".to_string()),
        };
        assert!(!request.test_logs.is_empty());
        assert!(request.test_name.is_some());
        assert!(request.workspace_id.is_some());
    }

    #[test]
    fn test_debug_response_with_all_fields() {
        let suggestion = DebugSuggestion {
            title: "Fix auth".to_string(),
            description: "Update token".to_string(),
            action: "Update config".to_string(),
            config_path: Some("/auth/token".to_string()),
            patch: Some(DebugPatch {
                op: "replace".to_string(),
                path: "/auth/token".to_string(),
                value: Some(json!("new-token")),
                from: None,
            }),
            linked_artifacts: vec![LinkedArtifact {
                artifact_type: "persona".to_string(),
                artifact_id: "persona-1".to_string(),
                artifact_name: Some("Admin".to_string()),
            }],
        };
        let response = DebugResponse {
            root_cause: "Authentication failed".to_string(),
            suggestions: vec![suggestion],
            related_configs: vec!["Persona: admin".to_string(), "Scenario: auth".to_string()],
            context: None,
            unified_context: None,
        };
        assert_eq!(response.suggestions.len(), 1);
        assert_eq!(response.related_configs.len(), 2);
    }

    #[test]
    fn test_debug_suggestion_with_patch() {
        let patch = DebugPatch {
            op: "replace".to_string(),
            path: "/status".to_string(),
            value: Some(json!("active")),
            from: None,
        };
        let suggestion = DebugSuggestion {
            title: "Update status".to_string(),
            description: "Change status to active".to_string(),
            action: "Apply patch".to_string(),
            config_path: Some("/status".to_string()),
            patch: Some(patch.clone()),
            linked_artifacts: vec![],
        };
        assert!(suggestion.patch.is_some());
        assert_eq!(suggestion.patch.unwrap().op, "replace");
    }

    #[test]
    fn test_debug_patch_all_operations() {
        let operations = vec!["add", "remove", "replace", "move", "copy"];
        for op in operations {
            let patch = DebugPatch {
                op: op.to_string(),
                path: "/test".to_string(),
                value: Some(json!("value")),
                from: None,
            };
            assert_eq!(patch.op, op);
        }
    }

    #[test]
    fn test_linked_artifact_with_name() {
        let artifact = LinkedArtifact {
            artifact_type: "persona".to_string(),
            artifact_id: "persona-123".to_string(),
            artifact_name: Some("Admin Persona".to_string()),
        };
        assert_eq!(artifact.artifact_type, "persona");
        assert!(artifact.artifact_name.is_some());
    }

    #[test]
    fn test_linked_artifact_without_name() {
        let artifact = LinkedArtifact {
            artifact_type: "scenario".to_string(),
            artifact_id: "scenario-456".to_string(),
            artifact_name: None,
        };
        assert_eq!(artifact.artifact_type, "scenario");
        assert!(artifact.artifact_name.is_none());
    }

    #[test]
    fn test_parsed_failure_info_with_all_fields() {
        let info = ParsedFailureInfo {
            method: Some("PUT".to_string()),
            path: Some("/api/users/123".to_string()),
            status_code: Some(422),
            error_message: Some("Validation failed: email is required".to_string()),
        };
        assert_eq!(info.method, Some("PUT".to_string()));
        assert_eq!(info.path, Some("/api/users/123".to_string()));
        assert_eq!(info.status_code, Some(422));
        assert!(info.error_message.is_some());
    }
}