Skip to content

Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.

43.6 KB
Raw Blame
#!/bin/bash
# SPDX-License-Identifier: AGPL-3.0-only
# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
# Guards install.sh's uv bootstrap: pinned release archive verified against a hardcoded SHA-256,
# with astral's installer kept as a fallback for musl, armv7 and hosts without a digest tool.
set -e

SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
. "$SCRIPT_DIR/_harness.sh"
INSTALL_SH="$SCRIPT_DIR/../../install.sh"
WORK="$(mktemp -d)"
trap 'rm -rf "$WORK"' EXIT

echo "=== source contract ==="

if grep -q '_uv_install_pinned' "$INSTALL_SH"; then
    ok "install.sh has a pinned-release uv path"
else
    bad "install.sh has a pinned-release uv path"
fi

# The pinned attempt must precede the fallback, or the fallback is what actually runs.
_pinned_at=$(grep -n 'if _uv_install_pinned[ ;]' "$INSTALL_SH" | head -1 | cut -d: -f1)
_fallback_at=$(grep -n 'download "https://astral.sh/uv/$UV_PINNED_VERSION/install.sh"' "$INSTALL_SH" | head -1 | cut -d: -f1)
if [ -n "$_pinned_at" ] && [ -n "$_fallback_at" ] && [ "$_pinned_at" -lt "$_fallback_at" ]; then
    ok "the pinned path is tried before the astral fallback"
else
    bad "the pinned path is tried before the astral fallback (pinned=$_pinned_at fallback=$_fallback_at)"
fi

# A truncated digest would never match and route every host to the fallback.
_bad_digests=$(grep -oE 'uv-[a-z0-9_]+-[a-z0-9.-]+\.tar\.gz [0-9a-f]*' "$INSTALL_SH" \
    | awk '{ if (length($2) != 64) print }' | wc -l | tr -d ' ')
if [ "$_bad_digests" = "0" ]; then
    ok "every pinned archive digest is a full sha256"
else
    bad "every pinned archive digest is a full sha256 ($_bad_digests malformed)"
fi

if grep -q '^UV_PINNED_VERSION="' "$INSTALL_SH"; then
    ok "the pinned uv version is a single constant"
else
    bad "the pinned uv version is a single constant"
fi

# All four installers must pin the same uv.
_pinned_versions=$(
    grep -hoE '(UV_PINNED_VERSION|_SETUP_UV_PINNED_VERSION)="[0-9.]+"' \
        "$INSTALL_SH" "$SCRIPT_DIR/../../studio/setup.sh"
    grep -hoE '\$UvPinnedVersion += +"[0-9.]+"' \
        "$SCRIPT_DIR/../../install.ps1" "$SCRIPT_DIR/../../studio/setup.ps1"
)
_pinned_distinct=$(printf '%s\n' "$_pinned_versions" | grep -oE '[0-9]+\.[0-9]+(\.[0-9]+)?' | sort -u)
_pinned_count=$(printf '%s\n' "$_pinned_distinct" | grep -c .)
if [ "$(printf '%s\n' "$_pinned_versions" | grep -c .)" = "4" ] && [ "$_pinned_count" = "1" ]; then
    ok "all four installers pin the same uv ($_pinned_distinct)"
else
    bad "the pinned uv version disagrees across installers: $(printf '%s' "$_pinned_distinct" | tr '\n' ' ')"
fi

# The pin must clear every uv version floor in the tree, or the script installs a too-old uv.
_floors=$(
    grep -hoE '^UV_MIN_VERSION="[0-9.]+"|^UV_OFFLINE_MIN_VERSION="[0-9.]+"' "$INSTALL_SH"
    grep -hoE '\$UvMinVersion += +"[0-9.]+"' "$SCRIPT_DIR/../../install.ps1"
)
_floor_bad=0
for _floor in $(printf '%s\n' "$_floors" | grep -oE '[0-9]+\.[0-9]+(\.[0-9]+)?'); do
    _lowest=$(printf '%s\n%s\n' "$_floor" "$_pinned_distinct" | sort -V | head -1)
    if [ "$_lowest" != "$_floor" ] && [ "$_floor" != "$_pinned_distinct" ]; then
        echo "      floor $_floor is above the pin $_pinned_distinct"
        _floor_bad=$((_floor_bad + 1))
    fi
done
if [ "$_floor_bad" = 0 ]; then
    ok "the pinned uv clears every version floor in the tree"
else
    bad "$_floor_bad version floor(s) sit above the pinned uv"
fi

# The profile-write guard must read the inherited PATH: this process already prepended
# the directory, so the live $PATH would wrongly say a new shell can see it.
_snapshot_at=$(grep -n '^_UNSLOTH_LOGIN_PATH="\$PATH"' "$INSTALL_SH" | head -1 | cut -d: -f1)
_first_mutation=$(grep -n 'export PATH=' "$INSTALL_SH" | head -1 | cut -d: -f1)
if [ -n "$_snapshot_at" ] && [ -n "$_first_mutation" ] && [ "$_snapshot_at" -lt "$_first_mutation" ]; then
    ok "the login PATH is snapshotted before anything prepends to PATH"
else
    bad "the login PATH snapshot is missing or too late (snapshot=$_snapshot_at first mutation=$_first_mutation)"
fi
if grep -q '_path_has_dir "\$_UNSLOTH_LOGIN_PATH"' "$INSTALL_SH"; then
    ok "the shell-profile guard tests the inherited PATH, not the one we prepended to"
else
    bad "the shell-profile guard tests the inherited PATH, not the one we prepended to"
fi

# A fresh account may have no rc file, so the guard must create one. Runs the real block,
# including _persist_login_path_dir.
_fn_start=$(grep -n '^_path_has_dir() {' "$INSTALL_SH" | head -1 | cut -d: -f1)
_guard_end=$(grep -n '^# end of the PATH persistence block$' "$INSTALL_SH" | head -1 | cut -d: -f1)
sed -n "${_fn_start},${_guard_end}p" "$INSTALL_SH" > "$WORK/path_guard.sh"
mkdir -p "$WORK/fresh_home/.local/bin"
(
    set +e
    step() { :; }
    HOME="$WORK/fresh_home"; export HOME
    SHELL="/bin/bash"; export SHELL
    unset ZSH_VERSION
    _LOCAL_BIN="$HOME/.local/bin"
    _STUDIO_HOME_REDIRECT="none"
    _UNSLOTH_LOGIN_PATH="/usr/bin:/bin"
    # shellcheck disable=SC1090
    . "$WORK/path_guard.sh"
) >/dev/null 2>&1 || true
if grep -q '\.local/bin' "$WORK/fresh_home/.profile" 2>/dev/null; then
    ok "an account with no rc file still gets ~/.local/bin persisted"
else
    bad "an account with no rc file still gets ~/.local/bin persisted"
fi
(
    set +e
    step() { :; }
    HOME="$WORK/fresh_home"; export HOME
    SHELL="/bin/bash"; export SHELL
    unset ZSH_VERSION
    _LOCAL_BIN="$HOME/.local/bin"
    _STUDIO_HOME_REDIRECT="none"
    _UNSLOTH_LOGIN_PATH="/usr/bin:/bin"
    # shellcheck disable=SC1090
    . "$WORK/path_guard.sh"
) >/dev/null 2>&1 || true
if [ "$(grep -c 'Added by Unsloth installer' "$WORK/fresh_home/.profile")" = "1" ]; then
    ok "the created profile is written once, not once per run"
else
    bad "the created profile is written once, not once per run"
fi

