feat: check subcommand for firing off expired alarms

This commit is contained in:
Lewis Wynne 2026-04-02 00:29:35 +01:00
parent 9de60f23cc
commit c6b2148eea
2 changed files with 164 additions and 12 deletions

View file

@ -213,3 +213,94 @@ load test_helper
[[ "${_first_line}" =~ "standup" ]]
[[ "${_second_line}" =~ "take a break" ]]
}
# check #######################################################################
# Helper: write a raw alarm line directly to the alarms file.
write_alarm() {
mkdir -p "${NAG_DIR}"
printf "%s\\n" "$1" >> "${NAG_DIR}/alarms"
}
@test "check fires expired one-shot and removes it" {
local _past_ts=$(( $(date +%s) - 60 ))
write_alarm "$(printf "1\t%s\t\tpast alarm" "${_past_ts}")"
# Record what was fired.
local _fired="${NAG_DIR}/fired"
export NAG_CMD="${NAG_DIR}/recorder"
cat > "${NAG_CMD}" <<'SCRIPT'
#!/usr/bin/env bash
printf "%s\n" "$2" >> "${NAG_DIR}/fired"
SCRIPT
chmod +x "${NAG_CMD}"
run "${_NAG}" check
[ "${status}" -eq 0 ]
# Alarm should have been removed.
[[ ! -s "${NAG_DIR}/alarms" ]] || [ "$(wc -l < "${NAG_DIR}/alarms")" -eq 0 ]
# Notification should have fired.
grep -q "past alarm" "${_fired}"
}
@test "check reschedules expired repeating alarm" {
local _past_ts=$(( $(date +%s) - 60 ))
write_alarm "$(printf "1\t%s\tday\tdaily alarm" "${_past_ts}")"
run "${_NAG}" check
[ "${status}" -eq 0 ]
# Alarm should still exist with a future timestamp.
[ -s "${NAG_DIR}/alarms" ]
local _new_ts
_new_ts="$(cut -f2 "${NAG_DIR}/alarms" | head -1)"
local _now
_now="$(date +%s)"
(( _new_ts > _now ))
}
@test "check does not fire future alarms" {
local _future_ts=$(( $(date +%s) + 3600 ))
write_alarm "$(printf "1\t%s\t\tfuture alarm" "${_future_ts}")"
run "${_NAG}" check
[ "${status}" -eq 0 ]
# Alarm should still be there, unchanged.
[ -s "${NAG_DIR}/alarms" ]
grep -q "future alarm" "${NAG_DIR}/alarms"
}
@test "check fires all missed alarms" {
local _past1=$(( $(date +%s) - 120 ))
local _past2=$(( $(date +%s) - 60 ))
write_alarm "$(printf "1\t%s\t\tmissed one" "${_past1}")"
write_alarm "$(printf "2\t%s\t\tmissed two" "${_past2}")"
local _fired="${NAG_DIR}/fired"
export NAG_CMD="${NAG_DIR}/recorder"
cat > "${NAG_CMD}" <<'SCRIPT'
#!/usr/bin/env bash
printf "%s\n" "$2" >> "${NAG_DIR}/fired"
SCRIPT
chmod +x "${NAG_CMD}"
run "${_NAG}" check
[ "${status}" -eq 0 ]
# Both should have fired.
grep -q "missed one" "${_fired}"
grep -q "missed two" "${_fired}"
# Both should be removed (one-shots).
[[ ! -s "${NAG_DIR}/alarms" ]] || [ "$(wc -l < "${NAG_DIR}/alarms")" -eq 0 ]
}
@test "help check shows check usage" {
run "${_NAG}" help check
[ "${status}" -eq 0 ]
[[ "${output}" =~ "Usage:" ]]
[[ "${output}" =~ "check" ]]
}