#!/usr/bin/env bash

setup_file(){
  load "$PWD/tests/integration/helpers/common"
  _common_setup
  _add_server
  _new_app
}

teardown_file(){
  _rm_app
  _rm_server
  _reset_recipe
}

setup(){
  load "$PWD/tests/integration/helpers/common"
  _common_setup
  _ensure_catalogue
}

teardown(){
  _reset_recipe
  _undeploy_app
  _undeploy_app2 "gitea.$TEST_SERVER"
  _undeploy_app2 "zammad.$TEST_SERVER"

  _reset_app
  _reset_tags

  run rm -rf "$ABRA_DIR/recipes/$TEST_RECIPE/foo"
  assert_not_exists "$ABRA_DIR/recipes/$TEST_RECIPE/foo"

  if [[ -d "$ABRA_DIR/recipes/foo" ]]; then
    run rm -rf "$ABRA_DIR/recipes/foo"
    assert_not_exists "$ABRA_DIR/recipes/foo"
  fi

  # NOTE(d1): give some extra space for the pure chaos that we are unleashing
  #           on the CI machine with these deploy tests. the hope is to prevent
  #           lock-ups and network failures which are common in flaky swarm
  #           mode
  sleep 1
}

@test "validate app argument" {
  run $ABRA app deploy
  assert_failure

  run $ABRA app deploy DOESNTEXIST
  assert_failure
}

@test "bail if unstaged changes and no --chaos" {
  run bash -c "echo foo >> $ABRA_DIR/recipes/$TEST_RECIPE/foo"
  assert_success
  assert_exists "$ABRA_DIR/recipes/$TEST_RECIPE/foo"

  run git -C "$ABRA_DIR/recipes/$TEST_RECIPE" status
  assert_success
  assert_output --partial 'foo'

  assert_exists "$ABRA_DIR/recipes/$TEST_RECIPE/foo"
  assert_equal "$(_git_status)" "?? foo"

  run $ABRA app deploy "$TEST_APP_DOMAIN" --no-input
  assert_failure
  assert_output --partial 'locally unstaged changes'
  refute_output --partial 'chaos'
}

# bats test_tags=slow
@test "do not bail if unstaged changes and --chaos" {
  run bash -c 'echo "unstaged changes" >> "$ABRA_DIR/recipes/$TEST_RECIPE/foo"'
  assert_success
  assert_exists "$ABRA_DIR/recipes/$TEST_RECIPE/foo"

  run git -C "$ABRA_DIR/recipes/$TEST_RECIPE" status
  assert_success
  assert_output --partial 'foo'

  assert_exists "$ABRA_DIR/recipes/$TEST_RECIPE/foo"
  assert_equal "$(_git_status)" "?? foo"

  run $ABRA app deploy "$TEST_APP_DOMAIN" \
    --chaos --no-input --no-converge-checks
  assert_success
}

@test "bail if recipe lint errors and no --chaos" {
  run git -C "$ABRA_DIR/recipes/$TEST_RECIPE" checkout main
  assert_success

  # Break the recipe
  run sed -i '/traefik.enable=.*/d' "$ABRA_DIR/recipes/$TEST_RECIPE/compose.yml"
  assert_success

  # Commit the breakage (so we can test without --chaos)
  _set_git_author

  run git -C "$ABRA_DIR/recipes/$TEST_RECIPE" commit -a -m 'Break recipe'
  assert_success

  # Make a broken release
  run $ABRA recipe release --patch -n "$TEST_RECIPE"
  assert_success

  # Make sure we deploy latest
  _wipe_env_version
  
  run $ABRA app deploy "$TEST_APP_DOMAIN" --no-input
  assert_failure
  assert_output --partial 'failed lint checks'

  run git -C "$ABRA_DIR/recipes/$TEST_RECIPE" reset --hard HEAD~1
  latestRelease=$(_latest_release)
  run git -C "$ABRA_DIR/recipes/$TEST_RECIPE" tag -d "$latestRelease"
}

# bats test_tags=slow
@test "warn on recipe lint errors with --chaos" {
  # Break the recipe
  run sed -i '/traefik.enable=.*/d' "$ABRA_DIR/recipes/$TEST_RECIPE/compose.yml"
  assert_success

  run $ABRA app deploy "$TEST_APP_DOMAIN" --no-input --no-converge-checks --chaos
  assert_success
  assert_output --partial 'failed lint checks'
}

