feat: add watch streak miner status
This commit is contained in:
@@ -42,8 +42,18 @@ type IRCUpdate struct {
|
||||
|
||||
// MinerUpdate carries one miner log line into the TUI.
|
||||
type MinerUpdate struct {
|
||||
Login string
|
||||
Line string
|
||||
Login string
|
||||
Line string
|
||||
Status *MinerStatus
|
||||
}
|
||||
|
||||
// MinerStatus carries the current active watch state for one streamer.
|
||||
type MinerStatus struct {
|
||||
Watching bool
|
||||
Reason string
|
||||
WatchedMinutes int
|
||||
WatchStreakMinutes int
|
||||
WatchStreak *int
|
||||
}
|
||||
|
||||
type authStartedMsg struct {
|
||||
|
||||
+23
-15
@@ -61,6 +61,7 @@ type Model struct {
|
||||
focus panelFocus
|
||||
ircDetails map[string]ircDetail
|
||||
minerDetails map[string][]string
|
||||
minerStatuses map[string]MinerStatus
|
||||
authViewport viewport.Model
|
||||
ircViewport viewport.Model
|
||||
minerViewport viewport.Model
|
||||
@@ -79,18 +80,19 @@ func New(options Options) Model {
|
||||
}
|
||||
|
||||
model := Model{
|
||||
streamers: streamers,
|
||||
authState: options.AuthState,
|
||||
runtime: options.runtime,
|
||||
authLogs: []string{},
|
||||
mode: authView,
|
||||
ircDetails: make(map[string]ircDetail),
|
||||
minerDetails: make(map[string][]string),
|
||||
width: defaultViewWidth,
|
||||
height: 24,
|
||||
focus: focusStreamers,
|
||||
authViewport: newAuthViewport(contentWidth(defaultViewWidth), authViewportHeight(24)),
|
||||
ircViewport: newIRCViewport(detailViewportWidth(defaultViewWidth), detailViewportHeight(24, "")),
|
||||
streamers: streamers,
|
||||
authState: options.AuthState,
|
||||
runtime: options.runtime,
|
||||
authLogs: []string{},
|
||||
mode: authView,
|
||||
ircDetails: make(map[string]ircDetail),
|
||||
minerDetails: make(map[string][]string),
|
||||
minerStatuses: make(map[string]MinerStatus),
|
||||
width: defaultViewWidth,
|
||||
height: 24,
|
||||
focus: focusStreamers,
|
||||
authViewport: newAuthViewport(contentWidth(defaultViewWidth), authViewportHeight(24)),
|
||||
ircViewport: newIRCViewport(detailViewportWidth(defaultViewWidth), detailViewportHeight(24, "")),
|
||||
minerViewport: newMinerViewport(
|
||||
detailViewportWidth(defaultViewWidth),
|
||||
detailViewportHeight(24, ""),
|
||||
@@ -204,6 +206,7 @@ func (m Model) updateAuth(msg AuthUpdate) (tea.Model, tea.Cmd) {
|
||||
m.streamers = loadingEntries(m.streamers)
|
||||
m.ircDetails = make(map[string]ircDetail)
|
||||
m.minerDetails = make(map[string][]string)
|
||||
m.minerStatuses = make(map[string]MinerStatus)
|
||||
m.selectedConfig = ""
|
||||
m.focus = focusStreamers
|
||||
m.ensureSelection()
|
||||
@@ -269,11 +272,16 @@ func (m *Model) applyIRCUpdate(update IRCUpdate) {
|
||||
|
||||
func (m *Model) applyMinerUpdate(update MinerUpdate) {
|
||||
login := normalizeKey(update.Login)
|
||||
line := strings.TrimSpace(update.Line)
|
||||
if login == "" || line == "" {
|
||||
if login == "" {
|
||||
return
|
||||
}
|
||||
m.minerDetails[login] = appendCappedHistory(m.minerDetails[login], line)
|
||||
if update.Status != nil {
|
||||
m.minerStatuses[login] = *update.Status
|
||||
}
|
||||
line := strings.TrimSpace(update.Line)
|
||||
if line != "" {
|
||||
m.minerDetails[login] = appendCappedHistory(m.minerDetails[login], line)
|
||||
}
|
||||
}
|
||||
|
||||
func (m *Model) ensureSelection() {
|
||||
|
||||
@@ -128,6 +128,141 @@ func TestViewDisplaysInactiveDetailForOfflineStreamer(t *testing.T) {
|
||||
assertContainsAll(t, model.View(), "offline", "inactive")
|
||||
}
|
||||
|
||||
func TestInfoTabShowsFetchingWatchStreak(t *testing.T) {
|
||||
model := dashboardModel(twitch.StreamerEntry{ConfigLogin: "alpha", Login: "alpha_live", Status: twitch.StreamerReady})
|
||||
entry, ok := model.selectedEntry()
|
||||
if !ok {
|
||||
t.Fatal("expected selected entry")
|
||||
}
|
||||
assertLastInfoLine(t, model.renderInfoTab(entry), "Watch streak: fetching")
|
||||
}
|
||||
|
||||
func TestInfoTabShowsFetchedWatchStreak(t *testing.T) {
|
||||
streak := 3
|
||||
model := dashboardModel(twitch.StreamerEntry{
|
||||
ConfigLogin: "alpha",
|
||||
Login: "alpha_live",
|
||||
Live: true,
|
||||
Status: twitch.StreamerReady,
|
||||
WatchStreak: &streak,
|
||||
})
|
||||
entry, ok := model.selectedEntry()
|
||||
if !ok {
|
||||
t.Fatal("expected selected entry")
|
||||
}
|
||||
assertLastInfoLine(t, model.renderInfoTab(entry), "Watch streak: 3")
|
||||
}
|
||||
|
||||
func TestInfoTabShowsUnavailableWatchStreak(t *testing.T) {
|
||||
model := dashboardModel(twitch.StreamerEntry{
|
||||
ConfigLogin: "alpha",
|
||||
Login: "alpha_live",
|
||||
Live: true,
|
||||
Status: twitch.StreamerReady,
|
||||
WatchStreakError: "lookup failed",
|
||||
})
|
||||
entry, ok := model.selectedEntry()
|
||||
if !ok {
|
||||
t.Fatal("expected selected entry")
|
||||
}
|
||||
assertLastInfoLine(t, model.renderInfoTab(entry), "Watch streak: unavailable")
|
||||
}
|
||||
|
||||
func TestInfoTabShowsWatchStreakMinerStatus(t *testing.T) {
|
||||
streak := 3
|
||||
model := dashboardModel(twitch.StreamerEntry{
|
||||
ConfigLogin: "alpha",
|
||||
Login: "alpha_live",
|
||||
Live: true,
|
||||
Status: twitch.StreamerReady,
|
||||
WatchStreak: &streak,
|
||||
})
|
||||
updated, _ := model.Update(StreamerUpdate{Miner: &MinerUpdate{
|
||||
Login: "alpha_live",
|
||||
Status: &MinerStatus{
|
||||
Watching: true,
|
||||
Reason: "watchstreak",
|
||||
WatchedMinutes: 4,
|
||||
},
|
||||
}})
|
||||
next := updated.(Model)
|
||||
entry, ok := next.selectedEntry()
|
||||
if !ok {
|
||||
t.Fatal("expected selected entry")
|
||||
}
|
||||
info := next.renderInfoTab(entry)
|
||||
assertContainsAll(t, info, "Watching for watchstreak", "Watched for 4 minutes so far")
|
||||
assertNotContains(t, info, "Watching for watchstreak (4 mins so far)")
|
||||
assertLastInfoLine(t, info, "Watched for 4 minutes so far")
|
||||
}
|
||||
|
||||
func TestInfoTabShowsPointsMinerStatus(t *testing.T) {
|
||||
streak := 3
|
||||
minerStreak := 4
|
||||
model := dashboardModel(twitch.StreamerEntry{
|
||||
ConfigLogin: "alpha",
|
||||
Login: "alpha_live",
|
||||
Live: true,
|
||||
Status: twitch.StreamerReady,
|
||||
WatchStreak: &streak,
|
||||
})
|
||||
updated, _ := model.Update(StreamerUpdate{Miner: &MinerUpdate{
|
||||
Login: "alpha_live",
|
||||
Status: &MinerStatus{
|
||||
Watching: true,
|
||||
Reason: "points",
|
||||
WatchedMinutes: 6,
|
||||
WatchStreak: &minerStreak,
|
||||
},
|
||||
}})
|
||||
next := updated.(Model)
|
||||
entry, ok := next.selectedEntry()
|
||||
if !ok {
|
||||
t.Fatal("expected selected entry")
|
||||
}
|
||||
info := next.renderInfoTab(entry)
|
||||
assertContainsAll(t, info, "Watch streak: 4", "Watching for points", "Watched for 6 minutes so far")
|
||||
assertLastInfoLine(t, info, "Watched for 6 minutes so far")
|
||||
}
|
||||
|
||||
func TestInfoTabOmitsMinerStatusWhenInactiveOrUnwatched(t *testing.T) {
|
||||
streak := 3
|
||||
model := dashboardModel(twitch.StreamerEntry{
|
||||
ConfigLogin: "alpha",
|
||||
Login: "alpha_live",
|
||||
Status: twitch.StreamerReady,
|
||||
WatchStreak: &streak,
|
||||
})
|
||||
updated, _ := model.Update(StreamerUpdate{Miner: &MinerUpdate{
|
||||
Login: "alpha_live",
|
||||
Status: &MinerStatus{
|
||||
Watching: true,
|
||||
Reason: "points",
|
||||
},
|
||||
}})
|
||||
next := updated.(Model)
|
||||
entry, ok := next.selectedEntry()
|
||||
if !ok {
|
||||
t.Fatal("expected selected entry")
|
||||
}
|
||||
assertNotContains(t, next.renderInfoTab(entry), "Watching for")
|
||||
assertNotContains(t, next.renderInfoTab(entry), "Watched for")
|
||||
|
||||
next = dashboardModel(twitch.StreamerEntry{
|
||||
ConfigLogin: "alpha",
|
||||
Login: "alpha_live",
|
||||
Live: true,
|
||||
Status: twitch.StreamerReady,
|
||||
WatchStreak: &streak,
|
||||
})
|
||||
entry, ok = next.selectedEntry()
|
||||
if !ok {
|
||||
t.Fatal("expected selected entry")
|
||||
}
|
||||
assertNotContains(t, next.renderInfoTab(entry), "Watching for")
|
||||
assertNotContains(t, next.renderInfoTab(entry), "Watched for")
|
||||
}
|
||||
|
||||
func assertInOrder(t *testing.T, value string, parts ...string) {
|
||||
t.Helper()
|
||||
|
||||
@@ -589,3 +724,18 @@ func assertContainsAll(t *testing.T, got string, wants ...string) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func assertNotContains(t *testing.T, got string, want string) {
|
||||
t.Helper()
|
||||
if strings.Contains(got, want) {
|
||||
t.Fatalf("unexpected %q in:\n%s", want, got)
|
||||
}
|
||||
}
|
||||
|
||||
func assertLastInfoLine(t *testing.T, got, want string) {
|
||||
t.Helper()
|
||||
lines := strings.Split(got, "\n")
|
||||
if len(lines) == 0 || lines[len(lines)-1] != want {
|
||||
t.Fatalf("last line = %q in:\n%s", lines[len(lines)-1], got)
|
||||
}
|
||||
}
|
||||
|
||||
+39
-1
@@ -156,6 +156,7 @@ type streamerService interface {
|
||||
CurrentUser(context.Context) (*twitch.Viewer, error)
|
||||
ResolveStreamer(context.Context, string) (*twitch.Channel, error)
|
||||
StreamInfo(context.Context, string) (*twitch.StreamInfo, error)
|
||||
WatchStreak(context.Context, string) (*int, error)
|
||||
PlaybackAccessToken(context.Context, string) (*twitch.PlaybackToken, error)
|
||||
LoadChannelPointsContext(context.Context, string) (*twitch.ChannelPointsContext, error)
|
||||
ClaimCommunityPoints(context.Context, string, string) error
|
||||
@@ -209,7 +210,9 @@ func newIRCManager(ch chan<- StreamerUpdate) *irc.Manager {
|
||||
}
|
||||
|
||||
func newMinerManager(ctx context.Context, service miner.Service, ch chan<- StreamerUpdate) *miner.Manager {
|
||||
return miner.NewManager(ctx, service, nil, newMinerLogSink(ch))
|
||||
manager := miner.NewManager(ctx, service, nil, newMinerLogSink(ch))
|
||||
manager.SetStatusSink(newMinerStatusSink(ch))
|
||||
return manager
|
||||
}
|
||||
|
||||
func newMinerLogSink(ch chan<- StreamerUpdate) func(miner.LogEntry) {
|
||||
@@ -223,6 +226,23 @@ func newMinerLogSink(ch chan<- StreamerUpdate) func(miner.LogEntry) {
|
||||
}
|
||||
}
|
||||
|
||||
func newMinerStatusSink(ch chan<- StreamerUpdate) func(miner.StatusEntry) {
|
||||
return func(entry miner.StatusEntry) {
|
||||
ch <- StreamerUpdate{
|
||||
Miner: &MinerUpdate{
|
||||
Login: entry.Login,
|
||||
Status: &MinerStatus{
|
||||
Watching: entry.Watching,
|
||||
Reason: string(entry.Reason),
|
||||
WatchedMinutes: entry.WatchedMinutes,
|
||||
WatchStreakMinutes: entry.WatchStreakMinutes,
|
||||
WatchStreak: cloneInt(entry.WatchStreak),
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func resolveStreamerEntries(ctx context.Context, service streamerService, state *auth.State, logins []string, ircSyncer ircSyncer, minerSyncer minerSyncer, send func(StreamerUpdate)) error {
|
||||
return resolveStreamerEntriesWithSleep(ctx, service, state, logins, ircSyncer, minerSyncer, send, streamerRefreshInterval, sleepContext)
|
||||
}
|
||||
@@ -275,6 +295,16 @@ func resolveStreamerEntry(ctx context.Context, service streamerService, login st
|
||||
return entry, nil
|
||||
}
|
||||
|
||||
watchStreak, err := service.WatchStreak(ctx, channel.Login)
|
||||
switch {
|
||||
case err == nil:
|
||||
entry.WatchStreak = watchStreak
|
||||
case errors.Is(err, context.Canceled):
|
||||
return nil, err
|
||||
default:
|
||||
entry.WatchStreakError = err.Error()
|
||||
}
|
||||
|
||||
stream, err := service.StreamInfo(ctx, channel.ID)
|
||||
switch {
|
||||
case err == nil:
|
||||
@@ -331,3 +361,11 @@ func watchedIRCTargets(entries []twitch.StreamerEntry) []irc.Target {
|
||||
}
|
||||
return targets
|
||||
}
|
||||
|
||||
func cloneInt(value *int) *int {
|
||||
if value == nil {
|
||||
return nil
|
||||
}
|
||||
copy := *value
|
||||
return ©
|
||||
}
|
||||
|
||||
@@ -18,6 +18,7 @@ type fakeStreamerService struct {
|
||||
resolveErrs map[string]error
|
||||
streams map[string][]streamResult
|
||||
streamCalls map[string]int
|
||||
streaks map[string]watchStreakResult
|
||||
playbackErr map[string]error
|
||||
}
|
||||
|
||||
@@ -53,6 +54,18 @@ func (f *fakeStreamerService) StreamInfo(_ context.Context, channelID string) (*
|
||||
return results[call].info, nil
|
||||
}
|
||||
|
||||
func (f *fakeStreamerService) WatchStreak(_ context.Context, login string) (*int, error) {
|
||||
result, ok := f.streaks[login]
|
||||
if !ok {
|
||||
value := 0
|
||||
return &value, nil
|
||||
}
|
||||
if result.err != nil {
|
||||
return nil, result.err
|
||||
}
|
||||
return &result.value, nil
|
||||
}
|
||||
|
||||
func (f *fakeStreamerService) PlaybackAccessToken(_ context.Context, login string) (*twitch.PlaybackToken, error) {
|
||||
if err, ok := f.playbackErr[login]; ok {
|
||||
return nil, err
|
||||
@@ -145,6 +158,35 @@ func TestResolveStreamerEntriesPlaybackFailureStillMarksLive(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestResolveStreamerEntriesWatchStreakFailureStillMarksLive(t *testing.T) {
|
||||
updates := collectUpdates(t, streamService(
|
||||
channel("alpha", "1", "alpha_live"),
|
||||
live("1", true),
|
||||
watchStreakErr("alpha_live", errors.New("watch streak failed")),
|
||||
), []string{"alpha"}, nil, nil, cancelAfter(0))
|
||||
|
||||
entry := updates[1].Entry
|
||||
if entry == nil || entry.Status != twitch.StreamerReady || !entry.Live {
|
||||
t.Fatalf("entry = %#v", entry)
|
||||
}
|
||||
if entry.WatchStreakError == "" {
|
||||
t.Fatalf("entry = %#v, want watch streak error", entry)
|
||||
}
|
||||
}
|
||||
|
||||
func TestResolveStreamerEntriesStoresWatchStreak(t *testing.T) {
|
||||
updates := collectUpdates(t, streamService(
|
||||
channel("alpha", "1", "alpha_live"),
|
||||
live("1", false),
|
||||
watchStreak("alpha_live", 9),
|
||||
), []string{"alpha"}, nil, nil, cancelAfter(0))
|
||||
|
||||
entry := updates[1].Entry
|
||||
if entry == nil || entry.WatchStreak == nil || *entry.WatchStreak != 9 {
|
||||
t.Fatalf("entry = %#v, want watch streak 9", entry)
|
||||
}
|
||||
}
|
||||
|
||||
func TestResolveStreamerEntriesRefreshesLiveState(t *testing.T) {
|
||||
updates := collectUpdates(t, streamService(
|
||||
channel("alpha", "1", "alpha_live"),
|
||||
@@ -226,12 +268,18 @@ func TestNewMinerLogSinkBridgesMinerEntriesIntoStreamerUpdates(t *testing.T) {
|
||||
|
||||
type serviceOption func(*fakeStreamerService)
|
||||
|
||||
type watchStreakResult struct {
|
||||
value int
|
||||
err error
|
||||
}
|
||||
|
||||
func streamService(options ...serviceOption) *fakeStreamerService {
|
||||
service := &fakeStreamerService{
|
||||
channels: map[string]*twitch.Channel{},
|
||||
resolveErrs: map[string]error{},
|
||||
streams: map[string][]streamResult{},
|
||||
streamCalls: map[string]int{},
|
||||
streaks: map[string]watchStreakResult{},
|
||||
playbackErr: map[string]error{},
|
||||
}
|
||||
for _, option := range options {
|
||||
@@ -270,6 +318,18 @@ func playbackErr(login string, err error) serviceOption {
|
||||
}
|
||||
}
|
||||
|
||||
func watchStreak(login string, value int) serviceOption {
|
||||
return func(service *fakeStreamerService) {
|
||||
service.streaks[login] = watchStreakResult{value: value}
|
||||
}
|
||||
}
|
||||
|
||||
func watchStreakErr(login string, err error) serviceOption {
|
||||
return func(service *fakeStreamerService) {
|
||||
service.streaks[login] = watchStreakResult{err: err}
|
||||
}
|
||||
}
|
||||
|
||||
func collectUpdates(t *testing.T, service *fakeStreamerService, logins []string, ircSyncer ircSyncer, minerSyncer minerSyncer, sleep func(context.Context, time.Duration) error) []StreamerUpdate {
|
||||
t.Helper()
|
||||
var updates []StreamerUpdate
|
||||
|
||||
@@ -128,6 +128,8 @@ func (m Model) renderInfoTab(entry twitch.StreamerEntry) string {
|
||||
detail := m.ircDetails[normalizeKey(entry.Login)]
|
||||
if !isActive(entry) {
|
||||
lines = append(lines, "Chat: "+mutedStyle.Render("inactive"))
|
||||
lines = append(lines, m.watchStreakText(entry))
|
||||
lines = append(lines, m.minerWatchingLines(entry)...)
|
||||
return strings.Join(lines, "\n")
|
||||
}
|
||||
|
||||
@@ -136,6 +138,8 @@ func (m Model) renderInfoTab(entry twitch.StreamerEntry) string {
|
||||
} else {
|
||||
lines = append(lines, "Chat: "+mutedStyle.Render("not joined"))
|
||||
}
|
||||
lines = append(lines, m.watchStreakText(entry))
|
||||
lines = append(lines, m.minerWatchingLines(entry)...)
|
||||
return strings.Join(lines, "\n")
|
||||
}
|
||||
|
||||
@@ -220,6 +224,50 @@ func statusText(entry twitch.StreamerEntry) string {
|
||||
}
|
||||
}
|
||||
|
||||
func (m Model) watchStreakText(entry twitch.StreamerEntry) string {
|
||||
if status, ok := m.minerStatuses[normalizeKey(entry.Login)]; ok && status.WatchStreak != nil {
|
||||
return fmt.Sprintf("Watch streak: %d", *status.WatchStreak)
|
||||
}
|
||||
switch {
|
||||
case entry.WatchStreak != nil:
|
||||
return fmt.Sprintf("Watch streak: %d", *entry.WatchStreak)
|
||||
case entry.WatchStreakError != "":
|
||||
return "Watch streak: unavailable"
|
||||
default:
|
||||
return "Watch streak: fetching"
|
||||
}
|
||||
}
|
||||
|
||||
func (m Model) minerWatchingLines(entry twitch.StreamerEntry) []string {
|
||||
if !isActive(entry) {
|
||||
return nil
|
||||
}
|
||||
status, ok := m.minerStatuses[normalizeKey(entry.Login)]
|
||||
if !ok || !status.Watching {
|
||||
return nil
|
||||
}
|
||||
var reason string
|
||||
switch status.Reason {
|
||||
case string(minerWatchReasonStreak):
|
||||
reason = "Watching for watchstreak"
|
||||
case string(minerWatchReasonPoints):
|
||||
reason = "Watching for points"
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
return []string{
|
||||
reason,
|
||||
fmt.Sprintf("Watched for %d minutes so far", status.WatchedMinutes),
|
||||
}
|
||||
}
|
||||
|
||||
type minerWatchReason string
|
||||
|
||||
const (
|
||||
minerWatchReasonStreak minerWatchReason = "watchstreak"
|
||||
minerWatchReasonPoints minerWatchReason = "points"
|
||||
)
|
||||
|
||||
func rawStatus(entry twitch.StreamerEntry) string {
|
||||
switch {
|
||||
case entry.Status == twitch.StreamerError:
|
||||
|
||||
Reference in New Issue
Block a user