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) }