# The digest check belongs inside the mirror loop: a proxy answering 200 with its own body
# would otherwise consume the only attempt.
for _ps in "$SCRIPT_DIR/../../install.ps1" "$SCRIPT_DIR/../../studio/setup.ps1"; do
    _dl_line=$(grep -n 'if (-not \$downloaded) { return \$false }' "$_ps" | head -1 | cut -d: -f1)
    _hash_line=$(grep -n 'Get-FileHash -LiteralPath \$zip' "$_ps" | head -1 | cut -d: -f1)
    if [ -n "$_dl_line" ] && [ -n "$_hash_line" ] && [ "$_hash_line" -lt "$_dl_line" ]; then
        ok "${_ps##*/} verifies each uv mirror before it stops trying them"
    else
        bad "${_ps##*/} verifies each uv mirror before it stops trying them"
    fi
done

# A configured uv mirror is exclusive: download() has no timeout, so trying public hosts stalls.
for _impl in "$INSTALL_SH" "$SCRIPT_DIR/../../studio/setup.sh"; do
    if grep -q 'UV_INSTALLER_GHE_BASE_URL' "$_impl" && grep -q 'UV_INSTALLER_GITHUB_BASE_URL' "$_impl"; then
        ok "${_impl##*/} honours a configured uv mirror"
    else
        bad "${_impl##*/} honours a configured uv mirror"
    fi
done

# Staging name must be unique per process (mktemp in the destination dir), or a concurrent
# installer can publish a truncated uv.
for _impl in "$INSTALL_SH" "$SCRIPT_DIR/../../studio/setup.sh"; do
    if grep -qE 'mktemp "\$_[a-z]+_dest/\.\$_[a-z]+_exe\.XXXXXX"' "$_impl" \
       && ! grep -q 'unsloth-new' "$_impl"; then
        ok "${_impl##*/} stages the uv copy under a per-process name"
    else
        bad "${_impl##*/} stages the uv copy under a per-process name"
    fi
done

# UV_DOWNLOAD_URL / INSTALLER_DOWNLOAD_URL take priority over mirror vars, as in astral's installer.
for _impl in "$INSTALL_SH" "$SCRIPT_DIR/../../studio/setup.sh" \
             "$SCRIPT_DIR/../../install.ps1" "$SCRIPT_DIR/../../studio/setup.ps1"; do
    if grep -q 'UV_DOWNLOAD_URL' "$_impl" && grep -q 'INSTALLER_DOWNLOAD_URL' "$_impl"; then
        ok "${_impl##*/} honours astral's primary download override"
    else
        bad "${_impl##*/} honours astral's primary download override"
    fi
done

# 0755, not the umask default, so uv stays usable for other accounts.
for _impl in "$INSTALL_SH" "$SCRIPT_DIR/../../studio/setup.sh"; do
    if grep -q 'chmod 0755' "$_impl" && ! grep -qE 'chmod \+x "\$_[a-z]+_stage"' "$_impl"; then
        ok "${_impl##*/} stages uv with an explicit 0755"
    else
        bad "${_impl##*/} stages uv with an explicit 0755"
    fi
done

# astral's priority puts XDG_DATA_HOME/../bin between XDG_BIN_HOME and the home default.
for _impl in "$INSTALL_SH" "$SCRIPT_DIR/../../studio/setup.sh" \
             "$SCRIPT_DIR/../../install.ps1" "$SCRIPT_DIR/../../studio/setup.ps1"; do
    if grep -q 'XDG_DATA_HOME' "$_impl"; then
        ok "${_impl##*/} honours the XDG_DATA_HOME destination tier"
    else
        bad "${_impl##*/} honours the XDG_DATA_HOME destination tier"
    fi
done

echo "=== behaviour ==="

awk '/^# ── uv from a pinned release ──$/,/^if ! command -v uv /' "$INSTALL_SH" \
    | sed '$d' > "$WORK/uvfns.sh"

mkdir -p "$WORK/src/uv-fake-triple"
printf '#!/bin/sh\necho "uv 0.12.1 (fake)"\n' > "$WORK/src/uv-fake-triple/uv"
printf '#!/bin/sh\necho "uvx"\n' > "$WORK/src/uv-fake-triple/uvx"
tar -czf "$WORK/uv-fake.tar.gz" -C "$WORK/src" uv-fake-triple
if command -v sha256sum >/dev/null 2>&1; then
    FIXTURE_SHA=$(sha256sum "$WORK/uv-fake.tar.gz" | awk '{print $1}')
else
    FIXTURE_SHA=$(shasum -a 256 "$WORK/uv-fake.tar.gz" | awk '{print $1}')
fi

run_case() {
    # $ADVERTISED = the digest the pin claims, $1 = HOME for the run
    _rc_home="$1"
    mkdir -p "$_rc_home"
    (
        set +e
        tauri_log() { :; }
        # shellcheck disable=SC1090
        . "$WORK/uvfns.sh"
        _uv_pinned_asset() { echo "uv-fake.tar.gz $ADVERTISED"; }
        download() { cp -f "$WORK/uv-fake.tar.gz" "$2"; }
        HOME="$_rc_home"
        unset UV_INSTALL_DIR UV_UNMANAGED_INSTALL XDG_BIN_HOME
        if [ -n "${CASE_XDG_DATA_HOME:-}" ]; then
            export XDG_DATA_HOME="$CASE_XDG_DATA_HOME"
        else
            unset XDG_DATA_HOME
        fi
        _uv_install_pinned
        echo "rc=$?"
    )
}

ADVERTISED="$FIXTURE_SHA" run_case "$WORK/home_ok" > "$WORK/out_ok" 2>&1 || true
if grep -q '^rc=0$' "$WORK/out_ok" && [ -x "$WORK/home_ok/.local/bin/uv" ]; then
    ok "a matching digest installs uv into the default destination"
else
    bad "a matching digest installs uv into the default destination"
    sed 's/^/      /' "$WORK/out_ok"
fi

if [ -x "$WORK/home_ok/.local/bin/uvx" ]; then
    ok "uvx is installed alongside uv"
else
    bad "uvx is installed alongside uv"
fi

ADVERTISED="$FIXTURE_SHA" CASE_XDG_DATA_HOME="$WORK/home_xdg/share" \
    run_case "$WORK/home_xdg" > "$WORK/out_xdg" 2>&1 || true
if [ -x "$WORK/home_xdg/bin/uv" ] && [ ! -e "$WORK/home_xdg/.local/bin/uv" ]; then
    ok "XDG_DATA_HOME redirects the install away from the home default"
else
    bad "XDG_DATA_HOME redirects the install away from the home default"
    sed 's/^/      /' "$WORK/out_xdg"
fi

ADVERTISED="0000000000000000000000000000000000000000000000000000000000000000" \
    run_case "$WORK/home_bad" > "$WORK/out_bad" 2>&1 || true
if grep -q '^rc=0$' "$WORK/out_bad"; then
    bad "a mismatched digest is rejected"
else
    ok "a mismatched digest is rejected"
fi
if [ -e "$WORK/home_bad/.local/bin/uv" ]; then
    bad "a rejected archive installs nothing"
else
    ok "a rejected archive installs nothing"
fi

# Re-runs over the same HOME must land on the same tree.
_sig() { printf '%s|%s' "$(cd "$1" && find . -type f | LC_ALL=C sort | tr '\n' ' ')" \
                        "$(cat "$1/.local/bin/uv" 2>/dev/null)"; }
_idem_ok=1
_idem_sig=""
for _i in 1 2 3; do
    ADVERTISED="$FIXTURE_SHA" run_case "$WORK/home_idem" > "$WORK/out_idem_$_i" 2>&1 || true
    grep -q '^rc=0$' "$WORK/out_idem_$_i" || _idem_ok=0
    _s=$(_sig "$WORK/home_idem")
    [ -z "$_idem_sig" ] && _idem_sig="$_s"
    [ "$_s" = "$_idem_sig" ] || _idem_ok=0
