mirror of
https://github.com/truewhile/MeBox.git
synced 2026-09-28 11:16:37 +08:00
b0fe40142a
* Rebrand MMTL to MeBox across codebase and assets Rename the project display name, Go module path, environment variable prefix (MEBOX_*), Docker image references, and UI branding from MMTL/mmtl to MeBox/mebox. Replace logo assets with the new MeBox icon and keep legacy SQLite migration support for existing mmtl.db deployments. Co-authored-by: truewhile <truewhile@users.noreply.github.com> * Fix logo icons: use cube-only crop without truncated text Previous icon generation cropped too much of the source image, including partial MeBox wordmark text that was cut off in square icon containers. Regenerate logo-64/192/512, favicon, and SVG from cube-only region. Co-authored-by: truewhile <truewhile@users.noreply.github.com> --------- Co-authored-by: Cursor Agent <cursoragent@cursor.com> Co-authored-by: truewhile <truewhile@users.noreply.github.com>
124 lines
3.2 KiB
Go
124 lines
3.2 KiB
Go
package service
|
||
|
||
import (
|
||
"encoding/json"
|
||
"net/http"
|
||
"net/http/httptest"
|
||
"strings"
|
||
"testing"
|
||
|
||
"github.com/glebarez/sqlite"
|
||
"go.uber.org/zap"
|
||
"gorm.io/gorm"
|
||
|
||
"github.com/truewhile/MeBox/internal/config"
|
||
"github.com/truewhile/MeBox/internal/model"
|
||
"github.com/truewhile/MeBox/internal/repository"
|
||
)
|
||
|
||
func firstIndexFunc(values []string, match func(string) bool) int {
|
||
for i, value := range values {
|
||
if match(value) {
|
||
return i
|
||
}
|
||
}
|
||
return -1
|
||
}
|
||
|
||
func newTestScraper(t *testing.T) (*ScraperService, *repository.Container, func()) {
|
||
t.Helper()
|
||
|
||
upstream := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||
w.Header().Set("Content-Type", "application/json")
|
||
switch {
|
||
case strings.HasPrefix(r.URL.Path, "/search/tv"):
|
||
_ = json.NewEncoder(w).Encode(map[string]any{
|
||
"results": []map[string]any{{
|
||
"id": 12345,
|
||
"name": "间谍过家家",
|
||
"original_name": "SPY×FAMILY",
|
||
"overview": "测试简介",
|
||
"poster_path": "/poster.jpg",
|
||
"backdrop_path": "/backdrop.jpg",
|
||
"first_air_date": "2022-04-09",
|
||
"vote_average": 8.6,
|
||
}},
|
||
})
|
||
case r.URL.Path == "/tv/12345/season/2/episode/1":
|
||
_ = json.NewEncoder(w).Encode(map[string]any{
|
||
"name": "任务代号: 猫",
|
||
"overview": "单集剧情",
|
||
"still_path": "/still.jpg",
|
||
"air_date": "2023-10-07",
|
||
"vote_average": 9.1,
|
||
"runtime": 24,
|
||
})
|
||
case strings.HasPrefix(r.URL.Path, "/tv/12345"):
|
||
_ = json.NewEncoder(w).Encode(map[string]any{
|
||
"id": 12345,
|
||
"name": "间谍过家家",
|
||
"overview": "测试简介",
|
||
"poster_path": "/poster.jpg",
|
||
"backdrop_path": "/backdrop.jpg",
|
||
"first_air_date": "2022-04-09",
|
||
"vote_average": 8.6,
|
||
"origin_country": []string{"JP"},
|
||
"spoken_languages": []map[string]any{{
|
||
"iso_639_1": "ja",
|
||
}},
|
||
"genres": []map[string]any{{
|
||
"name": "Animation",
|
||
}},
|
||
})
|
||
case strings.HasPrefix(r.URL.Path, "/tv/999"):
|
||
_ = json.NewEncoder(w).Encode(map[string]any{
|
||
"id": 999,
|
||
"name": "错误旧匹配",
|
||
"first_air_date": "2020-01-01",
|
||
"origin_country": []string{"CN"},
|
||
"genres": []map[string]any{{
|
||
"name": "Drama",
|
||
}},
|
||
})
|
||
default:
|
||
http.NotFound(w, r)
|
||
}
|
||
}))
|
||
|
||
db, err := gorm.Open(sqlite.Open("file::memory:?cache=shared"), &gorm.Config{})
|
||
if err != nil {
|
||
upstream.Close()
|
||
t.Fatal(err)
|
||
}
|
||
if err := db.AutoMigrate(&model.Library{}, &model.Series{}, &model.Media{}); err != nil {
|
||
upstream.Close()
|
||
t.Fatal(err)
|
||
}
|
||
repos := repository.New(db)
|
||
cfg := &config.Config{}
|
||
cfg.Secrets.TMDbAPIKey = "test-key"
|
||
cfg.Secrets.TMDbAPIProxy = upstream.URL
|
||
cfg.Secrets.TMDbImageProxy = upstream.URL + "/images"
|
||
log := zap.NewNop()
|
||
tmdb := NewTMDbProvider(cfg, log, nil)
|
||
scraper := NewScraperService(cfg, log, repos, tmdb, nil, nil, nil, NewHub(log))
|
||
|
||
return scraper, repos, upstream.Close
|
||
}
|
||
|
||
func firstQuery(values []string) string {
|
||
if len(values) == 0 {
|
||
return ""
|
||
}
|
||
return values[0]
|
||
}
|
||
|
||
func lastIndexFunc(values []string, match func(string) bool) int {
|
||
for i := len(values) - 1; i >= 0; i-- {
|
||
if match(values[i]) {
|
||
return i
|
||
}
|
||
}
|
||
return -1
|
||
}
|