musikr: never reconcile destructively

This commit is contained in:
jay
2026-11-12 18:48:03 -05:00
parent 39deae7644
commit f492410484
2 changed files with 51 additions and 35 deletions
@@ -22,9 +22,10 @@ import org.oxycblt.musikr.Music
/**
* Reconciles stored tracked playlists against the playlist files discovered in a scan. The file
* on disk is authoritative: an unchanged file keeps the stored playlist (in-app edits survive), a
* changed file replaces it (in-app edits are discarded), and a deleted file deletes it. Playlists
* without a source file are unmanaged and never reach this unit.
* on disk wins when it speaks, but never destructively: an unchanged file keeps the stored
* playlist (in-app edits survive), a changed file with at least one resolved entry replaces it
* (in-app edits are discarded), and a file that is missing or resolves nothing keeps the stored
* playlist. Playlists without a source file are unmanaged and never reach this unit.
*
* @author OxygenCobalt (Fork)
*/
@@ -43,21 +44,19 @@ internal object PlaylistReconciler {
*
* @property uri The document URI of the file.
* @property signature The content signature of the file.
* @property resolvedCount How many entries resolved against the library.
*/
data class Discovered(val uri: String, val signature: Long)
data class Discovered(val uri: String, val signature: Long, val resolvedCount: Int)
/** What to do with a tracked playlist or discovered file after reconciliation. */
sealed interface Action {
/** The file is unchanged: keep the stored playlist as-is. */
/** Nothing changes: keep the stored playlist as-is. */
data class Keep(val tracked: Tracked) : Action
/** The file changed: re-import it, replacing the stored playlist's songs. */
/** The file changed and resolves: re-import it, replacing the stored playlist's songs. */
data class Replace(val tracked: Tracked, val file: Discovered) : Action
/** The file is gone: delete the stored playlist. */
data class Delete(val tracked: Tracked) : Action
/** The file has no stored playlist: import it as a new tracked playlist. */
/** The file has no stored playlist and resolves: import it as a new tracked playlist. */
data class Create(val file: Discovered) : Action
}
@@ -71,14 +70,17 @@ internal object PlaylistReconciler {
tracked.map { item ->
val file = filesByUri[item.sourceUri]
when {
file == null -> Action.Delete(item)
file == null -> Action.Keep(item)
file.signature == item.signature -> Action.Keep(item)
file.resolvedCount == 0 -> Action.Keep(item)
else -> Action.Replace(item, file)
}
}
val trackedUris = tracked.mapTo(mutableSetOf()) { it.sourceUri }
val createActions =
discovered.filter { it.uri !in trackedUris }.map { Action.Create(it) }
discovered
.filter { it.uri !in trackedUris && it.resolvedCount > 0 }
.map { Action.Create(it) }
return trackedActions + createActions
}
}
@@ -25,13 +25,8 @@ import org.oxycblt.musikr.Music
class PlaylistReconcilerTest {
@Test
fun reconcile_unchangedFile_keepsTheStoredPlaylist() {
val tracked =
PlaylistReconciler.Tracked(
Music.UID.auxio(Music.UID.Item.PLAYLIST),
"content://tree/playlists/x.m3u",
42L,
)
val discovered = PlaylistReconciler.Discovered("content://tree/playlists/x.m3u", 42L)
val tracked = tracked(42L)
val discovered = discovered(42L, resolvedCount = 1)
val actions = PlaylistReconciler.reconcile(listOf(tracked), listOf(discovered))
@@ -40,13 +35,8 @@ class PlaylistReconcilerTest {
@Test
fun reconcile_changedFile_replacesTheStoredPlaylist() {
val tracked =
PlaylistReconciler.Tracked(
Music.UID.auxio(Music.UID.Item.PLAYLIST),
"content://tree/playlists/x.m3u",
42L,
)
val discovered = PlaylistReconciler.Discovered("content://tree/playlists/x.m3u", 43L)
val tracked = tracked(42L)
val discovered = discovered(43L, resolvedCount = 1)
val actions = PlaylistReconciler.reconcile(listOf(tracked), listOf(discovered))
@@ -54,25 +44,49 @@ class PlaylistReconcilerTest {
}
@Test
fun reconcile_deletedFile_deletesTheStoredPlaylist() {
val tracked =
PlaylistReconciler.Tracked(
Music.UID.auxio(Music.UID.Item.PLAYLIST),
"content://tree/playlists/x.m3u",
42L,
)
fun reconcile_deletedFile_keepsTheStoredPlaylist() {
val tracked = tracked(42L)
val actions = PlaylistReconciler.reconcile(listOf(tracked), emptyList())
assertEquals(listOf(PlaylistReconciler.Action.Delete(tracked)), actions)
assertEquals(listOf(PlaylistReconciler.Action.Keep(tracked)), actions)
}
@Test
fun reconcile_changedFileWithNothingResolved_keepsTheStoredPlaylist() {
val tracked = tracked(42L)
val discovered = discovered(43L, resolvedCount = 0)
val actions = PlaylistReconciler.reconcile(listOf(tracked), listOf(discovered))
assertEquals(listOf(PlaylistReconciler.Action.Keep(tracked)), actions)
}
@Test
fun reconcile_newFile_createsATrackedPlaylist() {
val discovered = PlaylistReconciler.Discovered("content://tree/playlists/x.m3u", 42L)
val discovered = discovered(42L, resolvedCount = 1)
val actions = PlaylistReconciler.reconcile(emptyList(), listOf(discovered))
assertEquals(listOf(PlaylistReconciler.Action.Create(discovered)), actions)
}
@Test
fun reconcile_newFileWithNothingResolved_doesNothing() {
val discovered = discovered(42L, resolvedCount = 0)
val actions = PlaylistReconciler.reconcile(emptyList(), listOf(discovered))
assertEquals(emptyList<PlaylistReconciler.Action>(), actions)
}
private fun tracked(signature: Long) =
PlaylistReconciler.Tracked(
Music.UID.auxio(Music.UID.Item.PLAYLIST),
"content://tree/playlists/x.m3u",
signature,
)
private fun discovered(signature: Long, resolvedCount: Int) =
PlaylistReconciler.Discovered("content://tree/playlists/x.m3u", signature, resolvedCount)
}