#!/usr/bin/env bash

mise use dummy@latest

eval "$(mise activate bash)" && _mise_hook
assert "PATH="" $(which mise) doctor --json"

# Test PATH ordering warning when non-mise paths precede mise tool paths
assert_not_contains "mise doctor" "mise tool paths are not first in PATH"
export PATH="/tmp/fake-system-bin:$PATH"
assert_contains "mise doctor" "mise tool paths are not first in PATH"
assert_contains "mise doctor" "/tmp/fake-system-bin"
# Restore PATH for remaining tests
export PATH="${PATH#/tmp/fake-system-bin:}"

mise p add uv
mise use uv
assert_contains "mise doctor" "asdf:uv@"
assert_contains "mise doctor" "self_update_available"
assert_contains "mise doctor -J" "dummy"
assert_contains "mise doctor -J" "self_update_available"
assert_contains "mise doctor -J" "aqua"
assert_contains "mise doctor" "aqua"

# env_files section is present in both text and JSON output
assert_contains "mise doctor" "env_files"
assert_contains "mise doctor -J" "env_files"

# when no env files are configured, text output shows (none)
assert_contains "mise doctor" "(none)"

# when an env file is configured, it appears in doctor output
echo "DOCTOR_VAR=1" >.doctor-test-env
assert_contains "MISE_ENV_FILE=.doctor-test-env mise doctor" ".doctor-test-env"
assert_contains "MISE_ENV_FILE=.doctor-test-env mise doctor -J" ".doctor-test-env"

# Both outputs render the same warnings, so a check that only one path runs is a hole in the other.
# Seeding the version cache makes this deterministic: no network, and no waiting for a release.
# Asked of mise rather than assembled here, so the file is written wherever the version check
# will look for it.
# Kept last — the seeded version adds a warning to every doctor run after this point.
cache_dir="$(mise cache path)"
mkdir -p "$cache_dir"
echo "2099.1.0" >"$cache_dir/latest-version"
assert_contains "mise doctor" "new mise version 2099.1.0 available"
# Structure rather than substring on the JSON side: what this PR is about is the warning reaching
# `warnings`, not the text appearing somewhere in the document. `pipefail` because the helper runs
# this through its own `bash -c`, where the pipeline's status is jq's alone — a `mise doctor` that
# printed the right JSON and then exited non-zero would pass unnoticed.
assert_succeed "set -o pipefail; mise doctor -J | jq -e '.warnings | map(contains(\"new mise version 2099.1.0 available\")) | any'"
