#!/usr/bin/env bash

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

teardown(){
  run rm -rf "$ABRA_DIR/recipes/matrix-synapse"
  assert_success
  assert_not_exists "$ABRA_DIR/recipes/$TEST_RECIPE"

  run rm -rf "$ABRA_DIR/recipes/git_coopcloud_tech_coop-cloud_matrix-synapse"
  assert_success
  assert_not_exists "$ABRA_DIR/recipes/git_coopcloud_tech_coop-cloud_matrix-synapse"
}

# bats test_tags=slow
@test "recipe fetch all" {
  run rm -rf "$ABRA_DIR/recipes/matrix-synapse"
  assert_success
  assert_not_exists "$ABRA_DIR/recipes/matrix-synapse"

  run $ABRA recipe fetch --all
  assert_success
  assert_exists "$ABRA_DIR/recipes/matrix-synapse"
}

@test "recipe fetch single recipe" {
  run rm -rf "$ABRA_DIR/recipes/matrix-synapse"
  assert_success
  assert_not_exists "$ABRA_DIR/recipes/matrix-synapse"

  run $ABRA recipe fetch matrix-synapse
  assert_success
  assert_exists "$ABRA_DIR/recipes/matrix-synapse"
}

@test "error if missing args/flags" {
  run $ABRA recipe fetch
  assert_failure
}

@test "error if single recipe and --all" {
  run $ABRA recipe fetch matrix-synapse --all
  assert_failure
}

@test "do not refetch without --force" {
  run $ABRA recipe fetch matrix-synapse
  assert_success
  assert_exists "$ABRA_DIR/recipes/matrix-synapse"

  run $ABRA recipe fetch matrix-synapse
  assert_output --partial "already fetched"
}

@test "refetch with --force" {
  run $ABRA recipe fetch matrix-synapse
  assert_success
  assert_exists "$ABRA_DIR/recipes/matrix-synapse"

  run $ABRA recipe fetch matrix-synapse --force
  assert_success
  refute_output --partial "already fetched"
}

@test "refetch with --force does not erase unstaged changes" {
  run $ABRA recipe fetch matrix-synapse
  assert_success
  assert_exists "$ABRA_DIR/recipes/matrix-synapse"

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

  run $ABRA recipe fetch matrix-synapse --force
  assert_success
  assert_exists "$ABRA_DIR/recipes/matrix-synapse"
  assert_exists "$ABRA_DIR/recipes/matrix-synapse/foo"
}

@test "fetch with --ssh" {
  run $ABRA recipe fetch matrix-synapse --ssh
  assert_success
  assert_exists "$ABRA_DIR/recipes/matrix-synapse"

  run git -C "$ABRA_DIR/recipes/matrix-synapse" remote -v
  assert_success
  assert_output --partial "ssh://"
}

@test "re-fetch with --ssh/--force" {
  run $ABRA recipe fetch matrix-synapse
  assert_success
  assert_exists "$ABRA_DIR/recipes/matrix-synapse"

  run git -C "$ABRA_DIR/recipes/matrix-synapse" remote -v
  assert_success
  assert_output --partial "https://"

  run $ABRA recipe fetch matrix-synapse --ssh --force
  assert_success
  assert_exists "$ABRA_DIR/recipes/matrix-synapse"

  run git -C "$ABRA_DIR/recipes/matrix-synapse" remote -v
  assert_success
  assert_output --partial "ssh://"
}

@test "fetch remote recipe" {
  run $ABRA recipe fetch git.coopcloud.tech/coop-cloud/matrix-synapse
  assert_success
  assert_exists "$ABRA_DIR/recipes/git_coopcloud_tech_coop-cloud_matrix-synapse"
}

@test "remote recipe do not refetch without --force" {
  run $ABRA recipe fetch git.coopcloud.tech/coop-cloud/matrix-synapse
  assert_success
  assert_exists "$ABRA_DIR/recipes/git_coopcloud_tech_coop-cloud_matrix-synapse"

  run $ABRA recipe fetch git.coopcloud.tech/coop-cloud/matrix-synapse
  assert_success
  assert_output --partial "already fetched"
}
