Manage Tags: Stale tag hierarchy after external updates

Open
#23,217 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
48/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Active
Tech stack
android, kotlin
Domain
mobile, testing

Research direction

Start with the regression test in AnkiDroid/src/test/java/com/ichi2/anki/tags/ManageTagsRefreshRegressionTest.kt and the referenced PR review for context. Trace how ManageTagsViewModel prepares renames and refreshes tag state when ChangeManager publishes external updates. Done means the test confirms collision checks use the updated tag hierarchy and prevent an unintended merge.

Written by the indexing model from the issue text.

Description

Needs Triage
Subject: [EXISTING] Stale tag hierarchy after external updates

An API tag update during rename preparation leaves collision checks stale, allowing an unintended merge through the new rename dialog.

⚠️ 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/tags/ManageTagsRefreshRegressionTest.kt b/AnkiDroid/src/test/java/com/ichi2/anki/tags/ManageTagsRefreshRegressionTest.kt
new file mode 100644
index 0000000000..9ce0b14e02
--- /dev/null
+++ b/AnkiDroid/src/test/java/com/ichi2/anki/tags/ManageTagsRefreshRegressionTest.kt
@@ -0,0 +1,48 @@
+// SPDX-License-Identifier: GPL-3.0-or-later
+
+package com.ichi2.anki.tags
+
+import com.ichi2.anki.CollectionManager
+import com.ichi2.anki.observability.ChangeManager
+import com.ichi2.testutils.JvmTest
+import com.ichi2.testutils.rules.IcuCaseFoldingRule
+import com.ichi2.testutils.withViewModel
+import kotlinx.coroutines.CoroutineStart
+import kotlinx.coroutines.async
+import kotlinx.coroutines.test.StandardTestDispatcher
+import org.junit.Rule
+import org.junit.Test
+import kotlin.test.assertEquals
+import kotlin.test.assertTrue
+
+class ManageTagsRefreshRegressionTest : JvmTest() {
+    @get:Rule
+    val caseFolding = IcuCaseFoldingRule()
+
+    @Test
+    fun `external tag changes during rename preparation invalidate collision checks`() =
+        runTest {
+            val source = requireNotNull(TagName.build("source"))
+            val destination = requireNotNull(TagName.build("destination"))
+            val note = addBasicNote().update { addTag(source.value) }
+            val collection = col
+            withViewModel(ManageTagsViewModel()) {
+                assertTrue(tagExists(source))
+                val previousQueue = CollectionManager.setTestDispatcher(StandardTestDispatcher(testScheduler), useReentrantLock = false)
+                try {
+                    val preparation = async(start = CoroutineStart.UNDISPATCHED) { prepareRename(source) }
+                    assertTrue((state.value as ManageTagsState.Content).isWorking)
+                    // An API batch can publish changes while the eligibility query is queued.
+                    collection.getNote(note.id).also {
+                        it.addTag(destination.value)
+                        collection.updateNote(it)
+                    }
+                    ChangeManager.notifySubscribersAllValuesChanged()
+                    assertEquals(true, preparation.await())
+                    assertEquals(TagInputValidation.ALREADY_EXISTS, validateTagRename(source, destination.value, ::tagExists))
+                } finally {
+                    CollectionManager.setTestDispatcher(previousQueue)
+                }
+            }
+        }
+}

https://github.com/ankidroid/Anki-Android/pull/22849#pullrequestreview-5875194447

Dominant language
Kotlin
Stars
12.3k
Forks
3.1k
Avg merge
1d 11h
Merged PRs (30d)
383

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from ankidroid/Anki-Android

All issues in ankidroid/Anki-Android

Similar issues

More Kotlin issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.