#!/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 "validate app argument" {
  skip "https://git.coopcloud.tech/coop-cloud/organising/issues/583"

  run $ABRA app restore
  assert_failure
  assert_output --partial 'no app provided'

  run $ABRA app restore DOESNTEXIST
  assert_failure
  assert_output --partial 'cannot find app'
}

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

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

  run $ABRA app restore "$TEST_APP_DOMAIN" app DOESNTEXIST
  assert_failure
  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 restore "$TEST_APP_DOMAIN" app DOESNTEXIST
  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 touch "$BATS_TMPDIR/foo.txt"
  assert_success
  assert_exists "$BATS_TMPDIR/foo.txt"

  run tar -cvf "$BATS_TMPDIR/foo.tar.gz" "$BATS_TMPDIR/foo.txt"
  assert_success
  assert_exists "$BATS_TMPDIR/foo.tar.gz"

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

  run $ABRA app restore "$TEST_APP_DOMAIN" app "$BATS_TMPDIR/foo.tar.gz" \
    --debug --chaos
  assert_success
  assert_output --partial 'restore config detected'
  assert_output --partial 'detected pre-hook command'
  assert_output --partial 'detected post-hook command'

  run $ABRA app run "$TEST_APP_DOMAIN" app ls "$BATS_TMPDIR"
  assert_success
  assert_output --partial 'foo.txt'

  _undeploy_app

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

  _checkout_recipe
}

@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 --regexp 'behind .* 3 commits'

  run $ABRA app restore "$TEST_APP_DOMAIN" app
  assert_failure

  run git -C "$ABRA_DIR/recipes/$TEST_RECIPE" status
  assert_output --partial "up to date"
}

@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 --regexp 'behind .* 3 commits'

  run $ABRA app restore "$TEST_APP_DOMAIN" app --offline
  assert_failure

  run git -C "$ABRA_DIR/recipes/$TEST_RECIPE" status
  assert_output --regexp 'behind .* 3 commits'

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

  run git -C "$ABRA_DIR/recipes/$TEST_RECIPE" status
  assert_output --partial "HEAD detached at $latestCommit"
}

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

  run $ABRA app restore "$TEST_APP_DOMAIN"
  assert_failure
  assert_output --partial 'missing <service>'

  run $ABRA app restore "$TEST_APP_DOMAIN" app
  assert_failure
  assert_output --partial 'missing <file>'
}

@test "error if file doesn't exist" {
  skip "https://git.coopcloud.tech/coop-cloud/organising/issues/583"

  run $ABRA app restore "$TEST_APP_DOMAIN" app DOESNTEXIST
  assert_failure
  assert_output --partial "doesn't exist"
}

# bats test_tags=slow
@test "detect labels if restore enabled" {
  skip "https://git.coopcloud.tech/coop-cloud/organising/issues/583"

  run touch "$BATS_TMPDIR/foo.txt"
  assert_success
  assert_exists "$BATS_TMPDIR/foo.txt"

  run tar -cvf "$BATS_TMPDIR/foo.tar.gz" "$BATS_TMPDIR/foo.txt"
  assert_success
  assert_exists "$BATS_TMPDIR/foo.tar.gz"

  _deploy_app

  run $ABRA app restore "$TEST_APP_DOMAIN" app "$BATS_TMPDIR/foo.tar.gz" --debug
  assert_success
  assert_output --partial 'restore config detected'
  assert_output --partial 'detected pre-hook command'
  assert_output --partial 'detected post-hook command'

  _undeploy_app
}

# bats test_tags=slow
@test "no error if restore not enabled" {
  skip "https://git.coopcloud.tech/coop-cloud/organising/issues/583"

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

  run touch "$BATS_TMPDIR/foo.txt"
  assert_success
  assert_exists "$BATS_TMPDIR/foo.txt"

  run tar -cvf "$BATS_TMPDIR/foo.tar.gz" "$BATS_TMPDIR/foo.txt"
  assert_success
  assert_exists "$BATS_TMPDIR/foo.tar.gz"

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

  run $ABRA app restore "$TEST_APP_DOMAIN" app "$BATS_TMPDIR/foo.tar.gz" \
    --debug --chaos
  assert_success
  refute_output --partial 'restore config detected'
  refute_output --partial 'detected pre-hook command'
  refute_output --partial 'detected post-hook command'

  _undeploy_app

  _checkout_recipe
}

# bats test_tags=slow
@test "error if service doesn't exist" {
  skip "https://git.coopcloud.tech/coop-cloud/organising/issues/583"

  run touch "$BATS_TMPDIR/foo.txt"
  assert_success
  assert_exists "$BATS_TMPDIR/foo.txt"

  run tar -cvf "$BATS_TMPDIR/foo.tar.gz" "$BATS_TMPDIR/foo.txt"
  assert_success
  assert_exists "$BATS_TMPDIR/foo.tar.gz"

  _deploy_app

  run $ABRA app restore "$TEST_APP_DOMAIN" DOESNTEXIST "$BATS_TMPDIR/foo.tar.gz" --debug
  assert_failure
  assert_output --partial 'no containers matching'

  _undeploy_app
}

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

  run touch "$BATS_TMPDIR/foo.txt"
  assert_success
  assert_exists "$BATS_TMPDIR/foo.txt"

  run tar -cvf "$BATS_TMPDIR/foo.tar.gz" "$BATS_TMPDIR/foo.txt"
  assert_success
  assert_exists "$BATS_TMPDIR/foo.tar.gz"

  _deploy_app

  run $ABRA app restore "$TEST_APP_DOMAIN" app "$BATS_TMPDIR/foo.tar.gz" --debug
  assert_success
  assert_output --partial 'restore config detected'
  assert_output --partial 'detected pre-hook command'
  assert_output --partial 'detected post-hook command'

  run $ABRA app run "$TEST_APP_DOMAIN" app ls "$BATS_TMPDIR"
  assert_success
  assert_output --partial 'foo.txt'

  _undeploy_app
}
