#!/bin/bash # Behaviour tests for the i3blocks tutor block: today's credited # minutes, the hidden 60/60 state, the unreadable-ledger warning and the click. # The ledger is a fixture and run.sh is a stub, so nothing reads the real # ledger and no tutor or browser is ever started. set -euo pipefail SCRIPT_DIR=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd) REPO_DIR=$(cd -- "$SCRIPT_DIR/.." && pwd) BLOCK="$REPO_DIR/i3blocks/tutor.sh" TMP_DIR=$(mktemp -d) cleanup() { rm -rf "$TMP_DIR" } trap cleanup EXIT fail() { printf 'FAIL: %s\n' "$1" >&2 exit 1 } assert_eq() { local expected=$1 local actual=$2 local context=$3 if [[ "$expected" != "$actual" ]]; then fail "$context (expected '$expected', actual '$actual')" fi } printf '{}' | jq -e . >/dev/null 2>&1 || fail 'a working jq is required for the tutor block tests (pacman -S jq)' BIN_DIR="$TMP_DIR/bin" mkdir -p "$BIN_DIR" cat >"$BIN_DIR/run.sh" <>"$TMP_DIR/clicks" STUB chmod +x "$BIN_DIR/run.sh" LEDGER="$TMP_DIR/ledger.json" TODAY=$(date +%Y-%m-%d) YESTERDAY=$(date -d yesterday +%Y-%m-%d) # ledger ... (writes {"entries":[...]}; no minutes = legacy row) ledger() { local rows=() local spec for spec in "$@"; do local day kind minutes IFS=: read -r day kind minutes <<<"$spec" rows+=("$(jq -n --arg day "$day" --arg kind "$kind" --arg m "$minutes" \ '{day: $day, kind: $kind} + (if $m == "" then {} else {detail: {minutes: ($m | tonumber)}} end)')") done printf '%s\n' "${rows[@]}" | jq -s '{entries: .}' >"$LEDGER" } run_block() { TUTOR_LEDGER="$LEDGER" TUTOR_RUN="$BIN_DIR/run.sh" \ bash "$BLOCK" } line() { sed -n "${1}p" <<<"$2" } printf 'Checking tutor block states...\n' rm -f "$LEDGER" out=$(run_block) assert_eq '⚙ tutor 0/60' "$(line 1 "$out")" 'no ledger is 0/60' assert_eq '⚙ 0/60' "$(line 2 "$out")" 'no ledger short text' assert_eq '#F1FA8C' "$(line 3 "$out")" 'progress is yellow' ledger "$TODAY:credit:3" "$TODAY:credit:1" "$YESTERDAY:credit:50" out=$(run_block) assert_eq '⚙ tutor 4/60' "$(line 1 "$out")" 'sums only today' assert_eq '⚙ 4/60' "$(line 2 "$out")" 'short text' ledger "$TODAY:credit" "$TODAY:credit:2" assert_eq '⚙ tutor 17/60' "$(line 1 "$(run_block)")" 'legacy row counts 15' ledger "$TODAY:credit:5" "$TODAY:debit:9" assert_eq '⚙ tutor 5/60' "$(line 1 "$(run_block)")" 'only credit rows count' ledger "$TODAY:credit:59" "$TODAY:credit:1" out=$(run_block) assert_eq '' "$(line 1 "$out")" '60/60 hides the block' assert_eq '' "$(line 2 "$out")" '60/60 short text hidden' printf 'Checking failure states are never blank or green...\n' printf '{' >"$LEDGER" out=$(run_block) assert_eq '⚙ tutor: ledger unreadable' "$(line 1 "$out")" 'unreadable text' assert_eq '#FFB86C' "$(line 3 "$out")" 'unreadable is orange' printf 'Checking a click runs run.sh...\n' ledger "$TODAY:credit" BLOCK_BUTTON=1 run_block >/dev/null for _ in 1 2 3 4 5 6 7 8 9 10; do [[ -s "$TMP_DIR/clicks" ]] && break sleep 0.1 done assert_eq 'clicked' "$(cat "$TMP_DIR/clicks" 2>/dev/null)" 'click runs run.sh' printf 'tutor block: all checks passed\n'