#[cfg_attr(coverage_nightly, coverage(off))]
#[cfg(test)]
mod tests {
    use super::*;

    // ============ UnifiedAstEngine Tests ============

    #[test]
    fn test_unified_ast_engine_new() {
        let engine = UnifiedAstEngine::new();
        // Verify construction completes without panic
        let _ = std::mem::size_of_val(&engine);
    }

    #[test]
    fn test_unified_ast_engine_default() {
        let engine = UnifiedAstEngine;
        // Verify construction completes without panic
        let _ = std::mem::size_of_val(&engine);
    }

    #[tokio::test]
    async fn test_analyze_project() {
        let engine = UnifiedAstEngine::new();
        let result = engine.analyze_project(Path::new("/tmp")).await;
        assert!(result.is_ok());
    }

    #[tokio::test]
    async fn test_parse_project() {
        let engine = UnifiedAstEngine::new();
        let result = engine.parse_project(Path::new("/tmp")).await;
        assert!(result.is_ok());
    }

    #[test]
    fn test_compute_metrics() {
        let engine = UnifiedAstEngine::new();
        let forest = AstForest::default();
        let result = engine.compute_metrics(&forest);
        assert!(result.is_ok());
    }

    #[test]
    fn test_extract_dependencies() {
        let engine = UnifiedAstEngine::new();
        let forest = AstForest::default();
        let result = engine.extract_dependencies(&forest);
        assert!(result.is_ok());
    }

    #[tokio::test]
    async fn test_generate_artifacts() {
        let engine = UnifiedAstEngine::new();
        let result = engine.generate_artifacts(Path::new("/tmp")).await;
        assert!(result.is_ok());
    }

    // ============ AstForest Tests ============

    #[test]
    fn test_ast_forest_default() {
        let forest = AstForest::default();
        assert!(forest.modules.is_empty());
    }

    #[test]
    fn test_ast_forest_files_empty() {
        let forest = AstForest::default();
        assert_eq!(forest.files().count(), 0);
    }

    #[test]
    fn test_ast_forest_files_with_modules() {
        let mut forest = AstForest::default();
        forest.modules.push(ModuleNode {
            path: PathBuf::from("/test/a.rs"),
            name: "a".to_string(),
            visibility: "public".to_string(),
            metrics: ModuleMetrics::default(),
        });
        forest.modules.push(ModuleNode {
            path: PathBuf::from("/test/b.rs"),
            name: "b".to_string(),
            visibility: "private".to_string(),
            metrics: ModuleMetrics::default(),
        });
        assert_eq!(forest.files().count(), 2);
    }

    #[test]
    fn test_ast_forest_clone() {
        let forest = AstForest::default();
        let cloned = forest.clone();
        assert!(cloned.modules.is_empty());
    }

    #[test]
    fn test_ast_forest_debug() {
        let forest = AstForest::default();
        let debug = format!("{:?}", forest);
        assert!(debug.contains("AstForest"));
    }

    // ============ ModuleNode Tests ============

    #[test]
    fn test_module_node_default() {
        let node = ModuleNode::default();
        assert!(node.name.is_empty());
    }

    #[test]
    fn test_module_node_creation() {
        let node = ModuleNode {
            path: PathBuf::from("/src/lib.rs"),
            name: "lib".to_string(),
            visibility: "public".to_string(),
            metrics: ModuleMetrics {
                complexity: 10,
                lines: 100,
                functions: 5,
                classes: 2,
            },
        };
        assert_eq!(node.name, "lib");
        assert_eq!(node.metrics.complexity, 10);
    }

    #[test]
    fn test_module_node_clone() {
        let node = ModuleNode {
            path: PathBuf::from("/test.rs"),
            name: "test".to_string(),
            visibility: "private".to_string(),
            metrics: ModuleMetrics::default(),
        };
        let cloned = node.clone();
        assert_eq!(cloned.name, "test");
    }

    #[test]
    fn test_module_node_debug() {
        let node = ModuleNode::default();
        let debug = format!("{:?}", node);
        assert!(debug.contains("ModuleNode"));
    }

    // ============ ModuleMetrics Tests ============

    #[test]
    fn test_module_metrics_default() {
        let metrics = ModuleMetrics::default();
        assert_eq!(metrics.complexity, 0);
        assert_eq!(metrics.lines, 0);
        assert_eq!(metrics.functions, 0);
        assert_eq!(metrics.classes, 0);
    }

