#!/usr/bin/env bash

# The URL mise prints must be one pitchfork's proxy serves, for a project whose
# namespace is the hashed default. Needs a pitchfork with `config add --label`.
if [[ -z ${MISE_TEST_PITCHFORK:-} ]]; then
  echo "Skipping: set MISE_TEST_PITCHFORK to a pitchfork with 'config add --label'"
  exit 0
fi
if ! "$MISE_TEST_PITCHFORK" config add --help 2>&1 | grep -q -- '--label'; then
  echo "Skipping: $MISE_TEST_PITCHFORK has no 'config add --label'"
  exit 0
fi
cp "$MISE_TEST_PITCHFORK" ~/bin/pitchfork
export PATH="$HOME/bin:$PATH"
# Unix socket paths are short, so keep these directly under the harness-owned
# $TMPDIR (which the harness removes) rather than nesting them deeper. This test
# name is no longer than test_daemons_register_proxy_slow's, which uses the
# same layout.
export PITCHFORK_STATE_DIR="$TMPDIR/pf"
export PITCHFORK_CONFIG_DIR="$TMPDIR/pfc"
export PITCHFORK_INTERVAL=500ms
export PITCHFORK_PROXY_ENABLE=true
export PITCHFORK_PROXY_HTTPS=false
export PITCHFORK_PROXY_DNS=false
export PITCHFORK_PROXY_SYNC_HOSTS=false
export PITCHFORK_PROXY_AUTO_TRUST=false
export PITCHFORK_PROXY_TLD=localhost
PITCHFORK_PROXY_PORT=$(python3 -c 'import socket; s=socket.socket(); s.bind(("127.0.0.1",0)); print(s.getsockname()[1])')
export PITCHFORK_PROXY_PORT
trap 'pitchfork supervisor stop >/dev/null 2>&1 || true' EXIT

PITCHFORK_MISE_BIN="$(command -v mise)" pitchfork supervisor start

# No [daemons_settings] namespace: mise registers the primary checkout under a
# hashed namespace and prints hostnames named after the directory.
mkdir shop
cd shop
git init -q
cat >mise.toml <<'TOML'
[tools]
pitchfork = "system"
[daemons.web]
run = "exec python3 -m http.server $WEB_PORT --bind 127.0.0.1"
port = { auto = true, base = 43100 }
ready_cmd = "curl -fsS http://127.0.0.1:$WEB_PORT/proof"
TOML
git add mise.toml
git -c user.name=test -c user.email=test@example.invalid commit -qm fixture
git worktree add -qb feature ../feature

# The registry keeps the hashed namespace and carries the label beside it.
# Each URL mise prints has to reach its own checkout through the proxy.
expected_host() {
  case "$1" in
    shop) echo "web.shop.localhost" ;;
    feature) echo "web.feature.shop.localhost" ;;
  esac
}
for checkout in "$PWD" "$PWD/../feature"; do
  (
    cd "$checkout"
    name=$(basename "$checkout")
    printf '%s\n' "$name" >proof
    mise daemons start web
    url=$(mise daemons urls --json | jq -r '.[] | select(.name == "web") | .url')
    assert "echo '$url'" "http://$(expected_host "$name"):$PITCHFORK_PROXY_PORT"
    hostport=${url#http://}
    # A checkout registered a moment ago can take a second to be routable.
    status=000
    for _ in $(seq 20); do
      # The harness runs with -e: a refused connection must retry, not abort.
      status=$(curl --noproxy '*' -s -o /dev/null -w '%{http_code}' --max-time 30 --resolve "$hostport:127.0.0.1" "$url/proof") || status=000
      [[ $status == 200 ]] && break
      sleep 1
    done
    assert "echo $status" "200"
    assert_contains "curl --noproxy '*' -fsS --max-time 30 --resolve '$hostport:127.0.0.1' '$url/proof'" "$name"
  )
done
assert_contains "cat '$PITCHFORK_CONFIG_DIR/config.toml'" 'label = "shop"'
