Compare commits

...

2 commits

Author SHA1 Message Date
e636ea323c Add gum-styled annotations and less paging to VHS demo tapes
Annotations use demo/note.sh (gum style with clear) wrapped in
Hide/Show so only the styled box appears. Long output commands
pipe through less with search to highlight key fields.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-15 00:12:42 -07:00
d19709dc1f Add automated test suite for systab
44 tests covering job creation, status, logs, pause/resume,
notifications, time format parsing, error cases, and cleanup.
Tests run against real systemd user timers with automatic cleanup.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-14 22:50:17 -07:00
6 changed files with 460 additions and 30 deletions

View file

@ -31,16 +31,12 @@ Key functions: `parse_time` (time spec → OnCalendar), `_write_unit_files` (sha
## Testing
There are no automated tests. Test manually with systemd user timers:
```bash
./systab -t "every 5 minutes" -c "echo test"
./systab -S
./systab -L
./systab -P <id>
./systab -R <id>
./systab -C
./test.sh
```
Runs 44 tests against real systemd user timers covering job creation, status, logs, pause/resume, notifications, time format parsing, error cases, and cleanup. All test jobs are cleaned up automatically via trap.
## Notes
- ShellCheck can be used for linting: `shellcheck systab`.

View file

@ -12,9 +12,12 @@ Set TypingSpeed 50ms
Sleep 1s
# First create some jobs to edit
Type "# Create some jobs to work with"
Hide
Type "./demo/note.sh 'Creating some jobs to work with'"
Enter
Sleep 500ms
Show
Sleep 1s
Type "systab -t 'every 5 minutes' -c 'echo ping'"
Enter
Sleep 2s
@ -23,8 +26,11 @@ Enter
Sleep 2s
# Open edit mode with EDITOR=nano for visibility
Type "# Open edit mode — add notifications, create and modify jobs"
Hide
Type "./demo/note.sh 'Opening edit mode — add notifications, create and modify jobs'"
Enter
Sleep 500ms
Show
Sleep 1s
Type "EDITOR=nano systab -E"
Sleep 500ms
@ -54,17 +60,28 @@ Ctrl+X
Sleep 3s
# Show the result
Type "# See the changes applied"
Hide
Type "./demo/note.sh 'Changes applied — checking status'"
Enter
Sleep 500ms
Type "systab -S"
Show
Sleep 1s
Type "systab -S | less"
Sleep 500ms
Enter
Sleep 4s
Sleep 2s
Type "/Job:"
Enter
Sleep 3s
Type "q"
Sleep 1s
# Re-open edit mode to show flags are persisted
Type "# Re-open edit mode — flags are persisted"
Hide
Type "./demo/note.sh 'Re-opening edit mode — flags are persisted'"
Enter
Sleep 500ms
Show
Sleep 1s
Type "EDITOR=nano systab -E"
Sleep 500ms

3
demo/note.sh Executable file
View file

@ -0,0 +1,3 @@
#!/usr/bin/env bash
clear
gum style --bold --foreground 6 --border rounded --border-foreground 8 "$*"

View file

@ -12,8 +12,11 @@ Set TypingSpeed 50ms
Sleep 1s
# Desktop notification — success case
Type "# Desktop notification on success/failure"
Hide
Type "./demo/note.sh 'Desktop notification on success'"
Enter
Sleep 500ms
Show
Sleep 1s
Type "systab -t 'in 1 minute' -c 'echo success' -i"
Sleep 500ms
@ -21,14 +24,23 @@ Enter
Sleep 2s
# Desktop notification — failure case
Hide
Type "./demo/note.sh 'Desktop notification on failure'"
Enter
Sleep 500ms
Show
Sleep 1s
Type "systab -t 'in 1 minute' -c 'exit 1' -i"
Sleep 500ms
Enter
Sleep 2s
# Email notification
Type "# Email notification via sendmail"
Hide
Type "./demo/note.sh 'Email notification via sendmail'"
Enter
Sleep 500ms
Show
Sleep 1s
Type "systab -t 'in 1 minute' -c 'echo test' -m user@example.com"
Sleep 500ms
@ -36,8 +48,11 @@ Enter
Sleep 2s
# Both notifications
Type "# Both desktop and email"
Hide
Type "./demo/note.sh 'Both desktop and email notifications'"
Enter
Sleep 500ms
Show
Sleep 1s
Type "systab -t 'every day at 9am' -c '/home/user/backup.sh' -i -m admin@example.com"
Sleep 500ms
@ -45,18 +60,37 @@ Enter
Sleep 2s
# Show the generated service file to see ExecStopPost lines
Type "# Inspect generated service file"
Hide
Type "./demo/note.sh 'Inspecting generated service file'"
Enter
Sleep 500ms
Show
Sleep 1s
Type "cat ~/.config/systemd/user/systab_*.service | head -20"
Type "cat ~/.config/systemd/user/systab_*.service | less"
Sleep 500ms
Enter
Sleep 4s
# Check status
Type "systab -S"
Sleep 500ms
Sleep 2s
Type "/ExecStopPost"
Enter
Sleep 3s
Type "q"
Sleep 1s
# Check status
Hide
Type "./demo/note.sh 'Checking status of all jobs'"
Enter
Sleep 500ms
Show
Sleep 1s
Type "systab -S | less"
Sleep 500ms
Enter
Sleep 2s
Type "/Job:"
Enter
Sleep 3s
Type "q"
Sleep 1s
Sleep 2s

View file

@ -12,48 +12,111 @@ Set TypingSpeed 50ms
Sleep 1s
# Create a recurring job
Hide
Type "./demo/note.sh 'Creating a recurring health check (every 5 minutes)'"
Enter
Sleep 500ms
Show
Sleep 1s
Type "systab -t 'every 5 minutes' -c 'echo health check OK'"
Sleep 500ms
Enter
Sleep 2s
# Create a one-time job
Hide
Type "./demo/note.sh 'Creating a one-time reminder (30 minutes from now)'"
Enter
Sleep 500ms
Show
Sleep 1s
Type "systab -t 'in 30 minutes' -c 'echo reminder: meeting soon'"
Sleep 500ms
Enter
Sleep 2s
# Check status of all jobs
Type "systab -S"
Hide
Type "./demo/note.sh 'Checking status of all jobs'"
Enter
Sleep 500ms
Show
Sleep 1s
Type "systab -S | less"
Sleep 500ms
Enter
Sleep 4s
# View logs
Type "systab -L"
Sleep 500ms
Sleep 2s
Type "/Job:"
Enter
Sleep 3s
Type "q"
Sleep 1s
# View logs
Hide
Type "./demo/note.sh 'Viewing job logs'"
Enter
Sleep 500ms
Show
Sleep 1s
Type "systab -L | less"
Sleep 500ms
Enter
Sleep 2s
Type "/Logs for"
Enter
Sleep 3s
Type "q"
Sleep 1s
# Pause a job (uses the first job ID from status)
Hide
Type "./demo/note.sh 'Pausing a job'"
Enter
Sleep 500ms
Show
Sleep 1s
Type "systab -P $(systab -S 2>/dev/null | grep -m1 'Job:' | awk '{print $2}')"
Sleep 500ms
Enter
Sleep 2s
# Show it's paused
Type "systab -S"
Hide
Type "./demo/note.sh 'Verifying job is paused'"
Enter
Sleep 500ms
Show
Sleep 1s
Type "systab -S | less"
Sleep 500ms
Enter
Sleep 2s
Type "/Disabled"
Enter
Sleep 3s
Type "q"
Sleep 1s
# Resume it
Hide
Type "./demo/note.sh 'Resuming the paused job'"
Enter
Sleep 500ms
Show
Sleep 1s
Type "systab -R $(systab -S 2>/dev/null | grep -m1 'Disabled' -B4 | grep 'Job:' | awk '{print $2}')"
Sleep 500ms
Enter
Sleep 2s
# Clean up completed one-time jobs
Hide
Type "./demo/note.sh 'Cleaning up completed one-time jobs'"
Enter
Sleep 500ms
Show
Sleep 1s
Type "systab -C"
Sleep 500ms
Enter

317
test.sh Executable file
View file

@ -0,0 +1,317 @@
#!/usr/bin/env bash
set -euo pipefail
SYSTAB="./systab"
SYSTEMD_USER_DIR="${HOME}/.config/systemd/user"
passed=0
failed=0
total=0
# Colors (disabled if not a terminal)
if [[ -t 1 ]]; then
GREEN=$'\033[32m' RED=$'\033[31m' BOLD=$'\033[1m' RESET=$'\033[0m'
else
GREEN="" RED="" BOLD="" RESET=""
fi
# --- Helpers ---
pass() {
echo "${GREEN}[PASS]${RESET} $1"
passed=$((passed + 1))
total=$((total + 1))
}
fail() {
echo "${RED}[FAIL]${RESET} $1$2"
failed=$((failed + 1))
total=$((total + 1))
}
# Run command, expect exit 0
assert_success() {
local desc="$1"; shift
local output
if output=$("$@" 2>&1); then
_last_output="$output"
pass "$desc"
else
_last_output="$output"
fail "$desc" "expected exit 0, got $?"
fi
}
# Run command, expect non-zero exit
assert_failure() {
local desc="$1"; shift
local output
if output=$("$@" 2>&1); then
_last_output="$output"
fail "$desc" "expected non-zero exit, got 0"
else
_last_output="$output"
pass "$desc"
fi
}
# Run command, check stdout contains string
assert_output() {
local desc="$1" expected="$2"; shift 2
local output
if output=$("$@" 2>&1); then
_last_output="$output"
if [[ "$output" == *"$expected"* ]]; then
pass "$desc"
else
fail "$desc" "output missing \"$expected\""
fi
else
_last_output="$output"
fail "$desc" "command failed (exit $?)"
fi
}
# Check that $_last_output contains string
assert_last_output_contains() {
local desc="$1" expected="$2"
if [[ "$_last_output" == *"$expected"* ]]; then
pass "$desc"
else
fail "$desc" "output missing \"$expected\""
fi
}
# Check file exists and contains pattern (grep -q)
assert_file_contains() {
local desc="$1" file="$2" pattern="$3"
if [[ ! -f "$file" ]]; then
fail "$desc" "file not found: $file"
elif grep -q -e "$pattern" "$file"; then
pass "$desc"
else
fail "$desc" "pattern \"$pattern\" not in $file"
fi
}
# Extract job ID from "Job created: <id>" output
extract_id() {
sed -n 's/^Job created: \([0-9a-f]\{6\}\)$/\1/p' <<< "$_last_output"
}
# Remove all systab_* unit files and reload
cleanup() {
local had_units=false
for f in "$SYSTEMD_USER_DIR"/systab_*.service "$SYSTEMD_USER_DIR"/systab_*.timer; do
[[ -f "$f" ]] || continue
local unit
unit=$(basename "$f")
systemctl --user stop "$unit" 2>/dev/null || true
systemctl --user disable "$unit" 2>/dev/null || true
rm -f "$f"
had_units=true
done
if $had_units; then
systemctl --user daemon-reload 2>/dev/null || true
fi
}
# --- Setup ---
_last_output=""
trap cleanup EXIT
cleanup
echo "${BOLD}Running systab tests...${RESET}"
echo ""
# ============================================================
# Job creation
# ============================================================
echo "${BOLD}--- Job creation ---${RESET}"
assert_output "create recurring job" "Job created:" $SYSTAB -t "every 5 minutes" -c "echo test_recurring"
id_recurring=$(extract_id)
assert_output "create one-time job" "Job created:" $SYSTAB -t "in 30 minutes" -c "echo test_onetime"
id_onetime=$(extract_id)
if [[ -z "$id_recurring" || -z "$id_onetime" ]]; then
echo "FATAL: could not extract job IDs, aborting"
exit 1
fi
# ============================================================
# Status
# ============================================================
echo ""
echo "${BOLD}--- Status ---${RESET}"
assert_output "show status all" "Managed Jobs Status" $SYSTAB -S
assert_last_output_contains "status contains recurring job ID" "$id_recurring"
assert_last_output_contains "status contains one-time job ID" "$id_onetime"
assert_output "show status single job" "$id_recurring" $SYSTAB -S "$id_recurring"
# ============================================================
# Logs
# ============================================================
echo ""
echo "${BOLD}--- Logs ---${RESET}"
assert_output "view logs all" "Logs for" $SYSTAB -L
assert_output "view logs single job" "$id_recurring" $SYSTAB -L "$id_recurring"
# ============================================================
# Pause / Resume
# ============================================================
echo ""
echo "${BOLD}--- Pause / Resume ---${RESET}"
assert_output "pause job" "Paused:" $SYSTAB -P "$id_recurring"
assert_output "show paused state" "Disabled" $SYSTAB -S "$id_recurring"
assert_output "pause already paused" "Already paused:" $SYSTAB -P "$id_recurring"
assert_output "resume job" "Resumed:" $SYSTAB -R "$id_recurring"
assert_output "resume already running" "Already running:" $SYSTAB -R "$id_recurring"
# ============================================================
# Notifications
# ============================================================
echo ""
echo "${BOLD}--- Notifications ---${RESET}"
assert_output "create with -i" "Job created:" $SYSTAB -t "every 10 minutes" -c "echo notify_test" -i
id_notify=$(extract_id)
assert_file_contains "-i service has ExecStopPost" \
"$SYSTEMD_USER_DIR/systab_${id_notify}.service" "^ExecStopPost="
assert_file_contains "-i service has notify-send" \
"$SYSTEMD_USER_DIR/systab_${id_notify}.service" "notify-send"
assert_output "create with -i -o" "Job created:" $SYSTAB -t "every 10 minutes" -c "echo output_test" -i -o
id_output=$(extract_id)
assert_file_contains "-i -o service has journalctl" \
"$SYSTEMD_USER_DIR/systab_${id_output}.service" "journalctl"
assert_file_contains "-i -o service has %%s" \
"$SYSTEMD_USER_DIR/systab_${id_output}.service" "%%s"
assert_output "create with -i -o 5" "Job created:" $SYSTAB -t "every 10 minutes" -c "echo output5_test" -i -o 5
id_output5=$(extract_id)
assert_file_contains "-i -o 5 service has -n 5" \
"$SYSTEMD_USER_DIR/systab_${id_output5}.service" "-n 5"
# Email notification (only if sendmail/msmtp available)
if command -v sendmail &>/dev/null || command -v msmtp &>/dev/null; then
assert_output "create with -m" "Job created:" $SYSTAB -t "every 10 minutes" -c "echo mail_test" -m test@example.com
id_mail=$(extract_id)
assert_file_contains "-m service has ExecStopPost" \
"$SYSTEMD_USER_DIR/systab_${id_mail}.service" "^ExecStopPost="
assert_output "create with -i -m" "Job created:" $SYSTAB -t "every 10 minutes" -c "echo both_test" -i -m test@example.com
id_both=$(extract_id)
local_count=$(grep -c "^ExecStopPost=" "$SYSTEMD_USER_DIR/systab_${id_both}.service")
if [[ "$local_count" -ge 2 ]]; then
pass "-i -m service has two ExecStopPost lines"
else
fail "-i -m service has two ExecStopPost lines" "found $local_count"
fi
else
echo " (skipping email notification tests — no sendmail/msmtp)"
fi
# ============================================================
# Time format parsing
# ============================================================
echo ""
echo "${BOLD}--- Time format parsing ---${RESET}"
# Source parse_time from systab (we need the function)
# We can call it by running systab in a subshell that only defines the function
parse_time_test() {
local input="$1" expected="$2"
# Run parse_time via bash sourcing
local result
if result=$(bash -c '
source <(sed -n "/^parse_time()/,/^}/p" ./systab; sed -n "/^error()/,/^}/p" ./systab; sed -n "/^is_recurring()/,/^}/p" ./systab)
parse_time "$1"
' _ "$input" 2>&1); then
if [[ "$result" == "$expected" ]]; then
pass "parse_time '$input' -> '$expected'"
else
fail "parse_time '$input'" "got '$result', expected '$expected'"
fi
else
fail "parse_time '$input'" "failed: $result"
fi
}
parse_time_test "every 5 minutes" "*:0/5"
parse_time_test "every day at 2am" "*-*-* 02:00:00"
parse_time_test "daily" "daily"
parse_time_test "hourly" "hourly"
parse_time_test "every monday at 9am" "Mon *-*-* 09:00:00"
parse_time_test "*:0/15" "*:0/15"
parse_time_test "every minute" "*:*"
parse_time_test "every hour" "hourly"
parse_time_test "weekly" "weekly"
parse_time_test "monthly" "monthly"
# "in 5 minutes" produces an absolute timestamp — just check it doesn't fail
assert_success "parse_time 'in 5 minutes' succeeds" bash -c '
source <(sed -n "/^parse_time()/,/^}/p" ./systab; sed -n "/^error()/,/^}/p" ./systab)
parse_time "in 5 minutes"
'
# ============================================================
# Error cases
# ============================================================
echo ""
echo "${BOLD}--- Error cases ---${RESET}"
assert_failure "missing -t" $SYSTAB -c "echo hello"
assert_failure "-c and -f together" $SYSTAB -t "daily" -c "echo hello" -f /nonexistent
assert_failure "invalid job ID for -P" $SYSTAB -P "zzzzzz"
assert_failure "invalid job ID for -R" $SYSTAB -R "zzzzzz"
# -o without -i or -m: should succeed (flag accepted, just no notification lines)
assert_output "-o without -i/-m creates job" "Job created:" $SYSTAB -t "every 10 minutes" -c "echo bare_output" -o
id_bare_o=$(extract_id)
# Should have FLAGS comment but no ExecStopPost
assert_file_contains "-o without -i/-m has FLAGS comment" \
"$SYSTEMD_USER_DIR/systab_${id_bare_o}.service" "SYSTAB_FLAGS=o"
# Should NOT have ExecStopPost
if grep -q "^ExecStopPost=" "$SYSTEMD_USER_DIR/systab_${id_bare_o}.service"; then
fail "-o without -i/-m has no ExecStopPost" "found ExecStopPost"
else
pass "-o without -i/-m has no ExecStopPost"
fi
# ============================================================
# Clean
# ============================================================
echo ""
echo "${BOLD}--- Clean ---${RESET}"
# No elapsed one-time jobs (our one-time is still waiting)
assert_output "clean with no elapsed jobs" "No completed" $SYSTAB -C < /dev/null
# ============================================================
# Summary
# ============================================================
echo ""
echo "---"
echo "${BOLD}${total} tests: ${GREEN}${passed} passed${RESET}, ${RED}${failed} failed${RESET}"
[[ $failed -eq 0 ]]