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

teardown(){
  _undeploy_app
}

@test "retrieve recipe if missing" {
  skip "https://git.coopcloud.tech/coop-cloud/organising/issues/583"

  assert_exists "$ABRA_DIR/recipes/$TEST_RECIPE"

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

  run $ABRA app backup "$TEST_APP_DOMAIN"
  assert_failure
  assert_output --partial 'no containers matching'
  assert_exists "$ABRA_DIR/recipes/$TEST_RECIPE"
}

@test "bail if unstaged changes and no --chaos" {
  skip "https://git.coopcloud.tech/coop-cloud/organising/issues/583"

  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'

  run $ABRA app backup "$TEST_APP_DOMAIN" app
  assert_failure
  assert_output --partial 'locally unstaged changes'

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

  _checkout_recipe
}

# bats test_tags=slow
@test "do not bail if unstaged changes and --chaos" {
  skip "https://git.coopcloud.tech/coop-cloud/organising/issues/583"

  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'

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

  run $ABRA app backup "$TEST_APP_DOMAIN" app --chaos
  assert_success
  assert_output --partial 'running backup for the app service'

  _undeploy_app

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

@test "ensure recipe up to date if no --offline" {
  skip "https://git.coopcloud.tech/coop-cloud/organising/issues/583"

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

  run git -C "$ABRA_DIR/recipes/$TEST_RECIPE" status
  assert_output --partial 'behind 3'

  run $ABRA app backup "$TEST_APP_DOMAIN" --debug
  assert_failure
  assert_output --partial 'no containers matching'

  run git -C "$ABRA_DIR/recipes/$TEST_RECIPE" status
  refute_output --partial 'behind 3'

  _reset_recipe
}

@test "ensure recipe not up to date if --offline" {
  skip "https://git.coopcloud.tech/coop-cloud/organising/issues/583"

  latestCommit="$(git -C "$ABRA_DIR/recipes/$TEST_RECIPE" rev-parse --short HEAD)"
  refute [ -z "$latestCommit" ];

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

  run git -C "$ABRA_DIR/recipes/$TEST_RECIPE" status
  assert_output --partial 'behind 3'

  run $ABRA app backup "$TEST_APP_DOMAIN" --debug --offline
  assert_failure
  assert_output --partial 'no containers matching'

  run git -C "$ABRA_DIR/recipes/$TEST_RECIPE" status
  assert_output --partial 'behind 3'

  run git -C "$ABRA_DIR/recipes/$TEST_RECIPE" checkout "$latestCommit"
  assert_success

  run git -C "$ABRA_DIR/recipes/$TEST_RECIPE" status
  refute_output --partial 'behind 3'

  _reset_recipe
}

@test "detect backup labels" {
  skip "https://git.coopcloud.tech/coop-cloud/organising/issues/583"

  run $ABRA app backup "$TEST_APP_DOMAIN" --debug
  assert_failure
  assert_output --partial 'no containers matching'

  assert_output --partial 'detected backup paths'
  assert_output --partial 'detected pre-hook command'
  assert_output --partial 'detected post-hook command'
}

@test "error if backups not enabled" {
  skip "https://git.coopcloud.tech/coop-cloud/organising/issues/583"

  run sed -i '/backupbot.backup=true/d' "$ABRA_DIR/recipes/$TEST_RECIPE/compose.yml"
  assert_success

  run $ABRA app backup "$TEST_APP_DOMAIN" app --chaos
  assert_failure
  assert_output --partial 'no backup config for app'

  _checkout_recipe
}

@test "error if backup paths not configured" {
  skip "https://git.coopcloud.tech/coop-cloud/organising/issues/583"

  run sed -i '/backupbot.backup.path=.*/d' "$ABRA_DIR/recipes/$TEST_RECIPE/compose.yml"
  assert_success

  run $ABRA app backup "$TEST_APP_DOMAIN" app --chaos
  assert_failure
  assert_output --partial 'backup paths are empty for app?'

  _checkout_recipe
}

# bats test_tags=slow
@test "backup single service" {
  skip "https://git.coopcloud.tech/coop-cloud/organising/issues/583"

  _deploy_app

  run $ABRA app backup "$TEST_APP_DOMAIN" app
  assert_success
  assert_output --partial 'running backup for the app service'

  sanitisedDomainName="${TEST_APP_DOMAIN//./_}"
  assert_output --partial "_$sanitisedDomainName_app"

  assert_exists "$ABRA_DIR/backups"
  assert bash -c "ls $ABRA_DIR/backups | grep -q $1_$sanitisedDomainName_app"

  _undeploy_app
}
