Rewrite tests to run without a database
- Unit tests use an in-memory fake database (internal/testutil/fakedb), temp folders and httptest; `go test ./...` needs no Docker or env vars - Integration tests (build tag "integration") start PostgreSQL with testcontainers: migrations step by step, SQL dumps (skipped if missing) and a full sync against a real database - Removed tests that only checked copies of the logic or had empty bodies, the broken root integration_test.go, compose.test.yaml and .env.test with hardcoded paths - testMusic MP3s replaced with tiny placeholders (sync never decodes audio), 194 MB -> 168 KB
This commit is contained in:
1 parent
090e794460
commit
7b144ae419
42 files changed
+2334
-40
No files matched your search
@@ -0,0 +1,224 @@
|
||||
package backend
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"music-server/internal/db/repository"
|
||||
"music-server/internal/testutil/fakedb"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func writeFile(t *testing.T, path, content string) {
|
||||
t.Helper()
|
||||
require.NoError(t, os.MkdirAll(filepath.Dir(path), 0o755))
|
||||
require.NoError(t, os.WriteFile(path, []byte(content), 0o644))
|
||||
}
|
||||
|
||||
// addSoundtrackWithSongs creates a soundtrack folder with song files and matching database rows.
|
||||
func addSoundtrackWithSongs(t *testing.T, store *fakedb.Store, musicDir, name string, timesPlayed int32, songs ...string) repository.Soundtrack {
|
||||
t.Helper()
|
||||
dir := musicDir + "/" + name + "/"
|
||||
st := store.AddSoundtrack(repository.Soundtrack{SoundtrackName: name, Path: dir, TimesPlayed: timesPlayed})
|
||||
for _, song := range songs {
|
||||
fileName := song + ".mp3"
|
||||
writeFile(t, dir+fileName, song)
|
||||
store.AddTestSong(repository.Song{SoundtrackID: st.ID, SongName: song, Path: dir + fileName, FileName: &fileName})
|
||||
}
|
||||
return st
|
||||
}
|
||||
|
||||
func TestMusicServiceEmptyQueue(t *testing.T) {
|
||||
m := NewMusicService(fakedb.New(), t.TempDir())
|
||||
|
||||
_, err := m.NextSong()
|
||||
assert.ErrorIs(t, err, ErrQueueEmpty)
|
||||
_, err = m.PreviousSong()
|
||||
assert.ErrorIs(t, err, ErrQueueEmpty)
|
||||
_, err = m.Song(0)
|
||||
assert.ErrorIs(t, err, ErrQueueEmpty)
|
||||
assert.Equal(t, SongInfo{}, m.SongInfo(context.Background()))
|
||||
assert.Empty(t, m.PlayedSongs(context.Background()))
|
||||
assert.NoError(t, m.AddLatestPlayed(context.Background()))
|
||||
assert.NoError(t, m.SetPlayed(context.Background(), -1))
|
||||
}
|
||||
|
||||
func TestMusicServiceNoSoundtracks(t *testing.T) {
|
||||
m := NewMusicService(fakedb.New(), t.TempDir())
|
||||
ctx := context.Background()
|
||||
|
||||
_, err := m.RandomSong(ctx)
|
||||
assert.ErrorIs(t, err, ErrNoSongs)
|
||||
_, err = m.RandomSongLowChance(ctx)
|
||||
assert.ErrorIs(t, err, ErrNoSongs)
|
||||
_, err = m.RandomSongClassic(ctx)
|
||||
assert.ErrorIs(t, err, ErrNoSongs)
|
||||
}
|
||||
|
||||
func TestMusicServiceSoundtracksWithoutSongs(t *testing.T) {
|
||||
store := fakedb.New()
|
||||
store.AddSoundtrack(repository.Soundtrack{SoundtrackName: "Empty"})
|
||||
m := NewMusicService(store, t.TempDir())
|
||||
|
||||
_, err := m.RandomSong(context.Background())
|
||||
assert.ErrorIs(t, err, ErrNoSongs)
|
||||
}
|
||||
|
||||
func TestMusicServiceQueue(t *testing.T) {
|
||||
store := fakedb.New()
|
||||
musicDir := t.TempDir()
|
||||
addSoundtrackWithSongs(t, store, musicDir, "Game A", 0, "Song 1", "Song 2")
|
||||
m := NewMusicService(store, t.TempDir())
|
||||
ctx := context.Background()
|
||||
|
||||
first, err := m.RandomSong(ctx)
|
||||
require.NoError(t, err)
|
||||
m.AddLatestToQueue()
|
||||
second, err := m.RandomSong(ctx)
|
||||
require.NoError(t, err)
|
||||
m.AddLatestToQueue()
|
||||
|
||||
info := m.SongInfo(ctx)
|
||||
assert.Equal(t, "Game A", info.Soundtrack)
|
||||
assert.Equal(t, 1, info.SongNo)
|
||||
assert.True(t, info.CurrentlyPlaying)
|
||||
|
||||
path, err := m.PreviousSong()
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, first, path)
|
||||
path, err = m.PreviousSong()
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, first, path, "previous stays on the first song")
|
||||
|
||||
path, err = m.NextSong()
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, second, path)
|
||||
path, err = m.NextSong()
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, second, path, "next stays on the last song")
|
||||
|
||||
path, err = m.Song(99)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, second, path, "song number is clamped")
|
||||
|
||||
played := m.PlayedSongs(ctx)
|
||||
require.Len(t, played, 2)
|
||||
assert.False(t, played[0].CurrentlyPlaying)
|
||||
assert.True(t, played[1].CurrentlyPlaying)
|
||||
|
||||
require.NoError(t, m.Reset(ctx))
|
||||
_, err = m.NextSong()
|
||||
assert.ErrorIs(t, err, ErrQueueEmpty)
|
||||
}
|
||||
|
||||
func TestMusicServiceAddLatestToQueueWithoutFetch(t *testing.T) {
|
||||
m := NewMusicService(fakedb.New(), t.TempDir())
|
||||
m.AddLatestToQueue()
|
||||
_, err := m.NextSong()
|
||||
assert.ErrorIs(t, err, ErrQueueEmpty)
|
||||
}
|
||||
|
||||
func TestMusicServiceMarkPlayed(t *testing.T) {
|
||||
store := fakedb.New()
|
||||
st := addSoundtrackWithSongs(t, store, t.TempDir(), "Game A", 0, "Song 1")
|
||||
m := NewMusicService(store, t.TempDir())
|
||||
ctx := context.Background()
|
||||
|
||||
_, err := m.RandomSong(ctx)
|
||||
require.NoError(t, err)
|
||||
m.AddLatestToQueue()
|
||||
|
||||
require.NoError(t, m.AddLatestPlayed(ctx))
|
||||
require.NoError(t, m.SetPlayed(ctx, 0))
|
||||
require.NoError(t, m.SetPlayed(ctx, 5), "out of range is ignored")
|
||||
|
||||
updated, _ := store.Soundtrack("Game A")
|
||||
assert.Equal(t, int32(2), updated.TimesPlayed)
|
||||
assert.Equal(t, int32(2), store.SongsOf(st.ID)[0].TimesPlayed)
|
||||
}
|
||||
|
||||
func TestMusicServiceRemovesBrokenSongs(t *testing.T) {
|
||||
store := fakedb.New()
|
||||
musicDir := t.TempDir()
|
||||
st := addSoundtrackWithSongs(t, store, musicDir, "Game A", 0, "Good")
|
||||
missing := "Missing.mp3"
|
||||
store.AddTestSong(repository.Song{SoundtrackID: st.ID, SongName: "Missing", Path: st.Path + missing, FileName: &missing})
|
||||
wav := "Wave.wav"
|
||||
writeFile(t, st.Path+wav, "wav")
|
||||
store.AddTestSong(repository.Song{SoundtrackID: st.ID, SongName: "Wave", Path: st.Path + wav, FileName: &wav})
|
||||
m := NewMusicService(store, t.TempDir())
|
||||
|
||||
for range 20 {
|
||||
path, err := m.RandomSong(context.Background())
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, st.Path+"Good.mp3", path)
|
||||
}
|
||||
songs := store.SongsOf(st.ID)
|
||||
require.Len(t, songs, 1)
|
||||
assert.Equal(t, "Good", songs[0].SongName)
|
||||
}
|
||||
|
||||
func TestMusicServiceRandomSongClassic(t *testing.T) {
|
||||
store := fakedb.New()
|
||||
st := addSoundtrackWithSongs(t, store, t.TempDir(), "Game A", 0, "Good")
|
||||
missing := "Missing.mp3"
|
||||
store.AddTestSong(repository.Song{SoundtrackID: st.ID, SongName: "Missing", Path: st.Path + missing, FileName: &missing})
|
||||
m := NewMusicService(store, t.TempDir())
|
||||
|
||||
for range 20 {
|
||||
path, err := m.RandomSongClassic(context.Background())
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, st.Path+"Good.mp3", path)
|
||||
}
|
||||
assert.Len(t, store.SongsOf(st.ID), 1)
|
||||
}
|
||||
|
||||
func TestLowChanceWeights(t *testing.T) {
|
||||
soundtracks := []repository.Soundtrack{
|
||||
{SoundtrackName: "Often", TimesPlayed: 30},
|
||||
{SoundtrackName: "Average", TimesPlayed: 20},
|
||||
{SoundtrackName: "Rarely", TimesPlayed: 10},
|
||||
}
|
||||
weights := lowChanceWeights(soundtracks)
|
||||
assert.Equal(t, []int{1, 1, 10}, []int{weights[0].weight, weights[1].weight, weights[2].weight})
|
||||
}
|
||||
|
||||
func TestAveragePlayed(t *testing.T) {
|
||||
assert.Equal(t, int32(0), averagePlayed(nil))
|
||||
assert.Equal(t, int32(20), averagePlayed([]repository.Soundtrack{{TimesPlayed: 10}, {TimesPlayed: 30}}))
|
||||
}
|
||||
|
||||
func TestSoundtrackNames(t *testing.T) {
|
||||
store := fakedb.New()
|
||||
store.AddSoundtrack(repository.Soundtrack{SoundtrackName: "B"})
|
||||
store.AddSoundtrack(repository.Soundtrack{SoundtrackName: "A"})
|
||||
m := NewMusicService(store, t.TempDir())
|
||||
|
||||
names, err := m.SoundtrackNames(context.Background())
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, []string{"A", "B"}, names)
|
||||
|
||||
random, err := m.SoundtrackNamesRandom(context.Background())
|
||||
require.NoError(t, err)
|
||||
assert.ElementsMatch(t, []string{"A", "B"}, random)
|
||||
assert.Equal(t, []string{"A", "B"}, names, "shuffling must not change the cached order")
|
||||
}
|
||||
|
||||
func TestSoundCheckSong(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
m := NewMusicService(fakedb.New(), dir)
|
||||
_, err := m.SoundCheckSong()
|
||||
assert.ErrorIs(t, err, ErrNoSongs)
|
||||
|
||||
writeFile(t, filepath.Join(dir, "check.mp3"), "x")
|
||||
path, err := m.SoundCheckSong()
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, filepath.Join(dir, "check.mp3"), path)
|
||||
|
||||
_, err = NewMusicService(fakedb.New(), filepath.Join(dir, "missing")).SoundCheckSong()
|
||||
assert.Error(t, err)
|
||||
}
|
||||
Reference in new issue
Block a user