From dc638c7f7c2479d5ba27b30cdd11c5e3605ab905 Mon Sep 17 00:00:00 2001 From: KainTheReaper Date: Tue, 16 Mar 2021 14:11:22 -0700 Subject: [PATCH 1/7] Hype Train second merge attempt Lets try that again --- internal/events/models.go | 33 +++-- .../hype_train/hype_train_begin_event.go | 107 +++++++++++++ .../hype_train/hype_train_begin_event_test.go | 88 +++++++++++ .../types/hype_train/hype_train_end_event.go | 103 +++++++++++++ .../hype_train/hype_train_end_event_test.go | 88 +++++++++++ .../hype_train/hype_train_progress_event.go | 140 ++++++++++++++++++ .../hype_train_progress_event_test.go | 99 +++++++++++++ internal/events/types/types.go | 6 + internal/models/hype_train.go | 91 ++++++++++++ internal/util/random.go | 14 ++ 10 files changed, 754 insertions(+), 15 deletions(-) create mode 100644 internal/events/types/hype_train/hype_train_begin_event.go create mode 100644 internal/events/types/hype_train/hype_train_begin_event_test.go create mode 100644 internal/events/types/hype_train/hype_train_end_event.go create mode 100644 internal/events/types/hype_train/hype_train_end_event_test.go create mode 100644 internal/events/types/hype_train/hype_train_progress_event.go create mode 100644 internal/events/types/hype_train/hype_train_progress_event_test.go create mode 100644 internal/models/hype_train.go diff --git a/internal/events/models.go b/internal/events/models.go index e2ee9d32..705f591c 100644 --- a/internal/events/models.go +++ b/internal/events/models.go @@ -3,21 +3,24 @@ package events var triggerSupported = map[string]bool{ - "subscribe": true, - "unsubscribe": true, - "gift": true, - "cheer": true, - "transaction": true, - "follow": true, - "add-redemption": true, - "update-redemption": true, - "add-reward": true, - "update-reward": true, - "remove-reward": true, - "add-moderator": true, - "remove-moderator": true, - "ban": true, - "unban": true, + "subscribe": true, + "unsubscribe": true, + "gift": true, + "cheer": true, + "transaction": true, + "follow": true, + "add-redemption": true, + "update-redemption": true, + "add-reward": true, + "update-reward": true, + "remove-reward": true, + "add-moderator": true, + "remove-moderator": true, + "ban": true, + "unban": true, + "hype-train-begin": true, + "hype-train-progress": true, + "hype-train-end": true, } var transportSupported = map[string]bool{ diff --git a/internal/events/types/hype_train/hype_train_begin_event.go b/internal/events/types/hype_train/hype_train_begin_event.go new file mode 100644 index 00000000..246c2268 --- /dev/null +++ b/internal/events/types/hype_train/hype_train_begin_event.go @@ -0,0 +1,107 @@ +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 +package hype_train_begin + +import( + "encoding/json" + "time" + + "github.com/twitchdev/twitch-cli/internal/events" + "github.com/twitchdev/twitch-cli/internal/models" + "github.com/twitchdev/twitch-cli/internal/util" +) + +var transportsSupported = map[string]bool{ + models.TransportWebSub: false, + models.TransportEventSub: true, +} + +var triggerSupported = []string{"hype-train-begin"} + +var triggerMapping = map[string]map[string]string{ + models.TransportEventSub: { + "hype-train-begin": "channel.hype_train.begin", + }, +} + +type Event struct{} + +func (e Event) GenerateEvent(params events.MockEventParameters) (events.MockEventResponse, error) { + var event []byte + var err error + + //Some values + StartedAtTime := util.GetTimestamp() + localTotal := util.RandomViewerCount() + localGoal := util.RandomViewerCount() + localProgress := (localTotal/localGoal) + localRandomUser1 := util.RandomUserID() + localRandomUser2 := util.RandomUserID() + localRandomUser3 := util.RandomUserID() + localLC := models.ContributionData{TotalContribution: util.RandomViewerCount(), TypeOfContribution: util.RandomType(), UserWhoMadeContribution: localRandomUser1, UserNameWhoMadeContribution: localRandomUser1, UserLoginWhoMadeContribution: localRandomUser1} + localTC := []models.ContributionData{{TotalContribution: util.RandomViewerCount(), TypeOfContribution: util.RandomType(), UserWhoMadeContribution: localRandomUser2, UserNameWhoMadeContribution: localRandomUser2, UserLoginWhoMadeContribution: localRandomUser2},{TotalContribution: util.RandomViewerCount(), TypeOfContribution: util.RandomType(), UserWhoMadeContribution: localRandomUser3, UserNameWhoMadeContribution: localRandomUser3, UserLoginWhoMadeContribution: localRandomUser3}} + ExpiresAtTime := util.GetTimestamp() + + switch params.Transport { + case models.TransportEventSub: + body := *&models.EventsubResponse{ + Subscription: models.EventsubSubscription{ + ID: params.ID, + Status: "enabled", + Type: triggerMapping[params.Transport][params.Trigger], + Version: "1.0", + Condition: models.EventsubCondition{ + BroadcasterUserID: params.ToUserID, + }, + Transport: models.EventsubTransport{ + Method: "webhook", + Callback: "null", + }, + CreatedAt: util.GetTimestamp().Format(time.RFC3339Nano), + }, + Event: models.HypeTrainEventBeginSubEvent{ + BroadcasterUserID: params.ToUserID, + BroadcasterUserLogin: params.ToUserName, + BroadcasterUserName: params.ToUserName, + Total: localTotal, + Progress: localProgress, + Goal: localGoal, + TopContributions: localTC, + LastContribution: localLC, + StartedAtTimestamp: StartedAtTime.String(), + ExpiresAtTimestamp: ExpiresAtTime.String(), + }, + } + + event, err = json.Marshal(body) + if err != nil { + return events.MockEventResponse{}, err + } + + default: + return events.MockEventResponse{}, nil + } + + return events.MockEventResponse{ + ID: params.ID, + JSON: event, + FromUser: params.FromUserID, + ToUser: params.ToUserID, + }, nil +} + +func (e Event) ValidTransport(t string) bool { + return transportsSupported[t] +} + +func (e Event) ValidTrigger(t string) bool { + for _, ts := range triggerSupported { + if ts == t { + return true + } + } + return false +} +func (e Event) GetTopic(transport string, trigger string) string { + return triggerMapping[transport][trigger] +} \ No newline at end of file diff --git a/internal/events/types/hype_train/hype_train_begin_event_test.go b/internal/events/types/hype_train/hype_train_begin_event_test.go new file mode 100644 index 00000000..a3b4bddc --- /dev/null +++ b/internal/events/types/hype_train/hype_train_begin_event_test.go @@ -0,0 +1,88 @@ +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 +package hype_train_begin + +import ( + "encoding/json" + "testing" + + "github.com/twitchdev/twitch-cli/internal/events" + "github.com/twitchdev/twitch-cli/internal/models" + "github.com/twitchdev/twitch-cli/internal/util" +) + + +var toUser = "4567" + +func TestEventSub(t *testing.T) { + a := util.SetupTestEnv(t) + + params := *&events.MockEventParameters{ + ToUserID: toUser, + Transport: models.TransportEventSub, + Trigger: "hype-train-begin", + } + + r, err := Event{}.GenerateEvent(params) + a.Nil(err) + + var body models.HypeTrainEventBeginSubResponse + err = json.Unmarshal(r.JSON, &body) + a.Nil(err) + + a.Equal("channel.hype_train.begin", body.Subscription.Type, "Expected event type %v, got %v", "channel.hype_train.begin", body.Subscription.Type) + a.Equal(toUser, body.Event.BroadcasterUserID, "Expected to user %v, got %v", toUser, body.Event.BroadcasterUserID) + +} + +func TestWebSub(t *testing.T) { + + // Hype_Train_begin does not have a websub part + // Remove? + + a := util.SetupTestEnv(t) + + params := *&events.MockEventParameters{ + ToUserID: toUser, + Transport: models.TransportWebSub, + Trigger: "hype-train-begin", + } + + _, err := Event{}.GenerateEvent(params) + a.NotNil(err) + + +} +func TestFakeTransport(t *testing.T) { + a := util.SetupTestEnv(t) + + params := *&events.MockEventParameters{ + ToUserID: toUser, + Transport: "fake_transport", + Trigger: "hype-train-begin", + } + + r, err := Event{}.GenerateEvent(params) + a.Nil(err) + a.Empty(r) +} +func TestValidTrigger(t *testing.T) { + a := util.SetupTestEnv(t) + + r := Event{}.ValidTrigger("hype-train-begin") + a.Equal(true, r) +} + +func TestValidTransport(t *testing.T) { + a := util.SetupTestEnv(t) + + r := Event{}.ValidTransport(models.TransportWebSub) + a.Equal(false, r) + + r = Event{}.ValidTransport(models.TransportEventSub) + a.Equal(true, r) +} + +func TestGetTopic(t *testing.T) { + a := util.SetupTestEnv(t) +} \ No newline at end of file diff --git a/internal/events/types/hype_train/hype_train_end_event.go b/internal/events/types/hype_train/hype_train_end_event.go new file mode 100644 index 00000000..d62c66a3 --- /dev/null +++ b/internal/events/types/hype_train/hype_train_end_event.go @@ -0,0 +1,103 @@ +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 +package hype_train_end + +import( + "encoding/json" + "time" + + "github.com/twitchdev/twitch-cli/internal/events" + "github.com/twitchdev/twitch-cli/internal/models" + "github.com/twitchdev/twitch-cli/internal/util" +) + +var transportsSupported = map[string]bool{ + models.TransportWebSub: false, + models.TransportEventSub: true, +} + +var triggerSupported = []string{"hype-train-end"} + +var triggerMapping = map[string]map[string]string{ + models.TransportEventSub: { + "hype-train-end": "channel.hype_train.end", + }, +} + +type Event struct{} + +func (e Event) GenerateEvent(params events.MockEventParameters) (events.MockEventResponse, error) { + var event []byte + var err error + + //Some values + StartedAtTime := util.GetTimestamp() + localTotal := util.RandomViewerCount() + localLevel := util.RandomViewerCount()%4 + localRandomUser2 := util.RandomUserID() + localRandomUser3 := util.RandomUserID() + localTC := []models.ContributionData{{TotalContribution: util.RandomViewerCount(), TypeOfContribution: util.RandomType(), UserWhoMadeContribution: localRandomUser2, UserNameWhoMadeContribution: localRandomUser2, UserLoginWhoMadeContribution: localRandomUser2},{TotalContribution: util.RandomViewerCount(), TypeOfContribution: util.RandomType(), UserWhoMadeContribution: localRandomUser3, UserNameWhoMadeContribution: localRandomUser3, UserLoginWhoMadeContribution: localRandomUser3}} + EndedAtTime := util.GetTimestamp() + CooldownAtTime := util.GetTimestamp() + + switch params.Transport { + case models.TransportEventSub: + body := *&models.EventsubResponse{ + Subscription: models.EventsubSubscription{ + ID: params.ID, + Status: "enabled", + Type: triggerMapping[params.Transport][params.Trigger], + Version: "1.0", + Condition: models.EventsubCondition{ + BroadcasterUserID: params.ToUserID, + }, + Transport: models.EventsubTransport{ + Method: "webhook", + Callback: "null", + }, + CreatedAt: util.GetTimestamp().Format(time.RFC3339Nano), + }, + Event: models.HypeTrainEventEndSubEvent{ + BroadcasterUserID: params.ToUserID, + BroadcasterUserLogin: params.ToUserName, + BroadcasterUserName: params.ToUserName, + Level: localLevel, + Total: localTotal, + TopContributions: localTC, + StartedAtTimestamp: StartedAtTime.String(), + EndedAtTimestamp: EndedAtTime.String(), + CooldownEndsAtTimestamp: CooldownAtTime.String(), + }, + } + + event, err = json.Marshal(body) + if err != nil { + return events.MockEventResponse{}, err + } + default: + return events.MockEventResponse{}, nil + } + + return events.MockEventResponse{ + ID: params.ID, + JSON: event, + FromUser: params.FromUserID, + ToUser: params.ToUserID, + }, nil +} + +func (e Event) ValidTransport(t string) bool { + return transportsSupported[t] +} + +func (e Event) ValidTrigger(t string) bool { + for _, ts := range triggerSupported { + if ts == t { + return true + } + } + return false +} +func (e Event) GetTopic(transport string, trigger string) string { + return triggerMapping[transport][trigger] +} \ No newline at end of file diff --git a/internal/events/types/hype_train/hype_train_end_event_test.go b/internal/events/types/hype_train/hype_train_end_event_test.go new file mode 100644 index 00000000..2130dfdf --- /dev/null +++ b/internal/events/types/hype_train/hype_train_end_event_test.go @@ -0,0 +1,88 @@ +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 +package hype_train_end + +import ( + "encoding/json" + "testing" + + "github.com/twitchdev/twitch-cli/internal/events" + "github.com/twitchdev/twitch-cli/internal/models" + "github.com/twitchdev/twitch-cli/internal/util" +) + + +var toUser = "4567" + +func TestEventSub(t *testing.T) { + a := util.SetupTestEnv(t) + + params := *&events.MockEventParameters{ + ToUserID: toUser, + Transport: models.TransportEventSub, + Trigger: "hype-train-end", + } + + r, err := Event{}.GenerateEvent(params) + a.Nil(err) + + var body models.HypeTrainEventEndSubResponse + err = json.Unmarshal(r.JSON, &body) + a.Nil(err) + + a.Equal("channel.hype_train.end", body.Subscription.Type, "Expected event type %v, got %v", "channel.hype_train.end", body.Subscription.Type) + a.Equal(toUser, body.Event.BroadcasterUserID, "Expected to user %v, got %v", toUser, body.Event.BroadcasterUserID) + +} + +func TestWebSub(t *testing.T) { + + // Hype_Train_begin does not have a websub part + // Remove? + + a := util.SetupTestEnv(t) + + params := *&events.MockEventParameters{ + ToUserID: toUser, + Transport: models.TransportWebSub, + Trigger: "hype-train-end", + } + + _, err := Event{}.GenerateEvent(params) + a.NotNil(err) + + +} +func TestFakeTransport(t *testing.T) { + a := util.SetupTestEnv(t) + + params := *&events.MockEventParameters{ + ToUserID: toUser, + Transport: "fake_transport", + Trigger: "hype-train-end", + } + + r, err := Event{}.GenerateEvent(params) + a.Nil(err) + a.Empty(r) +} +func TestValidTrigger(t *testing.T) { + a := util.SetupTestEnv(t) + + r := Event{}.ValidTrigger("hype-train-end") + a.Equal(true, r) +} + +func TestValidTransport(t *testing.T) { + a := util.SetupTestEnv(t) + + r := Event{}.ValidTransport(models.TransportWebSub) + a.Equal(false, r) + + r = Event{}.ValidTransport(models.TransportEventSub) + a.Equal(true, r) +} + +func TestGetTopic(t *testing.T) { + a := util.SetupTestEnv(t) +} \ No newline at end of file diff --git a/internal/events/types/hype_train/hype_train_progress_event.go b/internal/events/types/hype_train/hype_train_progress_event.go new file mode 100644 index 00000000..19a59cf9 --- /dev/null +++ b/internal/events/types/hype_train/hype_train_progress_event.go @@ -0,0 +1,140 @@ +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 +package hype_train_progress + +import( + "encoding/json" + "time" + + "github.com/twitchdev/twitch-cli/internal/events" + "github.com/twitchdev/twitch-cli/internal/models" + "github.com/twitchdev/twitch-cli/internal/util" +) + +var transportsSupported = map[string]bool{ + models.TransportWebSub: true, + models.TransportEventSub: true, +} + +var triggerSupported = []string{"hype-train-begin", "hype-train-progress", "hype-train-end"} + +var triggerMapping = map[string]map[string]string{ + models.TransportWebSub: { + "hype-train-progress": "hypetrain.progression", + }, + models.TransportEventSub: { + "hype-train-progress": "channel.hype_train.progress", + }, +} + +type Event struct{} + +func (e Event) GenerateEvent(params events.MockEventParameters) (events.MockEventResponse, error) { + var event []byte + var err error + + //Some values + StartedAtTime := util.GetTimestamp() + localTotal := util.RandomViewerCount() + localLevel := util.RandomViewerCount()%4 + localGoal := util.RandomViewerCount() + localProgress := (localTotal/localGoal) + localRandomUser1 := util.RandomUserID() + localRandomUser2 := util.RandomUserID() + localRandomUser3 := util.RandomUserID() + localLC := models.ContributionData{TotalContribution: util.RandomViewerCount(), TypeOfContribution: util.RandomType(), UserWhoMadeContribution: localRandomUser1, UserNameWhoMadeContribution: localRandomUser1, UserLoginWhoMadeContribution: localRandomUser1} + localTC := []models.ContributionData{{TotalContribution: util.RandomViewerCount(), TypeOfContribution: util.RandomType(), UserWhoMadeContribution: localRandomUser2, UserNameWhoMadeContribution: localRandomUser2, UserLoginWhoMadeContribution: localRandomUser2},{TotalContribution: util.RandomViewerCount(), TypeOfContribution: util.RandomType(), UserWhoMadeContribution: localRandomUser3, UserNameWhoMadeContribution: localRandomUser3, UserLoginWhoMadeContribution: localRandomUser3}} + ExpiresAtTime := util.GetTimestamp() + CooldownAtTime := util.GetTimestamp() + + switch params.Transport { + case models.TransportEventSub: + body := *&models.EventsubResponse{ + Subscription: models.EventsubSubscription{ + ID: params.ID, + Status: "enabled", + Type: triggerMapping[params.Transport][params.Trigger], + Version: "1", + Condition: models.EventsubCondition{ + BroadcasterUserID: params.ToUserID, + }, + Transport: models.EventsubTransport{ + Method: "webhook", + Callback: "null", + }, + CreatedAt: util.GetTimestamp().Format(time.RFC3339Nano), + }, + Event: models.HypeTrainEventProgressSubEvent{ + BroadcasterUserID: params.ToUserID, + BroadcasterUserLogin: params.ToUserName, + BroadcasterUserName: params.ToUserName, + Level: localLevel, + Total: localTotal, + Progress: localProgress, + Goal: localGoal, + TopContributions: localTC, + LastContribution: localLC, + StartedAtTimestamp: StartedAtTime.String(), + ExpiresAtTimestamp: ExpiresAtTime.String(), + }, + } + + event, err = json.Marshal(body) + if err != nil { + return events.MockEventResponse{}, err + } + case models.TransportWebSub: + body := *&models.HypeTrainWebSubResponse{ + Data: []models.HypeTrainWebSubEvent{ + { + ID: params.ID, + EventType: triggerMapping[params.Transport][params.Trigger], + EventTimestamp: util.GetTimestamp().Format(time.RFC3339), + Version: "1.0", + EventData: models.HypeTrainWebsubEventData{ + BroadcasterID: params.ToUserID, + CooldownEndTimestamp: CooldownAtTime.String(), + ExpiresAtTimestamp: ExpiresAtTime.String(), + Goal: localGoal, + Id: util.RandomGUID(), + LastContribution: localLC, + Level: localLevel, + StartedAtTimestamp: StartedAtTime.String(), + TopContributions: localTC, + Total: localTotal, + }, + }, + }, + } + event, err = json.Marshal(body) + if err != nil { + return events.MockEventResponse{}, err + } + + default: + return events.MockEventResponse{}, nil + } + + return events.MockEventResponse{ + ID: params.ID, + JSON: event, + FromUser: params.FromUserID, + ToUser: params.ToUserID, + }, nil +} + +func (e Event) ValidTransport(t string) bool { + return transportsSupported[t] +} + +func (e Event) ValidTrigger(t string) bool { + for _, ts := range triggerSupported { + if ts == t { + return true + } + } + return false +} +func (e Event) GetTopic(transport string, trigger string) string { + return triggerMapping[transport][trigger] +} \ No newline at end of file diff --git a/internal/events/types/hype_train/hype_train_progress_event_test.go b/internal/events/types/hype_train/hype_train_progress_event_test.go new file mode 100644 index 00000000..b3883e69 --- /dev/null +++ b/internal/events/types/hype_train/hype_train_progress_event_test.go @@ -0,0 +1,99 @@ +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 +package hype_train_progress + +import ( + "encoding/json" + "testing" + + "github.com/twitchdev/twitch-cli/internal/events" + "github.com/twitchdev/twitch-cli/internal/models" + "github.com/twitchdev/twitch-cli/internal/util" +) + +var toUser = "4567" + +func TestEventSub(t *testing.T) { + a := util.SetupTestEnv(t) + + params := *&events.MockEventParameters{ + FromUserID: fromUser, + ToUserID: toUser, + Transport: models.TransportEventSub, + Trigger: "hype-train-progress", + } + + r, err := Event{}.GenerateEvent(params) + a.Nil(err) + + var body models.HypeTrainEventProgressSubResponse + err = json.Unmarshal(r.JSON, &body) + a.Nil(err) + + a.Equal("channel.hype_train.progress", body.Subscription.Type, "Expected event type %v, got %v", "channel.hype_train.progress", body.Subscription.Type) + a.Equal(toUser, body.Event.BroadcasterUserID, "Expected to user %v, got %v", toUser, body.Event.BroadcasterUserID) + +} + +func TestWebSub(t *testing.T) { + a := util.SetupTestEnv(t) + + params := *&events.MockEventParameters{ + ToUserID: toUser, + Transport: models.TransportWebSub, + Trigger: "hype-train-progress", + } + + r, err := Event{}.GenerateEvent(params) + a.Nil(err) + + var body models.HypeTrainWebSubResponse + err = json.Unmarshal(r.JSON, &body) + a.Nil(err) + + a.Equal("hypetrain.progression", body.Data[0].EventType, "Expected event type %v, got %v", "hypetrain.progression", body.Data[0].EventType) + a.Equal(toUser, body.Data[0].EventData.BroadcasterID, "Expected to user %v, got %v", toUser, body.Data[0].EventData.BroadcasterID) + + + params = *&events.MockEventParameters{ + ToUserID: toUser, + Transport: models.TransportWebSub, + } +} +func TestFakeTransport(t *testing.T) { + a := util.SetupTestEnv(t) + + params := *&events.MockEventParameters{ + ToUserID: toUser, + Transport: "fake_transport", + Trigger: "hype-train-progress", + } + + r, err := Event{}.GenerateEvent(params) + a.Nil(err) + a.Empty(r) +} +func TestValidTrigger(t *testing.T) { + a := util.SetupTestEnv(t) + + r := Event{}.ValidTrigger("hype-train-progress") + a.Equal(true, r) + +} + +func TestValidTransport(t *testing.T) { + a := util.SetupTestEnv(t) + + r := Event{}.ValidTransport(models.TransportWebSub) + a.Equal(true, r) + + r = Event{}.ValidTransport(models.TransportEventSub) + a.Equal(true, r) +} + +func TestGetTopic(t *testing.T) { + a := util.SetupTestEnv(t) + + r := Event{}.GetTopic(models.TransportWebSub, "hype-train-progress") + a.Equal("hypetrain.progression", r, "Expected %v, got %v", "hypetrain.progression", r) +} \ No newline at end of file diff --git a/internal/events/types/types.go b/internal/events/types/types.go index 4549244a..07165cfb 100644 --- a/internal/events/types/types.go +++ b/internal/events/types/types.go @@ -12,6 +12,9 @@ import ( "github.com/twitchdev/twitch-cli/internal/events/types/cheer" "github.com/twitchdev/twitch-cli/internal/events/types/extension_transaction" "github.com/twitchdev/twitch-cli/internal/events/types/follow" + "github.com/twitchdev/twitch-cli/internal/events/types/hype_train_begin" + "github.com/twitchdev/twitch-cli/internal/events/types/hype_train_end" + "github.com/twitchdev/twitch-cli/internal/events/types/hype_train_progress" "github.com/twitchdev/twitch-cli/internal/events/types/moderator_change" "github.com/twitchdev/twitch-cli/internal/events/types/raid" "github.com/twitchdev/twitch-cli/internal/events/types/stream_change" @@ -29,6 +32,9 @@ func All() []events.MockEvent { cheer.Event{}, extension_transaction.Event{}, follow.Event{}, + hype_train_begin.Event{}, + hype_train_end.Event{}, + hype_train_progress.Event{}, raid.Event{}, subscribe.Event{}, stream_change.Event{}, diff --git a/internal/models/hype_train.go b/internal/models/hype_train.go new file mode 100644 index 00000000..2441559e --- /dev/null +++ b/internal/models/hype_train.go @@ -0,0 +1,91 @@ +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 +package models + + +type HypeTrainWebSubEvent struct { + ID string `json:"id"` + EventType string `json:"event_type"` + EventTimestamp string `json:"event_timestamp"` + Version string `json:"version"` + EventData HypeTrainWebsubEventData `json:"event_data"` +} + +type HypeTrainWebsubEventData struct { + BroadcasterID string `json:"broadcaster_id"` + CooldownEndTimestamp string `json:"cooldown_end_time"` + ExpiresAtTimestamp string `json:"expires_at"` + Goal int64 `json:"goal"` + Id string `json:"id"` + LastContribution ContributionData `json:"last_contribution"` + Level int64 `json:"level"` + StartedAtTimestamp string `json:"started_at"` + TopContributions []ContributionData `json:"top_contributions"` + Total int64 `json:"total"` +} + +type ContributionData struct{ + TotalContribution int64 `json:"total"` + TypeOfContribution string `json:"type"` + UserWhoMadeContribution string `json:"user_id"` + UserNameWhoMadeContribution string `json:"user_name"` + UserLoginWhoMadeContribution string `json:"user_login"` +} + +type HypeTrainWebSubResponse struct { + Data []HypeTrainWebSubEvent `json:"data"` +} + +type HypeTrainEventBeginSubResponse struct { + Subscription EventsubSubscription `json:"subscription"` + Event HypeTrainEventBeginSubEvent `json:"event"` +} + +type HypeTrainEventProgressSubResponse struct { + Subscription EventsubSubscription `json:"subscription"` + Event HypeTrainEventProgressSubEvent `json:"event"` +} + +type HypeTrainEventEndSubResponse struct { + Subscription EventsubSubscription `json:"subscription"` + Event HypeTrainEventEndSubEvent `json:"event"` +} + +type HypeTrainEventBeginSubEvent struct { + BroadcasterUserID string `json:"broadcaster_user_id"` + BroadcasterUserLogin string `json:"broadcaster_user_login"` + BroadcasterUserName string `json:"broadcaster_user_name"` + Total int64 `json:"total"` + Progress int64 `json:"progress"` + Goal int64 `json:"goal"` + TopContributions []ContributionData `json:"top_contributions"` + LastContribution ContributionData `json:"last_contribution"` + StartedAtTimestamp string `json:"started_at"` + ExpiresAtTimestamp string `json:"expires_at"` +} + +type HypeTrainEventProgressSubEvent struct { + BroadcasterUserID string `json:"broadcaster_user_id"` + BroadcasterUserLogin string `json:"broadcaster_user_login"` + BroadcasterUserName string `json:"broadcaster_user_name"` + Level int64 `json:"level"` + Total int64 `json:"total"` + Progress int64 `json:"progress"` + Goal int64 `json:"goal"` + TopContributions []ContributionData `json:"top_contributions"` + LastContribution ContributionData `json:"last_contribution"` + StartedAtTimestamp string `json:"started_at"` + ExpiresAtTimestamp string `json:"expires_at"` +} + +type HypeTrainEventEndSubEvent struct { + BroadcasterUserID string `json:"broadcaster_user_id"` + BroadcasterUserLogin string `json:"broadcaster_user_login"` + BroadcasterUserName string `json:"broadcaster_user_name"` + Level int64 `json:"level"` + Total int64 `json:"total"` + TopContributions []ContributionData `json:"top_contributions"` + StartedAtTimestamp string `json:"started_at"` + EndedAtTimestamp string `json:"ended_at"` + CooldownEndsAtTimestamp string `json:"cooldown_ends_at"` +} \ No newline at end of file diff --git a/internal/util/random.go b/internal/util/random.go index 6e7ceb15..6e454955 100644 --- a/internal/util/random.go +++ b/internal/util/random.go @@ -46,3 +46,17 @@ func RandomViewerCount() int64 { } return viewer.Int64() } + +// RandomViewerCount generates a fake viewercount between 0->10,000,000 +func RandomType() string { + someInt, err := rand.Int(rand.Reader, big.NewInt(1*10*100*100*100)) + if err != nil { + log.Fatal(err.Error()) + } + if (someInt.Int64()%2) == 0{ + return "bits" + }else + { + return "subscription" + } +} \ No newline at end of file From e80a5e9534bf63afa887bbc1c2afe4115d857fe0 Mon Sep 17 00:00:00 2001 From: KainTheReaper Date: Tue, 16 Mar 2021 15:31:48 -0700 Subject: [PATCH 2/7] Easy fixes Doing the easy ones first --- .../hype_train/hype_train_begin_event.go | 4 ++-- .../hype_train/hype_train_begin_event_test.go | 18 ------------------ .../types/hype_train/hype_train_end_event.go | 6 +++--- .../hype_train/hype_train_end_event_test.go | 19 ------------------- .../hype_train/hype_train_progress_event.go | 8 ++++---- internal/util/random.go | 2 +- 6 files changed, 10 insertions(+), 47 deletions(-) diff --git a/internal/events/types/hype_train/hype_train_begin_event.go b/internal/events/types/hype_train/hype_train_begin_event.go index 246c2268..b861fb79 100644 --- a/internal/events/types/hype_train/hype_train_begin_event.go +++ b/internal/events/types/hype_train/hype_train_begin_event.go @@ -68,8 +68,8 @@ func (e Event) GenerateEvent(params events.MockEventParameters) (events.MockEven Goal: localGoal, TopContributions: localTC, LastContribution: localLC, - StartedAtTimestamp: StartedAtTime.String(), - ExpiresAtTimestamp: ExpiresAtTime.String(), + StartedAtTimestamp: StartedAtTime.Format(time.RFC3339Nano), + ExpiresAtTimestamp: ExpiresAtTime.Format(time.RFC3339Nano), }, } diff --git a/internal/events/types/hype_train/hype_train_begin_event_test.go b/internal/events/types/hype_train/hype_train_begin_event_test.go index a3b4bddc..972c7e8d 100644 --- a/internal/events/types/hype_train/hype_train_begin_event_test.go +++ b/internal/events/types/hype_train/hype_train_begin_event_test.go @@ -35,24 +35,6 @@ func TestEventSub(t *testing.T) { } -func TestWebSub(t *testing.T) { - - // Hype_Train_begin does not have a websub part - // Remove? - - a := util.SetupTestEnv(t) - - params := *&events.MockEventParameters{ - ToUserID: toUser, - Transport: models.TransportWebSub, - Trigger: "hype-train-begin", - } - - _, err := Event{}.GenerateEvent(params) - a.NotNil(err) - - -} func TestFakeTransport(t *testing.T) { a := util.SetupTestEnv(t) diff --git a/internal/events/types/hype_train/hype_train_end_event.go b/internal/events/types/hype_train/hype_train_end_event.go index d62c66a3..4a9de55e 100644 --- a/internal/events/types/hype_train/hype_train_end_event.go +++ b/internal/events/types/hype_train/hype_train_end_event.go @@ -64,9 +64,9 @@ func (e Event) GenerateEvent(params events.MockEventParameters) (events.MockEven Level: localLevel, Total: localTotal, TopContributions: localTC, - StartedAtTimestamp: StartedAtTime.String(), - EndedAtTimestamp: EndedAtTime.String(), - CooldownEndsAtTimestamp: CooldownAtTime.String(), + StartedAtTimestamp: StartedAtTime.Format(time.RFC3339Nano), + EndedAtTimestamp: EndedAtTime.Format(time.RFC3339Nano), + CooldownEndsAtTimestamp: CooldownAtTime.Format(time.RFC3339Nano), }, } diff --git a/internal/events/types/hype_train/hype_train_end_event_test.go b/internal/events/types/hype_train/hype_train_end_event_test.go index 2130dfdf..0dbdfe05 100644 --- a/internal/events/types/hype_train/hype_train_end_event_test.go +++ b/internal/events/types/hype_train/hype_train_end_event_test.go @@ -33,25 +33,6 @@ func TestEventSub(t *testing.T) { a.Equal("channel.hype_train.end", body.Subscription.Type, "Expected event type %v, got %v", "channel.hype_train.end", body.Subscription.Type) a.Equal(toUser, body.Event.BroadcasterUserID, "Expected to user %v, got %v", toUser, body.Event.BroadcasterUserID) -} - -func TestWebSub(t *testing.T) { - - // Hype_Train_begin does not have a websub part - // Remove? - - a := util.SetupTestEnv(t) - - params := *&events.MockEventParameters{ - ToUserID: toUser, - Transport: models.TransportWebSub, - Trigger: "hype-train-end", - } - - _, err := Event{}.GenerateEvent(params) - a.NotNil(err) - - } func TestFakeTransport(t *testing.T) { a := util.SetupTestEnv(t) diff --git a/internal/events/types/hype_train/hype_train_progress_event.go b/internal/events/types/hype_train/hype_train_progress_event.go index 19a59cf9..cb6c6af8 100644 --- a/internal/events/types/hype_train/hype_train_progress_event.go +++ b/internal/events/types/hype_train/hype_train_progress_event.go @@ -74,8 +74,8 @@ func (e Event) GenerateEvent(params events.MockEventParameters) (events.MockEven Goal: localGoal, TopContributions: localTC, LastContribution: localLC, - StartedAtTimestamp: StartedAtTime.String(), - ExpiresAtTimestamp: ExpiresAtTime.String(), + StartedAtTimestamp: StartedAtTime.Format(time.RFC3339Nano), + ExpiresAtTimestamp: ExpiresAtTime.Format(time.RFC3339Nano), }, } @@ -93,8 +93,8 @@ func (e Event) GenerateEvent(params events.MockEventParameters) (events.MockEven Version: "1.0", EventData: models.HypeTrainWebsubEventData{ BroadcasterID: params.ToUserID, - CooldownEndTimestamp: CooldownAtTime.String(), - ExpiresAtTimestamp: ExpiresAtTime.String(), + CooldownEndTimestamp: CooldownAtTime.Format(time.RFC3339), + ExpiresAtTimestamp: ExpiresAtTime.Format(time.RFC3339), Goal: localGoal, Id: util.RandomGUID(), LastContribution: localLC, diff --git a/internal/util/random.go b/internal/util/random.go index 6e454955..ceac3936 100644 --- a/internal/util/random.go +++ b/internal/util/random.go @@ -47,7 +47,7 @@ func RandomViewerCount() int64 { return viewer.Int64() } -// RandomViewerCount generates a fake viewercount between 0->10,000,000 +// RandomType generates a fake type; Either bits or subscription, in roughly even distribution func RandomType() string { someInt, err := rand.Int(rand.Reader, big.NewInt(1*10*100*100*100)) if err != nil { From 5eab482192bbc7ddef63e94c7a4a0151c0c93fc4 Mon Sep 17 00:00:00 2001 From: KainTheReaper Date: Wed, 17 Mar 2021 14:42:18 -0700 Subject: [PATCH 3/7] Refactored Hype Train has been refactored down from 3 events to a single event. Its a bit messy at points but it does the trick --- .../hype_train/hype_train_begin_event.go | 107 --------- .../hype_train/hype_train_begin_event_test.go | 70 ------ .../types/hype_train/hype_train_end_event.go | 103 -------- .../hype_train/hype_train_end_event_test.go | 69 ------ .../types/hype_train/hype_train_event.go | 224 ++++++++++++++++++ ...event_test.go => hype_train_event_test.go} | 42 +++- .../hype_train/hype_train_progress_event.go | 140 ----------- internal/events/types/types.go | 8 +- internal/models/hype_train.go | 104 +++----- 9 files changed, 302 insertions(+), 565 deletions(-) delete mode 100644 internal/events/types/hype_train/hype_train_begin_event.go delete mode 100644 internal/events/types/hype_train/hype_train_begin_event_test.go delete mode 100644 internal/events/types/hype_train/hype_train_end_event.go delete mode 100644 internal/events/types/hype_train/hype_train_end_event_test.go create mode 100644 internal/events/types/hype_train/hype_train_event.go rename internal/events/types/hype_train/{hype_train_progress_event_test.go => hype_train_event_test.go} (66%) delete mode 100644 internal/events/types/hype_train/hype_train_progress_event.go diff --git a/internal/events/types/hype_train/hype_train_begin_event.go b/internal/events/types/hype_train/hype_train_begin_event.go deleted file mode 100644 index b861fb79..00000000 --- a/internal/events/types/hype_train/hype_train_begin_event.go +++ /dev/null @@ -1,107 +0,0 @@ -// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. -// SPDX-License-Identifier: Apache-2.0 -package hype_train_begin - -import( - "encoding/json" - "time" - - "github.com/twitchdev/twitch-cli/internal/events" - "github.com/twitchdev/twitch-cli/internal/models" - "github.com/twitchdev/twitch-cli/internal/util" -) - -var transportsSupported = map[string]bool{ - models.TransportWebSub: false, - models.TransportEventSub: true, -} - -var triggerSupported = []string{"hype-train-begin"} - -var triggerMapping = map[string]map[string]string{ - models.TransportEventSub: { - "hype-train-begin": "channel.hype_train.begin", - }, -} - -type Event struct{} - -func (e Event) GenerateEvent(params events.MockEventParameters) (events.MockEventResponse, error) { - var event []byte - var err error - - //Some values - StartedAtTime := util.GetTimestamp() - localTotal := util.RandomViewerCount() - localGoal := util.RandomViewerCount() - localProgress := (localTotal/localGoal) - localRandomUser1 := util.RandomUserID() - localRandomUser2 := util.RandomUserID() - localRandomUser3 := util.RandomUserID() - localLC := models.ContributionData{TotalContribution: util.RandomViewerCount(), TypeOfContribution: util.RandomType(), UserWhoMadeContribution: localRandomUser1, UserNameWhoMadeContribution: localRandomUser1, UserLoginWhoMadeContribution: localRandomUser1} - localTC := []models.ContributionData{{TotalContribution: util.RandomViewerCount(), TypeOfContribution: util.RandomType(), UserWhoMadeContribution: localRandomUser2, UserNameWhoMadeContribution: localRandomUser2, UserLoginWhoMadeContribution: localRandomUser2},{TotalContribution: util.RandomViewerCount(), TypeOfContribution: util.RandomType(), UserWhoMadeContribution: localRandomUser3, UserNameWhoMadeContribution: localRandomUser3, UserLoginWhoMadeContribution: localRandomUser3}} - ExpiresAtTime := util.GetTimestamp() - - switch params.Transport { - case models.TransportEventSub: - body := *&models.EventsubResponse{ - Subscription: models.EventsubSubscription{ - ID: params.ID, - Status: "enabled", - Type: triggerMapping[params.Transport][params.Trigger], - Version: "1.0", - Condition: models.EventsubCondition{ - BroadcasterUserID: params.ToUserID, - }, - Transport: models.EventsubTransport{ - Method: "webhook", - Callback: "null", - }, - CreatedAt: util.GetTimestamp().Format(time.RFC3339Nano), - }, - Event: models.HypeTrainEventBeginSubEvent{ - BroadcasterUserID: params.ToUserID, - BroadcasterUserLogin: params.ToUserName, - BroadcasterUserName: params.ToUserName, - Total: localTotal, - Progress: localProgress, - Goal: localGoal, - TopContributions: localTC, - LastContribution: localLC, - StartedAtTimestamp: StartedAtTime.Format(time.RFC3339Nano), - ExpiresAtTimestamp: ExpiresAtTime.Format(time.RFC3339Nano), - }, - } - - event, err = json.Marshal(body) - if err != nil { - return events.MockEventResponse{}, err - } - - default: - return events.MockEventResponse{}, nil - } - - return events.MockEventResponse{ - ID: params.ID, - JSON: event, - FromUser: params.FromUserID, - ToUser: params.ToUserID, - }, nil -} - -func (e Event) ValidTransport(t string) bool { - return transportsSupported[t] -} - -func (e Event) ValidTrigger(t string) bool { - for _, ts := range triggerSupported { - if ts == t { - return true - } - } - return false -} -func (e Event) GetTopic(transport string, trigger string) string { - return triggerMapping[transport][trigger] -} \ No newline at end of file diff --git a/internal/events/types/hype_train/hype_train_begin_event_test.go b/internal/events/types/hype_train/hype_train_begin_event_test.go deleted file mode 100644 index 972c7e8d..00000000 --- a/internal/events/types/hype_train/hype_train_begin_event_test.go +++ /dev/null @@ -1,70 +0,0 @@ -// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. -// SPDX-License-Identifier: Apache-2.0 -package hype_train_begin - -import ( - "encoding/json" - "testing" - - "github.com/twitchdev/twitch-cli/internal/events" - "github.com/twitchdev/twitch-cli/internal/models" - "github.com/twitchdev/twitch-cli/internal/util" -) - - -var toUser = "4567" - -func TestEventSub(t *testing.T) { - a := util.SetupTestEnv(t) - - params := *&events.MockEventParameters{ - ToUserID: toUser, - Transport: models.TransportEventSub, - Trigger: "hype-train-begin", - } - - r, err := Event{}.GenerateEvent(params) - a.Nil(err) - - var body models.HypeTrainEventBeginSubResponse - err = json.Unmarshal(r.JSON, &body) - a.Nil(err) - - a.Equal("channel.hype_train.begin", body.Subscription.Type, "Expected event type %v, got %v", "channel.hype_train.begin", body.Subscription.Type) - a.Equal(toUser, body.Event.BroadcasterUserID, "Expected to user %v, got %v", toUser, body.Event.BroadcasterUserID) - -} - -func TestFakeTransport(t *testing.T) { - a := util.SetupTestEnv(t) - - params := *&events.MockEventParameters{ - ToUserID: toUser, - Transport: "fake_transport", - Trigger: "hype-train-begin", - } - - r, err := Event{}.GenerateEvent(params) - a.Nil(err) - a.Empty(r) -} -func TestValidTrigger(t *testing.T) { - a := util.SetupTestEnv(t) - - r := Event{}.ValidTrigger("hype-train-begin") - a.Equal(true, r) -} - -func TestValidTransport(t *testing.T) { - a := util.SetupTestEnv(t) - - r := Event{}.ValidTransport(models.TransportWebSub) - a.Equal(false, r) - - r = Event{}.ValidTransport(models.TransportEventSub) - a.Equal(true, r) -} - -func TestGetTopic(t *testing.T) { - a := util.SetupTestEnv(t) -} \ No newline at end of file diff --git a/internal/events/types/hype_train/hype_train_end_event.go b/internal/events/types/hype_train/hype_train_end_event.go deleted file mode 100644 index 4a9de55e..00000000 --- a/internal/events/types/hype_train/hype_train_end_event.go +++ /dev/null @@ -1,103 +0,0 @@ -// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. -// SPDX-License-Identifier: Apache-2.0 -package hype_train_end - -import( - "encoding/json" - "time" - - "github.com/twitchdev/twitch-cli/internal/events" - "github.com/twitchdev/twitch-cli/internal/models" - "github.com/twitchdev/twitch-cli/internal/util" -) - -var transportsSupported = map[string]bool{ - models.TransportWebSub: false, - models.TransportEventSub: true, -} - -var triggerSupported = []string{"hype-train-end"} - -var triggerMapping = map[string]map[string]string{ - models.TransportEventSub: { - "hype-train-end": "channel.hype_train.end", - }, -} - -type Event struct{} - -func (e Event) GenerateEvent(params events.MockEventParameters) (events.MockEventResponse, error) { - var event []byte - var err error - - //Some values - StartedAtTime := util.GetTimestamp() - localTotal := util.RandomViewerCount() - localLevel := util.RandomViewerCount()%4 - localRandomUser2 := util.RandomUserID() - localRandomUser3 := util.RandomUserID() - localTC := []models.ContributionData{{TotalContribution: util.RandomViewerCount(), TypeOfContribution: util.RandomType(), UserWhoMadeContribution: localRandomUser2, UserNameWhoMadeContribution: localRandomUser2, UserLoginWhoMadeContribution: localRandomUser2},{TotalContribution: util.RandomViewerCount(), TypeOfContribution: util.RandomType(), UserWhoMadeContribution: localRandomUser3, UserNameWhoMadeContribution: localRandomUser3, UserLoginWhoMadeContribution: localRandomUser3}} - EndedAtTime := util.GetTimestamp() - CooldownAtTime := util.GetTimestamp() - - switch params.Transport { - case models.TransportEventSub: - body := *&models.EventsubResponse{ - Subscription: models.EventsubSubscription{ - ID: params.ID, - Status: "enabled", - Type: triggerMapping[params.Transport][params.Trigger], - Version: "1.0", - Condition: models.EventsubCondition{ - BroadcasterUserID: params.ToUserID, - }, - Transport: models.EventsubTransport{ - Method: "webhook", - Callback: "null", - }, - CreatedAt: util.GetTimestamp().Format(time.RFC3339Nano), - }, - Event: models.HypeTrainEventEndSubEvent{ - BroadcasterUserID: params.ToUserID, - BroadcasterUserLogin: params.ToUserName, - BroadcasterUserName: params.ToUserName, - Level: localLevel, - Total: localTotal, - TopContributions: localTC, - StartedAtTimestamp: StartedAtTime.Format(time.RFC3339Nano), - EndedAtTimestamp: EndedAtTime.Format(time.RFC3339Nano), - CooldownEndsAtTimestamp: CooldownAtTime.Format(time.RFC3339Nano), - }, - } - - event, err = json.Marshal(body) - if err != nil { - return events.MockEventResponse{}, err - } - default: - return events.MockEventResponse{}, nil - } - - return events.MockEventResponse{ - ID: params.ID, - JSON: event, - FromUser: params.FromUserID, - ToUser: params.ToUserID, - }, nil -} - -func (e Event) ValidTransport(t string) bool { - return transportsSupported[t] -} - -func (e Event) ValidTrigger(t string) bool { - for _, ts := range triggerSupported { - if ts == t { - return true - } - } - return false -} -func (e Event) GetTopic(transport string, trigger string) string { - return triggerMapping[transport][trigger] -} \ No newline at end of file diff --git a/internal/events/types/hype_train/hype_train_end_event_test.go b/internal/events/types/hype_train/hype_train_end_event_test.go deleted file mode 100644 index 0dbdfe05..00000000 --- a/internal/events/types/hype_train/hype_train_end_event_test.go +++ /dev/null @@ -1,69 +0,0 @@ -// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. -// SPDX-License-Identifier: Apache-2.0 -package hype_train_end - -import ( - "encoding/json" - "testing" - - "github.com/twitchdev/twitch-cli/internal/events" - "github.com/twitchdev/twitch-cli/internal/models" - "github.com/twitchdev/twitch-cli/internal/util" -) - - -var toUser = "4567" - -func TestEventSub(t *testing.T) { - a := util.SetupTestEnv(t) - - params := *&events.MockEventParameters{ - ToUserID: toUser, - Transport: models.TransportEventSub, - Trigger: "hype-train-end", - } - - r, err := Event{}.GenerateEvent(params) - a.Nil(err) - - var body models.HypeTrainEventEndSubResponse - err = json.Unmarshal(r.JSON, &body) - a.Nil(err) - - a.Equal("channel.hype_train.end", body.Subscription.Type, "Expected event type %v, got %v", "channel.hype_train.end", body.Subscription.Type) - a.Equal(toUser, body.Event.BroadcasterUserID, "Expected to user %v, got %v", toUser, body.Event.BroadcasterUserID) - -} -func TestFakeTransport(t *testing.T) { - a := util.SetupTestEnv(t) - - params := *&events.MockEventParameters{ - ToUserID: toUser, - Transport: "fake_transport", - Trigger: "hype-train-end", - } - - r, err := Event{}.GenerateEvent(params) - a.Nil(err) - a.Empty(r) -} -func TestValidTrigger(t *testing.T) { - a := util.SetupTestEnv(t) - - r := Event{}.ValidTrigger("hype-train-end") - a.Equal(true, r) -} - -func TestValidTransport(t *testing.T) { - a := util.SetupTestEnv(t) - - r := Event{}.ValidTransport(models.TransportWebSub) - a.Equal(false, r) - - r = Event{}.ValidTransport(models.TransportEventSub) - a.Equal(true, r) -} - -func TestGetTopic(t *testing.T) { - a := util.SetupTestEnv(t) -} \ No newline at end of file diff --git a/internal/events/types/hype_train/hype_train_event.go b/internal/events/types/hype_train/hype_train_event.go new file mode 100644 index 00000000..5b9a8185 --- /dev/null +++ b/internal/events/types/hype_train/hype_train_event.go @@ -0,0 +1,224 @@ +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 +package hype_train + +import( + "encoding/json" + "time" + + "github.com/twitchdev/twitch-cli/internal/events" + "github.com/twitchdev/twitch-cli/internal/models" + "github.com/twitchdev/twitch-cli/internal/util" +) + +var transportsSupported = map[string]bool{ + models.TransportWebSub: true, + models.TransportEventSub: true, +} + +var triggerSupported = []string{"hype-train-begin", "hype-train-progress", "hype-train-end"} + +var triggerMapping = map[string]map[string]string{ + models.TransportWebSub: { + "hype-train-progress": "hypetrain.progression", + }, + models.TransportEventSub: { + "hype-train-progress": "channel.hype_train.progress", + "hype-train-begin": "channel.hype_train.begin", + "hype-train-end": "channel.hype_train.end", + }, +} + +type Event struct{} + +func (e Event) GenerateEvent(params events.MockEventParameters) (events.MockEventResponse, error) { + var event []byte + var err error + + + + //Local variables which will be used for the trigger params below + localTotal := util.RandomViewerCount() + localGoal := util.RandomViewerCount() + localProgress := (localTotal/localGoal) + localRandomUser1 := util.RandomUserID() + localRandomUser2 := util.RandomUserID() + localRandomUser3 := util.RandomUserID() + localLC := models.ContributionData{TotalContribution: util.RandomViewerCount(), TypeOfContribution: util.RandomType(), UserWhoMadeContribution: localRandomUser1, UserNameWhoMadeContribution: localRandomUser1, UserLoginWhoMadeContribution: localRandomUser1} + localTC := []models.ContributionData{{TotalContribution: util.RandomViewerCount(), TypeOfContribution: util.RandomType(), UserWhoMadeContribution: localRandomUser2, UserNameWhoMadeContribution: localRandomUser2, UserLoginWhoMadeContribution: localRandomUser2},{TotalContribution: util.RandomViewerCount(), TypeOfContribution: util.RandomType(), UserWhoMadeContribution: localRandomUser3, UserNameWhoMadeContribution: localRandomUser3, UserLoginWhoMadeContribution: localRandomUser3}} + + + if params.Trigger == "hype-train-begin" { + switch params.Transport { + case models.TransportEventSub: + body := *&models.EventsubResponse{ + Subscription: models.EventsubSubscription{ + ID: params.ID, + Status: "enabled", + Type: triggerMapping[params.Transport][params.Trigger], + Version: "1.0", + Condition: models.EventsubCondition{ + BroadcasterUserID: params.ToUserID, + }, + Transport: models.EventsubTransport{ + Method: "webhook", + Callback: "null", + }, + CreatedAt: util.GetTimestamp().Format(time.RFC3339Nano), + }, + Event: models.HypeTrainEventSubEvent{ + BroadcasterUserID: params.ToUserID, + BroadcasterUserLogin: params.ToUserName, + BroadcasterUserName: params.ToUserName, + Total: localTotal, + Progress: localProgress, + Goal: localGoal, + TopContributions: localTC, + LastContribution: localLC, + StartedAtTimestamp: util.GetTimestamp().Format(time.RFC3339Nano), + ExpiresAtTimestamp: util.GetTimestamp().Format(time.RFC3339Nano), + }, + } + + event, err = json.Marshal(body) + if err != nil { + return events.MockEventResponse{}, err + } + + default: + return events.MockEventResponse{}, nil + } + } + + if params.Trigger == "hype-train-end" { + switch params.Transport { + case models.TransportEventSub: + body := *&models.EventsubResponse{ + Subscription: models.EventsubSubscription{ + ID: params.ID, + Status: "enabled", + Type: triggerMapping[params.Transport][params.Trigger], + Version: "1.0", + Condition: models.EventsubCondition{ + BroadcasterUserID: params.ToUserID, + }, + Transport: models.EventsubTransport{ + Method: "webhook", + Callback: "null", + }, + CreatedAt: util.GetTimestamp().Format(time.RFC3339Nano), + }, + Event: models.HypeTrainEventSubEvent{ + BroadcasterUserID: params.ToUserID, + BroadcasterUserLogin: params.ToUserName, + BroadcasterUserName: params.ToUserName, + Level: util.RandomViewerCount()%4, + Total: localTotal, + TopContributions: localTC, + StartedAtTimestamp: util.GetTimestamp().Format(time.RFC3339Nano), + EndedAtTimestamp: util.GetTimestamp().Format(time.RFC3339Nano), + CooldownEndsAtTimestamp: util.GetTimestamp().Format(time.RFC3339Nano), + }, + } + + event, err = json.Marshal(body) + if err != nil { + return events.MockEventResponse{}, err + } + default: + return events.MockEventResponse{}, nil + } + } + + if params.Trigger == "hype-train-progress" { + switch params.Transport { + case models.TransportEventSub: + body := *&models.EventsubResponse{ + Subscription: models.EventsubSubscription{ + ID: params.ID, + Status: "enabled", + Type: triggerMapping[params.Transport][params.Trigger], + Version: "1", + Condition: models.EventsubCondition{ + BroadcasterUserID: params.ToUserID, + }, + Transport: models.EventsubTransport{ + Method: "webhook", + Callback: "null", + }, + CreatedAt: util.GetTimestamp().Format(time.RFC3339Nano), + }, + Event: models.HypeTrainEventSubEvent{ + BroadcasterUserID: params.ToUserID, + BroadcasterUserLogin: params.ToUserName, + BroadcasterUserName: params.ToUserName, + Level: util.RandomViewerCount()%4, + Total: localTotal, + Progress: localProgress, + Goal: localGoal, + TopContributions: localTC, + LastContribution: localLC, + StartedAtTimestamp: util.GetTimestamp().Format(time.RFC3339Nano), + ExpiresAtTimestamp: util.GetTimestamp().Format(time.RFC3339Nano), + }, + } + + event, err = json.Marshal(body) + if err != nil { + return events.MockEventResponse{}, err + } + case models.TransportWebSub: + body := *&models.HypeTrainWebSubResponse{ + Data: []models.HypeTrainWebSubEvent{ + { + ID: params.ID, + EventType: triggerMapping[params.Transport][params.Trigger], + EventTimestamp: util.GetTimestamp().Format(time.RFC3339), + Version: "1.0", + EventData: models.HypeTrainWebsubEventData{ + BroadcasterID: params.ToUserID, + CooldownEndTimestamp: util.GetTimestamp().Format(time.RFC3339), + ExpiresAtTimestamp: util.GetTimestamp().Format(time.RFC3339), + Goal: localGoal, + Id: util.RandomGUID(), + LastContribution: localLC, + Level: util.RandomViewerCount()%4, + StartedAtTimestamp: util.GetTimestamp().Format(time.RFC3339), + TopContributions: localTC, + Total: localTotal, + }, + }, + }, + } + event, err = json.Marshal(body) + if err != nil { + return events.MockEventResponse{}, err + } + + default: + return events.MockEventResponse{}, nil + } + } + return events.MockEventResponse{ + ID: params.ID, + JSON: event, + FromUser: params.FromUserID, + ToUser: params.ToUserID, + }, nil +} + +func (e Event) ValidTransport(t string) bool { + return transportsSupported[t] +} + +func (e Event) ValidTrigger(t string) bool { + for _, ts := range triggerSupported { + if ts == t { + return true + } + } + return false +} +func (e Event) GetTopic(transport string, trigger string) string { + return triggerMapping[transport][trigger] +} \ No newline at end of file diff --git a/internal/events/types/hype_train/hype_train_progress_event_test.go b/internal/events/types/hype_train/hype_train_event_test.go similarity index 66% rename from internal/events/types/hype_train/hype_train_progress_event_test.go rename to internal/events/types/hype_train/hype_train_event_test.go index b3883e69..edc7de0d 100644 --- a/internal/events/types/hype_train/hype_train_progress_event_test.go +++ b/internal/events/types/hype_train/hype_train_event_test.go @@ -1,6 +1,6 @@ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. // SPDX-License-Identifier: Apache-2.0 -package hype_train_progress +package hype_train import ( "encoding/json" @@ -16,6 +16,23 @@ var toUser = "4567" func TestEventSub(t *testing.T) { a := util.SetupTestEnv(t) + params := *&events.MockEventParameters{ + FromUserID: fromUser, + ToUserID: toUser, + Transport: models.TransportEventSub, + Trigger: "hype-train-begin", + } + + r, err := Event{}.GenerateEvent(params) + a.Nil(err) + + var body models.HypeTrainEventProgressSubResponse + err = json.Unmarshal(r.JSON, &body) + a.Nil(err) + + a.Equal("channel.hype_train.begin", body.Subscription.Type, "Expected event type %v, got %v", "channel.hype_train.begin", body.Subscription.Type) + a.Equal(toUser, body.Event.BroadcasterUserID, "Expected to user %v, got %v", toUser, body.Event.BroadcasterUserID) + params := *&events.MockEventParameters{ FromUserID: fromUser, ToUserID: toUser, @@ -32,6 +49,23 @@ func TestEventSub(t *testing.T) { a.Equal("channel.hype_train.progress", body.Subscription.Type, "Expected event type %v, got %v", "channel.hype_train.progress", body.Subscription.Type) a.Equal(toUser, body.Event.BroadcasterUserID, "Expected to user %v, got %v", toUser, body.Event.BroadcasterUserID) + + params := *&events.MockEventParameters{ + FromUserID: fromUser, + ToUserID: toUser, + Transport: models.TransportEventSub, + Trigger: "hype-train-end", + } + + r, err := Event{}.GenerateEvent(params) + a.Nil(err) + + var body models.HypeTrainEventProgressSubResponse + err = json.Unmarshal(r.JSON, &body) + a.Nil(err) + + a.Equal("channel.hype_train.end", body.Subscription.Type, "Expected event type %v, got %v", "channel.hype_train.end", body.Subscription.Type) + a.Equal(toUser, body.Event.BroadcasterUserID, "Expected to user %v, got %v", toUser, body.Event.BroadcasterUserID) } @@ -76,9 +110,15 @@ func TestFakeTransport(t *testing.T) { func TestValidTrigger(t *testing.T) { a := util.SetupTestEnv(t) + r := Event{}.ValidTrigger("hype-train-begin") + a.Equal(true, r) + r := Event{}.ValidTrigger("hype-train-progress") a.Equal(true, r) + r := Event{}.ValidTrigger("hype-train-end") + a.Equal(true, r) + } func TestValidTransport(t *testing.T) { diff --git a/internal/events/types/hype_train/hype_train_progress_event.go b/internal/events/types/hype_train/hype_train_progress_event.go deleted file mode 100644 index cb6c6af8..00000000 --- a/internal/events/types/hype_train/hype_train_progress_event.go +++ /dev/null @@ -1,140 +0,0 @@ -// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. -// SPDX-License-Identifier: Apache-2.0 -package hype_train_progress - -import( - "encoding/json" - "time" - - "github.com/twitchdev/twitch-cli/internal/events" - "github.com/twitchdev/twitch-cli/internal/models" - "github.com/twitchdev/twitch-cli/internal/util" -) - -var transportsSupported = map[string]bool{ - models.TransportWebSub: true, - models.TransportEventSub: true, -} - -var triggerSupported = []string{"hype-train-begin", "hype-train-progress", "hype-train-end"} - -var triggerMapping = map[string]map[string]string{ - models.TransportWebSub: { - "hype-train-progress": "hypetrain.progression", - }, - models.TransportEventSub: { - "hype-train-progress": "channel.hype_train.progress", - }, -} - -type Event struct{} - -func (e Event) GenerateEvent(params events.MockEventParameters) (events.MockEventResponse, error) { - var event []byte - var err error - - //Some values - StartedAtTime := util.GetTimestamp() - localTotal := util.RandomViewerCount() - localLevel := util.RandomViewerCount()%4 - localGoal := util.RandomViewerCount() - localProgress := (localTotal/localGoal) - localRandomUser1 := util.RandomUserID() - localRandomUser2 := util.RandomUserID() - localRandomUser3 := util.RandomUserID() - localLC := models.ContributionData{TotalContribution: util.RandomViewerCount(), TypeOfContribution: util.RandomType(), UserWhoMadeContribution: localRandomUser1, UserNameWhoMadeContribution: localRandomUser1, UserLoginWhoMadeContribution: localRandomUser1} - localTC := []models.ContributionData{{TotalContribution: util.RandomViewerCount(), TypeOfContribution: util.RandomType(), UserWhoMadeContribution: localRandomUser2, UserNameWhoMadeContribution: localRandomUser2, UserLoginWhoMadeContribution: localRandomUser2},{TotalContribution: util.RandomViewerCount(), TypeOfContribution: util.RandomType(), UserWhoMadeContribution: localRandomUser3, UserNameWhoMadeContribution: localRandomUser3, UserLoginWhoMadeContribution: localRandomUser3}} - ExpiresAtTime := util.GetTimestamp() - CooldownAtTime := util.GetTimestamp() - - switch params.Transport { - case models.TransportEventSub: - body := *&models.EventsubResponse{ - Subscription: models.EventsubSubscription{ - ID: params.ID, - Status: "enabled", - Type: triggerMapping[params.Transport][params.Trigger], - Version: "1", - Condition: models.EventsubCondition{ - BroadcasterUserID: params.ToUserID, - }, - Transport: models.EventsubTransport{ - Method: "webhook", - Callback: "null", - }, - CreatedAt: util.GetTimestamp().Format(time.RFC3339Nano), - }, - Event: models.HypeTrainEventProgressSubEvent{ - BroadcasterUserID: params.ToUserID, - BroadcasterUserLogin: params.ToUserName, - BroadcasterUserName: params.ToUserName, - Level: localLevel, - Total: localTotal, - Progress: localProgress, - Goal: localGoal, - TopContributions: localTC, - LastContribution: localLC, - StartedAtTimestamp: StartedAtTime.Format(time.RFC3339Nano), - ExpiresAtTimestamp: ExpiresAtTime.Format(time.RFC3339Nano), - }, - } - - event, err = json.Marshal(body) - if err != nil { - return events.MockEventResponse{}, err - } - case models.TransportWebSub: - body := *&models.HypeTrainWebSubResponse{ - Data: []models.HypeTrainWebSubEvent{ - { - ID: params.ID, - EventType: triggerMapping[params.Transport][params.Trigger], - EventTimestamp: util.GetTimestamp().Format(time.RFC3339), - Version: "1.0", - EventData: models.HypeTrainWebsubEventData{ - BroadcasterID: params.ToUserID, - CooldownEndTimestamp: CooldownAtTime.Format(time.RFC3339), - ExpiresAtTimestamp: ExpiresAtTime.Format(time.RFC3339), - Goal: localGoal, - Id: util.RandomGUID(), - LastContribution: localLC, - Level: localLevel, - StartedAtTimestamp: StartedAtTime.String(), - TopContributions: localTC, - Total: localTotal, - }, - }, - }, - } - event, err = json.Marshal(body) - if err != nil { - return events.MockEventResponse{}, err - } - - default: - return events.MockEventResponse{}, nil - } - - return events.MockEventResponse{ - ID: params.ID, - JSON: event, - FromUser: params.FromUserID, - ToUser: params.ToUserID, - }, nil -} - -func (e Event) ValidTransport(t string) bool { - return transportsSupported[t] -} - -func (e Event) ValidTrigger(t string) bool { - for _, ts := range triggerSupported { - if ts == t { - return true - } - } - return false -} -func (e Event) GetTopic(transport string, trigger string) string { - return triggerMapping[transport][trigger] -} \ No newline at end of file diff --git a/internal/events/types/types.go b/internal/events/types/types.go index 07165cfb..49676ad4 100644 --- a/internal/events/types/types.go +++ b/internal/events/types/types.go @@ -12,9 +12,7 @@ import ( "github.com/twitchdev/twitch-cli/internal/events/types/cheer" "github.com/twitchdev/twitch-cli/internal/events/types/extension_transaction" "github.com/twitchdev/twitch-cli/internal/events/types/follow" - "github.com/twitchdev/twitch-cli/internal/events/types/hype_train_begin" - "github.com/twitchdev/twitch-cli/internal/events/types/hype_train_end" - "github.com/twitchdev/twitch-cli/internal/events/types/hype_train_progress" + "github.com/twitchdev/twitch-cli/internal/events/types/hype_train" "github.com/twitchdev/twitch-cli/internal/events/types/moderator_change" "github.com/twitchdev/twitch-cli/internal/events/types/raid" "github.com/twitchdev/twitch-cli/internal/events/types/stream_change" @@ -32,9 +30,7 @@ func All() []events.MockEvent { cheer.Event{}, extension_transaction.Event{}, follow.Event{}, - hype_train_begin.Event{}, - hype_train_end.Event{}, - hype_train_progress.Event{}, + hype_train.Event{}, raid.Event{}, subscribe.Event{}, stream_change.Event{}, diff --git a/internal/models/hype_train.go b/internal/models/hype_train.go index 2441559e..3c80cf40 100644 --- a/internal/models/hype_train.go +++ b/internal/models/hype_train.go @@ -2,6 +2,13 @@ // SPDX-License-Identifier: Apache-2.0 package models +type ContributionData struct{ + TotalContribution int64 `json:"total"` + TypeOfContribution string `json:"type"` + UserWhoMadeContribution string `json:"user_id"` + UserNameWhoMadeContribution string `json:"user_name"` + UserLoginWhoMadeContribution string `json:"user_login"` +} type HypeTrainWebSubEvent struct { ID string `json:"id"` @@ -12,80 +19,39 @@ type HypeTrainWebSubEvent struct { } type HypeTrainWebsubEventData struct { - BroadcasterID string `json:"broadcaster_id"` - CooldownEndTimestamp string `json:"cooldown_end_time"` - ExpiresAtTimestamp string `json:"expires_at"` - Goal int64 `json:"goal"` - Id string `json:"id"` - LastContribution ContributionData `json:"last_contribution"` - Level int64 `json:"level"` - StartedAtTimestamp string `json:"started_at"` - TopContributions []ContributionData `json:"top_contributions"` - Total int64 `json:"total"` -} - -type ContributionData struct{ - TotalContribution int64 `json:"total"` - TypeOfContribution string `json:"type"` - UserWhoMadeContribution string `json:"user_id"` - UserNameWhoMadeContribution string `json:"user_name"` - UserLoginWhoMadeContribution string `json:"user_login"` + BroadcasterID string `json:"broadcaster_id,omitempty"` + CooldownEndTimestamp string `json:"cooldown_end_time,omitempty"` + ExpiresAtTimestamp string `json:"expires_at,omitempty"` + Goal int64 `json:"goal,omitempty"` + Id string `json:"id,omitempty"` + LastContribution ContributionData `json:"last_contribution,omitempty"` + Level int64 `json:"level,omitempty"` + StartedAtTimestamp string `json:"started_at,omitempty"` + TopContributions []ContributionData `json:"top_contributions,omitempty"` + Total int64 `json:"total,omitempty"` } type HypeTrainWebSubResponse struct { Data []HypeTrainWebSubEvent `json:"data"` } -type HypeTrainEventBeginSubResponse struct { - Subscription EventsubSubscription `json:"subscription"` - Event HypeTrainEventBeginSubEvent `json:"event"` -} - -type HypeTrainEventProgressSubResponse struct { - Subscription EventsubSubscription `json:"subscription"` - Event HypeTrainEventProgressSubEvent `json:"event"` -} - -type HypeTrainEventEndSubResponse struct { +type HypeTrainEventSubResponse struct { Subscription EventsubSubscription `json:"subscription"` - Event HypeTrainEventEndSubEvent `json:"event"` -} - -type HypeTrainEventBeginSubEvent struct { - BroadcasterUserID string `json:"broadcaster_user_id"` - BroadcasterUserLogin string `json:"broadcaster_user_login"` - BroadcasterUserName string `json:"broadcaster_user_name"` - Total int64 `json:"total"` - Progress int64 `json:"progress"` - Goal int64 `json:"goal"` - TopContributions []ContributionData `json:"top_contributions"` - LastContribution ContributionData `json:"last_contribution"` - StartedAtTimestamp string `json:"started_at"` - ExpiresAtTimestamp string `json:"expires_at"` -} - -type HypeTrainEventProgressSubEvent struct { - BroadcasterUserID string `json:"broadcaster_user_id"` - BroadcasterUserLogin string `json:"broadcaster_user_login"` - BroadcasterUserName string `json:"broadcaster_user_name"` - Level int64 `json:"level"` - Total int64 `json:"total"` - Progress int64 `json:"progress"` - Goal int64 `json:"goal"` - TopContributions []ContributionData `json:"top_contributions"` - LastContribution ContributionData `json:"last_contribution"` - StartedAtTimestamp string `json:"started_at"` - ExpiresAtTimestamp string `json:"expires_at"` -} - -type HypeTrainEventEndSubEvent struct { - BroadcasterUserID string `json:"broadcaster_user_id"` - BroadcasterUserLogin string `json:"broadcaster_user_login"` - BroadcasterUserName string `json:"broadcaster_user_name"` - Level int64 `json:"level"` - Total int64 `json:"total"` - TopContributions []ContributionData `json:"top_contributions"` - StartedAtTimestamp string `json:"started_at"` - EndedAtTimestamp string `json:"ended_at"` - CooldownEndsAtTimestamp string `json:"cooldown_ends_at"` + Event HypeTrainEventSubEvent `json:"event"` +} + +type HypeTrainEventSubEvent struct { + BroadcasterUserID string `json:"broadcaster_user_id,omitempty"` + BroadcasterUserLogin string `json:"broadcaster_user_login,omitempty"` + BroadcasterUserName string `json:"broadcaster_user_name,omitempty"` + Level int64 `json:"level,omitempty"` + Total int64 `json:"total,omitempty"` + Progress int64 `json:"progress,omitempty"` + Goal int64 `json:"goal,omitempty"` + TopContributions []ContributionData `json:"top_contributions,omitempty"` + LastContribution ContributionData `json:"last_contribution,omitempty"` + StartedAtTimestamp string `json:"started_at,omitempty"` + ExpiresAtTimestamp string `json:"expires_at,omitempty"` + EndedAtTimestamp string `json:"ended_at,omitempty"` + CooldownEndsAtTimestamp string `json:"cooldown_ends_at,omitempty"` } \ No newline at end of file From b3ced8c358617d6a2ba9ebc3e73767467d68371b Mon Sep 17 00:00:00 2001 From: KainTheReaper Date: Wed, 17 Mar 2021 14:49:11 -0700 Subject: [PATCH 4/7] minor fixes to test case Should prevent test case failure --- .../types/hype_train/hype_train_event_test.go | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/internal/events/types/hype_train/hype_train_event_test.go b/internal/events/types/hype_train/hype_train_event_test.go index edc7de0d..f16cde46 100644 --- a/internal/events/types/hype_train/hype_train_event_test.go +++ b/internal/events/types/hype_train/hype_train_event_test.go @@ -33,34 +33,34 @@ func TestEventSub(t *testing.T) { a.Equal("channel.hype_train.begin", body.Subscription.Type, "Expected event type %v, got %v", "channel.hype_train.begin", body.Subscription.Type) a.Equal(toUser, body.Event.BroadcasterUserID, "Expected to user %v, got %v", toUser, body.Event.BroadcasterUserID) - params := *&events.MockEventParameters{ + params = *&events.MockEventParameters{ FromUserID: fromUser, ToUserID: toUser, Transport: models.TransportEventSub, Trigger: "hype-train-progress", } - r, err := Event{}.GenerateEvent(params) + r, err = Event{}.GenerateEvent(params) a.Nil(err) - var body models.HypeTrainEventProgressSubResponse + //var body models.HypeTrainEventProgressSubResponse err = json.Unmarshal(r.JSON, &body) a.Nil(err) a.Equal("channel.hype_train.progress", body.Subscription.Type, "Expected event type %v, got %v", "channel.hype_train.progress", body.Subscription.Type) a.Equal(toUser, body.Event.BroadcasterUserID, "Expected to user %v, got %v", toUser, body.Event.BroadcasterUserID) - params := *&events.MockEventParameters{ + params = *&events.MockEventParameters{ FromUserID: fromUser, ToUserID: toUser, Transport: models.TransportEventSub, Trigger: "hype-train-end", } - r, err := Event{}.GenerateEvent(params) + r, err = Event{}.GenerateEvent(params) a.Nil(err) - var body models.HypeTrainEventProgressSubResponse + //var body models.HypeTrainEventProgressSubResponse err = json.Unmarshal(r.JSON, &body) a.Nil(err) @@ -113,10 +113,10 @@ func TestValidTrigger(t *testing.T) { r := Event{}.ValidTrigger("hype-train-begin") a.Equal(true, r) - r := Event{}.ValidTrigger("hype-train-progress") + r = Event{}.ValidTrigger("hype-train-progress") a.Equal(true, r) - r := Event{}.ValidTrigger("hype-train-end") + r = Event{}.ValidTrigger("hype-train-end") a.Equal(true, r) } From e7bf0dfb2b7547b052dabf9360bffeabc29c2ddc Mon Sep 17 00:00:00 2001 From: KainTheReaper Date: Wed, 17 Mar 2021 14:52:46 -0700 Subject: [PATCH 5/7] test case fix 2 a go go Should be good for real this time --- internal/events/types/hype_train/hype_train_event_test.go | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/internal/events/types/hype_train/hype_train_event_test.go b/internal/events/types/hype_train/hype_train_event_test.go index f16cde46..80a4a268 100644 --- a/internal/events/types/hype_train/hype_train_event_test.go +++ b/internal/events/types/hype_train/hype_train_event_test.go @@ -17,7 +17,6 @@ func TestEventSub(t *testing.T) { a := util.SetupTestEnv(t) params := *&events.MockEventParameters{ - FromUserID: fromUser, ToUserID: toUser, Transport: models.TransportEventSub, Trigger: "hype-train-begin", @@ -26,7 +25,7 @@ func TestEventSub(t *testing.T) { r, err := Event{}.GenerateEvent(params) a.Nil(err) - var body models.HypeTrainEventProgressSubResponse + var body models.HypeTrainEventSubResponse err = json.Unmarshal(r.JSON, &body) a.Nil(err) @@ -34,7 +33,6 @@ func TestEventSub(t *testing.T) { a.Equal(toUser, body.Event.BroadcasterUserID, "Expected to user %v, got %v", toUser, body.Event.BroadcasterUserID) params = *&events.MockEventParameters{ - FromUserID: fromUser, ToUserID: toUser, Transport: models.TransportEventSub, Trigger: "hype-train-progress", @@ -51,7 +49,6 @@ func TestEventSub(t *testing.T) { a.Equal(toUser, body.Event.BroadcasterUserID, "Expected to user %v, got %v", toUser, body.Event.BroadcasterUserID) params = *&events.MockEventParameters{ - FromUserID: fromUser, ToUserID: toUser, Transport: models.TransportEventSub, Trigger: "hype-train-end", From 199680815761b47763e4f26d043572c32676162d Mon Sep 17 00:00:00 2001 From: KainTheReaper Date: Wed, 17 Mar 2021 16:16:07 -0700 Subject: [PATCH 6/7] Streamlined hype Hype train slightly refactored again Minor changes to hype_train model ~~This train keeps on rolling~~ --- .../types/hype_train/hype_train_event.go | 252 +++++++----------- internal/models/hype_train.go | 20 +- 2 files changed, 105 insertions(+), 167 deletions(-) diff --git a/internal/events/types/hype_train/hype_train_event.go b/internal/events/types/hype_train/hype_train_event.go index 5b9a8185..6c065c20 100644 --- a/internal/events/types/hype_train/hype_train_event.go +++ b/internal/events/types/hype_train/hype_train_event.go @@ -1,203 +1,143 @@ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. // SPDX-License-Identifier: Apache-2.0 package hype_train - -import( +import ( "encoding/json" "time" - "github.com/twitchdev/twitch-cli/internal/events" "github.com/twitchdev/twitch-cli/internal/models" "github.com/twitchdev/twitch-cli/internal/util" ) - var transportsSupported = map[string]bool{ models.TransportWebSub: true, models.TransportEventSub: true, } - var triggerSupported = []string{"hype-train-begin", "hype-train-progress", "hype-train-end"} - var triggerMapping = map[string]map[string]string{ models.TransportWebSub: { - "hype-train-progress": "hypetrain.progression", + "hype-train-progress": "hypetrain.progression", + "hype-train-begin": "hypetrain.progression", + "hype-train-end": "hypetrain.progression", }, models.TransportEventSub: { - "hype-train-progress": "channel.hype_train.progress", - "hype-train-begin": "channel.hype_train.begin", - "hype-train-end": "channel.hype_train.end", + "hype-train-progress": "channel.hype_train.progress", + "hype-train-begin": "channel.hype_train.begin", + "hype-train-end": "channel.hype_train.end", }, } - type Event struct{} - func (e Event) GenerateEvent(params events.MockEventParameters) (events.MockEventResponse, error) { var event []byte var err error - - - + lastUser := util.RandomUserID() + lastTotal := util.RandomViewerCount() + lastType := util.RandomType() //Local variables which will be used for the trigger params below localTotal := util.RandomViewerCount() localGoal := util.RandomViewerCount() - localProgress := (localTotal/localGoal) - localRandomUser1 := util.RandomUserID() - localRandomUser2 := util.RandomUserID() - localRandomUser3 := util.RandomUserID() - localLC := models.ContributionData{TotalContribution: util.RandomViewerCount(), TypeOfContribution: util.RandomType(), UserWhoMadeContribution: localRandomUser1, UserNameWhoMadeContribution: localRandomUser1, UserLoginWhoMadeContribution: localRandomUser1} - localTC := []models.ContributionData{{TotalContribution: util.RandomViewerCount(), TypeOfContribution: util.RandomType(), UserWhoMadeContribution: localRandomUser2, UserNameWhoMadeContribution: localRandomUser2, UserLoginWhoMadeContribution: localRandomUser2},{TotalContribution: util.RandomViewerCount(), TypeOfContribution: util.RandomType(), UserWhoMadeContribution: localRandomUser3, UserNameWhoMadeContribution: localRandomUser3, UserLoginWhoMadeContribution: localRandomUser3}} - - - if params.Trigger == "hype-train-begin" { - switch params.Transport { - case models.TransportEventSub: - body := *&models.EventsubResponse{ - Subscription: models.EventsubSubscription{ - ID: params.ID, - Status: "enabled", - Type: triggerMapping[params.Transport][params.Trigger], - Version: "1.0", - Condition: models.EventsubCondition{ - BroadcasterUserID: params.ToUserID, - }, - Transport: models.EventsubTransport{ - Method: "webhook", - Callback: "null", - }, - CreatedAt: util.GetTimestamp().Format(time.RFC3339Nano), - }, - Event: models.HypeTrainEventSubEvent{ - BroadcasterUserID: params.ToUserID, - BroadcasterUserLogin: params.ToUserName, - BroadcasterUserName: params.ToUserName, - Total: localTotal, - Progress: localProgress, - Goal: localGoal, - TopContributions: localTC, - LastContribution: localLC, - StartedAtTimestamp: util.GetTimestamp().Format(time.RFC3339Nano), - ExpiresAtTimestamp: util.GetTimestamp().Format(time.RFC3339Nano), + localProgress := (localTotal / localGoal) + switch params.Transport { + case models.TransportEventSub: + body := *&models.HypeTrainEventSubResponse{ + Subscription: models.EventsubSubscription{ + ID: params.ID, + Status: "enabled", + Type: triggerMapping[params.Transport][params.Trigger], + Version: "1.0", + Condition: models.EventsubCondition{ + BroadcasterUserID: params.ToUserID, }, - } - - event, err = json.Marshal(body) - if err != nil { - return events.MockEventResponse{}, err - } - - default: - return events.MockEventResponse{}, nil - } - } - - if params.Trigger == "hype-train-end" { - switch params.Transport { - case models.TransportEventSub: - body := *&models.EventsubResponse{ - Subscription: models.EventsubSubscription{ - ID: params.ID, - Status: "enabled", - Type: triggerMapping[params.Transport][params.Trigger], - Version: "1.0", - Condition: models.EventsubCondition{ - BroadcasterUserID: params.ToUserID, + Transport: models.EventsubTransport{ + Method: "webhook", + Callback: "null", + }, + CreatedAt: util.GetTimestamp().Format(time.RFC3339Nano), + }, + Event: models.HypeTrainEventSubEvent{ + BroadcasterUserID: params.ToUserID, + BroadcasterUserLogin: params.ToUserName, + BroadcasterUserName: params.ToUserName, + Total: util.RandomViewerCount(), + Progress: localProgress, + Goal: localGoal, + TopContributions: []models.ContributionData{ + { + TotalContribution: util.RandomViewerCount(), + TypeOfContribution: util.RandomType(), + UserWhoMadeContribution: util.RandomUserID(), + UserNameWhoMadeContribution: "cli_user1", + UserLoginWhoMadeContribution: "cli_user1", }, - Transport: models.EventsubTransport{ - Method: "webhook", - Callback: "null", + { + TotalContribution: lastTotal, + TypeOfContribution: lastType, + UserWhoMadeContribution: lastUser, + UserNameWhoMadeContribution: "cli_user2", + UserLoginWhoMadeContribution: "cli_user2", }, - CreatedAt: util.GetTimestamp().Format(time.RFC3339Nano), }, - Event: models.HypeTrainEventSubEvent{ - BroadcasterUserID: params.ToUserID, - BroadcasterUserLogin: params.ToUserName, - BroadcasterUserName: params.ToUserName, - Level: util.RandomViewerCount()%4, - Total: localTotal, - TopContributions: localTC, - StartedAtTimestamp: util.GetTimestamp().Format(time.RFC3339Nano), - EndedAtTimestamp: util.GetTimestamp().Format(time.RFC3339Nano), - CooldownEndsAtTimestamp: util.GetTimestamp().Format(time.RFC3339Nano), + LastContribution: models.ContributionData{ + TotalContribution: util.RandomViewerCount(), + TypeOfContribution: util.RandomType(), + UserWhoMadeContribution: lastUser, + UserNameWhoMadeContribution: "cli_user2", + UserLoginWhoMadeContribution: "cli_user2", }, - } - - event, err = json.Marshal(body) - if err != nil { - return events.MockEventResponse{}, err - } - default: - return events.MockEventResponse{}, nil + StartedAtTimestamp: util.GetTimestamp().Format(time.RFC3339Nano), + ExpiresAtTimestamp: util.GetTimestamp().Format(time.RFC3339Nano), + }, + } + if triggerMapping[params.Transport][params.Trigger] == "hype-train-end " { + body.Event.CooldownEndsAtTimestamp = util.GetTimestamp().Format(time.RFC3339Nano) } - } - - if params.Trigger == "hype-train-progress" { - switch params.Transport { - case models.TransportEventSub: - body := *&models.EventsubResponse{ - Subscription: models.EventsubSubscription{ - ID: params.ID, - Status: "enabled", - Type: triggerMapping[params.Transport][params.Trigger], - Version: "1", - Condition: models.EventsubCondition{ - BroadcasterUserID: params.ToUserID, - }, - Transport: models.EventsubTransport{ - Method: "webhook", - Callback: "null", - }, - CreatedAt: util.GetTimestamp().Format(time.RFC3339Nano), - }, - Event: models.HypeTrainEventSubEvent{ - BroadcasterUserID: params.ToUserID, - BroadcasterUserLogin: params.ToUserName, - BroadcasterUserName: params.ToUserName, - Level: util.RandomViewerCount()%4, - Total: localTotal, - Progress: localProgress, - Goal: localGoal, - TopContributions: localTC, - LastContribution: localLC, - StartedAtTimestamp: util.GetTimestamp().Format(time.RFC3339Nano), - ExpiresAtTimestamp: util.GetTimestamp().Format(time.RFC3339Nano), - }, - } - event, err = json.Marshal(body) if err != nil { return events.MockEventResponse{}, err } - case models.TransportWebSub: - body := *&models.HypeTrainWebSubResponse{ - Data: []models.HypeTrainWebSubEvent{ - { - ID: params.ID, - EventType: triggerMapping[params.Transport][params.Trigger], - EventTimestamp: util.GetTimestamp().Format(time.RFC3339), - Version: "1.0", - EventData: models.HypeTrainWebsubEventData{ - BroadcasterID: params.ToUserID, - CooldownEndTimestamp: util.GetTimestamp().Format(time.RFC3339), - ExpiresAtTimestamp: util.GetTimestamp().Format(time.RFC3339), - Goal: localGoal, - Id: util.RandomGUID(), - LastContribution: localLC, - Level: util.RandomViewerCount()%4, - StartedAtTimestamp: util.GetTimestamp().Format(time.RFC3339), - TopContributions: localTC, - Total: localTotal, + case models.TransportWebSub: + body := *&models.HypeTrainWebSubResponse{ + Data: []models.HypeTrainWebSubEvent{ + { + ID: params.ID, + EventType: triggerMapping[params.Transport][params.Trigger], + EventTimestamp: util.GetTimestamp().Format(time.RFC3339), + Version: "1.0", + EventData: models.HypeTrainWebsubEventData{ + BroadcasterID: params.ToUserID, + CooldownEndTimestamp: util.GetTimestamp().Format(time.RFC3339), + ExpiresAtTimestamp: util.GetTimestamp().Format(time.RFC3339), + Goal: localGoal, + Id: util.RandomGUID(), + LastContribution: models.ContributionData{ + TotalContribution: lastTotal, + TypeOfContribution: lastType, + UserWhoMadeContribution: lastUser, }, + Level: util.RandomViewerCount() % 4, + StartedAtTimestamp: util.GetTimestamp().Format(time.RFC3339), + TopContributions: []models.ContributionData{ + { + TotalContribution: lastTotal, + TypeOfContribution: lastType, + UserWhoMadeContribution: lastUser, + }, + { + TotalContribution: util.RandomViewerCount(), + TypeOfContribution: util.RandomType(), + UserWhoMadeContribution: util.RandomUserID(), + }, + }, + Total: localTotal, }, }, - } + }, + } event, err = json.Marshal(body) if err != nil { return events.MockEventResponse{}, err } - - default: + default: return events.MockEventResponse{}, nil - } } return events.MockEventResponse{ ID: params.ID, @@ -206,11 +146,9 @@ func (e Event) GenerateEvent(params events.MockEventParameters) (events.MockEven ToUser: params.ToUserID, }, nil } - func (e Event) ValidTransport(t string) bool { return transportsSupported[t] } - func (e Event) ValidTrigger(t string) bool { for _, ts := range triggerSupported { if ts == t { diff --git a/internal/models/hype_train.go b/internal/models/hype_train.go index 3c80cf40..aa5073c2 100644 --- a/internal/models/hype_train.go +++ b/internal/models/hype_train.go @@ -19,16 +19,16 @@ type HypeTrainWebSubEvent struct { } type HypeTrainWebsubEventData struct { - BroadcasterID string `json:"broadcaster_id,omitempty"` - CooldownEndTimestamp string `json:"cooldown_end_time,omitempty"` - ExpiresAtTimestamp string `json:"expires_at,omitempty"` + BroadcasterID string `json:"broadcaster_id"` + CooldownEndTimestamp string `json:"cooldown_end_time"` + ExpiresAtTimestamp string `json:"expires_at"` Goal int64 `json:"goal,omitempty"` Id string `json:"id,omitempty"` LastContribution ContributionData `json:"last_contribution,omitempty"` Level int64 `json:"level,omitempty"` StartedAtTimestamp string `json:"started_at,omitempty"` - TopContributions []ContributionData `json:"top_contributions,omitempty"` - Total int64 `json:"total,omitempty"` + TopContributions []ContributionData `json:"top_contributions"` + Total int64 `json:"total"` } type HypeTrainWebSubResponse struct { @@ -41,14 +41,14 @@ type HypeTrainEventSubResponse struct { } type HypeTrainEventSubEvent struct { - BroadcasterUserID string `json:"broadcaster_user_id,omitempty"` - BroadcasterUserLogin string `json:"broadcaster_user_login,omitempty"` - BroadcasterUserName string `json:"broadcaster_user_name,omitempty"` + BroadcasterUserID string `json:"broadcaster_user_id"` + BroadcasterUserLogin string `json:"broadcaster_user_login"` + BroadcasterUserName string `json:"broadcaster_user_name"` Level int64 `json:"level,omitempty"` - Total int64 `json:"total,omitempty"` + Total int64 `json:"total"` Progress int64 `json:"progress,omitempty"` Goal int64 `json:"goal,omitempty"` - TopContributions []ContributionData `json:"top_contributions,omitempty"` + TopContributions []ContributionData `json:"top_contributions"` LastContribution ContributionData `json:"last_contribution,omitempty"` StartedAtTimestamp string `json:"started_at,omitempty"` ExpiresAtTimestamp string `json:"expires_at,omitempty"` From ca79191da7d4a1e09da4066e57d1e8b145577141 Mon Sep 17 00:00:00 2001 From: lleadbet Date: Thu, 18 Mar 2021 09:39:20 -0700 Subject: [PATCH 7/7] updating model --- .../types/hype_train/hype_train_event.go | 30 +++++---- internal/models/hype_train.go | 65 ++++++++++--------- 2 files changed, 51 insertions(+), 44 deletions(-) diff --git a/internal/events/types/hype_train/hype_train_event.go b/internal/events/types/hype_train/hype_train_event.go index 6c065c20..54c7479f 100644 --- a/internal/events/types/hype_train/hype_train_event.go +++ b/internal/events/types/hype_train/hype_train_event.go @@ -1,13 +1,16 @@ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. // SPDX-License-Identifier: Apache-2.0 package hype_train + import ( "encoding/json" "time" + "github.com/twitchdev/twitch-cli/internal/events" "github.com/twitchdev/twitch-cli/internal/models" "github.com/twitchdev/twitch-cli/internal/util" ) + var transportsSupported = map[string]bool{ models.TransportWebSub: true, models.TransportEventSub: true, @@ -25,7 +28,9 @@ var triggerMapping = map[string]map[string]string{ "hype-train-end": "channel.hype_train.end", }, } + type Event struct{} + func (e Event) GenerateEvent(params events.MockEventParameters) (events.MockEventResponse, error) { var event []byte var err error @@ -36,6 +41,7 @@ func (e Event) GenerateEvent(params events.MockEventParameters) (events.MockEven localTotal := util.RandomViewerCount() localGoal := util.RandomViewerCount() localProgress := (localTotal / localGoal) + switch params.Transport { case models.TransportEventSub: body := *&models.HypeTrainEventSubResponse{ @@ -77,8 +83,8 @@ func (e Event) GenerateEvent(params events.MockEventParameters) (events.MockEven }, }, LastContribution: models.ContributionData{ - TotalContribution: util.RandomViewerCount(), - TypeOfContribution: util.RandomType(), + TotalContribution: lastTotal, + TypeOfContribution: lastType, UserWhoMadeContribution: lastUser, UserNameWhoMadeContribution: "cli_user2", UserLoginWhoMadeContribution: "cli_user2", @@ -109,22 +115,22 @@ func (e Event) GenerateEvent(params events.MockEventParameters) (events.MockEven Goal: localGoal, Id: util.RandomGUID(), LastContribution: models.ContributionData{ - TotalContribution: lastTotal, - TypeOfContribution: lastType, - UserWhoMadeContribution: lastUser, + TotalContribution: lastTotal, + TypeOfContribution: lastType, + WebSubUser: lastUser, }, Level: util.RandomViewerCount() % 4, StartedAtTimestamp: util.GetTimestamp().Format(time.RFC3339), TopContributions: []models.ContributionData{ { - TotalContribution: lastTotal, - TypeOfContribution: lastType, - UserWhoMadeContribution: lastUser, + TotalContribution: lastTotal, + TypeOfContribution: lastType, + WebSubUser: lastUser, }, { - TotalContribution: util.RandomViewerCount(), - TypeOfContribution: util.RandomType(), - UserWhoMadeContribution: util.RandomUserID(), + TotalContribution: util.RandomViewerCount(), + TypeOfContribution: util.RandomType(), + WebSubUser: util.RandomUserID(), }, }, Total: localTotal, @@ -159,4 +165,4 @@ func (e Event) ValidTrigger(t string) bool { } func (e Event) GetTopic(transport string, trigger string) string { return triggerMapping[transport][trigger] -} \ No newline at end of file +} diff --git a/internal/models/hype_train.go b/internal/models/hype_train.go index aa5073c2..3ba1c24b 100644 --- a/internal/models/hype_train.go +++ b/internal/models/hype_train.go @@ -2,12 +2,13 @@ // SPDX-License-Identifier: Apache-2.0 package models -type ContributionData struct{ - TotalContribution int64 `json:"total"` - TypeOfContribution string `json:"type"` - UserWhoMadeContribution string `json:"user_id"` - UserNameWhoMadeContribution string `json:"user_name"` - UserLoginWhoMadeContribution string `json:"user_login"` +type ContributionData struct { + TotalContribution int64 `json:"total"` + TypeOfContribution string `json:"type"` + WebSubUser string `json:"user,omitempty"` + UserWhoMadeContribution string `json:"user_id,omitempty"` + UserNameWhoMadeContribution string `json:"user_name,omitempty"` + UserLoginWhoMadeContribution string `json:"user_login,omitempty"` } type HypeTrainWebSubEvent struct { @@ -19,16 +20,16 @@ type HypeTrainWebSubEvent struct { } type HypeTrainWebsubEventData struct { - BroadcasterID string `json:"broadcaster_id"` - CooldownEndTimestamp string `json:"cooldown_end_time"` - ExpiresAtTimestamp string `json:"expires_at"` - Goal int64 `json:"goal,omitempty"` - Id string `json:"id,omitempty"` - LastContribution ContributionData `json:"last_contribution,omitempty"` - Level int64 `json:"level,omitempty"` - StartedAtTimestamp string `json:"started_at,omitempty"` - TopContributions []ContributionData `json:"top_contributions"` - Total int64 `json:"total"` + BroadcasterID string `json:"broadcaster_id"` + CooldownEndTimestamp string `json:"cooldown_end_time"` + ExpiresAtTimestamp string `json:"expires_at"` + Goal int64 `json:"goal,omitempty"` + Id string `json:"id,omitempty"` + LastContribution ContributionData `json:"last_contribution,omitempty"` + Level int64 `json:"level,omitempty"` + StartedAtTimestamp string `json:"started_at,omitempty"` + TopContributions []ContributionData `json:"top_contributions"` + Total int64 `json:"total"` } type HypeTrainWebSubResponse struct { @@ -36,22 +37,22 @@ type HypeTrainWebSubResponse struct { } type HypeTrainEventSubResponse struct { - Subscription EventsubSubscription `json:"subscription"` - Event HypeTrainEventSubEvent `json:"event"` + Subscription EventsubSubscription `json:"subscription"` + Event HypeTrainEventSubEvent `json:"event"` } type HypeTrainEventSubEvent struct { - BroadcasterUserID string `json:"broadcaster_user_id"` - BroadcasterUserLogin string `json:"broadcaster_user_login"` - BroadcasterUserName string `json:"broadcaster_user_name"` - Level int64 `json:"level,omitempty"` - Total int64 `json:"total"` - Progress int64 `json:"progress,omitempty"` - Goal int64 `json:"goal,omitempty"` - TopContributions []ContributionData `json:"top_contributions"` - LastContribution ContributionData `json:"last_contribution,omitempty"` - StartedAtTimestamp string `json:"started_at,omitempty"` - ExpiresAtTimestamp string `json:"expires_at,omitempty"` - EndedAtTimestamp string `json:"ended_at,omitempty"` - CooldownEndsAtTimestamp string `json:"cooldown_ends_at,omitempty"` -} \ No newline at end of file + BroadcasterUserID string `json:"broadcaster_user_id"` + BroadcasterUserLogin string `json:"broadcaster_user_login"` + BroadcasterUserName string `json:"broadcaster_user_name"` + Level int64 `json:"level,omitempty"` + Total int64 `json:"total"` + Progress int64 `json:"progress,omitempty"` + Goal int64 `json:"goal,omitempty"` + TopContributions []ContributionData `json:"top_contributions"` + LastContribution ContributionData `json:"last_contribution,omitempty"` + StartedAtTimestamp string `json:"started_at,omitempty"` + ExpiresAtTimestamp string `json:"expires_at,omitempty"` + EndedAtTimestamp string `json:"ended_at,omitempty"` + CooldownEndsAtTimestamp string `json:"cooldown_ends_at,omitempty"` +}