#!/usr/bin/env bash
set -euo pipefail
export MISE_LOCKFILE_MODE=generate

cat >mise.toml <<'TOML'
[settings]
lockfile_platforms = ["linux-x64", "macos-arm64"]

[tools."http:current-fixture"]
version = "1.0.0"
url = "https://mise.jdx.dev/test-fixtures/hello-world-1.0.0.tar.gz"
checksum = "sha256:dbca4f08377d70dc0828f5822fc47ecec3895cd9a6dacbc54cc984d346a571af"
bin_path = "hello-world-1.0.0/bin"
TOML

mise lock
mise install

# A marker makes unnecessary serialization observable: the regular writer
# canonicalizes TOML and removes it. Warm installs should preserve the file.
echo '# warm no-op marker' >>mise.lock
cp mise.lock expected.lock
mise install
diff -u expected.lock mise.lock

# Explicit lock commands and forced verification still use full generation.
mise lock
assert_not_contains "cat mise.lock" 'warm no-op marker'
echo '# warm no-op marker' >>mise.lock
MISE_LOCKED_VERIFY_PROVENANCE=1 mise install
assert_not_contains "cat mise.lock" 'warm no-op marker'

# Nothing needs installing, but a new target still needs lock metadata.
python3 - <<'PY'
from pathlib import Path
p = Path("mise.toml")
p.write_text(p.read_text().replace('"linux-x64", "macos-arm64"', '"linux-x64", "macos-arm64", "linux-arm64"'))
PY
mise install
assert_contains "cat mise.lock" 'platforms.linux-arm64'

# Stale entries must still be removed on a warm install.
cat >>mise.lock <<'TOML'

[[tools.obsolete]]
version = "1.0.0"
backend = "http:obsolete"
TOML
mise install
assert_not_contains "cat mise.lock" 'tools.obsolete'
