Skip to content

Commit

Permalink
Merge pull request #47 from twitchdev/hypetrain
Browse files Browse the repository at this point in the history
Hype Train second merge attempt
  • Loading branch information
MonstersOnParade authored Mar 18, 2021
2 parents 90a3173 + ca79191 commit a754c72
Show file tree
Hide file tree
Showing 6 changed files with 396 additions and 15 deletions.
33 changes: 18 additions & 15 deletions internal/events/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
168 changes: 168 additions & 0 deletions internal/events/types/hype_train/hype_train_event.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
// 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",
"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",
},
}

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)

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,
},
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",
},
{
TotalContribution: lastTotal,
TypeOfContribution: lastType,
UserWhoMadeContribution: lastUser,
UserNameWhoMadeContribution: "cli_user2",
UserLoginWhoMadeContribution: "cli_user2",
},
},
LastContribution: models.ContributionData{
TotalContribution: lastTotal,
TypeOfContribution: lastType,
UserWhoMadeContribution: lastUser,
UserNameWhoMadeContribution: "cli_user2",
UserLoginWhoMadeContribution: "cli_user2",
},
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)
}
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: models.ContributionData{
TotalContribution: lastTotal,
TypeOfContribution: lastType,
WebSubUser: lastUser,
},
Level: util.RandomViewerCount() % 4,
StartedAtTimestamp: util.GetTimestamp().Format(time.RFC3339),
TopContributions: []models.ContributionData{
{
TotalContribution: lastTotal,
TypeOfContribution: lastType,
WebSubUser: lastUser,
},
{
TotalContribution: util.RandomViewerCount(),
TypeOfContribution: util.RandomType(),
WebSubUser: util.RandomUserID(),
},
},
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]
}
136 changes: 136 additions & 0 deletions internal/events/types/hype_train/hype_train_event_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
package hype_train

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.HypeTrainEventSubResponse
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{
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)

params = *&events.MockEventParameters{
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)

}

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-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) {
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)
}
2 changes: 2 additions & 0 deletions internal/events/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +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"
"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"
Expand All @@ -29,6 +30,7 @@ func All() []events.MockEvent {
cheer.Event{},
extension_transaction.Event{},
follow.Event{},
hype_train.Event{},
raid.Event{},
subscribe.Event{},
stream_change.Event{},
Expand Down
Loading

0 comments on commit a754c72

Please sign in to comment.