feat(data): a transactional alarm edit, and the audio source policy
The editor and the engine write to the same row from different threads, so an edit is a transaction: read, transform, write back, with the id and the creation stamp forced from the stored row. Without it the editor would re-enable an alarm the engine had just resolved as disabled, or move a row's history. `AudioSourcePolicy` is where the silent sentinel becomes an empty source list — the audio player then fails to start by design, and the ring falls through to vibration instead of to nothing. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Wmy1BpCKi8KeSjaWhYuCPV
This commit is contained in:
1 parent
52204d3e9c
commit
8002651e3f
5 files changed
+118
-10
No files matched your search
@@ -5,6 +5,7 @@ import de.jeanlucmakiola.clockula.domain.Alarm
|
||||
import de.jeanlucmakiola.clockula.domain.AlarmDraft
|
||||
import de.jeanlucmakiola.clockula.domain.DismissChallenge
|
||||
import de.jeanlucmakiola.clockula.domain.RepeatDays
|
||||
import de.jeanlucmakiola.clockula.domain.Ringtones
|
||||
import de.jeanlucmakiola.clockula.domain.TimeOfDay
|
||||
import de.jeanlucmakiola.clockula.testing.T0
|
||||
import de.jeanlucmakiola.clockula.testing.fullAlarm
|
||||
@@ -124,4 +125,12 @@ class AlarmMapperTest {
|
||||
),
|
||||
)
|
||||
}
|
||||
@Test
|
||||
fun `the silent sentinel round-trips, because blank-to-null does not eat it`() {
|
||||
val entity = fullAlarmEntity().copy(ringtoneUri = Ringtones.SILENT_URI)
|
||||
|
||||
val roundTripped = AlarmMapper.toEntity(AlarmMapper.toDomain(entity))
|
||||
|
||||
assertThat(roundTripped.ringtoneUri).isEqualTo(Ringtones.SILENT_URI)
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,7 @@ package de.jeanlucmakiola.clockula.data.alarms
|
||||
import app.cash.turbine.test
|
||||
import com.google.common.truth.Truth.assertThat
|
||||
import de.jeanlucmakiola.clockula.domain.AlarmDraft
|
||||
import de.jeanlucmakiola.clockula.domain.Ringtones
|
||||
import de.jeanlucmakiola.clockula.domain.TimeOfDay
|
||||
import de.jeanlucmakiola.clockula.testing.FakeAlarmDao
|
||||
import de.jeanlucmakiola.clockula.testing.FakeWallClock
|
||||
@@ -160,4 +161,71 @@ class AlarmRepositoryTest {
|
||||
cancel()
|
||||
}
|
||||
}
|
||||
// --- M5: the transactional read-modify-write (D25) ---
|
||||
|
||||
@Test
|
||||
fun `editing changes the one field the transform touched`() = runTest {
|
||||
val repository = repository(FakeAlarmDao())
|
||||
val id = repository.create(AlarmDraft(TimeOfDay(7, 0)))
|
||||
val original = repository.find(id)!!
|
||||
|
||||
repository.edit(id) { it.copy(label = "Gym") }
|
||||
|
||||
assertThat(repository.find(id)).isEqualTo(original.copy(label = "Gym"))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `editing stamps the wall clock's current value`() = runTest {
|
||||
val repository = repository(FakeAlarmDao())
|
||||
val id = repository.create(AlarmDraft(TimeOfDay(7, 0)))
|
||||
|
||||
wallClock.advance(5.minutes)
|
||||
repository.edit(id) { it.copy(label = "Gym") }
|
||||
|
||||
assertThat(repository.find(id)!!.updatedAt).isEqualTo(T0 + 5.minutes)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `editing an id no alarm has writes nothing and throws nothing`() = runTest {
|
||||
val dao = FakeAlarmDao()
|
||||
val repository = repository(dao)
|
||||
repository.create(AlarmDraft(TimeOfDay(7, 0)))
|
||||
val before = dao.stored
|
||||
|
||||
repository.edit(999L) { it.copy(label = "Gym") }
|
||||
|
||||
assertThat(dao.stored).isEqualTo(before)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `a transform cannot move a row to another id`() = runTest {
|
||||
val repository = repository(FakeAlarmDao())
|
||||
val id = repository.create(AlarmDraft(TimeOfDay(7, 0)))
|
||||
|
||||
repository.edit(id) { it.copy(id = id + 100L, label = "Gym") }
|
||||
|
||||
assertThat(repository.find(id)!!.label).isEqualTo("Gym")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `two edits to different fields both survive`() = runTest {
|
||||
val repository = repository(FakeAlarmDao())
|
||||
val id = repository.create(AlarmDraft(TimeOfDay(7, 0)))
|
||||
|
||||
repository.edit(id) { it.copy(snoozeLimit = 1) }
|
||||
repository.edit(id) { it.copy(time = TimeOfDay(6, 45)) }
|
||||
|
||||
val alarm = repository.find(id)!!
|
||||
assertThat(alarm.snoozeLimit to alarm.time).isEqualTo(1 to TimeOfDay(6, 45))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `the silent sentinel survives a create and an edit`() = runTest {
|
||||
val repository = repository(FakeAlarmDao())
|
||||
val id = repository.create(AlarmDraft(TimeOfDay(7, 0), ringtoneUri = Ringtones.SILENT_URI))
|
||||
|
||||
repository.edit(id) { it.copy(label = "Gym") }
|
||||
|
||||
assertThat(repository.find(id)!!.ringtoneUri).isEqualTo(Ringtones.SILENT_URI)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user