Files
MusicServer/internal/backend/uploads_integration_test.go
T
SansanandClaude Opus 5.5 a7e2280969
CI / integration (pull_request) Successful in 21s
CI / test (pull_request) Successful in 26s
CI / publish (pull_request) Skipped
CI / docker (pull_request) Successful in 43s
Upload new soundtracks through the API
A client starts an upload with the soundtrack's name, sends one MP3 per
request, and finishes it. The server then moves the songs into a new folder
in the music folder and syncs only that folder, so the soundtrack is in the
library, lists and search right away.

- POST /api/v1/soundtracks/uploads, GET/DELETE .../uploads/{id},
  PUT .../uploads/{id}/files/{file} and POST .../uploads/{id}/finish.
- Names must work as file names on every system and be free, ignoring case.
  File names are plain: clients flatten disc folders.
- Only MP3 files. Size limits from UPLOAD_MAX_FILE_MB (50) and
  UPLOAD_MAX_TOTAL_MB (2048).
- Refused while a sync runs, and a sync cannot start while an upload is
  finished. Songs equal to an existing soundtrack's are refused, as the sync
  would take them for that soundtrack renamed.
- Unfinished uploads live in MUSIC_PATH/.uploads and are removed after a day.
  The sync now skips every hidden folder.

Closes #33

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-15 21:38:26 +02:00

78 lines
2.6 KiB
Go

//go:build integration
package backend
import (
"context"
"path/filepath"
"testing"
"music-server/internal/db/repository"
"music-server/internal/testutil/pgtest"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// TestUploadWithPostgres uploads songs and checks the soundtrack in a real database.
func TestUploadWithPostgres(t *testing.T) {
ctx := context.Background()
q := repository.New(pgtest.NewMigratedDatabase(t).Pool)
musicDir := t.TempDir()
writeFile(t, filepath.Join(musicDir, "Original", "01 Song.mp3"), string(mp3("original one")))
writeFile(t, filepath.Join(musicDir, "Original", "02 Song.mp3"), string(mp3("original two")))
s := NewSyncService(q, musicDir, nil)
require.True(t, s.Start(false))
s.Wait()
require.Empty(t, s.Result().CatchedErrors)
library := NewLibraryService(q)
uploads := NewUploadService(q, musicDir, s, library, 50<<20, 500<<20)
upload := func(name string, songs map[string]string) Upload {
u, err := uploads.Start(ctx, name)
require.NoError(t, err)
for file, content := range songs {
_, err := uploads.PutFile(u.ID, file, mp3(content))
require.NoError(t, err)
}
return u
}
// The songs of an existing soundtrack under a new name are refused.
duplicate := upload("Copy", map[string]string{"01 Song.mp3": "original one", "02 Song.mp3": "original two"})
_, err := uploads.Finish(ctx, duplicate.ID)
assert.ErrorIs(t, err, ErrConflict)
require.NoError(t, uploads.Cancel(duplicate.ID))
// The name of an existing soundtrack is refused.
_, err = uploads.Start(ctx, "original")
assert.ErrorIs(t, err, ErrConflict)
added := upload("Uploaded Soundtrack", map[string]string{"1-01 First.mp3": "first", "1-02 Second.mp3": "second"})
soundtrack, err := uploads.Finish(ctx, added.ID)
require.NoError(t, err)
assert.Equal(t, "Uploaded Soundtrack", soundtrack.Name)
require.Len(t, soundtrack.Songs, 2)
assert.Equal(t, "1-01 First", soundtrack.Songs[0].Name)
path, err := library.SongPath(ctx, soundtrack.Songs[0].Uuid)
require.NoError(t, err)
assert.FileExists(t, path)
count, err := q.CountLibrary(ctx)
require.NoError(t, err)
assert.Equal(t, int64(2), count.Soundtracks)
assert.Equal(t, int64(4), count.Songs)
// A sync afterwards changes nothing.
require.True(t, s.Start(false))
s.Wait()
result := s.Result()
assert.Empty(t, result.CatchedErrors)
assert.Empty(t, result.SoundtracksAdded)
assert.Empty(t, result.SoundtracksChangedTitle)
assert.Empty(t, result.SoundtracksRemoved)
again, err := library.Soundtrack(ctx, soundtrack.Uuid)
require.NoError(t, err)
assert.Len(t, again.Songs, 2)
}