#!/usr/bin/env bats
# antidote install tests

load helpers/common

setup() { antidote_common_setup; }

@test "install requires a bundle argument" {
  fixture_session <<'EOS'
antidote install 2>&1
EOS
  assert_failure 1
  assert_output "antidote: error: required argument 'bundle' not provided, try --help"
}

@test "installing an existing bundle fails" {
  fixture_session <<'EOS'
antidote install foo/bar &>/dev/null; echo "exit: $?"
antidote install foo/bar 2>&1 | subenv HOME
EOS
  assert_line --index 0 "exit: 1"
  assert_line --index 1 'antidote: error: foo/bar already installed: $HOME/.cache/antidote/fakegitsite.com/foo/bar'
}

# The clone failure emits git error details with variable temp paths,
# so assert only the stable lines.
@test "installing a non-existent bundle fails" {
  fixture_session <<'EOS'
antidote install does-not/exist &>/dev/null; echo "exit: $?"
antidote install does-not/exist 2>&1 >/dev/null
EOS
  assert_line "exit: 1"
  assert_line "# antidote: unable to install bundle 'does-not/exist'."
}

# An unrecognized short flag must error out, not silently append a
# malformed ':value' annotation to the plugins file.
@test "install rejects unknown flags" {
  fixture_session <<'EOS'
antidote install -x foo themes/purify 2>&1; echo "exit: $?"
print -r -- "purify lines: $(grep -c purify $ZDOTDIR/.zsh_plugins.txt)"
EOS
  assert_line "antidote: error: unknown flag '-x', try --help"
  assert_line "exit: 1"
  assert_line "purify lines: 0"
}

# Long flags get the same treatment: no silently inventing a 'bogus:x'
# annotation in the plugins file.
@test "install rejects unknown long flags" {
  fixture_session <<'EOS'
antidote install --bogus x themes/purify 2>&1; echo "exit: $?"
print -r -- "purify lines: $(grep -c purify $ZDOTDIR/.zsh_plugins.txt)"
EOS
  assert_line "antidote: error: unknown flag '--bogus', try --help"
  assert_line "exit: 1"
  assert_line "purify lines: 0"
}

@test "install clones and appends to the plugins file" {
  fixture_session <<'EOS'
antidote install themes/purify | subenv ZDOTDIR
tail -n 1 $ZDOTDIR/.zsh_plugins.txt
EOS
  expected=$(cat <<'EOF'
# antidote cloning themes/purify...
Adding bundle to '$ZDOTDIR/.zsh_plugins.txt':
themes/purify
themes/purify
EOF
)
  expect "$expected"
}

@test "install with --kind and --conditional" {
  fixture_session <<'EOS'
antidote install --kind fpath --conditional is-macos themes/ohmytheme | subenv ZDOTDIR
tail -n 1 $ZDOTDIR/.zsh_plugins.txt
EOS
  expected=$(cat <<'EOF'
# antidote cloning themes/ohmytheme...
Adding bundle to '$ZDOTDIR/.zsh_plugins.txt':
themes/ohmytheme kind:fpath conditional:is-macos
themes/ohmytheme kind:fpath conditional:is-macos
EOF
)
  expect "$expected"
}

# --branch is the one whitelist entry the other flag tests never exercise,
# so a typo in it would only surface for a user.
@test "install with --branch" {
  fixture_session <<'EOS'
antidote install --branch main test/install | subenv ZDOTDIR
tail -n 1 $ZDOTDIR/.zsh_plugins.txt
EOS
  assert_line "test/install branch:main"
}

@test "install with all annotation flags" {
  fixture_session <<'EOS'
antidote install --path lib --autoload functions --pre setup_func --post teardown_func --pin 367eaa595eb776634c100cec24f241cc2256e79e test/install | subenv ZDOTDIR
tail -n 1 $ZDOTDIR/.zsh_plugins.txt
EOS
  expected=$(cat <<'EOF'
# antidote cloning test/install...
Adding bundle to '$ZDOTDIR/.zsh_plugins.txt':
test/install path:lib autoload:functions pre:setup_func post:teardown_func pin:367eaa595eb776634c100cec24f241cc2256e79e
test/install path:lib autoload:functions pre:setup_func post:teardown_func pin:367eaa595eb776634c100cec24f241cc2256e79e
EOF
)
  expect "$expected"
}
