feat(prefs): clock defaults and the stopwatch run record in DataStore
Alarm and timer defaults, world-clock preferences, and the stopwatch's running state — the things that are settings rather than rows. The run record is read and written as a whole in one DataStore transaction, so a reader never sees a half-applied record and a process killed mid-write cannot persist one. Values are clamped on read: a preferences file someone has edited by hand degrades to the default instead of propagating nonsense upwards. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
1 parent
1d85faa54e
commit
11b220aba0
6 files changed
+503
-2
No files matched your search
@@ -0,0 +1,154 @@
|
||||
package de.jeanlucmakiola.clockula.data.prefs
|
||||
|
||||
import androidx.datastore.core.DataStore
|
||||
import androidx.datastore.preferences.core.PreferenceDataStoreFactory
|
||||
import androidx.datastore.preferences.core.Preferences
|
||||
import androidx.datastore.preferences.core.intPreferencesKey
|
||||
import androidx.datastore.preferences.core.stringPreferencesKey
|
||||
import app.cash.turbine.test
|
||||
import com.google.common.truth.Truth.assertThat
|
||||
import de.jeanlucmakiola.clockula.domain.ClockDefaults
|
||||
import de.jeanlucmakiola.clockula.domain.DismissChallenge
|
||||
import de.jeanlucmakiola.floret.prefs.PrefStore
|
||||
import de.jeanlucmakiola.floret.prefs.ThemeMode
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Job
|
||||
import kotlinx.coroutines.flow.first
|
||||
import kotlinx.coroutines.test.TestScope
|
||||
import kotlinx.coroutines.test.UnconfinedTestDispatcher
|
||||
import kotlinx.coroutines.test.runTest
|
||||
import org.junit.jupiter.api.Test
|
||||
import org.junit.jupiter.api.io.TempDir
|
||||
import java.nio.file.Path
|
||||
import kotlin.time.Duration.Companion.seconds
|
||||
|
||||
/**
|
||||
* The clock defaults every alarm and timer inherits. A value the user could
|
||||
* never choose must not come back out of a hand-edited or restored file, so
|
||||
* both writes and reads clamp.
|
||||
*/
|
||||
class ClockPrefsTest {
|
||||
|
||||
private fun TestScope.newDataStore(tempDir: Path): DataStore<Preferences> =
|
||||
PreferenceDataStoreFactory.create(
|
||||
scope = CoroutineScope(UnconfinedTestDispatcher(testScheduler) + Job()),
|
||||
produceFile = { tempDir.resolve("clockula_prefs_test.preferences_pb").toFile() },
|
||||
)
|
||||
|
||||
@Test
|
||||
fun `a fresh install reads the documented defaults`(@TempDir tempDir: Path) = runTest {
|
||||
val prefs = SettingsPrefs(PrefStore(newDataStore(tempDir)))
|
||||
|
||||
assertThat(prefs.defaults.first()).isEqualTo(ClockDefaults())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `every default round-trips`(@TempDir tempDir: Path) = runTest {
|
||||
val prefs = SettingsPrefs(PrefStore(newDataStore(tempDir)))
|
||||
|
||||
prefs.setSnoozeMinutes(5)
|
||||
prefs.setSnoozeLimit(1)
|
||||
prefs.setVibrate(false)
|
||||
prefs.setVolumeRampSeconds(0)
|
||||
prefs.setAlarmRingtoneUri("content://a")
|
||||
prefs.setTimerRingtoneUri("content://t")
|
||||
prefs.setDismissChallenge(DismissChallenge.HOLD)
|
||||
prefs.setTimerDuration(90.seconds)
|
||||
prefs.setHomeZoneId("Europe/Berlin")
|
||||
|
||||
assertThat(prefs.defaults.first()).isEqualTo(
|
||||
ClockDefaults(
|
||||
snoozeMinutes = 5,
|
||||
snoozeLimit = 1,
|
||||
vibrate = false,
|
||||
volumeRampSeconds = 0,
|
||||
alarmRingtoneUri = "content://a",
|
||||
timerRingtoneUri = "content://t",
|
||||
dismissChallenge = DismissChallenge.HOLD,
|
||||
timerDuration = 90.seconds,
|
||||
homeZoneId = "Europe/Berlin",
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `the defaults do not re-emit when the same value is written again`(@TempDir tempDir: Path) = runTest {
|
||||
val prefs = SettingsPrefs(PrefStore(newDataStore(tempDir)))
|
||||
|
||||
prefs.defaults.test {
|
||||
assertThat(awaitItem()).isEqualTo(ClockDefaults())
|
||||
|
||||
prefs.setSnoozeMinutes(ClockDefaults().snoozeMinutes)
|
||||
|
||||
expectNoEvents()
|
||||
cancel()
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `snooze minutes clamp on write`(@TempDir tempDir: Path) = runTest {
|
||||
val prefs = SettingsPrefs(PrefStore(newDataStore(tempDir)))
|
||||
|
||||
prefs.setSnoozeMinutes(0)
|
||||
assertThat(prefs.defaults.first().snoozeMinutes).isEqualTo(1)
|
||||
|
||||
prefs.setSnoozeMinutes(999)
|
||||
assertThat(prefs.defaults.first().snoozeMinutes).isEqualTo(60)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `a corrupt stored snooze value clamps on read`(@TempDir tempDir: Path) = runTest {
|
||||
val dataStore = newDataStore(tempDir)
|
||||
val prefs = SettingsPrefs(PrefStore(dataStore))
|
||||
|
||||
dataStore.updateData { stored ->
|
||||
stored.toMutablePreferences().apply { this[intPreferencesKey("default_snooze_minutes")] = -5 }
|
||||
}
|
||||
|
||||
assertThat(prefs.defaults.first().snoozeMinutes).isEqualTo(1)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `a corrupt dismiss challenge degrades to none`(@TempDir tempDir: Path) = runTest {
|
||||
val dataStore = newDataStore(tempDir)
|
||||
val prefs = SettingsPrefs(PrefStore(dataStore))
|
||||
|
||||
dataStore.updateData { stored ->
|
||||
stored.toMutablePreferences()
|
||||
.apply { this[stringPreferencesKey("default_dismiss_challenge")] = "TELEPORT" }
|
||||
}
|
||||
|
||||
assertThat(prefs.defaults.first().dismissChallenge).isEqualTo(DismissChallenge.NONE)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `a home zone the device no longer knows reads back as absent`(@TempDir tempDir: Path) = runTest {
|
||||
val dataStore = newDataStore(tempDir)
|
||||
val prefs = SettingsPrefs(PrefStore(dataStore))
|
||||
|
||||
dataStore.updateData { stored ->
|
||||
stored.toMutablePreferences().apply { this[stringPreferencesKey("home_zone_id")] = "Mars/Phobos" }
|
||||
}
|
||||
assertThat(prefs.defaults.first().homeZoneId).isNull()
|
||||
|
||||
prefs.setHomeZoneId(" Europe/Berlin ")
|
||||
assertThat(prefs.defaults.first().homeZoneId).isEqualTo("Europe/Berlin")
|
||||
|
||||
prefs.setHomeZoneId(null)
|
||||
assertThat(prefs.defaults.first().homeZoneId).isNull()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `writing a clock default does not disturb the appearance flows`(@TempDir tempDir: Path) = runTest {
|
||||
val prefs = SettingsPrefs(PrefStore(newDataStore(tempDir)))
|
||||
|
||||
prefs.themeMode.test {
|
||||
assertThat(awaitItem()).isEqualTo(ThemeMode.SYSTEM)
|
||||
|
||||
prefs.setSnoozeMinutes(5)
|
||||
|
||||
expectNoEvents()
|
||||
cancel()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,137 @@
|
||||
package de.jeanlucmakiola.clockula.data.prefs
|
||||
|
||||
import androidx.datastore.core.DataStore
|
||||
import androidx.datastore.preferences.core.PreferenceDataStoreFactory
|
||||
import androidx.datastore.preferences.core.Preferences
|
||||
import androidx.datastore.preferences.core.stringPreferencesKey
|
||||
import com.google.common.truth.Truth.assertThat
|
||||
import de.jeanlucmakiola.clockula.data.stopwatch.StopwatchStateStore
|
||||
import de.jeanlucmakiola.clockula.domain.StopwatchRun
|
||||
import de.jeanlucmakiola.clockula.domain.StopwatchState
|
||||
import de.jeanlucmakiola.floret.prefs.PrefStore
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Job
|
||||
import kotlinx.coroutines.flow.first
|
||||
import kotlinx.coroutines.flow.toList
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.test.TestScope
|
||||
import kotlinx.coroutines.test.UnconfinedTestDispatcher
|
||||
import kotlinx.coroutines.test.runTest
|
||||
import org.junit.jupiter.api.Test
|
||||
import org.junit.jupiter.api.io.TempDir
|
||||
import java.nio.file.Path
|
||||
import kotlin.time.Duration
|
||||
import kotlin.time.Duration.Companion.seconds
|
||||
|
||||
/**
|
||||
* The stopwatch's run record. It is monotonic time and nothing else — no key
|
||||
* here may hold a wall-clock instant.
|
||||
*/
|
||||
class StopwatchPrefsTest {
|
||||
|
||||
private fun TestScope.newDataStore(tempDir: Path): DataStore<Preferences> =
|
||||
PreferenceDataStoreFactory.create(
|
||||
scope = CoroutineScope(UnconfinedTestDispatcher(testScheduler) + Job()),
|
||||
produceFile = { tempDir.resolve("clockula_prefs_test.preferences_pb").toFile() },
|
||||
)
|
||||
|
||||
@Test
|
||||
fun `the stored run record holds no wall-clock value`(@TempDir tempDir: Path) = runTest {
|
||||
val dataStore = newDataStore(tempDir)
|
||||
val store = StopwatchStateStore(PrefStore(dataStore))
|
||||
|
||||
store.set(
|
||||
StopwatchRun(
|
||||
state = StopwatchState.RUNNING,
|
||||
accumulated = Duration.ZERO,
|
||||
startedAtElapsedRealtime = 1_000.seconds,
|
||||
),
|
||||
)
|
||||
|
||||
val snapshot = dataStore.data.first().asMap()
|
||||
assertThat(snapshot.values.filterIsInstance<Long>().filter { it >= 1_000_000_000_000L }).isEmpty()
|
||||
assertThat(snapshot.keys.map { it.name }).containsExactly(
|
||||
"stopwatch_state",
|
||||
"stopwatch_started_elapsed_millis",
|
||||
"stopwatch_accumulated_millis",
|
||||
"stopwatch_last_lap_cumulative_millis",
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `a fresh install is the default run`(@TempDir tempDir: Path) = runTest {
|
||||
val store = StopwatchStateStore(PrefStore(newDataStore(tempDir)))
|
||||
|
||||
assertThat(store.current()).isEqualTo(StopwatchRun())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `a full record round-trips`(@TempDir tempDir: Path) = runTest {
|
||||
val store = StopwatchStateStore(PrefStore(newDataStore(tempDir)))
|
||||
val run = StopwatchRun(
|
||||
state = StopwatchState.PAUSED,
|
||||
accumulated = 42.seconds,
|
||||
startedAtElapsedRealtime = null,
|
||||
lastLapCumulative = 30.seconds,
|
||||
)
|
||||
|
||||
store.set(run)
|
||||
|
||||
assertThat(store.current()).isEqualTo(run)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `an unknown stored state degrades to idle`(@TempDir tempDir: Path) = runTest {
|
||||
val dataStore = newDataStore(tempDir)
|
||||
val store = StopwatchStateStore(PrefStore(dataStore))
|
||||
|
||||
dataStore.updateData { stored ->
|
||||
stored.toMutablePreferences().apply { this[stringPreferencesKey("stopwatch_state")] = "SPINNING" }
|
||||
}
|
||||
|
||||
assertThat(store.current().state).isEqualTo(StopwatchState.IDLE)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `a whole record lands in a single emission`(@TempDir tempDir: Path) = runTest {
|
||||
val store = StopwatchStateStore(PrefStore(newDataStore(tempDir)))
|
||||
val seen = mutableListOf<StopwatchRun>()
|
||||
backgroundScope.launch(UnconfinedTestDispatcher(testScheduler)) { store.run.toList(seen) }
|
||||
|
||||
store.set(
|
||||
StopwatchRun(
|
||||
state = StopwatchState.RUNNING,
|
||||
accumulated = 42.seconds,
|
||||
startedAtElapsedRealtime = 1_000.seconds,
|
||||
lastLapCumulative = 30.seconds,
|
||||
),
|
||||
)
|
||||
|
||||
// Four keys, one transaction: a reader never sees RUNNING without its
|
||||
// anchor, or the new state beside the old accumulated total.
|
||||
assertThat(seen).containsExactly(
|
||||
StopwatchRun(),
|
||||
StopwatchRun(
|
||||
state = StopwatchState.RUNNING,
|
||||
accumulated = 42.seconds,
|
||||
startedAtElapsedRealtime = 1_000.seconds,
|
||||
lastLapCumulative = 30.seconds,
|
||||
),
|
||||
).inOrder()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `clearing the start anchor removes its key`(@TempDir tempDir: Path) = runTest {
|
||||
val dataStore = newDataStore(tempDir)
|
||||
val store = StopwatchStateStore(PrefStore(dataStore))
|
||||
store.set(
|
||||
StopwatchRun(state = StopwatchState.RUNNING, startedAtElapsedRealtime = 1_000.seconds),
|
||||
)
|
||||
|
||||
store.set(StopwatchRun(state = StopwatchState.PAUSED, startedAtElapsedRealtime = null))
|
||||
|
||||
assertThat(store.current().startedAtElapsedRealtime).isNull()
|
||||
assertThat(dataStore.data.first().asMap().keys.map { it.name })
|
||||
.doesNotContain("stopwatch_started_elapsed_millis")
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user