chore: add cast commands for testing purpose
This commit is contained in:
@ -19,8 +19,8 @@ func TestAppModuleWithMemoryRepository(t *testing.T) {
|
||||
module.ContextModuleFactory(),
|
||||
module.ConsoleModuleFactory(),
|
||||
appModule.ModuleFactory(NewRepository(
|
||||
func(ctx context.Context, id app.ID) (string, error) {
|
||||
return fmt.Sprintf("http//%s.example.com", id), nil
|
||||
func(ctx context.Context, id app.ID, from string) (string, error) {
|
||||
return fmt.Sprintf("http//%s.example.com?from=%s", id, from), nil
|
||||
},
|
||||
&app.Manifest{
|
||||
ID: "dummy1.arcad.app",
|
||||
|
@ -8,7 +8,7 @@ import (
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
type GetURLFunc func(context.Context, app.ID) (string, error)
|
||||
type GetURLFunc func(context.Context, app.ID, string) (string, error)
|
||||
|
||||
type Repository struct {
|
||||
getURL GetURLFunc
|
||||
@ -16,8 +16,8 @@ type Repository struct {
|
||||
}
|
||||
|
||||
// GetURL implements app.Repository
|
||||
func (r *Repository) GetURL(ctx context.Context, id app.ID) (string, error) {
|
||||
url, err := r.getURL(ctx, id)
|
||||
func (r *Repository) GetURL(ctx context.Context, id app.ID, from string) (string, error) {
|
||||
url, err := r.getURL(ctx, id, from)
|
||||
if err != nil {
|
||||
return "", errors.WithStack(err)
|
||||
}
|
||||
|
@ -86,7 +86,12 @@ func (m *Module) getURL(call goja.FunctionCall, rt *goja.Runtime) goja.Value {
|
||||
ctx := util.AssertContext(call.Argument(0), rt)
|
||||
appID := assertAppID(call.Argument(1), rt)
|
||||
|
||||
url, err := m.repository.GetURL(ctx, appID)
|
||||
var from string
|
||||
if len(call.Arguments) > 2 {
|
||||
from = util.AssertString(call.Argument(2), rt)
|
||||
}
|
||||
|
||||
url, err := m.repository.GetURL(ctx, appID, from)
|
||||
if err != nil {
|
||||
panic(rt.ToValue(errors.WithStack(err)))
|
||||
}
|
||||
|
@ -9,5 +9,5 @@ import (
|
||||
type Repository interface {
|
||||
List(context.Context) ([]*app.Manifest, error)
|
||||
Get(context.Context, app.ID) (*app.Manifest, error)
|
||||
GetURL(context.Context, app.ID) (string, error)
|
||||
GetURL(context.Context, app.ID, string) (string, error)
|
||||
}
|
||||
|
@ -39,7 +39,7 @@ const (
|
||||
)
|
||||
|
||||
func getDeviceClientByUUID(ctx context.Context, uuid string) (*cast.Client, error) {
|
||||
device, err := findDeviceByUUID(ctx, uuid)
|
||||
device, err := FindDeviceByUUID(ctx, uuid)
|
||||
if err != nil {
|
||||
return nil, errors.WithStack(err)
|
||||
}
|
||||
@ -49,7 +49,7 @@ func getDeviceClientByUUID(ctx context.Context, uuid string) (*cast.Client, erro
|
||||
return client, nil
|
||||
}
|
||||
|
||||
func findDeviceByUUID(ctx context.Context, uuid string) (*Device, error) {
|
||||
func FindDeviceByUUID(ctx context.Context, uuid string) (*Device, error) {
|
||||
service := discovery.NewService(ctx)
|
||||
defer service.Stop()
|
||||
|
||||
@ -83,7 +83,7 @@ LOOP:
|
||||
return nil, errors.WithStack(ErrDeviceNotFound)
|
||||
}
|
||||
|
||||
func findDevices(ctx context.Context) ([]*Device, error) {
|
||||
func FindDevices(ctx context.Context) ([]*Device, error) {
|
||||
service := discovery.NewService(ctx)
|
||||
defer service.Stop()
|
||||
|
||||
@ -124,7 +124,7 @@ LOOP:
|
||||
return devices, nil
|
||||
}
|
||||
|
||||
func loadURL(ctx context.Context, deviceUUID string, url string) error {
|
||||
func LoadURL(ctx context.Context, deviceUUID string, url string) error {
|
||||
client, err := getDeviceClientByUUID(ctx, deviceUUID)
|
||||
if err != nil {
|
||||
return errors.WithStack(err)
|
||||
@ -153,7 +153,7 @@ func isLoadURLContextExceeded(err error) bool {
|
||||
return err.Error() == "Failed to send load command: context deadline exceeded"
|
||||
}
|
||||
|
||||
func stopCast(ctx context.Context, deviceUUID string) error {
|
||||
func StopCast(ctx context.Context, deviceUUID string) error {
|
||||
client, err := getDeviceClientByUUID(ctx, deviceUUID)
|
||||
if err != nil {
|
||||
return errors.WithStack(err)
|
||||
|
@ -26,7 +26,7 @@ func TestCastLoadURL(t *testing.T) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||
defer cancel()
|
||||
|
||||
devices, err := findDevices(ctx)
|
||||
devices, err := FindDevices(ctx)
|
||||
if err != nil {
|
||||
t.Error(errors.WithStack(err))
|
||||
}
|
||||
@ -40,7 +40,7 @@ func TestCastLoadURL(t *testing.T) {
|
||||
ctx, cancel2 := context.WithTimeout(context.Background(), 15*time.Second)
|
||||
defer cancel2()
|
||||
|
||||
if err := loadURL(ctx, dev.UUID, "https://go.dev"); err != nil {
|
||||
if err := LoadURL(ctx, dev.UUID, "https://go.dev"); err != nil {
|
||||
t.Error(errors.WithStack(err))
|
||||
}
|
||||
|
||||
@ -57,7 +57,7 @@ func TestCastLoadURL(t *testing.T) {
|
||||
ctx, cancel4 := context.WithTimeout(context.Background(), 15*time.Second)
|
||||
defer cancel4()
|
||||
|
||||
if err := stopCast(ctx, dev.UUID); err != nil {
|
||||
if err := StopCast(ctx, dev.UUID); err != nil {
|
||||
t.Error(errors.WithStack(err))
|
||||
}
|
||||
}
|
||||
|
@ -72,7 +72,7 @@ func (m *Module) refreshDevices(call goja.FunctionCall, rt *goja.Runtime) goja.V
|
||||
ctx, cancel := context.WithTimeout(context.Background(), timeout)
|
||||
defer cancel()
|
||||
|
||||
devices, err := findDevices(ctx)
|
||||
devices, err := FindDevices(ctx)
|
||||
if err != nil && !errors.Is(err, context.DeadlineExceeded) {
|
||||
err = errors.WithStack(err)
|
||||
logger.Error(ctx, "error refreshing casting devices list", logger.E(errors.WithStack(err)))
|
||||
@ -128,7 +128,7 @@ func (m *Module) loadUrl(call goja.FunctionCall, rt *goja.Runtime) goja.Value {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), timeout)
|
||||
defer cancel()
|
||||
|
||||
err := loadURL(ctx, deviceUUID, url)
|
||||
err := LoadURL(ctx, deviceUUID, url)
|
||||
if err != nil {
|
||||
err = errors.WithStack(err)
|
||||
logger.Error(ctx, "error while casting url", logger.E(err))
|
||||
@ -166,7 +166,7 @@ func (m *Module) stopCast(call goja.FunctionCall, rt *goja.Runtime) goja.Value {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), timeout)
|
||||
defer cancel()
|
||||
|
||||
err := stopCast(ctx, deviceUUID)
|
||||
err := StopCast(ctx, deviceUUID)
|
||||
if err != nil {
|
||||
err = errors.WithStack(err)
|
||||
logger.Error(ctx, "error while quitting casting device app", logger.E(errors.WithStack(err)))
|
||||
|
Reference in New Issue
Block a user