#!/usr/bin/env bash
# Test that `mise install --system` creates symlinks in the system installs dir,
# not the user installs dir.
# Regression test for https://github.com/jdx/mise/discussions/8596

# Set up a custom system data dir within the test environment
export MISE_SYSTEM_DATA_DIR="$HOME/.local/share/mise-system"
SYSTEM_INSTALLS="$MISE_SYSTEM_DATA_DIR/installs"
USER_INSTALLS="$MISE_DATA_DIR/installs"

# Install a tool to the user dir first, then a different version to system dir.
# This reproduces the scenario where the backend's installs_path points to the
# user dir but versions also exist in the system dir.
mise install tiny@1.0.0
mise install --system tiny@2.1.0

# A tool installed only in the system dir must satisfy an unqualified `latest`
# request without a remote lookup. Lazy backend loading keeps its user-local
# installs_path, so latest resolution must recover the system path from install
# state.
mise install --system dummy@2.1.0
cat >mise.toml <<'EOF'
[tools]
dummy = "latest"
EOF
assert "MISE_OFFLINE=1 mise current dummy" "2.1.0"

# Both directories should have their respective version installed
assert_directory_exists "$USER_INSTALLS/tiny/1.0.0"
assert_directory_exists "$SYSTEM_INSTALLS/tiny/2.1.0"
assert_directory_exists "$SYSTEM_INSTALLS/dummy/2.1.0"

# The system dir should have a "latest" symlink pointing to its own version
assert "readlink $SYSTEM_INSTALLS/tiny/latest" "./2.1.0"

# The user dir "latest" symlink should point to the user dir version, NOT a
# version that only exists in the system dir (which would be a broken symlink)
assert "readlink $USER_INSTALLS/tiny/latest" "./1.0.0"