done
if [ "$_idem_ok" = 1 ]; then
    ok "runs 1..3 over the same HOME leave an identical tree"
else
    bad "repeat application is not idempotent"
    sed 's/^/      /' "$WORK/out_idem_3"
fi

printf 'stale binary' > "$WORK/home_idem/.local/bin/uv"
ADVERTISED="$FIXTURE_SHA" run_case "$WORK/home_idem" > "$WORK/out_replace" 2>&1 || true
if grep -q 'fake' "$WORK/home_idem/.local/bin/uv" 2>/dev/null \
   && [ "$(cd "$WORK/home_idem" && find . -name uv -type f | wc -l | tr -d ' ')" = 1 ]; then
    ok "an existing uv at the destination is replaced in place"
else
    bad "an existing uv at the destination is replaced in place"
fi

# A symlinked destination must be replaced, not written through (e.g. -> Homebrew's uv).
mkdir -p "$WORK/home_link/.local/bin" "$WORK/elsewhere"
printf 'other package manager owns this' > "$WORK/elsewhere/uv"
_link_before=$(sha256sum "$WORK/elsewhere/uv" 2>/dev/null || shasum -a 256 "$WORK/elsewhere/uv")
ln -sf "$WORK/elsewhere/uv" "$WORK/home_link/.local/bin/uv"
ADVERTISED="$FIXTURE_SHA" run_case "$WORK/home_link" > "$WORK/out_link" 2>&1 || true
_link_after=$(sha256sum "$WORK/elsewhere/uv" 2>/dev/null || shasum -a 256 "$WORK/elsewhere/uv")
if [ "$_link_before" = "$_link_after" ]; then
    ok "a symlinked destination is not written through"
else
    bad "the install rewrote the file the destination symlink pointed at"
fi
if [ ! -L "$WORK/home_link/.local/bin/uv" ] && grep -q 'fake' "$WORK/home_link/.local/bin/uv" 2>/dev/null; then
    ok "the symlink itself is replaced by the installed uv"
else
    bad "the symlink itself is replaced by the installed uv"
fi
if [ -z "$(find "$WORK/home_link/.local/bin" -name '.uv.*' 2>/dev/null)" ]; then
    ok "no staging file is left behind"
else
    bad "no staging file is left behind"
fi

# A binary that cannot execute (missing loader) must decline so the fallback still runs.
mkdir -p "$WORK/src_bad/uv-fake-triple"
printf '\177ELF not a real loader target\n' > "$WORK/src_bad/uv-fake-triple/uv"
printf '#!/bin/sh\necho uvx\n' > "$WORK/src_bad/uv-fake-triple/uvx"
tar -czf "$WORK/uv-bad.tar.gz" -C "$WORK/src_bad" uv-fake-triple
if command -v sha256sum >/dev/null 2>&1; then
    BAD_EXEC_SHA=$(sha256sum "$WORK/uv-bad.tar.gz" | awk '{print $1}')
else
    BAD_EXEC_SHA=$(shasum -a 256 "$WORK/uv-bad.tar.gz" | awk '{print $1}')
fi
mkdir -p "$WORK/home_noexec"
(
    set +e
    tauri_log() { :; }
    # shellcheck disable=SC1090
    . "$WORK/uvfns.sh"
    _uv_pinned_asset() { echo "uv-bad.tar.gz $BAD_EXEC_SHA"; }
    download() { cp -f "$WORK/uv-bad.tar.gz" "$2"; }
    HOME="$WORK/home_noexec"; export HOME
    unset UV_INSTALL_DIR UV_UNMANAGED_INSTALL XDG_BIN_HOME XDG_DATA_HOME
    _uv_install_pinned
    echo "rc=$?"
) > "$WORK/out_noexec" 2>&1 || true
if grep -q '^rc=0$' "$WORK/out_noexec"; then
    bad "a uv that cannot execute declines to the fallback"
else
    ok "a uv that cannot execute declines to the fallback"
fi

_unfetched() ( set +e; tauri_log() { :; }; . "$WORK/uvfns.sh"; HOME="$WORK/home_noexec"; unset UNSLOTH_UV_WHEEL_MIRROR; eval "$1"; _uv_install_pinned > /dev/null 2>&1; echo "$_UIP_UNFETCHED" )
_got=$(echo $(_unfetched '_uv_pinned_asset() { return 1; }'; _unfetched "_uv_pinned_asset() { echo 'uv-bad.tar.gz 00'; }; download() { return 6; }"; _unfetched "_uv_pinned_asset() { echo 'uv-bad.tar.gz 00'; }; download() { cp -f '$WORK/uv-bad.tar.gz' \"\$2\"; }"))
if [ "$_got" = "false true false" ]; then ok "_UIP_UNFETCHED: only a download no source served is left for the mirror retry"; else bad "only a download no source served is left for the mirror (got: $_got)"; fi

# Validation must happen before the rename, or a failed new uv destroys the working old one.
mkdir -p "$WORK/home_keep/.local/bin"
printf '#!/bin/sh\necho "uv 0.9.9 (incumbent)"\n' > "$WORK/home_keep/.local/bin/uv"
chmod +x "$WORK/home_keep/.local/bin/uv"
(
    set +e
    tauri_log() { :; }
    # shellcheck disable=SC1090
    . "$WORK/uvfns.sh"
    _uv_pinned_asset() { echo "uv-bad.tar.gz $BAD_EXEC_SHA"; }
    download() { cp -f "$WORK/uv-bad.tar.gz" "$2"; }
    HOME="$WORK/home_keep"; export HOME
    unset UV_INSTALL_DIR UV_UNMANAGED_INSTALL XDG_BIN_HOME XDG_DATA_HOME
    _uv_install_pinned
) >/dev/null 2>&1 || true
if "$WORK/home_keep/.local/bin/uv" 2>/dev/null | grep -q incumbent; then
    ok "a uv that cannot execute never replaces the working one"
else
    bad "a uv that cannot execute replaced the working one"
fi
if [ -z "$(find "$WORK/home_keep/.local/bin" -name '.uv.*' 2>/dev/null)" ]; then
    ok "the rejected staging file is cleaned up"
else
    bad "the rejected staging file is cleaned up"
fi
# uv and uvx ship as a set: rejecting uv must abandon the whole placement.
if [ -f "$WORK/home_keep/.local/bin/uvx" ]; then
    bad "a rejected uv must not publish its uvx"
else
    ok "a rejected uv must not publish its uvx"
fi

# The probe must be bounded against reading stdin and never exiting.
mkdir -p "$WORK/src_hang/uv-fake-triple"
printf '#!/bin/sh\nread _line\nsleep 120\n' > "$WORK/src_hang/uv-fake-triple/uv"
chmod +x "$WORK/src_hang/uv-fake-triple/uv"
printf '#!/bin/sh\necho uvx\n' > "$WORK/src_hang/uv-fake-triple/uvx"
tar -czf "$WORK/uv-hang.tar.gz" -C "$WORK/src_hang" uv-fake-triple
if command -v sha256sum >/dev/null 2>&1; then
    HANG_SHA=$(sha256sum "$WORK/uv-hang.tar.gz" | awk '{print $1}')
else
    HANG_SHA=$(shasum -a 256 "$WORK/uv-hang.tar.gz" | awk '{print $1}')
