fix: allow empty search results from jikan

This commit is contained in:
2026-06-16 17:20:39 +02:00
committed by Milas Holsting
parent d4e6de9e98
commit bb8aac06eb
2 changed files with 27 additions and 1 deletions

View File

@@ -180,7 +180,7 @@ func isEmptyResult(out any) bool {
case *TopAnimeResponse:
return len(v.Data) == 0
case *SearchResponse:
return len(v.Data) == 0
return false
case *AnimeResponse:
return v.Data.MalID == 0
case *EpisodesResponse:

View File

@@ -52,6 +52,32 @@ func TestGetWithCacheReturnsStaleAndRefreshesAsync(t *testing.T) {
waitForFreshCache(t, sqlDB, client, "top:1")
}
func TestGetWithCacheAllowsEmptySearchResults(t *testing.T) {
sqlDB := newTestCacheDB(t)
defer sqlDB.Close()
queries := db.New(sqlDB)
client := NewClient(config.Config{}, queries, observability.NewMetrics())
client.fetcher.HTTPClient = &http.Client{
Transport: roundTripFunc(func(*http.Request) (*http.Response, error) {
body := `{"pagination":{"has_next_page":false},"data":[]}`
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(strings.NewReader(body)),
Header: make(http.Header),
}, nil
}),
}
var got SearchResponse
if err := client.getWithCache(context.Background(), "search::::::12:0:true:1:24", time.Hour, "https://example.test/anime?genres=12", &got); err != nil {
t.Fatalf("getWithCache() returned error for empty search response: %v", err)
}
if len(got.Data) != 0 {
t.Fatalf("getWithCache() data length = %d, want 0", len(got.Data))
}
}
func newTestCacheDB(t *testing.T) *sql.DB {
t.Helper()
ctx := context.Background()