fix: update watchlist service tests
This commit is contained in:
@@ -123,10 +123,10 @@ func (h *Handler) HandleWatchPage(w http.ResponseWriter, r *http.Request) {
|
|||||||
for i := start; i <= maxCount; i++ {
|
for i := start; i <= maxCount; i++ {
|
||||||
epStr := strconv.Itoa(i)
|
epStr := strconv.Itoa(i)
|
||||||
meta, err := h.svc.GetEpisodeMetadata(r.Context(), id, epStr)
|
meta, err := h.svc.GetEpisodeMetadata(r.Context(), id, epStr)
|
||||||
|
|
||||||
title := fmt.Sprintf("Episode %d", i)
|
title := fmt.Sprintf("Episode %d", i)
|
||||||
imgURL := ""
|
imgURL := ""
|
||||||
|
|
||||||
if err == nil && meta != nil {
|
if err == nil && meta != nil {
|
||||||
if info, ok := meta["episodeInfo"].(map[string]any); ok {
|
if info, ok := meta["episodeInfo"].(map[string]any); ok {
|
||||||
if thumbs, ok := info["thumbnails"].([]any); ok && len(thumbs) > 0 {
|
if thumbs, ok := info["thumbnails"].([]any); ok && len(thumbs) > 0 {
|
||||||
|
|||||||
@@ -35,14 +35,14 @@ type SkipSegment struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type WatchPageData struct {
|
type WatchPageData struct {
|
||||||
MalID int
|
MalID int
|
||||||
Title string
|
Title string
|
||||||
CurrentEpisode string
|
CurrentEpisode string
|
||||||
StartTimeSeconds float64
|
StartTimeSeconds float64
|
||||||
CurrentStatus string
|
CurrentStatus string
|
||||||
InitialMode string
|
InitialMode string
|
||||||
AvailableModes []string
|
AvailableModes []string
|
||||||
ModeSources map[string]ModeSource
|
ModeSources map[string]ModeSource
|
||||||
Segments []SkipSegment
|
Segments []SkipSegment
|
||||||
FallbackEpisodes map[string]int
|
FallbackEpisodes map[string]int
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -32,12 +32,9 @@ func TestAddEntry_RejectsInvalidAnimeID(t *testing.T) {
|
|||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
q := &fakeQuerier{}
|
q := &fakeQuerier{}
|
||||||
svc := NewService(q, nil)
|
svc := NewService(q, nil, nil)
|
||||||
|
|
||||||
err := svc.AddEntry(context.Background(), "user-1", AddRequest{
|
err := svc.AddToWatchlist(context.Background(), "user-1", 0, "watching")
|
||||||
AnimeID: 0,
|
|
||||||
Status: "watching",
|
|
||||||
})
|
|
||||||
|
|
||||||
if err != ErrInvalidAnimeID {
|
if err != ErrInvalidAnimeID {
|
||||||
t.Fatalf("expected ErrInvalidAnimeID, got %v", err)
|
t.Fatalf("expected ErrInvalidAnimeID, got %v", err)
|
||||||
@@ -52,12 +49,9 @@ func TestAddEntry_RejectsInvalidStatus(t *testing.T) {
|
|||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
q := &fakeQuerier{}
|
q := &fakeQuerier{}
|
||||||
svc := NewService(q, nil)
|
svc := NewService(q, nil, nil)
|
||||||
|
|
||||||
err := svc.AddEntry(context.Background(), "user-1", AddRequest{
|
err := svc.AddToWatchlist(context.Background(), "user-1", 1, "invalid")
|
||||||
AnimeID: 1,
|
|
||||||
Status: "invalid",
|
|
||||||
})
|
|
||||||
|
|
||||||
if err != ErrInvalidStatus {
|
if err != ErrInvalidStatus {
|
||||||
t.Fatalf("expected ErrInvalidStatus, got %v", err)
|
t.Fatalf("expected ErrInvalidStatus, got %v", err)
|
||||||
|
|||||||
@@ -43,7 +43,7 @@ func (e *Episode) GetFallbackImage(animeID int) string {
|
|||||||
// MAL URLs usually follow this format, and it redirects to the slug version
|
// MAL URLs usually follow this format, and it redirects to the slug version
|
||||||
episodeURL := fmt.Sprintf("https://myanimelist.net/anime/%d/_/episode/%d", animeID, episodeNum)
|
episodeURL := fmt.Sprintf("https://myanimelist.net/anime/%d/_/episode/%d", animeID, episodeNum)
|
||||||
fallbackURL := scrapeAnimeImageFromEpisodePage(episodeURL, episodeNum)
|
fallbackURL := scrapeAnimeImageFromEpisodePage(episodeURL, episodeNum)
|
||||||
|
|
||||||
if fallbackURL != "" {
|
if fallbackURL != "" {
|
||||||
return fallbackURL
|
return fallbackURL
|
||||||
}
|
}
|
||||||
@@ -79,7 +79,7 @@ func scrapeAnimeImageFromEpisodePage(episodeURL string, episodeNum int) string {
|
|||||||
|
|
||||||
html := string(body)
|
html := string(body)
|
||||||
|
|
||||||
// MAL sometimes redirects to a URL with a slug.
|
// MAL sometimes redirects to a URL with a slug.
|
||||||
// The JSON object is very likely to be present in the full page.
|
// The JSON object is very likely to be present in the full page.
|
||||||
// We extract the object {} containing "episode_number":X
|
// We extract the object {} containing "episode_number":X
|
||||||
episodeStr := strconv.Itoa(episodeNum)
|
episodeStr := strconv.Itoa(episodeNum)
|
||||||
@@ -90,7 +90,7 @@ func scrapeAnimeImageFromEpisodePage(episodeURL string, episodeNum int) string {
|
|||||||
objPattern = regexp.MustCompile(`\{[^}]*"episode_number":\s*` + episodeStr + `[^}]*\}`)
|
objPattern = regexp.MustCompile(`\{[^}]*"episode_number":\s*` + episodeStr + `[^}]*\}`)
|
||||||
match = objPattern.FindString(html)
|
match = objPattern.FindString(html)
|
||||||
}
|
}
|
||||||
|
|
||||||
if match != "" {
|
if match != "" {
|
||||||
thumbRe := regexp.MustCompile(`"thumbnail":\s*"([^"]+)"`)
|
thumbRe := regexp.MustCompile(`"thumbnail":\s*"([^"]+)"`)
|
||||||
thumbMatch := thumbRe.FindStringSubmatch(match)
|
thumbMatch := thumbRe.FindStringSubmatch(match)
|
||||||
|
|||||||
Reference in New Issue
Block a user