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

# Selective imports (ADR-025 Phase 3 Layer 2).
# extensions/cache star-import was dead (no used symbols in this test).
# utils/logger.nu does not exist — dangling import removed.

# Test cache directory creation
export def test_cache_dir [] {
    print "Testing cache directory..."

    let cache_dir = (get-cache-dir)

    assert ($cache_dir | path exists) "Cache directory should exist"
    print $"   Cache dir: ($cache_dir)"

    print "✅ Cache directory test passed"
}

# Test cache path generation
export def test_cache_path [] {
    print "Testing cache path generation..."

    let path = (get-cache-path "taskserv" "kubernetes" "1.28.0")

    assert ($path | str contains "taskserv") "Path should contain type"
    assert ($path | str contains "kubernetes") "Path should contain name"
    assert ($path | str contains "1.28.0") "Path should contain version"

    print $"   Cache path: ($path)"
    print "✅ Cache path test passed"
}

# Test cache index
export def test_cache_index [] {
    print "Testing cache index..."

    let index = (load-cache-index)

    assert ($index.extensions? | is-not-empty) "Index should have extensions field"
    assert ($index.metadata? | is-not-empty) "Index should have metadata"

    print "✅ Cache index test passed"
}

# Test cache statistics
export def test_cache_stats [] {
    print "Testing cache statistics..."

    let stats = (get-cache-stats)

    assert ($stats.total_extensions? | describe) =~ "int" "Should have total count"
    assert ($stats.cache_dir | is-not-empty) "Should have cache dir"

    print $"   Total extensions: ($stats.total_extensions)"
    print $"   Cache size: ($stats.total_size_bytes) bytes"

    print "✅ Cache stats test passed"
}

# Test listing cached extensions
export def test_list_cached [] {
    print "Testing list cached extensions..."

    let cached = (list-cached)

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

    print "✅ List cached test passed"
}

# Run all cache tests
export def main [] {
    print "🧪 Running Cache Tests"
    print ""

    test_cache_dir
    test_cache_path
    test_cache_index
    test_cache_stats
    test_list_cached

    print ""
    print "✅ All cache tests passed"
}
