#!/usr/bin/env bash

# Lock options are rendered tool options, and mise.lock is meant to be
# committed, so a token that mise.toml templates into one from the environment
# must not be written to it.

export MISE_LOCKFILE=1
export MISE_OFFLINE=1
export INDEX_TOKEN=idx_lockopt_s3cret

cat <<'EOF' >mise.toml
[tools."pypi:my-tool"]
version = "1.0.0"
uvx = false
pipx_args = "--pip-args='--index-url https://deploy:{{ env.INDEX_TOKEN }}@packages.example.com/pypi/simple'"
EOF

touch mise.lock
mise lock
assert_not_contains "cat mise.lock" "idx_lockopt_s3cret"
assert_contains "cat mise.lock" "https://packages.example.com/pypi/simple"

# An entry an older mise wrote with the token still matches, and is rewritten
# without it.
cat <<EOF >mise.lock
[[tools."pypi:my-tool"]]
version = "1.0.0"
backend = "pypi:my-tool"

[tools."pypi:my-tool".options]
pipx_args = "--pip-args='--index-url https://deploy:$INDEX_TOKEN@packages.example.com/pypi/simple'"
uvx = "false"
EOF
mise lock
assert_not_contains "cat mise.lock" "idx_lockopt_s3cret"
assert "grep -c '^version = ' mise.lock" "1"
