fix(alarms): no fire for an occurrence already accounted for

A natural fire whose instant equals the skip or the handled-occurrence watermark is ignored, so a dismissal racing a reschedule cannot ring. Dismissing a repeating alarm persists its resolution before arming the skip, so an unspent old skip cannot absorb it. The deferred notice sync runs even if starting the ring throws, the swipe record is one synchronised read-modify-write, and it stays out of backups.

Fixes #16
This commit is contained in:
makiolaj committed 2026-10-21 20:26:50 +02:00
1 parent dedcbd5dfd
commit 1253c0fc55
8 files changed
+95 -25

No files matched your search

@@ -8,6 +8,7 @@ import de.jeanlucmakiola.clockula.domain.alarm.UpcomingNotices
import de.jeanlucmakiola.clockula.testing.RingEvent
import de.jeanlucmakiola.clockula.testing.alarmEngineHarness
import de.jeanlucmakiola.clockula.testing.offsetInstant
import de.jeanlucmakiola.clockula.testing.ringState
import kotlinx.coroutines.test.runTest
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.io.TempDir
@@ -207,4 +208,41 @@ class AlarmEngineUpcomingTest {
assertThat(harness.engine.dismissUpcomingOccurrence(1L, sixThirty)).isEqualTo(DismissOutcome.NotFound)
}
@Test
fun `a fire that lands after the dismissed skip was spent still does not ring`(@TempDir tempDir: Path) =
runTest {
val harness = alarmEngineHarness(tempDir)
harness.givenAlarm(id = 1L, hour = 6, minute = 30, repeatDays = RepeatDays.EVERY_DAY)
harness.engine.reschedule()
harness.engine.dismissUpcomingOccurrence(1L, sixThirty)
// Another entry point reschedules first and spends the skip.
harness.wallClock.instant = sixThirty
harness.engine.reschedule()
val outcome = harness.engine.onFire(1L, sixThirty)
assertThat(outcome).isEqualTo(FireOutcome.Ignored)
assertThat(harness.ring.events).isEmpty()
}
@Test
fun `an old skip gone by but not yet spent does not swallow the new dismissal`(@TempDir tempDir: Path) =
runTest {
val harness = alarmEngineHarness(tempDir)
harness.givenAlarm(
id = 1L,
hour = 6,
minute = 30,
repeatDays = RepeatDays.EVERY_DAY,
skipNextOccurrence = true,
)
// Yesterday's skip, never spent because nothing rescheduled since.
harness.states.save(ringState(alarmId = 1L, skippedOccurrence = offsetInstant("2026-06-09T06:30+02:00")))
val outcome = harness.engine.dismissUpcomingOccurrence(1L, sixThirty)
assertThat(outcome).isEqualTo(DismissOutcome.SkippedNext(1L))
assertThat(harness.scheduler.next?.fireAt).isEqualTo(sixThirtyTomorrow)
}
}