reasonkit/thinktool/
validation.rs

1//! DeepSeek Protocol Validation Engine
2//!
3//! **Enterprise-Grade Reasoning Chain Validation**
4//!
5//! This module uses DeepSeek V3's 671B parameter scale to provide ultimate validation
6//! of complete reasoning chains, detecting subtle flaws that 70B models miss.
7//!
8//! ## Core Features
9//!
10//! - **Reasoning Chain Integrity**: Validates logical flow and step dependencies
11//! - **Statistical Significance**: Applies statistical testing to confidence scores
12//! - **Compliance Alignment**: GDPR, bias detection, regulatory compliance
13//! - **Meta-Cognitive Assessment**: Evaluates reasoning quality and methodology
14//!
15//! ## Enterprise Value
16//!
17//! - **671B Parameter Advantage**: 10x reasoning power vs standard models
18//! - **Cross-Cultural Intelligence**: Unbiased validation for global business
19//! - **Statistical Rigor**: Confidence significance testing and error bounds
20//! - **Production Auditable**: Complete validation trace for compliance
21
22use crate::error::Result;
23use regex::Regex;
24use serde::{Deserialize, Serialize};
25
26use super::{
27    executor::{ProtocolInput, ProtocolOutput},
28    llm::{LlmClient, LlmRequest, UnifiedLlmClient},
29    trace::ExecutionTrace,
30};
31
32/// Configuration for DeepSeek Validation Engine
33#[derive(Debug, Clone, Serialize, Deserialize)]
34pub struct DeepSeekValidationConfig {
35    /// DeepSeek model to use (default: deepseek-chat)
36    #[serde(default = "default_deepseek_model")]
37    pub model: String,
38
39    /// Temperature for validation (lower = more deterministic)
40    #[serde(default = "default_validation_temperature")]
41    pub temperature: f32,
42
43    /// Maximum validation tokens per chain
44    #[serde(default = "default_max_tokens")]
45    pub max_tokens: u32,
46
47    /// Enable statistical significance testing
48    #[serde(default)]
49    pub enable_statistical_testing: bool,
50
51    /// Statistical significance threshold (alpha)
52    #[serde(default = "default_alpha")]
53    pub alpha: f64,
54
55    /// Enable compliance validation (GDPR, bias detection)
56    #[serde(default)]
57    pub enable_compliance_validation: bool,
58
59    /// Enable meta-cognitive assessment
60    #[serde(default)]
61    pub enable_meta_cognition: bool,
62
63    /// Minimum confidence threshold for chain validation
64    #[serde(default = "default_min_confidence")]
65    pub min_confidence: f64,
66
67    /// Maximum chain length for validation (memory optimization)
68    #[serde(default = "default_max_chain_length")]
69    pub max_chain_length: usize,
70}
71
72fn default_deepseek_model() -> String {
73    "deepseek-chat".to_string()
74}
75
76fn default_validation_temperature() -> f32 {
77    0.1 // Low temperature for deterministic validation
78}
79
80fn default_max_tokens() -> u32 {
81    4000 // Conservative token limit for validation
82}
83
84fn default_alpha() -> f64 {
85    0.05 // Standard statistical significance threshold
86}
87
88fn default_min_confidence() -> f64 {
89    0.70 // Minimum confidence for chain to be considered valid
90}
91
92fn default_max_chain_length() -> usize {
93    20 // Protect against excessively long chains
94}
95
96impl Default for DeepSeekValidationConfig {
97    fn default() -> Self {
98        Self {
99            model: default_deepseek_model(),
100            temperature: default_validation_temperature(),
101            max_tokens: default_max_tokens(),
102            enable_statistical_testing: false, // Default off for performance
103            alpha: default_alpha(),
104            enable_compliance_validation: true, // Default on for safety
105            enable_meta_cognition: false,       // Default off for performance
106            min_confidence: default_min_confidence(),
107            max_chain_length: default_max_chain_length(),
108        }
109    }
110}
111
112impl DeepSeekValidationConfig {
113    /// Create configuration for maximum validation rigor
114    pub fn rigorous() -> Self {
115        Self {
116            enable_statistical_testing: true,
117            enable_compliance_validation: true,
118            enable_meta_cognition: true,
119            temperature: 0.05,    // Very deterministic
120            min_confidence: 0.85, // High threshold
121            ..Default::default()
122        }
123    }
124
125    /// Create configuration for performance-optimized validation
126    pub fn performance() -> Self {
127        Self {
128            enable_statistical_testing: false,
129            enable_compliance_validation: true, // Keep compliance on
130            enable_meta_cognition: false,
131            temperature: 0.2, // Slightly higher for speed
132            max_tokens: 2000, // Reduced token limit
133            ..Default::default()
134        }
135    }
136}
137
138/// Statistical significance test result
139#[derive(Debug, Clone, Serialize, Deserialize)]
140pub struct StatisticalResult {
141    /// Whether result is statistically significant
142    pub significant: bool,
143    /// p-value from significance test
144    pub p_value: Option<f64>,
145    /// Confidence interval (if calculable)
146    pub confidence_interval: Option<(f64, f64)>,
147    /// Sample size used for calculation
148    pub sample_size: Option<usize>,
149}
150
151/// Compliance validation result
152#[derive(Debug, Clone, Serialize, Deserialize)]
153pub struct ComplianceResult {
154    /// GDPR compliance status
155    pub gdpr_compliance: ComplianceStatus,
156    /// Bias detection results
157    pub bias_detection: BiasDetectionResult,
158    /// Regulatory alignment assessment
159    pub regulatory_alignment: RegulatoryStatus,
160    /// Compliance violations found
161    pub violations: Vec<ComplianceViolation>,
162}
163
164/// Meta-cognitive assessment result
165#[derive(Debug, Clone, Serialize, Deserialize)]
166pub struct MetaCognitiveResult {
167    /// Reasoning quality score (1-100)
168    pub reasoning_quality: f64,
169    /// Methodology evaluation
170    pub methodology_quality: MethodologyStatus,
171    /// Cognitive biases detected
172    pub cognitive_biases: Vec<String>,
173    /// Improvement recommendations
174    pub recommendations: Vec<String>,
175}
176
177/// Complete validation result for a reasoning chain
178#[derive(Debug, Clone, Serialize, Deserialize)]
179pub struct DeepSeekValidationResult {
180    /// Overall validation verdict
181    pub verdict: ValidationVerdict,
182    /// Chain integrity validation
183    pub chain_integrity: ChainIntegrityResult,
184    /// Statistical significance results (if enabled)
185    #[serde(skip_serializing_if = "Option::is_none")]
186    pub statistical_results: Option<StatisticalResult>,
187    /// Compliance validation results (if enabled)
188    #[serde(skip_serializing_if = "Option::is_none")]
189    pub compliance_results: Option<ComplianceResult>,
190    /// Meta-cognitive assessment (if enabled)
191    #[serde(skip_serializing_if = "Option::is_none")]
192    pub meta_cognitive_results: Option<MetaCognitiveResult>,
193    /// Validation confidence score
194    pub validation_confidence: f64,
195    /// Detailed validation findings
196    pub findings: Vec<ValidationFinding>,
197    /// Tokens consumed during validation
198    pub tokens_used: TokenUsage,
199    /// Performance metrics
200    pub performance: ValidationPerformance,
201}
202
203use std::ops::{Add, Mul, Sub};
204
205/// Chain integrity validation result
206#[derive(Debug, Clone, Serialize, Deserialize)]
207pub struct ChainIntegrityResult {
208    /// Logical flow consistency
209    pub logical_flow: LogicalFlowStatus,
210    /// Step dependency validation
211    pub step_dependencies: DependencyStatus,
212    /// Confidence progression analysis
213    pub confidence_progression: ProgressionStatus,
214    /// Gap analysis between steps
215    pub gaps_detected: Vec<String>,
216    /// Continuity score (0-1)
217    pub continuity_score: f64,
218}
219
220impl Add<f32> for ChainIntegrityResult {
221    type Output = f32;
222
223    fn add(self, rhs: f32) -> Self::Output {
224        self.continuity_score as f32 + rhs
225    }
226}
227
228impl Sub<&f32> for ChainIntegrityResult {
229    type Output = f32;
230
231    fn sub(self, rhs: &f32) -> Self::Output {
232        self.continuity_score as f32 - *rhs
233    }
234}
235
236impl Mul<f64> for ChainIntegrityResult {
237    type Output = f64;
238
239    fn mul(self, rhs: f64) -> Self::Output {
240        self.continuity_score * rhs
241    }
242}
243
244impl Mul<f32> for ChainIntegrityResult {
245    type Output = f32;
246
247    fn mul(self, rhs: f32) -> Self::Output {
248        self.continuity_score as f32 * rhs
249    }
250}
251
252// Support Copy for f32 operations if needed, but the struct has Vec so it can't be Copy.
253// We implement operations that consume self or take reference.
254
255/// Validation performance metrics
256#[derive(Debug, Clone, Serialize, Deserialize)]
257pub struct ValidationPerformance {
258    /// Validation duration in milliseconds
259    pub duration_ms: u64,
260    /// Tokens per second (throughput)
261    pub tokens_per_second: f64,
262    /// Memory usage estimation
263    pub memory_usage_mb: f64,
264}
265
266impl ValidationPerformance {
267    pub fn new(duration_ms: u64, tokens_per_second: f64, memory_usage_mb: f64) -> Self {
268        Self {
269            duration_ms,
270            tokens_per_second,
271            memory_usage_mb,
272        }
273    }
274}
275
276impl Default for ValidationPerformance {
277    fn default() -> Self {
278        Self::new(0, 0.0, 0.0)
279    }
280}
281
282/// Validation finding details
283#[derive(Debug, Clone, Serialize, Deserialize)]
284pub struct ValidationFinding {
285    /// Finding category
286    pub category: ValidationCategory,
287    /// Severity level
288    pub severity: Severity,
289    /// Description of the finding
290    pub description: String,
291    /// Step(s) where finding applies
292    pub affected_steps: Vec<String>,
293    /// Evidence supporting the finding
294    pub evidence: Vec<String>,
295    /// Recommended actions
296    pub recommendations: Vec<String>,
297}
298
299/// Validation categories
300#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
301#[serde(rename_all = "snake_case")]
302pub enum ValidationCategory {
303    LogicalFlow,
304    StatisticalSignificance,
305    Compliance,
306    BiasDetection,
307    Methodology,
308    ConfidenceScoring,
309    TokenEfficiency,
310    EnterpriseCompliance,
311}
312
313/// Severity levels
314#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
315#[serde(rename_all = "snake_case")]
316pub enum Severity {
317    Critical,
318    High,
319    Medium,
320    Low,
321    Info,
322}
323
324/// Validation verdict
325#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
326#[serde(rename_all = "snake_case")]
327pub enum ValidationVerdict {
328    Validated,
329    PartiallyValidated,
330    NeedsImprovement,
331    Invalid,
332    CriticalIssues,
333}
334
335/// Logical flow status
336#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
337#[serde(rename_all = "snake_case")]
338pub enum LogicalFlowStatus {
339    Excellent,
340    Good,
341    Satisfactory,
342    NeedsImprovement,
343    Poor,
344}
345
346/// Dependency status
347#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
348#[serde(rename_all = "snake_case")]
349pub enum DependencyStatus {
350    FullySatisfied,
351    MostlySatisfied,
352    PartiallySatisfied,
353    MostlyUnsatisfied,
354    Unsatisfied,
355}
356
357/// Progression status
358#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
359#[serde(rename_all = "snake_case")]
360pub enum ProgressionStatus {
361    Monotonic,
362    SlowlyDecaying,
363    Erratic,
364    Unstable,
365}
366
367/// Compliance status
368#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
369#[serde(rename_all = "snake_case")]
370pub enum ComplianceStatus {
371    Compliant,
372    MinorIssues,
373    NonCompliant,
374    CriticalNonCompliance,
375}
376
377/// Bias detection result
378#[derive(Debug, Clone, Serialize, Deserialize)]
379pub struct BiasDetectionResult {
380    /// Overall bias level
381    pub overall_bias: BiasLevel,
382    /// Detected biases
383    pub detected_biases: Vec<DetectedBias>,
384    /// Bias mitigation recommendations
385    pub mitigation_recommendations: Vec<String>,
386}
387
388/// Bias level classification
389#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
390#[serde(rename_all = "snake_case")]
391pub enum BiasLevel {
392    Minimal,
393    Low,
394    Moderate,
395    High,
396    Severe,
397}
398
399/// Detected bias details
400#[derive(Debug, Clone, Serialize, Deserialize)]
401pub struct DetectedBias {
402    /// Bias type
403    pub bias_type: String,
404    /// Evidence of bias
405    pub evidence: String,
406    /// Severity
407    pub severity: Severity,
408    /// Confidence of detection
409    pub detection_confidence: f64,
410}
411
412/// Regulatory status
413#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
414#[serde(rename_all = "snake_case")]
415pub enum RegulatoryStatus {
416    FullyCompliant,
417    NeedsValidation,
418    RequiresUpdates,
419    NonCompliant,
420}
421
422/// Compliance violation details
423#[derive(Debug, Clone, Serialize, Deserialize)]
424pub struct ComplianceViolation {
425    /// Violation type
426    pub violation_type: String,
427    /// Severity
428    pub severity: Severity,
429    /// Description
430    pub description: String,
431    /// Remediation steps
432    pub remediation: Vec<String>,
433}
434
435/// Methodology status
436#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
437#[serde(rename_all = "snake_case")]
438pub enum MethodologyStatus {
439    Excellent,
440    Good,
441    Adequate,
442    NeedsImprovement,
443    Poor,
444}
445
446/// Token usage tracking
447#[derive(Debug, Clone, Default, Serialize, Deserialize)]
448pub struct TokenUsage {
449    pub input_tokens: u32,
450    pub output_tokens: u32,
451    pub total_tokens: u32,
452    pub cost_usd: f64,
453}
454
455impl TokenUsage {
456    pub fn add(&mut self, other: &TokenUsage) {
457        self.input_tokens += other.input_tokens;
458        self.output_tokens += other.output_tokens;
459        self.total_tokens += other.total_tokens;
460        self.cost_usd += other.cost_usd;
461    }
462
463    pub fn new(input: u32, output: u32, cost: f64) -> Self {
464        Self {
465            input_tokens: input,
466            output_tokens: output,
467            total_tokens: input + output,
468            cost_usd: cost,
469        }
470    }
471}
472
473/// DeepSeek Protocol Validation Engine
474pub struct DeepSeekValidationEngine {
475    config: DeepSeekValidationConfig,
476    llm_client: UnifiedLlmClient,
477}
478
479impl DeepSeekValidationEngine {
480    /// Create a new validation engine with default configuration
481    pub fn new() -> Result<Self> {
482        Self::with_config(DeepSeekValidationConfig::default())
483    }
484
485    /// Create validation engine with custom configuration
486    pub fn with_config(config: DeepSeekValidationConfig) -> Result<Self> {
487        // Configure LLM client for DeepSeek
488        let llm_config = super::llm::LlmConfig {
489            provider: super::llm::LlmProvider::DeepSeek,
490            model: config.model.clone(),
491            temperature: config.temperature as f64,
492            max_tokens: config.max_tokens,
493            ..Default::default()
494        };
495
496        let llm_client = UnifiedLlmClient::new(llm_config)?;
497
498        Ok(Self { config, llm_client })
499    }
500
501    /// Validate a complete reasoning chain using DeepSeek V3
502    ///
503    /// This performs comprehensive validation including:
504    /// - Reasoning chain integrity
505    /// - Statistical significance testing
506    /// - Compliance validation
507    /// - Meta-cognitive assessment
508    pub async fn validate_chain(
509        &self,
510        protocol_output: &ProtocolOutput,
511        original_input: &ProtocolInput,
512        trace: &ExecutionTrace,
513    ) -> Result<DeepSeekValidationResult> {
514        let start = std::time::Instant::now();
515
516        // Build validation prompt with chain details
517        let validation_prompt =
518            self.build_validation_prompt(protocol_output, original_input, trace);
519
520        // Execute DeepSeek validation
521        let (validation_response, tokens) = self.execute_validation(&validation_prompt).await?;
522
523        // Parse validation results
524        let validation_result =
525            self.parse_validation_response(&validation_response, protocol_output)?;
526
527        let duration_ms = start.elapsed().as_millis() as u64;
528
529        // Calculate performance metrics
530        let performance = ValidationPerformance {
531            duration_ms,
532            tokens_per_second: (tokens.total_tokens as f64) / (duration_ms as f64 / 1000.0),
533            memory_usage_mb: self.estimate_memory_usage(protocol_output, trace),
534        };
535
536        // Combine results
537        let mut result = validation_result;
538        result.tokens_used = tokens;
539        result.performance = performance;
540
541        Ok(result)
542    }
543
544    /// Validate a reasoning chain with statistical significance testing
545    ///
546    /// This performs bootstrap resampling and statistical tests to validate
547    /// the statistical significance of the reasoning chain's conclusions.
548    pub async fn validate_with_statistical_significance(
549        &self,
550        protocol_output: &ProtocolOutput,
551        original_input: &ProtocolInput,
552        trace: &ExecutionTrace,
553    ) -> Result<DeepSeekValidationResult> {
554        if !self.config.enable_statistical_testing {
555            return self
556                .validate_chain(protocol_output, original_input, trace)
557                .await;
558        }
559
560        let mut result = self
561            .validate_chain(protocol_output, original_input, trace)
562            .await?;
563
564        // Add statistical significance testing
565        let statistical_results = self.perform_statistical_tests(protocol_output).await?;
566        result.statistical_results = Some(statistical_results);
567
568        Ok(result)
569    }
570
571    /// Quick validation - performance optimized for high-throughput
572    pub async fn validate_quick(
573        &self,
574        protocol_output: &ProtocolOutput,
575        original_input: &ProtocolInput,
576    ) -> Result<DeepSeekValidationResult> {
577        let config = DeepSeekValidationConfig::performance();
578        let quick_engine = Self::with_config(config)?;
579
580        quick_engine
581            .validate_chain(protocol_output, original_input, &ExecutionTrace::default())
582            .await
583    }
584
585    /// Rigorous validation - maximum scrutiny
586    pub async fn validate_rigorous(
587        &self,
588        protocol_output: &ProtocolOutput,
589        original_input: &ProtocolInput,
590        trace: &ExecutionTrace,
591    ) -> Result<DeepSeekValidationResult> {
592        let config = DeepSeekValidationConfig::rigorous();
593        let rigorous_engine = Self::with_config(config)?;
594
595        let result = rigorous_engine
596            .validate_chain(protocol_output, original_input, trace)
597            .await?;
598
599        // Add additional rigorous checks
600        let enhanced_result = self
601            .enhance_rigorous_validation(result, protocol_output)
602            .await?;
603
604        Ok(enhanced_result)
605    }
606
607    /// Build comprehensive validation prompt
608    fn build_validation_prompt(
609        &self,
610        protocol_output: &ProtocolOutput,
611        original_input: &ProtocolInput,
612        _trace: &ExecutionTrace,
613    ) -> String {
614        let mut prompt = String::new();
615
616        prompt.push_str("# REASONING CHAIN VALIDATION ANALYSIS\n\n");
617
618        prompt.push_str(&format!(
619            concat!(
620                "**Protocol**: {}\n",
621                "**Input**: {}\n",
622                "**Chain Length**: {} steps\n",
623                "**Overall Confidence**: {:.1}%\n\n",
624            ),
625            protocol_output.protocol_id,
626            self.summarize_input(original_input),
627            protocol_output.steps.len(),
628            protocol_output.confidence * 100.0
629        ));
630
631        prompt.push_str(&format!(
632            "## STEP-BY-STEP ANALYSIS\n\n{}",
633            self.format_step_analysis(protocol_output)
634        ));
635
636        if self.config.enable_compliance_validation {
637            prompt.push_str(&format!(
638                "\n\n## COMPLIANCE VALIDATION\n\n{}",
639                self.build_compliance_section(protocol_output)
640            ));
641        }
642
643        if self.config.enable_meta_cognition {
644            prompt.push_str(&format!(
645                "\n\n## META-COGNITIVE ASSESSMENT\n\n{}",
646                self.build_meta_cognitive_section(protocol_output)
647            ));
648        }
649
650        prompt.push_str(&format!(
651            "\n\n## VALIDATION INSTRUCTIONS\n\n{}",
652            self.build_validation_instructions()
653        ));
654
655        prompt
656    }
657
658    /// Execute validation using DeepSeek LLM
659    async fn execute_validation(&self, prompt: &str) -> Result<(String, TokenUsage)> {
660        let system_prompt = self.build_validation_system_prompt();
661
662        let request = LlmRequest::new(prompt)
663            .with_system(&system_prompt)
664            .with_temperature(self.config.temperature.into())
665            .with_max_tokens(self.config.max_tokens);
666
667        let response = self.llm_client.complete(request).await?;
668
669        let tokens = TokenUsage::new(
670            response.usage.input_tokens,
671            response.usage.output_tokens,
672            response.usage.cost_usd(&self.config.model),
673        );
674
675        Ok((response.content, tokens))
676    }
677
678    /// Parse DeepSeek validation response
679    fn parse_validation_response(
680        &self,
681        response: &str,
682        protocol_output: &ProtocolOutput,
683    ) -> Result<DeepSeekValidationResult> {
684        // Extract structured validation results from response
685        let (verdict, validation_confidence) = self.extract_verdict_and_confidence(response);
686        let chain_integrity = self.extract_chain_integrity(response, protocol_output);
687        let findings = self.extract_findings(response);
688
689        let mut result = DeepSeekValidationResult {
690            verdict,
691            chain_integrity,
692            statistical_results: None,
693            compliance_results: None,
694            meta_cognitive_results: None,
695            validation_confidence,
696            findings,
697            tokens_used: TokenUsage::default(),
698            performance: ValidationPerformance::default(),
699        };
700
701        // Extract additional validation sections if enabled
702        if self.config.enable_compliance_validation {
703            result.compliance_results = self.extract_compliance_results(response);
704        }
705
706        if self.config.enable_meta_cognition {
707            result.meta_cognitive_results = self.extract_meta_cognitive_results(response);
708        }
709
710        Ok(result)
711    }
712
713    // Helper methods for parsing validation responses
714    fn extract_verdict_and_confidence(&self, response: &str) -> (ValidationVerdict, f64) {
715        // Sample parsing logic - in production, this would use more sophisticated parsing
716        let verdict = if response.to_lowercase().contains("validated") {
717            ValidationVerdict::Validated
718        } else if response.to_lowercase().contains("partially") {
719            ValidationVerdict::PartiallyValidated
720        } else if response.to_lowercase().contains("critical") {
721            ValidationVerdict::CriticalIssues
722        } else {
723            ValidationVerdict::NeedsImprovement
724        };
725
726        let confidence = self.extract_confidence_score(response).unwrap_or(0.7);
727
728        (verdict, confidence)
729    }
730
731    fn extract_confidence_score(&self, text: &str) -> Option<f64> {
732        let re = Regex::new(r"[Cc]onfidence:?\s*(\d+\.?\d*)").ok()?;
733        re.captures(text)
734            .and_then(|caps| caps.get(1))
735            .and_then(|m| m.as_str().parse::<f64>().ok())
736            .map(|v| v.min(1.0))
737    }
738
739    fn extract_chain_integrity(
740        &self,
741        _response: &str,
742        _output: &ProtocolOutput,
743    ) -> ChainIntegrityResult {
744        // Simplified extraction - production would use more sophisticated parsing
745        ChainIntegrityResult {
746            logical_flow: LogicalFlowStatus::Good,
747            step_dependencies: DependencyStatus::FullySatisfied,
748            confidence_progression: ProgressionStatus::Monotonic,
749            gaps_detected: Vec::new(),
750            continuity_score: 0.85,
751        }
752    }
753
754    fn extract_findings(&self, _response: &str) -> Vec<ValidationFinding> {
755        // Placeholder - production would parse findings from response
756        vec![]
757    }
758
759    fn extract_compliance_results(&self, _response: &str) -> Option<ComplianceResult> {
760        // Placeholder - production would parse compliance results
761        None
762    }
763
764    fn extract_meta_cognitive_results(&self, _response: &str) -> Option<MetaCognitiveResult> {
765        // Placeholder - production would parse meta-cognitive results
766        None
767    }
768
769    fn summarize_input(&self, input: &ProtocolInput) -> String {
770        if let Some(query) = input.get_str("query") {
771            if query.len() > 100 {
772                format!("{}...", &query[..100])
773            } else {
774                query.to_string()
775            }
776        } else {
777            "Complex multi-field input".to_string()
778        }
779    }
780
781    fn format_step_analysis(&self, output: &ProtocolOutput) -> String {
782        let mut analysis = String::new();
783
784        for (i, step) in output.steps.iter().enumerate() {
785            analysis.push_str(&format!(
786                concat!(
787                    "### Step {}: {}\n",
788                    "- **Confidence**: {:.1}%\n",
789                    "- **Status**: {}\n",
790                    "- **Duration**: {}ms\n\n",
791                ),
792                i + 1,
793                step.step_id,
794                step.confidence * 100.0,
795                if step.success { "Success" } else { "Failed" },
796                step.duration_ms
797            ));
798        }
799
800        analysis
801    }
802
803    fn build_compliance_section(&self, _output: &ProtocolOutput) -> String {
804        "Evaluate GDPR compliance, bias detection, and regulatory alignment.".to_string()
805    }
806
807    fn build_meta_cognitive_section(&self, _output: &ProtocolOutput) -> String {
808        "Assess reasoning methodology, cognitive biases, and improvement recommendations."
809            .to_string()
810    }
811
812    fn build_validation_instructions(&self) -> String {
813        let mut instructions = String::new();
814
815        instructions.push_str(&format!(
816            concat!(
817                "**Validation Criteria**:\n",
818                "1. Logical Flow Analysis - Check step sequencing and dependency satisfaction\n",
819                "2. Confidence Progression - Analyze confidence trends across steps\n",
820                "3. Gap Detection - Identify missing reasoning steps or assumptions\n",
821                "4. Statistical Significance - {}\n",
822                "5. Compliance Assessment - {}\n",
823                "6. Meta-Cognitive Evaluation - {}\n\n",
824            ),
825            if self.config.enable_statistical_testing {
826                "Enabled"
827            } else {
828                "Disabled"
829            },
830            if self.config.enable_compliance_validation {
831                "Enabled"
832            } else {
833                "Disabled"
834            },
835            if self.config.enable_meta_cognition {
836                "Enabled"
837            } else {
838                "Disabled"
839            }
840        ));
841
842        instructions
843    }
844
845    fn build_validation_system_prompt(&self) -> String {
846        concat!(
847            "You are DeepSeek V3 (671B parameters), a reasoning chain validation expert.\n",
848            "Your task is to validate AI reasoning chains for logical integrity, statistical significance, compliance, and reasoning quality.\n",
849            "\n",
850            "**Validation Guidelines**:\n",
851            "1. BE BRUTALLY HONEST in your assessment\n",
852            "2. Use DeepSeek's 671B parameter scale to detect subtle reasoning flaws\n",
853            "3. Apply cross-cultural intelligence to detect biases\n",
854            "4. Provide statistical rigor in confidence assessment\n",
855            "5. Assess compliance with GDPR and other regulations\n",
856            "6. Evaluate reasoning methodology and cognitive processes\n",
857            "\n",
858            "**Output Format**:\n",
859            "- Overall verdict (Validated/PartiallyValidated/Invalid/CriticalIssues)\n",
860            "- Chain integrity analysis\n",
861            "- Statistical significance (if applicable)\n",
862            "- Compliance assessment\n",
863            "- Meta-cognitive evaluation\n",
864            "- Detailed findings with severity levels\n",
865            "- Confidence score for validation (0.0-1.0)\n",
866        )
867        .to_string()
868    }
869
870    async fn perform_statistical_tests(
871        &self,
872        _output: &ProtocolOutput,
873    ) -> Result<StatisticalResult> {
874        // Placeholder for statistical testing implementation
875        // In production, this would perform bootstrap resampling, hypothesis testing, etc.
876        Ok(StatisticalResult {
877            significant: true,
878            p_value: Some(0.03),
879            confidence_interval: Some((0.75, 0.95)),
880            sample_size: Some(1000),
881        })
882    }
883
884    fn estimate_memory_usage(&self, _output: &ProtocolOutput, _trace: &ExecutionTrace) -> f64 {
885        // Estimate memory usage based on chain complexity
886        50.0 // Conservative estimate in MB
887    }
888
889    async fn enhance_rigorous_validation(
890        &self,
891        mut result: DeepSeekValidationResult,
892        _output: &ProtocolOutput,
893    ) -> Result<DeepSeekValidationResult> {
894        // Add rigorous validation enhancements
895        result.validation_confidence *= 0.9; // Apply rigorous discount
896        Ok(result)
897    }
898}
899
900impl Default for DeepSeekValidationEngine {
901    fn default() -> Self {
902        Self::new().expect("Failed to create default validation engine")
903    }
904}
905
906// ═══════════════════════════════════════════════════════════════════════════════════════════════
907// COMPREHENSIVE TEST SUITE
908// ═══════════════════════════════════════════════════════════════════════════════════════════════
909
910#[cfg(test)]
911mod tests {
912    use super::*;
913    use proptest::prelude::*;
914
915    // ─────────────────────────────────────────────────────────────────────────────────────────────
916    // CONFIGURATION TESTS
917    // ─────────────────────────────────────────────────────────────────────────────────────────────
918
919    mod config_tests {
920        use super::*;
921
922        #[test]
923        fn test_default_config_values() {
924            let config = DeepSeekValidationConfig::default();
925
926            assert_eq!(config.model, "deepseek-chat");
927            assert!((config.temperature - 0.1).abs() < f32::EPSILON);
928            assert_eq!(config.max_tokens, 4000);
929            assert!(!config.enable_statistical_testing);
930            assert!((config.alpha - 0.05).abs() < f64::EPSILON);
931            assert!(config.enable_compliance_validation);
932            assert!(!config.enable_meta_cognition);
933            assert!((config.min_confidence - 0.70).abs() < f64::EPSILON);
934            assert_eq!(config.max_chain_length, 20);
935        }
936
937        #[test]
938        fn test_rigorous_config() {
939            let config = DeepSeekValidationConfig::rigorous();
940
941            assert!(config.enable_statistical_testing);
942            assert!(config.enable_compliance_validation);
943            assert!(config.enable_meta_cognition);
944            assert!((config.temperature - 0.05).abs() < f32::EPSILON);
945            assert!((config.min_confidence - 0.85).abs() < f64::EPSILON);
946        }
947
948        #[test]
949        fn test_performance_config() {
950            let config = DeepSeekValidationConfig::performance();
951
952            assert!(!config.enable_statistical_testing);
953            assert!(config.enable_compliance_validation);
954            assert!(!config.enable_meta_cognition);
955            assert!((config.temperature - 0.2).abs() < f32::EPSILON);
956            assert_eq!(config.max_tokens, 2000);
957        }
958
959        #[test]
960        fn test_config_serialization_roundtrip() {
961            let config = DeepSeekValidationConfig::rigorous();
962            let json = serde_json::to_string(&config).unwrap();
963            let deserialized: DeepSeekValidationConfig = serde_json::from_str(&json).unwrap();
964
965            assert_eq!(config.model, deserialized.model);
966            assert!((config.temperature - deserialized.temperature).abs() < f32::EPSILON);
967            assert_eq!(
968                config.enable_statistical_testing,
969                deserialized.enable_statistical_testing
970            );
971        }
972
973        #[test]
974        fn test_config_deserialization_with_defaults() {
975            // Minimal JSON - should use defaults for missing fields
976            let json = r#"{"model": "custom-model"}"#;
977            let config: DeepSeekValidationConfig = serde_json::from_str(json).unwrap();
978
979            assert_eq!(config.model, "custom-model");
980            // All other fields should have defaults
981            assert!((config.temperature - 0.1).abs() < f32::EPSILON);
982            assert_eq!(config.max_tokens, 4000);
983        }
984    }
985
986    // ─────────────────────────────────────────────────────────────────────────────────────────────
987    // TOKEN USAGE TESTS
988    // ─────────────────────────────────────────────────────────────────────────────────────────────
989
990    mod token_usage_tests {
991        use super::*;
992
993        #[test]
994        fn test_token_usage_new() {
995            let usage = TokenUsage::new(100, 50, 0.01);
996
997            assert_eq!(usage.input_tokens, 100);
998            assert_eq!(usage.output_tokens, 50);
999            assert_eq!(usage.total_tokens, 150);
1000            assert!((usage.cost_usd - 0.01).abs() < f64::EPSILON);
1001        }
1002
1003        #[test]
1004        fn test_token_usage_default() {
1005            let usage = TokenUsage::default();
1006
1007            assert_eq!(usage.input_tokens, 0);
1008            assert_eq!(usage.output_tokens, 0);
1009            assert_eq!(usage.total_tokens, 0);
1010            assert!((usage.cost_usd - 0.0).abs() < f64::EPSILON);
1011        }
1012
1013        #[test]
1014        fn test_token_usage_add() {
1015            let mut usage1 = TokenUsage::new(100, 50, 0.01);
1016            let usage2 = TokenUsage::new(200, 100, 0.02);
1017
1018            usage1.add(&usage2);
1019
1020            assert_eq!(usage1.input_tokens, 300);
1021            assert_eq!(usage1.output_tokens, 150);
1022            assert_eq!(usage1.total_tokens, 450);
1023            assert!((usage1.cost_usd - 0.03).abs() < f64::EPSILON);
1024        }
1025
1026        #[test]
1027        fn test_token_usage_add_to_zero() {
1028            let mut usage = TokenUsage::default();
1029            let other = TokenUsage::new(500, 250, 0.05);
1030
1031            usage.add(&other);
1032
1033            assert_eq!(usage.input_tokens, 500);
1034            assert_eq!(usage.output_tokens, 250);
1035            assert_eq!(usage.total_tokens, 750);
1036        }
1037
1038        #[test]
1039        fn test_token_usage_boundary_max_u32() {
1040            let usage = TokenUsage::new(u32::MAX - 10, 5, 0.0);
1041            assert_eq!(usage.total_tokens, u32::MAX - 5);
1042        }
1043
1044        #[test]
1045        fn test_token_usage_serialization() {
1046            let usage = TokenUsage::new(1000, 500, 0.123);
1047            let json = serde_json::to_string(&usage).unwrap();
1048            let deserialized: TokenUsage = serde_json::from_str(&json).unwrap();
1049
1050            assert_eq!(usage.input_tokens, deserialized.input_tokens);
1051            assert_eq!(usage.output_tokens, deserialized.output_tokens);
1052            assert_eq!(usage.total_tokens, deserialized.total_tokens);
1053            assert!((usage.cost_usd - deserialized.cost_usd).abs() < 0.0001);
1054        }
1055    }
1056
1057    // ─────────────────────────────────────────────────────────────────────────────────────────────
1058    // CHAIN INTEGRITY RESULT ARITHMETIC TESTS
1059    // ─────────────────────────────────────────────────────────────────────────────────────────────
1060
1061    mod chain_integrity_arithmetic_tests {
1062        use super::*;
1063
1064        fn create_chain_integrity(continuity_score: f64) -> ChainIntegrityResult {
1065            ChainIntegrityResult {
1066                logical_flow: LogicalFlowStatus::Good,
1067                step_dependencies: DependencyStatus::FullySatisfied,
1068                confidence_progression: ProgressionStatus::Monotonic,
1069                gaps_detected: vec![],
1070                continuity_score,
1071            }
1072        }
1073
1074        #[test]
1075        fn test_chain_integrity_add_f32() {
1076            let result = create_chain_integrity(0.85);
1077            let sum = result + 0.15f32;
1078            assert!((sum - 1.0).abs() < 0.0001);
1079        }
1080
1081        #[test]
1082        fn test_chain_integrity_sub_f32_ref() {
1083            let result = create_chain_integrity(0.85);
1084            let diff = result - &0.35f32;
1085            assert!((diff - 0.5).abs() < 0.0001);
1086        }
1087
1088        #[test]
1089        fn test_chain_integrity_mul_f64() {
1090            let result = create_chain_integrity(0.5);
1091            let product = result * 2.0f64;
1092            assert!((product - 1.0).abs() < f64::EPSILON);
1093        }
1094
1095        #[test]
1096        fn test_chain_integrity_mul_f32() {
1097            let result = create_chain_integrity(0.5);
1098            let product = result * 4.0f32;
1099            assert!((product - 2.0).abs() < 0.0001);
1100        }
1101
1102        #[test]
1103        fn test_chain_integrity_boundary_zero() {
1104            let result = create_chain_integrity(0.0);
1105            let product = result * 100.0f64;
1106            assert!((product - 0.0).abs() < f64::EPSILON);
1107        }
1108
1109        #[test]
1110        fn test_chain_integrity_boundary_one() {
1111            let result = create_chain_integrity(1.0);
1112            let product = result * 0.5f64;
1113            assert!((product - 0.5).abs() < f64::EPSILON);
1114        }
1115    }
1116
1117    // ─────────────────────────────────────────────────────────────────────────────────────────────
1118    // VALIDATION PERFORMANCE TESTS
1119    // ─────────────────────────────────────────────────────────────────────────────────────────────
1120
1121    mod validation_performance_tests {
1122        use super::*;
1123
1124        #[test]
1125        fn test_validation_performance_new() {
1126            let perf = ValidationPerformance::new(1000, 500.0, 128.5);
1127
1128            assert_eq!(perf.duration_ms, 1000);
1129            assert!((perf.tokens_per_second - 500.0).abs() < f64::EPSILON);
1130            assert!((perf.memory_usage_mb - 128.5).abs() < f64::EPSILON);
1131        }
1132
1133        #[test]
1134        fn test_validation_performance_default() {
1135            let perf = ValidationPerformance::default();
1136
1137            assert_eq!(perf.duration_ms, 0);
1138            assert!((perf.tokens_per_second - 0.0).abs() < f64::EPSILON);
1139            assert!((perf.memory_usage_mb - 0.0).abs() < f64::EPSILON);
1140        }
1141
1142        #[test]
1143        fn test_validation_performance_serialization() {
1144            let perf = ValidationPerformance::new(5000, 1200.5, 256.0);
1145            let json = serde_json::to_string(&perf).unwrap();
1146            let deserialized: ValidationPerformance = serde_json::from_str(&json).unwrap();
1147
1148            assert_eq!(perf.duration_ms, deserialized.duration_ms);
1149        }
1150    }
1151
1152    // ─────────────────────────────────────────────────────────────────────────────────────────────
1153    // ENUM SERIALIZATION TESTS
1154    // ─────────────────────────────────────────────────────────────────────────────────────────────
1155
1156    mod enum_serialization_tests {
1157        use super::*;
1158
1159        #[test]
1160        fn test_validation_verdict_serialization() {
1161            let cases = [
1162                (ValidationVerdict::Validated, "\"validated\""),
1163                (
1164                    ValidationVerdict::PartiallyValidated,
1165                    "\"partially_validated\"",
1166                ),
1167                (ValidationVerdict::NeedsImprovement, "\"needs_improvement\""),
1168                (ValidationVerdict::Invalid, "\"invalid\""),
1169                (ValidationVerdict::CriticalIssues, "\"critical_issues\""),
1170            ];
1171
1172            for (verdict, expected_json) in cases {
1173                let json = serde_json::to_string(&verdict).unwrap();
1174                assert_eq!(json, expected_json);
1175
1176                let deserialized: ValidationVerdict = serde_json::from_str(&json).unwrap();
1177                assert_eq!(verdict, deserialized);
1178            }
1179        }
1180
1181        #[test]
1182        fn test_severity_serialization() {
1183            let cases = [
1184                (Severity::Critical, "\"critical\""),
1185                (Severity::High, "\"high\""),
1186                (Severity::Medium, "\"medium\""),
1187                (Severity::Low, "\"low\""),
1188                (Severity::Info, "\"info\""),
1189            ];
1190
1191            for (severity, expected_json) in cases {
1192                let json = serde_json::to_string(&severity).unwrap();
1193                assert_eq!(json, expected_json);
1194
1195                let deserialized: Severity = serde_json::from_str(&json).unwrap();
1196                assert_eq!(severity, deserialized);
1197            }
1198        }
1199
1200        #[test]
1201        fn test_bias_level_serialization() {
1202            let cases = [
1203                (BiasLevel::Minimal, "\"minimal\""),
1204                (BiasLevel::Low, "\"low\""),
1205                (BiasLevel::Moderate, "\"moderate\""),
1206                (BiasLevel::High, "\"high\""),
1207                (BiasLevel::Severe, "\"severe\""),
1208            ];
1209
1210            for (level, expected_json) in cases {
1211                let json = serde_json::to_string(&level).unwrap();
1212                assert_eq!(json, expected_json);
1213            }
1214        }
1215
1216        #[test]
1217        fn test_logical_flow_status_serialization() {
1218            let status = LogicalFlowStatus::Excellent;
1219            let json = serde_json::to_string(&status).unwrap();
1220            assert_eq!(json, "\"excellent\"");
1221        }
1222
1223        #[test]
1224        fn test_dependency_status_serialization() {
1225            let status = DependencyStatus::PartiallySatisfied;
1226            let json = serde_json::to_string(&status).unwrap();
1227            assert_eq!(json, "\"partially_satisfied\"");
1228        }
1229
1230        #[test]
1231        fn test_progression_status_serialization() {
1232            let status = ProgressionStatus::SlowlyDecaying;
1233            let json = serde_json::to_string(&status).unwrap();
1234            assert_eq!(json, "\"slowly_decaying\"");
1235        }
1236
1237        #[test]
1238        fn test_compliance_status_serialization() {
1239            let status = ComplianceStatus::CriticalNonCompliance;
1240            let json = serde_json::to_string(&status).unwrap();
1241            assert_eq!(json, "\"critical_non_compliance\"");
1242        }
1243
1244        #[test]
1245        fn test_regulatory_status_serialization() {
1246            let status = RegulatoryStatus::NeedsValidation;
1247            let json = serde_json::to_string(&status).unwrap();
1248            assert_eq!(json, "\"needs_validation\"");
1249        }
1250
1251        #[test]
1252        fn test_methodology_status_serialization() {
1253            let status = MethodologyStatus::NeedsImprovement;
1254            let json = serde_json::to_string(&status).unwrap();
1255            assert_eq!(json, "\"needs_improvement\"");
1256        }
1257
1258        #[test]
1259        fn test_validation_category_serialization() {
1260            let category = ValidationCategory::EnterpriseCompliance;
1261            let json = serde_json::to_string(&category).unwrap();
1262            assert_eq!(json, "\"enterprise_compliance\"");
1263        }
1264    }
1265
1266    // ─────────────────────────────────────────────────────────────────────────────────────────────
1267    // STATISTICAL RESULT TESTS
1268    // ─────────────────────────────────────────────────────────────────────────────────────────────
1269
1270    mod statistical_result_tests {
1271        use super::*;
1272
1273        #[test]
1274        fn test_statistical_result_significant() {
1275            let result = StatisticalResult {
1276                significant: true,
1277                p_value: Some(0.01),
1278                confidence_interval: Some((0.80, 0.95)),
1279                sample_size: Some(1000),
1280            };
1281
1282            assert!(result.significant);
1283            assert!(result.p_value.unwrap() < 0.05);
1284        }
1285
1286        #[test]
1287        fn test_statistical_result_not_significant() {
1288            let result = StatisticalResult {
1289                significant: false,
1290                p_value: Some(0.15),
1291                confidence_interval: None,
1292                sample_size: Some(50),
1293            };
1294
1295            assert!(!result.significant);
1296            assert!(result.p_value.unwrap() > 0.05);
1297        }
1298
1299        #[test]
1300        fn test_statistical_result_serialization() {
1301            let result = StatisticalResult {
1302                significant: true,
1303                p_value: Some(0.03),
1304                confidence_interval: Some((0.75, 0.95)),
1305                sample_size: Some(500),
1306            };
1307
1308            let json = serde_json::to_string(&result).unwrap();
1309            let deserialized: StatisticalResult = serde_json::from_str(&json).unwrap();
1310
1311            assert_eq!(result.significant, deserialized.significant);
1312            assert_eq!(result.p_value, deserialized.p_value);
1313            assert_eq!(result.confidence_interval, deserialized.confidence_interval);
1314        }
1315
1316        #[test]
1317        fn test_statistical_result_boundary_p_value() {
1318            // Test boundary at alpha = 0.05
1319            let exactly_significant = StatisticalResult {
1320                significant: true,
1321                p_value: Some(0.049),
1322                confidence_interval: None,
1323                sample_size: None,
1324            };
1325            assert!(exactly_significant.p_value.unwrap() < 0.05);
1326
1327            let not_significant = StatisticalResult {
1328                significant: false,
1329                p_value: Some(0.051),
1330                confidence_interval: None,
1331                sample_size: None,
1332            };
1333            assert!(not_significant.p_value.unwrap() > 0.05);
1334        }
1335    }
1336
1337    // ─────────────────────────────────────────────────────────────────────────────────────────────
1338    // VALIDATION FINDING TESTS
1339    // ─────────────────────────────────────────────────────────────────────────────────────────────
1340
1341    mod validation_finding_tests {
1342        use super::*;
1343
1344        #[test]
1345        fn test_validation_finding_creation() {
1346            let finding = ValidationFinding {
1347                category: ValidationCategory::LogicalFlow,
1348                severity: Severity::High,
1349                description: "Gap detected between steps 2 and 3".to_string(),
1350                affected_steps: vec!["step_2".to_string(), "step_3".to_string()],
1351                evidence: vec!["Missing intermediate reasoning".to_string()],
1352                recommendations: vec!["Add bridging step".to_string()],
1353            };
1354
1355            assert_eq!(finding.category, ValidationCategory::LogicalFlow);
1356            assert_eq!(finding.severity, Severity::High);
1357            assert_eq!(finding.affected_steps.len(), 2);
1358        }
1359
1360        #[test]
1361        fn test_validation_finding_with_unicode() {
1362            let finding = ValidationFinding {
1363                category: ValidationCategory::BiasDetection,
1364                severity: Severity::Medium,
1365                description: "Unicode test: \u{1F4A1} \u{1F4CA} \u{2705}".to_string(),
1366                affected_steps: vec!["step_\u{03B1}".to_string()],
1367                evidence: vec!["Evidence with emoji: \u{1F914}".to_string()],
1368                recommendations: vec!["\u{2728} Improve coverage".to_string()],
1369            };
1370
1371            let json = serde_json::to_string(&finding).unwrap();
1372            let deserialized: ValidationFinding = serde_json::from_str(&json).unwrap();
1373
1374            assert!(deserialized.description.contains('\u{1F4A1}'));
1375        }
1376
1377        #[test]
1378        fn test_validation_finding_empty_vectors() {
1379            let finding = ValidationFinding {
1380                category: ValidationCategory::Methodology,
1381                severity: Severity::Info,
1382                description: "Minor observation".to_string(),
1383                affected_steps: vec![],
1384                evidence: vec![],
1385                recommendations: vec![],
1386            };
1387
1388            assert!(finding.affected_steps.is_empty());
1389            assert!(finding.evidence.is_empty());
1390            assert!(finding.recommendations.is_empty());
1391        }
1392    }
1393
1394    // ─────────────────────────────────────────────────────────────────────────────────────────────
1395    // DETECTED BIAS TESTS
1396    // ─────────────────────────────────────────────────────────────────────────────────────────────
1397
1398    mod detected_bias_tests {
1399        use super::*;
1400
1401        #[test]
1402        fn test_detected_bias_creation() {
1403            let bias = DetectedBias {
1404                bias_type: "confirmation_bias".to_string(),
1405                evidence: "Selectively interpreting data".to_string(),
1406                severity: Severity::Medium,
1407                detection_confidence: 0.85,
1408            };
1409
1410            assert_eq!(bias.bias_type, "confirmation_bias");
1411            assert!((bias.detection_confidence - 0.85).abs() < f64::EPSILON);
1412        }
1413
1414        #[test]
1415        fn test_detected_bias_confidence_boundaries() {
1416            let low_confidence = DetectedBias {
1417                bias_type: "anchoring".to_string(),
1418                evidence: "Weak signal".to_string(),
1419                severity: Severity::Low,
1420                detection_confidence: 0.0,
1421            };
1422            assert!((low_confidence.detection_confidence - 0.0).abs() < f64::EPSILON);
1423
1424            let high_confidence = DetectedBias {
1425                bias_type: "anchoring".to_string(),
1426                evidence: "Strong signal".to_string(),
1427                severity: Severity::Critical,
1428                detection_confidence: 1.0,
1429            };
1430            assert!((high_confidence.detection_confidence - 1.0).abs() < f64::EPSILON);
1431        }
1432    }
1433
1434    // ─────────────────────────────────────────────────────────────────────────────────────────────
1435    // COMPLIANCE VIOLATION TESTS
1436    // ─────────────────────────────────────────────────────────────────────────────────────────────
1437
1438    mod compliance_violation_tests {
1439        use super::*;
1440
1441        #[test]
1442        fn test_compliance_violation_creation() {
1443            let violation = ComplianceViolation {
1444                violation_type: "GDPR_ARTICLE_17".to_string(),
1445                severity: Severity::Critical,
1446                description: "Right to erasure not implemented".to_string(),
1447                remediation: vec![
1448                    "Implement data deletion endpoint".to_string(),
1449                    "Add audit logging".to_string(),
1450                ],
1451            };
1452
1453            assert_eq!(violation.violation_type, "GDPR_ARTICLE_17");
1454            assert_eq!(violation.remediation.len(), 2);
1455        }
1456
1457        #[test]
1458        fn test_compliance_violation_serialization() {
1459            let violation = ComplianceViolation {
1460                violation_type: "SOX_COMPLIANCE".to_string(),
1461                severity: Severity::High,
1462                description: "Audit trail incomplete".to_string(),
1463                remediation: vec!["Add comprehensive logging".to_string()],
1464            };
1465
1466            let json = serde_json::to_string(&violation).unwrap();
1467            assert!(json.contains("SOX_COMPLIANCE"));
1468
1469            let deserialized: ComplianceViolation = serde_json::from_str(&json).unwrap();
1470            assert_eq!(violation.violation_type, deserialized.violation_type);
1471        }
1472    }
1473
1474    // ─────────────────────────────────────────────────────────────────────────────────────────────
1475    // BIAS DETECTION RESULT TESTS
1476    // ─────────────────────────────────────────────────────────────────────────────────────────────
1477
1478    mod bias_detection_result_tests {
1479        use super::*;
1480
1481        #[test]
1482        fn test_bias_detection_result_no_biases() {
1483            let result = BiasDetectionResult {
1484                overall_bias: BiasLevel::Minimal,
1485                detected_biases: vec![],
1486                mitigation_recommendations: vec![],
1487            };
1488
1489            assert_eq!(result.overall_bias, BiasLevel::Minimal);
1490            assert!(result.detected_biases.is_empty());
1491        }
1492
1493        #[test]
1494        fn test_bias_detection_result_multiple_biases() {
1495            let result = BiasDetectionResult {
1496                overall_bias: BiasLevel::Moderate,
1497                detected_biases: vec![
1498                    DetectedBias {
1499                        bias_type: "confirmation".to_string(),
1500                        evidence: "Evidence 1".to_string(),
1501                        severity: Severity::Medium,
1502                        detection_confidence: 0.7,
1503                    },
1504                    DetectedBias {
1505                        bias_type: "availability".to_string(),
1506                        evidence: "Evidence 2".to_string(),
1507                        severity: Severity::Low,
1508                        detection_confidence: 0.6,
1509                    },
1510                ],
1511                mitigation_recommendations: vec![
1512                    "Consider alternative perspectives".to_string(),
1513                    "Use structured decision frameworks".to_string(),
1514                ],
1515            };
1516
1517            assert_eq!(result.detected_biases.len(), 2);
1518            assert_eq!(result.mitigation_recommendations.len(), 2);
1519        }
1520    }
1521
1522    // ─────────────────────────────────────────────────────────────────────────────────────────────
1523    // META-COGNITIVE RESULT TESTS
1524    // ─────────────────────────────────────────────────────────────────────────────────────────────
1525
1526    mod meta_cognitive_result_tests {
1527        use super::*;
1528
1529        #[test]
1530        fn test_meta_cognitive_result_creation() {
1531            let result = MetaCognitiveResult {
1532                reasoning_quality: 85.5,
1533                methodology_quality: MethodologyStatus::Good,
1534                cognitive_biases: vec!["overconfidence".to_string()],
1535                recommendations: vec!["Seek external validation".to_string()],
1536            };
1537
1538            assert!((result.reasoning_quality - 85.5).abs() < f64::EPSILON);
1539            assert_eq!(result.methodology_quality, MethodologyStatus::Good);
1540        }
1541
1542        #[test]
1543        fn test_meta_cognitive_result_boundary_scores() {
1544            let min_score = MetaCognitiveResult {
1545                reasoning_quality: 0.0,
1546                methodology_quality: MethodologyStatus::Poor,
1547                cognitive_biases: vec![],
1548                recommendations: vec![],
1549            };
1550            assert!((min_score.reasoning_quality - 0.0).abs() < f64::EPSILON);
1551
1552            let max_score = MetaCognitiveResult {
1553                reasoning_quality: 100.0,
1554                methodology_quality: MethodologyStatus::Excellent,
1555                cognitive_biases: vec![],
1556                recommendations: vec![],
1557            };
1558            assert!((max_score.reasoning_quality - 100.0).abs() < f64::EPSILON);
1559        }
1560    }
1561
1562    // ─────────────────────────────────────────────────────────────────────────────────────────────
1563    // CHAIN INTEGRITY RESULT TESTS
1564    // ─────────────────────────────────────────────────────────────────────────────────────────────
1565
1566    mod chain_integrity_result_tests {
1567        use super::*;
1568
1569        #[test]
1570        fn test_chain_integrity_result_creation() {
1571            let result = ChainIntegrityResult {
1572                logical_flow: LogicalFlowStatus::Excellent,
1573                step_dependencies: DependencyStatus::FullySatisfied,
1574                confidence_progression: ProgressionStatus::Monotonic,
1575                gaps_detected: vec![],
1576                continuity_score: 0.95,
1577            };
1578
1579            assert_eq!(result.logical_flow, LogicalFlowStatus::Excellent);
1580            assert!((result.continuity_score - 0.95).abs() < f64::EPSILON);
1581        }
1582
1583        #[test]
1584        fn test_chain_integrity_result_with_gaps() {
1585            let result = ChainIntegrityResult {
1586                logical_flow: LogicalFlowStatus::NeedsImprovement,
1587                step_dependencies: DependencyStatus::PartiallySatisfied,
1588                confidence_progression: ProgressionStatus::Erratic,
1589                gaps_detected: vec![
1590                    "Missing justification between step 1 and 2".to_string(),
1591                    "Unexplained confidence drop at step 4".to_string(),
1592                ],
1593                continuity_score: 0.45,
1594            };
1595
1596            assert_eq!(result.gaps_detected.len(), 2);
1597            assert!(result.continuity_score < 0.5);
1598        }
1599
1600        #[test]
1601        fn test_chain_integrity_continuity_score_boundaries() {
1602            let zero_score = ChainIntegrityResult {
1603                logical_flow: LogicalFlowStatus::Poor,
1604                step_dependencies: DependencyStatus::Unsatisfied,
1605                confidence_progression: ProgressionStatus::Unstable,
1606                gaps_detected: vec![],
1607                continuity_score: 0.0,
1608            };
1609            assert!((zero_score.continuity_score - 0.0).abs() < f64::EPSILON);
1610
1611            let perfect_score = ChainIntegrityResult {
1612                logical_flow: LogicalFlowStatus::Excellent,
1613                step_dependencies: DependencyStatus::FullySatisfied,
1614                confidence_progression: ProgressionStatus::Monotonic,
1615                gaps_detected: vec![],
1616                continuity_score: 1.0,
1617            };
1618            assert!((perfect_score.continuity_score - 1.0).abs() < f64::EPSILON);
1619        }
1620    }
1621
1622    // ─────────────────────────────────────────────────────────────────────────────────────────────
1623    // DEEPSEEK VALIDATION RESULT TESTS
1624    // ─────────────────────────────────────────────────────────────────────────────────────────────
1625
1626    mod deepseek_validation_result_tests {
1627        use super::*;
1628
1629        fn create_minimal_result() -> DeepSeekValidationResult {
1630            DeepSeekValidationResult {
1631                verdict: ValidationVerdict::Validated,
1632                chain_integrity: ChainIntegrityResult {
1633                    logical_flow: LogicalFlowStatus::Good,
1634                    step_dependencies: DependencyStatus::FullySatisfied,
1635                    confidence_progression: ProgressionStatus::Monotonic,
1636                    gaps_detected: vec![],
1637                    continuity_score: 0.85,
1638                },
1639                statistical_results: None,
1640                compliance_results: None,
1641                meta_cognitive_results: None,
1642                validation_confidence: 0.90,
1643                findings: vec![],
1644                tokens_used: TokenUsage::default(),
1645                performance: ValidationPerformance::default(),
1646            }
1647        }
1648
1649        #[test]
1650        fn test_validation_result_minimal() {
1651            let result = create_minimal_result();
1652
1653            assert_eq!(result.verdict, ValidationVerdict::Validated);
1654            assert!(result.statistical_results.is_none());
1655            assert!(result.compliance_results.is_none());
1656            assert!(result.meta_cognitive_results.is_none());
1657        }
1658
1659        #[test]
1660        fn test_validation_result_serialization_skip_none() {
1661            let result = create_minimal_result();
1662            let json = serde_json::to_string(&result).unwrap();
1663
1664            // Optional None fields should be skipped due to skip_serializing_if
1665            assert!(!json.contains("statistical_results"));
1666            assert!(!json.contains("compliance_results"));
1667            assert!(!json.contains("meta_cognitive_results"));
1668        }
1669
1670        #[test]
1671        fn test_validation_result_with_all_options() {
1672            let result = DeepSeekValidationResult {
1673                verdict: ValidationVerdict::PartiallyValidated,
1674                chain_integrity: ChainIntegrityResult {
1675                    logical_flow: LogicalFlowStatus::Satisfactory,
1676                    step_dependencies: DependencyStatus::MostlySatisfied,
1677                    confidence_progression: ProgressionStatus::SlowlyDecaying,
1678                    gaps_detected: vec!["Minor gap".to_string()],
1679                    continuity_score: 0.75,
1680                },
1681                statistical_results: Some(StatisticalResult {
1682                    significant: true,
1683                    p_value: Some(0.02),
1684                    confidence_interval: Some((0.70, 0.90)),
1685                    sample_size: Some(100),
1686                }),
1687                compliance_results: Some(ComplianceResult {
1688                    gdpr_compliance: ComplianceStatus::MinorIssues,
1689                    bias_detection: BiasDetectionResult {
1690                        overall_bias: BiasLevel::Low,
1691                        detected_biases: vec![],
1692                        mitigation_recommendations: vec![],
1693                    },
1694                    regulatory_alignment: RegulatoryStatus::NeedsValidation,
1695                    violations: vec![],
1696                }),
1697                meta_cognitive_results: Some(MetaCognitiveResult {
1698                    reasoning_quality: 78.0,
1699                    methodology_quality: MethodologyStatus::Good,
1700                    cognitive_biases: vec![],
1701                    recommendations: vec![],
1702                }),
1703                validation_confidence: 0.80,
1704                findings: vec![ValidationFinding {
1705                    category: ValidationCategory::LogicalFlow,
1706                    severity: Severity::Low,
1707                    description: "Minor observation".to_string(),
1708                    affected_steps: vec![],
1709                    evidence: vec![],
1710                    recommendations: vec![],
1711                }],
1712                tokens_used: TokenUsage::new(1000, 500, 0.05),
1713                performance: ValidationPerformance::new(2000, 750.0, 64.0),
1714            };
1715
1716            let json = serde_json::to_string(&result).unwrap();
1717            let deserialized: DeepSeekValidationResult = serde_json::from_str(&json).unwrap();
1718
1719            assert_eq!(result.verdict, deserialized.verdict);
1720            assert!(deserialized.statistical_results.is_some());
1721            assert!(deserialized.compliance_results.is_some());
1722            assert!(deserialized.meta_cognitive_results.is_some());
1723        }
1724    }
1725
1726    // ─────────────────────────────────────────────────────────────────────────────────────────────
1727    // COMPLIANCE RESULT TESTS
1728    // ─────────────────────────────────────────────────────────────────────────────────────────────
1729
1730    mod compliance_result_tests {
1731        use super::*;
1732
1733        #[test]
1734        fn test_compliance_result_fully_compliant() {
1735            let result = ComplianceResult {
1736                gdpr_compliance: ComplianceStatus::Compliant,
1737                bias_detection: BiasDetectionResult {
1738                    overall_bias: BiasLevel::Minimal,
1739                    detected_biases: vec![],
1740                    mitigation_recommendations: vec![],
1741                },
1742                regulatory_alignment: RegulatoryStatus::FullyCompliant,
1743                violations: vec![],
1744            };
1745
1746            assert_eq!(result.gdpr_compliance, ComplianceStatus::Compliant);
1747            assert!(result.violations.is_empty());
1748        }
1749
1750        #[test]
1751        fn test_compliance_result_with_violations() {
1752            let result = ComplianceResult {
1753                gdpr_compliance: ComplianceStatus::NonCompliant,
1754                bias_detection: BiasDetectionResult {
1755                    overall_bias: BiasLevel::High,
1756                    detected_biases: vec![DetectedBias {
1757                        bias_type: "demographic".to_string(),
1758                        evidence: "Unequal treatment detected".to_string(),
1759                        severity: Severity::High,
1760                        detection_confidence: 0.9,
1761                    }],
1762                    mitigation_recommendations: vec!["Implement fairness constraints".to_string()],
1763                },
1764                regulatory_alignment: RegulatoryStatus::NonCompliant,
1765                violations: vec![
1766                    ComplianceViolation {
1767                        violation_type: "GDPR_CONSENT".to_string(),
1768                        severity: Severity::Critical,
1769                        description: "Missing consent mechanism".to_string(),
1770                        remediation: vec!["Add consent UI".to_string()],
1771                    },
1772                    ComplianceViolation {
1773                        violation_type: "GDPR_DATA_RETENTION".to_string(),
1774                        severity: Severity::High,
1775                        description: "Data retained beyond policy".to_string(),
1776                        remediation: vec!["Implement data lifecycle management".to_string()],
1777                    },
1778                ],
1779            };
1780
1781            assert_eq!(result.violations.len(), 2);
1782            assert_eq!(result.bias_detection.detected_biases.len(), 1);
1783        }
1784    }
1785
1786    // ─────────────────────────────────────────────────────────────────────────────────────────────
1787    // UNICODE AND SPECIAL CHARACTER TESTS
1788    // ─────────────────────────────────────────────────────────────────────────────────────────────
1789
1790    mod unicode_tests {
1791        use super::*;
1792
1793        #[test]
1794        fn test_unicode_in_chain_integrity_gaps() {
1795            let result = ChainIntegrityResult {
1796                logical_flow: LogicalFlowStatus::Satisfactory,
1797                step_dependencies: DependencyStatus::MostlySatisfied,
1798                confidence_progression: ProgressionStatus::SlowlyDecaying,
1799                gaps_detected: vec![
1800                    "\u{4E2D}\u{6587}\u{6D4B}\u{8BD5}".to_string(), // Chinese: "Chinese test"
1801                    "\u{65E5}\u{672C}\u{8A9E}".to_string(),         // Japanese: "Japanese"
1802                    "\u{D55C}\u{AD6D}\u{C5B4}".to_string(),         // Korean: "Korean"
1803                    "\u{0410}\u{0411}\u{0412}".to_string(),         // Russian: "ABV"
1804                    "\u{05D0}\u{05D1}\u{05D2}".to_string(),         // Hebrew: "Alef Bet Gimel"
1805                    "\u{0627}\u{0628}\u{062A}".to_string(),         // Arabic: letters
1806                ],
1807                continuity_score: 0.7,
1808            };
1809
1810            let json = serde_json::to_string(&result).unwrap();
1811            let deserialized: ChainIntegrityResult = serde_json::from_str(&json).unwrap();
1812
1813            assert_eq!(result.gaps_detected.len(), deserialized.gaps_detected.len());
1814            for (original, parsed) in result
1815                .gaps_detected
1816                .iter()
1817                .zip(deserialized.gaps_detected.iter())
1818            {
1819                assert_eq!(original, parsed);
1820            }
1821        }
1822
1823        #[test]
1824        fn test_unicode_emoji_in_descriptions() {
1825            let finding = ValidationFinding {
1826                category: ValidationCategory::Methodology,
1827                severity: Severity::Info,
1828                description: "Status: \u{2705} Pass \u{274C} Fail \u{26A0} Warning".to_string(),
1829                affected_steps: vec!["\u{1F3AF} Target Step".to_string()],
1830                evidence: vec!["\u{1F4CA} Chart data shows trend".to_string()],
1831                recommendations: vec!["\u{1F4A1} Consider improvement".to_string()],
1832            };
1833
1834            let json = serde_json::to_string(&finding).unwrap();
1835            let deserialized: ValidationFinding = serde_json::from_str(&json).unwrap();
1836
1837            assert!(deserialized.description.contains('\u{2705}'));
1838            assert!(deserialized.description.contains('\u{274C}'));
1839        }
1840
1841        #[test]
1842        fn test_special_characters_in_strings() {
1843            let violation = ComplianceViolation {
1844                violation_type: "TEST_<>\"'&".to_string(),
1845                severity: Severity::Low,
1846                description: "Special chars: <script>alert('xss')</script>".to_string(),
1847                remediation: vec!["Use proper escaping: &amp; &lt; &gt;".to_string()],
1848            };
1849
1850            let json = serde_json::to_string(&violation).unwrap();
1851            let deserialized: ComplianceViolation = serde_json::from_str(&json).unwrap();
1852
1853            assert!(deserialized.violation_type.contains('<'));
1854            assert!(deserialized.violation_type.contains('>'));
1855        }
1856
1857        #[test]
1858        fn test_empty_strings() {
1859            let bias = DetectedBias {
1860                bias_type: "".to_string(),
1861                evidence: "".to_string(),
1862                severity: Severity::Info,
1863                detection_confidence: 0.0,
1864            };
1865
1866            let json = serde_json::to_string(&bias).unwrap();
1867            let deserialized: DetectedBias = serde_json::from_str(&json).unwrap();
1868
1869            assert!(deserialized.bias_type.is_empty());
1870            assert!(deserialized.evidence.is_empty());
1871        }
1872
1873        #[test]
1874        fn test_very_long_strings() {
1875            let long_string = "A".repeat(100_000);
1876
1877            let finding = ValidationFinding {
1878                category: ValidationCategory::TokenEfficiency,
1879                severity: Severity::Low,
1880                description: long_string.clone(),
1881                affected_steps: vec![],
1882                evidence: vec![],
1883                recommendations: vec![],
1884            };
1885
1886            let json = serde_json::to_string(&finding).unwrap();
1887            let deserialized: ValidationFinding = serde_json::from_str(&json).unwrap();
1888
1889            assert_eq!(deserialized.description.len(), 100_000);
1890        }
1891    }
1892
1893    // ─────────────────────────────────────────────────────────────────────────────────────────────
1894    // PROPERTY-BASED TESTS (using proptest)
1895    // ─────────────────────────────────────────────────────────────────────────────────────────────
1896
1897    mod property_tests {
1898        use super::*;
1899
1900        proptest! {
1901            #[test]
1902            fn test_token_usage_add_is_associative(
1903                input1 in 0u32..1000,
1904                output1 in 0u32..1000,
1905                cost1 in 0.0f64..10.0,
1906                input2 in 0u32..1000,
1907                output2 in 0u32..1000,
1908                cost2 in 0.0f64..10.0,
1909            ) {
1910                let mut usage1 = TokenUsage::new(input1, output1, cost1);
1911                let usage2 = TokenUsage::new(input2, output2, cost2);
1912
1913                usage1.add(&usage2);
1914
1915                prop_assert_eq!(usage1.input_tokens, input1 + input2);
1916                prop_assert_eq!(usage1.output_tokens, output1 + output2);
1917                prop_assert_eq!(usage1.total_tokens, input1 + output1 + input2 + output2);
1918                prop_assert!((usage1.cost_usd - (cost1 + cost2)).abs() < 0.0001);
1919            }
1920
1921            #[test]
1922            fn test_token_usage_new_total_is_sum(input in 0u32..u32::MAX/2, output in 0u32..u32::MAX/2) {
1923                let usage = TokenUsage::new(input, output, 0.0);
1924                prop_assert_eq!(usage.total_tokens, input + output);
1925            }
1926
1927            #[test]
1928            fn test_chain_integrity_mul_f64_preserves_zero(rhs in -1000.0f64..1000.0) {
1929                let result = ChainIntegrityResult {
1930                    logical_flow: LogicalFlowStatus::Good,
1931                    step_dependencies: DependencyStatus::FullySatisfied,
1932                    confidence_progression: ProgressionStatus::Monotonic,
1933                    gaps_detected: vec![],
1934                    continuity_score: 0.0,
1935                };
1936
1937                let product = result * rhs;
1938                prop_assert!((product - 0.0).abs() < f64::EPSILON);
1939            }
1940
1941            #[test]
1942            fn test_chain_integrity_mul_identity(score in 0.0f64..1.0) {
1943                let result = ChainIntegrityResult {
1944                    logical_flow: LogicalFlowStatus::Good,
1945                    step_dependencies: DependencyStatus::FullySatisfied,
1946                    confidence_progression: ProgressionStatus::Monotonic,
1947                    gaps_detected: vec![],
1948                    continuity_score: score,
1949                };
1950
1951                let product = result * 1.0f64;
1952                prop_assert!((product - score).abs() < f64::EPSILON);
1953            }
1954
1955            #[test]
1956            fn test_validation_performance_serialization_roundtrip(
1957                duration in 0u64..u64::MAX,
1958                tps in 0.0f64..10000.0,
1959                memory in 0.0f64..10000.0,
1960            ) {
1961                let perf = ValidationPerformance::new(duration, tps, memory);
1962                let json = serde_json::to_string(&perf).unwrap();
1963                let deserialized: ValidationPerformance = serde_json::from_str(&json).unwrap();
1964
1965                prop_assert_eq!(perf.duration_ms, deserialized.duration_ms);
1966                // Floating point comparison with tolerance
1967                prop_assert!((perf.tokens_per_second - deserialized.tokens_per_second).abs() < 0.0001);
1968            }
1969
1970            #[test]
1971            fn test_config_temperature_in_valid_range(temp in 0.0f32..2.0) {
1972                // Temperature should be preserved through serialization
1973                let config = DeepSeekValidationConfig {
1974                    temperature: temp,
1975                    ..Default::default()
1976                };
1977
1978                let json = serde_json::to_string(&config).unwrap();
1979                let deserialized: DeepSeekValidationConfig = serde_json::from_str(&json).unwrap();
1980
1981                prop_assert!((config.temperature - deserialized.temperature).abs() < 0.0001);
1982            }
1983
1984            #[test]
1985            fn test_arbitrary_string_in_gaps(s in "\\PC*") {
1986                // Test that any valid UTF-8 string can be stored in gaps
1987                let result = ChainIntegrityResult {
1988                    logical_flow: LogicalFlowStatus::Good,
1989                    step_dependencies: DependencyStatus::FullySatisfied,
1990                    confidence_progression: ProgressionStatus::Monotonic,
1991                    gaps_detected: vec![s.clone()],
1992                    continuity_score: 0.5,
1993                };
1994
1995                let json = serde_json::to_string(&result).unwrap();
1996                let deserialized: ChainIntegrityResult = serde_json::from_str(&json).unwrap();
1997
1998                prop_assert_eq!(result.gaps_detected, deserialized.gaps_detected);
1999            }
2000        }
2001    }
2002
2003    // ─────────────────────────────────────────────────────────────────────────────────────────────
2004    // VALIDATION ENGINE TESTS (Requires API key - skipped in CI)
2005    // ─────────────────────────────────────────────────────────────────────────────────────────────
2006
2007    mod engine_tests {
2008        use super::*;
2009
2010        #[tokio::test]
2011        async fn test_validation_engine_creation() {
2012            // This test may fail if DEEPSEEK_API_KEY is not set
2013            // In production, this would be mocked
2014            let result = DeepSeekValidationEngine::new();
2015
2016            // Engine creation should succeed regardless of API key (key checked on use)
2017            assert!(result.is_ok());
2018
2019            if let Ok(engine) = result {
2020                assert_eq!(engine.config.model, "deepseek-chat");
2021            }
2022        }
2023
2024        #[test]
2025        fn test_configuration_options() {
2026            let config = DeepSeekValidationConfig::rigorous();
2027            assert!(config.enable_statistical_testing);
2028            assert!(config.enable_compliance_validation);
2029            assert!(config.enable_meta_cognition);
2030
2031            let perf_config = DeepSeekValidationConfig::performance();
2032            assert!(!perf_config.enable_statistical_testing);
2033            assert!(perf_config.enable_compliance_validation);
2034        }
2035
2036        #[test]
2037        fn test_validation_engine_with_custom_config() {
2038            let config = DeepSeekValidationConfig {
2039                model: "deepseek-coder".to_string(),
2040                temperature: 0.0,
2041                max_tokens: 8000,
2042                enable_statistical_testing: true,
2043                alpha: 0.01,
2044                enable_compliance_validation: true,
2045                enable_meta_cognition: true,
2046                min_confidence: 0.90,
2047                max_chain_length: 50,
2048            };
2049
2050            let result = DeepSeekValidationEngine::with_config(config);
2051            assert!(result.is_ok());
2052
2053            if let Ok(engine) = result {
2054                assert_eq!(engine.config.model, "deepseek-coder");
2055                assert_eq!(engine.config.max_tokens, 8000);
2056                assert!((engine.config.alpha - 0.01).abs() < f64::EPSILON);
2057            }
2058        }
2059    }
2060
2061    // ─────────────────────────────────────────────────────────────────────────────────────────────
2062    // EDGE CASE TESTS
2063    // ─────────────────────────────────────────────────────────────────────────────────────────────
2064
2065    mod edge_case_tests {
2066        use super::*;
2067
2068        #[test]
2069        fn test_token_usage_zero_values() {
2070            let usage = TokenUsage::new(0, 0, 0.0);
2071            assert_eq!(usage.total_tokens, 0);
2072            assert!((usage.cost_usd - 0.0).abs() < f64::EPSILON);
2073        }
2074
2075        #[test]
2076        fn test_validation_performance_zero_duration() {
2077            let perf = ValidationPerformance::new(0, 0.0, 0.0);
2078            // Division by zero should be handled gracefully in actual usage
2079            assert_eq!(perf.duration_ms, 0);
2080        }
2081
2082        #[test]
2083        fn test_continuity_score_exactly_zero() {
2084            let result = ChainIntegrityResult {
2085                logical_flow: LogicalFlowStatus::Poor,
2086                step_dependencies: DependencyStatus::Unsatisfied,
2087                confidence_progression: ProgressionStatus::Unstable,
2088                gaps_detected: vec![],
2089                continuity_score: 0.0,
2090            };
2091
2092            // Multiplying by any value should still be zero
2093            let product = result.clone() * 100.0f64;
2094            assert!((product - 0.0).abs() < f64::EPSILON);
2095        }
2096
2097        #[test]
2098        fn test_continuity_score_exactly_one() {
2099            let result = ChainIntegrityResult {
2100                logical_flow: LogicalFlowStatus::Excellent,
2101                step_dependencies: DependencyStatus::FullySatisfied,
2102                confidence_progression: ProgressionStatus::Monotonic,
2103                gaps_detected: vec![],
2104                continuity_score: 1.0,
2105            };
2106
2107            // Multiplying by identity should preserve value
2108            let product = result * 1.0f64;
2109            assert!((product - 1.0).abs() < f64::EPSILON);
2110        }
2111
2112        #[test]
2113        fn test_negative_cost_handling() {
2114            // While unusual, negative cost might be used for credits/refunds
2115            let usage = TokenUsage::new(100, 50, -0.01);
2116            assert!(usage.cost_usd < 0.0);
2117        }
2118
2119        #[test]
2120        fn test_very_small_confidence_values() {
2121            let result = StatisticalResult {
2122                significant: true,
2123                p_value: Some(1e-100),
2124                confidence_interval: Some((0.999999, 0.9999999)),
2125                sample_size: Some(1_000_000),
2126            };
2127
2128            let json = serde_json::to_string(&result).unwrap();
2129            let deserialized: StatisticalResult = serde_json::from_str(&json).unwrap();
2130
2131            // Very small p-values should be preserved
2132            assert!(deserialized.p_value.unwrap() < 1e-50);
2133        }
2134
2135        #[test]
2136        fn test_default_function_values() {
2137            // Test all default functions directly
2138            assert_eq!(default_deepseek_model(), "deepseek-chat");
2139            assert!((default_validation_temperature() - 0.1).abs() < f32::EPSILON);
2140            assert_eq!(default_max_tokens(), 4000);
2141            assert!((default_alpha() - 0.05).abs() < f64::EPSILON);
2142            assert!((default_min_confidence() - 0.70).abs() < f64::EPSILON);
2143            assert_eq!(default_max_chain_length(), 20);
2144        }
2145    }
2146}