#!/usr/bin/env bash
# shellcheck disable=SC2317
# shellcheck disable=SC2329

set -euo pipefail

function set_up_before_script() {
  SCRIPT_TEMP_FILE=$(temp_file "custom-prefix")
  SCRIPT_TEMP_DIR=$(temp_dir "custom-prefix")
}

function tear_down_after_script() {
  export BASHUNIT_DEV_LOG=""
}

function set_up() {
  BASHUNIT_DEV_LOG=$(temp_file)
  export BASHUNIT_DEV_LOG
}

function tear_down() {
  rm -f "$BASHUNIT_DEV_LOG"
}

function test_globals_current_dir() {
  assert_same "tests/unit" "$(current_dir)"
}

function test_globals_current_filename() {
  assert_same "globals_test.sh" "$(current_filename)"
}

function test_globals_caller_filename() {
  assert_same "./src" "$(caller_filename)"
}

function test_globals_caller_line() {
  assert_matches "[0-9]*" "$(caller_line)"
}

function test_globals_current_timestamp() {
  assert_matches \
    "^[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}$" \
    "$(current_timestamp)"
}

function test_globals_is_command_available() {
  function existing_fn(){
    return 0
  }
  assert_successful_code "$(is_command_available existing_fn)"
}

function test_globals_is_command_not_available() {
  assert_general_error "$(is_command_available non_existing_fn)"
}

function test_globals_random_str_default_len() {
  assert_matches "^[A-Za-z0-9]{6}$" "$(random_str)"
}

function test_globals_random_str_custom_len() {
  assert_matches "^[A-Za-z0-9]{3}$" "$(random_str 3)"
}

function test_globals_temp_file_in_test_function() {
  # shellcheck disable=SC2155
  local temp_file=$(temp_file "custom-prefix")
  assert_file_exists "$temp_file"
  cleanup_testcase_temp_files
  assert_file_not_exists "$temp_file"
}

function test_globals_temp_dir_in_test_function() {
  # shellcheck disable=SC2155
  local temp_dir=$(temp_dir "custom-prefix")
  assert_directory_exists "$temp_dir"
  cleanup_testcase_temp_files
  assert_directory_not_exists "$temp_dir"
}

function test_globals_temp_dir_and_file_in_script() {
  assert_directory_exists "$SCRIPT_TEMP_DIR"
  assert_file_exists "$SCRIPT_TEMP_FILE"
  cleanup_script_temp_files
  assert_directory_not_exists "$SCRIPT_TEMP_DIR"
  assert_file_not_exists "$SCRIPT_TEMP_FILE"
}

function test_globals_log_level_error() {
  log "error" "hello," "error"

  assert_file_contains "$BASHUNIT_DEV_LOG" "[ERROR]: hello, error"
}

function test_globals_log_level_warning() {
  log "warning" "hello," "warning"

  assert_file_contains "$BASHUNIT_DEV_LOG" "[WARNING]: hello, warning"
}

function test_globals_log_level_debug() {
  log "debug" "hello," "debug"

  assert_file_contains "$BASHUNIT_DEV_LOG" "[DEBUG]: hello, debug"
}

function test_globals_log_level_critical() {
  log "critical" "hello," "critical"

  assert_file_contains "$BASHUNIT_DEV_LOG" "[CRITICAL]: hello, critical"
}

function test_globals_log_level_info() {
  log "info" "hello," "info"

  assert_file_contains "$BASHUNIT_DEV_LOG" "[INFO]: hello, info"
}

function test_globals_log_level_default() {
  log "hello," "info"

  assert_file_contains "$BASHUNIT_DEV_LOG" "[INFO]: hello, info"
}

function test_internal_log_prefix() {
  export BASHUNIT_INTERNAL_LOG=true
  internal_log "info" "some" "message"
  export BASHUNIT_INTERNAL_LOG=false

  assert_file_contains "$BASHUNIT_DEV_LOG" "[INTERNAL]: info some message"
}
