#!/usr/bin/env bash
# shellcheck disable=SC2016
# A common Git ancestor is sufficient even without a per-machine sync baseline.
require_cmd git
export MISE_HISTORY_NOTIFY=false
origin="$PWD/ordinary-origin.git"
git init -q --bare -b main "$origin"
echo base >~/.native
echo base >~/.other
echo base >~/.third
assert_succeed "mise bootstrap dotfiles track ~/.native ~/.other ~/.third"
assert_succeed "mise bootstrap dotfiles origin set file://$origin --sync manual --yes"

second="$(dirname "$HOME")/second"
mkdir -p "$second/.config/mise" "$second/.local/state/mise/history"
git clone -q --bare "$origin" "$second/.local/state/mise/history/repo.git"
echo base >"$second/.native"
echo base >"$second/.other"
echo base >"$second/.third"
cat >"$second/.config/mise/config.toml" <<'TOML'
[dotfiles]
"~/.native" = { mode = "track" }
"~/.other" = { mode = "track" }
"~/.third" = { mode = "track" }
TOML
b() {
  (
    cd "$second" || exit
    env HOME="$second" XDG_CONFIG_HOME="$second/.config" MISE_CONFIG_DIR="$second/.config/mise" \
      MISE_STATE_DIR="$second/.local/state/mise" MISE_DATA_DIR="$second/.local/share/mise" \
      MISE_CACHE_DIR="$second/.cache/mise" MISE_TRUSTED_CONFIG_PATHS="$second" mise "$@"
  )
}
export second
export -f b
assert_succeed "b bootstrap dotfiles origin set file://$origin --sync manual --yes"
state="$second/.local/state/mise/history/repo.git/mise-sync-state.json"
if [[ -f $state ]]; then
  mv "$state" "$state.saved-for-test"
fi
echo incoming >~/.native
assert_succeed "mise bootstrap dotfiles save"
assert_succeed "mise bootstrap dotfiles sync"
incoming=$(git --git-dir="$origin" rev-parse main)
assert_succeed "b bootstrap dotfiles sync --fetch-only"
assert_succeed "b bootstrap dotfiles pull --yes"
assert "cat $second/.native" "incoming"
assert_succeed "b bootstrap dotfiles sync"
assert_succeed "git --git-dir=$origin merge-base --is-ancestor $incoming main"

# Two unresolved files hold unrelated incoming changes until all choices exist.
assert_succeed "mise bootstrap dotfiles sync"
echo first-machine >~/.native
echo first-machine >~/.third
echo incoming-other >~/.other
echo second-machine >"$second/.native"
echo second-machine >"$second/.third"
echo newly-enrolled >~/.fourth
assert_succeed "mise bootstrap dotfiles track ~/.fourth"
assert_succeed "mise bootstrap dotfiles save"
assert_succeed "mise bootstrap dotfiles sync"
first_head=$(git --git-dir="$origin" rev-parse main)
assert_succeed "b bootstrap dotfiles save"
assert_succeed "b bootstrap dotfiles sync"
assert "git --git-dir=$origin rev-parse main" "$first_head"
assert "cat $second/.other" "base"
assert_fail "test -e $second/.fourth"
assert_succeed "b bootstrap dotfiles pull --keep-local $second/.native --yes"
assert "cat $second/.other" "base"
assert_fail "test -e $second/.fourth"
assert "git --git-dir=$origin rev-parse main" "$first_head"
echo newer-second-machine >"$second/.native"
assert_succeed "b bootstrap dotfiles save"
assert_succeed "b bootstrap dotfiles pull --keep-local $second/.third --yes"
assert "cat $second/.other" "base"
assert_fail "test -e $second/.fourth"
assert_succeed "b bootstrap dotfiles pull --keep-local $second/.native --yes"
assert "cat $second/.other" "incoming-other"
assert "cat $second/.fourth" "newly-enrolled"
assert_succeed "b bootstrap dotfiles sync"
assert "git --git-dir=$origin show main:home/.native" "newer-second-machine"
assert "git --git-dir=$origin show main:home/.third" "second-machine"
assert "git --git-dir=$origin show main:home/.other" "incoming-other"
assert_succeed "git --git-dir=$origin merge-base --is-ancestor $first_head main"
