Files
MeBox/internal/handler/media_test.go
T
ShukeBta 23d1fbfddb fix cloud library scale and playback compatibility
Root cause: display-only cloud library filtering was reused by scan jobs, merged cloud mounts could be skipped, and OpenList listing relied on WebDAV/first-page behavior that could cap huge directories around 100 items. Cloud scans also let new items consume probe budget before existing rows with missing track/artwork metadata.\n\nChanges:\n- split display filtering from scannable cloud filtering so merged cloud mounts still scan\n- add OpenList API pagination with WebDAV fallback\n- prioritize existing cloud media missing metadata before new imports\n- restrict automatic cloud sync to one successful 19:00-21:00 daily window and keep manual scan immediate\n- disable startup cloud full-scan by default, with an explicit opt-in setting\n- improve Emby/cloud playback compatibility and cache/search/test coverage from the continued work
2026-06-11 18:31:01 +08:00

110 lines
3.7 KiB
Go

package handler
import (
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
"github.com/gin-gonic/gin"
"github.com/glebarez/sqlite"
"go.uber.org/zap"
"gorm.io/gorm"
"github.com/ShukeBta/MediaStationGo/internal/config"
"github.com/ShukeBta/MediaStationGo/internal/middleware"
"github.com/ShukeBta/MediaStationGo/internal/model"
"github.com/ShukeBta/MediaStationGo/internal/repository"
"github.com/ShukeBta/MediaStationGo/internal/service"
)
func TestListLibrariesHidesAdultDirectoriesUnlessAdminRequestsAll(t *testing.T) {
gin.SetMode(gin.TestMode)
db, err := gorm.Open(sqlite.Open(":memory:"), &gorm.Config{})
if err != nil {
t.Fatal(err)
}
if err := db.AutoMigrate(&model.User{}, &model.Library{}, &model.Media{}, &model.Setting{}, &model.PlayProfile{}); err != nil {
t.Fatal(err)
}
repos := repository.New(db)
viewer := &model.User{Username: "viewer", PasswordHash: "hash", Role: "admin", HideAdult: true}
if err := repos.User.Create(t.Context(), viewer); err != nil {
t.Fatal(err)
}
safe := model.Library{Name: "电影", Path: "/media/movie", Type: "movie", Enabled: true}
adult := model.Library{Name: "9KG", Path: "/media/9KG", Type: "movie", Enabled: true}
if err := repos.Library.Create(t.Context(), &safe); err != nil {
t.Fatal(err)
}
if err := repos.Library.Create(t.Context(), &adult); err != nil {
t.Fatal(err)
}
if err := repos.Setting.Set(t.Context(), service.AdultLibraryIDsSettingKey, `["`+adult.ID+`"]`); err != nil {
t.Fatal(err)
}
if err := repos.Media.Upsert(t.Context(), &model.Media{LibraryID: safe.ID, Title: "误入普通库的成人条目", Path: "/media/movie/nsfw.mkv", NSFW: true}); err != nil {
t.Fatal(err)
}
svc := &service.Container{
Repo: repos,
Media: service.NewMediaService(&config.Config{}, zap.NewNop(), repos),
}
visible := requestLibraries(t, svc, viewer.ID, "admin", "/api/libraries")
if len(visible) != 1 || visible[0].ID != safe.ID {
t.Fatalf("watching library list should hide adult directories, got %#v", visible)
}
all := requestLibraries(t, svc, viewer.ID, "admin", "/api/libraries?include_hidden=1")
if len(all) != 2 {
t.Fatalf("admin include_hidden list should keep management access, got %#v", all)
}
}
func TestListLibrariesIncludeHiddenNormalizesCloudDisplayNames(t *testing.T) {
gin.SetMode(gin.TestMode)
db, err := gorm.Open(sqlite.Open(":memory:"), &gorm.Config{})
if err != nil {
t.Fatal(err)
}
if err := db.AutoMigrate(&model.Library{}, &model.Media{}); err != nil {
t.Fatal(err)
}
repos := repository.New(db)
cloud := model.Library{Name: "OpenList · 国产剧", Path: service.BuildCloudLibraryPath("openlist", "/国产剧", "/国产剧"), Type: "tv", Enabled: true}
if err := repos.Library.Create(t.Context(), &cloud); err != nil {
t.Fatal(err)
}
svc := &service.Container{
Repo: repos,
Media: service.NewMediaService(&config.Config{}, zap.NewNop(), repos),
}
all := requestLibraries(t, svc, "admin", "admin", "/api/libraries?include_hidden=1")
if len(all) != 1 {
t.Fatalf("include_hidden list = %#v, want one library", all)
}
if all[0].Name != "国产剧" {
t.Fatalf("cloud display name = %q, want stripped directory name", all[0].Name)
}
}
func requestLibraries(t *testing.T, svc *service.Container, userID, role, path string) []model.Library {
t.Helper()
w := httptest.NewRecorder()
c, _ := gin.CreateTestContext(w)
c.Set(middleware.CtxUserID, userID)
c.Set(middleware.CtxUserRole, role)
c.Request = httptest.NewRequest(http.MethodGet, path, nil)
listLibrariesHandler(svc)(c)
if w.Code != http.StatusOK {
t.Fatalf("GET %s status = %d body=%s", path, w.Code, w.Body.String())
}
var libs []model.Library
if err := json.Unmarshal(w.Body.Bytes(), &libs); err != nil {
t.Fatalf("decode libraries: %v", err)
}
return libs
}