#!/usr/bin/env bash

# A dotfiles checkout linked into the global config dir keeps the lockfile
# beside its mise.toml, and that lockfile keeps its download URLs: the user
# made the checkout global config. A project mise.toml linked the other way, to
# the global config, still does not vouch for the project's lockfile, and
# global config does not vouch for a monorepo's shared lockfile.
#
# `mise install` trusts the config it runs in, so the arms that need an
# untrusted project run in safe mode, which trusts nothing implicitly.

export MISE_LOCKFILE=1
export MISE_OFFLINE=1
export MISE_TRUSTED_CONFIG_PATHS=""
unset CI GITHUB_ACTIONS GITHUB_ACTION 2>/dev/null || true

detect_platform
PLATFORM="$MISE_PLATFORM"

write_lock() {
  cat >"$1/mise.lock" <<EOF
[[tools.jq]]
version = "1.7.1"
backend = "aqua:jqlang/jq"
"platforms.$PLATFORM" = { url = "https://example.com/jq-1.7.1.tar.gz" }
EOF
}

mkdir -p dotfiles "$MISE_CONFIG_DIR/conf.d"
cat >dotfiles/mise.toml <<'EOF'
[tools]
jq = "1.7.1"
EOF
write_lock dotfiles
dotfiles="$(cd dotfiles && pwd -P)"

install="install jq --locked --dry-run"
assert_fail_contains "MISE_SAFE=1 mise -C dotfiles $install 2>&1" "No lockfile URL found"
mise trust dotfiles/mise.toml
assert_contains "mise -C dotfiles $install 2>&1" "would install"

# Linking the checkout into conf.d must not cost its lockfile the URLs.
ln -s "$dotfiles" "$MISE_CONFIG_DIR/conf.d/dotfiles"
assert_contains "mise -C dotfiles $install 2>&1" "would install"

# The link is the user's own consent, so trust is not needed on top of it.
mise trust --untrust dotfiles/mise.toml
assert_contains "MISE_SAFE=1 mise -C dotfiles $install 2>&1" "would install"

# A global config file linked straight to the checkout behaves the same.
rm "$MISE_CONFIG_DIR/conf.d/dotfiles"
ln -s "$dotfiles/mise.toml" "$MISE_CONFIG_DIR/config.toml"
assert_contains "MISE_SAFE=1 mise -C dotfiles $install 2>&1" "would install"
rm "$MISE_CONFIG_DIR/config.toml"

# A checkout inside an untrusted monorepo with a shared lockfile keeps only its
# own lockfile: being global config does not vouch for the monorepo's.
mkdir -p mono/dotfiles
cat >mono/mise.toml <<'EOF'
monorepo_root = true

[monorepo]
lockfile = true
config_roots = ["dotfiles"]
EOF
cp dotfiles/mise.toml mono/dotfiles/mise.toml
write_lock mono
ln -s "$(cd mono/dotfiles && pwd -P)" "$MISE_CONFIG_DIR/conf.d/dotfiles"
assert_fail_contains "MISE_EXPERIMENTAL=1 MISE_SAFE=1 mise -C mono/dotfiles $install 2>&1" "No lockfile URL found"
rm "$MISE_CONFIG_DIR/conf.d/dotfiles"

# An untrusted project cannot borrow global trust by linking its mise.toml to
# the global config: that config keeps the global lockfile, which has no URL.
cat >"$MISE_CONFIG_DIR/config.toml" <<'EOF'
[tools]
jq = "1.7.1"
EOF
mkdir proj
ln -s "$MISE_CONFIG_DIR/config.toml" proj/mise.toml
write_lock proj
assert_fail "MISE_SAFE=1 mise -C proj $install 2>&1"
