fix: 榜单逐项 lookup 带上榜单地区

ChartsContext 对榜单条目逐个 lookup 时不带 country,Apple 缺省查 US 区,
只在其他地区上架的节目因此查不到并被静默丢弃。实测 CN 区 1498(语言学习)
榜前 50 档,不带地区只查回 48 档,带 country=cn 查全 50 档。

补测试:lookup 请求带上 ChartsOptions.Country,只在该地区上架的节目能被查回。

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
ans
2026-09-29 19:28:32 +08:00
co-authored by Claude Opus 5.5
parent f8bdd7f08f
commit 9e9b031956
2 changed files with 59 additions and 0 deletions
+54
View File
@@ -0,0 +1,54 @@
package search
import (
"context"
"net/http"
"net/http/httptest"
"strings"
"sync"
"testing"
"go.uber.org/ratelimit"
)
// 榜单逐项 lookup 必须带上榜单地区:只在该地区上架的节目,缺省(US 区)lookup 查不到。
func TestChartsContextLookupUsesChartCountry(t *testing.T) {
var mu sync.Mutex
var lookupQueries []string
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch {
case strings.HasPrefix(r.URL.Path, "/cn/rss/toppodcasts/"):
_, _ = w.Write([]byte(`{"feed":{"entry":[{"id":{"attributes":{"im:id":"1796650777"}},"summary":{"label":"s"}}]}}`))
case r.URL.Path == "/lookup":
mu.Lock()
lookupQueries = append(lookupQueries, r.URL.RawQuery)
mu.Unlock()
if r.URL.Query().Get("country") != "cn" {
_, _ = w.Write([]byte(`{"resultCount":0,"results":[]}`))
return
}
_, _ = w.Write([]byte(`{"resultCount":1,"results":[{"collectionId":1796650777,"collectionName":"CN Only","feedUrl":"https://f"}]}`))
default:
http.NotFound(w, r)
}
}))
defer srv.Close()
p := NewITunesProvider()
p.FeedAPIEndpoint = srv.URL
p.Limiter = ratelimit.NewUnlimited()
result, err := p.ChartsContext(context.Background(), &ChartsOptions{Country: "cn", GenreID: "1498", Limit: 1})
if err != nil {
t.Fatalf("ChartsContext: %v", err)
}
if len(lookupQueries) != 1 || !strings.Contains(lookupQueries[0], "country=cn") {
t.Fatalf("lookup 应带 country=cn,实际 %v", lookupQueries)
}
if result.ResultCount != 1 || result.Items[0].CollectionName != "CN Only" {
t.Fatalf("只在 CN 区上架的节目应被查回,实际 %+v", result.Items)
}
if result.Items[0].Summary != "s" {
t.Fatalf("简介取自榜单条目,实际 %q", result.Items[0].Summary)
}
}