# bats test_tags=slow
@test "ensure recipe up to date if no --offline" {
  wantHash=$(_get_n_hash 3)

  run git -C "$ABRA_DIR/recipes/$TEST_RECIPE" reset --hard HEAD~3
  assert_success

  assert_equal $(_get_current_hash) "$wantHash"

  run $ABRA app deploy "$TEST_APP_DOMAIN" --no-input --no-converge-checks
  assert_success

  assert_equal $(_get_head_hash) $(_get_current_hash)
}

# bats test_tags=slow
@test "ensure recipe not up to date if --offline" {
  _ensure_env_version "0.1.0+1.20.0"
  latestRelease=$(_latest_release)

  run git -C "$ABRA_DIR/recipes/$TEST_RECIPE" tag -d "$latestRelease"
  assert_success

  run $ABRA app deploy "$TEST_APP_DOMAIN" \
    --no-input --no-converge-checks --offline
  assert_success

  run git -C "$ABRA_DIR/recipes/$TEST_RECIPE" tag -l
  refute_output --partial "$latestRelease"
}

# bats test_tags=slow
@test "deploy latest commit if no published versions and no --chaos" {
  _remove_tags
  _wipe_env_version

  # NOTE(d1): need to pass --offline to stop tags being pulled again
  run $ABRA app deploy "$TEST_APP_DOMAIN" \
    --no-input --no-converge-checks --offline
  assert_success
  assert_output --partial "${_get_head_hash:0:8}"
}

# bats test_tags=slow
@test "ensure same commit if --chaos" {
  wantHash=$(_get_n_hash 3)

  run git -C "$ABRA_DIR/recipes/$TEST_RECIPE" reset --hard HEAD~3
  assert_success

  assert_equal $(_get_current_hash) "$wantHash"

  _wipe_env_version

  run $ABRA app deploy "$TEST_APP_DOMAIN" \
    --no-input --no-converge-checks --chaos
  assert_success
  assert_output --partial "${wantHash:0:8}"
}

# bats test_tags=slow
@test "do not bail if env version is a hash but no --chaos" {
  wantHash=$(_get_n_hash 3)

  run git -C "$ABRA_DIR/recipes/$TEST_RECIPE" reset --hard HEAD~3
  assert_success

  assert_equal $(_get_current_hash) "$wantHash"

  _ensure_env_version "$wantHash"

  run $ABRA app deploy "$TEST_APP_DOMAIN" \
    --no-input --no-converge-checks
  assert_success
  assert_output --partial "appears to be a chaos commit"
}

# bats test_tags=slow
@test "retrieve recipe if missing" {
  run rm -rf "$ABRA_DIR/recipes/$TEST_RECIPE"
  assert_success
  assert_not_exists "$ABRA_DIR/recipes/$TEST_RECIPE"

  run $ABRA app deploy "$TEST_APP_DOMAIN" --no-input --no-converge-checks
  assert_success

  assert_exists "$ABRA_DIR/recipes/$TEST_RECIPE"
}

# bats test_tags=slow
@test "error if already deployed and no --force/--chaos" {
  _deploy_app

  run $ABRA app deploy "$TEST_APP_DOMAIN" \
    --no-input --no-converge-checks
  assert_failure
  assert_output --partial 'already deployed'
}

@test "no re-deploy after chaos deploy without --force/--chaos" {
  run $ABRA app deploy "$TEST_APP_DOMAIN" \
    --no-input --no-converge-checks --chaos
  assert_success

  run $ABRA app deploy "$TEST_APP_DOMAIN" \
    --no-input --no-converge-checks
  assert_failure

  run $ABRA app deploy "$TEST_APP_DOMAIN" \
    --no-input --no-converge-checks --chaos
  assert_success

  run $ABRA app deploy "$TEST_APP_DOMAIN" \
    --no-input --no-converge-checks --force
  assert_success
}

@test "no re-deploy without --force" {
  _deploy_app

  run $ABRA app deploy "$TEST_APP_DOMAIN" \
    --no-input --no-converge-checks
  assert_failure
}

# bats test_tags=slow
@test "re-deploy deployed app if --force/--chaos" {
  _deploy_app

  run $ABRA app deploy "$TEST_APP_DOMAIN" \
    --no-input --no-converge-checks --force
  assert_success

  run $ABRA app deploy "$TEST_APP_DOMAIN" \
    --no-input --no-converge-checks --chaos
  assert_success
  assert_output --regexp "NEW DEPLOYMENT.*${_get_head_hash:0:8}"
}

