#!/usr/bin/env bash

# A tool whose latest version cannot be looked up must not be reported as up
# to date (discussion #15170).
plugin_dir="$MISE_DATA_DIR/plugins/unreachable"
mkdir -p "$plugin_dir/bin"
cat >"$plugin_dir/bin/list-all" <<'LISTALL'
#!/usr/bin/env bash
if [[ -f "$HOME/unreachable" ]]; then
  echo "registry unreachable" >&2
  exit 1
fi
echo "1.0.0 1.1.0"
LISTALL
cat >"$plugin_dir/bin/install" <<'INSTALL'
#!/usr/bin/env bash
mkdir -p "$ASDF_INSTALL_PATH/bin"
INSTALL
chmod +x "$plugin_dir/bin/list-all" "$plugin_dir/bin/install"

mise install unreachable@1.0.0
cat >mise.toml <<'TOML'
[tools]
unreachable = "1"
TOML
touch "$HOME/unreachable"
mise cache clear

assert_contains "mise upgrade --dry-run 2>&1" "Could not check 1 tool for updates: asdf:unreachable@1.0.0"
assert_not_contains "mise upgrade --dry-run 2>&1" "All tools are up to date"
assert_contains "mise outdated 2>&1" "Could not check 1 tool for updates: asdf:unreachable@1.0.0"
assert_not_contains "mise outdated 2>&1" "All tools are up to date"
assert_contains "mise outdated --json 2>/dev/null" "{}"

# The configured range checks out, but the backend-wide lookup behind the
# out-of-range note fails, so the tool may still be out of date.
plugin_dir="$MISE_DATA_DIR/plugins/nostable"
mkdir -p "$plugin_dir/bin"
cat >"$plugin_dir/bin/list-all" <<'LISTALL'
#!/usr/bin/env bash
echo "1.0.0"
LISTALL
cat >"$plugin_dir/bin/latest-stable" <<'LATEST'
#!/usr/bin/env bash
echo "registry unreachable" >&2
exit 1
LATEST
cat >"$plugin_dir/bin/install" <<'INSTALL'
#!/usr/bin/env bash
mkdir -p "$ASDF_INSTALL_PATH/bin"
INSTALL
chmod +x "$plugin_dir/bin/list-all" "$plugin_dir/bin/latest-stable" "$plugin_dir/bin/install"

mise install nostable@1.0.0
cat >mise.toml <<'TOML'
[tools]
nostable = "1"
TOML

assert_contains "mise upgrade --dry-run 2>&1" "Could not check 1 tool for updates: asdf:nostable@1.0.0"
assert_not_contains "mise upgrade --dry-run 2>&1" "All tools are up to date"
assert_contains "mise outdated 2>&1" "Could not check 1 tool for updates: asdf:nostable@1.0.0"
assert_not_contains "mise outdated 2>&1" "All tools are up to date"
