Deck action preparation hides database corruption

Aperta
#23,362 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
3/5
Tempo stimato
1-2 giorni
Idoneità per principianti
56/100
Tipo di issue
Bug
Chiarezza
Abbastanza chiara
Stato di attività
Attiva
Stack tecnologico
android, kotlin
Ambito
backend, database

Direzione di ricerca

Start with the regression test shown in the issue, DeckPickerPreparedActionCorruptionRegressionTest.kt, which covers deletion confirmation and the Study shortcut when reading the selected deck fails. Trace how those actions prepare a deck and handle backend corruption, then compare that path with the existing database recovery handling. Done means both cases detect corruption and open DatabaseErrorDialog instead of showing a generic error.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

[EXISTING] Deck action preparation hides database corruption
Subject: [EXISTING] Deck action preparation hides database corruption

Backend corruption while preparing Delete or the Study shortcut becomes a generic error, hiding database recovery.

⚠️ This issue existed before the PR and should likely be split off into a separate issue.

---
diff --git a/AnkiDroid/src/test/java/com/ichi2/anki/DeckPickerPreparedActionCorruptionRegressionTest.kt b/AnkiDroid/src/test/java/com/ichi2/anki/DeckPickerPreparedActionCorruptionRegressionTest.kt
new file mode 100644
index 0000000000000000000000000000000000000000..3c2c4ac14d7d40656f0469e5241b26b2241b5175
--- /dev/null
+++ b/AnkiDroid/src/test/java/com/ichi2/anki/DeckPickerPreparedActionCorruptionRegressionTest.kt
@@ -0,0 +1,65 @@
+// SPDX-License-Identifier: GPL-3.0-or-later
+
+package com.ichi2.anki
+
+import androidx.test.ext.junit.runners.AndroidJUnit4
+import anki.backend.backendError
+import com.ichi2.anki.backend.DatabaseCorruption
+import com.ichi2.anki.deckpicker.DeckPickerViewModel
+import com.ichi2.anki.dialogs.DatabaseErrorDialog
+import com.ichi2.testutils.rules.BackendOverrideRule
+import kotlinx.coroutines.Job
+import net.ankiweb.rsdroid.Backend
+import net.ankiweb.rsdroid.BackendException.BackendDbException.BackendDbCorruptException
+import org.junit.Rule
+import org.junit.Test
+import org.junit.runner.RunWith
+import org.robolectric.shadows.ShadowDialog
+import java.util.concurrent.atomic.AtomicBoolean
+import kotlin.test.assertTrue
+
+@RunWith(AndroidJUnit4::class)
+class DeckPickerPreparedActionCorruptionRegressionTest : RobolectricTest() {
+    @get:Rule
+    val backendOverride = BackendOverrideRule()
+
+    @Test
+    fun `corruption reading the deck for deletion confirmation opens recovery`() = checkCorruption { requestDeleteDeckConfirmation() }
+
+    @Test
+    fun `corruption reading the deck for the study shortcut opens recovery`() = checkCorruption { requestStudySelectedDeck() }
+
+    private fun checkCorruption(request: DeckPickerViewModel.() -> Job) {
+        val failNextRead = AtomicBoolean(false)
+        backendOverride.replaceWith {
+            object : Backend() {
+                override fun getCurrentDeck(): anki.decks.Deck =
+                    if (failNextRead.getAndSet(false)) {
+                        throw BackendDbCorruptException(backendError { message = "database disk image is malformed" })
+                    } else {
+                        super.getCurrentDeck()
+                    }
+            }
+        }
+        addDeck("Target", setAsSelected = true)
+        val previouslyDetected = DatabaseCorruption.isDetected
+        DatabaseCorruption.isDetected = false
+        try {
+            withDeckPicker(deckCount = 0) { picker ->
+                advanceRobolectricLooperUntil { picker.viewModel.loadDeckCounts?.isCompleted == true }
+                advanceRobolectricLooper()
+                throwOnShowError = false
+                failNextRead.set(true)
+                val action = picker.viewModel.request()
+                advanceRobolectricLooperUntil { action.isCompleted && ShadowDialog.getLatestDialog()?.isShowing == true }
+                assertTrue(DatabaseCorruption.isDetected, "Preparing a deck action must preserve corruption detection")
+                assertTrue(
+                    picker.supportFragmentManager.fragments.any { it is DatabaseErrorDialog && it.dialog?.isShowing == true },
+                    "Preparing a deck action must open recovery instead of a generic error alert",
+                )
+            }
+        } finally {
+            DatabaseCorruption.isDetected = previouslyDetected
+        }
+    }
+}

Originally posted by @david-allison in https://github.com/ankidroid/Anki-Android/pull/22504#pullrequestreview-5860114910

Lingua principale
Kotlin
Stelle
12.3k
Fork
3.1k
Merge medio
1g 11h
PR unite (30g)
383

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di ankidroid/Anki-Android

Tutte le issue di ankidroid/Anki-Android

Issue simili

Altre issue su Kotlin

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.