# bats test_tags=slow
@test "deploy latest version from catalogue if no --chaos" {
  latestVersion=$(jq -r '.gitea.versions[-1] | keys[0]' < "$ABRA_DIR/catalogue/recipes.json")
  refute [ -z "$latestVersion" ];

  run $ABRA app new gitea \
    --no-input \
    --server "$TEST_SERVER" \
    --domain "gitea.$TEST_SERVER" \
    --secrets
  assert_success
  assert_exists "$ABRA_DIR/servers/$TEST_SERVER/gitea.$TEST_SERVER.env"

  run $ABRA app deploy "gitea.$TEST_SERVER" --no-input --no-converge-checks
  assert_success
  assert_output --partial "$latestVersion"
}

# bats test_tags=slow
@test "skip domain check if missing DOMAIN=" {
  run cp "$ABRA_DIR/servers/$TEST_SERVER/$TEST_APP_DOMAIN.env" "$BATS_TMPDIR/$TEST_APP_DOMAIN.env"
  assert_success
  assert_exists "$BATS_TMPDIR/$TEST_APP_DOMAIN.env"

  run grep -q "DOMAIN=" "$ABRA_DIR/servers/$TEST_SERVER/$TEST_APP_DOMAIN.env"
  assert_success

  run sed -i '/DOMAIN=.*/d' "$ABRA_DIR/servers/$TEST_SERVER/$TEST_APP_DOMAIN.env"
  assert_success

  run grep -q "DOMAIN=" "$ABRA_DIR/servers/$TEST_SERVER/$TEST_APP_DOMAIN.env"
  assert_failure

  run $ABRA app deploy "$TEST_APP_DOMAIN" --no-input --no-converge-checks
  assert_success

  run $ABRA app undeploy "$TEST_APP_DOMAIN" --no-input
  assert_success

  run mv "$BATS_TMPDIR/$TEST_APP_DOMAIN.env" "$ABRA_DIR/servers/$TEST_SERVER/$TEST_APP_DOMAIN.env"
  assert_success
}

# bats test_tags=dns
@test "ensure domain is checked" {
  if [[ "$TEST_SERVER" == "default" ]]; then
      skip "domain checks are disabled for local server"
  fi

  appDomain="custom-html.DOESNTEXIST"

  run $ABRA app new custom-html \
    --no-input \
    --server "$TEST_SERVER" \
    --domain "$appDomain"
  assert_success
  assert_exists "$ABRA_DIR/servers/$TEST_SERVER/$appDomain.env"

  run $ABRA app deploy "$appDomain" --no-input
  assert_failure
  assert_output --partial 'no such host'
}

# bats test_tags=slow
@test "skip domain check when requested" {
  run $ABRA app deploy "$TEST_APP_DOMAIN" \
    --no-input --no-converge-checks --no-domain-checks
  assert_success
}

@test "error if specific version does not exist" {
  run $ABRA app deploy "$TEST_APP_DOMAIN" DOESNTEXIST --no-input --no-converge-checks
  assert_failure
  assert_output --partial "reference not found"
}

# bats test_tags=slow
@test "deploy specific version" {
  run $ABRA app deploy "$TEST_APP_DOMAIN" "0.2.0+1.21.0" --no-input
  assert_success
  assert_output --partial "0.2.0+1.21.0"
}

@test "bail out if specific version and chaos" {
  run $ABRA app deploy "$TEST_APP_DOMAIN" "0.2.0+1.21.0" \
    --chaos --no-input --no-converge-checks
  assert_failure
  assert_output --partial 'cannot use'
}

# bats test_tags=slow
@test "COMPOSE_FILE with \$COMPOSE_FILE override works" {
  run sed -i 's/#COMPOSE_FILE="$COMPOSE_FILE:compose.extra_env.yml"/COMPOSE_FILE="$COMPOSE_FILE:compose.extra_env.yml"/g' \
    "$ABRA_DIR/servers/$TEST_SERVER/$TEST_APP_DOMAIN.env"
  assert_success

  # NOTE(d1): --chaos used to bypass versions and access compose.extra_env.yml
  run $ABRA app deploy "$TEST_APP_DOMAIN" \
    --no-input --no-converge-checks --chaos
  assert_success
  assert_output --partial "compose.yml"
  assert_output --partial "compose.extra_env.yml"
}

