edge/pkg/module/cast/module_test.go

91 lines
1.6 KiB
Go
Raw Normal View History

2023-02-17 10:38:45 +01:00
package cast
import (
"context"
2023-02-17 10:38:45 +01:00
"os"
"testing"
"time"
"cdr.dev/slog"
"forge.cadoles.com/arcad/edge/pkg/app"
"forge.cadoles.com/arcad/edge/pkg/module"
"github.com/davecgh/go-spew/spew"
"github.com/pkg/errors"
"gitlab.com/wpetit/goweb/logger"
)
func TestCastModule(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
}
2023-11-28 16:35:49 +01:00
if testing.Verbose() {
logger.SetLevel(slog.LevelDebug)
}
2023-02-17 10:38:45 +01:00
server := app.NewServer(
module.ConsoleModuleFactory(),
CastModuleFactory(),
)
2023-11-28 16:35:49 +01:00
script := "testdata/cast.js"
2023-02-17 10:38:45 +01:00
2023-11-28 16:35:49 +01:00
data, err := os.ReadFile(script)
if err != nil {
2023-02-17 10:38:45 +01:00
t.Fatal(err)
}
ctx := context.Background()
2023-11-28 16:35:49 +01:00
if err := server.Start(ctx, script, string(data)); err != nil {
2023-02-17 10:38:45 +01:00
t.Fatalf("%+v", errors.WithStack(err))
}
defer server.Stop()
time.Sleep(20 * time.Second)
}
func TestCastModuleRefreshDevices(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
}
2023-11-28 16:35:49 +01:00
if testing.Verbose() {
logger.SetLevel(slog.LevelDebug)
}
2023-02-17 10:38:45 +01:00
server := app.NewServer(
module.ConsoleModuleFactory(),
CastModuleFactory(),
)
2023-11-28 16:35:49 +01:00
script := "testdata/refresh_devices.js"
2023-02-17 10:38:45 +01:00
2023-11-28 16:35:49 +01:00
data, err := os.ReadFile(script)
if err != nil {
2023-02-17 10:38:45 +01:00
t.Fatal(err)
}
ctx := context.Background()
2023-11-28 16:35:49 +01:00
if err := server.Start(ctx, script, string(data)); err != nil {
2023-02-17 10:38:45 +01:00
t.Fatalf("%+v", errors.WithStack(err))
}
defer server.Stop()
result, err := server.ExecFuncByName(context.Background(), "refreshDevices")
2023-02-17 10:38:45 +01:00
if err != nil {
t.Error(errors.WithStack(err))
}
2023-11-28 16:35:49 +01:00
spew.Dump(result)
2023-02-17 10:38:45 +01:00
}