#!/usr/bin/env bash
# Test --before flag for date-based version filtering

set -euo pipefail

# Clean up any existing installations
mise uninstall tiny --all 2>/dev/null || true
rm -f mise.toml .tool-versions

# Test: install --before with dry-run should show the tool
output=$(mise install tiny@latest --before 2020-01-01 --dry-run 2>&1)
assert_contains "echo '$output'" "tiny"

# Test: install --before should install a version
mise install tiny@latest --before 2025-01-01
assert_contains "mise ls --installed tiny" "3.1.0"

# Test: use --before with dry-run
mise uninstall tiny --all 2>/dev/null || true
output=$(mise use tiny@latest --before 2025-01-01 --dry-run 2>&1)
assert_contains "echo '$output'" "tiny"
rm -f mise.toml

# Test: MISE_INSTALL_BEFORE environment variable works
mise uninstall tiny --all 2>/dev/null || true
export MISE_INSTALL_BEFORE="2025-01-01"
mise install tiny@latest
assert_contains "mise ls --installed tiny" "3.1.0"
unset MISE_INSTALL_BEFORE

# Test: upgrade --before with dry-run
cat <<EOF >mise.toml
[tools]
tiny = "1"
EOF
mise uninstall tiny --all 2>/dev/null || true
mise install tiny@1.0.0
output=$(mise upgrade --before 2025-01-01 --dry-run 2>&1)
assert_contains "echo '$output'" "tiny"

# Test: install --before with relative duration "90d"
mise uninstall tiny --all 2>/dev/null || true
output=$(mise install tiny@latest --before 90d --dry-run 2>&1)
assert_contains "echo '$output'" "tiny"

# Test: install --before with relative duration "1y"
output=$(mise install tiny@latest --before 1y --dry-run 2>&1)
assert_contains "echo '$output'" "tiny"

# Test: install --before with relative duration "6m"
output=$(mise install tiny@latest --before 6m --dry-run 2>&1)
assert_contains "echo '$output'" "tiny"

# Test: MISE_INSTALL_BEFORE with relative duration
mise uninstall tiny --all 2>/dev/null || true
export MISE_INSTALL_BEFORE="90d"
mise install tiny@latest
assert_contains "mise ls --installed tiny" "3.1.0"
unset MISE_INSTALL_BEFORE

# Test: per-tool install_before is newer than global (per-tool wins)
# global=2019-01-01 would give jq 1.6, per-tool=2024-01-01 gives jq 1.7.1
mise uninstall jq --all 2>/dev/null || true
rm -f mise.toml
cat <<EOF >mise.toml
[settings]
install_before = "2019-01-01"

[tools.jq]
version = "latest"
install_before = "2024-01-01"
EOF
mise install
assert_contains "mise ls --installed jq" "1.7.1"

# Test: per-tool install_before is older than global (per-tool wins)
# global=2024-01-01 would give jq 1.7.1, per-tool=2019-01-01 gives jq 1.6
mise uninstall jq --all 2>/dev/null || true
rm -f mise.toml
cat <<EOF >mise.toml
[settings]
install_before = "2024-01-01"

[tools.jq]
version = "latest"
install_before = "2019-01-01"
EOF
mise install
assert_contains "mise ls --installed jq" "1.6"

# Test: CLI --before flag overrides per-tool install_before
# per-tool=2024-01-01 would give jq 1.7.1, CLI --before=2019-01-01 gives jq 1.6
mise uninstall jq --all 2>/dev/null || true
rm -f mise.toml
cat <<EOF >mise.toml
[tools.jq]
version = "latest"
install_before = "2024-01-01"
EOF
mise install --before 2019-01-01
assert_contains "mise ls --installed jq" "1.6"