@test "error if no secrets generated" {
  run sed -i 's/COMPOSE_FILE="compose.yml"/COMPOSE_FILE="compose.yml:compose.extra_secret.yml"/g' \
    "$ABRA_DIR/servers/$TEST_SERVER/$TEST_APP_DOMAIN.env"
  assert_success

  run sed -i 's/#SECRET_EXTRA_PASS_VERSION=v1/SECRET_EXTRA_PASS_VERSION=v1/g' \
    "$ABRA_DIR/servers/$TEST_SERVER/$TEST_APP_DOMAIN.env"
  assert_success

  run $ABRA app deploy "$TEST_APP_DOMAIN" --no-input --no-converge-checks
  assert_failure
  assert_output --partial "secret not generated"
}

@test "error if secret not inserted" {
  run sed -i 's/COMPOSE_FILE="compose.yml"/COMPOSE_FILE="compose.yml:compose.skip_pass.yml"/g' \
    "$ABRA_DIR/servers/$TEST_SERVER/$TEST_APP_DOMAIN.env"
  assert_success

  run sed -i 's/#SECRET_TEST_SKIP_PASS_VERSION=v1/SECRET_TEST_SKIP_PASS_VERSION=v1/g' \
    "$ABRA_DIR/servers/$TEST_SERVER/$TEST_APP_DOMAIN.env"
  assert_success

  run $ABRA app deploy "$TEST_APP_DOMAIN" --no-input --no-converge-checks
  assert_failure
  assert_output --partial "secret not inserted"
}

# bats test_tags=slow
@test "recipe config comments not present in values" {
  run $ABRA app deploy "$TEST_APP_DOMAIN" --no-input
  assert_success

  run $ABRA app run "$TEST_APP_DOMAIN" app env
  assert_success
  refute_output --partial 'should be removed'
}

# bats test_tags=slow
@test "deploy specific version with incompatible HEAD" {
  run sed -i 's/COMPOSE_FILE="compose.yml"/COMPOSE_FILE="compose.yml:compose.extra_secret.yml"/g' \
    "$ABRA_DIR/servers/$TEST_SERVER/$TEST_APP_DOMAIN.env"
  assert_success

  run sed -i 's/#SECRET_EXTRA_PASS_VERSION=v1/SECRET_EXTRA_PASS_VERSION=v1/g' \
    "$ABRA_DIR/servers/$TEST_SERVER/$TEST_APP_DOMAIN.env"
  assert_success

  run $ABRA app secret generate "$TEST_APP_DOMAIN" --all
  assert_success
  assert_output --partial 'extra_pass'

  run rm -rf "$ABRA_DIR/recipes/$TEST_RECIPE/compose.extra_secret.yml"
  assert_not_exists "$ABRA_DIR/recipes/$TEST_RECIPE/compose.extra_secret.yml"

  _git_commit

  # NOTE(d1): 0.1.1+1.20.2 is a previous version which includes compose.extra_secret.yml
  run $ABRA app deploy "$TEST_APP_DOMAIN" "0.1.1+1.20.2" \
    --no-input --no-converge-checks
  assert_success

  _undeploy_app

  run $ABRA app secret rm "$TEST_APP_DOMAIN" --all
  assert_success
}

# bats test_tags=slow
@test "deploy latest from undeployed specific version" {
  latestRelease=$(_latest_release)

  run $ABRA app deploy "$TEST_APP_DOMAIN" "0.1.0+1.20.0" \
    --no-input --no-converge-checks
  assert_success

  _undeploy_app

  run grep -q "TYPE=$TEST_RECIPE:0.1.0+1.20.0" \
    "$ABRA_DIR/servers/$TEST_SERVER/$TEST_APP_DOMAIN.env"
  assert_success

  run $ABRA app deploy "$TEST_APP_DOMAIN" \
    --no-input --no-converge-checks --latest
  assert_success
  assert_output --partial "$latestRelease"

  run grep -q "TYPE=$TEST_RECIPE:$latestRelease" \
    "$ABRA_DIR/servers/$TEST_SERVER/$TEST_APP_DOMAIN.env"
  assert_success
}

@test "--latest is exclusive" {
  run $ABRA app deploy "$TEST_APP_DOMAIN" "0.1.0+1.20.0" --latest
  assert_failure
  assert_output --regexp "cannot use .* together"

  run $ABRA app deploy "$TEST_APP_DOMAIN" --chaos --latest
  assert_failure
  assert_output --regexp "cannot use .* together"
}

