#!/usr/bin/env bash
# Test that MISE_CONFIG_DIR properly overrides the default config location
# When MISE_CONFIG_DIR is set to a custom location, configs from the default
# location (~/.config/mise) should NOT be loaded during directory traversal.
# See: https://github.com/jdx/mise/discussions/7015

# Create a custom config directory
CUSTOM_CONFIG_DIR="$HOME/.mise-custom"
mkdir -p "$CUSTOM_CONFIG_DIR"

# Create config in the custom location
cat >"$CUSTOM_CONFIG_DIR/config.toml" <<'EOF'
[tools]
dummy = "1.0.0"
EOF

# Create config in the DEFAULT location (~/.config/mise) which should be ignored
# when MISE_CONFIG_DIR is overridden
mkdir -p "$HOME/.config/mise"
cat >"$HOME/.config/mise/config.toml" <<'EOF'
[tools]
dummy = "2.0.0"
EOF

# Set MISE_CONFIG_DIR to custom location and verify only custom config is loaded
export MISE_CONFIG_DIR="$CUSTOM_CONFIG_DIR"

# The config list should show ONLY the custom config, not the default location
# Note: mise uses ~ shorthand in output, so check for that
assert_contains "mise config ls" ".mise-custom/config.toml"

# Should NOT contain the default config location
assert_not_contains "mise config ls" ".config/mise/config.toml"

# Verify the correct tool version is used (1.0.0 from custom, not 2.0.0 from default)
mise install
assert_contains "mise x -- dummy" "1.0.0"

# The write target has to respect the same exclusion. `mise use` used to pick
# ~/.config/mise/config.toml here -- the very file the assertions above prove is not
# loaded -- so the tool landed somewhere mise would never read it back.
mkdir -p "$HOME/writetarget"
cd "$HOME/writetarget" || exit 1

mise use dummy@1.0.0
assert "cat mise.toml" '[tools]
dummy = "1.0.0"'
assert "cat $HOME/.config/mise/config.toml" '[tools]
dummy = "2.0.0"'

# `mise set` already resolved this correctly; it shares the resolver now, so pin it.
mise set WRITE_TARGET=local
assert_contains "cat mise.toml" "WRITE_TARGET"
assert_not_contains "cat $HOME/.config/mise/config.toml" "WRITE_TARGET"