    #[test]
    fn test_module_metrics_creation() {
        let metrics = ModuleMetrics {
            complexity: 25,
            lines: 500,
            functions: 20,
            classes: 3,
        };
        assert_eq!(metrics.complexity, 25);
        assert_eq!(metrics.lines, 500);
    }

    #[test]
    fn test_module_metrics_clone() {
        let metrics = ModuleMetrics {
            complexity: 5,
            lines: 50,
            functions: 3,
            classes: 1,
        };
        let cloned = metrics.clone();
        assert_eq!(cloned.complexity, metrics.complexity);
    }

    #[test]
    fn test_module_metrics_debug() {
        let metrics = ModuleMetrics::default();
        let debug = format!("{:?}", metrics);
        assert!(debug.contains("ModuleMetrics"));
    }

    // ============ ProjectMetrics Tests ============

    #[test]
    fn test_project_metrics_default() {
        let metrics = ProjectMetrics::default();
        assert_eq!(metrics.total_complexity, 0);
        assert_eq!(metrics.total_lines, 0);
    }

    #[test]
    fn test_project_metrics_creation() {
        let metrics = ProjectMetrics {
            total_complexity: 100,
            total_lines: 5000,
            file_count: 50,
            function_count: 200,
            avg_complexity: 2.0,
            max_complexity: 30,
        };
        assert_eq!(metrics.file_count, 50);
        assert_eq!(metrics.function_count, 200);
    }

    #[test]
    fn test_project_metrics_clone() {
        let metrics = ProjectMetrics {
            total_complexity: 50,
            total_lines: 1000,
            file_count: 10,
            function_count: 30,
            avg_complexity: 5.0,
            max_complexity: 15,
        };
        let cloned = metrics.clone();
        assert_eq!(cloned.total_complexity, metrics.total_complexity);
    }

    #[test]
    fn test_project_metrics_debug() {
        let metrics = ProjectMetrics::default();
        let debug = format!("{:?}", metrics);
        assert!(debug.contains("ProjectMetrics"));
    }

    // ============ ArtifactTree Tests ============

    #[test]
    fn test_artifact_tree_default() {
        let tree = ArtifactTree::default();
        assert!(tree.dogfooding.is_empty());
        assert!(tree.templates.is_empty());
    }

    #[test]
    fn test_artifact_tree_with_items() {
        let mut tree = ArtifactTree::default();
        tree.dogfooding
            .insert("key1".to_string(), "value1".to_string());
        tree.templates.push(Template::default());
        assert_eq!(tree.dogfooding.len(), 1);
        assert_eq!(tree.templates.len(), 1);
    }

    #[test]
    fn test_artifact_tree_clone() {
        let tree = ArtifactTree::default();
        let cloned = tree.clone();
        assert!(cloned.dogfooding.is_empty());
    }

    #[test]
    fn test_artifact_tree_debug() {
        let tree = ArtifactTree::default();
        let debug = format!("{:?}", tree);
        assert!(debug.contains("ArtifactTree"));
    }

    // ============ MermaidArtifacts Tests ============

    #[test]
    fn test_mermaid_artifacts_default() {
        let artifacts = MermaidArtifacts::default();
        assert!(artifacts.ast_generated.is_empty());
        assert!(artifacts.non_code.is_empty());
    }

    #[test]
    fn test_mermaid_artifacts_with_items() {
        let mut artifacts = MermaidArtifacts::default();
        artifacts
            .ast_generated
            .insert("module.rs".to_string(), "graph TD".to_string());
        artifacts
            .non_code
            .insert("readme.md".to_string(), "flowchart".to_string());
        assert_eq!(artifacts.ast_generated.len(), 1);
        assert_eq!(artifacts.non_code.len(), 1);
    }

    #[test]
    fn test_mermaid_artifacts_clone() {
        let artifacts = MermaidArtifacts::default();
        let cloned = artifacts.clone();
        assert!(cloned.ast_generated.is_empty());
    }

    #[test]
    fn test_mermaid_artifacts_debug() {
        let artifacts = MermaidArtifacts::default();
        let debug = format!("{:?}", artifacts);
        assert!(debug.contains("MermaidArtifacts"));
    }

    // ============ Template Tests ============

