#!/usr/bin/env bash
# When a remote version listing fails, say so. Previously the failure degraded
# into an empty version list and the generic "no versions found" path blamed
# whatever filter happened to be configured -- e.g. "no versions found for X
# matching date filter" -- even though no filter ever ran. The same fetch was
# also retried, and warned about, once per call site that resolved the tool.

# Point every host the aqua backend lists versions from at a closed port so the
# listing fails for a reason that has nothing to do with a date filter.
export MISE_URL_REPLACEMENTS='{"https://api.github.com":"http://127.0.0.1:1","https://github.com":"http://127.0.0.1:1","https://mise-versions.jdx.dev":"http://127.0.0.1:1"}'
export MISE_USE_VERSIONS_HOST=0
export MISE_HTTP_RETRIES=0
# A date cutoff is what made the old message misleading, so keep one active.
export MISE_MINIMUM_RELEASE_AGE=7d
export RUST_BACKTRACE=0

status=0
out="$(mise install "aqua:suzuki-shunsuke/tfcmt@latest" 2>&1)" || status=$?

if [[ $status -eq 0 ]]; then
  fail "expected the install to fail, but it exited 0"
fi
ok "install failed with status $status"

assert_contains_text "$out" "unable to fetch versions for aqua:suzuki-shunsuke/tfcmt"
assert_not_contains_text "$out" "matching date filter"
# The specific cause is warned instead of the generic "no versions" message.
assert_not_contains_text "$out" "No versions found for"

# Exactly one: zero would mean the warning was dropped and the regression this
# test guards (one warning per call site that resolved the tool) went unnoticed.
warns="$(grep -c "Remote versions cannot be fetched" <<<"$out" || true)"
if [[ $warns -ne 1 ]]; then
  fail "expected exactly 1 fetch-failure warning, got $warns"
fi
ok "fetch failure warned exactly once"

# Without the listing, `4` cannot be told apart from an exact version, so it is
# still tried as one. The install error has to say the list was never checked,
# and lock must not record `version = "4"` as though it were a known release.
status=0
out="$(mise install "aqua:suzuki-shunsuke/tfcmt@4" 2>&1)" || status=$?
if [[ $status -eq 0 ]]; then
  fail "expected the prefix install to fail, but it exited 0"
fi
assert_contains_text "$out" "was not checked against its version list, which could not be fetched"

cat <<'TOML' >mise.toml
[tools]
"aqua:suzuki-shunsuke/tfcmt" = "4"
TOML
assert_fail_contains "mise lock --platform linux-x64" "unable to fetch versions for aqua:suzuki-shunsuke/tfcmt"
if [[ -f mise.lock ]]; then
  assert_not_contains "cat mise.lock" 'version = "4"'
fi

# The same holds for a tool@version argument overriding an exact pin. Without a
# release-age cutoff lock records the argument without re-resolving it, so the
# configured request stays 4.14.0 while the version becomes 4.
cat <<'TOML' >mise.toml
[tools]
"aqua:suzuki-shunsuke/tfcmt" = "4.14.0"
TOML
assert_fail_contains "MISE_MINIMUM_RELEASE_AGE=0 mise lock aqua:suzuki-shunsuke/tfcmt@4 --platform linux-x64" "cannot lock"
if [[ -f mise.lock ]]; then
  assert_not_contains "cat mise.lock" 'version = "4"'
fi
