#!/bin/sh
# Jetable. L'ecran 5a sur le serveur de dev.
set -eu

BASE=http://127.0.0.1:4214
JAR=$(mktemp -d)

signin() {
  curl -s -c "$JAR/$1.txt" -H 'Content-Type: application/json' \
    -d "{\"username\":\"$1\",\"password\":\"$2\"}" \
    "$BASE/api/auth/sign-in/username" > /dev/null
}
signin narin admin1
signin malai manager1
signin nattikan staff1

printf '%-46s %-8s %-8s %s\n' route admin manager staff
for r in /tasks /review /positions /admin/people /admin/holidays /admin/recurrence /tasks/new \
         /team; do
  a=$(curl -s -o /dev/null -w '%{http_code}' -b "$JAR/narin.txt" "$BASE$r")
  m=$(curl -s -o /dev/null -w '%{http_code}' -b "$JAR/malai.txt" "$BASE$r")
  s=$(curl -s -o /dev/null -w '%{http_code}' -b "$JAR/nattikan.txt" "$BASE$r")
  printf '%-46s %-8s %-8s %s\n' "$r" "$a" "$m" "$s"
done

page() { curl -s -b "$JAR/$1.txt" "$BASE$2"; }

echo
echo "=== ARIA, admin sur Accounting ==="
P=$(page narin '/team?dept=Accounting')
echo "role=tablist   : $(printf '%s' "$P" | grep -o 'role="tablist"' | wc -l | tr -d ' ')"
echo "role=tab       : $(printf '%s' "$P" | grep -o 'role="tab"' | wc -l | tr -d ' ')"
echo "aria-selected=true : $(printf '%s' "$P" | grep -o 'aria-selected="true"' | wc -l | tr -d ' ')"
echo "role=tabpanel  : $(printf '%s' "$P" | grep -o 'role="tabpanel"' | wc -l | tr -d ' ')"

echo
echo "=== corps, admin sur Accounting, onglet en retard groupe ==="
printf '%s' "$P" | sed -n 's/.*<main[^>]*>\(.*\)<\/main>.*/\1/p' \
  | sed 's/<[^>]*>/\n/g' | grep -v '^[[:space:]]*$' | head -30
