#!/usr/bin/env nu
# Tests for Version Resolution Module

use ../versions.nu *

# Test semver validation
export def test_is_semver [] {
    print "Testing semver validation..."

    assert ("1.0.0" | is-semver) "1.0.0 should be valid semver"
    assert ("v1.2.3" | is-semver) "v1.2.3 should be valid semver"
    assert ("1.0.0-alpha" | is-semver) "1.0.0-alpha should be valid semver"
    assert ("1.0.0+build123" | is-semver) "1.0.0+build123 should be valid semver"
    assert (not ("1.0" | is-semver)) "1.0 should not be valid semver"
    assert (not ("abc" | is-semver)) "abc should not be valid semver"

    print "✅ Semver validation test passed"
}

# Test semver comparison
export def test_compare_semver [] {
    print "Testing semver comparison..."

    assert ((compare-semver "1.0.0" "2.0.0") == -1) "1.0.0 < 2.0.0"
    assert ((compare-semver "2.0.0" "1.0.0") == 1) "2.0.0 > 1.0.0"
    assert ((compare-semver "1.2.3" "1.2.3") == 0) "1.2.3 == 1.2.3"
    assert ((compare-semver "1.10.0" "1.9.0") == 1) "1.10.0 > 1.9.0"
    assert ((compare-semver "1.0.0" "1.0.0-alpha") == 1) "Release > pre-release"

    print "✅ Semver comparison test passed"
}

# Test semver sorting
export def test_sort_semver [] {
    print "Testing semver sorting..."

    let versions = ["2.0.0", "1.0.0", "1.10.0", "1.2.0", "v1.5.0"]
    let sorted = ($versions | sort-by-semver)

    assert (($sorted | get 0) == "1.0.0") "First should be 1.0.0"
    assert (($sorted | last) == "2.0.0") "Last should be 2.0.0"

    print $"   Sorted: ($sorted | str join ', ')"
    print "✅ Semver sorting test passed"
}

# Test latest version
export def test_latest_version [] {
    print "Testing latest version..."

    let versions = ["1.0.0", "2.0.0", "1.5.0", "not-semver"]
    let latest = (get-latest-version $versions)

    assert ($latest == "2.0.0") "Latest should be 2.0.0"

    print $"   Latest: ($latest)"
    print "✅ Latest version test passed"
}

# Test constraint satisfaction
export def test_satisfies_constraint [] {
    print "Testing constraint satisfaction..."

    # Exact version
    assert (satisfies-constraint "1.2.3" "1.2.3") "Exact match should satisfy"
    assert (not (satisfies-constraint "1.2.4" "1.2.3")) "Different version shouldn't satisfy"

    # Wildcard
    assert (satisfies-constraint "1.2.3" "*") "Any version should satisfy *"
    assert (satisfies-constraint "1.2.3" "latest") "Any version should satisfy latest"

    # Caret (^)
    assert (satisfies-constraint "1.5.0" "^1.2.0") "1.5.0 should satisfy ^1.2.0"
    assert (satisfies-constraint "1.2.0" "^1.2.0") "1.2.0 should satisfy ^1.2.0"
    assert (not (satisfies-constraint "2.0.0" "^1.2.0")) "2.0.0 shouldn't satisfy ^1.2.0"

    # Tilde (~)
    assert (satisfies-constraint "1.2.5" "~1.2.0") "1.2.5 should satisfy ~1.2.0"
    assert (not (satisfies-constraint "1.3.0" "~1.2.0")) "1.3.0 shouldn't satisfy ~1.2.0"

    # Range
    assert (satisfies-constraint "1.5.0" "1.2.0-1.8.0") "1.5.0 should be in range"
    assert (not (satisfies-constraint "2.0.0" "1.2.0-1.8.0")) "2.0.0 shouldn't be in range"

    print "✅ Constraint satisfaction test passed"
}

# Run all version tests
export def main [] {
    print "🧪 Running Version Resolution Tests"
    print ""

    test_is_semver
    test_compare_semver
    test_sort_semver
    test_latest_version
    test_satisfies_constraint

    print ""
    print "✅ All version tests passed"
}
