Validate add-ons / YAML and layout (pull_request) Failing after 35s
Validate add-ons / emby-mcp Go (pull_request) Failing after 3m55s
Validate add-ons / openai-codex-proxy (pull_request) Successful in 13s
Validate add-ons / n95-mqtt-bridge pin and upstream tests (pull_request) Failing after 39s
Gitea ubuntu-latest is PEP 668 so pip install pyyaml fails. gofmt -l was dirty on main.go, items_test.go, tools_browse.go.
154 lines
4.0 KiB
Go
154 lines
4.0 KiB
Go
package emby
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"net/http"
|
|
"testing"
|
|
)
|
|
|
|
// itemsHandler captures the query params of GET /Users/{uid}/Items and returns
|
|
// a canned item list.
|
|
func itemsHandler(t *testing.T, items []map[string]any, captured *map[string][]string) http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
if captured != nil {
|
|
*captured = map[string][]string(r.URL.Query())
|
|
}
|
|
json.NewEncoder(w).Encode(map[string]any{
|
|
"Items": items,
|
|
"TotalRecordCount": len(items),
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestGetLibraryListFilters(t *testing.T) {
|
|
c, _ := newTestClient(t, func(w http.ResponseWriter, r *http.Request) {
|
|
json.NewEncoder(w).Encode(map[string]any{
|
|
"TotalRecordCount": 3,
|
|
"Items": []map[string]any{
|
|
{"Name": "Music", "Type": "CollectionFolder", "CollectionType": "music", "Id": "1"},
|
|
{"Name": "BoxSets", "Type": "BoxSet", "CollectionType": "boxsets", "Id": "2"},
|
|
{"Name": "Movies", "Type": "CollectionFolder", "CollectionType": "movies", "Id": "3"},
|
|
},
|
|
})
|
|
})
|
|
libs, err := c.GetLibraryList(context.Background())
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(libs) != 2 || libs[0].Name != "Music" || libs[1].ID != "3" {
|
|
t.Fatalf("libs = %+v", libs)
|
|
}
|
|
if libs[0].Type != "music" {
|
|
t.Errorf("type = %q", libs[0].Type)
|
|
}
|
|
}
|
|
|
|
func TestGetItemsQueryTranslation(t *testing.T) {
|
|
var q map[string][]string
|
|
c, srv := newTestClient(t, itemsHandler(t, nil, &q))
|
|
defer srv.Close()
|
|
_, err := c.GetItems(context.Background(), "u1", "lib9", ItemQuery{
|
|
SearchTerm: "track", Artist: "bob", Genre: "Jazz",
|
|
Years: "1999,2001", FirstDate: "1990-01-01", LastDate: "2005-12-31",
|
|
IsUnplayed: true, IsFavorite: true, Limit: "5",
|
|
})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
want := map[string]string{
|
|
"SearchTerm": "track",
|
|
"Artists": "bob",
|
|
"Genres": "Jazz",
|
|
"Years": "1999,2001",
|
|
"MinStartDate": "1990-01-01",
|
|
"MaxEndDate": "2005-12-31",
|
|
"Filters": "IsUnplayed,IsFavorite",
|
|
"Limit": "5",
|
|
"ParentId": "lib9",
|
|
"MediaTypes": "Audio,Video",
|
|
"Recursive": "true",
|
|
}
|
|
for k, v := range want {
|
|
if got := q[k]; len(got) != 1 || got[0] != v {
|
|
t.Errorf("query[%s] = %v, want %q", k, got, v)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestToMediaItemPremiereDate(t *testing.T) {
|
|
it := &BaseItemDto{
|
|
ID: "e1", Name: "Pilot", Type: "Episode",
|
|
PremiereDate: "2025-03-31T00:00:00.0000000Z",
|
|
}
|
|
year := 2025
|
|
it.ProductionYear = &year
|
|
mi := toMediaItem(it)
|
|
if mi.PremiereDate != "2025-03-31" {
|
|
t.Fatalf("premiere_date = %q", mi.PremiereDate)
|
|
}
|
|
if mi.ProductionYear != 2025 {
|
|
t.Fatalf("production_year = %v", mi.ProductionYear)
|
|
}
|
|
empty := toMediaItem(&BaseItemDto{ID: "e2", Name: "Unknown"})
|
|
if empty.PremiereDate != "" {
|
|
t.Fatalf("empty premiere_date = %q", empty.PremiereDate)
|
|
}
|
|
}
|
|
|
|
func TestGetItemsRequestsPremiereDateField(t *testing.T) {
|
|
var q map[string][]string
|
|
c, srv := newTestClient(t, itemsHandler(t, nil, &q))
|
|
defer srv.Close()
|
|
if _, err := c.GetItems(context.Background(), "u1", "", ItemQuery{}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
fields := ""
|
|
if got := q["Fields"]; len(got) == 1 {
|
|
fields = got[0]
|
|
}
|
|
if fields == "" || !containsCSV(fields, "PremiereDate") {
|
|
t.Fatalf("Fields = %q, want PremiereDate", fields)
|
|
}
|
|
}
|
|
|
|
func TestFormatISODate(t *testing.T) {
|
|
cases := map[string]string{
|
|
"": "",
|
|
"2025-03-31T00:00:00.0000000Z": "2025-03-31",
|
|
"2025-03-31": "2025-03-31",
|
|
" 2024-12-01T15:04:05+01:00 ": "2024-12-01",
|
|
}
|
|
for in, want := range cases {
|
|
if got := formatISODate(in); got != want {
|
|
t.Errorf("formatISODate(%q) = %q, want %q", in, got, want)
|
|
}
|
|
}
|
|
}
|
|
|
|
func containsCSV(csv, needle string) bool {
|
|
for _, p := range splitComma(csv) {
|
|
if p == needle {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
func splitComma(s string) []string {
|
|
var out []string
|
|
cur := ""
|
|
for i := 0; i < len(s); i++ {
|
|
if s[i] == ',' {
|
|
out = append(out, cur)
|
|
cur = ""
|
|
continue
|
|
}
|
|
cur += string(s[i])
|
|
}
|
|
if cur != "" || len(s) > 0 && s[len(s)-1] == ',' {
|
|
out = append(out, cur)
|
|
}
|
|
return out
|
|
}
|