#!/usr/bin/env bash

export MISE_PYTHON_GITHUB_ATTESTATIONS=0
# Pinned: uv 0.11.9 was manually published and lacks SLSA attestations
# (astral-sh/uv#19278). Bump once 0.11.10 ships with attestations restored.
assert "mise use -g uv@0.11.8 python@3.11.3"
assert "mise x -- uv python install 3.11.1"
mkdir -p "$MISE_CACHE_DIR/python/3.11.1"
touch "$MISE_CACHE_DIR/python/3.11.1/incomplete"
test_uv_python_dir="$(mise x -- uv python dir)"
# Multiple provider entries can map to the same mise version. Preserve the
# first sorted entry, matching the previous create-if-missing behavior.
mkdir -p "$test_uv_python_dir/pypy-3.11.1-linux-x86_64-gnu"
mkdir -p "$test_uv_python_dir/unrecognized"
export UV_PYTHON_DOWNLOADS=never
assert "mise sync python --uv"
assert "test ! -f \"$MISE_CACHE_DIR/python/3.11.1/incomplete\""
assert "mise where python@3.11.1"
assert_contains "readlink \"$MISE_DATA_DIR/installs/python/3.11.1\"" "cpython-3.11.1"
touch "$MISE_CACHE_DIR/python/3.11.1/incomplete"
assert "mise sync python --uv"
assert "test ! -f \"$MISE_CACHE_DIR/python/3.11.1/incomplete\""
assert "mise x python@3.11.1 -- python -V" "Python 3.11.1"
assert "mise x -- uv run -p 3.11.3 -- python -V" "Python 3.11.3"

# Joint reconciliation lets the earlier pyenv source replace a stale uv-owned
# link before uv removes it.
export PYENV_ROOT="$PWD/.pyenv"
mkdir -p "$PYENV_ROOT/versions/3.11.2"
ln -s "$test_uv_python_dir/cpython-3.11.2-linux-x86_64-gnu" \
  "$MISE_DATA_DIR/installs/python/3.11.2"
assert "mise sync python --pyenv --uv"
assert_contains "readlink \"$MISE_DATA_DIR/installs/python/3.11.2\"" ".pyenv/versions/3.11.2"