fi
mkdir -p "$WORK/home_hang"
_HANG_START=$(date +%s)
(
    set +e
    tauri_log() { :; }
    # shellcheck disable=SC1090
    . "$WORK/uvfns.sh"
    _uv_pinned_asset() { echo "uv-hang.tar.gz $HANG_SHA"; }
    download() { cp -f "$WORK/uv-hang.tar.gz" "$2"; }
    HOME="$WORK/home_hang"; export HOME
    unset UV_INSTALL_DIR UV_UNMANAGED_INSTALL XDG_BIN_HOME XDG_DATA_HOME
    _uv_install_pinned
    echo "rc=$?"
) > "$WORK/out_hang" 2>&1 || true
_HANG_ELAPSED=$(( $(date +%s) - _HANG_START ))
# 40s: hosts without `timeout` still return because stdin is closed.
if [ "$_HANG_ELAPSED" -lt 40 ] && grep -q '^rc=1$' "$WORK/out_hang"; then
    ok "the executable probe is bounded and declines (${_HANG_ELAPSED}s)"
else
    bad "the executable probe is bounded and declines (${_HANG_ELAPSED}s, $(cat "$WORK/out_hang"))"
fi

# Host matrix: every host must get its own triple or decline to the fallback.
# $1 fn, $2 os, $3 arch, $4 libc: musl | none | glibc version | rosetta; $5 bits: getconf LONG_BIT.
_probe_asset() {
    (
        set +e
        # Bind before the stubs: inside a function body $2..$5 are the stub's own arguments.
        _pa_os="$2"; _pa_arch="$3"; _pa_libc="$4"; _pa_bits="$5"
        tauri_log() { :; }
        # shellcheck disable=SC1090
        . "$WORK/uvfns.sh"
        uname() { case "${1:-}" in -m) echo "$_pa_arch" ;; *) echo "$_pa_os" ;; esac; }
        ldd() {
            case "$_pa_libc" in
                musl) echo "musl libc (x86_64)" ;;
                none) return 127 ;;
                *) echo "ldd (Ubuntu GLIBC $_pa_libc-0ubuntu1) $_pa_libc" ;;
            esac
        }
        getconf() {
            case "${1:-}" in
                LONG_BIT) echo "$_pa_bits" ;;
                GNU_LIBC_VERSION) [ "$_pa_libc" = none ] && return 1; echo "glibc $_pa_libc" ;;
            esac
        }
        sysctl() { [ "$_pa_libc" = rosetta ] && echo 1; }
        "$1" 2>/dev/null
    )
}
_matrix_bad=0
# "<os> <arch> <libc> <bits> <expected triple, or - to decline>"
while read -r _m_os _m_arch _m_libc _m_bits _m_want; do
    [ -n "$_m_os" ] || continue
    # `|| _m_got=`: a declining host exits non-zero, and set -e would end the run here.
    _m_got=$(_probe_asset _uv_pinned_asset "$_m_os" "$_m_arch" "$_m_libc" "$_m_bits") || _m_got=""
    _m_label="$_m_os/$_m_arch/$_m_libc/$_m_bits"
    if [ "$_m_want" = "-" ]; then
        [ -z "$_m_got" ] || { echo "      $_m_label should decline, got '$_m_got'"; _matrix_bad=$((_matrix_bad + 1)); }
    else
        case "$_m_got" in
            "uv-$_m_want.tar.gz "*) : ;;
            *) echo "      $_m_label expected $_m_want, got '$_m_got'"; _matrix_bad=$((_matrix_bad + 1)) ;;
        esac
    fi
done <<'MATRIX'
Linux x86_64 2.35 64 x86_64-unknown-linux-gnu
Linux amd64 2.35 64 x86_64-unknown-linux-gnu
Linux aarch64 2.35 64 aarch64-unknown-linux-gnu
Linux arm64 2.28 64 aarch64-unknown-linux-gnu
Linux x86_64 2.17 64 x86_64-unknown-linux-gnu
Darwin x86_64 2.35 64 x86_64-apple-darwin
Darwin arm64 2.35 64 aarch64-apple-darwin
Darwin aarch64 2.35 64 aarch64-apple-darwin
Darwin x86_64 rosetta 64 aarch64-apple-darwin
Linux x86_64 musl 64 -
Linux aarch64 musl 64 -
Linux x86_64 none 64 -
Linux aarch64 none 64 -
Linux x86_64 2.12 64 -
Linux aarch64 2.27 64 -
Linux x86_64 2.35 32 -
Linux aarch64 2.35 32 -
Linux armv7l 2.35 64 -
Linux i686 2.35 64 -
Linux ppc64le 2.35 64 -
Linux riscv64 2.35 64 -
Linux s390x 2.35 64 -
Darwin i386 2.35 64 -
FreeBSD x86_64 2.35 64 -
SunOS x86_64 2.35 64 -
MINGW64_NT-10.0 x86_64 2.35 64 -
CYGWIN_NT-10.0 x86_64 2.35 64 -
unknown unknown 2.35 64 -
MATRIX
if [ "$_matrix_bad" = 0 ]; then
    ok "every host either gets its own triple or declines to the fallback"
else
    bad "the host matrix has $_matrix_bad wrong outcomes"
fi

(
    set +e
    tauri_log() { :; }
    # shellcheck disable=SC1090
    . "$WORK/uvfns.sh"
    uname() { if [ "$1" = "-s" ]; then echo Linux; else echo sparc64; fi; }
    _uv_pinned_asset >/dev/null 2>&1
    echo "rc=$?"
) > "$WORK/out_arch" 2>&1 || true
if grep -q '^rc=0$' "$WORK/out_arch"; then
    bad "an unpinned architecture declines so the fallback runs"
else
    ok "an unpinned architecture declines so the fallback runs"
fi

# Temporaries (~40 MB work dir, staging file on PATH) must be cleaned by the signal and exit traps.
_cleanup_body=$(awk '/^_cleanup_install_temporaries\(\) \{/,/^\}/' "$INSTALL_SH")
if printf '%s' "$_cleanup_body" | grep -q '_UIP_WORK' && printf '%s' "$_cleanup_body" | grep -q '_UIP_STAGE'; then
    ok "the pinned uv temporaries are removed by the interrupt cleanup"
else
    bad "the pinned uv temporaries are removed by the interrupt cleanup"
fi
if grep -q '^_UIP_WORK=""' "$INSTALL_SH" && grep -q '_UIP_WORK="\$_uip_work"' "$INSTALL_SH" \
   && grep -q '_UIP_STAGE="\$_uip_stage"' "$INSTALL_SH"; then
    ok "the pinned uv temporaries are published to the trap as they are created"
else
    bad "the pinned uv temporaries are published to the trap as they are created"
fi
# fish sources no POSIX rc files, so it needs its own drop-in.
mkdir -p "$WORK/fish_home/.local/bin"
(
    set +e
    step() { :; }
    HOME="$WORK/fish_home"; export HOME
    SHELL="/usr/bin/fish"; export SHELL
    unset ZSH_VERSION XDG_CONFIG_HOME
    _LOCAL_BIN="$HOME/.local/bin"
    _STUDIO_HOME_REDIRECT="none"
    _UNSLOTH_LOGIN_PATH="/usr/bin:/bin"
    # shellcheck disable=SC1090
    . "$WORK/path_guard.sh"
) >/dev/null 2>&1 || true
if grep -q 'fish_add_path' "$WORK/fish_home/.config/fish/conf.d/unsloth.fish" 2>/dev/null \
   && [ ! -f "$WORK/fish_home/.profile" ]; then
    ok "a fish login shell gets a fish drop-in, not an ignored ~/.profile"
else
    bad "a fish login shell gets a fish drop-in, not an ignored ~/.profile"
