#!/usr/bin/env nu
# Tests for Extension Discovery Module

use ../discovery.nu *
use ../../utils/logger.nu *

# Test local extension discovery
export def test_discover_local [] {
    print "Testing local extension discovery..."

    let extensions = (discover-local-extensions)

    print $"   Found ($extensions | length) local extensions"

    if ($extensions | length) > 0 {
        print "   Examples:"
        $extensions | first 3 | each {|ext|
            print $"     • ($ext.name) \(($ext.type))"
        }
    }

    print "✅ Local discovery test passed"
}

# Test OCI extension discovery (if available)
export def test_discover_oci [] {
    print "Testing OCI extension discovery..."

    if not (is-oci-available) {
        print "   ⚠️  OCI registry not available, skipping"
        return
    }

    let extensions = (discover-oci-extensions)

    print $"   Found ($extensions | length) OCI extensions"

    if ($extensions | length) > 0 {
        print "   Examples:"
        $extensions | first 3 | each {|ext|
            print $"     • ($ext.name):($ext.latest) \(($ext.type))"
        }
    }

    print "✅ OCI discovery test passed"
}

# Test extension search
export def test_search_extensions [] {
    print "Testing extension search..."

    let results = (search-extensions "kube" --source "local")

    print $"   Found ($results | length) matches for 'kube'"

    if ($results | length) > 0 {
        print "   Results:"
        $results | each {|ext|
            print $"     • ($ext.name) \(($ext.source))"
        }
    }

    print "✅ Extension search test passed"
}

# Test list extensions
export def test_list_extensions [] {
    print "Testing list extensions..."

    let extensions = (list-extensions --format "table")

    print $"   Total extensions: ($extensions | length)"

    print "✅ List extensions test passed"
}

# Test get extension versions
export def test_get_versions [] {
    print "Testing get extension versions..."

    # Test with a known extension if available
    let all_exts = (discover-local-extensions)

    if ($all_exts | length) > 0 {
        let test_ext = ($all_exts | first)
        let versions = (get-extension-versions $test_ext.name --source "local")

        print $"   Versions for ($test_ext.name): ($versions | length)"
    } else {
        print "   ⚠️  No local extensions found, skipping version test"
    }

    print "✅ Get versions test passed"
}

# Run all discovery tests
export def main [] {
    print "🧪 Running Discovery Tests"
    print ""

    test_discover_local
    test_discover_oci
    test_search_extensions
    test_list_extensions
    test_get_versions

    print ""
    print "✅ All discovery tests passed"
}
