#!/usr/bin/env bash

# Test that MISE_ENV selects the correct locked version from separate lockfiles

export MISE_LOCKFILE=1

# Create base config and env-specific config both requesting "tiny"
cat >mise.toml <<'EOF'
[tools]
tiny = "latest"
EOF

cat >mise.test.toml <<'EOF'
[tools]
tiny = "latest"
EOF

# Install both versions FIRST (before creating lockfile)
# This prevents install from overwriting our manually-crafted lockfile
assert "mise install tiny@1.0.0"
assert "mise install tiny@2.1.0"

# Create separate lockfiles: base and env-specific
cat >mise.lock <<'EOF'
[[tools.tiny]]
version = "1.0.0"
backend = "asdf:tiny"
EOF

cat >mise.test.lock <<'EOF'
[[tools.tiny]]
version = "2.1.0"
backend = "asdf:tiny"
EOF

# Without MISE_ENV, should use base version from mise.lock (1.0.0)
assert "mise where tiny" "$MISE_DATA_DIR/installs/tiny/1.0.0"

# With MISE_ENV=test, should use version from mise.test.lock (2.1.0)
# Note: Must pass MISE_ENV in the command itself since assert uses bash -c
assert "MISE_ENV=test mise where tiny" "$MISE_DATA_DIR/installs/tiny/2.1.0"

# With MISE_ENV=production (no mise.production.lock), should fall back to base (1.0.0)
assert "MISE_ENV=production mise where tiny" "$MISE_DATA_DIR/installs/tiny/1.0.0"
