feat: add twitch graphql streamer resolution
This commit is contained in:
+84
-1
@@ -5,6 +5,7 @@ package app
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"os"
|
||||
@@ -15,7 +16,9 @@ import (
|
||||
|
||||
"parasocial/internal/auth"
|
||||
"parasocial/internal/config"
|
||||
"parasocial/internal/gql"
|
||||
"parasocial/internal/tui"
|
||||
"parasocial/internal/twitch"
|
||||
)
|
||||
|
||||
// Run loads the application configuration and starts the terminal UI.
|
||||
@@ -39,7 +42,7 @@ func Run(ctx context.Context) error {
|
||||
|
||||
program := tea.NewProgram(
|
||||
tui.New(tui.Options{
|
||||
Streamers: cfg.Streamers,
|
||||
Streamers: twitch.LoadingStreamerEntries(cfg.Streamers),
|
||||
AuthState: state,
|
||||
StartAuth: func(ch chan<- tui.AuthUpdate) {
|
||||
go func() {
|
||||
@@ -60,6 +63,27 @@ func Run(ctx context.Context) error {
|
||||
ch <- tui.AuthUpdate{State: state, Done: true}
|
||||
}()
|
||||
},
|
||||
StartResolve: func(state *auth.State, ch chan<- tui.StreamerUpdate) {
|
||||
go func() {
|
||||
defer close(ch)
|
||||
|
||||
service, err := newTwitchService(httpClient, state)
|
||||
if err != nil {
|
||||
ch <- tui.StreamerUpdate{Err: err, Done: true}
|
||||
return
|
||||
}
|
||||
if err := resolveStreamerEntries(ctx, service, cfg.Streamers, func(update tui.StreamerUpdate) {
|
||||
ch <- update
|
||||
}); err != nil {
|
||||
if errors.Is(err, context.Canceled) {
|
||||
return
|
||||
}
|
||||
ch <- tui.StreamerUpdate{Err: err, Done: true}
|
||||
return
|
||||
}
|
||||
ch <- tui.StreamerUpdate{Done: true}
|
||||
}()
|
||||
},
|
||||
}),
|
||||
tea.WithContext(ctx),
|
||||
tea.WithInput(os.Stdin),
|
||||
@@ -68,3 +92,62 @@ func Run(ctx context.Context) error {
|
||||
_, err = program.Run()
|
||||
return err
|
||||
}
|
||||
|
||||
// streamerService captures the Twitch lookups the app needs during UI resolution.
|
||||
type streamerService interface {
|
||||
CurrentUser(context.Context) (*twitch.Viewer, error)
|
||||
ResolveStreamer(context.Context, string) (*twitch.Channel, error)
|
||||
}
|
||||
|
||||
// newTwitchService builds a Twitch service from the authenticated session state.
|
||||
func newTwitchService(httpClient *http.Client, state *auth.State) (*twitch.Service, error) {
|
||||
client := &gql.Client{
|
||||
HTTPClient: httpClient,
|
||||
Session: gql.Session{
|
||||
AccessToken: state.AccessToken,
|
||||
ClientID: state.ClientID,
|
||||
DeviceID: state.DeviceID,
|
||||
UserAgent: auth.TVUserAgent(),
|
||||
},
|
||||
}
|
||||
if err := client.Validate(); err != nil {
|
||||
return nil, fmt.Errorf("configure graphql session: %w", err)
|
||||
}
|
||||
return &twitch.Service{GQL: client}, nil
|
||||
}
|
||||
|
||||
// resolveStreamerEntries streams viewer and streamer resolution results into the TUI.
|
||||
func resolveStreamerEntries(ctx context.Context, service streamerService, logins []string, send func(tui.StreamerUpdate)) error {
|
||||
viewer, err := service.CurrentUser(ctx)
|
||||
if err != nil {
|
||||
return fmt.Errorf("resolve current user: %w", err)
|
||||
}
|
||||
send(tui.StreamerUpdate{Viewer: viewer})
|
||||
|
||||
for index, login := range logins {
|
||||
entry := twitch.StreamerEntry{
|
||||
ConfigLogin: login,
|
||||
Status: twitch.StreamerLoading,
|
||||
}
|
||||
|
||||
channel, err := service.ResolveStreamer(ctx, login)
|
||||
switch {
|
||||
case err == nil:
|
||||
entry.Login = channel.Login
|
||||
entry.ChannelID = channel.ID
|
||||
entry.Status = twitch.StreamerReady
|
||||
case errors.Is(err, context.Canceled):
|
||||
return err
|
||||
default:
|
||||
entry.Status = twitch.StreamerError
|
||||
entry.Error = err.Error()
|
||||
}
|
||||
|
||||
send(tui.StreamerUpdate{
|
||||
Index: index,
|
||||
Entry: &entry,
|
||||
})
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -0,0 +1,72 @@
|
||||
// app_test.go covers the app-layer orchestration around Twitch viewer and streamer resolution.
|
||||
// It exercises the background resolution loop independently of Bubble Tea startup
|
||||
// so the app wiring can be validated without requiring a full interactive session.
|
||||
package app
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"testing"
|
||||
|
||||
"parasocial/internal/tui"
|
||||
"parasocial/internal/twitch"
|
||||
)
|
||||
|
||||
// fakeStreamerService is a test double for the app's Twitch resolution dependency.
|
||||
type fakeStreamerService struct {
|
||||
viewer *twitch.Viewer
|
||||
viewerErr error
|
||||
channels map[string]*twitch.Channel
|
||||
errs map[string]error
|
||||
}
|
||||
|
||||
// CurrentUser returns the configured fake viewer or viewer error.
|
||||
func (f *fakeStreamerService) CurrentUser(context.Context) (*twitch.Viewer, error) {
|
||||
if f.viewerErr != nil {
|
||||
return nil, f.viewerErr
|
||||
}
|
||||
return f.viewer, nil
|
||||
}
|
||||
|
||||
// ResolveStreamer returns the configured fake channel or resolution error.
|
||||
func (f *fakeStreamerService) ResolveStreamer(_ context.Context, login string) (*twitch.Channel, error) {
|
||||
if err, ok := f.errs[login]; ok {
|
||||
return nil, err
|
||||
}
|
||||
return f.channels[login], nil
|
||||
}
|
||||
|
||||
// TestResolveStreamerEntries verifies that app resolution emits viewer, success, and error updates.
|
||||
func TestResolveStreamerEntries(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
service := &fakeStreamerService{
|
||||
viewer: &twitch.Viewer{ID: "7", Login: "viewer"},
|
||||
channels: map[string]*twitch.Channel{
|
||||
"alpha": {ID: "1", Login: "alpha_live"},
|
||||
},
|
||||
errs: map[string]error{
|
||||
"beta": errors.New("lookup failed"),
|
||||
},
|
||||
}
|
||||
|
||||
var updates []tui.StreamerUpdate
|
||||
err := resolveStreamerEntries(context.Background(), service, []string{"alpha", "beta"}, func(update tui.StreamerUpdate) {
|
||||
updates = append(updates, update)
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(updates) != 3 {
|
||||
t.Fatalf("len(updates) = %d", len(updates))
|
||||
}
|
||||
if updates[0].Viewer == nil || updates[0].Viewer.Login != "viewer" {
|
||||
t.Fatalf("viewer update = %#v", updates[0])
|
||||
}
|
||||
if updates[1].Entry == nil || updates[1].Entry.Status != twitch.StreamerReady || updates[1].Entry.Login != "alpha_live" {
|
||||
t.Fatalf("alpha update = %#v", updates[1])
|
||||
}
|
||||
if updates[2].Entry == nil || updates[2].Entry.Status != twitch.StreamerError {
|
||||
t.Fatalf("beta update = %#v", updates[2])
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user