Files
MusicServer/internal/backend/search_cache_test.go
T
Sansan 7b144ae419 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
2026-10-06 15:20:31 +02:00

68 lines
2.0 KiB
Go

package backend
import (
"context"
"errors"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestCleanSearchTerm(t *testing.T) {
tests := map[string]string{
"MGS4": "mgs4",
" test ": "test",
"Test 123": "test123",
"A+B": "aplusb",
"Mario & Luigi": "marioandluigi",
"É": "e",
"It's": "it's",
"Rock 'n Roll": "rockandroll",
"": "",
}
for input, expected := range tests {
assert.Equal(t, expected, CleanSearchTerm(input), input)
}
}
func TestMatchesSubsequence(t *testing.T) {
assert.True(t, MatchesSubsequence("", "anything"))
assert.True(t, MatchesSubsequence("ff7", "finalfantasy7"))
assert.True(t, MatchesSubsequence("lumière", "clairobscur-lumière"))
assert.False(t, MatchesSubsequence("7ff", "finalfantasy7"))
// Characters that are special in regular expressions are matched literally.
for _, term := range []string{"(", "[", "*", "\\", "a(b", ".*"} {
assert.NotPanics(t, func() { MatchesSubsequence(term, "plain text") }, term)
assert.False(t, MatchesSubsequence(term, "plain text"), term)
}
assert.True(t, MatchesSubsequence("(", "game (remake)"))
}
func TestSearchCache(t *testing.T) {
names := []string{"Final Fantasy VII"}
calls := 0
cache := NewSearchCache(func(context.Context) ([]string, error) {
calls++
return names, nil
})
ctx := context.Background()
data, err := cache.Get(ctx)
require.NoError(t, err)
assert.Equal(t, []SoundtrackData{{Original: "Final Fantasy VII", Lowercase: "final fantasy vii", Cleaned: "finalfantasyvii"}}, data)
_, _ = cache.Get(ctx)
assert.Equal(t, 1, calls, "data is cached")
names = []string{"Chrono Trigger"}
require.NoError(t, cache.Reload(ctx))
data, _ = cache.Get(ctx)
assert.Equal(t, "Chrono Trigger", data[0].Original)
}
func TestSearchCacheLoadError(t *testing.T) {
cache := NewSearchCache(func(context.Context) ([]string, error) { return nil, errors.New("db down") })
_, err := cache.Get(context.Background())
assert.Error(t, err)
}