fi

# uv may land outside ~/.local/bin (UV_INSTALL_DIR), so its destination must be persisted too.
mkdir -p "$WORK/uvdir_home/.local/bin" "$WORK/uvdir_home/opt/uvbin"
: > "$WORK/uvdir_home/.bashrc"
(
    set +e
    step() { :; }
    HOME="$WORK/uvdir_home"; export HOME
    SHELL="/bin/bash"; export SHELL
    unset ZSH_VERSION UV_NO_MODIFY_PATH
    _LOCAL_BIN="$HOME/.local/bin"
    _STUDIO_HOME_REDIRECT="none"
    _UNSLOTH_LOGIN_PATH="/usr/bin:/bin"
    _UNSLOTH_UV_BIN_DIR="$HOME/opt/uvbin"
    # shellcheck disable=SC1090
    . "$WORK/path_guard.sh"
) >/dev/null 2>&1 || true
if grep -qF "$WORK/uvdir_home/opt/uvbin" "$WORK/uvdir_home/.bashrc" 2>/dev/null; then
    ok "a custom uv install directory reaches the next shell"
else
    bad "a custom uv install directory reaches the next shell"
fi
# UV_NO_MODIFY_PATH is astral's opt-out and must be honoured.
mkdir -p "$WORK/uvopt_home/.local/bin" "$WORK/uvopt_home/opt/uvbin"
: > "$WORK/uvopt_home/.bashrc"
(
    set +e
    step() { :; }
    HOME="$WORK/uvopt_home"; export HOME
    SHELL="/bin/bash"; export SHELL
    unset ZSH_VERSION
    UV_NO_MODIFY_PATH=1; export UV_NO_MODIFY_PATH
    _LOCAL_BIN="$HOME/.local/bin"
    _STUDIO_HOME_REDIRECT="none"
    _UNSLOTH_LOGIN_PATH="/usr/bin:/bin"
    _UNSLOTH_UV_BIN_DIR="$HOME/opt/uvbin"
    # shellcheck disable=SC1090
    . "$WORK/path_guard.sh"
) >/dev/null 2>&1 || true
if grep -qF "$WORK/uvopt_home/opt/uvbin" "$WORK/uvopt_home/.bashrc" 2>/dev/null; then
    bad "UV_NO_MODIFY_PATH still suppresses the uv directory write"
else
    ok "UV_NO_MODIFY_PATH still suppresses the uv directory write"
fi

# Paths with spaces must be quoted in the fish drop-in.
mkdir -p "$WORK/fish sp home/.local/bin"
(
    set +e
    step() { :; }
    HOME="$WORK/fish sp home"; export HOME
    SHELL="/usr/bin/fish"; export SHELL
    unset ZSH_VERSION XDG_CONFIG_HOME
    _LOCAL_BIN="$HOME/.local/bin"
    _STUDIO_HOME_REDIRECT="none"
    _UNSLOTH_LOGIN_PATH="/usr/bin:/bin"
    # shellcheck disable=SC1090
    . "$WORK/path_guard.sh"
) >/dev/null 2>&1 || true
_fish_line=$(grep 'fish_add_path' "$WORK/fish sp home/.config/fish/conf.d/unsloth.fish" 2>/dev/null)
if [ "$_fish_line" = "fish_add_path '$WORK/fish sp home/.local/bin'" ]; then
    ok "a fish path with a space is written as one quoted argument"
else
    bad "a fish path with a space is written as one quoted argument ($_fish_line)"
fi

# The rc line is double-quoted, so $ or a backtick in a uv dir is expanded, not kept as a path.
mkdir -p "$WORK/uvmeta_home/.local/bin" "$WORK/uvmeta_home/opt/a\$b/bin"
: > "$WORK/uvmeta_home/.bashrc"
(
    set +e
    step() { :; }
    HOME="$WORK/uvmeta_home"; export HOME
    SHELL="/bin/bash"; export SHELL
    unset ZSH_VERSION UV_NO_MODIFY_PATH
    _LOCAL_BIN="$HOME/.local/bin"
    _STUDIO_HOME_REDIRECT="none"
    _UNSLOTH_LOGIN_PATH="/usr/bin:/bin"
    _UNSLOTH_UV_BIN_DIR="$HOME/opt/a\$b/bin"
    # shellcheck disable=SC1090
    . "$WORK/path_guard.sh"
) >/dev/null 2>&1 || true
_uvmeta_path=$(HOME="$WORK/uvmeta_home"; PATH="/usr/bin:/bin"; . "$WORK/uvmeta_home/.bashrc" 2>/dev/null; printf '%s' "$PATH")
case ":$_uvmeta_path:" in
    *":$WORK/uvmeta_home/opt/a\$b/bin:"*)
        ok "a uv directory holding shell metacharacters survives the rc round trip" ;;
    *)
        bad "a uv directory holding shell metacharacters survives the rc round trip" ;;
esac

# Failing to publish uvx after uv must restore the incumbent uv.
mkdir -p "$WORK/home_half/.local/bin"
printf '#!/bin/sh\necho "uv 0.9.9 (incumbent)"\n' > "$WORK/home_half/.local/bin/uv"
printf '#!/bin/sh\necho "uvx 0.9.9 (incumbent)"\n' > "$WORK/home_half/.local/bin/uvx"
chmod +x "$WORK/home_half/.local/bin/uv" "$WORK/home_half/.local/bin/uvx"
(
    set +e
    tauri_log() { :; }
    # Fail the second rename only: `mv f d` into a directory would succeed.
    mv() {
        case "$*" in
            *"/uvx") return 1 ;;
        esac
        command mv "$@"
    }
    # shellcheck disable=SC1090
    . "$WORK/uvfns.sh"
    _uv_pinned_asset() { echo "uv-fake.tar.gz $FIXTURE_SHA"; }
    download() { cp -f "$WORK/uv-fake.tar.gz" "$2"; }
    HOME="$WORK/home_half"; export HOME
    unset UV_INSTALL_DIR UV_UNMANAGED_INSTALL XDG_BIN_HOME XDG_DATA_HOME
    _uv_install_pinned
    echo "rc=$?"
) > "$WORK/out_half" 2>&1 || true
if grep -q '^rc=0$' "$WORK/out_half"; then
    bad "a half-published pair declines to the fallback"
else
    ok "a half-published pair declines to the fallback"
fi
if [ -z "$(find "$WORK/home_half/.local/bin" -maxdepth 1 -name '.uv*' 2>/dev/null)" ]; then
    ok "the half-published staging and undo files are cleaned up"
else
    bad "the half-published staging and undo files are cleaned up"
fi

# studio/setup.sh is run directly, so it must persist uv's PATH entry itself.
SETUP_SH="$SCRIPT_DIR/../../studio/setup.sh"
SETUP_PS1="$SCRIPT_DIR/../../studio/setup.ps1"
_sp_start=$(grep -n '^_setup_persist_uv_path() {' "$SETUP_SH" | head -1 | cut -d: -f1)
_sp_end=$(awk -v s="$_sp_start" 'NR>s && /^\}$/ {print NR; exit}' "$SETUP_SH")
sed -n "${_sp_start},${_sp_end}p" "$SETUP_SH" > "$WORK/setup_path.sh"
mkdir -p "$WORK/setup_home" "$WORK/setup_home/opt/bin"
: > "$WORK/setup_home/.bashrc"
(
    set +e
    HOME="$WORK/setup_home"; export HOME
    SHELL="/bin/bash"; export SHELL
    unset ZSH_VERSION UV_NO_MODIFY_PATH UV_UNMANAGED_INSTALL
    _SETUP_LOGIN_PATH="/usr/bin:/bin"
    # shellcheck disable=SC1090
    . "$WORK/setup_path.sh"
    _setup_persist_uv_path "$HOME/opt/bin"
) >/dev/null 2>&1 || true
if grep -qF "$WORK/setup_home/opt/bin" "$WORK/setup_home/.bashrc" 2>/dev/null; then
    ok "a direct setup.sh run persists the uv destination"
