#!/usr/bin/env bash
export MISE_EXPERIMENTAL=1

detect_platform

assert_fail "MISE_DISABLE_BACKENDS=ubi mise install ubi:example/example@1.0.0" "backend ubi is disabled by disable_backends"
assert_fail "MISE_DISABLE_BACKENDS=ubi mise use -g ubi:example/example@1.0.0" "backend ubi is disabled by disable_backends"
assert_fail "MISE_DISABLE_BACKENDS=asdf mise install asdf:dummy@1.0.0" "backend asdf is disabled by disable_backends"

assert "mise registry age" "aqua:FiloSottile/age asdf:threkk/asdf-age"

mise install age
assert_fail "ls $MISE_DATA_DIR/plugins/age"
mise uninstall age

MISE_DISABLE_BACKENDS=aqua mise install age@1.2.1
ls "$MISE_DATA_DIR/plugins/age"
assert "MISE_DISABLE_BACKENDS=asdf mise tool age --backend" "aqua:FiloSottile/age"
eval "$(mise activate bash)" && _mise_hook
assert_contains "MISE_DISABLE_BACKENDS=asdf mise doctor 2>&1 || true" "age"
assert_contains "MISE_DISABLE_BACKENDS=asdf mise doctor -J 2>&1 || true" '"age"'

# a disabled plugin never overrides an enabled registry backend, even when the requested
# version was installed through that plugin
cat <<EOF >mise.toml
[tools]
age = "1.2.1"
EOF
assert "MISE_DISABLE_BACKENDS=asdf mise tool age --backend" "aqua:FiloSottile/age"
MISE_DISABLE_BACKENDS=asdf mise install --dry-run
MISE_DISABLE_BACKENDS=asdf mise lock --platform "$MISE_PLATFORM"
assert_contains "cat mise.lock" 'backend = "aqua:FiloSottile/age"'

# the backends section marks disabled backends instead of listing them as if enabled
assert_contains "MISE_DISABLE_BACKENDS=asdf,vfox mise doctor 2>&1 || true" "asdf (disabled)"
assert_contains "MISE_DISABLE_BACKENDS=asdf,vfox mise doctor 2>&1 || true" "vfox (disabled)"
assert_not_contains "mise doctor 2>&1 || true" "(disabled)"

# a leftover plugin must not route a registry shorthand to a disabled backend: with nothing
# installed through it, resolution falls back to the registry instead of failing the install
# https://github.com/jdx/mise/discussions/6021
mise uninstall --all age
ls "$MISE_DATA_DIR/plugins/age"
assert "MISE_DISABLE_BACKENDS=asdf mise tool age --backend" "aqua:FiloSottile/age"

# and the install that follows uses the registry backend rather than failing
MISE_DISABLE_BACKENDS=asdf mise install age
assert_contains "mise ls age" "age  "
