Files
ha-gronod-addons/emby-mcp/internal/emby/client_test.go
gronod d8b642436b feat: Home Assistant add-on with in-process Go REST bridge
Bake the former Python bridge.py into emby-mcp as internal/bridge.
/mcp and /call/{tool} share one process. Session init errors are
returned instead of swallowed, and 401/400/404 trigger a fresh
initialize plus retry with the caller's Authorization header.
2026-09-21 16:31:34 +00:00

112 lines
3.0 KiB
Go

package emby
import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"strings"
"testing"
)
func newTestClient(t *testing.T, handler http.HandlerFunc) (*Client, *httptest.Server) {
t.Helper()
srv := httptest.NewServer(handler)
t.Cleanup(srv.Close)
c := NewClient(srv.URL, Options{
ClientName: "Emby.MCP for AI",
ClientVersion: "1.0.2",
DeviceName: "testhost (linux)",
DeviceID: "dev-1",
VerifySSL: true,
})
return c, srv
}
func TestAuthenticate(t *testing.T) {
var gotAuth, gotBody string
c, _ := newTestClient(t, func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/Users/AuthenticateByName" {
t.Errorf("unexpected path %s", r.URL.Path)
}
gotAuth = r.Header.Get("X-Emby-Authorization")
var body map[string]string
json.NewDecoder(r.Body).Decode(&body)
gotBody = body["Username"] + "/" + body["Pw"]
json.NewEncoder(w).Encode(map[string]any{
"AccessToken": "tok123",
"User": map[string]string{"Id": "u1", "Name": "user"},
})
})
res, err := c.Authenticate(context.Background(), "user", "pass")
if err != nil {
t.Fatal(err)
}
if res.AccessToken != "tok123" || res.User.ID != "u1" {
t.Fatalf("res = %+v", res)
}
if gotBody != "user/pass" {
t.Errorf("body = %q", gotBody)
}
if !strings.Contains(gotAuth, `Client="Emby.MCP for AI"`) ||
!strings.Contains(gotAuth, `DeviceId="dev-1"`) ||
!strings.Contains(gotAuth, `Version="1.0.2"`) {
t.Errorf("auth header = %q", gotAuth)
}
if !c.UsesPasswordAuth() {
t.Error("UsesPasswordAuth should be true")
}
}
func TestTokenSentAfterAuth(t *testing.T) {
var tok string
c, _ := newTestClient(t, func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/Users/AuthenticateByName" {
json.NewEncoder(w).Encode(map[string]any{
"AccessToken": "tok123",
"User": map[string]string{"Id": "u1"},
})
return
}
tok = r.Header.Get("X-Emby-Token")
json.NewEncoder(w).Encode(map[string]any{"Items": []any{}, "TotalRecordCount": 0})
})
if _, err := c.Authenticate(context.Background(), "u", "p"); err != nil {
t.Fatal(err)
}
if _, err := c.GetLibraryList(context.Background()); err != nil {
t.Fatal(err)
}
if tok != "tok123" {
t.Errorf("X-Emby-Token = %q", tok)
}
}
func TestAPIKeyMode(t *testing.T) {
var tok string
c, _ := newTestClient(t, func(w http.ResponseWriter, r *http.Request) {
tok = r.Header.Get("X-Emby-Token")
json.NewEncoder(w).Encode(map[string]any{"Items": []any{}, "TotalRecordCount": 0})
})
c.SetAPIKey("mykey")
if _, err := c.GetLibraryList(context.Background()); err != nil {
t.Fatal(err)
}
if tok != "mykey" {
t.Errorf("X-Emby-Token = %q", tok)
}
if c.UsesPasswordAuth() {
t.Error("UsesPasswordAuth should be false for API key")
}
}
func TestHTTPError(t *testing.T) {
c, _ := newTestClient(t, func(w http.ResponseWriter, r *http.Request) {
http.Error(w, "nope", http.StatusUnauthorized)
})
_, err := c.GetLibraryList(context.Background())
if err == nil || !strings.Contains(err.Error(), "401") {
t.Fatalf("err = %v", err)
}
}