internal/provider_match_test.go
68 lines · 2515 bytes
1package internal
2
3import (
4 "regexp"
5 "strings"
6 "testing"
7)
8
9func TestSelectBestProviderSearchResultMarchComesInLikeALion(t *testing.T) {
10 anime := &Anime{
11 AnilistId: 21366,
12 MalId: 31646,
13 TotalEpisodes: 22,
14 Title: AnimeTitle{Romaji: "3-gatsu no Lion", English: "March comes in like a lion"},
15 }
16 options := []SelectionOption{
17 {Title: "March comes in like a lion Season 2", Key: "s2", Label: "March comes in like a lion Season 2 (22 episodes)", Thumbnail: "https://s4.anilist.co/file/anilistcdn/media/anime/cover/large/bx98478-dF3mpSKiZkQu.jpg"},
18 {Title: "March comes in like a lion", Key: "s1", Label: "March comes in like a lion (23 episodes)", Thumbnail: "https://cdn.myanimelist.net/images/anime/1637/108857.jpg"},
19 }
20 best, ok := selectBestProviderSearchResult(options, anime, "3-gatsu no Lion")
21 if !ok {
22 t.Fatal("expected match")
23 }
24 if best.Key != "s1" {
25 t.Fatalf("expected season 1, got %q (%q)", best.Key, best.Title)
26 }
27}
28
29func TestConfidentProviderSearchMatchMarchComesInLikeALion(t *testing.T) {
30 anime := &Anime{
31 AnilistId: 21366,
32 MalId: 31646,
33 TotalEpisodes: 22,
34 Title: AnimeTitle{Romaji: "3-gatsu no Lion", English: "March comes in like a lion"},
35 }
36 options := []SelectionOption{
37 {Title: "March comes in like a lion Season 2", Key: "s2", Label: "March comes in like a lion Season 2 (22 episodes)", Thumbnail: "https://s4.anilist.co/file/anilistcdn/media/anime/cover/large/bx98478-dF3mpSKiZkQu.jpg"},
38 {Title: "March comes in like a lion", Key: "s1", Label: "March comes in like a lion (23 episodes)", Thumbnail: "https://cdn.myanimelist.net/images/anime/1637/108857.jpg"},
39 }
40 best, ok := confidentProviderSearchMatch(options, anime, "3-gatsu no Lion")
41 if !ok {
42 t.Fatal("expected confident auto-match")
43 }
44 if best.Key != "s1" {
45 t.Fatalf("expected season 1, got %q", best.Key)
46 }
47}
48
49func TestMalThumbnailMatchMarchComesInLikeALion(t *testing.T) {
50 jikanUrls := []string{"https://cdn.myanimelist.net/images/anime/1637/108857.jpg"}
51 thumb := "https://cdn.myanimelist.net/images/anime/1637/108857.jpg"
52 malRegex := regexp.MustCompile(`myanimelist\.net/images/anime/[^/]+/([^/]+\.jpg)`)
53 matches := malRegex.FindStringSubmatch(thumb)
54 if len(matches) < 2 {
55 t.Fatal("regex failed")
56 }
57 fileName := matches[1]
58 found := false
59 for _, url := range jikanUrls {
60 if strings.HasSuffix(url, "/"+fileName) || strings.Contains(url, fileName) {
61 found = true
62 break
63 }
64 }
65 if !found {
66 t.Fatal("expected MAL thumbnail match")
67 }
68}