#!/usr/bin/env zsh
set -euo pipefail
orig_path="$PATH"

rtx trust
rtx trust ../.e2e.rtx.toml
rtx trust 18/.e2e.rtx.toml

assert_path() {
  local expected="${1//$HOME/\~}"
  local actual="${PATH/%$orig_path/}"
  actual="${actual//$HOME/\~}"
  actual="${actual/%:/}"
  if [[ "$actual" != "$expected" ]]; then
    echo "Invalid PATH:  $actual"
    echo "Expected PATH: $expected"
    exit 1
  fi
}

rtx install node@20.0.0 node@18.0.0
# shellcheck disable=SC1090
eval "$(rtx activate zsh --status)" && _rtx_hook

#rtx install
test "$(node -v)" = "v20.0.0"

cd 18 && _rtx_hook
#rtx install
test "$(node -v)" = "v18.0.0"

cd .. && _rtx_hook
test "$(node -v)" = "v20.0.0"

rtx shell node@18.0.0 && _rtx_hook
test "$(node -v)" = "v18.0.0"

rtx deactivate
assert_path ""
