#!/usr/bin/env bash
set -e

if [[ $# != 1 ]]; then
  echo >&2 "usage: $(basename "$0") <tests-dir>"
  exit 1
fi

{
  cd "$1"
  for x in VMTests/*/*; do
    echo >&2 "$x"
    echo -n "$x " ; hevm vm-test --file $x
  done
} | {
  while read path test outcome; do
    category=$(dirname "$path")
    testcase=$(basename "${path%.json}")
    row="<tr><td class=testcase>$testcase<td>$outcome<td class=category>$category"
    row+=$'\n'
    case $outcome in
      ok) passed+=$row ;;
      *)  failed+=$row ;;
    esac
  done
  
  cat <<.
<!doctype html>
<title>hevm test results</title>
<style>
* {
  font-family:
    "latin modern mono", "fantasque sans mono",
    inconsolata, menlo, monospace;
  font-size: 22px;
  line-height: 26px;
}

body { margin: 2rem; }
header { text-align: center; margin: 4rem 0; }
table { border-collapse: collapse; width: 100%; }
tr:nth-child(even) { background: rgba(0, 0, 0, 0.05); }
td:not(:first-child):not(:last-child) { padding: 0 1rem; }
.category { opacity: 0.6; text-align: right }
a { color: darkblue; text-decoration: none; }
h1, h2 { text-align: center; margin-top: 2rem  }
.testcase { font-weight: bold }
#failed .testcase { color: rgb(200, 0, 0) }
#passed .testcase { color: rgb(0, 150, 0) }
</style>
<header>
<h1>hevm consensus test report</h1>
<p>
$(date +%Y-%m-%d)
<p>
.

  wc -l <<<"$passed"
  echo "passed, "
  wc -l <<<"$failed"
  echo "failed"
  
  cat <<.
<p>
(Test suite: <span class=VMTests</span>VMTests</span> for Homestead)
</header>
<h2>Failed tests</h2>
<table id=failed>
<tbody>
.
  echo "$failed"
  cat <<.
</table>
<h2>Passed tests</h2>
<table id=passed>
<tbody>
.
  echo "$passed"
  cat <<.
</table>
.
}