#!/usr/bin/env bash

# Test that `mise x tool@latest` installs the actual latest version from registry
# even if an older version is already installed

# Install an older version
mise install dummy@1.0.0

# Verify only 1.0.0 is installed
assert_contains "mise ls --installed dummy" "1.0.0"
assert_not_contains "mise ls --installed dummy" "2.0.0"

# Running `mise x dummy@latest` should install and use the actual latest (2.0.0)
# not just use the latest installed (1.0.0)
assert_contains "mise x dummy@latest -- dummy" "2.0.0"

# Verify 2.0.0 is now installed
assert_contains "mise ls --installed dummy" "2.0.0"

# Contrast with `mise x dummy` (no explicit @latest) which should use config or latest installed
# First, ensure no config file requests dummy
rm -f mise.toml .tool-versions

# `mise x dummy` without explicit version should still work with what's installed
# (this tests that we didn't break the default behavior)
# Should output 2.0.0 (latest installed after the above test)
assert_contains "mise x dummy -- dummy" "2.0.0"

# Test that consecutive `mise x tool@latest` calls use the cache
# The second call should not trigger a version list fetch (no bin/list-all or latest-stable call)
# Run with debug to capture backend calls
MISE_DEBUG=1 mise x dummy@latest -- dummy 2>&1 | tee /tmp/exec_latest_output.txt
# Verify it ran successfully with 2.0.0
grep -q "2.0.0" /tmp/exec_latest_output.txt
# Should NOT call list-all or latest-stable again since versions are cached
if grep -q "bin/list-all" /tmp/exec_latest_output.txt; then
	echo "ERROR: Expected cache hit but got bin/list-all call"
	exit 1
fi
if grep -q "latest-stable" /tmp/exec_latest_output.txt; then
	echo "ERROR: Expected cache hit but got latest-stable call"
	exit 1
fi

# Now clear cache and verify that latest-stable IS called (to re-resolve @latest)
mise cache clear dummy
MISE_DEBUG=1 mise x dummy@latest -- dummy 2>&1 | tee /tmp/exec_latest_output2.txt
grep -q "2.0.0" /tmp/exec_latest_output2.txt
# After cache clear, latest-stable SHOULD be called to resolve @latest
if ! grep -q "latest-stable" /tmp/exec_latest_output2.txt; then
	echo "ERROR: Expected cache miss but latest-stable was not called"
	exit 1
fi

# Test that @latest only affects the tool it's specified for, not other tools
# When running `mise x dummy@1.0.0 dummy@latest`, the 1.0.0 should use installed version
# First clear cache to ensure a clean test
mise cache clear dummy
# Run with both a specific version and @latest - should NOT fetch for 1.0.0
# (We can't easily test this with dummy since it's the same tool, but we verify
# the resolve logic works correctly by ensuring both versions work together)
assert_contains "mise x dummy@1.0.0 -- dummy" "1.0.0"
