mirror of
https://github.com/truewhile/MeBox.git
synced 2026-09-28 11:16:37 +08:00
65d1ad5ae5
优化番号处理逻辑,添加对MetaTube的支持
196 lines
6.4 KiB
Go
196 lines
6.4 KiB
Go
package service
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
"github.com/ShukeBta/MediaStationGo/internal/model"
|
|
"github.com/ShukeBta/MediaStationGo/internal/repository"
|
|
)
|
|
|
|
func TestNormalizeAdultCode(t *testing.T) {
|
|
cases := map[string]string{
|
|
"SSIS001.mp4": "SSIS-001",
|
|
"fc2-ppv-1234567.mkv": "FC2-PPV-1234567",
|
|
"heyzo_1234.mp4": "HEYZO-1234",
|
|
"120118_001-carib.mp4": "120118-001",
|
|
"movie.1080p.x264.mkv": "",
|
|
}
|
|
for in, want := range cases {
|
|
if got := normalizeAdultCode(in); got != want {
|
|
t.Fatalf("normalizeAdultCode(%q) = %q, want %q", in, got, want)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestParseAdultDetailHTML(t *testing.T) {
|
|
html := `<html>
|
|
<h2 class="title"><strong>SSIS-001 测试标题</strong></h2>
|
|
<img class="video-cover" src="/covers/ssis001.jpg">
|
|
<a class="sample-box" href="/samples/1.jpg"></a>
|
|
<span class="score"><span class="value">4.7</span></span>
|
|
<div>日期 2024-05-01</div>
|
|
</html>`
|
|
|
|
got := parseAdultDetailHTML(html, "SSIS-001", "javdb", "https://javdb.com/v/abc")
|
|
if got == nil {
|
|
t.Fatal("parseAdultDetailHTML returned nil")
|
|
}
|
|
if got.Title != "SSIS-001-测试标题" || got.OriginalName != "SSIS-001" || !got.NSFW {
|
|
t.Fatalf("unexpected metadata: %+v", got)
|
|
}
|
|
if got.PosterURL != "https://javdb.com/covers/ssis001.jpg" || got.BackdropURL != "https://javdb.com/samples/1.jpg" {
|
|
t.Fatalf("unexpected artwork: %+v", got)
|
|
}
|
|
if got.Year != 2024 {
|
|
t.Fatalf("year = %d, want 2024", got.Year)
|
|
}
|
|
}
|
|
|
|
func TestParseAdultDetailHTMLDerivesDMMPoster(t *testing.T) {
|
|
html := `<html>
|
|
<h3>NACR-833 测试标题</h3>
|
|
<a class="sample-box" href="https://pics.dmm.co.jp/digital/video/h_237nacr00833/h_237nacr00833jp-1.jpg"></a>
|
|
</html>`
|
|
|
|
got := parseAdultDetailHTML(html, "NACR-833", "javbus", "https://www.javbus.com/NACR-833")
|
|
if got == nil {
|
|
t.Fatal("parseAdultDetailHTML returned nil")
|
|
}
|
|
if got.PosterURL != "https://pics.dmm.co.jp/digital/video/h_237nacr00833/h_237nacr00833pl.jpg" {
|
|
t.Fatalf("PosterURL = %q", got.PosterURL)
|
|
}
|
|
}
|
|
|
|
func TestAdultProviderUsesConfiguredMultipleSources(t *testing.T) {
|
|
bad := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
http.Error(w, "temporary failure", http.StatusInternalServerError)
|
|
}))
|
|
defer bad.Close()
|
|
|
|
good := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
switch r.URL.Path {
|
|
case "/search":
|
|
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
|
_, _ = w.Write([]byte(`<a class="box" href="/v/ssis001"><strong>SSIS-001 多源入口</strong></a>`))
|
|
case "/v/ssis001":
|
|
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
|
_, _ = w.Write([]byte(`<h2 class="title"><strong>SSIS-001 多源命中标题</strong></h2><img class="video-cover" src="/cover.jpg">`))
|
|
default:
|
|
http.NotFound(w, r)
|
|
}
|
|
}))
|
|
defer good.Close()
|
|
|
|
db := newServiceTestDB(t, &model.APIConfig{})
|
|
apiConfig := NewAPIConfigService(zap.NewNop(), repository.New(db), NewCryptoService("", zap.NewNop()))
|
|
baseURL := bad.URL + "\n" + good.URL
|
|
if _, err := apiConfig.Update(context.Background(), "adult", APIConfigPatch{BaseURL: &baseURL}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
provider := NewAdultProvider(zap.NewNop(), apiConfig)
|
|
match, err := provider.Search(context.Background(), "SSIS-001")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if match == nil || match.Title != "SSIS-001-多源命中标题" || match.OriginalName != "SSIS-001" || !match.NSFW {
|
|
t.Fatalf("multi-source adult match = %+v", match)
|
|
}
|
|
}
|
|
|
|
func TestAdultSourceKindRecognizesJavBusMirrors(t *testing.T) {
|
|
cases := map[string]string{
|
|
"https://javdb.com": "javdb",
|
|
"https://javbus.sbs": "javbus",
|
|
"https://www.javbus.com": "javbus",
|
|
"https://www.cdnbus.cyou": "javbus",
|
|
"https://www.javsee.cyou": "javbus",
|
|
"https://www.busjav.cyou": "javbus",
|
|
"www.cdnbus.cyou": "javbus",
|
|
"https://example.invalid": "javdb",
|
|
}
|
|
for in, want := range cases {
|
|
if got := adultSourceKind(in); got != want {
|
|
t.Fatalf("adultSourceKind(%q) = %q, want %q", in, got, want)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestAdultProviderDefaultBases(t *testing.T) {
|
|
provider := &AdultProvider{}
|
|
got := provider.resolveBases(context.Background())
|
|
want := []string{
|
|
"https://javdb.com",
|
|
"https://javbus.sbs",
|
|
"https://www.javbus.com",
|
|
"https://www.cdnbus.cyou",
|
|
"https://www.javsee.cyou",
|
|
"https://www.busjav.cyou",
|
|
}
|
|
if len(got) != len(want) {
|
|
t.Fatalf("resolveBases len = %d, want %d: %v", len(got), len(want), got)
|
|
}
|
|
for i := range want {
|
|
if got[i] != want[i] {
|
|
t.Fatalf("resolveBases[%d] = %q, want %q", i, got[i], want[i])
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestParseAdultDetailHTMLRejectsAgeVerification(t *testing.T) {
|
|
verifyHTML := `<html>
|
|
<head><title>Age Verification JavBus - JavBus</title></head>
|
|
<body><h4 class="modal-title">你是否已經成年?</h4><a href="/doc/driver-verify">driver-verify</a></body>
|
|
</html>`
|
|
got := parseAdultDetailHTML(verifyHTML, "IPX-235", "javbus", "https://www.javbus.com/IPX-235")
|
|
if got != nil {
|
|
t.Fatalf("expected nil for age verification page, got: %+v", got)
|
|
}
|
|
}
|
|
|
|
func TestAdultProviderPassesDefaultCookie(t *testing.T) {
|
|
receivedCookie := ""
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
receivedCookie = r.Header.Get("Cookie")
|
|
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
|
_, _ = w.Write([]byte(`<html><h3>IPX-235 测试番号</h3><a class="bigImage" href="/pics/cover/6v0a_b.jpg"></a></html>`))
|
|
}))
|
|
defer server.Close()
|
|
|
|
baseURL := server.URL
|
|
provider := &AdultProvider{
|
|
client: server.Client(),
|
|
}
|
|
match, err := provider.scrapeJavBus(context.Background(), baseURL, "IPX-235")
|
|
if err != nil {
|
|
t.Fatalf("scrapeJavBus failed: %v", err)
|
|
}
|
|
if match == nil || match.PosterURL != baseURL+"/pics/cover/6v0a_b.jpg" {
|
|
t.Fatalf("unexpected match: %+v", match)
|
|
}
|
|
if receivedCookie != "age=verified; existmag=all" {
|
|
t.Fatalf("receivedCookie = %q, want 'age=verified; existmag=all'", receivedCookie)
|
|
}
|
|
}
|
|
|
|
func TestLiveAdultProviderJavBus(t *testing.T) {
|
|
provider := NewAdultProvider(zap.NewNop(), nil)
|
|
match, err := provider.scrapeJavBus(context.Background(), "https://www.javbus.com", "IPX-235")
|
|
if err != nil {
|
|
t.Logf("Live test skipped or failed due to network: %v", err)
|
|
return
|
|
}
|
|
if match == nil {
|
|
t.Fatal("expected match from live JavBus")
|
|
}
|
|
t.Logf("Live scrape success: Title=%q, PosterURL=%q, BackdropURL=%q", match.Title, match.PosterURL, match.BackdropURL)
|
|
if match.PosterURL == "" {
|
|
t.Fatal("expected non-empty PosterURL")
|
|
}
|
|
}
|