Skip to content

Commit

Permalink
Fix inaccurate notifications issue
Browse files Browse the repository at this point in the history
The `pomo notify` command uses the the `pomo_msg` bash function which
sleeps until the current cycle portion should end. Upon waking up, it
checks `pomo_stat` for the current pomodoro state. In order to get an
accurate state, `pomo_update` needs to be called first because the
pomodoro timer may have been restarted while `pomo_msg` was sleeping.
  • Loading branch information
stelcodes committed May 18, 2023
1 parent 5a1e3f9 commit 56c9f83
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 10 deletions.
1 change: 1 addition & 0 deletions pomo.sh
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ function pomo_msg {
# stat >= running + left (haven't updated timestamp) or
# stat < running (have just updated the timestamp from a
# separate pomo call, e.g. using pomo status).
pomo_update
stat=$(pomo_stat)
[[ $stat -ge $(( running + left )) ]] && break
$work || { [[ $stat -lt $running ]] && break; }
Expand Down
11 changes: 1 addition & 10 deletions tests/pomo_test.bats
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ function _pomo_msg_helper() {
expected_duration=$1
# duplicate first fake stat time as pomo_msg calls pomo_update once at initialisation.
stat_times=( "$2" "${@:2}" )
stat_times=( "$2" "$2" "$3" "$3" "$4" "$4" )
# Set iteration in a file as pomo_msg is run by BATS in a subshell.
iteration_file=$(mktemp --tmpdir="$BATS_RUN_TMPDIR")
echo 0 > "$iteration_file"
Expand Down Expand Up @@ -212,16 +213,6 @@ function notify-send() {
[[ "$output" == "Pomodoro End of a break period. Time for work!" ]]
}

@test "pomo_msg handles timestamp update before it can send the end of break message 1" {
_pomo_msg_helper 5 $(( (WORK_TIME+BREAK_TIME)*60-5)) 0
[[ "$output" == "Pomodoro End of a break period. Time for work!" ]]
}

@test "pomo_msg handles timestamp update before it can send the end of break message 2" {
_pomo_msg_helper 5 $(( (WORK_TIME+BREAK_TIME)*60-5)) 1
[[ "$output" == "Pomodoro End of a break period. Time for work!" ]]
}

@test "pomo_msg handles being paused" {
_pomo_msg_helper 7 $((WORK_TIME*60-5)) $((WORK_TIME*60-2)) $((WORK_TIME*60))
[[ "$output" == "Pomodoro End of a work period. Time for a break!" ]]
Expand Down

0 comments on commit 56c9f83

Please sign in to comment.