#!/usr/bin/env bash

# Regression test for https://github.com/jdx/mise/discussions/8880
# Current config options must override stale install-manifest options for HTTP
# version listings.

VERSION_LIST_DIR="$TMPDIR/http-version-list-opts"
mkdir -p "$VERSION_LIST_DIR"
cat <<'JSON' >"$VERSION_LIST_DIR/versions.json"
{"old":["0.1.0"],"current":["1.0.0","2.0.0"]}
JSON

HTTP_PORT_FILE="$TMPDIR/http_version_list_port"
python3 -c "
import http.server, socketserver
s = socketserver.TCPServer(('', 0), lambda *a: http.server.SimpleHTTPRequestHandler(*a, directory='$VERSION_LIST_DIR'))
with open('$HTTP_PORT_FILE', 'w') as f:
    f.write(str(s.server_address[1]))
s.serve_forever()
" &
HTTP_SERVER_PID=$!

wait_for_file "$HTTP_PORT_FILE" "HTTP test server port file" 30 "$HTTP_SERVER_PID"
HTTP_PORT=$(cat "$HTTP_PORT_FILE")

cleanup() {
  kill $HTTP_SERVER_PID 2>/dev/null || true
}
trap cleanup EXIT

if ! curl --retry 5 --retry-connrefused --retry-delay 1 -fsS "http://localhost:$HTTP_PORT/versions.json" >/dev/null; then
  fail "HTTP server failed to respond"
fi

mkdir -p "$MISE_DATA_DIR/installs"
cat <<EOF >"$MISE_DATA_DIR/installs/.mise-installs.toml"
[http-tool-opts]
short = "http:tool-opts"
full = "http:tool-opts"
explicit_backend = true

[http-tool-opts.opts]
version_list_url = "http://localhost:$HTTP_PORT/versions.json"
version_json_path = ".old"
EOF

cat <<EOF >mise.toml
[tools."http:tool-opts"]
version = "1.0.0"
version_list_url = "http://localhost:$HTTP_PORT/versions.json"
version_json_path = ".current"
EOF

assert "mise ls-remote http:tool-opts" "1.0.0
2.0.0"