else
    bad "a direct setup.sh run persists the uv destination"
fi
# PYTHONPATH contains the text PATH, so only a name boundary keeps it from passing for an entry.
mkdir -p "$WORK/setup_mention" "$WORK/setup_mention/opt/bin"
printf 'export PYTHONPATH="%s/opt/bin"\n' "$WORK/setup_mention" > "$WORK/setup_mention/.bashrc"
(
    set +e
    HOME="$WORK/setup_mention"; export HOME
    SHELL="/bin/bash"; export SHELL
    unset ZSH_VERSION UV_NO_MODIFY_PATH UV_UNMANAGED_INSTALL
    _SETUP_LOGIN_PATH="/usr/bin:/bin"
    # shellcheck disable=SC1090
    . "$WORK/setup_path.sh"
    _setup_persist_uv_path "$HOME/opt/bin"
) >/dev/null 2>&1 || true
if grep -q 'export PATH=' "$WORK/setup_mention/.bashrc" 2>/dev/null; then
    ok "setup.sh writes past a non-PATH mention of the destination"
else
    bad "setup.sh writes past a non-PATH mention of the destination"
fi

mkdir -p "$WORK/setup_unmanaged" "$WORK/setup_unmanaged/opt/bin"
: > "$WORK/setup_unmanaged/.bashrc"
(
    set +e
    HOME="$WORK/setup_unmanaged"; export HOME
    SHELL="/bin/bash"; export SHELL
    unset ZSH_VERSION UV_NO_MODIFY_PATH
    UV_UNMANAGED_INSTALL=1; export UV_UNMANAGED_INSTALL
    _SETUP_LOGIN_PATH="/usr/bin:/bin"
    # shellcheck disable=SC1090
    . "$WORK/setup_path.sh"
    _setup_persist_uv_path "$HOME/opt/bin"
) >/dev/null 2>&1 || true
if grep -qF "$WORK/setup_unmanaged/opt/bin" "$WORK/setup_unmanaged/.bashrc" 2>/dev/null; then
    bad "UV_UNMANAGED_INSTALL suppresses the setup.sh profile write"
else
    ok "UV_UNMANAGED_INSTALL suppresses the setup.sh profile write"
fi

# A glob character in the destination must not act as a case pattern.
mkdir -p "$WORK/glob_home/.local/bin" "$WORK/glob_home/opt/star"
: > "$WORK/glob_home/.bashrc"
(
    set +e
    step() { :; }
    HOME="$WORK/glob_home"; export HOME
    SHELL="/bin/bash"; export SHELL
    unset ZSH_VERSION UV_NO_MODIFY_PATH UV_UNMANAGED_INSTALL
    _LOCAL_BIN="$HOME/.local/bin"
    _STUDIO_HOME_REDIRECT="none"
    _UNSLOTH_LOGIN_PATH="$HOME/opt/star:/usr/bin"
    _UNSLOTH_UV_BIN_DIR="$HOME/opt/*"
    # shellcheck disable=SC1090
    . "$WORK/path_guard.sh"
) >/dev/null 2>&1 || true
if grep -qF 'opt/*' "$WORK/glob_home/.bashrc" 2>/dev/null; then
    ok "a uv directory holding a glob character is compared literally"
else
    bad "a uv directory holding a glob character is compared literally"
fi

