Files
MusicServer/internal/backend/library_test.go
T
Sansan 2c3e97190d
CI / docker (push) Successful in 46s
CI / integration (push) Successful in 23s
CI / test (push) Successful in 30s
CI / publish (push) Skipped
Prefer UUIDs in the API (Phase 3 of the UUID migration)
- New /api/v1 endpoints: GET /soundtracks, /soundtracks/{uuid},
  /songs/{uuid} and /songs/{uuid}/file (streams the song, Range support,
  locked during sync)
- UUIDs added to every response with soundtracks or songs: queue info and
  list (SoundtrackUuid, SongUuid) and statistics (soundtrack_uuid,
  song_uuid). Existing fields stay for backward compatibility
- Play counts and broken-song removal use song UUIDs instead of
  soundtrack ID + song name, so songs with the same name are counted
  separately
- Statistics return an empty song list for soundtracks without songs
  instead of a single empty entry
- The legacy .id file cleanup stays until Phase 4 (#31)

Closes #29
2026-10-06 15:54:41 +02:00

83 lines
2.7 KiB
Go

package backend
import (
"context"
"os"
"testing"
"time"
"music-server/internal/db/repository"
"music-server/internal/testutil/fakedb"
"github.com/google/uuid"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestLibrarySoundtracks(t *testing.T) {
store := fakedb.New()
b := store.AddSoundtrack(repository.Soundtrack{SoundtrackName: "B Game", TimesPlayed: 3})
a := store.AddSoundtrack(repository.Soundtrack{SoundtrackName: "A Game"})
deleted := time.Now()
store.AddSoundtrack(repository.Soundtrack{SoundtrackName: "Gone", Deleted: &deleted})
soundtracks, err := NewLibraryService(store).Soundtracks(context.Background())
require.NoError(t, err)
require.Len(t, soundtracks, 2)
assert.Equal(t, Soundtrack{Uuid: a.Uuid, Name: "A Game"}, soundtracks[0])
assert.Equal(t, b.Uuid, soundtracks[1].Uuid)
assert.Equal(t, int32(3), soundtracks[1].TimesPlayed)
}
func TestLibrarySoundtrackWithSongs(t *testing.T) {
store := fakedb.New()
st := addSoundtrackWithSongs(t, store, t.TempDir(), "Game A", 0, "Song B", "Song A")
l := NewLibraryService(store)
result, err := l.Soundtrack(context.Background(), st.Uuid)
require.NoError(t, err)
assert.Equal(t, "Game A", result.Name)
require.Len(t, result.Songs, 2)
assert.Equal(t, "Song A", result.Songs[0].Name)
assert.Equal(t, st.Uuid, result.Songs[0].SoundtrackUuid)
assert.Equal(t, "Game A", result.Songs[0].SoundtrackName)
_, err = l.Soundtrack(context.Background(), uuid.New())
assert.ErrorIs(t, err, ErrNotFound)
}
func TestLibrarySong(t *testing.T) {
store := fakedb.New()
st := addSoundtrackWithSongs(t, store, t.TempDir(), "Game A", 0, "Song")
song := store.SongsOf(st.ID)[0]
l := NewLibraryService(store)
ctx := context.Background()
result, err := l.Song(ctx, song.Uuid)
require.NoError(t, err)
assert.Equal(t, Song{Uuid: song.Uuid, Name: "Song", FileName: song.FileName, SoundtrackUuid: st.Uuid, SoundtrackName: "Game A"}, result)
path, err := l.SongPath(ctx, song.Uuid)
require.NoError(t, err)
assert.Equal(t, song.Path, path)
require.NoError(t, os.Remove(song.Path))
_, err = l.SongPath(ctx, song.Uuid)
assert.ErrorIs(t, err, ErrNotFound, "missing files are not served")
_, err = l.Song(ctx, uuid.New())
assert.ErrorIs(t, err, ErrNotFound)
}
func TestLibraryHidesSongsOfDeletedSoundtracks(t *testing.T) {
store := fakedb.New()
st := addSoundtrackWithSongs(t, store, t.TempDir(), "Game A", 0, "Song")
song := store.SongsOf(st.ID)[0]
require.NoError(t, store.MarkSoundtracksDeletedExcept(context.Background(), nil))
_, err := NewLibraryService(store).Song(context.Background(), song.Uuid)
assert.ErrorIs(t, err, ErrNotFound)
_, err = NewLibraryService(store).Soundtrack(context.Background(), st.Uuid)
assert.ErrorIs(t, err, ErrNotFound)
}