edge/pkg/module/cast/cast_test.go

92 lines
2.1 KiB
Go

package cast_test
import (
"context"
"fmt"
"os"
"testing"
"time"
"cdr.dev/slog"
"forge.cadoles.com/arcad/edge/pkg/module/cast"
"github.com/davecgh/go-spew/spew"
"github.com/pkg/errors"
"gitlab.com/wpetit/goweb/logger"
// Register casting device supported types
_ "forge.cadoles.com/arcad/edge/pkg/module/cast/arcast"
_ "forge.cadoles.com/arcad/edge/pkg/module/cast/chromecast"
)
func TestCastLoadURL(t *testing.T) {
t.Parallel()
if os.Getenv("TEST_CAST_MODULE") != "yes" {
t.Skip("Test skipped. Set environment variable TEST_CAST_MODULE=yes to run.")
return
}
if testing.Verbose() {
logger.SetLevel(slog.LevelDebug)
}
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
devices, err := cast.ListDevices(ctx, true)
if err != nil {
t.Error(errors.WithStack(err))
}
t.Logf("DEVICES: %s", spew.Sdump(devices))
if e, g := 1, len(devices); e > g {
t.Fatalf("len(devices): expected 'value >= %v', got '%v'", e, g)
}
devices, err = cast.ListDevices(ctx, false)
if err != nil {
t.Error(errors.WithStack(err))
}
t.Logf("CACHED DEVICES: %s", spew.Sdump(devices))
if e, g := 1, len(devices); e > g {
t.Fatalf("len(devices): expected 'value >= %v', got '%v'", e, g)
}
for _, device := range devices {
testName := fmt.Sprintf("%s(%s)", device.DeviceType(), device.DeviceID())
func(device cast.Device) {
t.Run(testName, func(t *testing.T) {
t.Parallel()
ctx, cancel2 := context.WithTimeout(context.Background(), 15*time.Second)
defer cancel2()
if err := cast.LoadURL(ctx, device.DeviceID(), "https://go.dev"); err != nil {
t.Error(errors.WithStack(err))
}
ctx, cancel3 := context.WithTimeout(context.Background(), 15*time.Second)
defer cancel3()
status, err := cast.GetStatus(ctx, device.DeviceID())
if err != nil {
t.Error(errors.WithStack(err))
}
t.Logf("DEVICE STATUS: %s", spew.Sdump(status))
ctx, cancel4 := context.WithTimeout(context.Background(), 15*time.Second)
defer cancel4()
if err := cast.StopCast(ctx, device.DeviceID()); err != nil {
t.Error(errors.WithStack(err))
}
})
}(device)
}
}