#!/bin/sh

# Author: Lance Lin <LQi254@protonmail.com>
# Date: 20 December 2021
# File: run-unit-test
# Purpose: run a simple-unit test for the fsm-lite package
#
# Details:
# Use a publicly accessible .fasta file format example. There are others, but they quite large (>30MB)
#
# Execute fsm-lite on the sample data and verify the resulting generated files are the correct length
#
# Verify the two ouptut is correct:
# tmp.meta should look like: "testfile	1508	", which is {inputFileName, filesize}
# tmp.tmp will have the sequencing and should be 1508 bytes in length, as reported by tmp.meta

set -e

if [ "${AUTOPKGTEST_TMP}" = "" ] ; then
  AUTOPKGTEST_TMP=$(mktemp -d /tmp/${pkg}-test.XXXXXX)
  trap "rm -rf ${AUTOPKGTEST_TMP}" 0 INT QUIT ABRT PIPE TERM
fi
cd "${AUTOPKGTEST_TMP}"

echo "run-unit-test: generating data"

# FASTA format file from "UT Southwestern Medical Center"
# URL: http://prodata.swmed.edu/promals/info/fasta_format_file_example.htm
# Website title: Fasta format file example
# Date accessed: 20 December 2021
cat <<EOF > testfile.fasta
>seq0
FQTWEEFSRAAEKLYLADPMKVRVVLKYRHVDGNLCIKVTDDLVCLVYRTDQAQDVKKIEKF
>seq1
KYRTWEEFTRAAEKLYQADPMKVRVVLKYRHCDGNLCIKVTDDVVCLLYRTDQAQDVKKIEKFHSQLMRLME LKVTDNKECLKFKTDQAQEAKKMEKLNNIFFTLM
>seq2
EEYQTWEEFARAAEKLYLTDPMKVRVVLKYRHCDGNLCMKVTDDAVCLQYKTDQAQDVKKVEKLHGK
>seq3
MYQVWEEFSRAVEKLYLTDPMKVRVVLKYRHCDGNLCIKVTDNSVCLQYKTDQAQDVK
>seq4
EEFSRAVEKLYLTDPMKVRVVLKYRHCDGNLCIKVTDNSVVSYEMRLFGVQKDNFALEHSLL
>seq5
SWEEFAKAAEVLYLEDPMKCRMCTKYRHVDHKLVVKLTDNHTVLKYVTDMAQDVKKIEKLTTLLMR
>seq6
FTNWEEFAKAAERLHSANPEKCRFVTKYNHTKGELVLKLTDDVVCLQYSTNQLQDVKKLEKLSSTLLRSI
>seq7
SWEEFVERSVQLFRGDPNATRYVMKYRHCEGKLVLKVTDDRECLKFKTDQAQDAKKMEKLNNIFF
>seq8
SWDEFVDRSVQLFRADPESTRYVMKYRHCDGKLVLKVTDNKECLKFKTDQAQEAKKMEKLNNIFFTLM
>seq9
KNWEDFEIAAENMYMANPQNCRYTMKYVHSKGHILLKMSDNVKCVQYRAENMPDLKK
>seq10
FDSWDEFVSKSVELFRNHPDTTRYVVKYRHCEGKLVLKVTDNHECLKFKTDQAQDAKKMEK
EOF


echo "run-unit-test: generating test input list file"
cat <<EOF > input.list
testfile ${AUTOPKGTEST_TMP}/testfile.fasta
EOF

echo "run-unit-test: generating md5 sums for verification"
echo "001b2030f247b79c4339a3a4d4b3438c  tmp.meta" >> checksums
echo "19f8e2431c735a5f86e60251a80b37f4  tmp.tmp" >> checksums

echo "run-unit-test: running fsm-lite"
fsm-lite -l input.list -t tmp | gzip - > output.txt.gz

echo "run-unit-test: verifying md5 checksums"
md5sum --check checksums

echo "run-unit-test: verifying output"

# Output in tmp.meta should contain "1508", the file size/output of tmp.tmp in bytes
# The test will capture the file size from tmp.meta and compare the output file length
fileSizeMeta=$(cat tmp.meta | cut -f2)

# Check file size of tmp.tmp matches what is reported by tmp.meta
fileSize=$(du -b tmp.tmp | cut -f1)

if [ ${fileSize} -ne ${fileSizeMeta} ]
then
	echo "run-unit-test: Failure - output file size does not match expected: $fileSize (generated) instead of $fileSizeMeta (expected)"
	exit 1
fi
