#!/usr/bin/env bash

# Test that os field supports os/arch compound syntax for filtering tools.

# Tool with a non-matching os/arch should be skipped (not installed).
# "windows/arm64" won't match any CI runner or dev machine running this test.
cat >mise.toml <<'EOF'
[tools]
tiny = { version = "latest", os = ["windows/arm64"] }
EOF
mise i
assert_fail_contains "mise ls --installed tiny" "tiny is not installed"

# Tool matching just the OS (no arch) should still work as before.
cat >mise.toml <<'EOF'
[tools]
tiny = { version = "latest", os = ["macos", "linux"] }
EOF
mise i
assert_contains "mise ls --installed tiny" "tiny"
mise uninstall --all tiny

# Tool with matching os/arch should install on this platform.
# Detect current platform to construct a matching entry.
case "$(uname -s)" in
Darwin) _os="macos" ;;
Linux) _os="linux" ;;
*) _os="windows" ;;
esac
case "$(uname -m)" in
x86_64) _arch="x64" ;;
aarch64 | arm64) _arch="arm64" ;;
*) _arch="$(uname -m)" ;;
esac

cat >mise.toml <<EOF
[tools]
tiny = { version = "latest", os = ["${_os}/${_arch}"] }
EOF
mise i
assert_contains "mise ls --installed tiny" "tiny"
mise uninstall --all tiny

# Tool with a mix: matching os (any arch) + non-matching os/arch should install.
cat >mise.toml <<EOF
[tools]
tiny = { version = "latest", os = ["${_os}", "windows/arm64"] }
EOF
mise i
assert_contains "mise ls --installed tiny" "tiny"
mise uninstall --all tiny

# darwin alias should work in compound syntax.
if [ "$_os" = "macos" ]; then
	cat >mise.toml <<EOF
[tools]
tiny = { version = "latest", os = ["darwin/${_arch}"] }
EOF
	mise i
	assert_contains "mise ls --installed tiny" "tiny"
	mise uninstall --all tiny
fi
