#!/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"

  _reset_app
  _reset_tags

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

@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
}

# 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 "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"
}

@test "no deploy if lint error" {
  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_failure
  assert_output --partial 'failed lint checks'
}

# 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
}

# 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
}

# 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 "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 "ignore env version checkout after deploy" {
  tagHash=$(_get_tag_hash "0.1.0+1.20.0")

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

  assert_equal $(_get_current_hash) "$tagHash"

  run $ABRA app check --ignore-env-version "$TEST_APP_DOMAIN"
  assert_success

  assert_equal $(_get_current_hash) "$(_get_head_hash)"

  run $ABRA app check "$TEST_APP_DOMAIN"
  assert_success

  assert_equal $(_get_current_hash) "$tagHash"
}

# bats test_tags=slow
@test "ignore env version 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 --ignore-env-version
  assert_success
  assert_output --partial "$latestRelease"
}

# 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"
}
