#!/usr/bin/env bash

# Test --locked mode enforcement during install and dry-run

export MISE_LOCKFILE=1

detect_platform
PLATFORM="$MISE_PLATFORM"

# Use jq via aqua backend, which supports lockfile URLs
cat <<'EOF' >mise.toml
[tools]
jq = "1.7.1"
EOF

# Uninstall jq so it's not already present
mise uninstall jq@1.7.1 2>/dev/null || true

# --- Test 1: --locked fails when no lockfile URL exists ---
cat <<EOF >mise.lock
[[tools.jq]]
version = "1.7.1"
backend = "aqua:jqlang/jq"
EOF

assert_fail_contains "mise install --locked 2>&1" "No lockfile URL found"

# --- Test 2: --locked --dry-run also fails when no lockfile URL exists ---
# This is the key behavior: locked validation must run before dry-run short-circuit
assert_fail_contains "mise install --locked --dry-run 2>&1" "No lockfile URL found"

# --- Test 3: --locked --dry-run succeeds when lockfile URL exists ---
cat <<EOF >mise.lock
[[tools.jq]]
version = "1.7.1"
backend = "aqua:jqlang/jq"
"platforms.$PLATFORM" = { url = "https://example.com/jq-1.7.1.tar.gz" }
EOF

assert_contains "mise install --locked --dry-run 2>&1" "would install"

# --- Test 4: locked=true with MISE_LOCKFILE=false errors (contradictory config) ---
cat <<EOF >mise.lock
[[tools.jq]]
version = "1.7.1"
backend = "aqua:jqlang/jq"
"platforms.$PLATFORM" = { url = "https://example.com/jq-1.7.1.tar.gz" }
EOF

MISE_LOCKFILE=0 assert_fail_contains "mise install --locked --dry-run 2>&1" "locked mode requires lockfile to be enabled"

# --- Test 5: locked setting via config with lockfile=false errors ---
# Unset MISE_LOCKFILE so the env var doesn't override the config file setting
unset MISE_LOCKFILE

cat <<'EOF' >mise.toml
[tools]
jq = "1.7.1"
[settings]
locked = true
lockfile = false
EOF

assert_fail_contains "mise install --dry-run 2>&1" "locked mode requires lockfile to be enabled"

# --- Test 6: locked=true with lockfile unset (not in config) works fine ---
cat <<'EOF' >mise.toml
[tools]
jq = "1.7.1"
[settings]
locked = true
EOF

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

assert_contains "mise install --dry-run 2>&1" "would install"

# --- Test 7: mise lock can refresh lockfiles in locked mode ---
export MISE_LOCKFILE=1

cat <<'EOF' >mise.toml
[tools]
jq = "1.7.1"
EOF

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

mise lock --locked --platform "$PLATFORM"
assert_contains "cat mise.lock" "1.7.1"
MISE_LOCKED=1 mise lock --platform "$PLATFORM"
assert_contains "cat mise.lock" "1.7.1"

# --- Test 8: links from another backend do not suppress lock data ---
# An alias can share an install directory with another backend (for example, core rust).
# External symlinks in that directory must not prevent the aliased HTTP tool from loading
# its platform URL from the lockfile.
mkdir -p "$PWD/linked-rust" "$MISE_DATA_DIR/installs/rust"
ln -s "$PWD/linked-rust" "$MISE_DATA_DIR/installs/rust/external"
cat <<'EOF' >"$MISE_DATA_DIR/installs/rust/.mise.backend.toml"
short = "rust"
full = "core:rust"
explicit_backend = true
EOF

cat <<EOF >mise.toml
[tools.rust]
version = "1.90.0"

[tools.rust.platforms.$PLATFORM]
url = "https://example.com/rust-1.90.0.tar.gz"

[tool_alias]
rust = "http:rust"
EOF

cat <<EOF >mise.lock
[[tools.rust]]
version = "1.90.0"
backend = "http:rust"
"platforms.$PLATFORM" = { url = "https://example.com/rust-1.90.0.tar.gz" }
EOF

assert_contains "mise install --locked --dry-run rust 2>&1" "would install"

# Restore for any subsequent tests
export MISE_LOCKFILE=1
