mirror of
https://github.com/truewhile/MeBox.git
synced 2026-10-03 04:26:36 +08:00
fix media organize and subscription workflows
This commit is contained in:
@@ -1,10 +1,14 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/glebarez/sqlite"
|
||||
"go.uber.org/zap"
|
||||
"go.uber.org/zap/zaptest/observer"
|
||||
"gorm.io/gorm"
|
||||
|
||||
"github.com/ShukeBta/MediaStationGo/internal/model"
|
||||
@@ -36,6 +40,81 @@ func TestDiscoverProviderEnabledHonorsAPIConfigToggle(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestDiscoverFetchFailureLogIncludesDiagnostics(t *testing.T) {
|
||||
core, observed := observer.New(zap.WarnLevel)
|
||||
logger := zap.New(core)
|
||||
|
||||
logDiscoverFetchFailed(
|
||||
&service.Container{Log: logger},
|
||||
"tmdb_latest_movie",
|
||||
2,
|
||||
1500*time.Millisecond,
|
||||
context.DeadlineExceeded,
|
||||
)
|
||||
|
||||
entries := observed.FilterMessage("discover section fetch failed").All()
|
||||
if len(entries) != 1 {
|
||||
t.Fatalf("expected one failure log entry, got %d", len(entries))
|
||||
}
|
||||
fields := entries[0].ContextMap()
|
||||
if fields["section"] != "tmdb_latest_movie" || fields["provider"] != "tmdb" {
|
||||
t.Fatalf("unexpected section/provider fields: %#v", fields)
|
||||
}
|
||||
if fields["page"] != int64(2) && fields["page"] != 2 {
|
||||
t.Fatalf("page field missing or wrong: %#v", fields["page"])
|
||||
}
|
||||
if fields["duration_ms"] != int64(1500) && fields["duration_ms"] != 1500 {
|
||||
t.Fatalf("duration_ms field missing or wrong: %#v", fields["duration_ms"])
|
||||
}
|
||||
if _, ok := fields["timeout"]; !ok {
|
||||
t.Fatalf("timeout field missing: %#v", fields)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDiscoverSlowFetchLogIncludesSectionTiming(t *testing.T) {
|
||||
core, observed := observer.New(zap.InfoLevel)
|
||||
logger := zap.New(core)
|
||||
|
||||
logDiscoverFetchSlow(&service.Container{Log: logger}, "douban_hot_movie", 1, discoverFeedSlowSectionThreshold-time.Millisecond, 24)
|
||||
if got := observed.FilterMessage("discover section fetch slow").Len(); got != 0 {
|
||||
t.Fatalf("fast section should not log, got %d entries", got)
|
||||
}
|
||||
|
||||
logDiscoverFetchSlow(&service.Container{Log: logger}, "douban_hot_movie", 1, discoverFeedSlowSectionThreshold, 24)
|
||||
entries := observed.FilterMessage("discover section fetch slow").All()
|
||||
if len(entries) != 1 {
|
||||
t.Fatalf("expected one slow log entry, got %d", len(entries))
|
||||
}
|
||||
fields := entries[0].ContextMap()
|
||||
if fields["section"] != "douban_hot_movie" || fields["provider"] != "douban" {
|
||||
t.Fatalf("unexpected section/provider fields: %#v", fields)
|
||||
}
|
||||
if fields["items"] != int64(24) && fields["items"] != 24 {
|
||||
t.Fatalf("items field missing or wrong: %#v", fields["items"])
|
||||
}
|
||||
if _, ok := fields["duration_ms"]; !ok {
|
||||
t.Fatalf("duration_ms field missing: %#v", fields)
|
||||
}
|
||||
if _, ok := fields["slow_threshold"]; !ok {
|
||||
t.Fatalf("slow_threshold field missing: %#v", fields)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDiscoverFeedErrorMessageHidesTechnicalTimeout(t *testing.T) {
|
||||
for _, err := range []error{
|
||||
context.DeadlineExceeded,
|
||||
errors.New("timeout of 30000ms exceeded"),
|
||||
} {
|
||||
got := discoverFeedErrorMessage(err)
|
||||
if got != "推荐源响应超时,已跳过本次加载" {
|
||||
t.Fatalf("message for %q = %q", err, got)
|
||||
}
|
||||
}
|
||||
if got := discoverFeedErrorMessage(errors.New("upstream 503")); got != "推荐源暂时不可用,已跳过本次加载" {
|
||||
t.Fatalf("generic message = %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDefaultDiscoverSectionKeysSkipDisabledProviders(t *testing.T) {
|
||||
db, err := gorm.Open(sqlite.Open("file::memory:?cache=shared"), &gorm.Config{})
|
||||
if err != nil {
|
||||
@@ -78,3 +157,17 @@ func TestDefaultDiscoverSectionKeysIncludeLatestTMDbRails(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestFallbackDiscoverSectionKeyUsesTMDbForDoubanRails(t *testing.T) {
|
||||
cases := map[string]string{
|
||||
"douban_hot_movie": "tmdb_popular_movie",
|
||||
"douban_hot_tv": "tmdb_popular_tv",
|
||||
"douban_top_movie": "tmdb_top_rated_movie",
|
||||
"tmdb_latest_tv": "",
|
||||
}
|
||||
for key, want := range cases {
|
||||
if got := fallbackDiscoverSectionKey(key); got != want {
|
||||
t.Fatalf("fallbackDiscoverSectionKey(%q) = %q, want %q", key, got, want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user