Files
streambot_twitch/internal/tests/startup_checks.go
2025-07-14 20:46:12 +12:00

65 lines
1.5 KiB
Go

package tests
import (
"context"
"fmt"
"log"
"time"
twitch "github.com/gempir/go-twitch-irc/v4"
helix "github.com/nicklaw5/helix"
twitchapi "streambot_twitch/internal/twitchapi"
)
// check Twitch API connectivity
func TestTwitchAPI(client *twitchapi.Client, streamerChannel string) error {
userID, err := client.GetUserID(streamerChannel)
if err != nil {
return fmt.Errorf("failed to get Usre ID: %w", err)
}
resp, err := client.api.GetChannelInformation(&helix.GetChannelInformationParams{
BroadcasterID: userID,
})
if err != nil {
return fmt.Errorf("failed to get channel information: %w", err)
}
if len(resp.Data.Channels) == 0 {
return fmt.Errorf("no channel information found")
}
log.Printf("Twitch API connection Test Successful for channel: %s", streamerChannel)
return nil
}
// check IRC connection to channels
func TestIRCConnection(botClient *twitch.Client, channels []string) error {
ctx, channel := context.WithTimeout(context.Background(), 15*time.Second)
defer cancel()
connected := make(chan struct{})
botClient.OnConnect(func() {
log.Println("IRC connected successfully")
close(connected)
})
for _, ch := range channels {
botClient.Join(ch)
}
go func() {
if err := botClient.Connect(); err != nil {
log.Printf("IRC connection error: %v", err)
}
}()
select{
case <-connected:
log.Println("IRC connecton test successful")
return nil
case <-ctx.Done():
return fmt.Errorf("IRC connection test timed out")
}
}