internal/episode_url_test.go

117 lines · 2972 bytes

  1package internal
  2
  3import "testing"
  4
  5func TestGetLinksFromSourceUrlsPrefersDirectPlayableSources(t *testing.T) {
  6	t.Parallel()
  7
  8	sourceUrls := []allanimeSource{
  9		{
 10			SourceUrl:  "--175948514e4c4f57",
 11			SourceName: "Luf-Mp4",
 12			Priority:   7.5,
 13		},
 14		{
 15			SourceUrl:  "https://tools.fast4speed.rsvp/media9/videos/example/sub/1?Authorization=test",
 16			SourceName: "Yt-mp4",
 17			Priority:   7.9,
 18		},
 19		{
 20			SourceUrl:  "https://mp4upload.com/embed-example.html",
 21			SourceName: "Mp4",
 22			Priority:   4,
 23		},
 24	}
 25
 26	links, err := getLinksFromSourceUrls(sourceUrls)
 27	if err != nil {
 28		t.Fatalf("expected direct Allanime sources to be returned without error, got %v", err)
 29	}
 30
 31	if len(links) != 1 {
 32		t.Fatalf("expected one direct playable source, got %d: %#v", len(links), links)
 33	}
 34
 35	if got, want := links[0], "https://tools.fast4speed.rsvp/media9/videos/example/sub/1?Authorization=test"; got != want {
 36		t.Fatalf("getLinksFromSourceUrls() returned %q, want %q", got, want)
 37	}
 38}
 39
 40func TestSortAllanimeSourcesByPriorityOrdersDescending(t *testing.T) {
 41	t.Parallel()
 42
 43	sourceUrls := []allanimeSource{
 44		{SourceUrl: "https://example.com/low.mp4", Priority: 1},
 45		{SourceUrl: "https://example.com/high.mp4", Priority: 9},
 46		{SourceUrl: "https://example.com/mid.mp4", Priority: 5},
 47	}
 48
 49	sorted := sortAllanimeSourcesByPriority(sourceUrls)
 50	if len(sorted) != 3 {
 51		t.Fatalf("expected 3 sorted sources, got %d", len(sorted))
 52	}
 53
 54	if sorted[0].SourceUrl != "https://example.com/high.mp4" {
 55		t.Fatalf("expected highest priority source first, got %#v", sorted)
 56	}
 57	if sorted[1].SourceUrl != "https://example.com/mid.mp4" {
 58		t.Fatalf("expected middle priority source second, got %#v", sorted)
 59	}
 60	if sorted[2].SourceUrl != "https://example.com/low.mp4" {
 61		t.Fatalf("expected lowest priority source last, got %#v", sorted)
 62	}
 63}
 64
 65func TestIsDirectPlayableAllanimeSource(t *testing.T) {
 66	t.Parallel()
 67
 68	cases := []struct {
 69		name   string
 70		source allanimeSource
 71		want   bool
 72	}{
 73		{
 74			name: "fast4speed player url",
 75			source: allanimeSource{
 76				SourceName: "Yt-mp4",
 77				SourceUrl:  "https://tools.fast4speed.rsvp/media9/videos/example/sub/1?Authorization=test",
 78			},
 79			want: true,
 80		},
 81		{
 82			name: "sharepoint mp4 source",
 83			source: allanimeSource{
 84				SourceName: "S-mp4",
 85				SourceUrl:  "https://tenant.sharepoint.com/video.mp4",
 86			},
 87			want: true,
 88		},
 89		{
 90			name: "mp4upload embed url",
 91			source: allanimeSource{
 92				SourceName: "Mp4",
 93				SourceUrl:  "https://mp4upload.com/embed-example.html",
 94			},
 95			want: false,
 96		},
 97		{
 98			name: "filemoon embed url",
 99			source: allanimeSource{
100				SourceName: "Fm-Hls",
101				SourceUrl:  "https://bysekoze.com/e/example",
102			},
103			want: false,
104		},
105	}
106
107	for _, tc := range cases {
108		tc := tc
109		t.Run(tc.name, func(t *testing.T) {
110			t.Parallel()
111
112			if got := isDirectPlayableAllanimeSource(tc.source); got != tc.want {
113				t.Fatalf("isDirectPlayableAllanimeSource(%+v) = %v, want %v", tc.source, got, tc.want)
114			}
115		})
116	}
117}