91 lines · 4.3 KB · KotlinRaw
package com.cottondb.android.platformimport androidx.test.ext.junit.runners.AndroidJUnit4import androidx.test.platform.app.InstrumentationRegistryimport com.cottondb.android.reminders.DesiredReminderimport com.cottondb.android.reminders.NotificationKindimport java.io.Fileimport java.util.UUIDimport kotlinx.coroutines.runBlockingimport kotlinx.coroutines.asyncimport kotlinx.coroutines.Dispatchersimport kotlinx.coroutines.CompletableDeferredimport kotlinx.coroutines.withTimeoutimport org.junit.Afterimport org.junit.Assert.assertEqualsimport org.junit.Assert.assertTrueimport org.junit.Testimport org.junit.runner.RunWith@RunWith(AndroidJUnit4::class)class AndroidReminderCentreTest { private val root = File(InstrumentationRegistry.getInstrumentation().targetContext.noBackupFilesDir, "reminder-platform-test-${UUID.randomUUID()}") @After fun cleanUp() { root.deleteRecursively() } @Test fun scheduleCancelAndDeliveryUseDurableRows() = runBlocking { val platform = FakePlatform() val ledger = ReminderLedger(File(root, "pending.json")) val centre = AndroidReminderCentre.withBoundary(ledger, platform) val first = reminder("first") val second = reminder("second") assertEquals(2, centre.schedule(listOf(first, second))) assertEquals(listOf("first", "second"), centre.heldIdentifiers().sorted()) centre.cancel(listOf("first")) centre.deliver("first") centre.deliver("second") assertEquals(listOf("first"), platform.cancelled) assertEquals(listOf("second"), platform.posted.map { it.identifier }) assertTrue(centre.heldIdentifiers().isEmpty()) } @Test fun invalidInstantAndPlatformFailureDoNotClaimALedgerRow() = runBlocking { val platform = FakePlatform(fail = true) val centre = AndroidReminderCentre.withBoundary(ReminderLedger(File(root, "pending.json")), platform) val invalid = reminder("invalid").copy(at = "not-an-instant") assertEquals(0, centre.schedule(listOf(invalid, reminder("fails")))) assertTrue(centre.heldIdentifiers().isEmpty()) } @Test fun recreatedCentreReadsLedgerAndBootClearsItBeforeReconcile() = runBlocking { val ledgerFile = File(root, "pending.json") val first = AndroidReminderCentre.withBoundary(ReminderLedger(ledgerFile), FakePlatform()) first.schedule(listOf(reminder("durable"))) val recreated = AndroidReminderCentre.withBoundary(ReminderLedger(ledgerFile), FakePlatform()) assertEquals(listOf("durable"), recreated.heldIdentifiers()) recreated.clearAfterBoot() assertTrue(recreated.heldIdentifiers().isEmpty()) } @Test fun cancellationWaitsForAnInFlightScheduleThenWinsTheLedgerAndPlatform() = runBlocking { val entered = CompletableDeferred<Unit>() val release = CompletableDeferred<Unit>() val platform = object : ReminderPlatform { val cancelled = mutableListOf<String>() override fun schedule(row: PendingReminder) { entered.complete(Unit); runBlocking { release.await() } } override fun cancel(identifier: String) { cancelled += identifier } override fun post(row: PendingReminder) = Unit } val centre = AndroidReminderCentre.withBoundary(ReminderLedger(File(root, "pending.json")), platform) val scheduling = async(Dispatchers.Default) { centre.schedule(listOf(reminder("race"))) } entered.await() val cancelling = async(Dispatchers.Default) { centre.cancel(listOf("race")) } assertTrue(!cancelling.isCompleted) release.complete(Unit) scheduling.await() withTimeout(2_000) { cancelling.await() } assertEquals(listOf("race"), platform.cancelled) assertTrue(centre.heldIdentifiers().isEmpty()) } private fun reminder(id: String) = DesiredReminder(id, NotificationKind.Todo, "record-$id", "2026-09-23T09:00:00Z", "Title $id", null) private class FakePlatform(private val fail: Boolean = false) : ReminderPlatform { val cancelled = mutableListOf<String>(); val posted = mutableListOf<PendingReminder>() override fun schedule(row: PendingReminder) { if (fail) error("platform refusal") } override fun cancel(identifier: String) { cancelled += identifier } override fun post(row: PendingReminder) { posted += row } }}