#!/usr/bin/env bash

mise i dummy@1.0.0

assert_contains "mise use dummy" "dummy@1.0.0"
assert "mise current dummy" "1.0.0"

assert_contains "mise use dummy@2" "dummy@2."
assert "mise current dummy" "2.0.0"

assert_not_contains "mise use --rm dummy" "dummy"
assert "mise current dummy" ""

assert_contains "mise use --env local dummy@2" "dummy@2."
assert "cat mise.local.toml" '[tools]
dummy = "2"'
assert "mise current dummy" "2.0.0"
rm mise.local.toml

mise use dummy@1 dummy@2
assert "mise current dummy" "1.0.0 2.0.0"

mise use --pin dummy@1
assert "cat mise.toml" '[tools]
dummy = "1.0.0"'

MISE_PIN=1 mise use --fuzzy dummy@1
assert "cat mise.toml" '[tools]
dummy = "1"'

MISE_PIN=1 mise use dummy@1 --path mise.local.toml
assert "cat mise.local.toml" '[tools]
dummy = "1.0.0"'

mise use --rm dummy --path mise.local.toml
assert "cat mise.local.toml" ""

mise use dummy@1 --path .
assert "cat mise.toml" '[tools]
dummy = "1"'

mise use dummy@1
assert "cat mise.local.toml" '[tools]
dummy = "1"'

rm -f mise.local.toml mise.toml
echo "dummy 1.0.0" >.tool-versions
mise use dummy@2
assert "cat .tool-versions" "dummy 2"

rm -f .tool-versions

mise use -g dummy@1
assert "cat ~/.config/mise/config.toml" '[tools]
dummy = "1"'
rm -f ~/.config/mise/config.toml

mise use -g "ubi:cli/cli[exe=gh]"
assert "cat ~/.config/mise/config.toml" '[tools]
"ubi:cli/cli" = { version = "latest", exe = "gh" }'
rm -f ~/.config/mise/config.toml

mise use -g gh@2
assert "cat ~/.config/mise/config.toml" '[tools]
gh = "2"'
rm -f ~/.config/mise/config.toml

mise uninstall dummy --all
mise use dummy@system
assert "mise ls dummy" "dummy  system  ~/workdir/mise.toml  system"

mkdir -p ~/workdir/mydummy
mise use "dummy@path:~/workdir/mydummy"
assert_contains "mise ls dummy" "dummy  path:~/workdir/mydummy  ~/workdir/mise.toml  path:~/workdir/mydummy"

cd "$HOME" || exit 1
assert_contains "mise use dummy@system" "mise ~/.config/mise/config.toml tools: dummy@system"
