#!/usr/bin/env bash

# Test that `mise lock <tool>` prunes stale version entries when the resolved
# version changes. This is a regression test for the bug where filtered lock
# runs appended a new [[tools.X]] section instead of replacing the old one.

export MISE_LOCKFILE=1

rm -f mise.toml mise.lock

echo "=== Setup: lock dummy at 1.0.0 ==="
cat <<'EOF' >mise.toml
[tools]
dummy = "1.0.0"
EOF

mise install dummy@1.0.0
mise lock --platform linux-x64
assert_contains "cat mise.lock" 'version = "1.0.0"'
assert_not_contains "cat mise.lock" 'version = "1.1.0"'

echo "=== Change config to 1.1.0 and re-lock with filter ==="
cat <<'EOF' >mise.toml
[tools]
dummy = "1.1.0"
EOF

mise install dummy@1.1.0

# Filtered lock run targeting only dummy
mise lock dummy --platform linux-x64

# Should have the new version
assert_contains "cat mise.lock" 'version = "1.1.0"'
# Should NOT have the old version (this was the bug: both would be present)
assert_not_contains "cat mise.lock" 'version = "1.0.0"'

echo "=== Cleanup ==="
rm -f mise.toml mise.lock
