feat: enforce strict mal id in allanime getstreams

This commit is contained in:
2026-07-01 18:53:19 +02:00
parent 223c0fa89e
commit 2918fa5051
3 changed files with 27 additions and 32 deletions

View File

@@ -55,8 +55,8 @@ func (c *AllAnimeProvider) apiBaseURL() string {
}
func (c *AllAnimeProvider) GetStreams(ctx context.Context, animeID int, titleCandidates []string, episode string, mode string) (*domain.StreamResult, error) {
showID := c.showID(ctx, animeID, titleCandidates, mode)
if showID == "" {
showID, err := c.strictShowID(ctx, animeID, titleCandidates, mode)
if err != nil {
return nil, fmt.Errorf("allanime: show not found for malID %d", animeID)
}

View File

@@ -8,6 +8,8 @@ import (
"crypto/sha256"
"encoding/base64"
"mal/internal/domain"
"net/http"
"strings"
"testing"
)
@@ -248,6 +250,29 @@ func TestBuildStreamSource(t *testing.T) {
})
}
func TestGetStreamsRequiresExactMalIDMatch(t *testing.T) {
t.Parallel()
provider := &AllAnimeProvider{
httpClient: &http.Client{
Transport: roundTripFunc(func(req *http.Request) (*http.Response, error) {
return mockStringResponse(http.StatusOK, `{"data":{"shows":{"edges":[{"_id":"wrong-show","malId":"1","name":"Wrong Anime"}]}}}`), nil
}),
},
utlsClient: &http.Client{
Transport: roundTripFunc(func(req *http.Request) (*http.Response, error) {
t.Fatal("GetStreams should not fetch episode sources without an exact MAL ID match")
return nil, nil
}),
},
}
_, err := provider.GetStreams(context.Background(), 62076, []string{"Super no Ura de Yani Suu Futari"}, "1", "sub")
if err == nil || !strings.Contains(err.Error(), "show not found") {
t.Fatalf("GetStreams() error = %v, want show not found", err)
}
}
func TestResolveDirectSourceSkipsEmbeds(t *testing.T) {
t.Parallel()

View File

@@ -99,36 +99,6 @@ func (c *AllAnimeProvider) Search(ctx context.Context, query string, mode string
return out, nil
}
func (c *AllAnimeProvider) showID(ctx context.Context, animeID int, titleCandidates []string, mode string) string {
targetMalIDStr := strconv.Itoa(animeID)
fallbackID := ""
for _, title := range titleCandidates {
searchResults, err := c.Search(ctx, title, mode)
if err != nil || len(searchResults) == 0 {
continue
}
if showID := exactMatchShowID(searchResults, targetMalIDStr); showID != "" {
return showID
}
if fallbackID == "" {
fallbackID = searchResults[0].ID
}
}
return fallbackID
}
func exactMatchShowID(searchResults []searchResult, targetMalID string) string {
for _, res := range searchResults {
if res.MalID == targetMalID {
return res.ID
}
}
return ""
}
func (c *AllAnimeProvider) ResolveEpisodeProviderID(ctx context.Context, animeID int, titleCandidates []string) (string, error) {
for _, mode := range []string{"sub", "dub"} {
showID, err := c.strictShowID(ctx, animeID, titleCandidates, mode)