    #[test]
    fn test_template_default() {
        let template = Template::default();
        assert!(template.name.is_empty());
        assert!(template.content.is_empty());
    }

    #[test]
    fn test_template_creation() {
        let template = Template {
            name: "my_template".to_string(),
            content: "template content".to_string(),
            hash: blake3::hash(b"template content"),
            source_location: PathBuf::from("/templates/my_template.hbs"),
        };
        assert_eq!(template.name, "my_template");
        assert!(!template.hash.to_string().is_empty());
    }

    #[test]
    fn test_template_clone() {
        let template = Template {
            name: "test".to_string(),
            content: "test content".to_string(),
            hash: blake3::hash(b"test"),
            source_location: PathBuf::from("/test"),
        };
        let cloned = template.clone();
        assert_eq!(cloned.name, template.name);
    }

    #[test]
    fn test_template_debug() {
        let template = Template::default();
        let debug = format!("{:?}", template);
        assert!(debug.contains("Template"));
    }

    // ============ FileAst Tests ============

    #[test]
    fn test_file_ast_typescript() {
        let ast = FileAst::TypeScript("const x = 1;".to_string());
        if let FileAst::TypeScript(code) = ast {
            assert!(code.contains("const"));
        } else {
            panic!("Expected TypeScript variant");
        }
    }

    #[test]
    fn test_file_ast_python() {
        let ast = FileAst::Python("def foo(): pass".to_string());
        if let FileAst::Python(code) = ast {
            assert!(code.contains("def"));
        } else {
            panic!("Expected Python variant");
        }
    }

    #[test]
    fn test_file_ast_c() {
        let ast = FileAst::C("int main() {}".to_string());
        if let FileAst::C(code) = ast {
            assert!(code.contains("int"));
        } else {
            panic!("Expected C variant");
        }
    }

    #[test]
    fn test_file_ast_cpp() {
        let ast = FileAst::Cpp("#include <iostream>".to_string());
        if let FileAst::Cpp(code) = ast {
            assert!(code.contains("include"));
        } else {
            panic!("Expected Cpp variant");
        }
    }

    #[test]
    fn test_file_ast_cython() {
        let ast = FileAst::Cython("cdef int x".to_string());
        if let FileAst::Cython(code) = ast {
            assert!(code.contains("cdef"));
        } else {
            panic!("Expected Cython variant");
        }
    }

    #[test]
    fn test_file_ast_kotlin() {
        let ast = FileAst::Kotlin("fun main() {}".to_string());
        if let FileAst::Kotlin(code) = ast {
            assert!(code.contains("fun"));
        } else {
            panic!("Expected Kotlin variant");
        }
    }

    #[test]
    fn test_file_ast_makefile() {
        let ast = FileAst::Makefile("all:\n\techo hello".to_string());
        if let FileAst::Makefile(code) = ast {
            assert!(code.contains("all"));
        } else {
            panic!("Expected Makefile variant");
        }
    }

    #[test]
    fn test_file_ast_markdown() {
        let ast = FileAst::Markdown("# Title".to_string());
        if let FileAst::Markdown(code) = ast {
            assert!(code.contains("#"));
        } else {
            panic!("Expected Markdown variant");
        }
    }

    #[test]
    fn test_file_ast_toml() {
        let ast = FileAst::Toml("[package]".to_string());
        if let FileAst::Toml(code) = ast {
            assert!(code.contains("package"));
        } else {
            panic!("Expected Toml variant");
        }
    }

    #[test]
    fn test_file_ast_yaml() {
        let ast = FileAst::Yaml("key: value".to_string());
        if let FileAst::Yaml(code) = ast {
            assert!(code.contains("key"));
        } else {
            panic!("Expected Yaml variant");
        }
    }

    #[test]
    fn test_file_ast_json() {
        let ast = FileAst::Json(r#"{"key": "value"}"#.to_string());
        if let FileAst::Json(code) = ast {
            assert!(code.contains("key"));
        } else {
            panic!("Expected Json variant");
        }
    }

    #[test]
    fn test_file_ast_shell() {
        let ast = FileAst::Shell("#!/bin/bash\necho hello".to_string());
        if let FileAst::Shell(code) = ast {
            assert!(code.contains("bash"));
        } else {
            panic!("Expected Shell variant");
        }
    }
}

#[cfg_attr(coverage_nightly, coverage(off))]
#[cfg(test)]
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);
        }
    }
}
