diff --git a/app/src/main/java/de/jeanlucmakiola/calendula/ui/common/EventMoveScope.kt b/app/src/main/java/de/jeanlucmakiola/calendula/ui/common/EventMoveScope.kt index 6eb9050f..ba46db11 100644 --- a/app/src/main/java/de/jeanlucmakiola/calendula/ui/common/EventMoveScope.kt +++ b/app/src/main/java/de/jeanlucmakiola/calendula/ui/common/EventMoveScope.kt @@ -63,8 +63,8 @@ private val moveTokens = AtomicLong() /** A fresh identity for one drop or undo, unique for the process. */ fun nextMoveToken(): Long = moveTokens.incrementAndGet() -/** How many ended drops [DropStates] remembers — far more than can be on screen at once. */ -private const val ENDED_KEPT = 32 +/** How many abandoned drops [DropStates] remembers — far more than can be on screen at once. */ +private const val ABANDONED_KEPT = 32 /** Per-drop progress, by token — see `RescheduleViewModel.drops`. */ @Immutable @@ -76,7 +76,10 @@ data class DropStates( fun ended(token: Long, landed: Boolean): DropStates = copy( pending = pending - token, - abandoned = if (landed) abandoned else (abandoned + token).toList().takeLast(ENDED_KEPT).toSet(), + // Insertion-ordered, so the oldest go first. + abandoned = if (landed) abandoned else (abandoned + token).let { kept -> + if (kept.size > ABANDONED_KEPT) kept.drop(kept.size - ABANDONED_KEPT).toSet() else kept + }, ) } diff --git a/app/src/main/java/de/jeanlucmakiola/calendula/ui/common/RescheduleViewModel.kt b/app/src/main/java/de/jeanlucmakiola/calendula/ui/common/RescheduleViewModel.kt index d40b0622..78ee7eb0 100644 --- a/app/src/main/java/de/jeanlucmakiola/calendula/ui/common/RescheduleViewModel.kt +++ b/app/src/main/java/de/jeanlucmakiola/calendula/ui/common/RescheduleViewModel.kt @@ -79,7 +79,7 @@ data class MoveRequest( val endMillis: Long, val target: MoveTarget, /** The drop's own identity, from [nextMoveToken], which the view holding its copy waits on. */ - val token: Long = nextMoveToken(), + val token: Long, ) /** @@ -122,8 +122,9 @@ sealed interface MoveOutcome { data object Gone : MoveOutcome /** - * A drop that waited for an earlier one found its occurrence no longer where - * it was picked up — the earlier write moved it — so it is not written (#494). + * The drop's occurrence is no longer where it was picked up — an earlier + * write moved it — and a recurring one can't be followed there, so it is not + * written (#494). */ data object Stale : MoveOutcome @@ -164,7 +165,7 @@ class RescheduleViewModel @Inject constructor( private val _inFlight = MutableStateFlow(false) - /** True while any write runs or waits, a queued drop included — what the confirmation chip holds for. */ + /** True while any drop or undo runs or waits — what the confirmation chip holds for. */ val inFlight: StateFlow = _inFlight.asStateFlow() private val _drops = MutableStateFlow(DropStates()) @@ -192,6 +193,13 @@ class RescheduleViewModel @Inject constructor( * written. Drops arriving in that window wait in [queue] and run in order. */ private var busy = false + set(value) { + field = value + _inFlight.value = value + } + + /** Whether a drop since the pipeline was last idle failed — see [report]. */ + private var failedThisRun = false /** Drops that arrived while another was in flight, oldest first (#494). */ private val queue = ArrayDeque() @@ -240,19 +248,14 @@ class RescheduleViewModel @Inject constructor( return false } _drops.update { it.started(request.token) } - if (busy) { - queue.addLast(request) - } else { - run(request, waited = false) - } - syncInFlight() + if (busy) queue.addLast(request) else run(request) return true } - private fun run(request: MoveRequest, waited: Boolean) { + private fun run(request: MoveRequest) { busy = true viewModelScope.launch { - val prepared = prepare(request, waited) + val prepared = prepare(request) if (prepared == null) { settle(request.token, landed = false) return@launch @@ -262,7 +265,7 @@ class RescheduleViewModel @Inject constructor( if (!prepared.isRecurring || (seriesOnly && prepared.canRealign)) { settle(request.token, write(prepared, RecurringWriteScope.AllEvents)) } else if (seriesOnly) { - _outcome.value = MoveOutcome.BlockedCancelledSeries + report(MoveOutcome.BlockedCancelledSeries) settle(request.token, landed = false) } else { // Still busy: the scope dialog is now the thing in flight. @@ -298,13 +301,26 @@ class RescheduleViewModel @Inject constructor( /** End the drop [token], then run the next queued one, if any. */ private fun settle(token: Long, landed: Boolean) { _drops.update { it.ended(token, landed) } - busy = false - queue.removeFirstOrNull()?.let { run(it, waited = true) } - syncInFlight() + // Straight on to the next, so busy never reads false between two drops. + val next = queue.removeFirstOrNull() + if (next != null) { + run(next) + } else { + busy = false + failedThisRun = false + } } - private fun syncInFlight() { - _inFlight.value = busy || queue.isNotEmpty() + /** + * Show [outcome] on the chip. While drops run back to back, a failure stays + * up rather than being replaced by a later drop's success: the grid shows + * what moved, but only the chip says what didn't. + */ + private fun report(outcome: MoveOutcome) { + val failure = outcome !is MoveOutcome.Moved && outcome !is MoveOutcome.Undone + if (!failure && failedThisRun) return + if (failure) failedThisRun = true + _outcome.value = outcome } /** @@ -316,11 +332,10 @@ class RescheduleViewModel @Inject constructor( * reports whether it landed. */ fun undo(undo: MoveUndo): Boolean { - if (busy || queue.isNotEmpty()) return false + if (busy) return false val token = nextMoveToken() _drops.update { it.started(token) } busy = true - syncInFlight() _undoStarted.value = token viewModelScope.launch { _outcome.value = try { @@ -345,25 +360,27 @@ class RescheduleViewModel @Inject constructor( _outcome.value = null } - private suspend fun prepare(request: MoveRequest, waited: Boolean): PreparedMove? { - val detail = try { - // A drop that waited carries times from before the writes ahead of it. - if (waited && !occurrenceStillAt(request)) { - _outcome.value = MoveOutcome.Stale - return null - } - repository.eventDetail(request.eventId) + private suspend fun prepare(dropped: MoveRequest): PreparedMove? { + val zone = zoneSource.current() + val (detail, request) = try { + val detail = repository.eventDetail(dropped.eventId) + // The drop carries the times the grid drew, which an earlier write + // may have moved on since (#494). + detail to if (occurrenceStillAt(dropped)) dropped else followed(dropped, detail, zone) } catch (e: CancellationException) { throw e } catch (e: NoSuchEventException) { - _outcome.value = MoveOutcome.Gone + report(MoveOutcome.Gone) return null } catch (e: Exception) { - _outcome.value = MoveOutcome.Failed + report(MoveOutcome.Failed) + return null + } + if (request == null) { + report(MoveOutcome.Stale) return null } - val zone = zoneSource.current() val original = detail.toEditForm(request.beginMillis, request.endMillis, zone) val shifted = when (val target = request.target) { is MoveTarget.Start -> original.shiftedTo(target.instant, zone) @@ -376,7 +393,7 @@ class RescheduleViewModel @Inject constructor( // The UNTIL check is deferred to the write, which knows how far the move // reaches and so which date has to clear it. if ((shifted.problems(zone) - EventFormProblem.RecurrenceEndsBeforeStart).isNotEmpty()) { - _outcome.value = MoveOutcome.Failed + report(MoveOutcome.Failed) return null } @@ -425,10 +442,10 @@ class RescheduleViewModel @Inject constructor( private suspend fun write(prepared: PreparedMove, scope: RecurringWriteScope): Boolean { val request = prepared.request if (endsBeforeItStarts(prepared, scope)) { - _outcome.value = MoveOutcome.BlockedSeriesEnd + report(MoveOutcome.BlockedSeriesEnd) return false } - _outcome.value = try { + val outcome = try { when { !prepared.isRecurring || scope == RecurringWriteScope.AllEvents -> repository.updateEvent(request.eventId, prepared.original, prepared.updated) @@ -463,18 +480,46 @@ class RescheduleViewModel @Inject constructor( } catch (e: Exception) { MoveOutcome.Failed } - return _outcome.value is MoveOutcome.Moved + report(outcome) + return outcome is MoveOutcome.Moved } - /** Whether [request]'s occurrence still begins where it was picked up. */ + /** Whether [request]'s occurrence is still exactly where it was picked up. */ private suspend fun occurrenceStillAt(request: MoveRequest): Boolean { val begin = Instant.fromEpochMilliseconds(request.beginMillis) - val end = Instant.fromEpochMilliseconds(maxOf(request.endMillis, request.beginMillis + 1)) - return repository.eventOccurrences(request.eventId, begin..end).any { - it.eventId == request.eventId && it.start == begin + val end = Instant.fromEpochMilliseconds(request.endMillis) + val range = begin..maxOf(end, Instant.fromEpochMilliseconds(request.beginMillis + 1)) + return repository.eventOccurrences(request.eventId, range).any { + it.eventId == request.eventId && it.start == begin && it.end == end } } + /** + * [dropped] rebased onto where its event now is, so the last drop wins: + * absolute targets stand as they are, and a whole-day shift is re-aimed at + * the day it was dropped on. Null for a recurring event, whose moved + * occurrence can't be told apart from the rest of its series. + */ + private fun followed(dropped: MoveRequest, detail: EventDetail, zone: TimeZone): MoveRequest? { + if (detail.rrule != null && !detail.isException) return null + val now = detail.instance + val target = when (val target = dropped.target) { + is MoveTarget.ByDays -> { + val dayZone = if (now.isAllDay) TimeZone.UTC else zone + val aimed = Instant.fromEpochMilliseconds(dropped.beginMillis).toLocalDateTime(dayZone).date + .plus(target.days, DateTimeUnit.DAY) + val from = now.start.toLocalDateTime(dayZone).date + MoveTarget.ByDays((aimed.toEpochDays() - from.toEpochDays()).toInt()) + } + else -> target + } + return dropped.copy( + beginMillis = now.start.toEpochMilliseconds(), + endMillis = now.end.toEpochMilliseconds(), + target = target, + ) + } + /** * Whether this write would leave a rule whose `UNTIL` precedes the first day * it now applies to — the provider then generates nothing at all. Which date diff --git a/app/src/main/java/de/jeanlucmakiola/calendula/ui/common/TimelineDrag.kt b/app/src/main/java/de/jeanlucmakiola/calendula/ui/common/TimelineDrag.kt index 39910c46..102d0df1 100644 --- a/app/src/main/java/de/jeanlucmakiola/calendula/ui/common/TimelineDrag.kt +++ b/app/src/main/java/de/jeanlucmakiola/calendula/ui/common/TimelineDrag.kt @@ -138,9 +138,17 @@ data class StripDrag(val event: EventInstance, val dayShift: Int = 0, val overGr sealed interface StripDrop { val event: EventInstance - data class Days(override val event: EventInstance, val days: Int) : StripDrop + /** The drop's identity, which its held copy waits on. */ + val token: Long - data class Timed(override val event: EventInstance, val start: Instant, val end: Instant) : StripDrop + data class Days(override val event: EventInstance, val days: Int, override val token: Long) : StripDrop + + data class Timed( + override val event: EventInstance, + val start: Instant, + val end: Instant, + override val token: Long, + ) : StripDrop } /** The event a long press left selected (#141). */ @@ -297,6 +305,8 @@ data class TimelineDrop( val startMin: Int, /** Minutes from the event's start to the dragged block's top edge. */ val clipOffsetMin: Int = 0, + /** The drop's identity, which its held copy waits on; set by the controller's finish. */ + val token: Long = 0L, ) /** The id a drafted block's stand-in event carries, which no provider row has (#153). */ @@ -340,7 +350,13 @@ internal fun draftBlock(draft: EventInstance, date: LocalDate, zone: TimeZone): enum class ResizeEdge { Top, Bottom } /** Where a finished resize asks its event to begin and end. */ -data class TimelineResizeDrop(val event: EventInstance, val start: Instant, val end: Instant) +data class TimelineResizeDrop( + val event: EventInstance, + val start: Instant, + val end: Instant, + /** The drop's identity, which its held copy waits on; set by the controller's finishResize. */ + val token: Long = 0L, +) /** * [event] resized by its [edge] to the span [startMin]..+[spanMin] from [date]'s @@ -610,7 +626,7 @@ class TimelineDragController { var settlingSelected: Boolean = false private set - /** The token of the drop [settling] waits on; a drop's caller hands it to its `MoveRequest`. */ + /** The token of the drop [settling] waits on. */ var settledToken: Long = 0L private set @@ -742,16 +758,16 @@ class TimelineDragController { val zone = dragZone cancel() if (held.overGrid && landed != null && zone != null && !landed.edgeOnly) { - settle(landed) + val token = settle(landed) val start = TimelineDrop(landed.event, landed.date, landed.startMin).startInstant(zone) - return StripDrop.Timed(held.event, start, start + draftMinutes.minutes) + return StripDrop.Timed(held.event, start, start + draftMinutes.minutes, token) } if (held.overGrid || held.dayShift == 0) return null stripSettling = held stripSeated = false stripToken = nextMoveToken() drops++ - return StripDrop.Days(held.event, held.dayShift) + return StripDrop.Days(held.event, held.dayShift, stripToken) } /** Let the held strip drop go, once the strip shows the event at its new days or the move failed. */ @@ -848,12 +864,15 @@ class TimelineDragController { if (landed == null || landed.slot == origin) return null // A draft dropped on a day off screen would vanish, so it stays where it was (#153). if (landed.edgeOnly && landed.event.isDraft) return null - settle(landed) - return TimelineDrop(landed.event, landed.date, landed.startMin, clipOffset) + val token = settle(landed) + return TimelineDrop(landed.event, landed.date, landed.startMin, clipOffset, token) } - /** Keep [landed]'s copy on the target as [settling] until [release]; [end] is a resize's. */ - private fun settle(landed: TimelineDrag, end: Instant? = null) { + /** + * Keep [landed]'s copy on the target as [settling] until [release]; [end] is a + * resize's. Returns the drop's fresh token. + */ + private fun settle(landed: TimelineDrag, end: Instant? = null): Long { // Landed on midnight to midnight, the event moves to the all-day strip (#296). val leavesGrid = !landed.event.isDraft && landed.eventSpanMin > 0 && landed.eventStartMin.mod(MINUTES_PER_DAY) == 0 && @@ -869,6 +888,7 @@ class TimelineDragController { settledToken = nextMoveToken() settledEnd = end drops++ + return settledToken } /** @@ -937,8 +957,7 @@ class TimelineDragController { cancel() if (landed == null || landed.slot == origin) return null val drop = resizedDrop(landed.event, edge, landed.date, landed.eventStartMin, landed.eventSpanMin, zone) - settle(landed, end = drop.end) - return drop + return drop.copy(token = settle(landed, end = drop.end)) } /** diff --git a/app/src/main/java/de/jeanlucmakiola/calendula/ui/week/WeekScreen.kt b/app/src/main/java/de/jeanlucmakiola/calendula/ui/week/WeekScreen.kt index f84dd47e..f79e4914 100644 --- a/app/src/main/java/de/jeanlucmakiola/calendula/ui/week/WeekScreen.kt +++ b/app/src/main/java/de/jeanlucmakiola/calendula/ui/week/WeekScreen.kt @@ -592,7 +592,7 @@ private fun WeekContent( beginMillis = drop.event.start.toEpochMilliseconds(), endMillis = drop.event.end.toEpochMilliseconds(), target = MoveTarget.Start(start), - token = dragController.settledToken, + token = drop.token, ), ) } @@ -612,7 +612,7 @@ private fun WeekContent( beginMillis = drop.event.start.toEpochMilliseconds(), endMillis = drop.event.end.toEpochMilliseconds(), target = MoveTarget.Span(drop.start, drop.end), - token = dragController.settledToken, + token = drop.token, ), ) } @@ -628,11 +628,7 @@ private fun WeekContent( is StripDrop.Days -> MoveTarget.ByDays(drop.days) is StripDrop.Timed -> MoveTarget.Span(drop.start, drop.end) }, - // A bar dropped into the grid settles as a timed copy, not on the strip. - token = when (drop) { - is StripDrop.Days -> dragController.stripToken - is StripDrop.Timed -> dragController.settledToken - }, + token = drop.token, ), ) } diff --git a/app/src/test/java/de/jeanlucmakiola/calendula/ui/common/RescheduleViewModelTest.kt b/app/src/test/java/de/jeanlucmakiola/calendula/ui/common/RescheduleViewModelTest.kt index 172c1350..fc174f1b 100644 --- a/app/src/test/java/de/jeanlucmakiola/calendula/ui/common/RescheduleViewModelTest.kt +++ b/app/src/test/java/de/jeanlucmakiola/calendula/ui/common/RescheduleViewModelTest.kt @@ -13,6 +13,7 @@ import de.jeanlucmakiola.calendula.testEvent import de.jeanlucmakiola.calendula.testPrefs import de.jeanlucmakiola.calendula.testRepository import de.jeanlucmakiola.calendula.testSettings +import kotlinx.coroutines.CoroutineDispatcher import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.Job @@ -80,18 +81,17 @@ class RescheduleViewModelTest { status = status, ) - private fun viewModel(tempDir: Path, fake: FakeCalendarDataSource): RescheduleViewModel { - val repo = testRepository( - fake, tempDir, dispatcher, - prefs = testPrefs(tempDir, CoroutineScope(dispatcher)), - settings = testSettings(tempDir, CoroutineScope(dispatcher)), - ) - return RescheduleViewModel(repo, dispatcher, FakeZoneSource()) - } - - /** A view model whose provider reads suspend until the scheduler runs, so a drop stays in flight. */ - private fun slowViewModel(tempDir: Path, fake: FakeCalendarDataSource): RescheduleViewModel { - val io = StandardTestDispatcher(dispatcher.scheduler) + /** + * [present] are the events whose occurrence the provider still reports where + * a drop picked it up; [io] a [StandardTestDispatcher] keeps a drop in flight. + */ + private fun viewModel( + tempDir: Path, + fake: FakeCalendarDataSource, + present: List = listOf(42L, 7L), + io: CoroutineDispatcher = dispatcher, + ): RescheduleViewModel { + fake.occurrencesAt(present) val repo = testRepository( fake, tempDir, io, prefs = testPrefs(tempDir, CoroutineScope(dispatcher)), @@ -100,6 +100,22 @@ class RescheduleViewModelTest { return RescheduleViewModel(repo, io, FakeZoneSource()) } + private fun slowViewModel(tempDir: Path, fake: FakeCalendarDataSource): RescheduleViewModel = + viewModel(tempDir, fake, io = StandardTestDispatcher(dispatcher.scheduler)) + + /** Every query answers with an occurrence of each of [ids] exactly at the queried span. */ + private fun FakeCalendarDataSource.occurrencesAt(ids: List) { + instancesResult = { begin, end -> + ids.map { id -> + testEvent( + id = id, + start = Instant.fromEpochMilliseconds(begin), + end = Instant.fromEpochMilliseconds(end), + ) + } + } + } + private val RescheduleViewModel.abandonedCount: Int get() = drops.value.abandoned.size private fun CoroutineScope.activate(vm: RescheduleViewModel): Job = @@ -111,6 +127,7 @@ class RescheduleViewModelTest { beginMillis = beginMillis, endMillis = endMillis, target = MoveTarget.ByDays(2), + token = nextMoveToken(), ) /** A drop an hour later, expressed the way a timeline drag expresses one. */ @@ -119,6 +136,7 @@ class RescheduleViewModelTest { beginMillis = beginMillis, endMillis = endMillis, target = MoveTarget.Start(Instant.fromEpochMilliseconds(beginMillis + 3_600_000L)), + token = nextMoveToken(), ) /** The end dragged an hour later, the start left where it is. */ @@ -130,6 +148,7 @@ class RescheduleViewModelTest { Instant.fromEpochMilliseconds(beginMillis), Instant.fromEpochMilliseconds(endMillis + 3_600_000L), ), + token = nextMoveToken(), ) @Test @@ -272,31 +291,17 @@ class RescheduleViewModelTest { assertThat(vm.scopePrompt.value).isEqualTo(MoveScopePrompt(occurrenceOnly = true)) } - /** The occurrences [ids] still at the dragged start, as a waiting drop re-checks them. */ - private fun FakeCalendarDataSource.occurrencesAt(vararg ids: Long) { - instancesResult = { _, _ -> - ids.map { id -> - testEvent( - id = id, - start = Instant.fromEpochMilliseconds(beginMillis), - end = Instant.fromEpochMilliseconds(endMillis), - ) - } - } - } - @Test fun `a drop of another event waits for the one in flight, then lands`( @TempDir tempDir: Path, ) = runTest(dispatcher) { val fake = FakeCalendarDataSource().apply { eventDetailResult = { detail() } - occurrencesAt(7L) } val vm = slowViewModel(tempDir, fake) val first = oneHourLater() - val second = toWednesday().copy(eventId = 7L) + val second = toWednesday().copy(eventId = 7L, token = nextMoveToken()) assertThat(vm.move(first)).isTrue() assertThat(vm.move(second)).isTrue() assertThat(vm.drops.value.pending).containsExactly(first.token, second.token) @@ -313,13 +318,15 @@ class RescheduleViewModelTest { ) = runTest(dispatcher) { val fake = FakeCalendarDataSource().apply { eventDetailResult = { detail() } - occurrencesAt(7L) } val vm = slowViewModel(tempDir, fake) // Zero distance: refused in prepare. - val nowhere = oneHourLater().copy(target = MoveTarget.Start(Instant.fromEpochMilliseconds(beginMillis))) - val queued = toWednesday().copy(eventId = 7L) + val nowhere = oneHourLater().copy( + target = MoveTarget.Start(Instant.fromEpochMilliseconds(beginMillis)), + token = nextMoveToken(), + ) + val queued = toWednesday().copy(eventId = 7L, token = nextMoveToken()) vm.move(nowhere) vm.move(queued) advanceUntilIdle() @@ -329,21 +336,80 @@ class RescheduleViewModelTest { } @Test - fun `a waiting drop whose occurrence an earlier write moved is not written`( + fun `the same event dropped twice ends up where it was dropped last`( @TempDir tempDir: Path, ) = runTest(dispatcher) { - // The first drop moved event 42, so nothing of it begins at the old start any more. - val fake = FakeCalendarDataSource().apply { eventDetailResult = { detail() } } + val hour = 3_600_000L + val fake = FakeCalendarDataSource() val vm = slowViewModel(tempDir, fake) + // Once the first drop is written, the event sits an hour later. + fake.eventDetailResult = { + if (fake.updatedEvents.isEmpty()) detail() else detail(anchorMillis = beginMillis + hour) + } + fake.instancesResult = { _, _ -> + val shift = if (fake.updatedEvents.isEmpty()) 0L else hour + listOf( + testEvent( + id = 42L, + start = Instant.fromEpochMilliseconds(beginMillis + shift), + end = Instant.fromEpochMilliseconds(endMillis + shift), + ), + ) + } - val again = toWednesday() vm.move(oneHourLater()) - vm.move(again) + // Picked up from where the grid still drew it, and dropped two days on. + vm.move(toWednesday()) advanceUntilIdle() - assertThat(fake.updatedEvents).hasSize(1) + assertThat(fake.updatedEvents).hasSize(2) + val last = fake.updatedEvents.last().third + assertThat(last.start.dateTime).isEqualTo(LocalDateTime(LocalDate(2026, 6, 10), LocalTime(13, 0))) + } + + @Test + fun `a recurring occurrence that moved since the drop is not written`( + @TempDir tempDir: Path, + ) = runTest(dispatcher) { + val fake = FakeCalendarDataSource().apply { eventDetailResult = { detail(rrule = "FREQ=WEEKLY") } } + val vm = viewModel(tempDir, fake, present = emptyList()) + + val drop = toWednesday() + vm.move(drop) + advanceUntilIdle() + + assertThat(vm.scopePrompt.value).isNull() assertThat(vm.outcome.value).isEqualTo(MoveOutcome.Stale) - assertThat(vm.drops.value.abandoned).containsExactly(again.token) + assertThat(vm.drops.value.abandoned).containsExactly(drop.token) + assertThat(fake.updatedEvents).isEmpty() + } + + @Test + fun `a deleted event reports itself as gone, not moved`(@TempDir tempDir: Path) = runTest(dispatcher) { + val fake = FakeCalendarDataSource().apply { eventDetailResult = { null } } + val vm = viewModel(tempDir, fake, present = emptyList()) + + vm.move(oneHourLater()) + advanceUntilIdle() + + assertThat(vm.outcome.value).isEqualTo(MoveOutcome.Gone) + } + + @Test + fun `a failure stays on the chip while later drops land`(@TempDir tempDir: Path) = runTest(dispatcher) { + val fake = FakeCalendarDataSource().apply { + eventDetailResult = { id -> + if (id == 42L) detail(rrule = "FREQ=WEEKLY;BYDAY=MO,WE", status = EventStatus.Cancelled) else detail() + } + } + val vm = slowViewModel(tempDir, fake) + + vm.move(toWednesday()) + vm.move(toWednesday().copy(eventId = 7L, token = nextMoveToken())) + advanceUntilIdle() + + assertThat(fake.updatedEvents.map { it.first }).containsExactly(7L) + assertThat(vm.outcome.value).isEqualTo(MoveOutcome.BlockedCancelledSeries) } @Test @@ -352,13 +418,12 @@ class RescheduleViewModelTest { ) = runTest(dispatcher) { val fake = FakeCalendarDataSource().apply { eventDetailResult = { id -> if (id == 42L) detail(rrule = "FREQ=WEEKLY") else detail() } - occurrencesAt(7L) } val vm = viewModel(tempDir, fake) vm.move(oneHourLater()) advanceUntilIdle() - val queued = toWednesday().copy(eventId = 7L) + val queued = toWednesday().copy(eventId = 7L, token = nextMoveToken()) assertThat(vm.move(queued)).isTrue() advanceUntilIdle() assertThat(fake.updatedEvents).isEmpty() @@ -510,6 +575,7 @@ class RescheduleViewModelTest { target = MoveTarget.Start( Instant.fromEpochMilliseconds(beginMillis + 26 * 3_600_000L), ), + token = nextMoveToken(), ), ) advanceUntilIdle() @@ -530,6 +596,7 @@ class RescheduleViewModelTest { beginMillis = beginMillis, endMillis = endMillis, target = MoveTarget.Start(Instant.fromEpochMilliseconds(beginMillis)), + token = nextMoveToken(), ), ) advanceUntilIdle() @@ -651,6 +718,7 @@ class RescheduleViewModelTest { beginMillis = beginMillis, endMillis = endMillis, target = MoveTarget.Start(Instant.fromEpochMilliseconds(beginMillis)), + token = nextMoveToken(), ), )).isTrue() advanceUntilIdle() @@ -763,6 +831,7 @@ class RescheduleViewModelTest { beginMillis = beginMillis, endMillis = endMillis, target = MoveTarget.Span(sundayEvening, Instant.fromEpochMilliseconds(endMillis)), + token = nextMoveToken(), ), ) advanceUntilIdle() @@ -834,6 +903,7 @@ class RescheduleViewModelTest { beginMillis = dayStart, endMillis = dayStart + 86_400_000L, target = MoveTarget.Span(start, Instant.fromEpochMilliseconds(endMillis)), + token = nextMoveToken(), ), ) advanceUntilIdle() @@ -870,6 +940,7 @@ class RescheduleViewModelTest { beginMillis = dayStart, endMillis = dayStart + 86_400_000L, target = MoveTarget.Span(eight, eight + 1.hours), + token = nextMoveToken(), ), ) advanceUntilIdle()