pmat 3.30.1

PMAT - Zero-config AI context generation and code quality toolkit (CLI, MCP)
Documentation
// Tests for lint hotspot handlers
// Extracted for file health compliance (CB-040)


mod tests {
    use super::*;

    fn create_test_hotspot_result() -> LintHotspotResult {
        LintHotspotResult {
            hotspot: LintHotspot {
                file: PathBuf::from("src/main.rs"),
                defect_density: 0.05,
                total_violations: 5,
                sloc: 100,
                severity_distribution: SeverityDistribution {
                    error: 2,
                    warning: 3,
                    suggestion: 0,
                    note: 0,
                },
                top_lints: vec![
                    ("clippy::too_many_arguments".to_string(), 2),
                    ("unused_variable".to_string(), 3),
                ],
                detailed_violations: vec![],
            },
            all_violations: vec![],
            summary_by_file: std::collections::HashMap::new(),
            total_project_violations: 5,
            enforcement: None,
            refactor_chain: None,
            quality_gate: QualityGateStatus {
                passed: true,
                violations: vec![],
                blocking: false,
            },
        }
    }

    /// Test that enforce flag exits with non-zero status when there are violations
    ///
    /// # Example
    ///
    /// ```
    /// use pmat::cli::handlers::lint_hotspot_handlers::should_exit_with_error;
    ///
    /// // With enforce flag and violations - should exit with error
    /// let should_exit = should_exit_with_error(true, true, 5);
    /// assert!(should_exit);
    ///
    /// // Without enforce flag but quality gate failed - should exit with error  
    /// let should_exit = should_exit_with_error(false, false, 5);
    /// assert!(should_exit);
    ///
    /// // Without enforce flag and no violations - should not exit with error
    /// let should_exit = should_exit_with_error(true, false, 0);
    /// assert!(!should_exit);
    /// ```
    fn should_exit_with_error(
        quality_gate_passed: bool,
        enforce: bool,
        total_violations: usize,
    ) -> bool {
        !quality_gate_passed || (enforce && total_violations > 0)
    }

    #[test]
    fn test_enforce_flag_behavior() {
        // Test 1: Enforce flag with violations should trigger exit
        assert!(should_exit_with_error(true, true, 5));

        // Test 2: Enforce flag without violations should not trigger exit
        assert!(!should_exit_with_error(true, true, 0));

        // Test 3: No enforce flag with violations should not trigger exit
        assert!(!should_exit_with_error(true, false, 5));

        // Test 4: Quality gate failed should always trigger exit
        assert!(should_exit_with_error(false, false, 0));
        assert!(should_exit_with_error(false, true, 5));
    }

    #[test]
    fn test_format_summary_with_violations() {
        let result = create_test_hotspot_result();
        let output = format_summary(&result, false, std::time::Duration::from_secs(1), 10).unwrap();

        assert!(output.contains("# Lint Hotspot Analysis"));
        assert!(output.contains("**Total Project Violations**: 5"));
        assert!(output.contains("## Top Files with Lint Issues"));
        assert!(output.contains("## Hottest File Details"));
        assert!(output.contains("**File**: src/main.rs"));
    }

    #[test]
    fn test_quality_gate_enforcement_scenario() {
        let mut result = create_test_hotspot_result();

        // Test case 1: Quality gate passes but enforce flag is set with violations
        result.quality_gate.passed = true;
        result.total_project_violations = 10;

        let should_exit = should_exit_with_error(
            result.quality_gate.passed,
            true, // enforce flag
            result.total_project_violations,
        );
        assert!(
            should_exit,
            "Should exit with error when enforce flag is set and violations exist"
        );

        // Test case 2: Quality gate passes, enforce flag set, no violations
        result.total_project_violations = 0;

        let should_exit = should_exit_with_error(
            result.quality_gate.passed,
            true, // enforce flag
            result.total_project_violations,
        );
        assert!(
            !should_exit,
            "Should not exit with error when enforce flag is set but no violations"
        );
    }

    #[test]
    fn test_multiple_enforcement_scenarios() {
        // Scenario 1: Quality gate failed, no enforce flag
        assert!(should_exit_with_error(false, false, 0));

        // Scenario 2: Quality gate passed, enforce flag, violations present
        assert!(should_exit_with_error(true, true, 1));

        // Scenario 3: Quality gate passed, enforce flag, no violations
        assert!(!should_exit_with_error(true, true, 0));

        // Scenario 4: Quality gate passed, no enforce flag, violations present
        assert!(!should_exit_with_error(true, false, 10));

        // Scenario 5: Quality gate failed, enforce flag, violations present
        assert!(should_exit_with_error(false, true, 5));
    }
}


mod property_tests {
    use proptest::prelude::*;

    proptest! {
        #[test]
        fn basic_property_stability(_input in ".*") {
            // Basic property test for coverage
            prop_assert!(true);
        }

        #[test]
        fn module_consistency_check(_x in 0u32..1000) {
            // Module consistency verification
            prop_assert!(_x < 1001);
        }
    }
}