# bats test_tags=slow
@test "deploy latest after chaos deploy" {
  latestRelease=$(_latest_release)

  run bash -c "echo foo >> $ABRA_DIR/recipes/$TEST_RECIPE/foo"
  assert_success
  assert_exists "$ABRA_DIR/recipes/$TEST_RECIPE/foo"

  run $ABRA app deploy "$TEST_APP_DOMAIN" \
    --no-input --no-converge-checks --chaos
  assert_success

  _reset_recipe
  assert_not_exists "$ABRA_DIR/recipes/$TEST_RECIPE/foo"

  run $ABRA app deploy "$TEST_APP_DOMAIN" \
    --no-input --no-converge-checks --force --latest
  assert_success
  assert_output --partial "$latestRelease"

  run grep -q "TYPE=$TEST_RECIPE:$latestRelease" \
    "$ABRA_DIR/servers/$TEST_SERVER/$TEST_APP_DOMAIN.env"
  assert_success
}

# bats test_tags=slow
@test "deploy latest after undeployed chaos deploy" {
  latestRelease=$(_latest_release)

  run bash -c "echo foo >> $ABRA_DIR/recipes/$TEST_RECIPE/foo"
  assert_success
  assert_exists "$ABRA_DIR/recipes/$TEST_RECIPE/foo"

  run $ABRA app deploy "$TEST_APP_DOMAIN" \
    --no-input --no-converge-checks --chaos
  assert_success

  _reset_recipe
  assert_not_exists "$ABRA_DIR/recipes/$TEST_RECIPE/foo"

  _undeploy_app

  run $ABRA app deploy "$TEST_APP_DOMAIN" \
    --no-input --no-converge-checks --latest
  assert_success
  assert_output --partial "$latestRelease"

  run grep -q "TYPE=$TEST_RECIPE:$latestRelease" \
    "$ABRA_DIR/servers/$TEST_SERVER/$TEST_APP_DOMAIN.env"
  assert_success
}

@test "chaos version label includes dirty marker" {
  run bash -c "echo foo >> $ABRA_DIR/recipes/$TEST_RECIPE/foo"
  assert_success
  assert_exists "$ABRA_DIR/recipes/$TEST_RECIPE/foo"

  run $ABRA app deploy "$TEST_APP_DOMAIN" \
    --no-input --no-converge-checks --chaos
  assert_success

  run $ABRA app labels "$TEST_APP_DOMAIN" --chaos
  assert_success
  assert_output --regexp 'chaos-version.*+U'
}

# bats test_tags=slow
@test "deploy latest on new deploy" {
  run $ABRA app deploy "$TEST_APP_DOMAIN" "0.1.0+1.20.0" \
    --no-input --no-converge-checks
  assert_success

  _undeploy_app

  latestRelease=$(_latest_release)

  run $ABRA app deploy "$TEST_APP_DOMAIN" \
    --no-input --no-converge-checks --latest
  assert_success
  assert_output --partial "$latestRelease"
}

# bats test_tags=slow
@test "ignore timeout when not present in env" {
  run $ABRA app deploy "$TEST_APP_DOMAIN" --no-input --no-converge-checks
  assert_success
  refute_output --partial "timeout: set to"
}

# bats test_tags=slow
@test "use timeout when present in env" {
  run sed -i 's/#TIMEOUT=120/TIMEOUT=120/g' \
    "$ABRA_DIR/servers/$TEST_SERVER/$TEST_APP_DOMAIN.env"
  assert_success

  # NOTE(d1}: --debug  required
  run $ABRA app deploy "$TEST_APP_DOMAIN" --no-input --no-converge-checks --debug
  assert_success
  assert_output --partial "timeout: set to 120"
}

# bats test_tags=slow
@test "no chaos version label if no chaos" {
  _deploy_app

  run $ABRA app labels "$TEST_APP_DOMAIN" --no-input
  assert_success
  refute_output --regexp "coop-cloud.abra-test-recipe.$TEST_SERVER.chaos-version"
}

# bats test_tags=slow
@test "no IMAGES diff if no images change" {
  run $ABRA app deploy "$TEST_APP_DOMAIN" --no-input --no-converge-checks
  assert_success

  run $ABRA app deploy "$TEST_APP_DOMAIN" --no-input --no-converge-checks --force
  assert_success
  refute_output --partial "IMAGES"
}

