#!/usr/bin/env bash
require_cmd direnv

# Create a "local" dummy binary
mkdir -p node_modules/.bin
cat <<EOF >node_modules/.bin/dummy
#!/usr/bin/env bash
echo "I am Dummy from node_modules!"
EOF
chmod +x node_modules/.bin/dummy

# Some configuration for direnv
cat <<EOF >.envrc
PATH_add node_modules/.bin
export FIRST='first'
EOF
direnv allow

export FIRST="last"

# Activate direnv and mise
eval "$(mise activate bash --status)"
eval "$(direnv hook bash)"

# Tell mise to use dummy@latest
mise use dummy@latest && _direnv_hook

# Should use dummy@latest
assert "which dummy" "$PWD/node_modules/.bin/dummy"
# shellcheck disable=SC2016
assert 'echo $FIRST' "first"

# Allow direnv to use the .envrc file
_direnv_hook

# Should use dummy from node_modules (direnv PATH takes precedence)
assert "which dummy" "$PWD/node_modules/.bin/dummy"
# shellcheck disable=SC2016
assert 'echo $FIRST' "first"
