#!/usr/bin/env -S bats --report-formatter junit --formatter tap -j 8
# -*-sh-*-

source ./common/test_helper_functions.bash || exit 1
source ./common/functions || exit 1

if [ -s ./common/TEST_ENV ]; then
	source ./common/TEST_ENV
fi

setup_file() {
	PKG=OMB
	MODULE=omb
	FAMILY="${LMOD_FAMILY_COMPILER}-${LMOD_FAMILY_MPI}"

	export PKG MODULE FAMILY

}

setup() {
	OUTPUT="$(mktemp)"

	export OUTPUT
}

teardown() {
	rm -f "${OUTPUT}"
}

@test "[$PKG] Verify $PKG module is loaded and matches rpm version (${FAMILY})" {
	module list "${MODULE}" | grep "1) ${MODULE}" >&"${OUTPUT}" || exit 1
	run grep "${MODULE}" "${OUTPUT}"
	assert_success

	# check version against rpm
	local rpm
	rpm=$(get_rpm_name "${MODULE}")
	local version
	version="$(rpm -q --queryformat='%{VERSION}\n' "${rpm}")"
	run cat "${OUTPUT}"
	assert_output "  1) ${MODULE}/$version"
}

@test "[$PKG] Verify ${PKG}_DIR is defined and directory exists (${FAMILY})" {
	PKG_DIR="${PKG}_DIR"

	if [ -z "${!PKG_DIR}" ]; then
		flunk "env setting ${PKG_DIR} not defined"
	fi

	if [ ! -d "${!PKG_DIR}" ]; then
		flunk "directory ${!PKG_DIR} does not exist"
	fi
}

# -------------
# Binary Tests
# --------------

@test "[$PKG] Verify osu_bw binary is available (${FAMILY})" {
	binary=osu_bw

	run which osu_bw
	assert_success
}

@test "[$PKG] Verify osu_latency binary is available (${FAMILY})" {
	binary=osu_latency

	run which $binary
	assert_success
}

@test "[$PKG] Verify osu_allgather binary is available (${FAMILY})" {
	binary=osu_allgather

	run which $binary
	assert_success
}

@test "[$PKG] Verify osu_get_bw binary is available (${FAMILY})" {
	binary=osu_get_bw

	run which $binary
	assert_success
}

@test "[$PKG] Verify osu_put_latency binary is available (${FAMILY})" {
	binary=osu_put_latency

	run which $binary
	assert_success
}
