-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #47 from twitchdev/hypetrain
Hype Train second merge attempt
- Loading branch information
Showing
6 changed files
with
396 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
136
internal/events/types/hype_train/hype_train_event_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.