# Run the fish escapers through sed rather than reading them: an invalid one kills setup.
for _impl in "$INSTALL_SH" "$SETUP_SH"; do
    _esc_line=$(grep -h 'sed "s/' "$_impl" | grep 'fish\|_quoted=' | head -1)
    _esc_expr=${_esc_line#*| }
    if [ -n "$_esc_expr" ] && printf '%s' "/opt/a b" | eval "${_esc_expr%\)}" >/dev/null 2>&1; then
        ok "${_impl##*/} escapes fish paths with a valid sed expression"
    else
        bad "${_impl##*/} escapes fish paths with a valid sed expression"
    fi
done

# A commented-out export or a prefix-matching directory is not an active PATH entry.
for _case in comment prefix; do
    _ch="$WORK/entry_$_case"
    mkdir -p "$_ch/.local/bin" "$_ch/opt/uv"
    if [ "$_case" = comment ]; then
        printf '# export PATH="%s/opt/uv:$PATH"\n' "$_ch" > "$_ch/.bashrc"
    else
        printf 'export PATH="%s/opt/uv-old:$PATH"\n' "$_ch" > "$_ch/.bashrc"
    fi
    (
        set +e
        step() { :; }
        HOME="$_ch"; export HOME
        SHELL="/bin/bash"; export SHELL
        unset ZSH_VERSION UV_NO_MODIFY_PATH UV_UNMANAGED_INSTALL
        _LOCAL_BIN="$HOME/.local/bin"
        _STUDIO_HOME_REDIRECT="none"
        _UNSLOTH_LOGIN_PATH="/usr/bin:/bin"
        _UNSLOTH_UV_BIN_DIR="$HOME/opt/uv"
        # shellcheck disable=SC1090
        . "$WORK/path_guard.sh"
    ) >/dev/null 2>&1 || true
    # Count only active lines naming the uv dir exactly; `|| _n=0` because grep -c exits 1 on none.
    _n=$(grep -v '^[[:space:]]*#' "$_ch/.bashrc" | grep -cE "(^|[^[:alnum:]_.~/-])$_ch/opt/uv([^[:alnum:]_.~/-]|$)") || _n=0
    if [ "$_n" = "1" ]; then
        ok "an inactive entry ($_case) does not suppress the uv PATH write"
    else
        bad "an inactive entry ($_case) does not suppress the uv PATH write (active lines: $_n)"
    fi
done
_ch="$WORK/entry_active"
mkdir -p "$_ch/.local/bin" "$_ch/opt/uv"
printf 'export PATH="%s/opt/uv:$PATH"\n' "$_ch" > "$_ch/.bashrc"
(
    set +e
    step() { :; }
    HOME="$_ch"; export HOME
    SHELL="/bin/bash"; export SHELL
    unset ZSH_VERSION UV_NO_MODIFY_PATH UV_UNMANAGED_INSTALL
    _LOCAL_BIN="$HOME/.local/bin"
    _STUDIO_HOME_REDIRECT="none"
    _UNSLOTH_LOGIN_PATH="/usr/bin:/bin"
    _UNSLOTH_UV_BIN_DIR="$HOME/opt/uv"
    # shellcheck disable=SC1090
    . "$WORK/path_guard.sh"
) >/dev/null 2>&1 || true
_n=$(grep -v '^[[:space:]]*#' "$_ch/.bashrc" | grep -cE "(^|[^[:alnum:]_.~/-])$_ch/opt/uv([^[:alnum:]_.~/-]|$)") || _n=0
if [ "$_n" = "1" ]; then
    ok "an active entry still suppresses the uv PATH write"
else
    bad "an active entry still suppresses the uv PATH write (active lines: $_n)"
fi

# The pinned destination must go back in front of PATH after the ~/.local/bin prepend.
if grep -q 'export PATH="\$_UNSLOTH_UV_BIN_DIR:\$PATH"' "$INSTALL_SH"; then
    ok "install.sh keeps the pinned uv destination ahead of ~/.local/bin"
else
    bad "install.sh keeps the pinned uv destination ahead of ~/.local/bin"
fi
if grep -q '\[ "\$_SETUP_UV_PINNED_OK" = true \] || export PATH="\$HOME/.local/bin:\$PATH"' "$SETUP_SH"; then
    ok "setup.sh does not prepend ~/.local/bin over a pinned destination"
else
    bad "setup.sh does not prepend ~/.local/bin over a pinned destination"
fi

# NSIS hooks run before the user can cancel and $INSTDIR may be user-chosen, so tidy-up
# applies only where our executable already is.
_hooks="$SCRIPT_DIR/../../studio/src-tauri/windows/hooks.nsh"
_h_gates=$(grep -c 'FileExists} "$INSTDIR\\${MAINBINARYNAME}.exe"' "$_hooks") || _h_gates=0
_h_deletes=$(grep -c 'Delete "$INSTDIR\\install.sh"' "$_hooks") || _h_deletes=0
if [ "$_h_gates" = "2" ] && [ "$_h_deletes" = "2" ]; then
    ok "the NSIS hooks only tidy a directory that already holds an Unsloth install"
else
    bad "the NSIS hooks only tidy a directory that already holds an Unsloth install"
fi

# Wire every startup file astral's installer did (~/.profile, bash files, ZDOTDIR zsh, fish).
_ph="$WORK/parity_home"
mkdir -p "$_ph/.local/bin" "$_ph/opt/uv"
: > "$_ph/.bashrc"; : > "$_ph/.bash_profile"; : > "$_ph/.zshrc"
for _pass in 1 2; do
    (
        set +e
        step() { :; }
        HOME="$_ph"; export HOME
        SHELL="/bin/bash"; export SHELL
        unset ZSH_VERSION ZDOTDIR UV_NO_MODIFY_PATH UV_UNMANAGED_INSTALL
        _LOCAL_BIN="$HOME/.local/bin"
        _STUDIO_HOME_REDIRECT="none"
        _UNSLOTH_LOGIN_PATH="/usr/bin:/bin"
        _UNSLOTH_UV_BIN_DIR="$HOME/opt/uv"
        # shellcheck disable=SC1090
        . "$WORK/path_guard.sh"
    ) >/dev/null 2>&1 || true
done
_missing=""
for _f in .profile .bashrc .bash_profile .zshrc .config/fish/conf.d/unsloth.fish; do
    _n=$(grep -c "opt/uv" "$_ph/$_f" 2>/dev/null) || _n=0
    [ "$_n" = "1" ] || _missing="$_missing $_f=$_n"
done
[ -f "$_ph/.bash_login" ] && _missing="$_missing .bash_login=created"
[ -f "$_ph/.zshenv" ] && _missing="$_missing .zshenv=created"
if [ -z "$_missing" ]; then
    ok "the uv PATH entry reaches every startup file astral wired, once each"
else
    bad "the uv PATH entry reaches every startup file astral wired, once each ($_missing)"
fi

_fh="$WORK/fish_entry_home"
mkdir -p "$_fh/.config/fish/conf.d" "$_fh/.local/bin" "$_fh/opt/uv"
printf "# Added by Unsloth installer\nfish_add_path '%s/opt/uv-old'\n" "$_fh" > "$_fh/.config/fish/conf.d/unsloth.fish"
(
    set +e
    step() { :; }
    HOME="$_fh"; export HOME
    SHELL="/bin/bash"; export SHELL
    unset ZSH_VERSION ZDOTDIR UV_NO_MODIFY_PATH UV_UNMANAGED_INSTALL
    _LOCAL_BIN="$HOME/.local/bin"
    _STUDIO_HOME_REDIRECT="none"
    _UNSLOTH_LOGIN_PATH="/usr/bin:/bin"
    _UNSLOTH_UV_BIN_DIR="$HOME/opt/uv"
    # shellcheck disable=SC1090
    . "$WORK/path_guard.sh"
) >/dev/null 2>&1 || true
if grep -qxF "fish_add_path '$_fh/opt/uv'" "$_fh/.config/fish/conf.d/unsloth.fish"; then
    ok "a different fish entry does not suppress this one"
else
    bad "a different fish entry does not suppress this one"
fi

# A profile that merely names the directory does not put it on PATH.
_mh="$WORK/mention_home"
mkdir -p "$_mh/.local/bin"
printf 'UV_CACHE="$HOME/.local/bin"\nexport PYTHONPATH="$HOME/.local/bin"\n' > "$_mh/.profile"
(
    set +e
    step() { :; }
    HOME="$_mh"; export HOME
    SHELL="/bin/bash"; export SHELL
    unset ZSH_VERSION
    _LOCAL_BIN="$HOME/.local/bin"
    _STUDIO_HOME_REDIRECT="none"
    _UNSLOTH_LOGIN_PATH="/usr/bin:/bin"
    # shellcheck disable=SC1090
    . "$WORK/path_guard.sh"
) >/dev/null 2>&1 || true
if grep -q 'export PATH=' "$_mh/.profile"; then
    ok "a non-PATH mention of the directory does not suppress the export"
else
    bad "a non-PATH mention of the directory does not suppress the export"
fi

# A launcher on a UNC share or mapped drive is a remote script that RemoteSigned refuses.
_ps1="$SCRIPT_DIR/../../install.ps1"
if grep -q '\$launcherIsRemote = \$launcherPs1 -like "\\\\\*"' "$_ps1" \
   && grep -q "DriveType -eq 'Network'" "$_ps1" \
   && grep -q '"-NoProfile -ExecutionPolicy Bypass -File `"$launcherPs1`""' "$_ps1"; then
    ok "a UNC or mapped-drive launcher gets a policy that can actually load it"
else
    bad "a UNC or mapped-drive launcher gets a policy that can actually load it"
fi

# The default ~/.local/bin install must also get the all-profile write.
_dh="$WORK/default_home"
mkdir -p "$_dh/.local/bin"
: > "$_dh/.bashrc"; : > "$_dh/.bash_profile"
for _pass in 1 2; do
    (
        set +e
        step() { :; }
        HOME="$_dh"; export HOME
        SHELL="/bin/bash"; export SHELL
        unset ZSH_VERSION ZDOTDIR UV_NO_MODIFY_PATH UV_UNMANAGED_INSTALL
        _LOCAL_BIN="$HOME/.local/bin"
        _STUDIO_HOME_REDIRECT="none"
        _UNSLOTH_LOGIN_PATH="/usr/bin:/bin"
        _UNSLOTH_UV_BIN_DIR="$HOME/.local/bin"
        # shellcheck disable=SC1090
        . "$WORK/path_guard.sh"
    ) >/dev/null 2>&1 || true
done
_dmiss=""
for _f in .profile .bashrc .bash_profile .config/fish/conf.d/unsloth.fish; do
    _n=$(grep -c "local/bin" "$_dh/$_f" 2>/dev/null) || _n=0
    [ "$_n" = "1" ] || _dmiss="$_dmiss $_f=$_n"
done
if [ -z "$_dmiss" ]; then
    ok "a default install wires every startup file, once each"
else
    bad "a default install wires every startup file, once each ($_dmiss)"
fi

# Check the flag, not the return value: Invoke-SetupCommand returns $LASTEXITCODE, so the
# function's $true always read as failed.
if grep -q 'if (-not $script:UvPinnedInstalled -and (Get-Command winget' "$SETUP_PS1" \
   && grep -q '$script:UvPinnedInstalled = $true' "$SETUP_PS1"; then
    ok "setup.ps1 still has a uv fallback when the pinned install fails"
else
    bad "setup.ps1 still has a uv fallback when the pinned install fails"
fi

# A directory named uv must not pass as a binary: `mv f d` moves into it and d passes -x.
mkdir -p "$WORK/home_dir_target/.local/bin/uv"
(
    set +e
    tauri_log() { :; }
    # shellcheck disable=SC1090
    . "$WORK/uvfns.sh"
    _uv_pinned_asset() { echo "uv-fake.tar.gz $FIXTURE_SHA"; }
    download() { cp -f "$WORK/uv-fake.tar.gz" "$2"; }
    HOME="$WORK/home_dir_target"; export HOME
    unset UV_INSTALL_DIR UV_UNMANAGED_INSTALL XDG_BIN_HOME XDG_DATA_HOME
    _uv_install_pinned
    echo "rc=$?"
) > "$WORK/out_dir_target" 2>&1 || true
if grep -q '^rc=0$' "$WORK/out_dir_target"; then
    bad "a directory at the destination declines to the fallback"
else
    ok "a directory at the destination declines to the fallback"
fi

# An ERE metacharacter in the destination must still match itself, or reinstalls append blocks.
_mh="$WORK/meta_home"
mkdir -p "$_mh/.local/bin" "$_mh/opt/a+b(c)"
: > "$_mh/.bashrc"
for _pass in 1 2; do
    (
        set +e
        step() { :; }
        HOME="$_mh"; export HOME
        SHELL="/bin/bash"; export SHELL
        unset ZSH_VERSION ZDOTDIR UV_NO_MODIFY_PATH UV_UNMANAGED_INSTALL
        _LOCAL_BIN="$HOME/.local/bin"
        _STUDIO_HOME_REDIRECT="none"
        _UNSLOTH_LOGIN_PATH="/usr/bin:/bin"
        _UNSLOTH_UV_BIN_DIR="$HOME/opt/a+b(c)"
        # shellcheck disable=SC1090
        . "$WORK/path_guard.sh"
    ) >/dev/null 2>&1 || true
done
_n=$(grep -cF 'a+b(c)' "$_mh/.bashrc") || _n=0
if [ "$_n" = "1" ]; then
    ok "a destination holding regex metacharacters is written once, not once per run"
else
    bad "a destination holding regex metacharacters is written once, not once per run ($_n)"
fi

mkdir -p "$WORK/whl/uv-0.12.1.data/scripts" && cp "$WORK/src/uv-fake-triple/uv" "$WORK/src/uv-fake-triple/uvx" "$WORK/whl/uv-0.12.1.data/scripts/"
(cd "$WORK/whl" && python3 -m zipfile -c "$WORK/uv-fake.whl" uv-0.12.1.data)
WHEEL_SHA=$( (sha256sum "$WORK/uv-fake.whl" 2>/dev/null || shasum -a 256 "$WORK/uv-fake.whl") | awk '{print $1}')
_sa=$(grep -n '^_SETUP_UV_PINNED_VERSION=' "$SETUP_SH" | cut -d: -f1)
_sb=$(awk -v s="$(grep -n '^_setup_install_uv_pinned() {' "$SETUP_SH" | cut -d: -f1)" 'NR > s && /^}$/ { print NR; exit }' "$SETUP_SH")
_pa=$(grep -n '^_SETUP_PROBE_TARGET=' "$SETUP_SH" | cut -d: -f1)
_pb=$(awk -v s="$(grep -n '^_setup_probe_version() {' "$SETUP_SH" | cut -d: -f1)" 'NR > s && /^}$/ { print NR; exit }' "$SETUP_SH")
{ sed -n "${_pa},${_pb}p" "$SETUP_SH"; sed -n "${_sa},${_sb}p" "$SETUP_SH"; } > "$WORK/uvfns_setup.sh"
cp "$WORK/uvfns.sh" "$WORK/uvfns_install.sh"
for _run in install:good install:bad setup:good setup:bad install:good:py setup:good:py; do
    _py=${_run#*:*:}; [ "$_py" != "$_run" ] || _py=""; _run=${_run%:py}
    _wh="$WORK/wheel_${_run%:*}_${_run#*:}${_py:+_py}"; mkdir -p "$_wh"; _want=$WHEEL_SHA; [ "${_run#*:}" = good ] || _want=$(printf '0%.0s' $(seq 64))
    (
        set +e; tauri_log() { :; }
        # shellcheck disable=SC1090
        . "$WORK/uvfns_${_run%:*}.sh"
        tar() { case "$*" in *.whl*) return 1 ;; esac; command tar "$@"; }
        [ -z "$_py" ] || unzip() { return 1; }
        _uv_pinned_asset() { echo "uv-fake.tar.gz $FIXTURE_SHA"; }; _setup_uv_pinned_asset() { _uv_pinned_asset; }
        _uv_pinned_wheel() { echo "packages/ab/cd/uv-fake.whl $_want"; }; _setup_uv_pinned_wheel() { _uv_pinned_wheel; }
        download() { echo "$1" >> "$_wh.get"; cp -f "$WORK/uv-fake.whl" "$2"; }; _setup_http_get() { download "$1" /dev/stdout; }; _setup_persist_uv_path() { :; }
        HOME="$_wh"; UNSLOTH_UV_WHEEL_MIRROR="https://mirror.example/pypi/web/"; export HOME UNSLOTH_UV_WHEEL_MIRROR
        unset UV_INSTALL_DIR UV_UNMANAGED_INSTALL XDG_BIN_HOME XDG_DATA_HOME UV_DOWNLOAD_URL INSTALLER_DOWNLOAD_URL UV_INSTALLER_GHE_BASE_URL UV_INSTALLER_GITHUB_BASE_URL
        if [ "${_run%:*}" = install ]; then _uv_install_pinned; else _setup_install_uv_pinned; fi
    ) > "$_wh.out" 2>&1 || true
    _got=no; [ -x "$_wh/.local/bin/uv" ] && [ -x "$_wh/.local/bin/uvx" ] \
        && [ "$(cat "$_wh.get")" = "https://mirror.example/pypi/web/packages/ab/cd/uv-fake.whl" ] && _got=yes
    _exp=no; [ "${_run#*:}" = good ] && _exp=yes
    assert_eq "$_run${_py:+ (python3 zipfile)}: wheel-mirror install (good digest installs from the mirror only, bad installs nothing)" "$_exp" "$_got"
done
_wheels() { grep -oE 'uv-[0-9.]+-py3-none-[a-z0-9_.]+\.whl [0-9a-f]{64}"' "$1" | sort; }
assert_eq "install.sh and setup.sh pin the same four wheels of the pinned uv" "4 $(_wheels "$INSTALL_SH")" \
    "$(_wheels "$INSTALL_SH" | grep -c "^uv-$(sed -n 's/^UV_PINNED_VERSION="\(.*\)"/\1/p' "$INSTALL_SH")-") $(_wheels "$SETUP_SH")"
_winwheels() { grep -oE 'uv-[0-9.]+-py3-none-win[a-z0-9_]*\.whl"; WheelSha256 = "[0-9A-F]{64}' "$1" | sort; }
assert_eq "install.ps1 and setup.ps1 pin the same three wheels of the pinned uv" "3 $(_winwheels "$SCRIPT_DIR/../../install.ps1")" "$(_winwheels "$SCRIPT_DIR/../../install.ps1" | grep -c "^uv-$(sed -n 's/^UV_PINNED_VERSION="\(.*\)"/\1/p' "$INSTALL_SH")-") $(_winwheels "$SETUP_PS1")"

echo
echo "passed: $PASS  failed: $FAIL"
[ "$FAIL" -eq 0 ]