|
| 1 | +package api |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "net/http" |
| 6 | + "net/http/httptest" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/stretchr/testify/require" |
| 10 | + |
| 11 | + "github.com/mechastrider/comm-relay/internal/bus" |
| 12 | + "github.com/mechastrider/comm-relay/internal/connector/status" |
| 13 | + "github.com/mechastrider/comm-relay/internal/streamstatus" |
| 14 | +) |
| 15 | + |
| 16 | +func TestStreamsStatus_WhenGet_ExpectThreePlatformsWithUnknownState(t *testing.T) { |
| 17 | + t.Parallel() |
| 18 | + |
| 19 | + store := testConfigStore(t) |
| 20 | + registry := status.NewRegistry() |
| 21 | + streamStore := streamstatus.NewStore(streamstatus.StoreOptions{}) |
| 22 | + |
| 23 | + handler, err := NewHandler(Options{ |
| 24 | + Hub: mustTestHub(t), |
| 25 | + Store: store, |
| 26 | + History: NewMessageHistory(0), |
| 27 | + Registry: registry, |
| 28 | + StreamStatus: streamStore, |
| 29 | + }) |
| 30 | + require.NoError(t, err) |
| 31 | + |
| 32 | + rec := httptest.NewRecorder() |
| 33 | + handler.ServeHTTP(rec, httptest.NewRequest(http.MethodGet, "/api/streams/status", nil)) |
| 34 | + require.Equal(t, http.StatusOK, rec.Code) |
| 35 | + |
| 36 | + var payload struct { |
| 37 | + ViewersTotal struct { |
| 38 | + Current *int `json:"current"` |
| 39 | + Source string `json:"source"` |
| 40 | + } `json:"viewers_total"` |
| 41 | + Platforms []struct { |
| 42 | + Platform string `json:"platform"` |
| 43 | + State string `json:"state"` |
| 44 | + Viewers struct { |
| 45 | + Current *int `json:"current"` |
| 46 | + } `json:"viewers"` |
| 47 | + Chat struct { |
| 48 | + State string `json:"state"` |
| 49 | + } `json:"chat"` |
| 50 | + } `json:"platforms"` |
| 51 | + } |
| 52 | + require.NoError(t, json.Unmarshal(rec.Body.Bytes(), &payload)) |
| 53 | + require.Nil(t, payload.ViewersTotal.Current) |
| 54 | + require.Equal(t, "local_samples", payload.ViewersTotal.Source) |
| 55 | + require.Len(t, payload.Platforms, 3) |
| 56 | + require.Equal(t, "twitch", payload.Platforms[0].Platform) |
| 57 | + require.Equal(t, "youtube", payload.Platforms[1].Platform) |
| 58 | + require.Equal(t, "vk", payload.Platforms[2].Platform) |
| 59 | + |
| 60 | + for _, platform := range payload.Platforms { |
| 61 | + require.Equal(t, "unknown", platform.State) |
| 62 | + require.Nil(t, platform.Viewers.Current) |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +func TestStreamsStatus_WhenConnectorConnected_ExpectChatStateOnly(t *testing.T) { |
| 67 | + t.Parallel() |
| 68 | + |
| 69 | + store := testConfigStore(t) |
| 70 | + updated := store.Snapshot() |
| 71 | + updated.Twitch.Enabled = true |
| 72 | + updated.Twitch.Channel = "streamer" |
| 73 | + require.NoError(t, store.Replace(updated)) |
| 74 | + |
| 75 | + registry := status.NewRegistry() |
| 76 | + registry.SetTwitch(status.Snapshot{State: status.StateConnected}) |
| 77 | + |
| 78 | + handler, err := NewHandler(Options{ |
| 79 | + Hub: mustTestHub(t), |
| 80 | + Store: store, |
| 81 | + History: NewMessageHistory(0), |
| 82 | + Registry: registry, |
| 83 | + StreamStatus: streamstatus.NewStore(streamstatus.StoreOptions{}), |
| 84 | + }) |
| 85 | + require.NoError(t, err) |
| 86 | + |
| 87 | + rec := httptest.NewRecorder() |
| 88 | + handler.ServeHTTP(rec, httptest.NewRequest(http.MethodGet, "/api/streams/status", nil)) |
| 89 | + require.Equal(t, http.StatusOK, rec.Code) |
| 90 | + |
| 91 | + var payload struct { |
| 92 | + Platforms []struct { |
| 93 | + State string `json:"state"` |
| 94 | + Chat struct { |
| 95 | + State string `json:"state"` |
| 96 | + } `json:"chat"` |
| 97 | + } `json:"platforms"` |
| 98 | + } |
| 99 | + require.NoError(t, json.Unmarshal(rec.Body.Bytes(), &payload)) |
| 100 | + require.Equal(t, "unknown", payload.Platforms[0].State) |
| 101 | + require.Equal(t, "connected", payload.Platforms[0].Chat.State) |
| 102 | +} |
| 103 | + |
| 104 | +func mustTestHub(t *testing.T) *Hub { |
| 105 | + t.Helper() |
| 106 | + |
| 107 | + hub, err := NewHub(bus.New(0)) |
| 108 | + require.NoError(t, err) |
| 109 | + return hub |
| 110 | +} |
0 commit comments