Rebuild library graphs from snapshot songs
This commit is contained in:
@@ -0,0 +1,39 @@
|
||||
/*
|
||||
* Copyright (c) 2026 Auxio Project
|
||||
* LibrarySnapshotGraph.kt is part of Auxio.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package org.oxycblt.musikr.snapshot
|
||||
|
||||
import org.oxycblt.musikr.MutableLibrary
|
||||
import org.oxycblt.musikr.graph.MusicGraph
|
||||
import org.oxycblt.musikr.model.LibraryFactory
|
||||
import org.oxycblt.musikr.playlist.db.StoredPlaylists
|
||||
import org.oxycblt.musikr.playlist.interpret.PlaylistInterpreter
|
||||
import org.oxycblt.musikr.tag.interpret.PreSong
|
||||
|
||||
internal object LibrarySnapshotGraph {
|
||||
fun restore(
|
||||
songs: List<PreSong>,
|
||||
storedPlaylists: StoredPlaylists,
|
||||
playlistInterpreter: PlaylistInterpreter,
|
||||
): MutableLibrary {
|
||||
val builder = MusicGraph.builder()
|
||||
songs.forEach { builder.add(it) }
|
||||
return LibraryFactory.new()
|
||||
.create(builder.build(), storedPlaylists, playlistInterpreter)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,268 @@
|
||||
/*
|
||||
* Copyright (c) 2026 Auxio Project
|
||||
* LibrarySnapshotGraphTest.kt is part of Auxio.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package org.oxycblt.musikr.snapshot
|
||||
|
||||
import android.net.Uri
|
||||
import io.mockk.coEvery
|
||||
import io.mockk.coVerify
|
||||
import io.mockk.every
|
||||
import io.mockk.mockk
|
||||
import io.mockk.verify
|
||||
import java.nio.file.Files
|
||||
import kotlinx.coroutines.runBlocking
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Assert.assertSame
|
||||
import org.junit.Assert.assertTrue
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import org.oxycblt.musikr.Music
|
||||
import org.oxycblt.musikr.covers.Cover
|
||||
import org.oxycblt.musikr.fs.Components
|
||||
import org.oxycblt.musikr.fs.Format
|
||||
import org.oxycblt.musikr.fs.Path
|
||||
import org.oxycblt.musikr.fs.path.InternalVolumeValue
|
||||
import org.oxycblt.musikr.playlist.PlaylistHandle
|
||||
import org.oxycblt.musikr.playlist.db.StoredPlaylists
|
||||
import org.oxycblt.musikr.playlist.interpret.PlaylistInterpreter
|
||||
import org.oxycblt.musikr.playlist.interpret.PostPlaylist
|
||||
import org.oxycblt.musikr.tag.Name
|
||||
import org.oxycblt.musikr.tag.ReleaseType
|
||||
import org.oxycblt.musikr.tag.ReplayGainAdjustment
|
||||
import org.oxycblt.musikr.tag.interpret.Naming
|
||||
import org.oxycblt.musikr.tag.interpret.PreAlbum
|
||||
import org.oxycblt.musikr.tag.interpret.PreArtist
|
||||
import org.oxycblt.musikr.tag.interpret.PreArtistsFrom
|
||||
import org.oxycblt.musikr.tag.interpret.PreGenre
|
||||
import org.oxycblt.musikr.tag.interpret.PreSong
|
||||
import org.robolectric.RobolectricTestRunner
|
||||
import org.robolectric.RuntimeEnvironment
|
||||
import org.robolectric.annotation.Config
|
||||
|
||||
@RunWith(RobolectricTestRunner::class)
|
||||
@Config(sdk = [30])
|
||||
class LibrarySnapshotGraphTest {
|
||||
@Test
|
||||
fun restore_sharedParentInputs_produceSingleSharedParentObjects() {
|
||||
val first = preSong("uas00000000-0000-4000-8000-000000000001", "First")
|
||||
val second = preSong("uas00000000-0000-4000-8000-000000000002", "Second")
|
||||
|
||||
val library =
|
||||
LibrarySnapshotGraph.restore(
|
||||
listOf(first, second),
|
||||
mockk<StoredPlaylists>(),
|
||||
mockk<PlaylistInterpreter>(),
|
||||
)
|
||||
val songs = library.songs.associateBy { it.uid }
|
||||
val firstSong = requireNotNull(songs[first.v363Uid])
|
||||
val secondSong = requireNotNull(songs[second.v363Uid])
|
||||
val album = library.albums.single()
|
||||
val artist = library.artists.single()
|
||||
val genre = library.genres.single()
|
||||
|
||||
assertEquals(2, library.songs.size)
|
||||
assertSame(album, firstSong.album)
|
||||
assertSame(album, secondSong.album)
|
||||
assertSame(artist, firstSong.artists.single())
|
||||
assertSame(artist, secondSong.artists.single())
|
||||
assertSame(genre, firstSong.genres.single())
|
||||
assertSame(genre, secondSong.genres.single())
|
||||
assertSame(artist, album.artists.single())
|
||||
assertSame(album, artist.explicitAlbums.single())
|
||||
assertSame(genre, artist.genres.single())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun restore_duplicateCurrentUid_keepsFirstRecordWithoutAdditionalParents() {
|
||||
val uid = "uas00000000-0000-4000-8000-000000000003"
|
||||
val first = preSong(uid, "First")
|
||||
val duplicate =
|
||||
preSong(
|
||||
uid,
|
||||
"Replacement",
|
||||
albumName = "Other album",
|
||||
artistName = "Other artist",
|
||||
genreName = "Other genre",
|
||||
)
|
||||
|
||||
val library =
|
||||
LibrarySnapshotGraph.restore(
|
||||
listOf(first, duplicate),
|
||||
mockk<StoredPlaylists>(),
|
||||
mockk<PlaylistInterpreter>(),
|
||||
)
|
||||
val song = library.songs.single()
|
||||
|
||||
assertEquals(first.v363Uid, song.uid)
|
||||
assertEquals("First", song.name.raw)
|
||||
assertEquals("Album", (song.album.name as Name.Known).raw)
|
||||
assertEquals("Artist", (song.artists.single().name as Name.Known).raw)
|
||||
assertEquals("Genre", (song.genres.single().name as Name.Known).raw)
|
||||
assertEquals(1, library.albums.size)
|
||||
assertEquals(1, library.artists.size)
|
||||
assertEquals(1, library.genres.size)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun restore_findSong_supportsCurrentAndBothCompatibilityUids() {
|
||||
val value =
|
||||
preSong(
|
||||
uid = "uas00000000-0000-4000-8000-000000000004",
|
||||
name = "Tracked",
|
||||
v400Uid = "uas00000000-0000-4000-8000-000000000104",
|
||||
v401Uid = "ums00000000-0000-4000-8000-000000000204",
|
||||
)
|
||||
|
||||
val library =
|
||||
LibrarySnapshotGraph.restore(
|
||||
listOf(value),
|
||||
mockk<StoredPlaylists>(),
|
||||
mockk<PlaylistInterpreter>(),
|
||||
)
|
||||
val song = library.songs.single()
|
||||
|
||||
assertSame(song, library.findSong(value.v363Uid))
|
||||
assertSame(song, library.findSong(value.v400Uid))
|
||||
assertSame(song, library.findSong(value.v401Uid))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun restore_coverReferences_preserveSongAndParentImageIdentity() {
|
||||
val storedCoverDirectory =
|
||||
Files.createTempDirectory("auxio-graph-cover").toFile().apply { deleteOnExit() }
|
||||
val cover =
|
||||
requireNotNull(
|
||||
LibrarySnapshotCovers.restore(
|
||||
"mcs:shared.webp",
|
||||
storedCoverDirectory,
|
||||
RuntimeEnvironment.getApplication(),
|
||||
)
|
||||
)
|
||||
|
||||
val library =
|
||||
LibrarySnapshotGraph.restore(
|
||||
listOf(
|
||||
preSong(
|
||||
"uas00000000-0000-4000-8000-000000000005",
|
||||
"Illustrated",
|
||||
cover = cover,
|
||||
)
|
||||
),
|
||||
mockk<StoredPlaylists>(),
|
||||
mockk<PlaylistInterpreter>(),
|
||||
)
|
||||
val song = library.songs.single()
|
||||
val albumCovers = song.album.covers.covers
|
||||
val artistCovers = song.artists.single().covers.covers
|
||||
val genreCovers = song.genres.single().covers.covers
|
||||
|
||||
assertSame(cover, song.cover)
|
||||
assertEquals(1, albumCovers.size)
|
||||
assertEquals(1, artistCovers.size)
|
||||
assertEquals(1, genreCovers.size)
|
||||
assertSame(cover, albumCovers.single())
|
||||
assertSame(cover, artistCovers.single())
|
||||
assertSame(cover, genreCovers.single())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun restoredMutableLibrary_createPlaylist_usesRetainedLiveDependencies() {
|
||||
val storedPlaylists = mockk<StoredPlaylists>()
|
||||
val playlistInterpreter = mockk<PlaylistInterpreter>()
|
||||
val handle = mockk<PlaylistHandle>()
|
||||
val playlistUid =
|
||||
requireNotNull(Music.UID.fromString("uap00000000-0000-4000-8000-000000000006"))
|
||||
every { handle.uid } returns playlistUid
|
||||
val library =
|
||||
LibrarySnapshotGraph.restore(
|
||||
listOf(preSong("uas00000000-0000-4000-8000-000000000006", "Mutable")),
|
||||
storedPlaylists,
|
||||
playlistInterpreter,
|
||||
)
|
||||
val song = library.songs.single()
|
||||
coEvery { storedPlaylists.new("Created", listOf(song)) } returns handle
|
||||
every { playlistInterpreter.interpret("Created", handle) } returns
|
||||
PostPlaylist(Naming.simple().name("Created", null), "Created", handle)
|
||||
|
||||
val updated = runBlocking { library.createPlaylist("Created", listOf(song)) }
|
||||
val created = updated.playlists.single()
|
||||
|
||||
assertTrue(library.playlists.isEmpty())
|
||||
assertEquals(playlistUid, created.uid)
|
||||
assertEquals("Created", created.name.raw)
|
||||
assertSame(song, created.songs.single())
|
||||
assertSame(created, updated.findPlaylist(playlistUid))
|
||||
assertSame(created, updated.findPlaylistByName("Created"))
|
||||
coVerify(exactly = 1) { storedPlaylists.new("Created", listOf(song)) }
|
||||
verify(exactly = 1) { playlistInterpreter.interpret("Created", handle) }
|
||||
}
|
||||
|
||||
private fun preSong(
|
||||
uid: String,
|
||||
name: String,
|
||||
v400Uid: String = uid,
|
||||
v401Uid: String = uid,
|
||||
albumName: String = "Album",
|
||||
artistName: String = "Artist",
|
||||
genreName: String = "Genre",
|
||||
cover: Cover? = null,
|
||||
): PreSong {
|
||||
val artist =
|
||||
PreArtist(null, Naming.simple().name(artistName, null), artistName)
|
||||
return PreSong(
|
||||
v363Uid = requireNotNull(Music.UID.fromString(uid)),
|
||||
v400Uid = requireNotNull(Music.UID.fromString(v400Uid)),
|
||||
v401Uid = requireNotNull(Music.UID.fromString(v401Uid)),
|
||||
musicBrainzId = null,
|
||||
name = Naming.simple().name(name, null),
|
||||
rawName = name,
|
||||
track = null,
|
||||
disc = null,
|
||||
date = null,
|
||||
uri = Uri.parse("content://snapshot/$uid"),
|
||||
path =
|
||||
Path(
|
||||
InternalVolumeValue(
|
||||
null,
|
||||
Components.parseUnix("/storage/emulated/0"),
|
||||
"Internal shared storage",
|
||||
),
|
||||
Components.parseUnix("Download/$name.mp3"),
|
||||
),
|
||||
format = Format.MPEG3,
|
||||
size = 1_024L,
|
||||
durationMs = 60_000L,
|
||||
bitrateKbps = 128,
|
||||
sampleRateHz = 44_100,
|
||||
replayGainAdjustment = ReplayGainAdjustment(null, null),
|
||||
modifiedMs = 100L,
|
||||
addedMs = 200L,
|
||||
cover = cover,
|
||||
preAlbum =
|
||||
PreAlbum(
|
||||
musicBrainzId = null,
|
||||
name = Naming.simple().name(albumName, null),
|
||||
rawName = albumName,
|
||||
releaseType = ReleaseType.Album(null),
|
||||
preArtists = PreArtistsFrom.Album(listOf(artist)),
|
||||
),
|
||||
preArtists = listOf(artist),
|
||||
preGenres = listOf(PreGenre(Naming.simple().name(genreName, null), genreName)),
|
||||
)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user