Files
nocal/tests/cli_week_start_test.sh
Bernardo Magri 989332ef9a feat: make month week start configurable
Add a Monday-default --week-start option for every weekday and carry it through domain layout, full and compact rendering, event query windows, print mode, and the interactive TUI.

Preserve byte-compatible Monday output and cover rotated headers, spill dates, invalid input, sanitizer behavior, and live terminal restoration.
2026-07-18 09:58:27 +01:00

99 lines
3.3 KiB
Bash

#!/bin/sh
set -eu
if [ "$#" -ne 1 ]; then
echo "usage: cli_week_start_test.sh /path/to/nocal" >&2
exit 2
fi
nocal=$1
tmpdir=$(mktemp -d "${TMPDIR:-/tmp}/nocal-cli-week-start.XXXXXX")
trap 'rm -rf "$tmpdir"' EXIT HUP INT TERM
fail()
{
echo "$1" >&2
exit 1
}
expect_failure()
{
description=$1
shift
if "$@" >"$tmpdir/out" 2>"$tmpdir/err"; then
fail "$description unexpectedly succeeded"
fi
}
help=$("$nocal" --help)
case $help in
*"--week-start DAY"*"monday through sunday"*) ;;
*) fail "help does not document --week-start and its accepted range" ;;
esac
"$nocal" --print --demo >"$tmpdir/default"
"$nocal" --print --demo --week-start monday >"$tmpdir/monday"
cmp "$tmpdir/default" "$tmpdir/monday" >/dev/null ||
fail "default frame is not Monday-first"
header=$(sed -n '2p' "$tmpdir/monday" | tr -d ' ')
case $header in
*MondayTuesdayWednesdayThursdayFridaySaturdaySunday*) ;;
*) fail "Monday-first header is not in weekday order" ;;
esac
"$nocal" --print --demo --week-start sunday >"$tmpdir/sunday"
header=$(sed -n '2p' "$tmpdir/sunday" | tr -d ' ')
case $header in
*SundayMondayTuesdayWednesdayThursdayFridaySaturday*) ;;
*) fail "Sunday-first header was not rotated" ;;
esac
"$nocal" --print --demo --week-start wednesday >"$tmpdir/wednesday"
header=$(sed -n '2p' "$tmpdir/wednesday" | tr -d ' ')
case $header in
*WednesdayThursdayFridaySaturdaySundayMondayTuesday*) ;;
*) fail "Wednesday-first header was not rotated" ;;
esac
# The first numbered cell must be the correct spill date for each selected
# week start. Nocal is Linux-only, so GNU date is available in its test setup.
month_start=$(date +%Y-%m-01)
month_weekday=$(date -d "$month_start" +%w)
for selection in sunday:0 wednesday:3; do
name=${selection%%:*}
weekday=${selection##*:}
offset=$(( (month_weekday - weekday + 7) % 7 ))
expected=$(date -d "$month_start -$offset days" +%-d)
first_row=$(sed -n '4p' "$tmpdir/$name")
actual=$(printf '%s\n' "$first_row" | sed 's/[^0-9]/ /g' | awk '{print $1}')
[ "$actual" = "$expected" ] ||
fail "$name-first grid began on $actual instead of spill date $expected"
done
# Option parsing finishes before the calendar is loaded or any write mode can
# run. Invalid, missing, and repeated values therefore leave the target exact.
calendar=$tmpdir/calendar.ics
printf '%s\n' 'sentinel calendar bytes' >"$calendar"
cp "$calendar" "$tmpdir/calendar-before"
expect_failure "invalid week-start value" \
"$nocal" --calendar "$calendar" --week-start MONDAY
grep -F -- "invalid --week-start day" "$tmpdir/err" >/dev/null ||
fail "invalid week-start value did not report its parse error"
expect_failure "missing week-start value" \
"$nocal" --calendar "$calendar" --week-start
grep -F -- "--week-start requires a day" "$tmpdir/err" >/dev/null ||
fail "missing week-start value did not report its parse error"
expect_failure "repeated week-start option" \
"$nocal" --calendar "$calendar" --week-start sunday --week-start monday
grep -F -- "--week-start may only be supplied once" "$tmpdir/err" >/dev/null ||
fail "repeated week-start did not report its parse error"
cmp "$calendar" "$tmpdir/calendar-before" >/dev/null ||
fail "week-start parse failure changed the calendar"
[ ! -e "$calendar.bak" ] ||
fail "week-start parse failure created a calendar backup"