feat(youtube): VideoByID for arbitrary-video metadata (paste-a-URL)
videos.list (part=snippet) for a single id, including channels the user does not follow. Data API call (1 quota unit), NOT the rate-limited caption path — ungated metadata; only the later transcript fetch hits globalFetchGate. Returns a subscription-less domain.Video scoped to the user, or ErrVideoNotFound for a deleted/private/typo'd id. Foundation for Feature 2. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,59 @@
|
||||
package youtube
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"net/http"
|
||||
"testing"
|
||||
|
||||
"gitea.d-ma.be/mathias/tapir/internal/domain"
|
||||
)
|
||||
|
||||
func TestVideoByID(t *testing.T) {
|
||||
const id = "dQw4w9WgXcQ"
|
||||
a, secrets := newTestAdapter(t, func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.URL.Path != "/videos" {
|
||||
t.Errorf("unexpected path %q (must use videos.list)", r.URL.Path)
|
||||
}
|
||||
if got := r.URL.Query().Get("id"); got != id {
|
||||
t.Errorf("expected id=%s, got %q", id, got)
|
||||
}
|
||||
if got := r.URL.Query().Get("part"); got != "snippet" {
|
||||
t.Errorf("expected part=snippet, got %q", got)
|
||||
}
|
||||
_, _ = w.Write([]byte(`{"items":[{"snippet":{"title":"Never Gonna Give You Up","publishedAt":"2026-05-20T09:00:00Z"}}]}`))
|
||||
})
|
||||
|
||||
v, err := a.VideoByID(context.Background(), "u1", id)
|
||||
if err != nil {
|
||||
t.Fatalf("VideoByID: %v", err)
|
||||
}
|
||||
if v.UserID != "u1" {
|
||||
t.Errorf("UserID = %q, want u1", v.UserID)
|
||||
}
|
||||
if v.ProviderVideoID != id || v.Title != "Never Gonna Give You Up" {
|
||||
t.Errorf("unexpected video: %+v", v)
|
||||
}
|
||||
if v.Provider != domain.ProviderYouTube || v.URL != "https://www.youtube.com/watch?v="+id {
|
||||
t.Errorf("video not wired correctly: %+v", v)
|
||||
}
|
||||
if v.PublishedAt.IsZero() {
|
||||
t.Errorf("expected publishedAt parsed, got zero")
|
||||
}
|
||||
if v.SubscriptionID != "" {
|
||||
t.Errorf("a pasted video must have no subscription, got %q", v.SubscriptionID)
|
||||
}
|
||||
if secrets.byRef == nil {
|
||||
t.Errorf("token must be resolved by reference through the SecretStore")
|
||||
}
|
||||
}
|
||||
|
||||
func TestVideoByIDNotFound(t *testing.T) {
|
||||
a, _ := newTestAdapter(t, func(w http.ResponseWriter, _ *http.Request) {
|
||||
_, _ = w.Write([]byte(`{"items":[]}`))
|
||||
})
|
||||
_, err := a.VideoByID(context.Background(), "u1", "missingvid0")
|
||||
if !errors.Is(err, ErrVideoNotFound) {
|
||||
t.Fatalf("VideoByID for missing id = %v, want ErrVideoNotFound", err)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user