diff --git a/CHANGELOG.md b/CHANGELOG.md index 9cf6612..d2f7d97 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -25,6 +25,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed +- **Lock message end time and 24:00 ranges:** The configure lock error printed a blank end time (`Lock ends: today`) because it formatted `HH:MM` values, which `parse_config` and the lock-hour reader return, as bare hours. `calculate_restriction_end_time` and `show_config_locked_error` now share `format_endpoint_12h`, so the message reads `Lock ends: 5:30 AM today`. A range that ends at 24 or 24:00 (for example `16-24`) now reads as ending at 12:00 AM on the next day. It used to read 12:00 PM, and `build_weekly_restricted_intervals` dropped it, so the popup's next-unrestricted time treated that evening as free. `range_endpoint_to_day_minutes` takes an `end` role that accepts 24, as the 16-hour cap code already did. A range that ends at the week boundary (Saturday 24:00) now continues into Sunday in that search. `status.sh` and the configure range summary also print a 24:00 end as 12:00 AM. No restriction, lock, cap, or popup-lifetime check changed: the runtime restricted-time test already handled 24:00. +- **Wayland resolution detection:** `get_display_resolution` tests `A || B && C`, which Bash reads as `(A || B) && C`, so `WAYLAND_DISPLAY` alone never selected Wayland detection. It now does, with or without `XDG_SESSION_TYPE`. - **Pause notice icon:** The Linux "Pause Activated" dialog passes the same yad, zenity, and kdialog icon flags as the warning. A warning that already ran in that process supplies the cached help answer, so the notice does not probe again. The notice watchdog, buttons, and per-day cap are unchanged. - **Icon help probe:** The help check runs the command in its own process group and reads the result from a temporary file after that group is stopped. GNU `timeout` and `gtimeout` are not used: they return when the direct child exits and leave a descendant holding the pipe. A descendant that leaves the process group is not signalled, and it still cannot hold the popup, because the popup reads the file. The check sends SIGTERM after one second and SIGKILL one second later, or SIGKILL immediately if the leader exits sooner. The help file is capped at 2 MiB so a writer that leaves the process group cannot fill the disk. Popup buttons, the watchdog, and the per-day cap are unchanged. - **Greptile review fixes:** Pre-spawn gamescope root-size queries now have a roughly two-second deadline, kill and reap stalled probes, and use fallback dimensions on failure. The test runner preserves its selected Bash interpreter for child tests when PATH points to an older Bash. diff --git a/dev-docs/TO_DO.md b/dev-docs/TO_DO.md index 12cbcfa..9f24e7e 100644 --- a/dev-docs/TO_DO.md +++ b/dev-docs/TO_DO.md @@ -29,10 +29,6 @@ Doc convention: **`docs/`** = end-users; **`dev-docs/`** = developers and agents - Plan: [plans/plan-safety-scan-fixes-2026-05-30.md](plans/plan-safety-scan-fixes-2026-05-30.md) · Analysis: [safety-linux-macos-analysis.md](safety-linux-macos-analysis.md) §5.1 - [ ] **Additional hours on unrestricted days** — Lock/uninstall hours should not apply when a day has no restrictions (opt-in override) → [plan](plans/plan-additional-hours-no-restrictions.md) - [ ] **Popup frequency early tolerance** — Allow last-popup-time gate to fire up to `min(5% of POPUP_DISPLAY_FREQUENCY, 5 min)` early so 10-min settings do not slip to ~15 min on the 5-min timer grid; clamp early so earliest gap stays above popup on-screen time (`+60s` and `≤90%` cushions). **Plan needs human review — do not implement yet.** → [plan](plans/plan-popup-frequency-early-tolerance-2026-10-19.md) -- [ ] **Three bugs found while writing tests (2026-10-28)** — none is a safety invariant. Each needs its own fix and a test that asserts the corrected behavior. - - `calculate_restriction_end_time` (`lib/time-calculations.sh`) returns a blank end time for `HH:MM-HH:MM` ranges, which is what `parse_config` returns. `format_hour_12h` takes a bare hour, so `05:00` prints `[: 05:00: integer expected` and the result is `|Monday|false`. The overnight tail from the previous day fails the same way. `configure-ui-errors.sh` shows this result in the lock message. - - `build_weekly_restricted_intervals` (`lib/time-calculations.sh`) drops any range that ends at 24, such as `TUESDAY=16-24`, because `range_endpoint_to_day_minutes` accepts hours 0–23 only. `get_next_unrestricted_time` then treats that evening as unrestricted. - - `get_display_resolution` (`lib/resolution-detection.sh`) writes `[ -n "$WAYLAND_DISPLAY" ] || [ -n "$XDG_SESSION_TYPE" ] && [ "$XDG_SESSION_TYPE" = wayland ]`. Bash reads that as `(A || B) && C`, so `WAYLAND_DISPLAY` alone never selects Wayland detection. - [ ] **#29** — kdialog window size via `--textbox` + `--geometry` → [enhancement-29](enhancements/enhancement-29-kdialog-window-size-control.md) - [ ] **#10** — Audio alerts → [enhancement-10](enhancements/enhancement-10-audio-alerts.md) - [x] **Configurable pause cooldown and duration** — Pause disabled, or cooldown every N hours (N = 6, 8, 12, 24; default 24); pause length 1/2/3/5/10/15 min → [plan](completed-plans/plan-configurable-pause-interval.md) (scope: [enhancement-21 pause](enhancements/enhancement-21/enhancement-21-pause-scope-clarification.md)) diff --git a/dev-docs/testing-assessments/test-tiers.md b/dev-docs/testing-assessments/test-tiers.md index 2ea1555..34eb82d 100644 --- a/dev-docs/testing-assessments/test-tiers.md +++ b/dev-docs/testing-assessments/test-tiers.md @@ -22,6 +22,7 @@ Fast, headless, no live installed config. Default timeout 300s per script. | `test-popup-size-config.sh` | Popup size getters, fixed and display-based dimensions, 95% clamp | | `test-config-safety.sh` | Default hours, runtime cap inputs, runtime cap gate, wizard default fill | | `test-time-calculations.sh` | Range durations, weekly intervals, next unrestricted time, restriction end time | +| `test-config-locked-message.sh` | Text of the configure lock error: active range, end time, end day | | `test-scheduler-platform.sh` | systemd and launchd helpers with a stubbed `systemctl` | | `test-resolution-detection-stubbed.sh` | xrandr, Wayland and gamescope resolution detection with stub tools | | `test-configure-dialogs-darwin-ui.py` | macOS configure dialogs with fake tkinter widgets (run by the Python coverage gate, not `run-tests.sh`) | diff --git a/lib/configure-core.sh b/lib/configure-core.sh index 5be8ce2..378b0e8 100644 --- a/lib/configure-core.sh +++ b/lib/configure-core.sh @@ -13,6 +13,7 @@ # - has_gui() - Check if GUI is available # - minutes_to_oncalendar() - Convert minutes to systemd OnCalendar format # - format_hour_12h() - Format hour to 12-hour format with AM/PM +# - format_endpoint_12h() - Format a range endpoint (H, HH:MM, 24:00) to 12-hour format # - get_threshold_warn_description() - Get human-readable warning threshold description # - get_threshold_block_description() - Get human-readable block threshold description # - day_num_to_name_upper() - Convert day number to uppercase day name (for config file) @@ -355,6 +356,34 @@ format_hour_12h() { fi } +# Format a range endpoint as "H:MM AM/PM". +# Accepts a bare hour ("5", "05") or "H:MM" / "HH:MM", the forms parse_config and +# the lock-hour readers return. A bare hour prints ":00", so "5" matches +# format_hour_12h. An end of 24 or 24:00 is midnight and prints "12:00 AM". +# Prints nothing and returns 1 for anything else. +format_endpoint_12h() { + local part="${1//[[:space:]]/}" + local hour minute display ampm + if [[ "${part}" =~ ^([0-9]{1,2})(:([0-9]{2}))?$ ]]; then + hour=$((10#${BASH_REMATCH[1]})) + minute=$((10#${BASH_REMATCH[3]:-0})) + else + return 1 + fi + if [ "${minute}" -gt 59 ] || [ "${hour}" -gt 24 ] || { [ "${hour}" -eq 24 ] && [ "${minute}" -ne 0 ]; }; then + return 1 + fi + ampm="AM" + if [ "${hour}" -ge 12 ] && [ "${hour}" -lt 24 ]; then + ampm="PM" + fi + display=$((hour % 12)) + if [ "${display}" -eq 0 ]; then + display=12 + fi + printf '%d:%02d %s\n' "${display}" "${minute}" "${ampm}" +} + # Get human-readable description of warning threshold # Returns: "X hours/day" format get_threshold_warn_description() { diff --git a/lib/configure-ui-errors.sh b/lib/configure-ui-errors.sh index 5d02a87..103b02b 100644 --- a/lib/configure-ui-errors.sh +++ b/lib/configure-ui-errors.sh @@ -28,7 +28,7 @@ _configure_ui_errors_ensure_deps() { # shellcheck disable=SC1091 [ -f "${_dir}/time-parsing.sh" ] && source "${_dir}/time-parsing.sh" fi - if ! type format_hour_12h >/dev/null 2>&1 || ! type format_ranges_human >/dev/null 2>&1; then + if ! type format_hour_12h >/dev/null 2>&1 || ! type format_endpoint_12h >/dev/null 2>&1 || ! type format_ranges_human >/dev/null 2>&1; then local _dir _dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" # shellcheck disable=SC1091 @@ -439,7 +439,7 @@ show_config_locked_error() { end_part=$(echo "${end_part}" | sed 's/^[[:space:]]*//;s/[[:space:]]*$//') if range_overnight_tail_active_for_hour "${current_hour}" "${start_part}" "${end_part}"; then local end_12h - end_12h=$(format_hour_12h "${end_part}") + end_12h=$(format_endpoint_12h "${end_part}") || continue lock_end="${end_12h}|${day_name}|true" break fi @@ -526,7 +526,7 @@ show_config_locked_error() { end_part=$(echo "${end_part}" | sed 's/^[[:space:]]*//;s/[[:space:]]*$//') if range_overnight_tail_active_for_hour "${current_hour}" "${start_part}" "${end_part}"; then local end_12h - end_12h=$(format_hour_12h "${end_part}") + end_12h=$(format_endpoint_12h "${end_part}") || continue end_info="${end_12h}|${day_name}|true" break fi diff --git a/lib/resolution-detection.sh b/lib/resolution-detection.sh index c215777..aeee90e 100644 --- a/lib/resolution-detection.sh +++ b/lib/resolution-detection.sh @@ -45,7 +45,7 @@ get_display_resolution() { if [ -n "${DISPLAY}" ]; then # X11 session display_env="x11" - elif [ -n "${WAYLAND_DISPLAY}" ] || [ -n "${XDG_SESSION_TYPE}" ] && [ "${XDG_SESSION_TYPE}" = "wayland" ]; then + elif [ -n "${WAYLAND_DISPLAY}" ] || [ "${XDG_SESSION_TYPE}" = "wayland" ]; then # Wayland session display_env="wayland" elif pgrep -x gamescope >/dev/null 2>&1; then diff --git a/lib/status-config-display.sh b/lib/status-config-display.sh index 386414d..df9d3a5 100644 --- a/lib/status-config-display.sh +++ b/lib/status-config-display.sh @@ -24,16 +24,13 @@ format_hour_12h() { return 0 fi - local display_hour="${hour}" local ampm="AM" - - if [ "${hour}" -ge 12 ]; then + # 24:00 is midnight at the end of the day, so it stays AM. + if [ "${hour}" -ge 12 ] && [ "${hour}" -lt 24 ]; then ampm="PM" - if [ "${hour}" -gt 12 ]; then - display_hour=$((hour - 12)) - fi fi + local display_hour=$((10#${hour} % 12)) if [ "${display_hour}" -eq 0 ]; then display_hour=12 fi diff --git a/lib/time-calculations.sh b/lib/time-calculations.sh index 71c7141..6ee9038 100644 --- a/lib/time-calculations.sh +++ b/lib/time-calculations.sh @@ -276,6 +276,7 @@ calculate_total_day_duration() { # Convert one range endpoint (HH or HH:MM) to minutes from midnight (0-1439). # Prints minutes on stdout; returns 1 if invalid. +# $2 is "start" (default) or "end". An end of 24 or 24:00 is 1440, the end of the day. _range_part_to_day_minutes() { if ! type range_endpoint_to_day_minutes >/dev/null 2>&1; then local _tc_dir @@ -283,7 +284,7 @@ _range_part_to_day_minutes() { # shellcheck disable=SC1091 [ -f "${_tc_dir}/time-parsing.sh" ] && source "${_tc_dir}/time-parsing.sh" fi - range_endpoint_to_day_minutes "$1" + range_endpoint_to_day_minutes "$1" "${2:-start}" } build_weekly_restricted_intervals() { @@ -331,7 +332,7 @@ build_weekly_restricted_intervals() { local start_minute end_minute start_minute=$(_range_part_to_day_minutes "${start_part}") || continue - end_minute=$(_range_part_to_day_minutes "${end_part}") || continue + end_minute=$(_range_part_to_day_minutes "${end_part}" end) || continue if [ "${start_minute}" -gt "${end_minute}" ]; then # Overnight range detected (e.g., "23-6" or "23:30-06:00") @@ -588,7 +589,7 @@ get_next_unrestricted_time() { fi # Find the end of the current restricted interval to jump ahead - local interval_end="${minutes_per_week}" # Default: end of week + local interval_end=-1 # Earliest end among the intervals holding search_minute while IFS= read -r interval; do [ -z "${interval}" ] && continue @@ -599,17 +600,17 @@ get_next_unrestricted_time() { if [ "${search_minute}" -ge "${start}" ] && [ "${search_minute}" -lt "${end}" ]; then # We're in this interval - the next unrestricted time is at 'end' # Use the earliest end time (in case of overlapping intervals, though they should be merged) - if [ "${end}" -lt "${interval_end}" ]; then + if [ "${interval_end}" -lt 0 ] || [ "${end}" -lt "${interval_end}" ]; then interval_end="${end}" fi fi done <<< "${merged_intervals}" - # Jump to the end of the current restricted interval - if [ "${interval_end}" -lt "${minutes_per_week}" ]; then - # Normal case: end is in current week + # Jump to the end of the current restricted interval. An interval that + # ends at the week boundary (Saturday 24:00) jumps to minute 0 of Sunday. + if [ "${interval_end}" -gt "${search_minute}" ]; then local jump_distance=$((interval_end - search_minute)) - search_minute="${interval_end}" + search_minute=$((interval_end % minutes_per_week)) search_count=$((search_count + jump_distance)) else # Shouldn't happen if we found an interval, but fallback: move to next minute @@ -647,7 +648,7 @@ _time_calculations_ensure_time_parsing() { } _time_calculations_ensure_configure_core() { - if type format_hour_12h >/dev/null 2>&1 && [ -n "${DAY_NAMES[*]:-}" ]; then + if type format_endpoint_12h >/dev/null 2>&1 && [ -n "${DAY_NAMES[*]:-}" ]; then return 0 fi local _tc_dir @@ -666,6 +667,26 @@ _time_calculations_ensure_config() { [ -f "${_tc_dir}/config.sh" ] && source "${_tc_dir}/config.sh" } +# Print "END_12H|DAY_NAME|IS_NEXT_DAY" for a range that is active now. +# The end falls on the next day when the range crosses midnight or ends at 24:00. +# The end may be "H" or "HH:MM", the forms parse_config and the lock-hour readers return. +# Returns 1 when the end is not a valid time. +_restriction_end_info() { + local range="$1" + local current_day="$2" + local start_part end_part end_12h end_minutes + start_part=$(echo "${range%%-*}" | sed 's/^[[:space:]]*//;s/[[:space:]]*$//') + end_part=$(echo "${range##*-}" | sed 's/^[[:space:]]*//;s/[[:space:]]*$//') + end_12h=$(format_endpoint_12h "${end_part}") || return 1 + + if { end_minutes=$(range_endpoint_to_day_minutes "${end_part}" end) && [ "${end_minutes}" -eq 1440 ]; } \ + || range_is_overnight "${start_part}" "${end_part}"; then + echo "${end_12h}|${DAY_NAMES[$(( (current_day + 1) % 7 ))]}|true" + else + echo "${end_12h}|${DAY_NAMES[${current_day}]}|false" + fi +} + # Calculate when the current restriction period ends # This function finds which restriction range the current hour falls into and calculates when it ends # Handles overnight ranges and multiple ranges per day @@ -674,7 +695,7 @@ _time_calculations_ensure_config() { # $2 - current_day (0-6, Sunday=0) # $3 - restricted_range (optional, comma-separated ranges for current day) # Returns: "END_HOUR_12H|END_DAY_NAME|IS_OVERNIGHT" or empty string if not in restriction -# Dependencies: configure-core.sh (for format_hour_12h, DAY_NAMES), time-validation.sh (for is_hour_in_single_range), config.sh (for parse_config) +# Dependencies: configure-core.sh (for format_endpoint_12h, DAY_NAMES), time-validation.sh (for is_hour_in_single_range), config.sh (for parse_config) calculate_restriction_end_time() { local current_hour="$1" local current_day="$2" @@ -708,9 +729,8 @@ calculate_restriction_end_time() { if range_overnight_tail_active_for_hour "${current_hour}" "${start_part}" "${end_part}"; then local end_12h - end_12h=$(format_hour_12h "${end_part}") - local current_day_name="${DAY_NAMES[${current_day}]}" - echo "${end_12h}|${current_day_name}|true" + end_12h=$(format_endpoint_12h "${end_part}") || return 1 + echo "${end_12h}|${DAY_NAMES[${current_day}]}|true" return 0 fi done @@ -728,80 +748,34 @@ calculate_restriction_end_time() { # Check if current hour is in this range if type is_time_in_single_range >/dev/null 2>&1; then if is_time_in_single_range "$(printf '%02d00' "${current_hour}")" "${range}" self; then - local start_part end_part end_12h - start_part="${range%%-*}" - end_part="${range##*-}" - start_part=$(echo "${start_part}" | sed 's/^[[:space:]]*//;s/[[:space:]]*$//') - end_part=$(echo "${end_part}" | sed 's/^[[:space:]]*//;s/[[:space:]]*$//') - - end_12h=$(format_hour_12h "${end_part}") - local current_day_name="${DAY_NAMES[${current_day}]}" - - if range_is_overnight "${start_part}" "${end_part}"; then - local next_day_index=$(( (current_day + 1) % 7 )) - local next_day_name="${DAY_NAMES[${next_day_index}]}" - echo "${end_12h}|${next_day_name}|true" - else - echo "${end_12h}|${current_day_name}|false" - fi + _restriction_end_info "${range}" "${current_day}" || return 1 return 0 fi - else + elif type is_restricted_time >/dev/null 2>&1; then # Fallback: use minute-aware restricted check when available - if type is_restricted_time >/dev/null 2>&1; then - local current_time_hhmm - current_time_hhmm=$(printf "%02d00" "${current_hour}") - if is_restricted_time "${current_time_hhmm}" "${range}" self; then - local start_part end_part end_12h - start_part="${range%%-*}" - end_part="${range##*-}" - start_part=$(echo "${start_part}" | sed 's/^[[:space:]]*//;s/[[:space:]]*$//') - end_part=$(echo "${end_part}" | sed 's/^[[:space:]]*//;s/[[:space:]]*$//') - end_12h=$(format_hour_12h "${end_part}") - local current_day_name="${DAY_NAMES[${current_day}]}" - if range_is_overnight "${start_part}" "${end_part}"; then - local next_day_index=$(( (current_day + 1) % 7 )) - local next_day_name="${DAY_NAMES[${next_day_index}]}" - echo "${end_12h}|${next_day_name}|true" - else - echo "${end_12h}|${current_day_name}|false" - fi - return 0 - fi - else + if is_restricted_time "$(printf '%02d00' "${current_hour}")" "${range}" self; then + _restriction_end_info "${range}" "${current_day}" || return 1 + return 0 + fi + else # Legacy integer-hour fallback - local start_hour end_hour + local start_hour end_hour in_range=false start_hour=$(echo "${range}" | cut -d'-' -f1) end_hour=$(echo "${range}" | cut -d'-' -f2) - - local in_range=false + if [ "${start_hour}" -gt "${end_hour}" ]; then # Overnight range if [ "${current_hour}" -ge "${start_hour}" ]; then in_range=true fi - else - # Normal range - if [ "${current_hour}" -ge "${start_hour}" ] && [ "${current_hour}" -lt "${end_hour}" ]; then - in_range=true - fi + elif [ "${current_hour}" -ge "${start_hour}" ] && [ "${current_hour}" -lt "${end_hour}" ]; then + in_range=true fi - + if [ "${in_range}" = "true" ]; then - local end_12h - end_12h=$(format_hour_12h "${end_hour}") - local current_day_name="${DAY_NAMES[${current_day}]}" - - if [ "${start_hour}" -gt "${end_hour}" ]; then - local next_day_index=$(( (current_day + 1) % 7 )) - local next_day_name="${DAY_NAMES[${next_day_index}]}" - echo "${end_12h}|${next_day_name}|true" - else - echo "${end_12h}|${current_day_name}|false" - fi + _restriction_end_info "${range}" "${current_day}" || return 1 return 0 fi - fi fi done diff --git a/lib/time-parsing.sh b/lib/time-parsing.sh index 2553755..b900de5 100644 --- a/lib/time-parsing.sh +++ b/lib/time-parsing.sh @@ -114,26 +114,23 @@ parse_single_time() { } # Convert 24h to 12h format -# Parameters: hour (0-23), minute (0-59) +# Parameters: hour (0-24), minute (0-59) +# An hour of 24 is midnight at the end of the day, so it prints 12:00am. 24hour_to_12hour() { - local hour="$1" + local hour=$((10#$1)) local minute="$2" - - local h12="${hour}" + local ampm="am" - - if [ "$((10#${hour}))" -ge 12 ]; then + if [ "${hour}" -ge 12 ] && [ "${hour}" -lt 24 ]; then ampm="pm" - if [ "$((10#${hour}))" -gt 12 ]; then - h12=$((10#${hour} - 12)) - fi fi - if [ "$((10#${h12}))" -eq 0 ]; then + local h12=$((hour % 12)) + if [ "${h12}" -eq 0 ]; then h12=12 fi - printf "%d:%02d%s" "$((10#${h12}))" "$((10#${minute}))" "${ampm}" + printf "%d:%02d%s" "${h12}" "$((10#${minute}))" "${ampm}" } # Split "hour minute" output from parse_time_token_to_24h_hm (IFS-safe; use instead of read). @@ -374,15 +371,19 @@ normalize_time_range() { # Convert one range endpoint (HH or HH:MM) to minutes from midnight (0-1439). # Prints minutes on stdout; returns 1 if invalid. +# $2 is the role, "start" (default) or "end". A range may end at 24 or 24:00, +# which is midnight at the end of the day, so the "end" role also accepts it and +# prints 1440. daily-caps.sh reads a 24:00 end the same way. range_endpoint_to_day_minutes() { local part="$1" + local role="${2:-start}" local minutes_per_hour="${MINUTES_PER_HOUR:-60}" + local h m part=$(echo "${part}" | sed 's/^[[:space:]]*//;s/[[:space:]]*$//') [ -z "${part}" ] && return 1 if [[ "${part}" =~ : ]]; then - local h m h=$(echo "${part}" | cut -d: -f1) m=$(echo "${part}" | cut -d: -f2) if ! [[ "${h}" =~ ^[0-9]+$ ]] || ! [[ "${m}" =~ ^[0-9]+$ ]]; then @@ -390,21 +391,22 @@ range_endpoint_to_day_minutes() { fi h=$((10#${h})) m=$((10#${m})) - if [ "${h}" -lt 0 ] || [ "${h}" -gt 23 ] || [ "${m}" -lt 0 ] || [ "${m}" -gt 59 ]; then + else + if ! [[ "${part}" =~ ^[0-9]+$ ]]; then return 1 fi - echo $((h * minutes_per_hour + m)) - return 0 + h=$((10#${part})) + m=0 fi - if ! [[ "${part}" =~ ^[0-9]+$ ]]; then - return 1 + if [ "${role}" = "end" ] && [ "${h}" -eq 24 ] && [ "${m}" -eq 0 ]; then + echo $((24 * minutes_per_hour)) + return 0 fi - local h=$((10#${part})) - if [ "${h}" -lt 0 ] || [ "${h}" -gt 23 ]; then + if [ "${h}" -lt 0 ] || [ "${h}" -gt 23 ] || [ "${m}" -lt 0 ] || [ "${m}" -gt 59 ]; then return 1 fi - echo $((h * minutes_per_hour)) + echo $((h * minutes_per_hour + m)) return 0 } diff --git a/tests/test-config-locked-message.sh b/tests/test-config-locked-message.sh new file mode 100644 index 0000000..fb9668b --- /dev/null +++ b/tests/test-config-locked-message.sh @@ -0,0 +1,115 @@ +#!/bin/bash + +############################################################################### +# Tests the text show_config_locked_error prints when configure is locked: +# the active range, the end time, and which day the lock ends on. Covers +# integer-hour and HH:MM configs, an overnight tail from the previous day, and +# ranges that end at 24 (midnight at the end of the day). +# Runs headless with a temp HOME and a stubbed clock. Text mode only. +############################################################################### + +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" + +TMP_HOME="$(mktemp -d)" +trap 'rm -rf "${TMP_HOME}"' EXIT +export HOME="${TMP_HOME}" + +# shellcheck disable=SC1091 +source "${SCRIPT_DIR}/lib/configure-core.sh" || exit 1 +# shellcheck disable=SC1091 +source "${SCRIPT_DIR}/lib/config.sh" || exit 1 +# shellcheck disable=SC1091 +source "${SCRIPT_DIR}/lib/time-parsing.sh" || exit 1 +# shellcheck disable=SC1091 +source "${SCRIPT_DIR}/lib/time-validation.sh" || exit 1 +# shellcheck disable=SC1091 +source "${SCRIPT_DIR}/lib/time-calculations.sh" || exit 1 +# shellcheck disable=SC1091 +source "${SCRIPT_DIR}/lib/configure-time.sh" || exit 1 +# shellcheck disable=SC1091 +source "${SCRIPT_DIR}/lib/configure-ui-errors.sh" || exit 1 + +CONFIG_FILE="${TMP_HOME}/sleep-warning.conf" + +FAKE_DOW=1 +FAKE_HOUR=3 +date() { + case "$1" in + +%w) echo "${FAKE_DOW}" ;; + +%H) printf '%02d\n' "${FAKE_HOUR}" ;; + +%H%M) printf '%02d00\n' "${FAKE_HOUR}" ;; + *) command date "$@" ;; + esac +} + +_PASS=0 +_FAIL=0 + +check_line() { + local desc="$1" expected="$2" output="$3" + if printf '%s\n' "${output}" | grep -Fqx -- "${expected}"; then + _PASS=$(( _PASS + 1 )); echo " PASS ${desc} (${expected})" + else + _FAIL=$(( _FAIL + 1 )); echo " FAIL ${desc}: no line '${expected}' in:" + printf '%s\n' "${output}" | sed 's/^/ /' + fi +} + +write_config() { + printf '%s\n' "SUNDAY=" "MONDAY=" "TUESDAY=" "WEDNESDAY=" "THURSDAY=" "FRIDAY=" "SATURDAY=" "$@" > "${CONFIG_FILE}" +} + +# message DAY HOUR: the text-mode error for that clock. +message() { + FAKE_DOW="$1" + FAKE_HOUR="$2" + show_config_locked_error false Configuration 2>&1 +} + +echo "=== same-day restriction ===" +write_config "MONDAY=0-5" +out="$(message 1 3)" +check_line "headline names the restriction" "Error: Configuration is locked during restricted hours." "${out}" +check_line "active range" "Active restriction: 12:00am -> 5:00am" "${out}" +check_line "ends today at 5:00 AM" "Lock ends: 5:00 AM today" "${out}" + +echo "=== HH:MM config ===" +write_config "MONDAY=00:00-05:30" +out="$(message 1 3)" +check_line "end time keeps its minutes" "Lock ends: 5:30 AM today" "${out}" + +echo "=== overnight range, started yesterday ===" +write_config "TUESDAY=22-6" +out="$(message 3 3)" +check_line "the tail is active on Wednesday" "Current time: Wednesday, 3:00 AM" "${out}" +check_line "ends Wednesday morning" "Lock ends: 6:00 AM on Wednesday" "${out}" +write_config "TUESDAY=22:00-06:30" +out="$(message 3 3)" +check_line "the tail with minutes" "Lock ends: 6:30 AM on Wednesday" "${out}" + +echo "=== overnight range, started today ===" +write_config "TUESDAY=22-6" +out="$(message 2 23)" +check_line "ends tomorrow" "Lock ends: 6:00 AM on Wednesday" "${out}" + +echo "=== range ending at 24 ===" +write_config "MONDAY=16-24" +out="$(message 1 17)" +check_line "the range reads as ending at midnight" "Active restriction: 4:00pm -> 12:00am" "${out}" +check_line "the lock ends at midnight, on the next day" "Lock ends: 12:00 AM on Tuesday" "${out}" +write_config "MONDAY=16:00-24:00" +out="$(message 1 23)" +check_line "the HH:MM form agrees" "Lock ends: 12:00 AM on Tuesday" "${out}" +write_config "SATURDAY=16-24" +out="$(message 6 20)" +check_line "Saturday's midnight is Sunday" "Lock ends: 12:00 AM on Sunday" "${out}" + +echo "=== lock hours only ===" +write_config "CONFIG_LOCK_HOURS_MONDAY=1-4" +out="$(message 1 2)" +check_line "headline names the lock hours" "Error: Configuration is locked during configured lock hours." "${out}" +check_line "lock ends today" "Lock ends: 4:00 AM today" "${out}" + +echo "" +echo "Results: ${_PASS} passed, ${_FAIL} failed" +[ "${_FAIL}" -eq 0 ] || exit 1 diff --git a/tests/test-resolution-detection-stubbed.sh b/tests/test-resolution-detection-stubbed.sh index e5a0a50..a23b54d 100755 --- a/tests/test-resolution-detection-stubbed.sh +++ b/tests/test-resolution-detection-stubbed.sh @@ -85,9 +85,11 @@ unstub_all export WAYLAND_DISPLAY="wayland-0" stub wlr-randr '[{"width": 1600, "height": 900}]' check "a Wayland session with WAYLAND_DISPLAY set" "1600 900" "$(detect)" -# Known gap, tracked in dev-docs/TO_DO.md: with XDG_SESSION_TYPE unset, -# WAYLAND_DISPLAY alone does not select Wayland detection, because -# `A || B && C` evaluates as `(A || B) && C`. This file does not assert that. +unset XDG_SESSION_TYPE +check "WAYLAND_DISPLAY alone selects Wayland detection" "1600 900" "$(detect)" +export XDG_SESSION_TYPE="tty" +check "WAYLAND_DISPLAY wins over a tty session type" "1600 900" "$(detect)" +unset XDG_SESSION_TYPE stub wlr-randr '[{"width": 100, "height": 100}]' detect >/dev/null; check "no Wayland tool gives a usable size" "1" "$?" unset WAYLAND_DISPLAY XDG_SESSION_TYPE diff --git a/tests/test-status-display.sh b/tests/test-status-display.sh index 1c78cba..9c5d91f 100755 --- a/tests/test-status-display.sh +++ b/tests/test-status-display.sh @@ -89,6 +89,34 @@ else pass=$((pass + 1)) fi +assert_equals() { + local label="$1" + local expected="$2" + local actual="$3" + if [ "${actual}" = "${expected}" ]; then + echo "PASS: ${label}" + pass=$((pass + 1)) + else + echo "FAIL: ${label} (expected '${expected}', got '${actual}')" + fail=$((fail + 1)) + fi +} + +# format_hour_12h in this module takes "HH[:MM]". 24:00 is midnight at the end +# of the day, so it must read as AM, not noon. +assert_equals "format_hour_12h midnight" "12:00 AM" "$(format_hour_12h 0)" +assert_equals "format_hour_12h 5:30" "5:30 AM" "$(format_hour_12h 05:30)" +assert_equals "format_hour_12h noon" "12:00 PM" "$(format_hour_12h 12)" +assert_equals "format_hour_12h 4 PM" "4:00 PM" "$(format_hour_12h 16:00)" +assert_equals "format_hour_12h 23:59" "11:59 PM" "$(format_hour_12h 23:59)" +assert_equals "format_hour_12h 24:00 is midnight" "12:00 AM" "$(format_hour_12h 24:00)" +assert_equals "format_hour_12h 24 is midnight" "12:00 AM" "$(format_hour_12h 24)" +assert_equals "format_hour_12h leaves a non-numeric value alone" "soon" "$(format_hour_12h soon)" +assert_equals "format_range_human range ending at 24:00 is not overnight" \ + "4:00 PM → 12:00 AM" "$(format_range_human "16:00-24:00" Monday)" +assert_equals "format_range_human overnight range" \ + "10:00 PM → 6:00 AM (overnight)" "$(format_range_human "22:00-06:00" Monday)" + echo "" echo "Results: ${pass} passed, ${fail} failed" [ "${fail}" -eq 0 ] diff --git a/tests/test-time-calculations.sh b/tests/test-time-calculations.sh index c30b74c..5013128 100644 --- a/tests/test-time-calculations.sh +++ b/tests/test-time-calculations.sh @@ -80,6 +80,19 @@ all_days_empty "SATURDAY=22-6" check "Sunday looks back at Saturday" "6" "$(calculate_total_day_duration 0 "")" check "partial hours round up" "2" "$(calculate_total_day_duration 1 "00:00-01:30")" +echo "=== range_endpoint_to_day_minutes ===" +check "start role: plain hour" "300" "$(range_endpoint_to_day_minutes 5)" +check "start role: HH:MM" "330" "$(range_endpoint_to_day_minutes 05:30)" +range_endpoint_to_day_minutes 24 >/dev/null; check "start role rejects 24" "1" "$?" +range_endpoint_to_day_minutes 24:00 start >/dev/null; check "start role rejects 24:00" "1" "$?" +check "end role accepts 24" "1440" "$(range_endpoint_to_day_minutes 24 end)" +check "end role accepts 24:00" "1440" "$(range_endpoint_to_day_minutes 24:00 end)" +range_endpoint_to_day_minutes 24:30 end >/dev/null; check "end role rejects 24:30" "1" "$?" +range_endpoint_to_day_minutes 25 end >/dev/null; check "end role rejects 25" "1" "$?" +check "end role still reads ordinary times" "1380" "$(range_endpoint_to_day_minutes 23 end)" +range_endpoint_to_day_minutes "" >/dev/null; check "empty is invalid" "1" "$?" +range_endpoint_to_day_minutes 7:75 >/dev/null; check "minutes over 59 are invalid" "1" "$?" + echo "=== build_weekly_restricted_intervals ===" all_days_empty check "no restrictions prints a single blank line" "" "$(build_weekly_restricted_intervals)" @@ -95,6 +108,16 @@ all_days_empty "MONDAY=07:30-08:45" check "HH:MM endpoints" "1890:1965" "$(build_weekly_restricted_intervals)" all_days_empty "MONDAY=bogus-range" check "an unparseable range is skipped" "" "$(build_weekly_restricted_intervals)" +all_days_empty "TUESDAY=16-24" +check "a range ending at 24 runs to the end of the day" "3840:4320" "$(build_weekly_restricted_intervals)" +all_days_empty "TUESDAY=16:00-24:00" +check "a range ending at 24:00 runs to the end of the day" "3840:4320" "$(build_weekly_restricted_intervals)" +all_days_empty "TUESDAY=0-24" +check "0-24 covers the whole day" "2880:4320" "$(build_weekly_restricted_intervals)" +all_days_empty "SATURDAY=16-24" +check "Saturday 16-24 ends at the end of the week" "9600:10080" "$(build_weekly_restricted_intervals)" +all_days_empty "TUESDAY=24-5" +check "24 is not a valid start" "" "$(build_weekly_restricted_intervals)" echo "=== get_current_minute_offset ===" date() { @@ -149,27 +172,63 @@ check "already unrestricted gives the current minute" "in 0 minutes" "$(get_next all_days_empty "MONDAY=22-6" FAKE_OFFSET=$((1440 + 23 * 60)) check "an overnight restriction ends tomorrow" "Tomorrow at 6:00 AM" "$(get_next_unrestricted_time)" +all_days_empty "MONDAY=16-24" +FAKE_OFFSET=$((1440 + 17 * 60)) +check "a range ending at 24 ends at midnight, tomorrow" "Tomorrow at 12:00 AM" "$(get_next_unrestricted_time)" +all_days_empty "MONDAY=16-24" "TUESDAY=0-5" +check "back-to-back ranges across midnight are one block" "Tomorrow at 5:00 AM" "$(get_next_unrestricted_time)" +all_days_empty "SATURDAY=16-24" "SUNDAY=0-6" +FAKE_OFFSET=$((6 * 1440 + 20 * 60)) +check "a range ending at the week boundary continues into Sunday" "Tomorrow at 6:00 AM" "$(get_next_unrestricted_time)" +all_days_empty "SATURDAY=22-6" +FAKE_OFFSET=$((6 * 1440 + 23 * 60 + 30)) +check "a Saturday overnight range wraps to Sunday morning" "Tomorrow at 6:00 AM" "$(get_next_unrestricted_time)" full_day="00:00-23:59,23:59-00:00" all_days_empty "SUNDAY=${full_day}" "MONDAY=${full_day}" "TUESDAY=${full_day}" "WEDNESDAY=${full_day}" "THURSDAY=${full_day}" "FRIDAY=${full_day}" "SATURDAY=${full_day}" FAKE_OFFSET=100 check "24/7 restrictions never end" "No unrestricted time scheduled (24/7 restrictions active)" "$(get_next_unrestricted_time)" +echo "=== format_endpoint_12h ===" +check "bare hour matches format_hour_12h" "5:00 AM" "$(format_endpoint_12h 5)" +check "zero-padded hour" "5:00 AM" "$(format_endpoint_12h 05)" +check "HH:MM keeps its minutes" "5:30 AM" "$(format_endpoint_12h 05:30)" +check "midnight" "12:00 AM" "$(format_endpoint_12h 0)" +check "noon" "12:00 PM" "$(format_endpoint_12h 12)" +check "12:15 PM" "12:15 PM" "$(format_endpoint_12h 12:15)" +check "afternoon" "4:45 PM" "$(format_endpoint_12h 16:45)" +check "23:59" "11:59 PM" "$(format_endpoint_12h 23:59)" +check "24 is midnight at the end of the day" "12:00 AM" "$(format_endpoint_12h 24)" +check "24:00 is midnight at the end of the day" "12:00 AM" "$(format_endpoint_12h 24:00)" +check "whitespace is ignored" "6:00 AM" "$(format_endpoint_12h " 06:00 ")" +for bad in 25 24:30 7:60 abc "" 5:5 -1; do + out="$(format_endpoint_12h "${bad}")"; rc=$? + check "invalid endpoint '${bad}' returns 1 and prints nothing" "1|" "${rc}|${out}" +done + echo "=== calculate_restriction_end_time ===" -# Known gap, tracked in dev-docs/TO_DO.md: parse_config returns HH:MM-HH:MM, and -# format_hour_12h rejects "05:00", so the end time comes back blank for those -# ranges and for an overnight tail from the previous day. These cases pass -# integer-hour ranges, which work. all_days_empty "MONDAY=0-5" "TUESDAY=22-6" check "inside a same-day range" "5:00 AM|Monday|false" "$(calculate_restriction_end_time 2 1 "0-5")" check "the last restricted hour" "5:00 AM|Monday|false" "$(calculate_restriction_end_time 4 1 "0-5")" +check "HH:MM range, as parse_config returns it" "5:00 AM|Monday|false" "$(calculate_restriction_end_time 2 1 "$(parse_config 1)")" +check "HH:MM end with minutes" "5:30 AM|Monday|false" "$(calculate_restriction_end_time 2 1 "00:00-05:30")" check "outside every range" "" "$(calculate_restriction_end_time 12 1 "0-5")" calculate_restriction_end_time 12 1 "0-5" >/dev/null check "outside every range returns failure" "1" "$?" calculate_restriction_end_time 12 3 "" >/dev/null check "no ranges for today returns failure" "1" "$?" check "inside an overnight range, ends tomorrow" "6:00 AM|Wednesday|true" "$(calculate_restriction_end_time 23 2 "22-6")" +check "inside an overnight HH:MM range" "6:00 AM|Wednesday|true" "$(calculate_restriction_end_time 23 2 "$(parse_config 2)")" check "the second of two ranges" "11:00 PM|Monday|false" "$(calculate_restriction_end_time 21 1 "0-5,20-23")" check "Saturday overnight ends on Sunday" "6:00 AM|Sunday|true" "$(calculate_restriction_end_time 23 6 "22-6")" +check "a range ending at 24 ends at midnight on the next day" "12:00 AM|Tuesday|true" "$(calculate_restriction_end_time 17 1 "16-24")" +check "the same range as HH:MM" "12:00 AM|Tuesday|true" "$(calculate_restriction_end_time 17 1 "16:00-24:00")" +check "Saturday 16-24 ends on Sunday" "12:00 AM|Sunday|true" "$(calculate_restriction_end_time 17 6 "16-24")" +all_days_empty "MONDAY=22-6" +check "yesterday's overnight tail ends today" "6:00 AM|Tuesday|true" "$(calculate_restriction_end_time 3 2 "")" +all_days_empty "SATURDAY=22:00-06:30" +check "Sunday looks back at Saturday's tail, with minutes" "6:30 AM|Sunday|true" "$(calculate_restriction_end_time 3 0 "")" +all_days_empty "MONDAY=22-6" "TUESDAY=0-3" +check "the tail wins over today's own ranges" "6:00 AM|Tuesday|true" "$(calculate_restriction_end_time 1 2 "0-3")" echo "" echo "Results: ${_PASS} passed, ${_FAIL} failed" diff --git a/tests/tiers.yaml b/tests/tiers.yaml index b3488fc..aa46d55 100644 --- a/tests/tiers.yaml +++ b/tests/tiers.yaml @@ -52,6 +52,7 @@ tier_a: - test-popup-size-config.sh - test-config-safety.sh - test-time-calculations.sh + - test-config-locked-message.sh - test-scheduler-platform.sh - test-resolution-detection-stubbed.sh - test-atomic-write.sh