# bats test_tags=slow
@test "re-deploy updates existing env vars" {
  run $ABRA app deploy "$TEST_APP_DOMAIN" --no-input
  assert_success

  run docker inspect --format='{{range .Config.Env}}{{println .}}{{end}}' \
    $(docker ps -f name="$TEST_APP_DOMAIN_$TEST_SERVER" -q)
  assert_success
  assert_output --partial "WITH_COMMENT=foo"

  run sed -i 's/WITH_COMMENT=foo/WITH_COMMENT=bar/g' \
    "$ABRA_DIR/servers/$TEST_SERVER/$TEST_APP_DOMAIN.env"
  assert_success

  run $ABRA app deploy "$TEST_APP_DOMAIN" --no-input --force
  assert_success

  run docker inspect --format='{{range .Config.Env}}{{println .}}{{end}}' \
    $(docker ps -f name="$TEST_APP_DOMAIN_$TEST_SERVER" -q)
  assert_success
  refute_output --partial "WITH_COMMENT=foo"
  assert_output --partial "WITH_COMMENT=bar"
}

# bats test_tags=slow
@test "deploy with udp and tcp on same port" {
  run sed -i 's/TYPE=abra-test-recipe:.*/TYPE=git.coopcloud.tech\/p4u1\/abra-test-recipe:030e8a1cb1a0f17281847b3e55d829220ad32c50/g' \
    "$ABRA_DIR/servers/$TEST_SERVER/$TEST_APP_DOMAIN.env"
  assert_success

  run bash -c "printf '\nCOMPOSE_FILE=\"\$COMPOSE_FILE:compose.udp-and-tcp.yml\"' >> $ABRA_DIR/servers/$TEST_SERVER/$TEST_APP_DOMAIN.env"
  assert_success

  run $ABRA app deploy "$TEST_APP_DOMAIN" --no-input
  assert_success

  run docker service inspect  --format '{{ range .Endpoint.Ports }}{{ .Protocol }}={{ .PublishedPort }}{{ end }}' \
    "${TEST_APP_DOMAIN//./_}_app"
  assert_success
  assert_output --partial "tcp=1312"
  assert_output --partial "udp=1312"
}

# bats test_tags=slow
@test "does not crash when docker image has no tag" {
  run sed -i 's/TYPE=abra-test-recipe:.*/TYPE=git.coopcloud.tech\/p4u1\/abra-test-recipe:b29422d5a344ea45df271443182f775ea82b4da8/g' \
    "$ABRA_DIR/servers/$TEST_SERVER/$TEST_APP_DOMAIN.env"
  assert_success

  run bash -c "printf '\nCOMPOSE_FILE=\"\$COMPOSE_FILE:compose.no-image-tag.yml\"' >> $ABRA_DIR/servers/$TEST_SERVER/$TEST_APP_DOMAIN.env"
  assert_success

  run $ABRA app deploy "$TEST_APP_DOMAIN" --no-input
  assert_success
}


@test "does not use old recipe version when recipe is broken" {
  run $ABRA app new zammad \
    --no-input \
    --server "$TEST_SERVER" \
    --domain "zammad.$TEST_SERVER" \
    --secrets
  assert_success
  assert_exists "$ABRA_DIR/servers/$TEST_SERVER/zammad.$TEST_SERVER.env"

  # NOTE(d1): --no-converge-checks because the zammad recipe is a beast and we
  #           mostly only care about the correct version being used
  run $ABRA app deploy "zammad.$TEST_SERVER" \
    --no-input --no-converge-checks
  assert_success
  refute_output --partial "1.0.0+6.3.1-95"
}

# bats test_tags=slow
@test "unable to deploy borked tag" {
  _remove_tags

  run git -C "$ABRA_DIR/recipes/$TEST_RECIPE" tag \
    -a "2.4.8_1" -m "feat: completely borked tag"
  assert_success

  run $ABRA app deploy "$TEST_APP_DOMAIN" "2.4.8_1" \
    --no-input --no-converge-checks --debug
  assert_failure
  assert_output --partial "unable to parse"
}

# bats test_tags=slow
@test "app deploy with borked sample env gives useful error" {
  run $ABRA recipe new foo --no-input
  assert_success

  run $ABRA app new foo \
    --no-input \
    --server "$TEST_SERVER" \
    --domain "foo.$TEST_SERVER" \
    --chaos
  assert_success
  assert_exists "$ABRA_DIR/servers/$TEST_SERVER/foo.$TEST_SERVER.env"

  run bash -c "printf '\nEVIL-VAR=EVIL' >> $ABRA_DIR/recipes/foo/.env.sample"
  assert_success

  run $ABRA app deploy "foo.$TEST_SERVER" \
    --no-input --no-converge-checks --chaos
  assert_failure
  assert_output --partial "unexpected character"
}
