#!/usr/bin/env julia

"""
Test script for AI News Digest functionality
"""

push!(LOAD_PATH, joinpath(@__DIR__, "src"))

using Test
include("src/config.jl")
include("src/ai_filter.jl")
include("src/rss_parser.jl")
include("src/digest_compiler.jl")
include("src/output_formatter.jl")

function test_config_loading()
    @testset "Config Loading" begin
        config = load_config("config.json")
        
        @test haskey(config, "feeds")
        @test haskey(config, "ai_filter")
        @test haskey(config["feeds"], "tech")
        @test haskey(config["feeds"], "ai")
        
        # Test feed filtering
        tech_feeds = get_feeds_by_category(config, "tech")
        ai_feeds = get_feeds_by_category(config, "ai")
        all_feeds = get_feeds_by_category(config, "all")
        
        @test length(tech_feeds) > 0
        @test length(ai_feeds) > 0
        @test length(all_feeds) == length(tech_feeds) + length(ai_feeds)
    end
end

function test_ai_filtering()
    @testset "AI Filtering" begin
        # Create test stories
        ai_story = Story(
            "OpenAI Releases GPT-5 with Advanced Reasoning",
            "https://example.com/gpt5",
            "OpenAI has announced GPT-5, featuring enhanced reasoning capabilities and multimodal input processing.",
            now(),
            "TechCrunch",
            ""
        )
        
        non_ai_story = Story(
            "New Restaurant Opens Downtown",
            "https://example.com/restaurant", 
            "A new Italian restaurant has opened in the downtown area, featuring traditional recipes.",
            now(),
            "Local News",
            ""
        )
        
        # Load filter config
        config = load_config("config.json")
        filter_config = config["ai_filter"]
        
        # Test AI scoring
        ai_score, ai_keywords, ai_companies = calculate_ai_score(ai_story, filter_config)
        non_ai_score, _, _ = calculate_ai_score(non_ai_story, filter_config)
        
        @test ai_score > non_ai_score
        @test ai_score >= filter_config["threshold"]
        @test length(ai_keywords) > 0
        @test length(ai_companies) > 0
        
        println("✅ AI story score: $ai_score (keywords: $ai_keywords)")
        println("✅ Non-AI story score: $non_ai_score")
    end
end

function test_text_similarity()
    @testset "Text Similarity" begin
        # Test identical texts
        @test text_similarity("hello world", "hello world") == 1.0
        
        # Test completely different texts
        @test text_similarity("hello world", "goodbye universe") < 0.5
        
        # Test similar texts
        sim = text_similarity("OpenAI releases GPT-5", "OpenAI announces GPT-5 release")
        @test sim > 0.5
        
        println("✅ Similarity test passed")
    end
end

function test_output_formatting()
    @testset "Output Formatting" begin
        # Create sample digest story
        sample_story = DigestStory(
            "AI Breakthrough in Healthcare",
            "https://example.com/ai-health",
            "Researchers have developed an AI system that can diagnose diseases faster than human doctors.",
            now() - Hour(2),
            "MIT Technology Review",
            85.0,
            1,
            ["artificial intelligence", "healthcare", "diagnosis"],
            ["MIT", "Google AI"]
        )
        
        stories = [sample_story]
        config = load_config("config.json")
        
        # Test JSON formatting
        json_output = format_output(stories, "json", config)
        @test contains(json_output, "AI Breakthrough")
        @test contains(json_output, "digest_date")
        
        # Test Markdown formatting
        md_output = format_output(stories, "markdown", config)
        @test contains(md_output, "# AI News Digest")
        @test contains(md_output, "## 1. AI Breakthrough")
        
        # Test HTML formatting
        html_output = format_output(stories, "email", config)
        @test contains(html_output, "<html>")
        @test contains(html_output, "AI Breakthrough")
        
        println("✅ Output formatting tests passed")
    end
end

function run_all_tests()
    println("🧪 Running AI News Digest Tests...")
    println()
    
    test_config_loading()
    test_ai_filtering() 
    test_text_similarity()
    test_output_formatting()
    
    println()
    println("✅ All tests completed successfully!")
end

# Run tests if this script is executed directly
if abspath(PROGRAM_FILE) == @__FILE__
    run_all_tests()
end